def handle_line(self, data): # print 'line |%s|' % data if data[0] == ':': n = data.find(' ') if n == -1: return # bogus prefix = data[1:n] data = data[n+1:] n = prefix.find('!') if n != -1: prefix = prefix[:n] else: prefix = '' n = data.find(' :') if n == -1: args = data.split(' ') else: args = data[:n].split(' ') args.append(data[n+2:]) try: getattr(self, 'irc_'+args[0],self.unknown)(prefix,args) except: print_traceback()
def stop_module(self, app): module = self.modules.get(app.lower()) if module: try: self.modules[app].shutdown() except : print_traceback() del self.modules[app] self.clearappwindows(app)
def queue(self, msg): # let the modules hook the queue for m in self.modules: module = self.modules[m] if hasattr(module, 'output_hook'): try: msg = module.output_hook(msg) except: print "<< output callback failure >>" print_traceback() # did a module eat the message? if msg == None: return # all messages get a serial number # msg.serial = self.serial self.serial = self.serial + 1 # Some message types should never be queued. # if msg.tag == 'show': if self.session: msg.send(self.session) return # All other messages get queued in the master queue # self.outbound.append(msg) # smsg style messages are displayed in the source # window, not the destination window # if (msg.tag == 'smsg') or (msg.tag == 'smsg/act'): win = self.getwindow(msg.app,msg.src) else: win = self.getwindow(msg.app,msg.dst) # attach the message to the queue of its window # enforce the per-window maxdepth of the queue # win.queue.append(msg) if (win.maxdepth > 0) and (len(win.queue) == win.maxdepth): old = win.queue.pop(0) self.outbound.remove(old) # If we're online, send it down # if self.session: msg.send(self.session)
def command(self, tag, dst, command): module = 'xmod_'+command try: print '<< reloading', module+',', exec "import %s" % module exec "reload(%s)" % module self.user.session.INFO(">> Module '%s' reloaded." % command) print "success >>" except: print "failed >>" print_traceback(); self.user.session.ERROR("Module '%s' failed to reload." % command) return
def loadstate(self): try: fp = open("user.%s.dat" % self.name,"r") try: p = pickle.Unpickler(fp) msgs = p.load() fp.close() for m in msgs: self.queue(m) finally: fp.close() except: print_traceback() print "<< cannot load state for '%s' >>" % self.name
def command(self, tag, dst, txt): if( tag != 'send' ): return ops = txt.split(" ") try : for i in ops : self.op(i) self.makedisplay() except "StackEmpty": self.user.queue( Msg(self.name,'fail','','','Empty stack error') ) except : print_traceback() self.user.queue( Msg(self.name,'fail','','','Unhandled exception') )
def command(self, tag, dst, command): try: print '<< reloading', command, modname = 'xmod_%s' % command modules = self.user.modules for as in modules.keys(): if modules[as].__class__.__module__ == modname: self.user.stop_module(as) if sys.modules.has_key(modname): del sys.modules[modname] module = __import__(modname, globals(), locals()) self.user.session.INFO(">> Module '%s' reloaded" % command) print "succeeded >>" except: print "failed >>" print_traceback() self.user.session.ERROR("Module '%s' failed to reload" % command) return
def command(self, tag, dst, txt): if not tag: return # make sure we're all upper case so lookups work # tag = tag.upper() # Keep track of the last context we issued a command from # if len(dst) > 0: self.lastctxt = dst # pass raw commands directly to the server # if tag[0] == '/': self.push(tag[1:] + ' ' + txt + '\n') return try: getattr(self, 'cmd_'+tag,self.badcommand)(tag, dst, txt) except: print "<< command() failure >>" print_traceback()
def getwindow(self, app, name): if not name: name = "" if not app: app = "" devicename = app+' '+name app = app.lower() win = self.windows.get(app+' '+name) if not win: # create window win = Window(name,app,devicename,self.queuelen) self.windows[app+' '+name] = win # window creation callback into application svr = self.modules.get(app.lower()) if svr and hasattr(svr, 'window_callback'): try: svr.window_callback(name,win) except: print '<< Window callback exception in %s >>' % app print_traceback() return win
def handle_error(self): print_traceback() #self.user.queue(Msg(self.name, 'fail', '', self.target, asyncore.compact_traceback())) self.user.queue(Msg(self.name, 'fail', '', self.target, 'connection failed'))
def found_terminator(self): try: self.handler.handle_line(self.buffer) except: print_traceback() self.buffer = ''
# "ircnick" : "testuser", # "name" : "guest", # "passwd" : "570a90bfbf8c7eab5dc5d4e26832d5b1" # fred # }) # try: import config for setting in ('PORT','SAVESTATE','ALLOWED_ADDRS'): if hasattr(config,setting): locals()[setting]=getattr(config,setting) except ImportError: pass print "<< %d user records >>" % len(xuser.USERS) xserver.Server('', PORT, ALLOWED_ADDRS) try: xserver.loop() except: print "<< error >>" print_traceback() if SAVESTATE: print "<< saving user state >>" for name in xuser.USERS: user = xuser.USERS[name] user.savestate() print "<< server exiting >>"
def command(self, serial, tag, dst, txt): #print 'tag: '+tag #print 'dst: '+dst #print 'txt: '+txt app = self.getappname(dst) name = self.getwinname(dst) # the following server stuff can't be eaten by modules. if tag == "clear": if name == '': # Deleting a module window self.stop_module(app) self.session.INFO(">> Module '%s' killed by clear" % app) else: win = self.checkwindow(app, name) if win: for msg in win.queue: self.outbound.remove(msg) win.queue = [] self.deletewindow(app, name) module = self.modules.get(app.lower()) if module : try: self.modules[app].command(tag, name, '') except: print_traceback() self.session.status("clear",dst,"") return if tag == "bye": self.session.disconnect() return if tag == "save": self.client_settings[dst] = txt return if tag == "query": if dst == "windows": for win in self.windows: win = self.windows[win] self.session.INFO( "window '%s' has %d messages" % (win.name,len(win.queue)) ) return # is it aimed at the server? if dst == '%server' : as = ''; # don't override module name if tag == "stop": module = self.modules.get(txt.lower()) if module: module.shutdown() del self.modules[txt] self.clearappwindows(txt) self.session.INFO(">> Module '%s' stopped" % txt) else : self.session.ERROR("Module '%s' not running" % txt) return if tag == "start": nameargs = txt.split(' ',1) if len(nameargs) > 1: tag, txt = nameargs else : tag = txt module = self.modules.get(tag.lower()) if module : self.session.ERROR("Module '%s' is already running" % txt) return if tag == "startas" : # parse arguments nameargs = txt.split(' ', 2) if len(nameargs) == 1: self.session.ERROR('Syntax: /startas <name> <module> <args>') return elif len(nameargs) == 2: as, tag = nameargs; txt = '' else: as, tag, txt = nameargs