"Fossies" - the Fresh Open Source Software Archive

Member "ettercap-0.8.3.1/src/interfaces/gtk3/ec_gtk3_plugins.c" (1 Aug 2020, 13962 Bytes) of package /linux/privat/ettercap-0.8.3.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "ec_gtk3_plugins.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 0.8.3_vs_0.8.3.1.

    1 /*
    2     ettercap -- GTK+ GUI
    3 
    4     Copyright (C) ALoR & NaGA
    5 
    6     This program is free software; you can redistribute it and/or modify
    7     it under the terms of the GNU General Public License as published by
    8     the Free Software Foundation; either version 2 of the License, or
    9     (at your option) any later version.
   10 
   11     This program is distributed in the hope that it will be useful,
   12     but WITHOUT ANY WARRANTY; without even the implied warranty of
   13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   14     GNU General Public License for more details.
   15 
   16     You should have received a copy of the GNU General Public License
   17     along with this program; if not, write to the Free Software
   18     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
   19 
   20 */
   21 
   22 #include <ec.h>
   23 #include <ec_gtk3.h>
   24 #include <ec_file.h>
   25 #include <ec_plugins.h>
   26 
   27 #define MAX_DESC_LEN 75
   28 
   29 /* proto */
   30 
   31 static void gtkui_load_plugin(const char *full);
   32 static void gtkui_add_plugin(char active, struct plugin_ops *ops);
   33 static void gtkui_plug_destroy(void);
   34 static void gtkui_plugins_detach(GtkWidget *child);
   35 static void gtkui_plugins_attach(void);
   36 static int  gtkui_select_plugin(gchar *plugin);
   37 static void gtkui_select_plugin_menu(GtkMenuItem *item, gpointer data);
   38 static void gtkui_select_plugin_treeview(GtkTreeView *treeview,
   39       GtkTreePath *path, GtkTreeViewColumn *column, gpointer data);
   40 static void gtkui_create_plug_array(void);
   41 gboolean gtkui_plugin_context(GtkWidget *widget, GdkEventButton *event,
   42    gpointer data);
   43 
   44 /* globals */
   45 
   46 static GtkWidget   *plugins_window = NULL;
   47 static GtkWidget         *treeview = NULL;
   48 static GtkListStore    *ls_plugins = NULL;
   49 static GtkTreeSelection *selection = NULL;
   50 
   51 /*******************************************/
   52 
   53 /*
   54  * display the file open dialog
   55  */
   56 void gtkui_plugin_load(GSimpleAction *action, GVariant *value, gpointer data)
   57 {
   58    GtkWidget *dialog, *chooser, *content;
   59    gchar *filename;
   60    int response = 0;
   61 #ifdef OS_WINDOWS
   62    char *path = get_full_path("/lib/", "");
   63 #else
   64    char *path = INSTALL_LIBDIR "/" PROGRAM "/";
   65 #endif
   66    
   67    (void) action;
   68    (void) value;
   69    (void) data;
   70 
   71    DEBUG_MSG("gtk_plugin_load");
   72    
   73    dialog = gtk_dialog_new_with_buttons("Select a plugin...",
   74          GTK_WINDOW(window), 
   75          GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_USE_HEADER_BAR,
   76          "_Cancel", GTK_RESPONSE_CANCEL,
   77          "_OK",     GTK_RESPONSE_OK,
   78          NULL);
   79    gtk_container_set_border_width(GTK_CONTAINER(dialog), 10);
   80 
   81    content = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
   82    chooser = gtk_file_chooser_widget_new(GTK_FILE_CHOOSER_ACTION_OPEN);
   83    gtk_container_add(GTK_CONTAINER(content), chooser);
   84    gtk_widget_show(chooser);
   85 
   86    gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(chooser), path);
   87 
   88 #ifdef OS_WINDOWS
   89    SAFE_FREE(path);
   90 #endif
   91    
   92    response = gtk_dialog_run (GTK_DIALOG (dialog));
   93    
   94    if (response == GTK_RESPONSE_OK) {
   95       gtk_widget_hide(dialog);
   96       filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(chooser));
   97       
   98       gtkui_load_plugin(filename);
   99 
  100       /* update the list */
  101       gtkui_create_plug_array();
  102       g_free(filename);
  103    }
  104    gtk_widget_destroy (dialog);
  105 }
  106 
  107 static void gtkui_load_plugin(const char *full)
  108 {
  109    char *file;
  110    int ret;
  111 
  112 #ifdef OS_WINDOWS
  113    file = strrchr(full, '\\');
  114 #else
  115    file = strrchr(full, '/');
  116 #endif
  117    /* remove the last /
  118       split path and file
  119       increment file pointer to point to filename */
  120    *file++ = 0;
  121 
  122    DEBUG_MSG("gtk_load_plugin %s/%s", full, file);
  123 
  124    /* load the plugin */
  125    ret = plugin_load_single(full, file);
  126 
  127 
  128    /* check the return code */
  129    switch (ret) {
  130       case E_SUCCESS:
  131          gtkui_message("Plugin loaded successfully");
  132          break;
  133       case -E_DUPLICATE:
  134          ui_error("plugin %s already loaded...", file);
  135          break;
  136       case -E_VERSION:
  137          ui_error("plugin %s was compiled for a different ettercap version...", file);
  138          break;
  139       case -E_INVALID:
  140       default:
  141          ui_error("Cannot load the plugin...\nthe file may be an invalid plugin\nor you don't have the permission to open it");
  142          break;
  143    }
  144 }
  145 
  146 /*
  147  * plugin management
  148  */
  149 void gtkui_plugin_mgmt(GSimpleAction *action, GVariant *value, gpointer data)
  150 {
  151    GtkWidget *scrolled, *vbox;
  152    GtkCellRenderer   *renderer;
  153    GtkTreeViewColumn *column;
  154 
  155    (void) action;
  156    (void) value;
  157    (void) data;
  158 
  159    DEBUG_MSG("gtk_plugin_mgmt");
  160    
  161    /* if the object already exist, set the focus to it */
  162    if (plugins_window) {
  163       if(GTK_IS_WINDOW (plugins_window))
  164          gtk_window_present(GTK_WINDOW (plugins_window));
  165       else
  166          gtkui_page_present(plugins_window);
  167       return;
  168    }
  169 
  170    plugins_window = gtkui_page_new("Plugins", &gtkui_plug_destroy, &gtkui_plugins_detach);
  171    
  172    vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
  173    gtk_container_add(GTK_CONTAINER (plugins_window), vbox);
  174    gtk_widget_show(vbox);
  175    
  176   /* list */
  177    scrolled = gtk_scrolled_window_new(NULL, NULL);
  178    gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW (scrolled), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
  179    gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW (scrolled), GTK_SHADOW_IN);
  180    gtk_box_pack_start(GTK_BOX(vbox), scrolled, TRUE, TRUE, 0);
  181    gtk_widget_show(scrolled);
  182    
  183    treeview = gtk_tree_view_new();
  184    gtk_container_add(GTK_CONTAINER (scrolled), treeview);
  185    gtk_widget_show(treeview);
  186 
  187    selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (treeview));
  188    gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
  189    g_signal_connect (G_OBJECT (treeview), "row-activated",
  190          G_CALLBACK(gtkui_select_plugin_treeview), NULL);
  191    
  192    renderer = gtk_cell_renderer_text_new ();
  193    column = gtk_tree_view_column_new_with_attributes (" ", renderer, "text", 0, NULL);
  194    gtk_tree_view_column_set_sort_column_id (column, 0);
  195    gtk_tree_view_append_column (GTK_TREE_VIEW(treeview), column);
  196 
  197    renderer = gtk_cell_renderer_text_new ();
  198    column = gtk_tree_view_column_new_with_attributes ("Name", renderer, "text", 1, NULL);
  199    gtk_tree_view_column_set_sort_column_id (column, 1);
  200    gtk_tree_view_append_column (GTK_TREE_VIEW(treeview), column);
  201 
  202    renderer = gtk_cell_renderer_text_new ();
  203    column = gtk_tree_view_column_new_with_attributes ("Version", renderer, "text", 2, NULL);
  204    gtk_tree_view_column_set_sort_column_id (column, 2);
  205    gtk_tree_view_append_column (GTK_TREE_VIEW(treeview), column);
  206 
  207    renderer = gtk_cell_renderer_text_new ();
  208    column = gtk_tree_view_column_new_with_attributes ("Info", renderer, "text", 3, NULL);
  209    gtk_tree_view_column_set_sort_column_id (column, 3);
  210    gtk_tree_view_append_column (GTK_TREE_VIEW(treeview), column);
  211 
  212    /* create the array for the list widget */
  213    /* or refreshes it if it exists */
  214    gtkui_create_plug_array();
  215    gtk_tree_view_set_model(GTK_TREE_VIEW (treeview), GTK_TREE_MODEL (ls_plugins));   
  216 
  217    g_signal_connect(G_OBJECT(treeview), "button-press-event", G_CALLBACK(gtkui_plugin_context), NULL);
  218 
  219    gtk_widget_show(plugins_window);
  220 }
  221 
  222 static void gtkui_plugins_detach(GtkWidget *child)
  223 {
  224    plugins_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  225    gtk_window_set_title(GTK_WINDOW (plugins_window), "Select a plugin...");
  226    gtk_window_set_default_size(GTK_WINDOW (plugins_window), 400, 300);
  227    g_signal_connect (G_OBJECT (plugins_window), "delete_event", G_CALLBACK (gtkui_plug_destroy), NULL);
  228 
  229    /* make <ctrl>d shortcut turn the window back into a tab */
  230    gtkui_page_attach_shortcut(plugins_window, gtkui_plugins_attach);
  231 
  232    gtk_container_add(GTK_CONTAINER (plugins_window), child);
  233 
  234    gtk_window_present(GTK_WINDOW (plugins_window));
  235 }
  236 
  237 static void gtkui_plugins_attach(void)
  238 {
  239    gtkui_plug_destroy();
  240    gtkui_plugin_mgmt(NULL, NULL, NULL);
  241 }
  242 
  243 static void gtkui_plug_destroy(void)
  244 {
  245    gtk_widget_destroy(plugins_window);
  246    plugins_window = NULL;
  247 }
  248 
  249 
  250 /*
  251  * create the array for the widget.
  252  * erase any previously alloc'd array 
  253  */
  254 static void gtkui_create_plug_array(void)
  255 {
  256    GtkTreeIter iter;
  257    int res;
  258    static int blocked = 0;
  259    
  260    DEBUG_MSG("gtk_create_plug_array");
  261    
  262    if(ls_plugins)
  263       gtk_list_store_clear(GTK_LIST_STORE (ls_plugins));
  264    else
  265       ls_plugins = gtk_list_store_new (4, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
  266    
  267    /* go thru the list of plugins */
  268    res = plugin_list_walk(PLP_MIN, PLP_MAX, &gtkui_add_plugin);
  269    if (res == -E_NOTFOUND) { 
  270       blocked = g_signal_handlers_block_by_func(G_OBJECT(treeview),
  271             G_CALLBACK(gtkui_select_plugin_treeview), NULL);
  272       gtk_list_store_append (ls_plugins, &iter);
  273       gtk_list_store_set (ls_plugins, &iter, 0, " ", 1, "No Plugins Loaded", -1);
  274    } else if(blocked > 0) {
  275       g_signal_handlers_unblock_by_func(G_OBJECT(treeview),
  276             G_CALLBACK(gtkui_select_plugin_treeview), NULL);
  277       blocked = 0;
  278    }
  279 }
  280 
  281 /*
  282  * callback function for displaying the plugin list 
  283  */
  284 static void gtkui_add_plugin(char active, struct plugin_ops *ops)
  285 {
  286    GtkTreeIter iter;
  287    char active_str[2];
  288 
  289    active_str[0] = (active)?'*':' ';
  290    active_str[1] = 0;
  291 
  292    gtk_list_store_append (ls_plugins, &iter);
  293    gtk_list_store_set (ls_plugins, &iter,
  294                        0, active_str,
  295                        1, ops->name,
  296                        2, ops->version,
  297                        3, ops->info, -1);
  298 }
  299 
  300 /*
  301  * toggle state of a plugin
  302  */
  303 static int gtkui_select_plugin(gchar *plugin)
  304 {
  305    int ret;
  306 
  307    if(!plugin)
  308       /* bad pointer from gtk_tree_model_get, shouldn't happen */
  309       return -E_NOTHANDLED;
  310 
  311    /* print the message */
  312    if (plugin_is_activated(plugin) == 0)
  313       INSTANT_USER_MSG("Activating %s plugin...\n", plugin);
  314    else
  315       INSTANT_USER_MSG("Deactivating %s plugin...\n", plugin);
  316          
  317    /*
  318     * pay attention on this !
  319     * if the plugin init does not return,
  320     * we are blocked here. So it is encouraged
  321     * to write plugins which spawn a thread
  322     * and immediately return
  323     */
  324    if (plugin_is_activated(plugin) == 1)
  325       ret = plugin_fini(plugin);
  326    else
  327       ret = plugin_init(plugin);
  328         
  329    /* refresh the list to mark plugin active */
  330    gtkui_create_plug_array();
  331 
  332    return ret;
  333 }
  334 
  335 /*
  336  * callback when plugin is selected using the context menu
  337  */
  338 static void gtkui_select_plugin_menu(GtkMenuItem *item, gpointer data)
  339 {
  340    gchar *plugin;
  341 
  342    (void) item;
  343 
  344    plugin = data;
  345 
  346    gtkui_select_plugin(plugin);
  347 
  348 }
  349 
  350 /*
  351  * callback when plugin is double clicked in the treeview
  352  */
  353 static void gtkui_select_plugin_treeview(GtkTreeView *treeview,
  354       GtkTreePath *path, GtkTreeViewColumn *column, gpointer data)
  355 {
  356    GtkTreeIter iter;
  357    GtkTreeModel *model;
  358    GtkTreeSelection *selection;
  359    gchar *plugin;
  360 
  361    (void) path;
  362    (void) column;
  363    (void) data;
  364 
  365    model = gtk_tree_view_get_model(treeview);
  366    selection = gtk_tree_view_get_selection(treeview);
  367 
  368    if (gtk_tree_selection_get_selected(selection, &model, &iter)) {
  369       gtk_tree_model_get (model, &iter, 1, &plugin, -1);
  370    } else
  371       return; /* nothing is selected */
  372 
  373    gtkui_select_plugin(plugin);
  374 }
  375 
  376 /*
  377  * check if plugins have been supplied on the CLI
  378  * and try to start all provided plugins
  379  */
  380 gboolean gtkui_plugins_autostart(gpointer data)
  381 {
  382    struct plugin_list *plugin, *tmp;
  383 
  384    (void) data;
  385 
  386    DEBUG_MSG("gtkui_plugins_autostart()");
  387 
  388    /* if plugins have been defined on the CLI */
  389    if (!LIST_EMPTY(&EC_GBL_OPTIONS->plugins)) {
  390       LIST_FOREACH_SAFE(plugin, &EC_GBL_OPTIONS->plugins, next, tmp) {
  391          /* first check if the plugin exists */
  392          if (search_plugin(plugin->name) != E_SUCCESS) {
  393             plugin->exists = false;
  394             USER_MSG("Sorry, plugin '%s' can not be found - skipping!\n\n",
  395                   plugin->name);
  396          }
  397          else {
  398             /* now we can try to start the plugin */
  399             plugin->exists = true;
  400             if (gtkui_select_plugin(plugin->name) != PLUGIN_RUNNING) {
  401                USER_MSG("Plugin '%s' can not be started - skipping!\n\n",
  402                      plugin->name);
  403             }
  404          }
  405       }
  406    }
  407 
  408    return FALSE;
  409 }
  410 
  411 gboolean gtkui_refresh_plugin_list(gpointer data)
  412 {
  413 
  414    /* avoid warning */
  415    (void)data;
  416 
  417    DEBUG_MSG("gtk_refresh_plugin_list");
  418    /* refresh the list to mark plugin active */
  419    gtkui_create_plug_array();
  420 
  421    /* return FALSE so g_idle_add() only calls it once */
  422    return FALSE;
  423 }
  424 
  425 gboolean gtkui_plugin_context(GtkWidget *widget, GdkEventButton *event, gpointer data)
  426 {
  427    GtkTreeIter iter;
  428    GtkTreeModel *model;
  429    GtkWidget *menu, *item;
  430    char *plugin = NULL;
  431 
  432    (void) widget;
  433    (void) data;
  434 
  435    model = GTK_TREE_MODEL(ls_plugins);
  436 
  437 
  438    if (gtk_tree_selection_get_selected (GTK_TREE_SELECTION(selection), &model, &iter)) {
  439       gtk_tree_model_get (model, &iter, 1, &plugin, -1);
  440    } else
  441       return FALSE; /* nothing is selected */
  442 
  443    if(!plugin)
  444       return FALSE; /* bad pointer from gtk_tree_model_get, shouldn't happen */
  445    /* create context menu and bind event to menu item */
  446    menu = gtk_menu_new();
  447    item = gtk_menu_item_new();
  448    gtk_menu_shell_append(GTK_MENU_SHELL(menu), item);
  449    g_signal_connect(G_OBJECT(item), "activate",
  450          G_CALLBACK(gtkui_select_plugin_menu), plugin);
  451    gtk_widget_show(item);
  452 
  453 
  454    /* print the message */
  455    if (plugin_is_activated(plugin) == 0)
  456       gtk_menu_item_set_label(GTK_MENU_ITEM(item), "Activate");
  457    else
  458       gtk_menu_item_set_label(GTK_MENU_ITEM(item), "Deactivate");
  459          
  460    if (event->button == 3) {
  461 #if GTK_CHECK_VERSION(3,22,0)
  462       gtk_menu_popup_at_pointer(GTK_MENU(menu), (GdkEvent*)event);
  463 #else
  464       gtk_menu_popup(GTK_MENU(data), NULL, NULL, NULL, NULL, 3, event->time);
  465 #endif
  466        /* 
  467         * button press event handle must return TRUE to keep the selection
  468         * active when pressing the mouse button 
  469         */
  470        return TRUE;
  471     }
  472 
  473     return FALSE;
  474 }
  475 
  476 /* EOF */
  477 
  478 // vim:ts=3:expandtab
  479