This repository has been archived by the owner on Apr 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.py
165 lines (121 loc) · 5.21 KB
/
plugin.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
from enum import Enum
from hashlib import sha256
import hmac
from multiprocessing import Queue
from time import time
from supybot.commands import *
import supybot.callbacks as callbacks
import supybot.conf as conf
import supybot.httpserver as httpserver
import supybot.ircmsgs as ircmsgs
import supybot.ircutils as ircutils
import supybot.log as log
import supybot.plugins as plugins
import supybot.schedule as schedule
import supybot.utils as utils
from dropbox import Dropbox
from dropbox.files import DeletedMetadata
class EventType(Enum):
update = 1
delete = 2
class DropboxWatchServerCallback(httpserver.SupyHTTPServerCallback):
name = 'DropboxWatch'
_dbx = Dropbox(conf.supybot.plugins.DropboxWatch.apiKey())
def __init__(self):
super(DropboxWatchServerCallback, self).__init__()
self._cursor = self._dbx.files_list_folder_get_latest_cursor(
'', recursive=True).cursor
def doGet(self, handler, path):
handler.send_response(200)
handler.send_header('Content-Type', 'text/plain')
handler.send_header('X-Content-Type-Options', 'nosniff')
handler.end_headers()
handler.wfile.write(path.replace('/?challenge=', '').encode('utf-8'))
def doPost(self, handler, path, form):
signature = handler.headers['X-Dropbox-Signature']
if not hmac.compare_digest(signature,
hmac.new((conf.supybot.plugins.DropboxWatch
.appSecret().encode('utf-8')),
form, sha256).hexdigest()):
handler.send_response(403)
log.warning('Invalid Dropbox signature: %s\n\t%s' % (
signature, str(form)))
handler.wfile.write('Invalid signature'.encode('utf-8'))
return
handler.send_response(200)
result = self._dbx.files_list_folder_continue(self._cursor)
self._cursor = result.cursor
for entry in result.entries:
log.info('DropboxWatch update: %s' % str(entry))
if type(entry) is DeletedMetadata:
events.put_nowait((EventType.delete, entry.path_display))
else:
events.put_nowait((EventType.update, entry.path_display))
class DropboxWatch(callbacks.Plugin):
def __init__(self, irc):
self.__parent = super(DropboxWatch, self)
self.__parent.__init__(irc)
callback = DropboxWatchServerCallback()
self._abbrev = DropboxWatchServerCallback.name.lower()
for server in httpserver.http_servers:
if self._abbrev in server.callbacks:
httpserver.unhook(self._abbrev)
break
httpserver.hook(self._abbrev, callback)
interval = conf.supybot.plugins.DropboxWatch.interval()
def f():
if events.empty():
return
path_dict = dict()
for channel in irc.state.channels:
paths = conf.supybot.plugins.DropboxWatch.paths.get(channel)()
if len(paths) == 0:
continue
for path in paths:
if path in path_dict:
continue
path_dict[path] = (set(), set())
try:
while not events.empty():
event = events.get_nowait()
for path in path_dict.keys():
if event[1].startswith('/' + path):
updates, deletes = path_dict[path]
if event[0] == EventType.delete:
deletes.add(event[1])
else:
updates.add(event[1])
path_dict[path] = (updates, deletes)
except Queue.Empty:
log.warning('Queue empty')
for k in path_dict.keys():
updates, deletes = path_dict[k]
output = ''
if len(deletes) > 0:
output = 'Deleted: %s' % (', '.join(deletes))
if len(updates) > 0:
if len(output) > 0:
output += ' | '
output += 'Updated: %s' % (', '.join(updates))
if len(output) == 0:
continue
output = '[Dropbox] %s' % output
for chan in irc.state.channels:
paths = conf.supybot.plugins.DropboxWatch.paths.get(chan)()
if len(paths) == 0 or k not in paths:
continue
log.info('%s >> %s' % (chan, output))
irc.queueMsg(ircmsgs.privmsg(chan, output))
if self._abbrev in schedule.schedule.events:
schedule.removeEvent(self._abbrev)
schedule.addPeriodicEvent(f, interval, name=self._abbrev, now=False)
def die(self):
self.__parent.die()
for server in httpserver.http_servers:
if self._abbrev in server.callbacks:
httpserver.unhook(self._abbrev)
break
if self._abbrev in schedule.schedule.events:
schedule.removeEvent(self._abbrev)
Class = DropboxWatch
events = Queue()