Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # encoding: utf-8
- """An implementation of a very simple IRC bot using the Protocol wrappers. This is the main script.
- Requires: pymongo
- """
- import logging
- import re
- from protocol import LineProtocol
- from plugins import *
- log = logging.getLogger(__name__)
- class IRCProtocol(LineProtocol):
- def __init__(self, host, port, nick="arkivo", user="Arkivo", name="Arkivo IRC Logger", password=None):
- super(IRCProtocol, self).__init__(host, port)
- self.nick = nick
- self.user = user
- self.name = name
- self.password = password
- self.plugins = {}
- def __call__(self):
- """This protocol only implements an IRC client, default to a client."""
- super(IRCProtocol, self).__call__(False)
- def listen(self, *args, **kw):
- """This protocol only implements an IRC client."""
- raise NotImplementedError()
- def write(self, line):
- """We add debug logging of all data written to the server."""
- log.info("<- %s", unicode(line).decode('utf-8', 'replace'))
- super(IRCProtocol, self).write(line.encode('utf-8'))
- def send(self, text=None, *args):
- if text:
- text = text.replace('\n', '').replace('\r', '')
- args = [arg.replace('\n', '').replace('\r', '') for arg in args]
- self.write(' '.join(args) + ('' if not text else (' :' + text + '\r\n')))
- def connect(self):
- log.info("Connecting to irc://%s:%s...", *self.address)
- super(IRCProtocol, self).connect()
- def connected(self):
- log.warn("Connected to irc://%s:%s.", *self.address)
- if self.password:
- self.send(None, 'PASS', self.password)
- self.send(None, 'NICK', self.nick)
- self.send(self.name, 'USER', self.user, '+iw', self.nick)
- super(IRCProtocol, self).connected()
- def stopped(self):
- log.warn("Disconnected from irc://%s:%s.", *self.address)
- def register(self, plugin, *args, **kw):
- instance = plugin(*args, **kw)
- for syntax in instance.syntax:
- self.plugins[re.compile(syntax)] = instance
- def process(self, line):
- if line[0] == ':':
- line = line[1:]
- log.info("-> %s", line.decode('utf-8', 'replace'))
- for syntax, plugin in self.plugins.iteritems():
- match = syntax.search(line)
- if match:
- # log.debug("Matched %r: %r", plugin, match)
- if plugin(self, **match.groupdict()):
- return True
- if __name__ == '__main__':
- logging.basicConfig(level=logging.WARN)
- protocol = IRCProtocol("irc.freenode.net", 6667, 'mvp-bot')
- protocol.register(Ping)
- protocol.register(UserPing)
- protocol.register(Echo)
- protocol.register(Control)
- protocol.register(Logger, 'localhost', 'arkivo')
- protocol.register(Join, channels=['mvp-devs', 'turbogears', 'webcore'])
- protocol()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement