""" RBhandler.py (C)2008 Don A. Hanlen I make no claims, and take no reponsibility, for use or abuse of this code! I require those who DO use it to cite its source! If this code is used for commercial purposes I expect a (negotiable) cut! This file is an adjunct for rePYbot(c) written for Python 1.5.2 (OWT's v.) It handles i/o with FIBS Ausilliary file: FIBStn15.py """ import FIBStn15 from string import split, find, lower import time True = 1==1 False = not True ## from the CLIP at FIBS.com _who = '5' _endwho = '6' _tell = '12' _shout = '13' _yousay = '16' LAG = 30 ## maximum block in seconds waiting for FIBS ## (tell, whois, query) class RBhandler: """ interface with FIBStn15 """ def __init__(self, uID, uPword, uClient, printer, master): """ connect to FIBS and initialize the user dictionary printer options for diagnostics are: 'console', '' """ self.printer = printer self.requestQ = [] self.shoutQ = [] self.master = master self.uDict = {} FIBStn15.connect(uID, uPword, uClient) self.__initDict__() def __initDict__(self): """ initialize the user dictionary this might block on input if rawwho doesn't return an _endwho or if 'set sortwho' fails ## self.uDict={[str:name: [str:nAME, str:ratings, str:experience]]} """ status = 'who0' while True: t = FIBStn15.getline() ts = split(t) self.output(" initDict:%s" %t[:68]) if len(ts): if status=='who0' and ts[0]==_endwho: self.output(" initDict: *** self.uDict.keys=%3d %s" %( len(self.uDict.keys()), status)) status = 'rating' FIBStn15.q("set sortwho rating") elif status=='rating' and ts[0]=='Value': ## Value of 'sortwho' set to rating self.output(" initDict: *** self.uDict.keys=%3d %s" %( len(self.uDict.keys()), status)) status = 'who1' FIBStn15.q("rawwho") elif status=='who1' and ts[0]==_endwho: self.output(" initDict: *** self.uDict.keys()=%3d %s" %( len(self.uDict.keys()), status)) status = 'rrating' FIBStn15.q("set sortwho rrating") elif status=='rrating' and ts[0]=='Value': ## Value of 'sortwho' set to rating self.output(" initDict: *** self.uDict.keys=%3d %s" %( len(self.uDict.keys()), status)) status = 'who2' FIBStn15.q("rawwho") elif status=='who2' and ts[0]==_endwho: self.output(" initDict: *** self.uDict.keys=%3d %s" %( len(self.uDict.keys()), status)) return else: self.processLine(ts) def getRequest(self): """ return ['name', 'word'(, ...)] tell from FIBS | False guaranteed len()>=2 """ if self.requestQ: return self.requestQ.pop(0) t = FIBStn15.getline() ts = split(t) if len(ts) and ts[0]!=_who and ts[0]!=_endwho: self.output("getRequest:%s" %t[:68]) if len(ts)>2 and ts[0]==_tell: return ts[1:] if len(ts): return self.processLine(ts) return False def getShout(self): """ return ['name', 'word'(, ...)] shout from FIBS | False guaranteed len()>=2 """ if self.shoutQ: return self.shoutQ.pop(0) return False def tell(self, user, message): """ tell user message return "done told" whether user's logged in or not! "lag" if FIBS is not responding """ FIBStn15.q("tell %s %s" %(user, message)) lagTimer = time.time() while True: if time.time()-lagTimer > LAG: return "lag" t = FIBStn15.getline() ts = split(t) self.output(" tell:%s" %t[:68]) if len(ts)>2 and (ts[0]==_yousay or ts[0]=='**'): return "done told" elif len(ts): self.processLine(ts) def whois(self, name): """ return ['nAME', 'ratings', 'experience'] else ['name', "No information found on user"] "lag" if FIBS is not responding """ if self.uDict.has_key(lower(name)): return self.uDict[lower(name)] FIBStn15.q("whois %s" %name) lagTimer = time.time() while True: if time.time()-lagTimer > LAG: return "lag" t = FIBStn15.getline() ts = split(t) self.output(" whois:%s" %t[:68]) if find(t, "No info")==0: ## No information found on user user. return([name, "No information found on user "]) elif find(t, "Informa")==0: ## Information about user: nAME = ts[2][:-1] elif find(t, " Rating")==0: ## Rating: rating Experience: experience self.uDict[lower(name)] = [nAME, ts[1], ts[3]] return self.uDict[lower(name)] elif len(ts): self.processLine(ts) def query(self, name1, name2): """ return int:time in seconds of match between players TimeBot will return 99999 for anything else! else return "no TimeBot" "lag" if FIBS is not responding """ FIBStn15.q("tell TimeBot query %s %s" %(name1, name2)) lagTimer = time.time() while True: if time.time()-lagTimer > LAG: return "lag" t = FIBStn15.getline() ts = split(t) self.output(" query:%s" %t[:68]) if find(t, '**')==0: ## ['**', 'There', 'is', 'no', 'one', 'called', 'TimeBot'] return "no TimeBot" elif len(ts)==3 and ts[1]=='TimeBot': ## ['12', 'TimeBot', '99999'] return int(ts[2]) elif len(ts): self.processLine(ts) def processLine(self, ts): """ methods that do not use lines from FIBS fall through to this """ if ts[0]==_who: ## ['5', 'don', '-', '-', '0', '0', '1757.10', '55347', ...] self.uDict[lower(ts[1])] = [ts[1], ts[6], ts[7]] elif len(ts)>2 and ts[0]==_tell: if ts[1]==self.master: self.requestQ.insert(0, ts[1:]) else: self.requestQ.append(ts[1:]) elif len(ts)>2 and ts[0]==_shout: self.shoutQ.append(ts[1:]) return False def output(self, line): """ diagnostic output can go to: 'console' stdout fileTag file with CR '' nowhere """ if self.printer=='console': print line elif self.printer: self.printer.write(line + '\n') self.printer.flush() def logout(self): """ send an exit command to FIBS """ FIBStn15.q('exit') def command(self, command): """ send command to FIBS """ FIBStn15.q(command) def whoami(self): """ return str:printer(setting) requestQ[size] and uDict[size] """ return("printer:(%s) self.requestQ[%d] self.uDict[%d]" %( self.printer, len(self.requestQ), len(self.uDict)))