Created
June 12, 2012 17:55
-
-
Save jul/2919021 to your computer and use it in GitHub Desktop.
a simple tao server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# Copyright (c) Twisted Matrix Laboratories. | |
# See LICENSE for details. | |
""" | |
Twisted original example of an irc bot. | |
modified to say a sentence of the tao if .tao is written | |
""" | |
# twisted imports | |
from twisted.words.protocols import irc | |
from twisted.internet import reactor, protocol | |
from twisted.python import log | |
from random import choice | |
# system imports | |
import time, sys | |
from this import s as _tao | |
tao = _tao.split("\n") | |
def random_tao(): | |
return choice(tao).encode("rot-13") | |
class LogBot(irc.IRCClient): | |
"""A logging IRC bot.""" | |
nickname = "ShintoistLiz" | |
def connectionMade(self): | |
irc.IRCClient.connectionMade(self) | |
def connectionLost(self, reason): | |
irc.IRCClient.connectionLost(self, reason) | |
# callbacks for events | |
def signedOn(self): | |
"""Called when bot has succesfully signed on to server.""" | |
self.join(self.factory.channel) | |
def joined(self, channel): | |
"""This will get called when the bot joins the channel.""" | |
self.msg(channel,"raaah, ca fait du bien de naître vierge de tout nom") | |
def privmsg(self, user, channel, msg): | |
"""This will get called when the bot receives a message.""" | |
user = user.split('!', 1)[0] | |
# Check to see if they're sending me a private message | |
if channel == self.nickname: | |
msg = "yeah" | |
self.msg(user, msg) | |
return | |
# Otherwise check to see if it is a message directed at me | |
if msg.startswith(".tao"): | |
msg = random_tao() | |
self.msg(channel, msg) | |
def action(self, user, channel, msg): | |
"""This will get called when the bot sees someone do an action.""" | |
user = user.split('!', 1)[0] | |
# irc callbacks | |
def irc_NICK(self, prefix, params): | |
"""Called when an IRC user changes their nickname.""" | |
old_nick = prefix.split('!')[0] | |
new_nick = params[0] | |
# For fun, override the method that determines how a nickname is changed on | |
# collisions. The default method appends an underscore. | |
def alterCollidedNick(self, nickname): | |
""" | |
Generate an altered version of a nickname that caused a collision in an | |
effort to create an unused related name for subsequent registration. | |
""" | |
return nickname + '^' | |
class LogBotFactory(protocol.ClientFactory): | |
"""A factory for LogBots. | |
A new protocol instance will be created each time we connect to the server. | |
""" | |
def __init__(self, channel="#python-fr"): | |
self.channel = channel | |
def buildProtocol(self, addr): | |
p = LogBot() | |
p.factory = self | |
return p | |
def clientConnectionLost(self, connector, reason): | |
"""If we get disconnected, reconnect to server.""" | |
connector.connect() | |
def clientConnectionFailed(self, connector, reason): | |
print "connection failed:", reason | |
reactor.stop() | |
if __name__ == '__main__': | |
# initialize logging | |
log.startLogging(sys.stdout) | |
# create factory protocol and application | |
f = LogBotFactory(sys.argv[1]) | |
# connect factory to this host and port | |
reactor.connectTCP("irc.freenode.net", 6667, f) | |
# run bot | |
reactor.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment