Created
February 12, 2011 21:58
-
-
Save e000/824172 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import sys | |
| import socket | |
| import string | |
| import os | |
| VERBOSE = True | |
| class AstonixIRCBot(object): | |
| socket = None | |
| connected = False | |
| def __init__(self, network, port, channels = None, | |
| nickname = 'Obelix', ident = 'Obelix', | |
| realname = 'Obelix', commandcharacter = '!', autoreconnect = False): | |
| self.network, self.port, self.channels, self.nickname, \ | |
| self.ident, self.realname, self.commandCharacter, self.autoReconnect = \ | |
| network, int(port), channels or [], nickname, ident, realname, commandcharacter, autoreconnect | |
| def connect(self): | |
| if not connected: | |
| try: | |
| self.socket = socket(socket.AF_INET, socket.SOCK_STREAM) | |
| self.socket.connect((self.network, self.port)) | |
| except socket.error, e: | |
| print 'failed to connect', e | |
| else: | |
| connected = True | |
| self._logOn() | |
| self._main() | |
| self._onDisconnect() | |
| def disconnect(self): | |
| self.socket.disconnect() | |
| self.connected = False | |
| self.socket = None | |
| def sendLine(self, line): | |
| if self.socket and self.connected: | |
| self.socket.send(line + '\r\n') | |
| if VERBOSE: | |
| print '<<', line | |
| elif VERBOSE: | |
| print 'did not send', line, 'not connected.' | |
| def privmsg(self, target, message): | |
| self.sendLine('PRIVMSG %s :%s' % (target, message)) | |
| def _logOn(self): | |
| self.sendLine('NICK %s' % self.nickname) | |
| self.sendLine('USER %s * * :%s' % (self.ident, self.realname)) | |
| def IRC_005(self, line): | |
| if VERBOSE: | |
| print 'signed on!' | |
| if self.channels: | |
| self.sendLine('JOIN %s' % ','.join(self.channels)) | |
| def _doPong(self, line): | |
| self.sendLine('PONG %s' % line.split()[1]) | |
| def _main(self): | |
| buf = self.socket.makefile() | |
| for line in buf: | |
| if not self.connected: | |
| break | |
| line = line.rstrip() | |
| if VERBOSE: | |
| print '>>', line | |
| if line.startswith('PING'): | |
| return self._doPong(line) | |
| if line.startswith(':'): | |
| parts = line[1:].split(" ") | |
| if len(parts) >= 2: | |
| command = parts[1] | |
| callback = getattr('IRC_%s' % command, self, None) | |
| if callback: | |
| try: | |
| callback(line, params) | |
| except e: | |
| print 'error', e | |
| elif VERBOSE: | |
| print 'unhandled' , command | |
| def _onDisconnect(self): | |
| if not self.connected and not self.socket: | |
| return | |
| if VERBOSE: | |
| print 'disconnected from server!' | |
| self.socket.disconnect() | |
| self.connected = False | |
| self.socket = None | |
| if self.autoReconnect: | |
| if VERBOSE: | |
| print 'reconnecting in 2 secs' | |
| import time | |
| time.sleep(2) | |
| self.connect() | |
| def IRC_PRIVMSG(self, line, params): | |
| nickname = params[0].split('!')[0] | |
| message = line[line.find(' :')+2:] | |
| channel = params[2] | |
| if message.startswith(self.commandCharacter): | |
| try: | |
| command, params = message[1:].split(' ', 1) | |
| except ValueError: | |
| command, params = message[1:], [] | |
| callback = getattr('COMMAND_%s' % command.lower(), self, None) | |
| if callback: | |
| callback(nickname, channel, message, params) | |
| def COMMAND_COMMANDS(self, nickname, channel, message, params): | |
| self.bot.privmsg(channel, "Use prefix ! for commands: Commands, About.") | |
| if __name__ == '__main__': | |
| network = 'dragon.anonops.ru' | |
| port = 6667 | |
| bot = AstonixIRCBot(network, port, channels = ['#bottesting']) | |
| bot.connect() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment