forked from adamrparsons/PostgreZNC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pznc.py
executable file
·213 lines (186 loc) · 7.92 KB
/
pznc.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
# pznc.py
# Adam Parsons ([email protected])
#
# Requires systemd-journal for debugging, I chose this because any sort of debugging or stdout with modpython is
# absolutely impossible, this should have taken me an hour to write, but took almost two days because of how
# awful znc's modpython is, and its awesome, descriptive error messages such as "Module Aborted"
# If you don't have or don't want systemd, just remove any line beginning with 'journal.send' and
# replace it with "pass" if its the sole statement after an except
#
# If you're new to python, pip, or anything here and don't know what you're doing, as root run (on debian)
# apt-get install python3-pip znc-dev znc-python build-essential postgresql
# pip3 install contextlib2 psycopg2
# After that, edit the database connection details in this file "postgresConnectString()" to reflect yours
#
# Note: You will not be able to run this script directly, its a python class, not an executable script, so don't bother trying,
# instead it should be placed inside your znc modules directory (I use /usr/lib/znc/)
# and load it inside your IRC client with "/msg *status loadmod pznc"
# Check your syslog or journalctl for errors in configuration
#
# Some of this is directly copied and pasted from a similar module but for mysql, and without exceptions
# you can compare theirs here: https://github.com/buxxi/znc-mysql/blob/master/sql.py
#
# You can also find the schema for the database at the very bottom of this file, manually enter this into a psql shell
#
# Known Issues: Duplicate lines often occur, easily sovable though if anyone cares, I didn't write this because
# I planned on using it, I wrote it to learn how to make a znc modpython module, I don't actually use this.
import znc, psycopg2, re
from contextlib2 import closing
from systemd import journal
from datetime import datetime
class pznc(znc.Module):
description = "PostgreSQL Logger for ZNC"
module_types = [znc.CModInfo.UserModule, znc.CModInfo.NetworkModule]
def OnPart(self, user, channel, message):
try:
self.insert("PART", channel.GetName(), user.GetHost(), user.GetNick(), None, self.timestamp(), None, message, str(self.GetNetwork()))
except Exception as e:
send.journal(repr(e))
return True
def OnChanMsg(self, user, channel, message):
try:
self.insert("SAY", channel.GetName(), user.GetHost(), user.GetNick(), self.findMode(channel, user), self.timestamp(), None, message.s, str(self.GetNetwork()))
except Exception as e:
journal.send(repr(e))
return True
def timestamp(self):
ts = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
return ts
def postgresConnectString(self):
dbpass = "j3U8sVnq%6^4" ##Not my actual password, nice try
connstring = "dbname='pznc' user='adam' host='localhost' password='" + dbpass + "'"
return connstring
def insert(self, code, channel, host, zuser, user_mode, date, target_user, message, network):
connstring = self.postgresConnectString()
try:
with closing(psycopg2.connect(connstring)) as conn:
with closing(conn.cursor()) as cursor:
cmd = "INSERT INTO chanlog (code, network, channel, host, zuser, user_mode, target_user, message, date) values (%s,%s,%s,%s,%s,%s,%s,%s,%s)"
cursor.execute(cmd, (code, network, channel, host, zuser, user_mode, target_user, message, date))
cursor.close()
conn.commit()
except Exception as e:
self.PutModule("Could not save {0} to database caused by: {1} {2}".format(code, type(e), str(e)))
journal.send(repr(e))
def resolveTarget(self, target):
target = target.s
channel = self.GetNetwork().FindChan(target)
if channel == None:
return (None, None)
user = self.GetNetwork().GetIRCNick()
return (channel, user)
def findMode(self, channel, user):
realUser = channel.FindNick(user.GetNick())
return chr(realUser.GetPermChar())
def OnTopic(self, user, channel, message):
try:
self.insert("TOPIC", channel.GetName(), user.GetHost(), user.GetNick(), None, self.timestamp(), None, message.s, str(self.GetNetwork()))
except Exception as e:
journal.send(repr(e))
return True
def OnQuit(self, user, message, channels):
try:
for channel in channels:
self.insert("QUIT", channel.GetName(), user.GetHost(), user.GetNick(), None, self.timestamp(), None, message, str(self.GetNetwork()))
except Exception as e:
journal.send(repr(e))
return True
def OnNick(self, user, new_nick, channels):
try:
for channel in channels:
self.insert("NICK", channel.GetName(), user.GetHost(), user.GetNick(), None, self.timestamp(), new_nick, None, str(self.GetNetwork()))
except Exception as e:
journal.send(repr(e))
return True
def OnKick(self, user, target_nick, channel, message):
try:
self.insert("KICK", channel.GetName(), user.GetHost(), user.GetNick(), None, self.timestamp(), target_nick, message, str(self.GetNetwork()))
except Exception as e:
journal.send(repr(e))
return True
def OnJoin(self, user, channel):
try:
self.insert("JOIN", channel.GetName(), user.GetHost(), user.GetNick(), None, self.timestamp(), None, None, str(self.GetNetwork()))
except Exception as e:
journal.send(repr(e))
return True
def OnUserAction (self, target, message):
(channel, user) = self.resolveTarget(target)
if channel == None:
return True
return self.OnChanAction(user, channel, message)
def OnUserMsg (self, target, message):
(channel, user) = self.resolveTarget(target)
if channel == None:
return True
return self.OnChanMsg(user, channel, message)
def OnChanAction(self, user, channel, message):
try:
self.insert("ME", channel.GetName(), user.GetHost(), user.GetNick(), None, self.timestamp(), None, message.s, str(self.GetNetwork()))
except Exception as e:
journal.send(repr(e))
return True
def OnMode(self, user, channel, mode, argument, added, noChange):
mode = chr(mode)
code = "MODE"
if mode == "b":
code = "BAN" if added else "UNBAN"
else:
sign = "+" if added else "-"
argument = "{0}{1} {2}".format(sign, mode, argument)
try:
self.insert(code, channel.GetName(), user.GetHost(), user.GetNick(), None, self.timestamp(), None, argument, str(self.GetNetwork()))
except Exception as e:
journal.self(repr(e))
return True
def OnOp(self, user, target_user, channel, noChange):
try:
self.insert("OP", channel.GetName(), user.GetHost(), user.GetNick(), None, self.timestamp(), target_user.GetNick(), None, str(self.GetNetwork()))
except Exception as e:
journal.self(repr(e))
return True
def OnDeop(self, user, target_user, channel, noChange):
try:
self.insert("DEOP", channel.GetName(), user.GetHost(), user.GetNick(), None, self.timestamp(), target_user.GetNick(), None, str(self.GetNetwork()))
except Exception as e:
journal.self(repr(e))
return True
def OnVoice(self, user, target_user, channel, noChange):
try:
self.insert("VOICE", channel.GetName(), user.GetHost(), user.GetNick(), None, self.timestamp(), target_user.GetNick(), None, str(self.GetNetwork()))
except Exception as e:
journal.self(repr(e))
return True
def OnDevoice(self, user, target_user, channel, noChange):
try:
self.insert("DEVOICE", channel.GetName(), user.GetHost(), user.GetNick(), None, self.timestamp(), target_user.GetNick(), None, str(self.GetNetwork()))
except Exception as e:
journal.send(repr(e))
return True
''' This is broken, will break znc, don't know why, don't care at all, if someone feels like fixing it, feel free
def OnLoad(self, args, message):
match = re.search("(.*?):(.*?)@(.*)", args)
if match:
self.username = match.group(1)
self.password = match.group(2)
self.host = match.group(3)
return True
else:
return False
'''
'''
Database Schema:
CREATE TABLE chanlog (
id SERIAL,
code varchar(10) DEFAULT NULL,
network varchar(64) DEFAULT NULL,
channel varchar(64) DEFAULT NULL,
host varchar(128) DEFAULT NULL,
zuser varchar(32) DEFAULT NULL,
user_mode char(1) DEFAULT NULL,
target_user varchar(32) DEFAULT NULL,
message text,
date timestamp DEFAULT NULL,
PRIMARY KEY (id)
);
'''