airgorah 0.3.0

A WiFi auditing software that can perform deauth attacks and passwords cracking
use crate::backend;
use crate::frontend::interfaces::*;

use glib::clone;
use gtk4::prelude::*;
use gtk4::*;
use regex::Regex;
use std::rc::Rc;

fn connect_random_mac_button(app_data: Rc<AppData>) {
    app_data
        .settings_gui
        .random_mac
        .connect_toggled(clone!(@strong app_data => move |_| {
            app_data.settings_gui.mac_entry.set_sensitive(false);
            app_data.settings_gui.save_but.set_sensitive(true);
        }));
}

fn connect_default_mac_button(app_data: Rc<AppData>) {
    app_data
        .settings_gui
        .default_mac
        .connect_toggled(clone!(@strong app_data => move |_| {
            app_data.settings_gui.mac_entry.set_sensitive(false);
            app_data.settings_gui.save_but.set_sensitive(true);
        }));
}

fn connect_specific_mac_button(app_data: Rc<AppData>) {
    app_data
        .settings_gui
        .specific_mac
        .connect_toggled(clone!(@strong app_data => move |_| {
            app_data.settings_gui.mac_entry.set_sensitive(true);
            app_data.settings_gui.mac_entry.notify("text");
        }));
}

fn connect_mac_entry(app_data: Rc<AppData>) {
    app_data.settings_gui.mac_entry.connect_text_notify(
        clone!(@strong app_data => move |this| {
            let mac_regex = Regex::new(r"^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})|([0-9a-fA-F]{4}\\.[0-9a-fA-F]{4}\\.[0-9a-fA-F]{4})$").unwrap();
            let entry = this.text().to_string();

            match mac_regex.is_match(&entry) {
                true => app_data.settings_gui.save_but.set_sensitive(true),
                false => app_data.settings_gui.save_but.set_sensitive(false),
            }
        }),
    );
}

fn connect_save_but(app_data: Rc<AppData>) {
    app_data
        .settings_gui
        .save_but
        .connect_clicked(clone!(@strong app_data => move |_| {
            let mut mac_address = crate::types::Settings::default().mac_address;

            if app_data.settings_gui.random_mac.is_active() {
                mac_address = "random".to_string()
            } else if app_data.settings_gui.default_mac.is_active() {
                mac_address = "default".to_string()
            } else if app_data.settings_gui.specific_mac.is_active() {
                mac_address = app_data.settings_gui.mac_entry.text().to_string()
            }

            let display_hidden_ap = app_data.settings_gui.display_hidden_ap.is_active();
            let kill_network_manager = app_data.settings_gui.kill_network_manager.is_active();

            let settings = crate::types::Settings {
                mac_address,
                display_hidden_ap,
                kill_network_manager,
            };

            backend::save_settings(settings);

            app_data.settings_gui.window.hide();
        }));
}

pub fn connect(app_data: Rc<AppData>) {
    connect_random_mac_button(app_data.clone());
    connect_default_mac_button(app_data.clone());
    connect_specific_mac_button(app_data.clone());
    connect_mac_entry(app_data.clone());
    connect_save_but(app_data);
}