Misza's IRC Bot

From Cometwiki

# -*- coding: utf-8 -*-
"""
An IRC bot for reporting recent changes on a MediaWiki instance.

Requires the Twisted library (http://twistedmatrix.com/).

Usage:

1) Add the following lines to the LocalSettings.php file of the wiki:

$wgRC2UDPAddress = 'example.com';
$wgRC2UDPPort = '31337';
$wgRC2UDPPrefix = "";

Where address is the host on which the bot runs and port must be the same as configured below.

2) Adjust configuration options (CAPITALIZED CONSTANTS) below.

3) Run with the following command:

$ twistd -y rc.tac

"""
#
# (C) Misza13 <misza1313@gmail.com>, 2007
#
# Distributed under the terms of the MIT license.
#
#
# Configuration section
#
# UDP_PORT
#   Port on wchich to listen for incoming packets (must be the same as set in LocalSettings.php)
UDP_PORT = 31337
#
# IRC_HOST & IRC_PORT
#   Host and port to which the reporting module should connect (the IRC server)
IRC_HOST = 'irc.example.net'
IRC_PORT = 6667
#
# IRC_NICKNAME
#   Nick to use on the IRC server
IRC_NICKNAME = 'rc'
#
# IRC_PASSWORD
#   Password to send to server on connection
IRC_PASSWORD = 'XXsecretXX'
#
# IRC_RC_CHANNEL
#   Channel to join and report in
IRC_RC_CHANNEL = '#mywiki'
#
# IRC_OPERATOR
#   The user that can order the quite bot to quit and shut down
#   Can be any regular expression, e.g.:
#       ^joe@.*
#       @unaffiliated/joe$
IRC_OPERATOR = '@unaffiliated/test'
#
# IRC_DELAY
#   Delay (in seconds) between reporting new events (used to avoid floodding)
IRC_DELAY = 0.5
#
# Configuration section ends here
#
import time, threading, re

from twisted.application import internet, service
from twisted.internet import protocol, reactor
from twisted.words.protocols import irc

Reporter = None

class InputProtocol(protocol.DatagramProtocol):
    def datagramReceived(self, datagram, addr):
        try:
            datagram = datagram[:-1]
            global Reporter
            if Reporter:
                Reporter.sync_msg(IRC_RC_CHANNEL,datagram)
        except:
            pass

class OutputProtocol(irc.IRCClient):
    def __init__(self):
        self.lock = threading.Lock()
        self.nickname = IRC_NICKNAME
        self.password = IRC_PASSWORD

    def receivedMOTD(self, motd):
        self.join(IRC_RC_CHANNEL)
        global Reporter
        Reporter = self

    def sync_msg(self, channel, line):
        try:
            self.lock.acquire()
            self.msg(channel,line)
            time.sleep(IRC_DELAY)
        finally:
            self.lock.release()

    def privmsg(self, user, channel, message):
        if channel.lower() == self.nickname.lower():
            if re.search(IRC_OPERATOR,user):
                if message.lower() == 'quit':
                    reactor.stop()

    def left(self, channel):
        self.join(channel)

    def kickedFrom(self, channel, kicker, message):
        self.join(channel)

application = service.Application('rc')
internet.UDPServer(UDP_PORT,InputProtocol()).setServiceParent(
        service.IServiceCollection(application))

oFactory = protocol.ReconnectingClientFactory()
oFactory.protocol = OutputProtocol
reactor.connectTCP(IRC_HOST,IRC_PORT,oFactory)
Personal tools
IRC Channel
My Blog