Skip to content

Commit

Permalink
geniuspaste: Add support for using the redirected URI as paste URI
Browse files Browse the repository at this point in the history
Most non-API-based pastebin services will redirect the client to the
newly created paste page.  It is then safer to use the redirect URI
than trying to parse some possibly complex HTML to extract the URI.
  • Loading branch information
b4n committed Jan 16, 2016
1 parent e052cc3 commit 649939a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 15 deletions.
14 changes: 12 additions & 2 deletions geniuspaste/README
Expand Up @@ -103,8 +103,18 @@ Each key in this section is a field, and each value that field's value.
*[parse]* section
+++++++++++++++++

The *parse* section defines the regular expression used to parse the raw
response from the pastebin service and build the final paste URL.
If the *parse* section is declared, it defines the regular expression used to
parse the raw response body from the pastebin service and build the final
paste URL.

If this section doesn't exist, the URI of the response itself is used, after
any possible redirection. This is usually good for non-API pastebin services,
where the server redirects the user to the paste page.

**Warning:** If the *parse* section exists, it will be used, no matter whether
any key is actually defined. This means that a ``[parse]`` line is enough to
enable response body parsing, and it will use the default *search* and
*replace* settings.

*search*
A regular expression (PCRE) pattern to match against the pastebin
Expand Down
34 changes: 21 additions & 13 deletions geniuspaste/src/geniuspaste.c
Expand Up @@ -544,7 +544,7 @@ static SoupMessage *pastebin_soup_message_new(const Pastebin *pastebin,
* or if the URL couldn't be found.
* @warning: it may return NULL even if @error is not set */
static gchar *pastebin_parse_response(const Pastebin *pastebin,
const gchar *response,
SoupMessage *msg,
GeanyDocument *doc,
const gchar *contents,
GError **error)
Expand All @@ -554,19 +554,27 @@ static gchar *pastebin_parse_response(const Pastebin *pastebin,
gchar *replace;

g_return_val_if_fail(pastebin != NULL, NULL);
g_return_val_if_fail(response != NULL, NULL);
g_return_val_if_fail(msg != NULL, NULL);

search = utils_get_setting_string(pastebin->config, PASTEBIN_GROUP_PARSE,
PASTEBIN_GROUP_PARSE_KEY_SEARCH,
"^[[:space:]]*(.+?)[[:space:]]*$");
replace = utils_get_setting_string(pastebin->config, PASTEBIN_GROUP_PARSE,
PASTEBIN_GROUP_PARSE_KEY_REPLACE, "\\1");
SETPTR(replace, expand_placeholders(replace, pastebin, doc, contents));
if (! g_key_file_has_group(pastebin->config, PASTEBIN_GROUP_PARSE))
{
/* by default, use the response URI (redirect) */
url = soup_uri_to_string(soup_message_get_uri(msg), FALSE);
}
else
{
search = utils_get_setting_string(pastebin->config, PASTEBIN_GROUP_PARSE,
PASTEBIN_GROUP_PARSE_KEY_SEARCH,
"^[[:space:]]*(.+?)[[:space:]]*$");
replace = utils_get_setting_string(pastebin->config, PASTEBIN_GROUP_PARSE,
PASTEBIN_GROUP_PARSE_KEY_REPLACE, "\\1");
SETPTR(replace, expand_placeholders(replace, pastebin, doc, contents));

url = regex_replace(search, response, replace, error);
url = regex_replace(search, msg->response_body->data, replace, error);

g_free(search);
g_free(replace);
g_free(search);
g_free(replace);
}

return url;
}
Expand Down Expand Up @@ -675,8 +683,8 @@ static void paste(GeanyDocument * doc, const gchar * website)
else
{
GError *err = NULL;
gchar *p_url = pastebin_parse_response(pastebin, msg->response_body->data,
doc, f_content, &err);
gchar *p_url = pastebin_parse_response(pastebin, msg, doc, f_content,
&err);

if (err || ! p_url)
{
Expand Down

0 comments on commit 649939a

Please sign in to comment.