-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathplugin.py
370 lines (332 loc) · 13.5 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
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
###
# Copyright (c) 2011, Valentin Lorentz
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions, and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions, and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the author of this software nor the name of
# contributors to this software may be used to endorse or promote products
# derived from this software without specific prior written consent.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
###
import re
import os
import sys
import html
import time
import urllib
import supybot.conf as conf
import supybot.utils as utils
from supybot.commands import *
import supybot.irclib as irclib
import supybot.ircmsgs as ircmsgs
import supybot.plugins as plugins
import supybot.ircutils as ircutils
import supybot.callbacks as callbacks
import supybot.httpserver as httpserver
from supybot.i18n import PluginInternationalization, internationalizeDocstring
_ = PluginInternationalization('WebLogs')
if sys.version_info[0] >= 3:
def b(s):
if isinstance(s, str):
return s.encode()
else:
return s
def s(b):
if isinstance(b, bytes):
return b.decode()
else:
return b
else:
def b(s):
return s
def s(b):
return b
page_template = """
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>%(title)s - WebLogs</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
h1 {
display: inline;
}
.line .timestamp {
display: none;
}
.line:hover .timestamp, .line:focus .timestamp {
display: inline;
float: right;
}
.command-PART { color: maroon; }
.command-QUIT { color: maroon; }
.command-JOIN { color: green; }
.command-MODE { color: olive; }
.command-KICK { color: red; }
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js">
</script>
<script>
$(document).ready(function() {
$("div.day div").hide();
$("div.day input.hide").hide();
$("input.reveal").click(function() {
$(this).hide();
$(this).parents("div").children("div.day input.hide").fadeIn(600);
$(this).parents("div").children("div.day div").fadeIn(600);
});
$("input.hide").click(function() {
$(this).hide();
$(this).parents("div").children("div.day input.reveal").fadeIn(600);
$(this).parents("div").children("div.day div").fadeOut(600);
});
});
</script>
</head>
<body>
%(body)s
</body>
</html>"""
# From http://stackoverflow.com/questions/1071191/detect-urls-in-a-string
URL_REGEXP = re.compile(r'''((?:mailto:|ftp://|http://)[^ <>'"{}|\\^`[\]]*)''')
def format_logs(logs):
def format_nick(nick):
template = '<span style="color: %(color)s;">%(nick)s</span>'
colors = ['red', 'orange', 'blue', 'lime', 'grey', 'green', 'purple',
'black', 'olive']
hash_ = sum([ord(x) for x in nick]) % len(colors)
return template % {'color': colors[hash_], 'nick': nick}
html_logs = '<div>' # Will be closed by the first "Changed day"
old_gmtime_day = None
for line in logs.split('\n'):
words = line.split(' ')
if len(words) < 2:
continue
timestamp = words[0]
command = words[1]
new_line = None
if command == 'PRIVMSG' or command == 'NOTICE':
if command == 'PRIVMSG':
nick_delimiters = ('<', '>')
else:
nick_delimiters = ('*', '*')
formatted_nick = nick_delimiters[0] + format_nick(words[2]) + \
nick_delimiters[1]
new_line = _('%(formatted_nick)s %(message)s') % {
'formatted_nick': formatted_nick,
'message': html.escape(' '.join(words[3:]))}
elif command == 'PRIVMSG-ACTION':
new_line = _('* %(nick)s %(message)s') % {
'nick': format_nick(words[2]),
'message': html.escape(' '.join(words[3:]))}
elif command == 'PART':
new_line = _('<-- %(nick)s has left the channel (%(reason)s)') % \
{'nick': format_nick(words[2]),
'reason': html.escape(' '.join(words[3:]))}
elif command == 'QUIT':
new_line = _('<-- %(nick)s has quit the network (%(reason)s)') % \
{'nick': format_nick(words[2]),
'reason': html.escape(' '.join(words[3:]))}
elif command == 'JOIN':
new_line = _('--> %(nick)s has joined the channel') % \
{'nick': format_nick(words[2])}
elif command == 'MODE':
new_line = _('*/* %(nick)s has set mode %(modes)s') % \
{'nick': format_nick(words[2]),
'modes': ' '.join(words[3:])}
elif command == 'KICK':
new_line = _('<-- %(kicked)s has been kicked by %(kicker)s (%(reason)s)') % \
{'kicked': format_nick(words[3]),
'kicker': format_nick(words[2]),
'reason': html.escape(' '.join(words[4:]))}
if new_line is not None:
template = """
<div class="line command-%(command)s">
<span class="timestamp">%(timestamp)s</span>
%(line)s
</div>"""
new_line = URL_REGEXP.sub(r'<a href="\1">\1</a>', new_line)
# Timestamp handling
gmtime = time.gmtime(int(words[0]))
gmtime_day = (gmtime.tm_year, gmtime.tm_mon, gmtime.tm_mday)
if old_gmtime_day != gmtime_day:
html_logs += '</div><div class="day">'
html_logs += """
<input type="button" value="reveal" class="reveal" />
<input type="button" value="hide" class="hide" />
"""
html_logs += '<h1>%i-%i-%i</h1>' % \
gmtime_day
old_gmtime_day = gmtime_day
timestamp = time.strftime('%H:%M:%S', gmtime)
html_logs += template % {'line': new_line,
'timestamp': timestamp, 'command': command}
return html_logs
class WebLogsMiddleware(object):
"""Class for reading and parsing WebLogs data."""
__shared_states = {}
def __init__(self, channel):
if channel in self.__shared_states:
self.__dict__ = self.__shared_states[channel]
else:
self._channel = channel
path = conf.supybot.directories.data.dirize('WebLogs_%s.log' %
channel)
self.fd = open(path, 'a+')
self.__shared_states.update({channel: self.__dict__})
@classmethod
def get_channel_list(cls):
channels = [x[len('WebLogs_'):-len('.log')]
for x in os.listdir(conf.supybot.directories.data())
if x.endswith('.log')]
return [x for x in channels
if cls._plugin.registryValue('enabled', x)]
def get_logs(self):
self.fd.seek(0)
return self.fd.read()
def write(self, *args):
self.fd.read()
self.fd.write(s('%i %s\n' % (time.time(), ' '.join(args))))
class WebLogsServerCallback(httpserver.SupyHTTPServerCallback):
name = 'WebLogs'
def doGet(self, handler, path):
if path == '':
self.send_response(301)
self.send_header('Location', '/weblogs/')
self.end_headers()
return
elif path == '/':
splitted_path = []
else:
splitted_path = path[1:].split('/')
if len(splitted_path) == 0:
self.send_response(200)
self.send_header('Content-type', 'text/html; charset=utf-8')
self.end_headers()
page_body = """Here is a list of available logs:<ul>"""
for channel in WebLogsMiddleware.get_channel_list():
page_body += '<li><a href="./html/%s/">%s</a></li>' % (
utils.web.urlquote(channel), channel)
page_body += '</ul>'
self.wfile.write(b(page_template %
{'title': 'Index', 'body': page_body}))
return
elif len(splitted_path) == 3:
mode, channel, page = splitted_path
else:
self.send_response(404)
self.send_header('Content-type', 'text/plain; charset=utf-8')
self.end_headers()
self.wfile.write(b('Bad URL.'))
return
assert mode in ('html', 'json')
channel = utils.web.urlunquote(channel)
if channel not in WebLogsMiddleware.get_channel_list():
self.send_response(404)
self.send_header('Content-type', 'text/plain; charset=utf-8')
self.end_headers()
self.wfile.write(b('This channel is not logged.'))
return
middleware = WebLogsMiddleware(channel)
if page == '':
self.send_response(200)
self.send_header('Content-type', 'text/html; charset=utf-8')
self.end_headers()
self.wfile.write(b(page_template % {
'title': channel,
'body': format_logs(middleware.get_logs())}))
def check_enabled(f):
def newf(self, irc, msg):
channel = msg.args[0]
if irc.isChannel(channel) and self.registryValue('enabled', channel) \
and not msg.tagged('relayedMsg'):
return f(self, irc, msg, WebLogsMiddleware(channel))
return msg
return newf
@internationalizeDocstring
class WebLogs(callbacks.Plugin):
"""Add the help for "@plugin help WebLogs" here
This should describe *how* to use this plugin."""
def __init__(self, irc):
# Some stuff needed by Supybot
self.__parent = super(WebLogs, self)
callbacks.Plugin.__init__(self, irc)
self.lastStates = {}
WebLogsMiddleware._plugin = self
# registering the callback
callback = WebLogsServerCallback() # create an instance of the callback
httpserver.hook('weblogs', callback)
@check_enabled
def doPrivmsg(self, irc, msg, middleware):
if ircmsgs.isAction(msg):
middleware.write('PRIVMSG-ACTION', msg.nick,
ircmsgs.unAction(msg))
else:
middleware.write('PRIVMSG', msg.nick, msg.args[1])
@check_enabled
def outFilter(self, irc, msg, middleware):
if msg.command == 'PRIVMSG':
middleware.write('PRIVMSG', irc.nick, msg.args[1])
elif msg.command == 'NOTICE':
middleware.write('NOTICE', irc.nick, msg.args[1])
return msg
@check_enabled
def doNotice(self, irc, msg, middleware):
middleware.write('NOTICE', msg.nick, msg.args[1])
@check_enabled
def doPart(self, irc, msg, middleware):
if len(msg.args) == 1:
reason = ''
else:
reason = msg.args[1]
middleware.write('PART', msg.nick, reason)
@check_enabled
def doJoin(self, irc, msg, middleware):
middleware.write('JOIN', msg.nick)
@check_enabled
def doMode(self, irc, msg, middleware):
middleware.write('MODE', msg.nick, msg.args[1], ' '.join(msg.args[2:]))
@check_enabled
def doKick(self, irc, msg, middleware):
middleware.write('KICK', msg.nick, ' '.join(msg.args[1:]))
def __call__(self, irc, msg):
self.__parent.__call__(irc, msg)
self.lastStates[irc] = irc.state.copy()
def doQuit(self, irc, msg):
if len(msg.args) == 0:
reason = ''
else:
reason = msg.args[0]
if not isinstance(irc, irclib.Irc):
irc = irc.getRealIrc()
for (channel, chan) in self.lastStates[irc].channels.items():
if msg.nick in chan.users:
if self.registryValue('enabled', channel):
middleware = WebLogsMiddleware(channel)
middleware.write('QUIT', msg.nick, reason)
def die(self):
# unregister the callback
httpserver.unhook('weblogs')
# Stuff for Supybot
self.__parent.die()
Class = WebLogs
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: