1
0
Fork 0
misc/fox.py

116 lines
2.8 KiB
Python

#!/usr/bin/env python
# coding: utf-8
#
# fox.py
# http://hg.mornie.org/misc/file/tip/fox.py
#
# Copyright © 2013 Daniele Tricoli <eriol@mornie.org>
#
# This program is free software. It comes without any warranty, to
# the extent permitted by applicable law. You can redistribute it
# and/or modify it under the terms of the Do What The Fuck You Want
# To Public License, Version 2, as published by Sam Hocevar.
# See http://www.wtfpl.net/ for more details.
#
# A present for Sidhe and ghunter!
'''
Simple IRC bot to tell you 'what the fox say' :)
You need a configuration file in ~/.fox.cfg that would look like:
[server]
hostname = your.irc.server
port = 9999
[irc]
channel = text
The fox is very bashful, it speaks only over SSL!
'''
import random
from twisted.internet import reactor, protocol, ssl
from twisted.words.protocols import irc
BOT_NICKNAME = '_the_fox_'
class Fox(object):
calls = ['Ring-ding-ding-ding-dingeringeding!',
'Gering-ding-ding-ding-dingeringeding!',
'Wa-pa-pa-pa-pa-pa-pow!',
'Hatee-hatee-hatee-ho!',
'Joff-tchoff-tchoffo-tchoffo-tchoff!',
'Tchoff-tchoff-tchoffo-tchoffo-tchoff!',
'Jacha-chacha-chacha-chow!',
'Chacha-chacha-chacha-chow!',
'Fraka-kaka-kaka-kaka-kow!',
'A-hee-ahee ha-hee!',
'A-oo-oo-oo-ooo!',
'Woo-oo-oo-ooo!']
@classmethod
def say(self):
return random.choice(Fox.calls)
class FoxBot(irc.IRCClient):
nickname = BOT_NICKNAME
def signedOn(self):
self.join(self.factory.channel)
def privmsg(self, user, channel, msg):
user = user.split('!', 1)[0]
# The fox is shy, it does't speak in private!
if channel == self.nickname:
return
if msg.startswith(self.nickname + ':'):
msg = '{0}: {1}'.format(user, Fox.say())
self.msg(channel, msg)
class FoxBotFactory(protocol.ClientFactory):
protocol = FoxBot
def __init__(self, channel):
self.channel = channel
def clientConnectionLost(self, connector, reason):
connector.connect()
def clientConnectionFailed(self, connector, reason):
print 'connection failed:', reason
reactor.stop()
if __name__ == '__main__':
import ConfigParser
import os
import sys
config = ConfigParser.ConfigParser()
found = config.read(os.path.expanduser('~/.fox.cfg'))
if not found:
sys.exit('You must provide ~/.fox.cfg')
hostname = config.get('server', 'hostname')
port = config.getint('server', 'port')
channel = config.get('irc', 'channel')
f = FoxBotFactory(channel)
reactor.connectSSL(hostname, port, f, ssl.ClientContextFactory())
reactor.run()