ec_interface_plugins.c
上传用户:nilegod
上传日期:2007-01-08
资源大小:220k
文件大小:13k
源码类别:

网络截获/分析

开发平台:

C/C++

  1. /*
  2.     ettercap -- ncurses interface for plugins
  3.     Copyright (C) 2001  ALoR <alor@users.sourceforge.net>, NaGA <crwm@freemail.it>
  4.     This program is free software; you can redistribute it and/or modify
  5.     it under the terms of the GNU General Public License as published by
  6.     the Free Software Foundation; either version 2 of the License, or
  7.     (at your option) any later version.
  8.     This program is distributed in the hope that it will be useful,
  9.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.     GNU General Public License for more details.
  12.     You should have received a copy of the GNU General Public License
  13.     along with this program; if not, write to the Free Software
  14.     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. */
  16. #include "include/ec_main.h"
  17. #ifdef PERMIT_PLUGINS
  18. #ifdef HAVE_NCURSES  // don't compile if ncurses interface is not supported
  19. #include <string.h>
  20. #include <ncurses.h>
  21. #include <unistd.h>
  22. #include <stdlib.h>
  23. #include "include/ec_interface.h"
  24. #include "include/ec_plugins.h"
  25. #ifdef DEBUG
  26.    #include "include/ec_debug.h"
  27. #endif
  28. #define NORM_COLOR   6
  29. #define TITLE_COLOR  2
  30. #define HELP_COLOR   6
  31. #define REV_COLOR    7
  32. #define KEY_RETURN   10
  33. #define KEY_CTRL_L   12
  34. #define PAD_BUFFER 500        // buffered lines for plugin window
  35. // protos....
  36. void Interface_Plugins_Run(void);
  37. void Interface_Plugins_Redraw(void);
  38. void Interface_Plugins_Winch(void);
  39. char Interface_Plugins_PopUp(char *question);
  40. void Interface_Plugins_PointItem(char direction);
  41. void Interface_Plugins_InitList(void);
  42. void Interface_Plugins_SelectItem(void);
  43. void Interface_Plugins_Scroll(short direction);
  44. void Interface_Plugins_DrawScroller(void);
  45. // global variables
  46. WINDOW *plugin_window, *b_plugin_window;
  47. extern WINDOW *main_window;
  48. extern int W_MAINX1, W_MAINY1, W_MAINX2, W_MAINY2, W_SELECTPLUG;
  49. short number_of_plugins;
  50. extern int Sel_Number;
  51. int Plug_Base_Pointer = 0;
  52. int Plug_Pointer = 0;
  53. short P_Sel_Number;
  54. #define LMARGIN 0
  55. short scroll_yp;
  56. // -------------------
  57. void Interface_Plugins_Winch(void)  // TODO better handling...
  58. {
  59. #ifdef DEBUG
  60.    Debug_msg("Interface_Plugin_WinchtTODO");
  61. #endif
  62.    Interface_Plugins_Redraw();
  63. }
  64. void Interface_Plugins_Redraw(void)
  65. {
  66.    Interface_Redraw();
  67. #ifdef DEBUG
  68.    Debug_msg("Interface_Plugins_Redraw");
  69. #endif
  70.    redrawwin(b_plugin_window);
  71.    wnoutrefresh(b_plugin_window);
  72.    pnoutrefresh(plugin_window, scroll_yp, 0, W_MAINY1 + 3, 3, W_MAINY2 - 3 , W_MAINX2 - 2);
  73.    doupdate();
  74. }
  75. char Interface_Plugins_PopUp(char *question)
  76. {
  77.    WINDOW *question_window;
  78.    char answer;
  79.    question_window = newwin(5, strlen(question) + 4,0,0);
  80.    mvwin(question_window, W_MAINY1+(W_MAINY2-W_MAINY1)/2-2, W_MAINX2/2 - (strlen(question)+4)/2 );
  81.    wbkgdset(question_window, COLOR_PAIR(TITLE_COLOR));
  82.    wattron(question_window, A_BOLD);
  83.    box(question_window,ACS_VLINE,ACS_HLINE);
  84.    mvwprintw(question_window,  2, 2, question);
  85.    wnoutrefresh(question_window);
  86.    doupdate();
  87.    answer = wgetch(question_window);
  88.    delwin(question_window);
  89.    touchwin(plugin_window);
  90.    pnoutrefresh(plugin_window, scroll_yp, 0, W_MAINY1 + 3, 3, W_MAINY2 - 3 , W_MAINX2 - 2);
  91.    doupdate();
  92. #ifdef DEBUG
  93.    Debug_msg("Interface_PLugins_PopUp returns -- %c", answer);
  94. #endif
  95.    return answer;
  96. }
  97. void Interface_Plugins_InitList(void)
  98. {
  99.    int j;
  100.    int Plug_Top_Pointer;
  101.    #include "include/ec_install_path.h"   // char *path = "...";
  102.    if (has_colors())
  103.       wbkgdset(plugin_window, COLOR_PAIR(NORM_COLOR));
  104.    else
  105.       wattroff(plugin_window,A_REVERSE);
  106.    werase(plugin_window);
  107.    if (number_of_plugins == 0)      // no plugin... no action... ;)
  108.    {
  109.       wmove(plugin_window, scroll_yp, LMARGIN );
  110.       wprintw(plugin_window, "NO plugin available in %s or in ./ !!n", path);
  111.       pnoutrefresh(plugin_window, scroll_yp, 0, W_MAINY1 + 3, 3, W_MAINY2 - 3 , W_MAINX2 - 2);
  112.       doupdate();
  113.       return;
  114.    }
  115.    Plug_Top_Pointer = (Plug_Base_Pointer+P_Sel_Number < number_of_plugins) ? Plug_Base_Pointer + P_Sel_Number : number_of_plugins ;
  116.    for(j=Plug_Base_Pointer; j<Plug_Top_Pointer; j++)     // prints connections within the plugin_window height
  117.    {
  118.       wmove(plugin_window, scroll_yp + j - Plug_Base_Pointer, LMARGIN );
  119.       wprintw(plugin_window, "%3d) %8s   v %.1f -- %s", j+1, Plugin_Getname(Plugins_list[j].name),
  120.                                                                Plugins_list[j].version,
  121.                                                                Plugins_list[j].description);
  122.    }
  123.    if (has_colors())
  124.       wbkgdset(plugin_window, COLOR_PAIR(REV_COLOR));
  125.    else
  126.       wattron(plugin_window,A_REVERSE);
  127.    wmove(plugin_window, scroll_yp + Plug_Pointer - Plug_Base_Pointer, LMARGIN );
  128.    whline(plugin_window, ' ', W_SELECTPLUG);
  129.    wprintw(plugin_window, "%3d) %8s   v %.1f -- %s", Plug_Pointer+1,
  130.                                                        Plugin_Getname(Plugins_list[Plug_Pointer].name),
  131.                                                        Plugins_list[Plug_Pointer].version,
  132.                                                        Plugins_list[Plug_Pointer].description);
  133.    pnoutrefresh(plugin_window, scroll_yp, 0, W_MAINY1 + 3, 3, W_MAINY2 - 3 , W_MAINX2 - 2);
  134.    doupdate();
  135. }
  136. void Interface_Plugins_PointItem(char direction)
  137. {
  138.    int Old_Plug_Pointer;
  139.    if (number_of_plugins == 0) return; // no plugin... no action... ;)
  140.    Old_Plug_Pointer = Plug_Pointer;
  141.    Plug_Pointer += direction;
  142.    if (Plug_Pointer > number_of_plugins -1 ) Plug_Pointer = number_of_plugins - 1;
  143.    if (Plug_Pointer < 0) Plug_Pointer = 0;
  144.    if ( (Plug_Pointer - Plug_Base_Pointer + direction  >= P_Sel_Number) && (direction > 0) )    // scroll down
  145.    {
  146.       if (Plug_Base_Pointer + P_Sel_Number <= number_of_plugins)
  147.          Plug_Base_Pointer = (Plug_Base_Pointer + direction < number_of_plugins) ? Plug_Base_Pointer + direction : number_of_plugins - P_Sel_Number;
  148.       Interface_Plugins_InitList();
  149.    }
  150.    else if ( (Plug_Pointer - Plug_Base_Pointer + direction < 0) && (direction < 0) )         // scroll up
  151.    {
  152.       if (Plug_Base_Pointer > 0)
  153.          Plug_Base_Pointer = (Plug_Base_Pointer + direction > 0) ? Plug_Base_Pointer + direction : 0;
  154.       Interface_Plugins_InitList();
  155.    }
  156.    if (has_colors())
  157.       wbkgdset(plugin_window, COLOR_PAIR(NORM_COLOR));
  158.    else
  159.       wattroff(plugin_window,A_REVERSE);
  160.    if ( (Old_Plug_Pointer >= Plug_Base_Pointer) && (Old_Plug_Pointer <= Plug_Base_Pointer + P_Sel_Number -1))  // DON'T redraw previous selected item if it is out of view
  161.    {
  162.       wmove(plugin_window, scroll_yp + Old_Plug_Pointer - Plug_Base_Pointer, LMARGIN);
  163.       whline(plugin_window,' ', W_SELECTPLUG);                          //deletes the previous position
  164.       wprintw(plugin_window, "%3d) %8s   v %.1f -- %s", Old_Plug_Pointer+1,
  165.                                                          Plugin_Getname(Plugins_list[Old_Plug_Pointer].name),
  166.                                                          Plugins_list[Old_Plug_Pointer].version,
  167.                                                          Plugins_list[Old_Plug_Pointer].description);
  168.    }
  169.    if (has_colors())
  170.       wbkgdset(plugin_window, COLOR_PAIR(REV_COLOR));
  171.    else
  172.       wattron(plugin_window,A_REVERSE);
  173.    wmove(plugin_window, scroll_yp + Plug_Pointer - Plug_Base_Pointer, LMARGIN);
  174.    whline(plugin_window, ' ', W_SELECTPLUG);                         //select new position
  175.    wprintw(plugin_window, "%3d) %8s   v %.1f -- %s", Plug_Pointer+1,
  176.                                                       Plugin_Getname(Plugins_list[Plug_Pointer].name),
  177.                                                       Plugins_list[Plug_Pointer].version,
  178.                                                       Plugins_list[Plug_Pointer].description);
  179.    pnoutrefresh(plugin_window, scroll_yp, 0, W_MAINY1 + 3, 3, W_MAINY2 - 3 , W_MAINX2 - 2);
  180.    doupdate();
  181. }
  182. void Interface_Plugins_DrawScroller(void)
  183. {
  184.    short sheight = (W_MAINY2-10)*(W_MAINY2-10)/PAD_BUFFER;
  185.    short vpos = (W_MAINY2-6)*scroll_yp/PAD_BUFFER;
  186.    sheight = (sheight < 1) ? 1 : sheight;
  187.    vpos = (vpos == 0) ? 1 : vpos;
  188.    vpos = (vpos > W_MAINY2-9-sheight) ? W_MAINY2-9-sheight : vpos;
  189.    mvwvline(b_plugin_window, 1, W_MAINX2 - 4, ACS_VLINE, W_MAINY2-10);
  190.    wattron(b_plugin_window, A_REVERSE);
  191.    mvwvline(b_plugin_window, vpos, W_MAINX2 - 4, ' ', sheight);
  192.    wnoutrefresh(b_plugin_window);
  193.    wattroff(b_plugin_window, A_REVERSE);
  194. }
  195. void Interface_Plugins_Scroll(short direction)
  196. {
  197.    scroll_yp += direction;
  198.    scroll_yp = (scroll_yp < 0) ? 0 : scroll_yp;
  199.    scroll_yp = (scroll_yp > PAD_BUFFER - (W_MAINY2-10)) ? PAD_BUFFER - (W_MAINY2-10) : scroll_yp;
  200.    pnoutrefresh(plugin_window, scroll_yp, 0, W_MAINY1 + 3, 3, W_MAINY2 - 3 , W_MAINX2 - 2);
  201.    Interface_Plugins_DrawScroller();
  202.    doupdate();
  203. }
  204. void Interface_Plugins_SelectItem(void)
  205. {
  206.    char plugin_file[100];
  207.    FILE *test;
  208.    int KeyPress;
  209. #ifdef DEBUG
  210.    Debug_msg("Interface_Plugins_SelectItem");
  211. #endif
  212.    if (number_of_plugins == 0) return; // no plugin... no action... ;)
  213.    scroll_yp = PAD_BUFFER - (W_MAINY2-10);
  214.    wbkgdset(plugin_window, COLOR_PAIR(NORM_COLOR));
  215.    werase(plugin_window);
  216.    wmove(plugin_window, scroll_yp, 0);
  217.    pnoutrefresh(plugin_window, scroll_yp, 0, W_MAINY1 + 3, 3, W_MAINY2 - 3 , W_MAINX2 - 2);
  218.    doupdate();
  219.    strcpy(plugin_file, Plugins_list[Plug_Pointer].path);
  220.    strcat(plugin_file, Plugins_list[Plug_Pointer].name);
  221.    if ( (test = fopen(plugin_file, "r")) )
  222.       Plugin_RunPlugIn(plugin_file, NULL);
  223.    else
  224.    {
  225.       Interface_Plugins_PopUp("Can't find the dinamyc plugin file !!");
  226.       return;
  227.    }
  228.    loop
  229.    {
  230.       KeyPress = wgetch(plugin_window);
  231.       switch(KeyPress)
  232.       {
  233.             case KEY_DOWN:
  234.                   Interface_Plugins_Scroll( +1 );
  235.                   break;
  236.             case KEY_UP:
  237.                   Interface_Plugins_Scroll( -1 );
  238.                   break;
  239.             case KEY_NPAGE:
  240.                   Interface_Plugins_Scroll( W_MAINY2-10 );     //PGDOWN
  241.                   break;
  242.             case KEY_PPAGE:
  243.                   Interface_Plugins_Scroll( -(W_MAINY2-10) );  //PGUP
  244.                   break;
  245.             case KEY_F(10):
  246.             case 'Q':
  247.             case 'q':
  248.                      scroll_yp = PAD_BUFFER - (W_MAINY2-10);
  249.                      Interface_Plugins_DrawScroller();
  250.                      return;
  251.                      break;
  252.       }
  253.    }
  254. }
  255. void Interface_Plugins_Run(void)
  256. {
  257.    int KeyPress;
  258. #ifdef DEBUG
  259.    Debug_msg("Interface_Plugins_Run");
  260. #endif
  261.    P_Sel_Number = Sel_Number - 6;
  262.    b_plugin_window = newwin(W_MAINY2 - W_MAINY1 - 3, W_MAINX2 - W_MAINX1 - 3, W_MAINY1 + 2, W_MAINX1 + 2);
  263.    plugin_window = newpad(PAD_BUFFER, W_MAINX2 - 6);
  264.    scroll_yp = PAD_BUFFER - (W_MAINY2-10);
  265.    wmove(plugin_window, scroll_yp, 0);
  266.    init_pair(REV_COLOR, COLOR_BLACK, COLOR_CYAN);
  267.    wbkgdset(plugin_window, COLOR_PAIR(NORM_COLOR));      // sets the colors
  268.    wbkgdset(b_plugin_window, COLOR_PAIR(TITLE_COLOR));
  269.    scrollok(plugin_window, TRUE);
  270.    werase(plugin_window);
  271.    keypad(plugin_window,1);
  272.    wattron(b_plugin_window, A_BOLD);
  273.    box(b_plugin_window,ACS_VLINE,ACS_HLINE);
  274.    wnoutrefresh(b_plugin_window);
  275.    pnoutrefresh(plugin_window, scroll_yp, 0, W_MAINY1 + 3, 3, W_MAINY2 - 3 , W_MAINX2 - 2);
  276.    doupdate();
  277.    number_of_plugins = Plugin_MakePlugList();
  278.    Interface_Plugins_DrawScroller();
  279.    Interface_Plugins_InitList();
  280.    loop
  281.    {
  282.       KeyPress = wgetch(plugin_window);
  283.       switch (KeyPress)
  284.       {
  285.          case KEY_DOWN:
  286.                   Interface_Plugins_PointItem( +1 );
  287.                   break;
  288.          case KEY_UP:
  289.                   Interface_Plugins_PointItem( -1 );
  290.                   break;
  291.          case KEY_NPAGE:
  292.                   Interface_Plugins_PointItem(P_Sel_Number-1);    //PGDOWN
  293.                   break;
  294.          case KEY_PPAGE:
  295.                   Interface_Plugins_PointItem(-P_Sel_Number+1);   //PGUP
  296.                   break;
  297.          case KEY_RETURN:
  298.                   Interface_Plugins_SelectItem();
  299.                   Interface_Plugins_InitList();
  300.                   break;
  301.          case KEY_F(1):
  302.          case 'H':
  303.          case 'h':{
  304.                      static char *help[] = {
  305.                         "[qQ][F10] - quit",
  306.                         "[return]  - run the selected plug in",
  307.                         "[hH]      - this help screen",
  308.                         NULL};
  309.                      Interface_HelpWindow(help);
  310.                   }
  311.                   Interface_Plugins_Redraw();
  312.                   break;
  313.          case KEY_CTRL_L:  // CTRL+L refresh the screen
  314.                   Interface_Plugins_Redraw();
  315.                   break;
  316.          case 'q':
  317.          case 'Q':
  318.                   #ifdef DEBUG
  319.                      Debug_msg("Interface_Plugin_Run_END");
  320.                   #endif
  321.                   if (Options.silent && Options.plugin) Interface_WExit("They are safe!!  for now... ");
  322.                   delwin(plugin_window);
  323.                   delwin(b_plugin_window);
  324.                   touchwin(main_window);
  325.                   wnoutrefresh(main_window);
  326.                   doupdate();
  327.                   plugin_window = NULL; // for winch see ec_signal.c
  328.                   return;
  329.                   break;
  330.          case KEY_F(10):
  331.                   #ifdef DEBUG
  332.                      Debug_msg("Interface_Plugin_Run_END");
  333.                      Debug_msg("Interface_Run_END");
  334.                   #endif
  335.                   Interface_WExit("Quitting... ");
  336.                   break;
  337.       }
  338.    }
  339. }
  340. #endif   // HAVE_NCURSES
  341. #endif   // PERMIT_PLUGINS
  342. /* EOF */