Update my email address
[wifi-radar.git] / wifi-radar
bloba41170c96673216291c28390f93af01af710de4f
1 #!/usr/bin/python -OO
2 # -*- coding: utf-8 -*-
4 # A utility for managing WiFi profiles on GNU/Linux.
6 # Copyright (C) 2004-2005 Ahmad Baitalmal <ahmad@baitalmal.com>
7 # Copyright (C) 2005 Nicolas Brouard <nicolas.brouard@mandrake.org>
8 # Copyright (C) 2005-2009 Brian Elliott Finley <brian@thefinleys.com>
9 # Copyright (C) 2006 David Decotigny <com.d2@free.fr>
10 # Copyright (C) 2006 Simon Gerber <gesimu@gmail.com>
11 # Copyright (C) 2006-2007 Joey Hurst <jhurst@lucubrate.org>
12 # Copyright (C) 2006, 2009 Ante Karamatic <ivoks@ubuntu.com>
13 # Copyright (C) 2009-2010,2014 Sean Robinson <robinson@tuxfamily.org>
14 # Copyright (C) 2010 Prokhor Shuchalov <p@shuchalov.ru>
16 # This program is free software; you can redistribute it and/or modify
17 # it under the terms of the GNU General Public License as published by
18 # the Free Software Foundation; version 2 of the License.
20 # This program is distributed in the hope that it will be useful,
21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 # GNU General Public License in LICENSE.GPL for more details.
25 # You should have received a copy of the GNU General Public License
26 # along with this program; if not, write to:
27 # Free Software Foundation, Inc.
28 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
30 # http://wifi-radar.tuxfamily.org/
32 # See CREDITS file for more contributors.
33 # See HISTORY file for, well, changes.
35 # NOTE: Remove the '-OO' from '#!/usr/bin/python -OO' in the first line to
36 # turn on console debugging.
39 from __future__ import print_function
41 # Set up a logging framework as early as possible.
42 import logging
43 logger = logging.getLogger("wifiradar")
44 generic_formatter = logging.Formatter(
45 fmt='%(asctime)s.%(msecs)d %(levelname)s %(name)s: %(message)s',
46 datefmt='%Y-%m-%d %H:%M:%S')
47 consoleLogHandler = logging.StreamHandler()
48 consoleLogHandler.setFormatter(generic_formatter)
49 logger.addHandler(consoleLogHandler)
50 # Start permissive and raise the level later.
51 if __debug__:
52 logger.setLevel(logging.DEBUG)
54 import os
55 import sys
57 import wifiradar
58 import wifiradar.config
59 from wifiradar.misc import _, WIFI_RADAR_VERSION
60 import wifiradar.gui.g2 as ui
61 import wifiradar.gui.g2.transients as transients
64 # Where the conf file should live could be different for your distro. Please change
65 # at install time with "make install sysconfdir=/etc/wifi-radar" or similar. -BEF-
67 CONF_FILE = "/etc/wifi-radar/wifi-radar.conf"
69 #os.environ['LC_MESSAGES'] = 'C'
71 # Load our conf file and known profiles
72 # Defaults, these may get overridden by values found in the conf file.
73 config_defaults = { # The network interface you use.
74 # Specify "auto_detect" as the interface to make wifi-radar automatically detect your wireless card.
75 'interface': "auto_detect",
76 # How long should the scan for access points last?
77 #'scan_timeout': '5',
78 # You may set this to true for cards that require a "commit" command with iwconfig
79 'commit_required': 'False',
80 # You may set this to true for cards that require the interface to be brought up first
81 'ifup_required': 'False',
82 # set the location and verbosity of the log file
83 'logfile': '/var/log/wifi-radar.log',
84 'loglevel': '50',
85 # Set the location of several important programs
86 'iwlist_command': '/sbin/iwlist',
87 'iwconfig_command': '/sbin/iwconfig',
88 'ifconfig_command': '/sbin/ifconfig',
89 'route_command': '/sbin/route',
90 'auto_profile_order': '[]',
91 'version': WIFI_RADAR_VERSION }
93 config_dhcp = { # DHCP client
94 'command': '/sbin/dhcpcd',
95 # How long to wait for an IP addr from DHCP server
96 'timeout': '30',
97 # Arguments to use with DHCP client on connect
98 'args': '-D -o -i dhcp_client -t %(timeout)s',
99 # Argument to use with DHCP client on disconnect
100 'kill_args': '-k',
101 # The file where DHCP client PID is written
102 'pidfile': '/etc/dhcpc/dhcpcd-%(interface)s.pid' }
104 config_wpa = { # WPA Supplicant
105 'command': '/usr/sbin/wpa_supplicant',
106 # Arguments to use with WPA Supplicant on connect
107 'args': '-B -i %(interface)s -c %(configuration)s -D %(driver)s -P %(pidfile)s',
108 # Arguments to use with WPA Supplicant on disconnect
109 'kill_command': '',
110 # Where the WPA Supplicant config file can be found
111 'configuration': '/etc/wpa_supplicant.conf',
112 # Driver to use with WPA Supplicant
113 'driver': 'wext',
114 # The file where WPA Supplicant PID is written
115 'pidfile': '/var/run/wpa_supplicant.pid' }
117 # initialize config, with defaults
118 config = wifiradar.config.ConfigFileManager(CONF_FILE, config_defaults)
119 config.set_section("DHCP", config_dhcp)
120 config.set_section("WPA", config_wpa)
122 if not os.path.isfile(CONF_FILE):
123 config.set_bool_opt('DEFAULT', 'new_file', True)
124 else:
125 if not os.access(CONF_FILE, os.R_OK):
126 print("Can't open {FILE}".format(FILE=CONF_FILE))
127 print("Are you root?")
128 sys.exit()
129 try:
130 config.read()
131 except (NameError, SyntaxError) as e:
132 error_message = _('A configuration file from a pre-2.0 version '
133 'of WiFi Radar was found at {FILE}.\n\nWiFi Radar v2.0.x does '
134 'not read configuration files from previous versions. ')
135 if isinstance(e, NameError):
136 error_message += _('Because {FILE} may contain information '
137 'that you might wish to use when configuring WiFi Radar '
138 '{VERSION}, rename this file and run the program again.')
139 elif isinstance(e, SyntaxError):
140 error_message += _('The old configuration file is probably '
141 'empty and can be removed. Rename {FILE} if you want '
142 'to be very careful. After removing or renaming '
143 '{FILE}, run this program again.')
144 error_message = error_message.format(FILE=CONF_FILE,
145 VERSION=WIFI_RADAR_VERSION)
146 error_dlg = transients.ErrorDialog(None, error_message)
147 del error_dlg
148 logger.critical(error_message)
149 sys.exit()
150 else:
151 if not __debug__:
152 logger.setLevel(self.config.get_opt_as_int('DEFAULT', 'loglevel'))
155 ####################################################################################################
156 # Make so we can be imported
157 if __name__ == "__main__":
158 if len(sys.argv) > 1 and (sys.argv[1] == '--version' or sys.argv[1] == '-v'):
159 print(_('WiFi Radar version {VERSION}').format(VERSION=WIFI_RADAR_VERSION))
160 elif len(sys.argv) > 1 and (sys.argv[1] == '--help' or sys.argv[1] == '-h'):
161 print(_('WiFi Radar version {VERSION}\n'
162 'For help, check man pages for wifi-radar and '
163 'wifi-radar.conf,\n'
164 'or visit http://wifi-radar.tuxfamily.org/').format(
165 VERSION=WIFI_RADAR_VERSION))
166 else:
167 wifiradar.Main(config)