Add check to verify that config file is not user readable
[dumbwifi.git] / util.py
blob80b6aaeb9d826a8a8c9d58245cb6509ef22715c4
1 #!/usr/bin/env python
3 import optparse
4 import sys
6 from conf import config
7 import regex
8 from sdict import sdict, sdictlist
11 # Read command line options
13 def main_read_opts():
14 p = optparse.OptionParser(add_help_option=None)
15 add = p.add_option
16 b = "store_true"
17 c = "callback"
18 add("-i", "--interface", dest="interface", action="append",
19 metavar="iface", help="Prefer this interface")
20 add("-n", "--network", dest="network", action="append",
21 metavar="name[,key]", help="Prefer this wireless network")
22 add("-w", "--wardrive", action=b,
23 help="Attempt to connect to any open network in range")
24 add("-d", "--debug", action=b, help="Run in foreground (debug mode)")
25 add("-D", "--dropip", action=b, help="Release existing ip")
26 add("-k", "--kill", action=b, help="Stop %s" % config.program_name)
27 add("-?", "--help", action=c, callback=help, help="Display this message")
28 opts = p.parse_args()[0]
29 config.opts.update( [(k, opts.__dict__[k]) for k in opts.__dict__] )
30 opts_init_networks()
31 opts_init_interfaces()
33 def scan_read_opts():
34 p = optparse.OptionParser(add_help_option=None)
35 add = p.add_option
36 b = "store_true"
37 c = "callback"
38 add("-i", "--interface", dest="interface", action="append",
39 metavar="iface", help="Prefer this interface")
40 add("-t", "--timeout", dest="timeout", action="store",
41 metavar="s", help="Scan for this many seconds, then exit")
42 add("-?", "--help", action=c, callback=opts_help, help="Display this message")
43 opts = p.parse_args()[0]
44 config.opts.update( [(k, opts.__dict__[k]) for k in opts.__dict__] )
45 opts_init_interfaces()
47 def opts_help(option, opt_str, value, parser):
48 print "Usage: %s [options]" % config.program_name
49 for o in parser.option_list:
50 var = o.metavar
51 if not o.metavar: var = ""
52 argument = "%s %s %s" % (o._short_opts[0], o._long_opts[0], var)
53 print " %s %s" % (argument.strip().ljust(25), o.help)
54 sys.exit(0)
56 def opts_init_networks():
57 # merge new nets info global list
58 if config.opts.network:
59 for (i, n) in enumerate(config.opts.network):
60 pair = n.split(",")
61 name = pair[0] ; pri = config.max_priortiy - i
62 d = sdict({ 'essid': name, 'priority': pri })
63 if len(pair) == 2: d['key'] = pair[1]
64 config.networks = config.networks.merge1(d, 'essid')
65 config.networks = negated_opts(config.networks, 'essid')
67 def opts_init_interfaces():
68 # merge new interfaces into global list
69 if config.opts.interface:
70 for (i, n) in enumerate(config.opts.interface):
71 pri = config.max_priortiy - i
72 d = sdict({'interface': n, 'priority': pri})
73 config.interfaces = config.interfaces.merge1(d, 'interface')
74 config.interfaces = negated_opts(config.interfaces, 'interface')
76 def negated_opts(dicts, key):
77 new_dicts = dicts
78 for (i, d) in enumerate(dicts):
79 val = regex.find1("^\^(.*)", d[key])
80 if val:
81 new_dicts.remove(i)
82 new_dicts = sdictlist(init_list=new_dicts.get_all(\
83 pred=lambda x: x[key] != val))
84 return new_dicts
86 def post_negate_nets(nets, key):
87 if not config.opts.network: return nets
89 new_nets = nets
90 for (i, n) in enumerate(config.opts.network):
91 pair = n.split(",")
92 key_input = pair[0]
93 key_value = regex.find1("^\^(.*)", key_input)
94 if key_value:
95 new_nets = sdictlist(init_list=\
96 nets.get_all(pred=lambda x: x[key] != key_value))
97 return new_nets