Timestamp on log-messages.
This commit is contained in:
1 parent
346404ac7d
commit
888e47fb27
1 file changed
+28
-21
@@ -1,11 +1,13 @@
|
||||
import sys
|
||||
from twisted.words.protocols import irc
|
||||
from twisted.internet import protocol, reactor
|
||||
from datetime import datetime
|
||||
|
||||
class SnurrBot(irc.IRCClient):
|
||||
|
||||
def _get_nickname(self):
|
||||
return self.factory.nickname
|
||||
|
||||
nickname = property(_get_nickname)
|
||||
|
||||
def __unicode__(self):
|
||||
@@ -13,20 +15,20 @@ class SnurrBot(irc.IRCClient):
|
||||
|
||||
def signedOn(self):
|
||||
self.join(self.factory.channel)
|
||||
print "Signed on as %s." % (self.nickname,)
|
||||
log("Signed on as %s." % (self.nickname,))
|
||||
# make the client instance known to the factory
|
||||
self.factory.bot = self
|
||||
|
||||
def joined(self, channel):
|
||||
print "Joined %s." % (channel,)
|
||||
log("Joined %s." % (channel,))
|
||||
|
||||
def privmsg(self, user, channel, msg):
|
||||
# Do nothing with PRIVMSG's
|
||||
print "PRIVMSG: %s" % (msg,)
|
||||
log("PRIVMSG: %s" % (msg,))
|
||||
|
||||
def msgToChannel(self, msg):
|
||||
# Sends a message to the predefined channel
|
||||
print "Message sent to %s" % (self.factory.channel,)
|
||||
log("Message sent to %s" % (self.factory.channel,))
|
||||
self.say(self.factory.channel, msg, length=512)
|
||||
|
||||
|
||||
@@ -41,45 +43,50 @@ class SnurrBotFactory(protocol.ClientFactory):
|
||||
return "SnurrBotFactory"
|
||||
|
||||
def clientConnectionLost(self, connector, reason):
|
||||
print "Lost connection (%s), reconnecting." % (reason,)
|
||||
log("Lost connection (%s), reconnecting." % (reason,))
|
||||
connector.connect()
|
||||
|
||||
def clientConnectionFailed(self, connector, reason):
|
||||
print "Could not connect: %s" % (reason,)
|
||||
log("Could not connect: %s" % (reason,))
|
||||
|
||||
class MediaWikiProtocol(protocol.DatagramProtocol):
|
||||
class UDPListener(protocol.DatagramProtocol):
|
||||
|
||||
def __init__(self, botfactory):
|
||||
self.botfactory = botfactory
|
||||
|
||||
def startProtocol(self):
|
||||
print "Listening for Recent Changes from MediaWiki"
|
||||
log("Listening for messages.")
|
||||
|
||||
def stopProtocol(self):
|
||||
print "MediWiki listener stopped"
|
||||
log("Listener stopped")
|
||||
|
||||
def datagramReceived(self, data, (host, port)):
|
||||
# UDP messages from MediaWiki arrive here and are relayed
|
||||
# UDP messages (from MediaWiki f.ex) arrive here and are relayed
|
||||
# to the ircbot created by the bot factory.
|
||||
print "Received %r from %s:%d" % (data, host, port)
|
||||
print "Relaying msg to IRCClient in %s" % (self.botfactory,)
|
||||
log("Received %r from %s:%d" % (data, host, port))
|
||||
log("Relaying msg to IRCClient in %s" % (self.botfactory,))
|
||||
if self.botfactory.bot:
|
||||
self.botfactory.bot.msgToChannel("EDBWiki: " + data)
|
||||
self.botfactory.bot.msgToChannel(data)
|
||||
|
||||
def log(message):
|
||||
now = datetime.now().strftime("%b %d %H:%M:%S")
|
||||
print "{0} {1}".format(now, message)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
if len(sys.argv) == 3:
|
||||
chan = sys.argv[1]
|
||||
wiki_port = int(sys.argv[2])
|
||||
if len(sys.argv) == 4:
|
||||
chan = sys.argv[2]
|
||||
listen_port = int(sys.argv[3])
|
||||
|
||||
snurr = SnurrBotFactory('#' + chan)
|
||||
wiki = MediaWikiProtocol(snurr)
|
||||
listener = UDPListener(snurr)
|
||||
|
||||
# Start the MediaWiki listener.
|
||||
reactor.listenUDP(wiki_port, wiki)
|
||||
# Start the listener.
|
||||
reactor.listenUDP(listen_port, listener)
|
||||
|
||||
# Start IRC-bot on IRCNet.
|
||||
reactor.connectTCP('irc.ifi.uio.no', 6667, snurr)
|
||||
reactor.connectTCP(sys.argv[1], 6667, snurr)
|
||||
|
||||
reactor.run()
|
||||
else:
|
||||
print "Usage: python %s <IRC_CHANNEL> <MEDIAWIKI_PORT>" % (sys.argv[0],)
|
||||
print "Usage: python %s <IRC_SERVER> <IRC_CHANNEL> <UDP_PORT>" % (sys.argv[0],)
|
||||
Reference in new issue
Block a user