From b79305412d6bedc3a3505165e107161bdaca82f0 Mon Sep 17 00:00:00 2001 From: Finn Date: Sat, 22 Nov 2014 18:53:56 -0800 Subject: [PATCH 01/10] Added an IRC logger --- kippo/dblog/irc.py | 85 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 kippo/dblog/irc.py diff --git a/kippo/dblog/irc.py b/kippo/dblog/irc.py new file mode 100644 index 0000000..ee5865e --- /dev/null +++ b/kippo/dblog/irc.py @@ -0,0 +1,85 @@ +from kippo.core import dblog +from twisted.internet import defer +from twisted.python import log +from twisted.python import log +from asyncirc.ircbot import IRCBot +import threading + +class DBLogger(dblog.DBLogger): + def start(self, cfg): + if cfg.has_option('database_irc', 'port'): + port = int(cfg.get('database_irc', 'port')) + else: + port = 6667 + + nick = self.getSensor() + if cfg.has_option('database_irc', 'nick'): + nick = cfg.get('database_irc', 'nick') + if nick is None: + import random + import string + nick = ''.join(random.choice(string.ascii_lowercase) for _ in range(8)) + + self.channels = ['kippo-events'] + if cfg.has_option('database_irc', 'channel'): + self.channels = cfg.get('database_irc', 'channel').split(",") + + server = 'irc.efnet.org' + if cfg.has_option('database_irc', 'server'): + server = cfg.get('database_irc', 'server') + + password = None + if cfg.has_option('database_irc', 'password'): + password = cfg.get('database_irc', 'password') + + ssl = False + if cfg.has_option('database_irc', 'ssl'): + ssl = cfg.get('database_irc', 'ssl') in ('1', 'true', 'yes') + + self.connection = IRCBot(server, port, nick, 'Kippo', password, ssl) + self.connection.start() + for channel in self.channels: + self.connection.join(channel) + + def write(self, session, message): + if self.connection: + for channel in self.channels: + self.connection.msg(channel, "[%s] %s" % (session, message)) + + def createSession(self, peerIP, peerPort, hostIP, hostPort): + sid = uuid.uuid1().hex + sensorname = self.getSensor() or hostIP + self.write(sid, 'New connection: %s:%s' % (peerIP, peerPort)) + return sid + + def handleConnectionLost(self, session, args): + self.write(session, 'Connection lost') + + def handleLoginFailed(self, session, args): + self.write(session, 'Login failed [%s/%s]' % \ + (args['username'], args['password'])) + + def handleLoginSucceeded(self, session, args): + self.write(session, 'Login succeeded [%s/%s]' % \ + (args['username'], args['password'])) + + def handleCommand(self, session, args): + self.write(session, 'Command [%s]' % (args['input'],)) + + def handleUnknownCommand(self, session, args): + self.write(session, 'Unknown command [%s]' % (args['input'],)) + + def handleInput(self, session, args): + self.write(session, 'Input [%s] @%s' % (args['input'], args['realm'])) + + def handleTerminalSize(self, session, args): + self.write(session, 'Terminal size: %sx%s' % \ + (args['width'], args['height'])) + + def handleClientVersion(self, session, args): + self.write(session, 'Client version: [%s]' % (args['version'],)) + + def handleFileDownload(self, session, args): + self.write(session, 'File download: [%s] -> %s' % \ + (args['url'], args['outfile'])) +# vim: set sw=4 et: From 8bc6d6b650020cf5a6791d1234786846159232df Mon Sep 17 00:00:00 2001 From: Finn Date: Sat, 22 Nov 2014 19:05:08 -0800 Subject: [PATCH 02/10] IRC config example --- kippo.cfg.dist | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/kippo.cfg.dist b/kippo.cfg.dist index ffd407f..b07a938 100644 --- a/kippo.cfg.dist +++ b/kippo.cfg.dist @@ -199,3 +199,19 @@ interact_port = 5123 #[database_textlog] #logfile = kippo-textlog.log + + +# IRC based logging module +# +# Logs to an IRC server. The default settings are shown, nick is randomly +# generated unless specified. Channel can be a list of comma separated channels, +# or even another nick (which will get PMs). +# +# To enable this module, remove the comments below, including the +# [database_irc] line. +#[database_irc] +#server=irc.efnet.org +#nick=kippobot +#channel=#kippo-events +#ssl=false +#password= From 1d9afcd6393ce444be9fd5ca3cf3f8b3f36e5ac7 Mon Sep 17 00:00:00 2001 From: Finn Date: Sat, 22 Nov 2014 19:28:09 -0800 Subject: [PATCH 03/10] ssl doesnt work too good apparently --- kippo/dblog/irc.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/kippo/dblog/irc.py b/kippo/dblog/irc.py index ee5865e..2900c67 100644 --- a/kippo/dblog/irc.py +++ b/kippo/dblog/irc.py @@ -32,11 +32,7 @@ def start(self, cfg): if cfg.has_option('database_irc', 'password'): password = cfg.get('database_irc', 'password') - ssl = False - if cfg.has_option('database_irc', 'ssl'): - ssl = cfg.get('database_irc', 'ssl') in ('1', 'true', 'yes') - - self.connection = IRCBot(server, port, nick, 'Kippo', password, ssl) + self.connection = IRCBot(server, port, nick, nick, 'Kippo', password) self.connection.start() for channel in self.channels: self.connection.join(channel) From bfe6740b44311d1b01c4b6bb4f7e0b216ad91727 Mon Sep 17 00:00:00 2001 From: Finn Date: Sat, 22 Nov 2014 19:50:43 -0800 Subject: [PATCH 04/10] Cleaned up some things for pylint --- kippo/dblog/irc.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/kippo/dblog/irc.py b/kippo/dblog/irc.py index 2900c67..4ac8b85 100644 --- a/kippo/dblog/irc.py +++ b/kippo/dblog/irc.py @@ -1,9 +1,6 @@ from kippo.core import dblog -from twisted.internet import defer -from twisted.python import log -from twisted.python import log from asyncirc.ircbot import IRCBot -import threading +import uuid class DBLogger(dblog.DBLogger): def start(self, cfg): @@ -44,7 +41,6 @@ def write(self, session, message): def createSession(self, peerIP, peerPort, hostIP, hostPort): sid = uuid.uuid1().hex - sensorname = self.getSensor() or hostIP self.write(sid, 'New connection: %s:%s' % (peerIP, peerPort)) return sid From 21e177626d72498d376e60d46749c3d4892b455e Mon Sep 17 00:00:00 2001 From: Finn Date: Sat, 22 Nov 2014 20:07:12 -0800 Subject: [PATCH 05/10] Fixed one last pylint error --- kippo/dblog/irc.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/kippo/dblog/irc.py b/kippo/dblog/irc.py index 4ac8b85..35836a0 100644 --- a/kippo/dblog/irc.py +++ b/kippo/dblog/irc.py @@ -3,31 +3,31 @@ import uuid class DBLogger(dblog.DBLogger): - def start(self, cfg): - if cfg.has_option('database_irc', 'port'): + def start(self): + if self.cfg.has_option('database_irc', 'port'): port = int(cfg.get('database_irc', 'port')) else: port = 6667 nick = self.getSensor() - if cfg.has_option('database_irc', 'nick'): - nick = cfg.get('database_irc', 'nick') + if self.cfg.has_option('database_irc', 'nick'): + nick = self.cfg.get('database_irc', 'nick') if nick is None: import random import string nick = ''.join(random.choice(string.ascii_lowercase) for _ in range(8)) self.channels = ['kippo-events'] - if cfg.has_option('database_irc', 'channel'): - self.channels = cfg.get('database_irc', 'channel').split(",") + if self.cfg.has_option('database_irc', 'channel'): + self.channels = self.cfg.get('database_irc', 'channel').split(",") server = 'irc.efnet.org' - if cfg.has_option('database_irc', 'server'): - server = cfg.get('database_irc', 'server') + if self.cfg.has_option('database_irc', 'server'): + server = self.cfg.get('database_irc', 'server') password = None - if cfg.has_option('database_irc', 'password'): - password = cfg.get('database_irc', 'password') + if self.cfg.has_option('database_irc', 'password'): + password = self.cfg.get('database_irc', 'password') self.connection = IRCBot(server, port, nick, nick, 'Kippo', password) self.connection.start() From 6ddaa59975dca9a82a06f144dfb6394b01b5111c Mon Sep 17 00:00:00 2001 From: Finn Date: Sat, 22 Nov 2014 20:09:45 -0800 Subject: [PATCH 06/10] Oops, that breaks things --- kippo/dblog/irc.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/kippo/dblog/irc.py b/kippo/dblog/irc.py index 35836a0..4ac8b85 100644 --- a/kippo/dblog/irc.py +++ b/kippo/dblog/irc.py @@ -3,31 +3,31 @@ import uuid class DBLogger(dblog.DBLogger): - def start(self): - if self.cfg.has_option('database_irc', 'port'): + def start(self, cfg): + if cfg.has_option('database_irc', 'port'): port = int(cfg.get('database_irc', 'port')) else: port = 6667 nick = self.getSensor() - if self.cfg.has_option('database_irc', 'nick'): - nick = self.cfg.get('database_irc', 'nick') + if cfg.has_option('database_irc', 'nick'): + nick = cfg.get('database_irc', 'nick') if nick is None: import random import string nick = ''.join(random.choice(string.ascii_lowercase) for _ in range(8)) self.channels = ['kippo-events'] - if self.cfg.has_option('database_irc', 'channel'): - self.channels = self.cfg.get('database_irc', 'channel').split(",") + if cfg.has_option('database_irc', 'channel'): + self.channels = cfg.get('database_irc', 'channel').split(",") server = 'irc.efnet.org' - if self.cfg.has_option('database_irc', 'server'): - server = self.cfg.get('database_irc', 'server') + if cfg.has_option('database_irc', 'server'): + server = cfg.get('database_irc', 'server') password = None - if self.cfg.has_option('database_irc', 'password'): - password = self.cfg.get('database_irc', 'password') + if cfg.has_option('database_irc', 'password'): + password = cfg.get('database_irc', 'password') self.connection = IRCBot(server, port, nick, nick, 'Kippo', password) self.connection.start() From 27df1033af5547e8b385914a4693173bb6f846cf Mon Sep 17 00:00:00 2001 From: Finn Date: Sat, 22 Nov 2014 21:14:21 -0800 Subject: [PATCH 07/10] Default channels should be a channel --- kippo/dblog/irc.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/kippo/dblog/irc.py b/kippo/dblog/irc.py index 4ac8b85..afbe4fc 100644 --- a/kippo/dblog/irc.py +++ b/kippo/dblog/irc.py @@ -17,7 +17,7 @@ def start(self, cfg): import string nick = ''.join(random.choice(string.ascii_lowercase) for _ in range(8)) - self.channels = ['kippo-events'] + self.channels = ['#kippo-events'] if cfg.has_option('database_irc', 'channel'): self.channels = cfg.get('database_irc', 'channel').split(",") @@ -31,6 +31,7 @@ def start(self, cfg): self.connection = IRCBot(server, port, nick, nick, 'Kippo', password) self.connection.start() + for channel in self.channels: self.connection.join(channel) From 2760e725f3b1972c225c0bd3f5f1325e5a393fa0 Mon Sep 17 00:00:00 2001 From: Finn Date: Sun, 23 Nov 2014 00:22:08 -0800 Subject: [PATCH 08/10] I made it work! No idea how/why --- kippo/dblog/irc.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/kippo/dblog/irc.py b/kippo/dblog/irc.py index afbe4fc..e8266a7 100644 --- a/kippo/dblog/irc.py +++ b/kippo/dblog/irc.py @@ -1,9 +1,11 @@ from kippo.core import dblog from asyncirc.ircbot import IRCBot +import logging import uuid class DBLogger(dblog.DBLogger): def start(self, cfg): + logging.basicConfig(filename='debug.log', level=logging.DEBUG) if cfg.has_option('database_irc', 'port'): port = int(cfg.get('database_irc', 'port')) else: @@ -17,9 +19,9 @@ def start(self, cfg): import string nick = ''.join(random.choice(string.ascii_lowercase) for _ in range(8)) - self.channels = ['#kippo-events'] + self.channel = '#kippo-events' if cfg.has_option('database_irc', 'channel'): - self.channels = cfg.get('database_irc', 'channel').split(",") + self.channels = cfg.get('database_irc', 'channel') server = 'irc.efnet.org' if cfg.has_option('database_irc', 'server'): @@ -32,13 +34,14 @@ def start(self, cfg): self.connection = IRCBot(server, port, nick, nick, 'Kippo', password) self.connection.start() - for channel in self.channels: - self.connection.join(channel) + print "Connected to %s/%s" % (server, port) + + print "Joining %s" % self.channel + self.connection.join(self.channel) def write(self, session, message): - if self.connection: - for channel in self.channels: - self.connection.msg(channel, "[%s] %s" % (session, message)) + self.connection.msg(self.channel, "[%s] %s" % (session, message)) + print "Sending to %s: [%s] %s" % (self.channel, session, message) def createSession(self, peerIP, peerPort, hostIP, hostPort): sid = uuid.uuid1().hex From 975ddb33472121185de587f9e599c9da7d10efc0 Mon Sep 17 00:00:00 2001 From: Finn Date: Sun, 23 Nov 2014 00:23:34 -0800 Subject: [PATCH 09/10] removed excessive logging --- kippo/dblog/irc.py | 6 ------ 1 file changed, 6 deletions(-) diff --git a/kippo/dblog/irc.py b/kippo/dblog/irc.py index e8266a7..69e0666 100644 --- a/kippo/dblog/irc.py +++ b/kippo/dblog/irc.py @@ -1,11 +1,9 @@ from kippo.core import dblog from asyncirc.ircbot import IRCBot -import logging import uuid class DBLogger(dblog.DBLogger): def start(self, cfg): - logging.basicConfig(filename='debug.log', level=logging.DEBUG) if cfg.has_option('database_irc', 'port'): port = int(cfg.get('database_irc', 'port')) else: @@ -34,14 +32,10 @@ def start(self, cfg): self.connection = IRCBot(server, port, nick, nick, 'Kippo', password) self.connection.start() - print "Connected to %s/%s" % (server, port) - - print "Joining %s" % self.channel self.connection.join(self.channel) def write(self, session, message): self.connection.msg(self.channel, "[%s] %s" % (session, message)) - print "Sending to %s: [%s] %s" % (self.channel, session, message) def createSession(self, peerIP, peerPort, hostIP, hostPort): sid = uuid.uuid1().hex From d1042a85754d5d5de6e1df7f4e3107f76bfca7d6 Mon Sep 17 00:00:00 2001 From: Finn Date: Sun, 23 Nov 2014 00:26:38 -0800 Subject: [PATCH 10/10] Since we dont do lists of channels anymore, remove it from the example text --- kippo.cfg.dist | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/kippo.cfg.dist b/kippo.cfg.dist index b07a938..04fc05a 100644 --- a/kippo.cfg.dist +++ b/kippo.cfg.dist @@ -204,8 +204,7 @@ interact_port = 5123 # IRC based logging module # # Logs to an IRC server. The default settings are shown, nick is randomly -# generated unless specified. Channel can be a list of comma separated channels, -# or even another nick (which will get PMs). +# generated unless specified. Channel can be either a channel or a nick # # To enable this module, remove the comments below, including the # [database_irc] line.