-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathzlog_sql.py
More file actions
529 lines (453 loc) · 17.2 KB
/
zlog_sql.py
File metadata and controls
529 lines (453 loc) · 17.2 KB
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
import inspect
import json
import multiprocessing
import os
import pprint
import re
import traceback
import warnings
from datetime import datetime
from time import sleep
import znc
# noinspection PyPep8Naming
class zlog_sql(znc.Module):
description = 'Logs all channels to a MySQL/SQLite database.'
module_types = [znc.CModInfo.GlobalModule]
wiki_page = 'ZLog_SQL'
has_args = True
args_help_text = ('Connection string in format: mysql://user:pass@host/database_name'
' or postgres://user:pass@host/database_name'
' or sqlite://path/to/db.sqlite')
log_queue = multiprocessing.SimpleQueue()
internal_log = None
hook_debugging = False
def OnLoad(self, args, message):
"""
This module hook is called when a module is loaded.
:type args: const CString &
:type args: CString &
:rtype: bool
:param args: The arguments for the modules.
:param message: A message that may be displayed to the user after loading the module.
:return: True if the module loaded successfully, else False.
"""
self.internal_log = InternalLog(self.GetSavePath())
self.debug_hook()
try:
db = self.parse_args(args)
multiprocessing.Process(target=DatabaseThread.worker_safe,
args=(db, self.log_queue, self.internal_log)).start()
return True
except Exception as e:
message.s = str(e)
with self.internal_log.error() as target:
target.write('Could not initialize module caused by: {} {}\n'.format(type(e), str(e)))
target.write('Stack trace: ' + traceback.format_exc())
target.write('\n')
return False
def __del__(self):
# Terminate worker process.
self.log_queue.put(None)
def GetServer(self):
pServer = self.GetNetwork().GetCurrentServer()
if pServer is None:
return '(no server)'
sSSL = '+' if pServer.IsSSL() else ''
return pServer.GetName() + ' ' + sSSL + pServer.GetPort()
# GENERAL IRC EVENTS
# ==================
def OnIRCConnected(self):
"""
This module hook is called after a successful login to IRC.
:rtype: None
"""
self.debug_hook()
self.put_log('Connected to IRC (' + self.GetServer() + ')')
def OnIRCDisconnected(self):
"""
This module hook is called when a user gets disconnected from IRC.
:rtype: None
"""
self.debug_hook()
self.put_log('Disconnected from IRC (' + self.GetServer() + ')')
def OnBroadcast(self, message):
"""
This module hook is called when a message is broadcasted to all users.
:type message: CString &
:rtype: EModRet
"""
self.debug_hook()
self.put_log('Broadcast: ' + str(message))
return znc.CONTINUE
def OnRawMode(self, opNick, channel, modes, args):
"""
Called on any channel mode change.
This is called before the more detailed mode hooks like e.g. OnOp() and OnMode().
:type opNick: const CNick &
:type channel: CChan &
:type modes: const CString &
:type args: const CString &
:rtype: None
"""
self.debug_hook()
sNick = opNick.GetNick() if opNick is not None else 'Server'
self.put_log('*** ' + sNick + ' sets mode: ' + modes + ' ' + args, channel.GetName())
def OnKick(self, opNick, kickedNick, channel, message):
"""
Called when a nick is kicked from a channel.
:type opNick: const CNick &
:type kickedNick: const CString &
:type channel: CChan &
:type message: const CString &
:rtype: None
"""
self.debug_hook()
self.put_log('*** ' + kickedNick + ' was kicked by ' + opNick.GetNick() + ' (' + message + ')',
channel.GetName())
def OnQuit(self, nick, message, channels):
"""
Called when a nick quit from IRC.
:type nick: const CNick &
:type message: const CString &
:type channels: std::vector<CChan*>
:rtype: None
"""
self.debug_hook()
for channel in channels:
self.put_log(
'*** Quits: ' + nick.GetNick() + ' (' + nick.GetIdent() + '@' + nick.GetHost() + ') (' + message + ')',
channel.GetName())
def OnJoin(self, nick, channel):
"""
Called when a nick joins a channel.
:type nick: const CNick &
:type channel: CChan &
:rtype: None
"""
self.debug_hook()
self.put_log('*** Joins: ' + nick.GetNick() + ' (' + nick.GetIdent() + '@' + nick.GetHost() + ')',
channel.GetName())
def OnPart(self, nick, channel, message):
"""
Called when a nick parts a channel.
:type nick: const CNick &
:type channel: CChan &
:type message: const CString &
:rtype: None
"""
self.debug_hook()
self.put_log(
'*** Parts: ' + nick.GetNick() + ' (' + nick.GetIdent() + '@' + nick.GetHost() + ') (' + message + ')',
channel.GetName())
def OnNick(self, oldNick, newNick, channels):
"""
Called when a nickname change occurs.
:type oldNick: const CNick &
:type newNick: const CString &
:type channels: std::vector<CChan*>
:rtype: None
"""
self.debug_hook()
for channel in channels:
self.put_log('*** ' + oldNick.GetNick() + ' is now known as ' + newNick, channel.GetName())
def OnTopic(self, nick, channel, topic):
"""
Called when we receive a channel topic change from IRC.
:type nick: CNick &
:type channel: CChan &
:type topic: CString &
:rtype: EModRet
"""
self.debug_hook()
self.put_log('*** ' + nick.GetNick() + ' changes topic to "' + str(topic) + '"', channel.GetName())
return znc.CONTINUE
# NOTICES
# =======
def OnUserNotice(self, target, message):
"""
This module hook is called when a user sends a NOTICE message.
:type target: CString &
:type message: CString &
:rtype: EModRet
"""
self.debug_hook()
network = self.GetNetwork()
if network:
self.put_log('-' + network.GetCurNick() + '- ' + str(message), str(target))
return znc.CONTINUE
def OnPrivNotice(self, nick, message):
"""
Called when we receive a private NOTICE message from IRC.
:type nick: CNick &
:type message: CString &
:rtype: EModRet
"""
self.debug_hook()
self.put_log('-' + nick.GetNick() + '- ' + str(message), nick.GetNick())
return znc.CONTINUE
def OnChanNotice(self, nick, channel, message):
"""
Called when we receive a channel NOTICE message from IRC.
:type nick: CNick &
:type channel: CChan &
:type message: CString &
:rtype: EModRet
"""
self.debug_hook()
self.put_log('-' + nick.GetNick() + '- ' + str(message), channel.GetName())
return znc.CONTINUE
# ACTIONS
# =======
def OnUserAction(self, target, message):
"""
Called when a client sends a CTCP ACTION request ("/me").
:type target: CString &
:type message: CString &
:rtype: EModRet
"""
self.debug_hook()
pNetwork = self.GetNetwork()
if pNetwork:
self.put_log('* ' + pNetwork.GetCurNick() + ' ' + str(message), str(target))
return znc.CONTINUE
def OnPrivAction(self, nick, message):
"""
Called when we receive a private CTCP ACTION ("/me" in query) from IRC.
:type nick: CNick &
:type message: CString &
:rtype: EModRet
"""
self.debug_hook()
self.put_log('* ' + nick.GetNick() + ' ' + str(message), nick.GetNick())
return znc.CONTINUE
def OnChanAction(self, nick, channel, message):
"""
Called when we receive a channel CTCP ACTION ("/me" in a channel) from IRC.
:type nick: CNick &
:type channel: CChan &
:type message: CString &
:rtype: EModRet
"""
self.debug_hook()
self.put_log('* ' + nick.GetNick() + ' ' + str(message), channel.GetName())
return znc.CONTINUE
# MESSAGES
# ========
def OnUserMsg(self, target, message):
"""
This module hook is called when a user sends a PRIVMSG message.
:type target: CString &
:type message: CString &
:rtype: EModRet
"""
self.debug_hook()
network = self.GetNetwork()
if network:
self.put_log('<' + network.GetCurNick() + '> ' + str(message), str(target))
return znc.CONTINUE
def OnPrivMsg(self, nick, message):
"""
Called when we receive a private PRIVMSG message from IRC.
:type nick: CNick &
:type message: CString &
:rtype: EModRet
"""
self.debug_hook()
self.put_log('<' + nick.GetNick() + '> ' + str(message), nick.GetNick())
return znc.CONTINUE
def OnChanMsg(self, nick, channel, message):
"""
Called when we receive a channel PRIVMSG message from IRC.
:type nick: CNick &
:type channel: CChan &
:type message: CString &
:rtype: EModRet
"""
self.debug_hook()
self.put_log('<' + nick.GetNick() + '> ' + str(message), channel.GetName())
return znc.CONTINUE
# LOGGING
# =======
def put_log(self, line, window="Status"):
"""
Adds the log line to database write queue.
"""
self.log_queue.put({
'created_at': datetime.utcnow().isoformat(),
'user': self.GetUser().GetUserName() if self.GetUser() is not None else None,
'network': self.GetNetwork().GetName() if self.GetUser() is not None else None,
'window': window,
'message': line.encode('utf8', 'replace').decode('utf8')})
# DEBUGGING HOOKS
# ===============
def debug_hook(self):
"""
Dumps parent calling method name and its arguments to debug logfile.
"""
if self.hook_debugging is not True:
return
frameinfo = inspect.stack()[1]
argvals = frameinfo.frame.f_locals
with self.internal_log.debug() as target:
target.write('Called method: ' + frameinfo.function + '()\n')
for argname in argvals:
if argname == 'self':
continue
target.write(' ' + argname + ' -> ' + pprint.pformat(argvals[argname]) + '\n')
target.write('\n')
# ARGUMENT PARSING
# ================
def parse_args(self, args):
if args.strip() == '':
raise Exception('Missing argument. Provide connection string as an argument.')
match = re.search('^\s*sqlite(?:://(.+))?\s*$', args)
if match:
if match.group(1) is None:
return SQLiteDatabase({'database': os.path.join(self.GetSavePath(), 'logs.sqlite')})
else:
return SQLiteDatabase({'database': match.group(1)})
match = re.search('^\s*mysql://(.+?):(.+?)@(.+?)/(.+)\s*$', args)
if match:
return MySQLDatabase({'host': match.group(3),
'user': match.group(1),
'passwd': match.group(2),
'db': match.group(4)})
match = re.search('^\s*postgres://(.+?):(.+?)@(.+?)/(.+)\s*$', args)
if match:
return PostgresDatabase({'host': match.group(3),
'user': match.group(1),
'password': match.group(2),
'database': match.group(4)})
raise Exception('Unrecognized connection string. Check the documentation.')
class DatabaseThread:
@staticmethod
def worker_safe(db, log_queue: multiprocessing.SimpleQueue, internal_log) -> None:
try:
DatabaseThread.worker(db, log_queue, internal_log)
except Exception as e:
with internal_log.error() as target:
target.write('Unrecoverable exception in worker thread: {0} {1}\n'.format(type(e), str(e)))
target.write('Stack trace: ' + traceback.format_exc())
target.write('\n')
raise
@staticmethod
def worker(db, log_queue: multiprocessing.SimpleQueue, internal_log) -> None:
db.connect()
while True:
item = log_queue.get()
if item is None:
break
try:
db.ensure_connected()
db.insert_into('logs', item)
except Exception as e:
sleep_for = 10
with internal_log.error() as target:
target.write('Could not save to database caused by: {0} {1}\n'.format(type(e), str(e)))
if 'open' in dir(db.conn):
target.write('Database handle state: {}\n'.format(db.conn.open))
target.write('Stack trace: ' + traceback.format_exc())
target.write('Current log: ')
json.dump(item, target)
target.write('\n\n')
target.write('Retry in {} s\n'.format(sleep_for))
sleep(sleep_for)
with internal_log.error() as target:
target.write('Retrying now.\n'.format(sleep_for))
log_queue.put(item)
class InternalLog:
def __init__(self, save_path: str):
self.save_path = save_path
def debug(self):
return self.open('debug')
def error(self):
return self.open('error')
def open(self, level: str):
target = open(os.path.join(self.save_path, level + '.log'), 'a')
line = 'Log opened at: {} UTC\n'.format(datetime.utcnow())
target.write(line)
target.write('=' * len(line) + '\n\n')
return target
class Database:
def __init__(self, dsn: dict):
self.dsn = dsn
self.conn = None
class PostgresDatabase(Database):
def connect(self) -> None:
import psycopg2
self.conn = psycopg2.connect(**self.dsn)
with warnings.catch_warnings():
warnings.simplefilter('ignore')
self.conn.cursor().execute('''
CREATE TABLE IF NOT EXISTS logs (
"id" BIGSERIAL NOT NULL,
"created_at" TIMESTAMP WITH TIME ZONE NOT NULL,
"user" VARCHAR(128) DEFAULT NULL,
"network" VARCHAR(128) DEFAULT NULL,
"window" VARCHAR(255) NOT NULL,
"message" TEXT,
PRIMARY KEY (id)
);
''')
self.conn.commit()
def ensure_connected(self):
if self.conn.status == 0:
self.connect()
def insert_into(self, table, row):
cols = ', '.join('"{}"'.format(col) for col in row.keys())
vals = ', '.join('%({})s'.format(col) for col in row.keys())
sql = 'INSERT INTO {} ({}) VALUES ({})'.format(table, cols, vals)
self.conn.cursor().execute(sql, row)
self.conn.commit()
class MySQLDatabase(Database):
def connect(self) -> None:
import pymysql
self.conn = pymysql.connect(use_unicode=True, charset='utf8mb4', **self.dsn)
with warnings.catch_warnings():
warnings.simplefilter('ignore')
self.conn.cursor().execute('''
CREATE TABLE IF NOT EXISTS `logs` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`created_at` DATETIME NOT NULL,
`user` VARCHAR(128) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`network` VARCHAR(128) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`window` VARCHAR(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`message` TEXT COLLATE utf8mb4_unicode_ci,
PRIMARY KEY (`id`),
KEY `created_at` (`created_at`),
KEY `user` (`user`),
KEY `network` (`network`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=COMPRESSED;
''')
self.conn.commit()
def ensure_connected(self):
if self.conn.open is False:
self.connect()
def insert_into(self, table, row):
cols = ', '.join('`{}`'.format(col) for col in row.keys())
vals = ', '.join('%({})s'.format(col) for col in row.keys())
sql = 'INSERT INTO `{}` ({}) VALUES ({})'.format(table, cols, vals)
self.conn.cursor().execute(sql, row)
self.conn.commit()
class SQLiteDatabase(Database):
def connect(self) -> None:
import sqlite3
self.conn = sqlite3.connect(**self.dsn)
self.conn.cursor().execute('''
CREATE TABLE IF NOT EXISTS [logs](
[id] INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
[created_at] DATETIME NOT NULL,
[user] VARCHAR,
[network] VARCHAR,
[window] VARCHAR,
[message] TEXT);
''')
self.conn.commit()
def ensure_connected(self):
pass
def insert_into(self, table: str, row: dict) -> None:
cols = ', '.join('[{}]'.format(col) for col in row.keys())
vals = ', '.join(':{}'.format(col) for col in row.keys())
sql = 'INSERT INTO [{}] ({}) VALUES ({})'.format(table, cols, vals)
self.conn.cursor().execute(sql, row)
self.conn.commit()