forked from hashweb/LogsToDB
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchannelLogger_model.py
More file actions
126 lines (100 loc) · 4.19 KB
/
channelLogger_model.py
File metadata and controls
126 lines (100 loc) · 4.19 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
#!/usr/bin/python
import sys
import json
import os
import time
import psycopg2
class LogviewerDB:
def __init__(self):
# Will only work on UNIX
if (hasattr(time, 'tzset')):
os.environ['TZ'] = 'Europe/London'
time.tzset()
# DB connection string
try:
with open('plugins/ChannelLogger/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()
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
print "connected!\n"
def add_message(self, user, host, msg):
self.__add_message(user, host, msg, 'message')
def add_join(self, user, host):
self.__add_message(user, host, '', 'join')
def add_part(self, user, host):
self.__add_message(user, host, '', 'part')
def add_quit(self, user, host):
self.__add_message(user, host, '', 'quit')
def add_emote(self, user, host, msg,):
self.__add_message(user, host, msg, 'emote')
def __add_message(self, user, host, msg, action):
# 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);
if (action == 'message' or action == 'emote'):
self.cursor.execute("INSERT INTO messages (\"user\", \"content\", \"action\") VALUES (%s, %s, %s)", (userID, msg, action))
else:
self.cursor.execute("INSERT INTO messages (\"user\", \"action\") VALUES (%s, %s)", (userID, action))
self.conn.commit()
# Check if user exists then return the user ID, if not return false
def check_user_host_exists(self, user, host):
self.cursor.execute("SELECT * FROM users WHERE \"user\"= %s AND host=%s", (user, host))
if self.cursor.rowcount:
return self.cursor.fetchone()[1]
else:
return False
def resetData(self):
self.cursor.execute("DELETE FROM users;")
self.cursor.execute("DELETE FROM messages;")
self.cursor.execute("ALTER SEQUENCE messages_id_seq RESTART WITH 1;")
self.cursor.execute("ALTER SEQUENCE users_id_seq RESTART WITH 1;")
self.conn.commit()
class LogviewerFile:
def __init__(self):
# Get Logfile Path
try:
with open('plugins/ChannelLogger/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")
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:
logFile.write("%s <%s> %s \n" % (time_stamp, user, 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:
logFile.write("%s --> <%s> (%s) joins %s \n" % (time_stamp, user, host, channel))
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:
logFile.write("%s <-- <%s> (%s) parts %s \n" % (time_stamp, user, host, channel))
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:
logFile.write("%s <-- <%s> (%s) quits %s \n" % (time_stamp, user, host, channel))
def main():
logviewerDB = LogviewerDB()
logviewerDB.resetData();
if __name__ == "__main__":
main()