Advertisement
Guest User

Untitled

a guest
Nov 14th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # encoding: utf-8
  3.  
  4. """An implementation of a very simple IRC bot using the Protocol wrappers. This is the main script.
  5.  
  6. Requires: pymongo
  7. """
  8.  
  9. import logging
  10. import re
  11.  
  12. from protocol import LineProtocol
  13.  
  14. from plugins import *
  15.  
  16.  
  17. log = logging.getLogger(__name__)
  18.  
  19.  
  20. class IRCProtocol(LineProtocol):
  21. def __init__(self, host, port, nick="arkivo", user="Arkivo", name="Arkivo IRC Logger", password=None):
  22. super(IRCProtocol, self).__init__(host, port)
  23.  
  24. self.nick = nick
  25. self.user = user
  26. self.name = name
  27. self.password = password
  28.  
  29. self.plugins = {}
  30.  
  31. def __call__(self):
  32. """This protocol only implements an IRC client, default to a client."""
  33.  
  34. super(IRCProtocol, self).__call__(False)
  35.  
  36. def listen(self, *args, **kw):
  37. """This protocol only implements an IRC client."""
  38.  
  39. raise NotImplementedError()
  40.  
  41. def write(self, line):
  42. """We add debug logging of all data written to the server."""
  43. log.info("<- %s", unicode(line).decode('utf-8', 'replace'))
  44. super(IRCProtocol, self).write(line.encode('utf-8'))
  45.  
  46. def send(self, text=None, *args):
  47. if text:
  48. text = text.replace('\n', '').replace('\r', '')
  49.  
  50. args = [arg.replace('\n', '').replace('\r', '') for arg in args]
  51.  
  52. self.write(' '.join(args) + ('' if not text else (' :' + text + '\r\n')))
  53.  
  54. def connect(self):
  55. log.info("Connecting to irc://%s:%s...", *self.address)
  56. super(IRCProtocol, self).connect()
  57.  
  58. def connected(self):
  59. log.warn("Connected to irc://%s:%s.", *self.address)
  60.  
  61. if self.password:
  62. self.send(None, 'PASS', self.password)
  63.  
  64. self.send(None, 'NICK', self.nick)
  65. self.send(self.name, 'USER', self.user, '+iw', self.nick)
  66.  
  67. super(IRCProtocol, self).connected()
  68.  
  69. def stopped(self):
  70. log.warn("Disconnected from irc://%s:%s.", *self.address)
  71.  
  72. def register(self, plugin, *args, **kw):
  73. instance = plugin(*args, **kw)
  74.  
  75. for syntax in instance.syntax:
  76. self.plugins[re.compile(syntax)] = instance
  77.  
  78. def process(self, line):
  79. if line[0] == ':':
  80. line = line[1:]
  81.  
  82. log.info("-> %s", line.decode('utf-8', 'replace'))
  83.  
  84. for syntax, plugin in self.plugins.iteritems():
  85. match = syntax.search(line)
  86.  
  87. if match:
  88. # log.debug("Matched %r: %r", plugin, match)
  89. if plugin(self, **match.groupdict()):
  90. return True
  91.  
  92.  
  93. if __name__ == '__main__':
  94. logging.basicConfig(level=logging.WARN)
  95.  
  96. protocol = IRCProtocol("irc.freenode.net", 6667, 'mvp-bot')
  97.  
  98. protocol.register(Ping)
  99. protocol.register(UserPing)
  100. protocol.register(Echo)
  101. protocol.register(Control)
  102. protocol.register(Logger, 'localhost', 'arkivo')
  103. protocol.register(Join, channels=['mvp-devs', 'turbogears', 'webcore'])
  104.  
  105. protocol()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement