def __init__(self, config): """ Constructor for the bot :param host: The server address as tuple of (HOST, PORT) :param nick_name: Nickname for the bot. :param real_name: Real name of the bot. It not provided, the nick is taken as real name. """ self.config = config self.host = config.HOST self.user_name = config.IDENTITY['username'] self.nick_name = config.IDENTITY['nick'] self.real_name = config.IDENTITY['realname'] self.real_name = self.real_name or self.nick_name self.channels = {} self.bus = Bus() # initialize PluginDispatcher and autoload modules self.plugin_dispatcher = PluginDispatcher(self, config.PATHS['plugins']) self.plugin_dispatcher.load_plugins(config.PLUGINS) self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
class PealBot(object): """ :author: Anton Zering <*****@*****.**> :author: Jules Held <*****@*****.**> :copyright: 2012, Anton Zering :license: Apache License, Version 2.0 """ __version__ = "PealBot v1.02 [2013-28-01] // @nemesis" def __init__(self, config): """ Constructor for the bot :param host: The server address as tuple of (HOST, PORT) :param nick_name: Nickname for the bot. :param real_name: Real name of the bot. It not provided, the nick is taken as real name. """ self.config = config self.host = config.HOST self.user_name = config.IDENTITY['username'] self.nick_name = config.IDENTITY['nick'] self.real_name = config.IDENTITY['realname'] self.real_name = self.real_name or self.nick_name self.channels = {} self.bus = Bus() # initialize PluginDispatcher and autoload modules self.plugin_dispatcher = PluginDispatcher(self, config.PATHS['plugins']) self.plugin_dispatcher.load_plugins(config.PLUGINS) self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) def debug(self, text): print "-- " + text def connect(self, host=None): """ Connects to the server :param host: The server address as tuple of (HOST, PORT). """ if not host: host = self.host self.debug("Connecting to " + str(host)) # connect to server socket self.sock.connect(self.host) # set default nick name self.nick(self.nick_name) # set default user and real name self.send("USER %s 0 0 :%s" % (self.user_name, self.real_name)) def disconnect(self): """Disconnects from the server if already connected.""" if not self.sock: return self.debug("Disconnecting from " + str(self.host)) self.sock.close() def send(self, msg): """ Sends raw messages to the server. :param msg: Message to be sent. :return: success of the send operation """ self.debug("<< " + msg) return self.sock.sendall(msg + "\r\n") def quit(self, quit_reason=""): """ Sends a QUIT command to the server. Then disconnects. :param quit_msg: Quit message. """ self.bus.publish("plugins.before_quit") self.send('QUIT %s' % quit_reason) self.disconnect() def join(self, channels): """ Sends a JOIN message to the server to joins one or multiple channels :param channels: One or more channels separated by whitespaces. """ return self.send('JOIN %s' % channels) def part(self, channels, part_reason=None): """ Sends the PART message to the searver to leave one or multiple channels :param channels: One or more channels separated by whitespaces. :param part_msg: Part message to appear in the channel """ return self.send('PART %s :%s' % (channels, part_reason)) def nick(self, new_nick): """ Sends the NICK message to the server to try a nickname change of the bot :param new_nick: The new nick """ return self.send("NICK %s" % new_nick) def notice(self, target, text): """ Sends a NOTICE message to a reciever :param target: Reciever of the message :param msg: Message to be sent """ return self.send("NOTICE %s :%s" % (target, text)) def msg(self, target, text): """ Sends a PRIVMSG message to a channel or another client :param target: Reciever of the message. Channel or a nick name. :param msg: Message to be sent """ return self.send("PRIVMSG %s :%s" % (target, text)) def msgStuff(self, text): """ Sends a all stuff :param text: commadns&stuff """ return self.send("PRIVMSG %s" % text) def opMsg(self, text): """ Sends a MODE message :param text: Mode to set """ return self.send("MODE %s" % text) def exit(self, exit_code=0): self.bus.publish("plugins.before_unload") sys.exit(exit_code) def start(self): # connect to server self.connect() # TODO: move to plugin! # Authenticate at Nickserv: # self.auth(self.config['password']) while True: try: recv = self.sock.recv(2048) # print recv except: self.exit(0) for line in recv.split("\r\n"): # ignore empty lines if not line: continue # handle irc commands msg = IRCMessage(line) # handle PING message if msg.cmd == 'ping': self.send("PONG :%s" % msg.text) else: self.plugin_dispatcher.handle_irc(msg)