# Copyright (C)  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.



Scanchan(){
    export SCAN=1; # TODO Create a mkinfobox function.
    single_question "${mark}Enter channel (single, range, or comma-separated): "
    channel_number="$ans"; set -- ${channel_number}
    rm -rf $DUMP_PATH/dump* &>/dev/null; monmode $wifi $channel_number

    execute "Scanning for targets on channel $channel_number" $AIRODUMP -w $DUMP_PATH/dump --channel $channel_number --encrypt $ENCRYPT -a $wifi
    export SCAN=0
}

select_target(){
    if [ "$AUTO" == 2 ]; then return; fi
    while true; do
        echo "Selecting client"
        mkmenu "Client Selection" "Detect clients" "Select from associated clients" "Select from all clients" "Manually enter client MAC" "No select client"
        case $choice in
            1 ) detectclients ; break ;;
            2 ) SelectClient dump-01.csv filter; break ;;
            3 ) SelectClient dump-01.csv nofilter; break;; 
            4 ) single_question "Type in client mac now"; Client_MAC="$ans"; set -- ${Client_MAC} ; break ;;
            5 ) break ;;
            * ) echo -e "`gettext \"Unknown response. Try again\"`";;
          esac
    done
}

detectclients(){
    check_function deauthall || source $path/attacks/auth
    if [ "$AUTO" == "2" ]; then return; fi
    $iwconfig $wificard channel $Host_CHAN; deauthall &
    export AUTO=0; export QUIET=""; export wait_for_execute=1; 
    warn "${mark}Press ctrl+C when you've collected as many clients as you want"
    capture; 
    export wait_for_execute=0;
    SelectClient $Host_MAC-01.csv filter
}

SelectClient(){
    HOST=(`cat $DUMP_PATH/$1 | grep -a $Host_MAC | awk '{ print $1 }'| grep -a -v 00:00:00:00| grep -a -v $Host_MAC|sed 's/,//'`)
    if [ "$2" == "nofilter" ]; then HOST=(`cat $DUMP_PATH/dump-01.csv | grep -a "0.:..:..:..:.." | awk '{ print $1 }'| grep -a -v 00:00:00:00`); fi
    if [ "$AUTO" == "1" ]; then return; fi;  [[ -z $HOST ]] && return
    mkmenu "Select client" ${HOST[@]}
    export Client_MAC=` echo ${HOST[$choice -1]} | awk '{split($1, info, "," );  print info[1]  }'`; 
}
