def __init__(self, bot, module_name='identhost', log_level = logging.INFO): self.bot = bot self.log = logging.getLogger(u'{0}.{1}'.format(bot.log_name, module_name)) self.log.setLevel(log_level) self.irc = bot.irc self.module_name = module_name self.channels = [] self.nickmap = defaultdict(str) self.hostmap = defaultdict(str) self.channel2user = defaultdict(list) self.user2channel = defaultdict(list) self.commands = [] self.events = [ eu.event(nu.BOT_JOIN, self.user_join), eu.event(nu.BOT_PART, self.user_part), eu.event(nu.RPL_WHOREPLY, self.users_who), eu.event(nu.BOT_QUIT, self.user_quit), eu.event(nu.BOT_NICK, self.user_changed_nick), ] self.bot.add_module(module_name, self)
def __init__(self, bot, module_name='identhost', log_level=logging.INFO): self.bot = bot self.log = logging.getLogger(u'{0}.{1}'.format(bot.log_name, module_name)) self.log.setLevel(log_level) self.irc = bot.irc self.module_name = module_name self.channels = [] self.nickmap = defaultdict(str) self.hostmap = defaultdict(str) self.channel2user = defaultdict(list) self.user2channel = defaultdict(list) self.commands = [] self.events = [ eu.event(nu.BOT_JOIN, self.user_join), eu.event(nu.BOT_PART, self.user_part), eu.event(nu.RPL_WHOREPLY, self.users_who), eu.event(nu.BOT_QUIT, self.user_quit), eu.event(nu.BOT_NICK, self.user_changed_nick), ] self.bot.add_module(module_name, self)
def event(self, event_id, func): return eu.event(event_id, func)
def __init__(self, nick, network, port, max_log_len = 100, authmodule=None, ircmodule=None, db_file = "bot.db", module_name="core", log_name="core", log_level=logging.DEBUG, log_handlers = None): #register a signal handler (closes bot no matter what) signal.signal(signal.SIGINT, self.signal_handler) signal.signal(signal.SIGTERM, self.signal_handler) signal.signal(signal.SIGQUIT, self.signal_handler) self.modules = {} #set up logging stuff self.log_name = log_name self.module_name = module_name self.log = logging.getLogger(self.log_name) self.log.setLevel(log_level) #if handlers were given we need to add them if log_handlers: for handler in log_handlers: self.log.addHandler(handler) #set up network stuff #IO queues self.inq = Queue.PriorityQueue() self.outq = Queue.PriorityQueue() #Set up network class net = Network(self.inq, self.outq, self.log_name) #Dispatch the thread self.log.debug("Dispatching network thread") thread = threading.Thread(target=net.loop) thread.start() #params for connection self.nick = nick self.network = network self.port = port #network stuff done #TODO a lot of these need to be made into config options, along with most of the #kwarg params self.max_reconnects = 3 self.times_reconnected = 0 self.is_running=True #create a ref to the db connection self.db = sqlite3.connect(db_file) #irc module bootstrapped before auth and ident, as auth uses it if not ircmodule: self.irc = IRC_Wrapper(self) else: self.irc = ircmodule self.ident = IdentHost(self, log_level=log_level)#set up ident self.identcontrol = IdentControl(self) # module for controlling it #if no authmodule is passed through, use the default host/ident module if not authmodule: self.auth = IdentAuth(self) else: self.auth = authmodule #variables for determining when the bot is registered self.registered = False self.channels = [] #variables for bot functionality self.is_mute = False self.commands = [ self.command("quit", self.end_command_handler, direct=True, auth_level=20), self.command("mute", self.mute, direct=True, can_mute=False, auth_level=20), self.command(r"!syntax ?(?P<module>\S+)?", self.syntax) ] #TODO I need to catch 441 or 436 and handle changing bot name by adding #a number or an underscore #catch also a 432 which is a bad uname self.events = [ eu.event(nu.RPL_ENDOFMOTD, self.registered_event), eu.event(nu.ERR_NOMOTD, self.registered_event), eu.event(nu.BOT_ERR, self.reconnect), eu.event(nu.BOT_KILL, self.reconnect), eu.event(nu.BOT_PING, self.ping), #TODO: can get privmsg handling as an event? #self.event("PRIVMSG", self.handle_priv), ] self.timed_events = [] #send out events to connect and send USER and NICK commands self.irc.connect(self.network, self.port) self.irc.user(self.nick, "Python Robot") self.irc.nick(self.nick)
def __init__(self, inqueue, outqueue, botname, module_name="network", b_size=1024, log_level=logging.INFO): self.socket = None self.module_name = module_name self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.socket.setblocking(0) self.incomplete_buffer = "" self.buffer_size = b_size self.log = logging.getLogger(u"{0}.{1}".format(botname, module_name)) self.log.setLevel(log_level) self.is_running = True self.connected = False # priority queues with data in form of (priority, data) self.inq = inqueue self.outq = outqueue # list of our sockets self.inputs = [self.socket] self.outputs = [self.socket] # Events coming out of the network - unused for now self.in_events = [] # Event coming in from the ircbot core self.out_events = [ eu.event(nu.BOT_MSG, self.msg), eu.event(nu.BOT_MSGS_ALL, self.msgs_all), eu.event(nu.BOT_MSGS, self.msgs), eu.event(nu.BOT_MSG_ALL, self.msg_all), eu.event(nu.BOT_NOTICE, self.notice), eu.event(nu.BOT_NOTICE_ALL, self.notice_all), eu.event(nu.BOT_CONN, self.connect), eu.event(nu.BOT_USER, self.user), eu.event(nu.BOT_NICK, self.nick), eu.event(nu.BOT_JOIN_CHAN, self.join), eu.event(nu.BOT_QUIT, self.quit), eu.event(nu.BOT_KILL, self.kill), eu.event(nu.BOT_PONG, self.pong), eu.event(nu.BOT_NAMES, self.names), eu.event(nu.BOT_WHO, self.who), ] self.log.info('network initialised')