-
Notifications
You must be signed in to change notification settings - Fork 4
/
commotion_applet_support.py
executable file
·511 lines (425 loc) · 19.4 KB
/
commotion_applet_support.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
#!/usr/bin/python
import dbus.mainloop.glib ; dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
import json
import NetworkManager
import os
import sys
import urllib2
import pprint
import subprocess
import commotionc
try:
from gi.repository import Gtk, GObject
except: # Can't use ImportError, as gi.repository isn't quite that nice...
import gtk as Gtk
class JsonInfo():
def __init__(self, baseurl='http://localhost:9090'):
self.baseurl = baseurl
self.jsondecoder = json.JSONDecoder()
def dump(self, data='/all'):
try:
return urllib2.urlopen(self.baseurl + data).read()
except urllib2.URLError:
return None
def dict(self, data='/all'):
dump = self.dump(data)
if dump:
return self.jsondecoder.decode(dump)
class MeshStatus():
def __init__(self, portinghacks):
self.port = portinghacks
self.jsoninfo = JsonInfo()
self.imagedir = '/usr/share/icons/hicolor/32x32/actions'
# liststore data: IP, LQ, NLQ, have_internet, have_HNA
self.liststore = Gtk.ListStore(str, int, int, bool, bool)
self.mesh_connected = False
self.myip = ''
self.other_routes = []
self.default_routes = []
def _set_icon(self, cell, filename=None):
if filename:
fullfilename = os.path.join(self.imagedir, filename)
cell.set_property("pixbuf", self.port.pixbuf_new_from_file(fullfilename))
else:
cell.set_property("pixbuf", None)
def _default_route_icon(self, column, cell, model, iter, destroy=None):
if model[iter][3]:
self._set_icon(cell, 'default_route.png')
else:
self._set_icon(cell)
def _other_route_icon(self, column, cell, model, iter, destroy=None):
if model[iter][4]:
self._set_icon(cell, 'other_route.png')
else:
self._set_icon(cell)
def dump(self, which='/all'):
return JsonInfo().dump(which)
def update(self):
self.liststore.clear()
links_hna = self.jsoninfo.dict('/links/hna')
if links_hna:
self.mesh_connected = True
self.other_routes = []
self.default_routes = []
for i in links_hna['hna']:
if i['destination'] == '0.0.0.0':
self.default_routes.append(i['gateway'])
else:
self.other_routes.append(i['gateway'])
for link in links_hna['links']:
ip = link['remoteIP']
lqPercent = int(link['linkQuality']*100)
nlqPercent = int(link['neighborLinkQuality']*100)
cell = [ ip, lqPercent, nlqPercent ]
if ip in self.default_routes:
cell.append(True)
else:
cell.append(False)
if ip in self.other_routes:
cell.append(True)
else:
cell.append(False)
self.liststore.append(cell)
else:
self.mesh_connected = False
if 'link' in locals():
self.myip = link['localIP']
else:
self.myip = ''
return True
def on_delete_event(self, widget, event):
GObject.source_remove(self.timeout_id)
return False
def show(self):
self.update()
self.timeout_id = GObject.timeout_add(5000, self.update)
window = Gtk.Window()
window.set_title('Mesh Status (Links and HNA)')
window.set_resizable(False)
window.connect('delete-event', self.on_delete_event)
vbox = Gtk.VBox(homogeneous=False, spacing=5)
window.add(vbox)
infobar = Gtk.InfoBar()
if self.mesh_connected:
infobar.set_message_type(self.port.MESSAGE_OTHER)
label = Gtk.Label('mesh address: ' + self.myip)
else:
infobar.set_message_type(self.port.MESSAGE_ERROR)
label = Gtk.Label('Not connected to a mesh!')
content = infobar.get_content_area()
content.set_homogeneous(False)
content.pack_start(label, False, True, 0)
if self.myip in self.other_routes:
image = Gtk.Image()
image.set_from_file(os.path.join(self.imagedir, 'other_route.png'))
content.pack_start(image, False, True, 0)
need_filler = True
else:
need_filler = False
if self.myip in self.default_routes:
image = Gtk.Image()
image.set_from_file(os.path.join(self.imagedir, 'default_route.png'))
# leave empty space so the internet icon is properly aligned
if need_filler:
content.pack_start(image, False, True, 0)
else:
content.pack_end(image, False, True, 0)
vbox.pack_start(infobar, False, True, 0)
treeview = Gtk.TreeView(model=self.liststore)
selection = treeview.get_selection()
selection.set_mode(self.port.SELECTION_NONE)
vbox.pack_start(treeview, True, True, 3)
column = Gtk.TreeViewColumn("Link IP Address")
column.set_alignment(0.5)
treeview.append_column(column)
cell = Gtk.CellRendererText()
column.pack_start(cell, False)
column.add_attribute(cell, "text", 0)
column = Gtk.TreeViewColumn("LQ")
column.set_alignment(0.5)
column.set_resizable(True)
treeview.append_column(column)
cell = Gtk.CellRendererProgress()
column.pack_start(cell, True)
column.add_attribute(cell, "value", 1)
column = Gtk.TreeViewColumn("NLQ")
column.set_alignment(0.5)
column.set_resizable(True)
treeview.append_column(column)
cell = Gtk.CellRendererProgress()
column.pack_start(cell, True)
column.add_attribute(cell, "value", 2)
column = Gtk.TreeViewColumn("HNA?")
column.set_alignment(0.5)
column.set_resizable(False)
treeview.append_column(column)
cell = Gtk.CellRendererPixbuf()
column.pack_start(cell, False)
column.set_cell_data_func(cell, self._other_route_icon)
column = Gtk.TreeViewColumn("Internet?")
column.set_alignment(0.5)
column.set_resizable(False)
treeview.append_column(column)
cell = Gtk.CellRendererPixbuf()
column.pack_start(cell, False)
column.set_cell_data_func(cell, self._default_route_icon)
window.show_all()
class PortingHacks():
'''
This is a collection of things that are quite similar across GTK versions,
but not named the same. The setup code for each GTK version should set
these.
'''
BUTTONS_CLOSE = None
DIALOG_DESTROY_WITH_PARENT = None
MESSAGE_ERROR = None
MESSAGE_OTHER = None
FILE_CHOOSER_ACTION_SAVE = None
RESPONSE_CANCEL = None
RESPONSE_OK = None
SELECTION_NONE = None
STOCK_ABOUT = None
pixbuf_new_from_file = None
class CommotionMeshApplet():
svg_dir = '/usr/share/icons/hicolor/scalable/apps'
nm_icon_dir = '/usr/share/icons/hicolor/22x22/apps'
def __init__(self, portinghacks):
self.commotion = commotionc.CommotionCore('commotion-mesh-applet')
self.port = portinghacks
self.meshstatus = MeshStatus(portinghacks)
self.menu = Gtk.Menu()
# update the menu whenever NetworkManager changes
NetworkManager.NetworkManager.connect_to_signal('StateChanged', self.create_menu)
NetworkManager.NetworkManager.connect_to_signal('PropertiesChanged', self.create_menu)
NetworkManager.NetworkManager.connect_to_signal('DeviceAdded', self.create_menu)
NetworkManager.NetworkManager.connect_to_signal('DeviceRemoved', self.create_menu)
def get_visible_adhocs(self):
actives = []
nets = []
strengths = dict()
for ac in NetworkManager.NetworkManager.ActiveConnections: #This won't work if there isn't already an active connection
for d in ac.Devices:
if d.Managed and d.DeviceType == NetworkManager.NM_DEVICE_TYPE_WIFI:
wireless = d.SpecificDevice()
aap = wireless.ActiveAccessPoint
if aap.Mode == NetworkManager.NM_802_11_MODE_ADHOC:
channel = (aap.Frequency - 2407) / 5
actives.append(tuple([aap.Ssid, aap.HwAddress, channel]))
for ap in wireless.GetAccessPoints():
if ap.Mode == NetworkManager.NM_802_11_MODE_ADHOC:
channel = (ap.Frequency - 2407) / 5
nets.append(tuple([ap.Ssid, ap.HwAddress, channel]))
strengths[ap.Ssid] = ap.Strength
return [tuple(actives), tuple(nets), strengths]
def add_menu_about(self):
self.add_menu_item(self.port.STOCK_ABOUT, self.show_about)
def add_menu_quit(self):
self.add_menu_item("Quit", self.do_exit)
def add_menu_separator(self):
sep = Gtk.SeparatorMenuItem()
sep.show()
self.menu.add(sep)
def add_menu_item(self, name, function, imagefile=None):
item = Gtk.ImageMenuItem()
if imagefile:
icon = Gtk.Image()
icon.set_from_file(imagefile)
item.set_image(icon)
item.set_always_show_image(True)
item.set_label(name)
item.show()
item.connect( "activate", function)
self.menu.add(item)
def add_menu_label(self, name):
item = Gtk.ImageMenuItem(name)
item.set_sensitive(False)
item.show()
self.menu.add(item)
def add_visible_profile_menu_item(self, name, function, strength):
if strength > 98:
self.add_menu_item(name, function, os.path.join(self.nm_icon_dir, 'nm-signal-100.png'))
elif strength >= 75:
self.add_menu_item(name, function, os.path.join(self.nm_icon_dir, 'nm-signal-75.png'))
elif strength >= 50:
self.add_menu_item(name, function, os.path.join(self.nm_icon_dir, 'nm-signal-50.png'))
elif strength >= 25:
self.add_menu_item(name, function, os.path.join(self.nm_icon_dir, 'nm-signal-25.png'))
else:
self.add_menu_item(name, function, os.path.join(self.nm_icon_dir, 'nm-signal-00.png'))
def choose_profile(self, *arguments):
connections = NetworkManager.Settings.ListConnections()
pprint.pprint(connections)
connections = dict([(x.GetSettings()['connection']['id'], x) for x in connections])
name = arguments[0].get_label()
try:
conn = connections[name]
except KeyError as e:
self.commotion.log('error: ' + name + ' does not exist as a connection (' + str(e) + ')')
return
ctype = conn.GetSettings()['connection']['type']
if ctype != '802-11-wireless':
self.commotion.log(name + ' is not a wifi device!')
return
devices = NetworkManager.NetworkManager.GetDevices()
for dev in devices:
if dev.DeviceType == NetworkManager.NM_DEVICE_TYPE_WIFI and dev.State > 20: #dev.State values 0, 10, and 20 indicate that interface is in an unusable state
#dev.Interface, dev.Driver
break
else:
self.commotion.log('No wifi device found!')
return
wpa_ver = subprocess.check_output(['/sbin/wpa_supplicant', '-v']).split()[1].strip('v')
if int(wpa_ver.split('.')[0]) < 1 and '802-11-wireless-security' in conn.GetSettings():
self.commotion.log('wpa_supplicant version ' + wpa_ver + ' does not support ad-hoc encryption. Starting replacement version...')
## dev.Disconnect() Necessary?
subprocess.Popen(['gksu', '/usr/share/pyshared/fallback.py ' + name + ' up'])
else:
NetworkManager.NetworkManager.ActivateConnection(conn, dev, "/")
def show_menu(self, widget, event, applet):
if event.type == Gtk.gdk.BUTTON_PRESS and event.button == 1:
self.menu.popup( None, None, None, event.button, event.time )
widget.emit_stop_by_name("button_press_event")
def create_menu(self, ignored=None):
# empty the menu first before filling it up
for widget in self.menu.get_children():
self.menu.remove(widget)
header_added = False
actives, visibles, strengths = self.get_visible_adhocs()
profiles = ((params['ssid'], params['bssid'], int(params['channel'])) for profile, params in self.commotion.readProfiles().iteritems())
for profile in actives:
if profile in profiles:
if not header_added:
self.add_menu_label('Active Mesh')
header_added = True
self.add_menu_item(profile[0], self.choose_profile,
os.path.join(self.nm_icon_dir, 'nm-adhoc.png'))
self.add_menu_label('BSSID: ' + profile[1])
self.add_menu_label('Channel: ' + str(profile[2]))
self.add_menu_separator()
self.add_menu_item('Browse Local Apps...', self.launch_app_browser)
self.add_menu_item('Show Mesh Status', self.show_mesh_status)
self.add_menu_item('Save Mesh Status To File...', self.save_mesh_status_to_file)
self.add_menu_item('Disconnect From Mesh', self.disconnect)
self.add_menu_separator()
self.add_menu_label('Available Profiles')
for profile in profiles:
if profile in actives:
continue
elif profile in visibles:
self.add_visible_profile_menu_item(profile[0], self.choose_profile,
strengths[profile[0]])
else:
self.add_menu_item(profile[0], self.choose_profile)
self.add_menu_item('Edit Mesh Network Profiles...', self.edit_profiles)
#self.add_menu_item('Show Debug Log', self.show_debug_log)
if not header_added:
self.add_menu_label('Browse Local Apps...')
self.add_menu_label('Show Mesh Status')
self.add_menu_label('Save Mesh Status To File...')
self.add_menu_label('Disconnect From Mesh')
self.add_menu_separator()
self.add_menu_about()
self.add_menu_quit()
return True
def edit_profiles(self, *arguments):
subprocess.Popen(['gksudo', 'xterm -e vi ' + self.commotion.profiledir])
instructions = Gtk.MessageDialog(toplevel,
self.port.DIALOG_DESTROY_WITH_PARENT,
self.port.MESSAGE_INFO,
(Gtk.BUTTONS_OK),
'All mesh profile files are stored in this folder. Make any desired changes to any of the profiles (as the root user), and then close the file browser.')
instructions.run()
instructions.destroy()
def launch_app_browser(self, *arguments):
os.system('xdg-open http://localhost:8080 &')
def show_mesh_status(self, *arguments):
self.meshstatus.show()
#def show_debug_log(self, *arguments):
# os.system('xdg-open /tmp/nm-dispatcher-olsrd.log &')
def save_mesh_status_to_file(self, *arguments):
toplevel = arguments[0].get_toplevel()
dialog = Gtk.FileChooserDialog("Save Mesh Status to File...",
toplevel,
self.port.FILE_CHOOSER_ACTION_SAVE,
(Gtk.STOCK_CANCEL, self.port.RESPONSE_CANCEL,
Gtk.STOCK_SAVE, self.port.RESPONSE_OK))
dialog.set_default_response(self.port.RESPONSE_OK)
dialog.set_do_overwrite_confirmation(True)
file_filter = Gtk.FileFilter()
file_filter.add_pattern("*.json")
file_filter.set_name("JSON (*.json)")
dialog.add_filter(file_filter)
file_filter = Gtk.FileFilter()
file_filter.add_pattern("*")
file_filter.set_name("All files (*.*)")
dialog.add_filter(file_filter)
response = dialog.run()
msg = None
if response == self.port.RESPONSE_OK:
filename = dialog.get_filename()
if not filename.endswith('.json'):
filename += '.json'
dump = self.meshstatus.dump()
if dump:
with open(filename, 'w') as f:
f.write(dump)
else:
msg = Gtk.MessageDialog(toplevel,
self.port.DIALOG_DESTROY_WITH_PARENT,
self.port.MESSAGE_ERROR,
(Gtk.BUTTONS_CLOSE),
'Nothing was written because olsrd is not running, there is no active mesh profile!')
msg.run()
msg.destroy()
dialog.destroy()
def disconnect(self, *arguments):
if 'asleep' in subprocess.check_output(['nmcli', 'nm', 'status']):
subprocess.call(['gksu', '/usr/share/pyshared/fallback.py', 'all down'])
else:
for ac in NetworkManager.NetworkManager.ActiveConnections:
for d in ac.Devices:
if d.Managed and d.DeviceType == NetworkManager.NM_DEVICE_TYPE_WIFI:
NetworkManager.NetworkManager.DeactivateConnection(ac)
def show_about(self, *arguments):
about_dialog = Gtk.AboutDialog()
about_dialog.set_destroy_with_parent(True)
about_dialog.set_program_name('Commotion Mesh Applet')
about_dialog.set_comments('Commotion is an open-source communication tool that uses mobile phones, computers, and other wireless devices to create decentralized mesh networks.')
about_dialog.set_website('https://commotionwireless.net/')
icon = self.port.pixbuf_new_from_file(os.path.join(self.svg_dir,
'commotion.svg'))
about_dialog.set_logo(icon)
def close(dialog, response, editor):
dialog.destroy()
about_dialog.connect("response", close, None)
about_dialog.show_all()
def do_exit(self, *arguments):
sys.exit()
def setup_gtk2_applet(self, applet):
icon = self.port.pixbuf_new_from_file(os.path.join(self.svg_dir,
'commotion-mesh-disconnected.svg'))
image = Gtk.Image()
image.set_from_pixbuf(icon.scale_simple(22, 22, Gtk.gdk.INTERP_BILINEAR))
button = Gtk.Button()
button.set_relief(Gtk.RELIEF_NONE)
button.set_image(image)
button.connect("button_press_event", self.show_menu, applet)
applet.add(button)
applet.show_all()
def applet_factory(applet, iid, data = None):
# set GTK2-specific versions of things
port = PortingHacks()
port.BUTTONS_CLOSE = Gtk.BUTTONS_CLOSE
port.FILE_CHOOSER_ACTION_SAVE = Gtk.FILE_CHOOSER_ACTION_SAVE
port.MESSAGE_ERROR = Gtk.MESSAGE_ERROR
port.MESSAGE_OTHER = Gtk.MESSAGE_OTHER
port.RESPONSE_CANCEL = Gtk.RESPONSE_CANCEL
port.RESPONSE_OK = Gtk.RESPONSE_OK
port.DIALOG_DESTROY_WITH_PARENT = Gtk.DIALOG_DESTROY_WITH_PARENT
port.SELECTION_NONE = Gtk.SELECTION_NONE
port.STOCK_ABOUT = Gtk.STOCK_ABOUT
port.pixbuf_new_from_file = Gtk.gdk.pixbuf_new_from_file
cma = CommotionMeshApplet(port)
cma.setup_gtk2_applet(applet)
print('Factory started')
return True