static void do_hello_test (int n, gboolean extra, const char *uri) { GPtrArray *args; char *title_arg = NULL, *name_arg = NULL; char *str_stdout = NULL; debug_printf (1, "%2d. '%s' '%s'%s: ", n * 2 + (extra ? 2 : 1), tests[n].title ? tests[n].title : "(null)", tests[n].name ? tests[n].name : "(null)", extra ? " + extra" : ""); args = g_ptr_array_new (); g_ptr_array_add (args, "curl"); g_ptr_array_add (args, "-G"); if (tests[n].title) { title_arg = soup_form_encode ("title", tests[n].title, NULL); g_ptr_array_add (args, "-d"); g_ptr_array_add (args, title_arg); } if (tests[n].name) { name_arg = soup_form_encode ("n@me", tests[n].name, NULL); g_ptr_array_add (args, "-d"); g_ptr_array_add (args, name_arg); } if (extra) { g_ptr_array_add (args, "-d"); g_ptr_array_add (args, "extra=something"); } g_ptr_array_add (args, (char *)uri); g_ptr_array_add (args, NULL); if (g_spawn_sync (NULL, (char **)args->pdata, NULL, G_SPAWN_SEARCH_PATH | G_SPAWN_STDERR_TO_DEV_NULL, NULL, NULL, &str_stdout, NULL, NULL, NULL)) { if (str_stdout && !strcmp (str_stdout, tests[n].result)) debug_printf (1, "OK!\n"); else { debug_printf (1, "WRONG!\n"); debug_printf (1, " expected '%s', got '%s'\n", tests[n].result, str_stdout ? str_stdout : "(error)"); errors++; } g_free (str_stdout); } else { debug_printf (1, "ERROR!\n"); errors++; } g_ptr_array_free (args, TRUE); g_free (title_arg); g_free (name_arg); }
static void do_hello_test (int n, gboolean extra, const char *uri) { GPtrArray *args; char *title_arg = NULL, *name_arg = NULL; char *str_stdout = NULL; GError *error = NULL; debug_printf (1, "%2d. '%s' '%s'%s: ", n * 2 + (extra ? 2 : 1), tests[n].title ? tests[n].title : "(null)", tests[n].name ? tests[n].name : "(null)", extra ? " + extra" : ""); args = g_ptr_array_new (); g_ptr_array_add (args, "curl"); g_ptr_array_add (args, "--noproxy"); g_ptr_array_add (args, "*"); g_ptr_array_add (args, "-G"); if (tests[n].title) { title_arg = soup_form_encode ("title", tests[n].title, NULL); g_ptr_array_add (args, "-d"); g_ptr_array_add (args, title_arg); } if (tests[n].name) { name_arg = soup_form_encode ("n@me", tests[n].name, NULL); g_ptr_array_add (args, "-d"); g_ptr_array_add (args, name_arg); } if (extra) { g_ptr_array_add (args, "-d"); g_ptr_array_add (args, "extra=something"); } g_ptr_array_add (args, (char *)uri); g_ptr_array_add (args, NULL); if (g_spawn_sync (NULL, (char **)args->pdata, NULL, G_SPAWN_SEARCH_PATH | G_SPAWN_STDERR_TO_DEV_NULL, NULL, NULL, &str_stdout, NULL, NULL, &error)) { g_assert_cmpstr (str_stdout, ==, tests[n].result); g_free (str_stdout); } else {
static void paste(GeanyDocument * doc, const gchar * website) { SoupSession *session; SoupMessage *msg = NULL; gchar *f_content; gchar const *f_type; gchar *f_title; gchar *p_url; gchar *formdata = NULL; gchar *user_agent = NULL; gchar **tokens_array; const gchar *langs_supported_codepad[] = { "C", "C++", "D", "Haskell", "Lua", "OCaml", "PHP", "Perl", "Plain Text", "Python", "Ruby", "Scheme", "Tcl" }; const gchar *langs_supported_dpaste[] = { "Bash", "C", "CSS", "Diff", "Django/Jinja", "HTML", "IRC logs", "JavaScript", "PHP", "Python console session", "Python Traceback", "Python", "Python3", "Restructured Text", "SQL", "Text only" }; gint occ_position; gint i; guint status; gsize f_length; g_return_if_fail(doc && doc->is_valid); f_type = doc->file_type->name; if (doc->file_name == NULL) f_title = document_get_basename_for_display(doc, -1); else f_title = g_path_get_basename(doc->file_name); load_settings(); f_content = get_paste_text(doc, &f_length); if (f_content == NULL || f_content[0] == '\0') { dialogs_show_msgbox(GTK_MESSAGE_ERROR, _("Refusing to create blank paste")); return; } switch (website_selected) { case CODEPAD_ORG: for (i = 0; i < G_N_ELEMENTS(langs_supported_codepad); i++) { if (g_strcmp0(f_type, langs_supported_codepad[i]) == 0) break; else f_type = DEFAULT_TYPE_CODEPAD; } msg = soup_message_new("POST", website); formdata = soup_form_encode("lang", f_type, "code", f_content, "submit", "Submit", NULL); break; case TINYPASTE_COM: msg = soup_message_new("POST", website); formdata = soup_form_encode("paste", f_content, "title", f_title, "is_code", g_strcmp0(f_type, "None") == 0 ? "0" : "1", NULL); break; case DPASTE_DE: for (i = 0; i < G_N_ELEMENTS(langs_supported_dpaste); i++) { if (g_strcmp0(f_type, langs_supported_dpaste[i]) == 0) break; else f_type = DEFAULT_TYPE_DPASTE; } msg = soup_message_new("POST", website); /* apparently dpaste.de detects automatically the syntax of the * pasted code so 'lexer' should be unneeded */ formdata = soup_form_encode("content", f_content, "title", f_title, "lexer", f_type, NULL); break; case SPRUNGE_US: msg = soup_message_new("POST", website); formdata = soup_form_encode("sprunge", f_content, NULL); break; case PASTEBIN_GEANY_ORG: msg = soup_message_new("POST", website); formdata = soup_form_encode("content", f_content, "author", author_name, "title", f_title, "lexer", f_type, NULL); break; } g_free(f_content); user_agent = g_strconcat(PLUGIN_NAME, " ", PLUGIN_VERSION, " / Geany ", GEANY_VERSION, NULL); session = soup_session_async_new_with_options(SOUP_SESSION_USER_AGENT, user_agent, NULL); g_free(user_agent); soup_message_set_request(msg, "application/x-www-form-urlencoded", SOUP_MEMORY_TAKE, formdata, strlen(formdata)); status = soup_session_send_message(session, msg); p_url = g_strdup(msg->response_body->data); g_object_unref(session); g_object_unref(msg); if(status == SOUP_STATUS_OK) { /* * codepad.org doesn't return only the url of the new snippet pasted * but an html page. This minimal parser will get the bare url. */ if (website_selected == CODEPAD_ORG) { tokens_array = g_strsplit(p_url, "<a href=\"", 0); /* cuts the string when it finds the first occurrence of '/' * It shoud work even if codepad would change its url. */ SETPTR(p_url, g_strdup(tokens_array[5])); occ_position = indexof(tokens_array[5], '\"'); g_strfreev(tokens_array); if(occ_position != -1) { p_url[occ_position] = '\0'; } else { dialogs_show_msgbox(GTK_MESSAGE_ERROR, _("Unable to paste the code on codepad.org\n" "Retry or select another pastebin.")); g_free(p_url); return; } } else if(website_selected == TINYPASTE_COM) { /* tinypaste.com returns a XML response which looks * like this: * * <?xml version="1.0" encoding="utf-8"?> * <result> * <response>xxxxx</response> * </result> */ tokens_array = g_strsplit_set(p_url, "<>", 0); SETPTR(p_url, g_strdup_printf("http://%s/%s", websites[TINYPASTE_COM], tokens_array[6])); g_strfreev(tokens_array); } else if(website_selected == DPASTE_DE) { SETPTR(p_url, g_strndup(p_url + 1, strlen(p_url) - 2)); } else if(website_selected == SPRUNGE_US) { /* in order to enable the syntax highlightning on sprunge.us * it is necessary to append at the returned url a question * mark '?' followed by the file type. * * e.g. sprunge.us/xxxx?c */ gchar *ft_tmp = g_ascii_strdown(f_type, -1); g_strstrip(p_url); SETPTR(p_url, g_strdup_printf("%s?%s", p_url, ft_tmp)); g_free(ft_tmp); } if (check_button_is_checked) { utils_open_browser(p_url); } else { GtkWidget *dlg = gtk_message_dialog_new(GTK_WINDOW(geany->main_widgets->window), GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_INFO, GTK_BUTTONS_OK, _("Paste Successful")); gtk_message_dialog_format_secondary_markup(GTK_MESSAGE_DIALOG(dlg), _("Your paste can be found here:\n<a href=\"%s\" " "title=\"Click to open the paste in your browser\">%s</a>"), p_url, p_url); gtk_dialog_run(GTK_DIALOG(dlg)); gtk_widget_destroy(dlg); } } else { dialogs_show_msgbox(GTK_MESSAGE_ERROR, _("Unable to paste the code. Check your connection and retry.\n" "Error code: %d\n"), status); } g_free(p_url); }