redundant
[dumbwifi.git] / dumbwifi
bloba3d9ca8d0dbf19e508fb7a8eac002e459ce62eec
1 #!/usr/bin/env python
3 # Author: Martin Matusiak <numerodix@gmail.com>
4 # Licensed under the GNU Public License, version 3.
6 # <desc> Dumb network connection manager </desc>
8 import sys
9 import time
11 from conf import config
12 import network
13 import opts
14 from output import logger
15 import output
16 import procs
17 import ui
20 def init():
21 # read config file
22 ui.init_routine()
24 # make sure we are the only instance
25 procs.kill_previous_instances()
27 # read command line options
28 process_opts()
30 # do sanity check
31 if config.opts.checktools:
32 ui.tools_check()
34 # kill switch was triggered
35 if config.opts.kill:
36 sys.exit(0)
38 # run in foreground if we want debug mode
39 if not config.opts.debug and not config.opts.wardrive:
40 procs.daemonize()
42 # drop existing ip
43 if config.opts.dropip or config.opts.wardrive:
44 [network.iface_down(i.interface) for i in config.interfaces]
46 # write our pid to the pidfile
47 procs.write_pid()
50 def process_opts():
51 p = opts.create_parser()
52 p.add_option("-i", "--interface", dest="interface", action="append",
53 metavar="iface", help="Prefer this interface")
54 p.add_option("-n", "--network", dest="network", action="append",
55 metavar="name[,key]", help="Prefer this wireless network")
56 p.add_option("-w", "--wardrive", action="store_true",
57 help="Attempt to connect to any open network in range")
58 p.add_option("-d", "--debug", action="store_true",
59 help="Run in foreground (debug mode)")
60 p.add_option("-D", "--dropip", action="store_true",
61 help="Release existing ip")
62 p.add_option("-C", "--checktools", action="store_true",
63 help="Do sanity check (check for missing tools)")
64 p.add_option("-k", "--kill", action="store_true",
65 help="Stop %s" % config.program_name)
66 opts.read_opts(p)
69 def obtain_ip(wardrive=None):
71 # make current state be previous state
72 config.had_ip = config.have_ip
74 # set output status
75 if config.have_ip: logger.mute()
76 else: logger.unmute()
78 if ui.ip_check():
79 config.have_ip = True
80 return
81 else: config.have_ip = False
83 # if we had an ip on the previous run, but we just discovered it's gone,
84 # we need to toggle screen output and re-run ip check
85 if config.had_ip and not config.have_ip: return
88 if not wardrive:
89 prefer_wired = ui.choose_medium()
91 if prefer_wired and ui.check_wired_link():
92 # find top prority wired interface
93 iface = config.interfaces.get_top(pred=lambda x: x.medium == "wired")
94 if ui.request_ip(iface): return
96 # find top prority wireless interface
97 iface = config.interfaces.get_top(pred=lambda x: x.medium == "wireless")
98 wifi_nets = ui.scan_wifi_networks(iface, wardrive=wardrive)
100 if wifi_nets:
101 for net in wifi_nets:
102 if ui.request_ip(iface, net=net): return
105 def main():
106 init()
107 while 1:
108 obtain_ip(wardrive=config.opts.wardrive)
109 time.sleep(1)
113 if __name__ == "__main__":
114 main()