/* Nessus
 * Copyright (C) 1998 Renaud Deraison
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */
 
#include <includes.h>
#include <gtk/gtk.h>
#include "../xstuff.h"
#include "../preferences.h"
#include "../plugin_infos.h"
#include "globals.h"

static void  scanner_infos_cb(GtkWidget *, struct arglist *);
static void  fill_scanner_list(struct arglist *);

     
struct arglist * prefs_dialog_scan_opt()
{
 GtkWidget * frame;
 GtkWidget * table;
 GtkWidget * ping_hosts;
 GtkWidget * firewall_test;
 GtkWidget * reverse_lookup;
 GtkWidget * box;
 GtkWidget * scan_level_normal;
 GtkWidget * scan_level_heavy;
 GtkWidget * label;
 GSList    * radio_group;
 GtkWidget * port_range;
 GtkWidget  * entry;
 GtkWidget * scanners_window;
 GtkWidget * list;
 
 struct arglist * ctrls = emalloc(sizeof(struct arglist));
 
 frame = gtk_frame_new("Scan options");
 gtk_container_border_width(GTK_CONTAINER(frame), 10);
 arg_add_value(ctrls, "FRAME", ARG_PTR, -1, frame);
 
 gtk_widget_show(frame);
 
 
 box = gtk_vbox_new(FALSE, 10);
 
 gtk_container_add(GTK_CONTAINER(frame), box);
 gtk_container_border_width(GTK_CONTAINER(box), 10);
 gtk_widget_show(box);
 
 
 ping_hosts = gtk_check_button_new_with_label("Ping hosts before scanning them");
 arg_add_value(ctrls, "PING_HOSTS", ARG_PTR, -1, ping_hosts);
 gtk_box_pack_start(GTK_BOX(box), ping_hosts, FALSE, FALSE, 0);
 gtk_widget_show(ping_hosts);
 
 reverse_lookup = gtk_check_button_new_with_label("Do a reverse lookup on the IP before testing it");
 arg_add_value(ctrls, "REVERSE_LOOKUP", ARG_PTR, -1, reverse_lookup);
 gtk_box_pack_start(GTK_BOX(box), reverse_lookup, FALSE, FALSE, 0);
 gtk_widget_show(reverse_lookup);
 
 
 firewall_test = gtk_check_button_new_with_label("We are outside a firewall");
 arg_add_value(ctrls, "OUTSIDE_FIREWALL", ARG_PTR, -1, firewall_test);
 gtk_box_pack_start(GTK_BOX(box), firewall_test, FALSE, FALSE, 0);
 gtk_widget_show(firewall_test);
 
 table = gtk_table_new(5, 2, FALSE);
 gtk_box_pack_start(GTK_BOX(box), table, FALSE, FALSE, 0);
 gtk_widget_show(table);
 
 label = gtk_label_new("Scan level : ");
 gtk_table_attach_defaults(GTK_TABLE(table),label, 0,1,0,1);
 gtk_widget_show(label);
 
 scan_level_normal = gtk_radio_button_new_with_label(NULL, "Normal");
 radio_group = gtk_radio_button_group(GTK_RADIO_BUTTON(scan_level_normal));
 scan_level_heavy = gtk_radio_button_new_with_label(radio_group, "Heavy");
 arg_add_value(ctrls, "SCAN_NORMAL", ARG_PTR, -1, scan_level_normal);
 arg_add_value(ctrls, "SCAN_HEAVY", ARG_PTR, -1, scan_level_heavy);
 
 gtk_table_attach_defaults(GTK_TABLE(table), scan_level_normal, 1,2,0,1);
 gtk_widget_show(scan_level_normal);
 
 gtk_table_attach_defaults(GTK_TABLE(table),scan_level_heavy, 1,2,1,2);
 gtk_widget_show(scan_level_heavy);

 gtk_table_set_row_spacing(GTK_TABLE(table), 1, 10);
 label = gtk_label_new("Port range : ");
 gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 2,3);
 gtk_widget_show(label);
 
 port_range = gtk_entry_new();
 gtk_table_attach_defaults(GTK_TABLE(table), port_range, 1,2,2,3);
 gtk_widget_show(port_range);
 arg_add_value(ctrls, "PORT_RANGE", ARG_PTR, -1, port_range);
 
 gtk_table_set_row_spacing(GTK_TABLE(table), 2, 10);
 label = gtk_label_new("Max threads : ");
 gtk_table_attach_defaults(GTK_TABLE(table), label, 0,1,3,4);
 gtk_widget_show(label);
 
 entry = gtk_entry_new();
 gtk_table_attach_defaults(GTK_TABLE(table), entry, 1,2,3,4);
 gtk_widget_show(entry);
 arg_add_value(ctrls, "MAX_THREADS", ARG_PTR, -1, entry);
 
 
 gtk_table_set_row_spacing(GTK_TABLE(table), 3, 10);
 label = gtk_label_new("Remote file to obtain : ");
 gtk_table_attach_defaults(GTK_TABLE(table), label, 0,1,4,5);
 gtk_widget_show(label);
 
 entry = gtk_entry_new();
 gtk_table_attach_defaults(GTK_TABLE(table), entry, 1,2,4,5);
 gtk_widget_show(entry);
 arg_add_value(ctrls, "TEST_FILE", ARG_PTR, -1, entry);
 
 
 
 
 scanners_window = gtk_scrolled_window_new(NULL,NULL);
 gtk_container_border_width(GTK_CONTAINER(scanners_window), 0);
 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scanners_window),	
  			 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
 gtk_box_pack_end(GTK_BOX(box), scanners_window, TRUE, TRUE, 0);
 gtk_widget_show(scanners_window); 
 
 label = gtk_label_new("Port scanner : ");
 gtk_box_pack_end(GTK_BOX(box), label, FALSE, FALSE, 0);
 gtk_widget_show(label);
 
 list = gtk_list_new();
 arg_add_value(ctrls, "SCANNERS_LIST", ARG_PTR, -1, list);
 gtk_container_add(GTK_CONTAINER(scanners_window), list);
 
 gtk_signal_connect(GTK_OBJECT(list),
		     "selection_changed",
		     GTK_SIGNAL_FUNC(scanner_infos_cb),     
		     ctrls);   
                     
                     
 gtk_widget_show(list);
 fill_scanner_list(ctrls);
 arg_add_value(ctrls, "SCANNERS_NUM", ARG_INT, sizeof(int), (void *)ScannersNum);
 return(ctrls);
}

static 
void prefs_scanner_list_toggle_callback(w, scanner)
     GtkWidget * w;
     struct arglist * scanner;
{
  int state = GTK_TOGGLE_BUTTON (w)->active;
  plug_set_launch(scanner,state);
  pluginset_reload(Plugins, Scanners);
}

static void 
fill_scanner_list(ctrls)
 struct arglist * ctrls;
{
 GtkTooltips * tooltips;
 struct arglist * scans = Scanners;
 GList * dlist = NULL;

 tooltips = gtk_tooltips_new();
 while(scans && scans->next)
   {
     GtkWidget * item;
     GtkWidget * box;
     GtkWidget * button;
     GtkWidget * label;
    
     item = gtk_list_item_new();
     if(arg_get_value(scans->value, "SUMMARY"))
	    gtk_tooltips_set_tip(tooltips, item, 
				 (gchar *)arg_get_value(scans->value, "SUMMARY"),"");
     button = gtk_toggle_button_new_with_label(" ");
     gtk_widget_set_usize(button, 15, 15);
     gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(button), 
				 plug_get_launch(scans->value) ? TRUE:FALSE);
     label = gtk_label_new(scans->name);
     box = gtk_hbox_new(FALSE,5);
     gtk_box_pack_start(GTK_BOX(box), label, FALSE, FALSE, 0);
     gtk_widget_show(label);
     gtk_box_pack_end(GTK_BOX(box), button, FALSE, FALSE,0);
	
     gtk_container_add(GTK_CONTAINER(item), box);
     gtk_signal_connect(GTK_OBJECT(button),
			"clicked",
			GTK_SIGNAL_FUNC(prefs_scanner_list_toggle_callback),
			scans->value);
     
     gtk_widget_show(button);
     gtk_widget_show(box);
     gtk_widget_show(item);
     dlist = g_list_append(dlist, item);
     gtk_object_set_data(GTK_OBJECT(item),
			 "list_item_data",
			  scans->name);
     scans = scans->next;
   }
   gtk_tooltips_enable(tooltips);
   gtk_list_append_items(GTK_LIST(arg_get_value(ctrls, "SCANNERS_LIST")), dlist);
}

void prefs_scanner_redraw(w, dumb, ctrls)
 GtkWidget * w;
 void * dumb;
 struct arglist * ctrls;
{
 int num;
 
 num = (int)arg_get_value(ctrls, "SCANNERS_NUM");
 if(num != ScannersNum){
  fill_scanner_list(ctrls);
  arg_set_value(ctrls, "SCANNERS_NUM", sizeof(int), (void *)ScannersNum);
  }
}


static void 
scanner_infos_cb(widget, ctrls)
     GtkWidget * widget;
     struct arglist * ctrls;
{
  GtkObject * list_item;
  char * scanner;
  GList * dlist;
  GtkWidget * list = arg_get_value(ctrls, "SCANNERS_LIST");
  
  dlist = GTK_LIST(list)->selection;
  if(!dlist)return;
  
  list_item = GTK_OBJECT(dlist->data);
  scanner = gtk_object_get_data(list_item,"list_item_data");
  plugin_info_window_setup(Scanners, scanner);             
}
