# 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(){
    mkmenu "Main Menu" "Attack   - Attack target" "Crack    - Get target key"  "Scan - Manually scan for targets" "Select - Reselect target" "Others   - Various utilities" "Autopawn - 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) menu_type;;
                4) select_ap;;
                5) menu_extra;;
                6) check_function autopwn || source $path/autocrack; autopwn;;
                7) check_function doexit || source $path/internal/exit; doexit;;
                *) $clear && _ "ERROR: Wrong number entered";;
            esac
        done  
    mainmenu
}

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
			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;;
             * ) _ "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;break;;
            2 ) Scanchan;break;;
            * ) _ "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;
		else $clear && _ '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" "Return to main menu"
        case $choice in
            1) menu_options; $clear; break;;
            2) othertmenu; $clear; break;;
            3) menu_plugins; $clear; break;;
            4) $clear;break ;;
            * ) _ "Unknown response. Try again" ;;
        esac
    done
}

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