# Funcion file used by airoscript
# Copyright (C) 2010-2011 David Francos Cuartero
#        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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

mainmenu(){
	$clear

	[[ $TERMINAL == "airoscript_quiet" ]] && { 
		[[ $notification != "" ]] && notify $notification;
	    mkmenu $"Main Menu" $"Attack - Attack target" $"Crack  - Get target key"  $"Select - Reselect target" $"Others - Various utilities" $"Autopwn- Automatic cracking" "Window - Window list" $"Exit   - Quits"
		window=1;
	}  || {
		[[ $notification != "" ]] && notify $notification;
		mkmenu $"Main Menu" $"Attack - Attack target" $"Crack  - Get target key"  $"Select - Reselect target" $"Others - Various utilities" $"Autopwn- Automatic cracking" $"Exit  - Quits"
	}
        for choix  in ${choice[*]}; do
            case "$choix" in
                1) menu_attack;;
                2) if [ x$interactive == x0 ]; then auto; else menu_crack; fi ;;
                3) select_ap;;
                4) menu_extra;;
                5) check_function autopwn || source $path/autocrack; autopwn runner;;
                6) [[ $window ]] && { menu_windows; } || { check_function doexit || source $path/internal/exit; doexit; } ;;
                7) check_function doexit || source $path/internal/exit; doexit;;
                *) $clear && markwarn $"ERROR: Wrong number entered";;
            esac
        done  
    mainmenu
}

menu_windows(){
	cd $DUMP_PATH/Terminals
	clear;
	windows=(*)
	mkmenu "Window list" * "Back to main menu"
	[[ $choice == ${#windows} ]] && return;
	echo; clear;
	notify "Press ctrl + c to return to airoscript"
	tail_launched=1;
	tail -f "${windows[$choice - 1]}"
	tail_launched=0;
	cd -
}
menu_attack(){
    monmode &>/dev/null
    if [[ "$Host_ENC" =~ (.*)"WEP"(.*) ]]; then attack_wep
    elif [[ "$Host_ENC" =~ (.*)"WPA"(.*) ]]; then attack_wpa
    else attack_opn; fi
}

menu_crack(){
    check_function crack_wep || source $path/crack/main
		[[ ! -e $DUMP_PATH/$Host_MAC-01.cap ]] && { error $"You have no IVS, please attack first." ; return; }
			while true; do
                mkmenu $"WEP/WPA Cracking Options" $"Crack with manual dictionary" $"Standard aircrack-ng" $"Return to menu" 
                case $choice in
					1 ) export crack_extra_opts=" -w $WORDLIST"; selectcracking; break ;;
					2 ) selectcracking ; break ;;
					3 ) $clear; break;;
					* ) echo $"Unknown response. Try again" ;;
				esac
			done
}

menu_type(){
    if [ "$AUTO" == 1 ]; then ENCRYPT="$1"; choosescan; return; fi
    if [ "$AUTO" == 2 ]; then ENCRYPT="$1"; choosescan; return; fi

    while true; do echo
        mkmenu $"Select encryption" "No filter" "OPN (open)" "WEP" "WPA" "WPA1" "WPA2" $"Return to main menu"
        case $choice in
            1 ) ENCRYPT=""     ; choosescan; break ;;
            2 ) ENCRYPT="OPN"  ; choosescan; break ;;
            3 ) ENCRYPT="WEP"  ; choosescan; break ;;
            4 ) ENCRYPT="WPA"  ; choosescan; break ;;
            5 ) ENCRYPT="WPA1" ; choosescan; break ;;
            6 ) ENCRYPT="WPA2" ; choosescan; break ;;
            7 ) break;;
             * ) echo $"Unknown response. Try again"   ;;
         esac
    done
}

choosescan(){
    if [ "$AUTO" == 1 ]; then Scan; return; fi
    if [ "$AUTO" == 2 ]; then Scan; return; fi

    while true; do
        mkmenu $"Channel" $"Channel Hoping" $"Specific Channel"
        case $choice in
            1 ) Scan 1 ;break;;
            2 ) Scanchan;break;;
            * ) echo $"Unknown response. Try again" ;;
        esac
    done
}

select_ap(){
    check_function select_target || source $path/internal/scansel
		if [ -e $DUMP_PATH/dump-01.csv ]; then
            cat $DUMP_PATH/dump-01.csv|sed -e '1,/BSSID/ s/.*//' -e '/Station/,$ s/.*//'|tac > $DUMP_PATH/dump-02.csv 2>/dev/null
            filter_ssid
			selectAp; $clear
			if [ "$Host_SSID" = $'\r' ]; then blankssid;
			elif [ "$Host_SSID" = "No SSID has been detected" ]; then blankssid; fi
			select_target; target
		else $clear && markwarn $'ERROR: You have to scan for targets first'; fi
}

menu_extra(){ 
    check_function menu_options || source $path/attacks/others
    while true; do
        mkmenu $"Extra tools" $"Airoscript Options" $"Advanced tools" $"External plugins" $'User interfaces' $"Return to main menu"
        case $choice in
            1) menu_options; $clear; break;;
            2) othertmenu; $clear; break;;
            3) menu_plugins; $clear; break;;
			3) menu_interfaces; $clear; break;;
            4) $clear;break ;;
            * ) echo $"Unknown response. Try again" ;;
        esac
    done
}

menu_interfaces(){
	mkmenu "User interfaces" "${interfaces[@]}"
	_source $path/plugins/${interfaces[$choice - 1]}
}

menu_plugins(){
    a_menu=( "${p_menu[@]}" ); a_menu+=("Return to main menu"); mkmenu "External plugins" "${a_menu[@]}"
    $(sed 's/ /_/g' <<< ${p_menu[$choice - 1]}) || { $clear; warn "Wrong option, try again"; menu_plugins; }
}
