-
Notifications
You must be signed in to change notification settings - Fork 1
/
setpurplestatus.py
executable file
·50 lines (43 loc) · 1.64 KB
/
setpurplestatus.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
#!/usr/bin/env python3
# purpose: Set the correct pidgin status message, depending on network connection
# copyright: B1 Systems GmbH <[email protected]>, 2015.
# license: GPLv3+, http://www.gnu.org/licenses/gpl-3.0.html
# author: Jan-Marten Brueggemann <[email protected]>, 2015.
import yaml
import netifaces
import os
from netaddr import IPNetwork, IPAddress
CONFIG_PATH = os.getenv('HOME') + '/.setpurplestatusrc'
PURPLEREMOTE = '/usr/bin/purple-remote'
if not os.path.isfile(CONFIG_PATH):
print("Please create config file in ~/.setpurplestatusrc")
print("Example:")
print("interfaces:")
print("\t- 'eth0'")
print("\t- 'wlan0'")
print("networks:")
print("\t'192.168.0.0/24':")
print("\t\tstatus: 'at home'")
print("\t'10.0.0.0/16':")
print("\t\tstatus: 'at work'")
exit(2)
stream = open(CONFIG_PATH, 'r')
config = yaml.load(stream)
gws = netifaces.gateways()
print(gws['default'][netifaces.AF_INET][0])
# Get active network connections
active_interfaces = {}
for interface in config['interfaces']:
tmpifaddress = netifaces.ifaddresses(interface)
if netifaces.AF_INET in tmpifaddress:
active_interfaces[interface] = []
for network in tmpifaddress[netifaces.AF_INET]:
active_interfaces[interface].append(network['addr'])
# Check matching of configured networks
for network in config['networks']:
for addresses in active_interfaces.values():
for address in addresses:
if IPAddress(address) in IPNetwork(network):
os.execv(PURPLEREMOTE, [PURPLEREMOTE, 'setstatus?message=' + config['networks'][network]['status']])
exit(0)
exit(1)