Skip to content

Instantly share code, notes, and snippets.

@mopemope
Created May 11, 2011 14:26
Show Gist options
  • Save mopemope/966543 to your computer and use it in GitHub Desktop.
Save mopemope/966543 to your computer and use it in GitHub Desktop.
irc logger
from twisted.internet import reactor, protocol
from twisted.words.protocols import irc
import time
name = 'logger'
channels = ['#coderepos-en', '#1986', '#subtech',
'#django-dev', '#pocoo']
logfiles = {}
class LoggingIRCClient(irc.IRCClient):
nickname = name
def signedOn(self):
for channel in channels:
self.join(channel)
print "join %s" % channel
def privmsg(self, user, channel, message):
t = time.strftime('%Y-%m-%d %H:%M:%S')
ch = channel.split('#')[-1]
msg = t + ' ' + user.split('!')[0] + ' -> ' + message
print(' '.join([channel, msg]))
logfile = None
if channel in logfiles:
logfile = logfiles[channel]
else:
logfile = file('/home/ma2/Dropbox/Public/' + ch + '.txt', 'a+')
logfiles[channel] = logfile
logfile.write(msg + '\n')
logfile.flush()
def userJoined(self, user, channel):
"""This will get called when the bot sees someone do an
action."""
user = user.split('!', 1)[0]
print "* join %s" % (user)
self.mode(channel, True, 'o', user = user)
def main():
f = protocol.ReconnectingClientFactory()
f.protocol = LoggingIRCClient
reactor.connectTCP('irc.freenode.net', 6667, f)
reactor.run()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment