-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinconnect_server.py
executable file
·216 lines (173 loc) · 7.46 KB
/
linconnect_server.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
'''
LinConnect: Mirror Android notifications on Linux Desktop
Copyright (C) 2013 Will Hauck
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''
from __future__ import print_function
# Imports
try:
import ConfigParser
except ImportError:
import configparser as ConfigParser
import os
import sys
import signal
import select
import threading
import platform
import re
import glob
import hashlib
import cherrypy
import subprocess
from gi.repository import Notify
from gi.repository import GLib
import pybonjour
import shutil
import base64
from datetime import datetime
app_name = 'linconnect-server'
version = "2.20"
# Global Variables
_notification_header = ""
_notification_description = ""
# Configuration
script_dir = os.path.abspath(os.path.dirname(__file__))
def user_specific_location(type, file):
dir = os.path.expanduser(os.path.join('~/.' + type, app_name))
if not os.path.isdir(dir):
os.makedirs(dir)
return os.path.join(dir, file)
conf_file = user_specific_location('config', 'conf.ini')
icon_path_format = user_specific_location('cache', 'icon_cache_%s.png')
# Clear the icon cache
for icon_cache_file in glob.glob(icon_path_format % '*'):
os.unlink(icon_cache_file)
old_conf_file = os.path.join(script_dir, 'conf.ini')
if os.path.isfile(old_conf_file):
if os.path.isfile(conf_file):
print("Both old and new config files exist: %s and %s, ignoring old one" % (old_conf_file, conf_file))
else:
print("Old config file %s found, moving to a new location: %s" % (old_conf_file, conf_file))
shutil.move(old_conf_file, conf_file)
del old_conf_file
try:
with open(conf_file):
print("Loading conf.ini")
except IOError:
print("Creating conf.ini")
with open(conf_file, 'w') as text_file:
text_file.write("""[connection]
port = 9090
enable_bonjour = 1
[other]
enable_instruction_webpage = 1
notify_timeout = 25000""")
parser = ConfigParser.ConfigParser()
parser.read(conf_file)
del conf_file
# Must append port because Java Bonjour library can't determine it
_service_name = platform.node()
class Notification(object):
if parser.getboolean('other', 'enable_instruction_webpage') == 1:
with open(os.path.join(script_dir, 'index.html'), 'rb') as f:
_index_source = f.read()
def index(self):
return self._index_source % (version, "<br/>".join(get_local_ip()))
index.exposed = True
def notif(self, notificon):
global _notification_header
global _notification_description
# Get notification data from HTTP header
try:
new_notification_header = base64.urlsafe_b64decode(cherrypy.request.headers['NOTIFHEADER'])
new_notification_description = base64.urlsafe_b64decode(cherrypy.request.headers['NOTIFDESCRIPTION'])
except:
# Maintain compatibility with old application
new_notification_header = cherrypy.request.headers['NOTIFHEADER'].replace('\x00', '').decode('iso-8859-1', 'replace').encode('utf-8')
new_notification_description = cherrypy.request.headers['NOTIFDESCRIPTION'].replace('\x00', '').decode('iso-8859-1', 'replace').encode('utf-8')
# Ensure the notification is not a duplicate
if (_notification_header != new_notification_header) \
or (_notification_description != new_notification_description):
_notification_header = new_notification_header
_notification_description = new_notification_description
# Icon should be small enough to fit into modern PCs RAM.
# Alternatively, can do this in chunks, twice: first to count MD5, second to copy the file.
icon_data = notificon.file.read()
icon_path = icon_path_format % hashlib.md5(icon_data).hexdigest()
if not os.path.isfile(icon_path):
with open(icon_path, 'w') as icon_file:
icon_file.write(icon_data)
# Send the notification
notif = Notify.Notification.new(_notification_header, _notification_description, icon_path)
f = open('/home/masterli/notifications.txt', 'a')
i = datetime.now()
f.write(str(i.strftime('%Y/%m/%d %H:%M:%S'))+" :: "+_notification_description+"\n")
f.close()
# Add 'value' hint to display nice progress bar if we see percents in the notification
percent_match = re.search(r'(1?\d{2})%', _notification_header + _notification_description)
if percent_match:
notif.set_hint('value', GLib.Variant('i', int(percent_match.group(1))))
if parser.has_option('other', 'notify_timeout'):
notif.set_timeout(parser.getint('other', 'notify_timeout'))
try:
notif.show()
except:
# Workaround for org.freedesktop.DBus.Error.ServiceUnknown
Notify.uninit()
Notify.init("com.willhauck.linconnect")
notif.show()
return "true"
notif.exposed = True
def register_callback(sdRef, flags, errorCode, name, regtype, domain):
if errorCode == pybonjour.kDNSServiceErr_NoError:
print("Registered Bonjour service " + name)
def sigterm_handler(_signo, _stack_frame):
# Raises SystemExit(0):
sys.exit(0)
def initialize_bonjour():
sdRef = pybonjour.DNSServiceRegister(name=_service_name,
regtype="_linconnect._tcp",
port=int(parser.get('connection', 'port')),
callBack=register_callback)
signal.signal(signal.SIGTERM, sigterm_handler)
signal.signal(signal.SIGINT, sigterm_handler)
try:
try:
while True:
ready = select.select([sdRef], [], [])
if sdRef in ready[0]:
pybonjour.DNSServiceProcessResult(sdRef)
except KeyboardInterrupt:
pass
finally:
sdRef.close()
def get_local_ip():
ips = []
for ip in subprocess.check_output("/sbin/ip address | grep -i 'inet ' | awk {'print $2'} | sed -e 's/\/[^\/]*$//'", shell=True).split("\n"):
if ip.__len__() > 0 and not ip.startswith("127."):
ips.append(ip + ":" + parser.get('connection', 'port'))
return ips
# Initialization
if not Notify.init("com.willhauck.linconnect"):
raise ImportError("Error initializing libnotify")
# Start Bonjour if desired
if parser.getboolean('connection', 'enable_bonjour') == 1:
thr = threading.Thread(target=initialize_bonjour)
thr.start()
config_instructions = "Configuration instructions at http://localhost:" + parser.get('connection', 'port')
print(config_instructions)
notif = Notify.Notification.new("Notification server started (version " + version + ")", config_instructions, "info")
notif.show()
cherrypy.server.socket_host = '0.0.0.0'
cherrypy.server.socket_port = int(parser.get('connection', 'port'))
cherrypy.quickstart(Notification())