-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpbxforwarderservice.py
172 lines (136 loc) · 5.72 KB
/
pbxforwarderservice.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
#!/usr/bin/python
# encoding:utf-8
import os
import re
import sys
import logging
import logging.handlers
from time import sleep
import signal
from urllib import urlencode
from urllib2 import Request, urlopen
import requests
class Log(object):
"""Custom log to be friendly with OSX's syslog"""
app = 'PBXForwarderService'
def __init__(self):
self.log = logging.getLogger()
self.log.setLevel(logging.DEBUG)
handler = logging.handlers.SysLogHandler(address='/var/run/syslog')
self.log.addHandler(handler)
def __getattr__(self, name):
levels = ['info', 'debug', 'warning', 'error', 'critical', 'log', 'exception']
if name not in levels:
self.warning('Log level %s does not exist, using info' % name)
name = 'info'
return lambda msg: getattr(self.log, name)('%s: %s' % (self.app, msg))
class Preferences(dict):
"""Preferences container to handle key prefixes more easily."""
prefix = 'mx.menta.pbx.'
def __init__(self, *args, **kwargs):
super(Preferences, self).__init__(*args, **kwargs)
def __getattr__(self, name):
return dict.__getattr__(self, self.prefix + name)
def __getitem__(self, name):
return dict.__getitem__(self, self.prefix + name)
class ServiceException(Exception):
pass
class Service(object):
"""Main service app that removes the forwarder, then sleeps in the
background, and adds the forwarder when SIGTERM is received."""
timeout = 20
def __init__(self):
self.log = Log()
self.session = requests.Session()
def get_preferences(self):
# ALWAYS read the preferences to catch any updates
plist_file = '~/Library/Preferences/com.apple.systempreferences.plist'
self.log.debug('Retrieving preferences from file %s' % plist_file)
raw_prefs = os.popen('defaults read %s' % plist_file).read()
prefixed_prefs_regexp = r'"(%s[^"]+)" = (.+);' % Preferences.prefix
prefixed_prefs = re.findall(prefixed_prefs_regexp, raw_prefs)
return Preferences(prefixed_prefs, foo='bar')
def main(self):
self.log.info('Starting application with: %s' % self.get_preferences())
self.log.info('Waiting %s seconds before removing the forwarder' % 60)
sleep(60)
self.remove_forwarder()
self.log.debug('Going to sleep waiting for SIGTERM')
for i in [x for x in dir(signal) if x.startswith('SIG')]:
try:
signum = getattr(signal, i)
signal.signal(signum, self.handle_signal)
except (RuntimeError, ValueError), m:
self.log.debug('Skipping signal %s' % i)
signal.signal(signal.SIG_IGN, self.handle_signal)
while True:
sleep(3)
def handle_signal(self, signum, frame):
if signum != signal.SIGTERM:
self.log.debug('Signal %s - aint nobody got time fo dat' % signum)
return
try:
self.log.debug('SIGTERM received - adding forwarding')
self.add_forwarder()
sys.exit()
except Exception, e:
error = 'No pude activar el forwardeo de tu extensión.\nError:\n%s' % e
self.log.critical(error)
self.display_error_alert(error)
def display_error_alert(self, error):
# http://helpx.adobe.com/photoshop/kb/unit-type-conversion-error-applescript.html
os.popen("""arch -i386 osascript <<-EOF
tell application "System Events"
activate
display dialog "PBXForwarderServiceError:\n\n%s" buttons {"OK"} with icon 0
end tell
EOF""" % error)
def login(self):
preferences = self.get_preferences()
login_data = {'extension': preferences['extension_number'],
'password': preferences['extension_password'],
'Submit.x': '54',
'Submit.y': '15',
'Submit': 'Log In',
'url': ''}
response = self.session.post('http://pbx.menta/index2.php', data=login_data,
timeout=self.timeout)
# TODO: check response body
if not response.ok:
error = 'Login failed'
self.log.critical(error)
raise ServiceException(error)
def add_forwarder(self):
self.login()
preferences = self.get_preferences()
forwarding_data = {'extension': preferences['extension_number'],
'number': preferences['target_forwarding_number']}
url = 'http://pbx.menta/userforwardmodify2.php'
response = self.session.post(url, data=forwarding_data,
timeout=self.timeout)
# TODO: check response body
if not response.ok:
error = 'Forwarding setup failed'
self.log.critical(error)
raise ServiceException(error)
def remove_forwarder(self):
self.login()
preferences = self.get_preferences()
forwarding_data = {'extension': preferences['extension_number'],
'Submit': 'Delete'}
url = 'http://pbx.menta/userforwarddelete2.php'
response = self.session.post(url, data=forwarding_data,
timeout=self.timeout)
# TODO: check response body
if not response.ok:
error = 'Forwarding removal failed'
self.log.critical(error)
raise ServiceException(error)
if __name__ == '__main__':
try:
service = Service()
service.main()
except Exception, e:
log = Log()
log.critical('Unhandled fatal error: %s' % e)
sys.exit(2)