-
Notifications
You must be signed in to change notification settings - Fork 2
/
channelLogger_model.py
237 lines (197 loc) · 8.85 KB
/
channelLogger_model.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
#!/usr/bin/python
import sys
import json
import os
import string
import time
import psycopg2
import logging
import logging.handlers
import re
class LogviewerDB:
def __init__(self):
# Will only work on UNIX
if (hasattr(time, 'tzset')):
os.environ['TZ'] = 'Europe/London'
time.tzset()
fh = logging.handlers.TimedRotatingFileHandler('combined.log', when='midnight', interval=1, backupCount=5);
logging.basicConfig(level=logging.DEBUG, handlers=[fh], format="%(levelname)s: %(asctime)s - %(message)s")
self.__connect()
def __connect(self):
logging.debug("Attempting to connect with database...")
# DB connection string
try:
with open('plugins/LogsToDB/config.json') as data:
config = json.load(data)
conn_string = "host='%s' dbname='%s' user='%s' password='%s'" % (config['db']['host'], config['db']['dbname'], config['db']['user'], config['db']['password'])
except IOError as e:
print(os.getcwd())
logging.error("Error! No config file supplied, please create a config.json file in the root")
sys.exit("Error! No config file supplied, please create a config.json file in the root")
# Print connection string
print("Connecting to database\n -> %s" % (conn_string))
# get a connection
conn = psycopg2.connect(conn_string)
# conn.curser will return a cursor object, you can use this to perform queries
self.cursor = conn.cursor()
self.conn = conn
logging.debug("connected!")
def add_count(self, count, channel, topic):
count = str(count)
topic = str(topic)
channel_id = self.get_channel_id(channel)
try:
self.cursor.execute("INSERT INTO user_count (count, channel_id, topic) VALUES (%s, %s, %s)", (count, channel_id, topic))
self.conn.commit()
except psycopg2.InterfaceError as e:
logging.error('Error within add_count: ' + e.message)
logging.debug('Attempting to reconnect with database...')
self.__connect()
def add_message(self, user, host, msg, channel):
self.__add_message(user, host, msg, 'message', channel)
def add_join(self, user, host, channel):
self.__add_message(user, host, '', 'join', channel)
def add_part(self, user, host, channel):
self.__add_message(user, host, '', 'part', channel)
def add_quit(self, user, host, channel):
self.__add_message(user, host, '', 'quit', channel)
def add_emote(self, user, host, msg, channel):
self.__add_message(user, host, msg, 'emote', channel)
def __add_message(self, user, host, msg, action, channel):
try:
# Was this message from a user we already have in our database?
# If so return the userID.
userID = self.check_user_host_exists(user, host) or False
# If userID is False, store the new combo then get back the userID
if not userID:
self.cursor.execute("INSERT INTO users (\"user\", \"host\") VALUES (%s, %s)", (user, host))
self.conn.commit()
# We should now have an ID for our new user/host combo
userID = self.check_user_host_exists(user, host);
# check channel exists, if not get_channel_id will generate an ID
channel_id = self.get_channel_id(channel)
if (action == 'message' or action == 'emote'):
self.cursor.execute("INSERT INTO messages (\"user\", \"content\", \"action\", \"channel_id\") VALUES (%s, %s, %s, %s)", (userID, msg, action, channel_id))
else:
self.cursor.execute("INSERT INTO messages (\"user\", \"action\", \"channel_id\") VALUES (%s, %s, %s)", (userID, action, channel_id))
self.conn.commit()
except psycopg2.InterfaceError as e:
logging.error('Error within add_message: ' + e.message)
logging.debug('Attempting to reconnect with database...')
self.__connect()
def write_ban(self, nick, host, mode, target, channel):
try:
# check channel exists, if not get_channel_id will generate an ID
channel_id = self.get_channel_id(channel)
# Sometimes users can be kicked to another channel because of join/quit floos, make sure we strip of the ban forwarding
if (len(re.split(r'(\$#.*)', target)) > 1):
banmask = re.split(r'(\$#.*)', target)[0]
forwarded_channel = re.sub('^\$', '', re.split(r'(\$#.*)', target)[1])
self.cursor.execute("INSERT INTO bans (banmask, banned_by, channel, reason) values (%s, %s, %s, %s)", (banmask, nick, channel_id, "Join/Quit flood, user forwarded to " + forwarded_channel))
else:
banmask = re.split(r'(\$#.*)', target)[0]
self.cursor.execute("INSERT INTO bans (banmask, banned_by, channel) values (%s, %s, %s)", (banmask, nick, channel_id))
self.conn.commit()
except psycopg2.InterfaceError as e:
logging.error('Error within write_ban: ' + e.message)
logging.debug('Attempting to reconnect with database...')
self.__connect()
def write_unban(self, nick, host, mode, target, channel):
try:
# check channel exists, if not get_channel_id will generate an ID
channel_id = self.get_channel_id(channel)
self.cursor.execute("UPDATE bans SET still_banned = FALSE WHERE channel = %s AND banmask = %s", (channel_id, target))
self.conn.commit()
except psycopg2.InterfaceError as e:
logging.error('Error within write_unban: ' + e.message)
logging.debug('Attempting to reconnect with database...')
self.__connect()
# UTILITY FUNCTIONS
# Check if user exists then return the user ID, if not return false
def check_user_host_exists(self, user, host):
try:
self.cursor.execute("SELECT * FROM users WHERE \"user\"= %s AND \"host\"= %s", (user, host))
if self.cursor.rowcount:
return self.cursor.fetchone()[0]
else:
return False
except psycopg2.InterfaceError as e:
logging.error('Error within check_user_host_exists: ' + e.message)
logging.debug('Attempting to reconnect with database...')
self.__connect()
def get_channel_id(self, channel):
try:
self.cursor.execute("SELECT id FROM channels WHERE channel_name = %s", (channel,))
if self.cursor.rowcount:
return self.cursor.fetchone()[0]
else:
self.cursor.execute("INSERT INTO channels (channel_name) VALUES (%s)", (channel,))
self.conn.commit()
return self.get_channel_id(channel)
except psycopg2.InterfaceError as e:
logging.error('Error within get_channel_id: ' + e.message)
logging.debug('Attempting to reconnect with database...')
self.__connect()
# Probably don't need this actually
def get_banned_row_id(self, banmask):
try:
self.cursor.execute("SELECT id FROM bans WHERE banmask = %s", (banmask,))
if self.cursor.rowcount:
return self.cursor.fetchone()[0]
return False
except psycopg2.InterfaceError as e:
logging.error('Error within get_banned_row_id: ' + e.message)
logging.debug('Attempting to reconnect with database...')
self.__connect()
class LogviewerFile:
def __init__(self):
# Get Logfile Path
try:
with open('plugins/LogsToDB/config.json') as data:
config = json.load(data)
self.logPath = config['logs']['folderPath']
except IOError as e:
sys.exit("Error! No config file supplied, please create a config.json file in the root")
# self.all_bytes = string.maketrans('', '')
def write_message(self, user, msg):
time_stamp = time.strftime("%H:%M:%S")
dateStamp = time.strftime("%Y-%m-%d")
with open(self.logPath + "/%s.log" % dateStamp, 'a') as logFile:
msg = "%s <%s> %s\n" % (time_stamp, user, msg)
logFile.write(msg)
def write_join(self, user, host, channel):
time_stamp = time.strftime("%H:%M:%S")
dateStamp = time.strftime("%Y-%m-%d")
with open(self.logPath + "/%s.log" % dateStamp, 'a') as logFile:
msg = "%s --> <%s> (%s) joins %s \n" % (time_stamp, user, host, channel)
logFile.write(msg)
def write_part(self, user, host, channel):
time_stamp = time.strftime("%H:%M:%S")
dateStamp = time.strftime("%Y-%m-%d")
with open(self.logPath + "/%s.log" % dateStamp, 'a') as logFile:
msg = "%s <-- <%s> (%s) parts %s \n" % (time_stamp, user, host, channel)
logFile.write(msg)
def write_quit(self, user, host, channel):
time_stamp = time.strftime("%H:%M:%S")
dateStamp = time.strftime("%Y-%m-%d")
with open(self.logPath + "/%s.log" % dateStamp, 'a') as logFile:
msg = "%s <-- <%s> (%s) quits %s \n" % (time_stamp, user, host, channel)
logFile.write(msg)
def write_kick(self, target, nick, channel):
time_stamp = time.strftime("%H:%M:%S")
dateStamp = time.strftime("%Y-%m-%d")
with open(self.logPath + "/%s.log" % dateStamp, 'a') as logFile:
msg = "%s %s has kicked %s from %s \n" % (time_stamp, nick, target, channel)
logFile.write(msg)
def write_ban(self, nick, host, mode, target, channel):
time_stamp = time.strftime("%H:%M:%S")
dateStamp = time.strftime("%Y-%m-%d")
with open(self.logPath + "/%s.log" % dateStamp, 'a') as logFile:
msg = '%s %s sets mode: %s %s\n' % (time_stamp, nick, mode, target)
logFile.write(msg)
def write_unban(self, nick, host, mode, target, channel):
time_stamp = time.strftime("%H:%M:%S")
dateStamp = time.strftime("%Y-%m-%d")
with open(self.logPath + "/%s.log" % dateStamp, 'a') as logFile:
msg = '%s %s sets mode: %s %s\n' % (time_stamp, nick, mode, target)
logFile.write(msg)