#todo: search in s for esc sequences #todo: enable blinking #todo: decrement x and y locations in gotoxy for down, right, and H until the result is different from current #todo: scrolling region #todo: esc c #todo: esc [@ #todo: esc [E #todo: esc [L #todo: esc [M #todo: esc [P #esc [Z #e[?6h #e[=255h #e[?6l #e[z #todo: ctrl keys #todo: delete, insert #10 goes down, 13 starts at 0 import telnetlib, WConio, sys, time, random sh, sw = WConio.gettextinfo()[7:9] ansiesc = False ansiesclb = False ansistring = "" ansiattr = 7 ansist=0 ansisb=sh ansicx, ansicy = 0, 0 ansicolor = [0, 4, 2, 6, 1, 5, 3, 7] watchlist = [] backbuffsize = 1000 def addonce(pattern, action, tag): global watchlist watchlist.append((pattern, 1, action, tag, 0)) def addind(pattern, action, tag): global watchlist watchlist.append((pattern, 0, action, tag, 0)) def rem(tag): global watchlist del watchlist[map(lambda x: x[3], watchlist).index(tag)] c = telnetlib.Telnet() c.open("gcomm.com") WConio.cputs("Connected.\n") buffer="" trans = [75,77,72,80,71,79,119,32,59,60,61,61,84,85,86,87,52,53] seq = '        OP OQ Ow Ox  '.split() def doansi(char): global ansiesc, ansiesclb, ansistring, ansiattr, ansist, ansisb, ansicx, ansicy if char==" ": WConio.textattr(7) WConio.clrscr() WConio.textattr(ansiattr) ansistring="" ansiesc=False ansiesclb=False return if ansiesclb: if char in "0123456789;?": ansistring = ansistring + char return elif char=="A": if ansistring=="": mu=1 else: mu=int(ansistring) WConio.gotoxy(WConio.wherex(), max(WConio.wherey()-mu, 0)) elif char=="B": if ansistring=="": md=1 else: md=int(ansistring) WConio.gotoxy(WConio.wherex(), max(WConio.wherey()+md, sh)) elif char=="C": if ansistring=="": mr=1 else: mr=int(ansistring) WConio.gotoxy(max(WConio.wherex()+mr, sw), WConio.wherey()) elif char=="D": if ansistring=="": ml=1 else: ml=int(ansistring) WConio.gotoxy(max(WConio.wherex()-ml, 0), WConio.wherey()) elif char=="H" or char=="f": if ansistring=="": WConio.gotoxy(0,0) else: vs = map(int, ansistring.split(";")) if len(vs)==1: WConio.gotoxy(0, vs[0]-1) else: WConio.gotoxy(vs[1]-1, vs[0]-1) elif char=="J": if ansistring in "0": #gets 0 and empty WConio.clreol() WConio.puttext(0, WConio.wherey()+1, ansisb, sh, (" "+chr(ansiattr))*(sw+1)*(ansisb-WConio.wherey())) elif ansistring=="1": WConio.puttext(0,ansist,sw, WConio.wherey()-1, (" "+chr(ansiattr))*(sw+1)*(WConio.wherey()-ansist)) WConio.puttext(0,WConio.wherey(), Wconio.wherex(), WConio.wherey(), (" "+chr(ansiattr))*(Wconio.wherex()+1)) elif ansistring=="2": WConio.puttext(0, ansist, sw, ansisb, (" "+chr(ansiattr))*(ansisb-ansist+1)) elif char=="K": if ansistring in "0": WConio.clreol() elif ansistring=="1": WConio.puttext(0,WConio.wherey(), Wconio.wherex(), WConio.wherey(), (" "+chr(ansiattr))*(Wconio.wherex()+1)) elif ansistring=="2": WConio.puttext(0, WConio.wherey(), sw, WConio.wherey(), (" "+chr(ansiattr))*(sw+1)) elif char=="U": if ansistring=="": WConio.puttext(0,0, sh, sw, (" "+chr(7))*(sh+1)*(sw+1)) WConio.gotoxy(0,0) elif char=="m": if ansistring=="": ansiattr=7 WConio.textattr(ansiattr) else: for v in map(int, ansistring.split(";")): if v==0: ansiattr=7 elif v==1: ansiattr = ansiattr | 8 elif v==2: ansiattr = ansiattr & (255-8) elif v==5 or v==6: ansiattr = ansiattr | 128 elif v==7: ansiattr = (ansiattr & 128) + 7*16 elif v==8: ansiattr = (ansiattr & (128+64+32+16)) + (ansiattr & 127) / 16 elif 30<=v<=37: ansiattr = (ansiattr & (128+64+32+16+8)) + ansicolor[v-30] elif 40<=v<=47: ansiattr = (ansiattr & (15+128)) + ansicolor[v-40]*16 WConio.textattr(ansiattr) elif char=="n": if ansistring=="6": c.write("["+str(WConio.wherey()+1)+";"+str(WConio.wherex()+1)+"R") elif ansistring=="255": c.write("["+str(sh+1)+";"+str(sw+1)+"R") elif char=="r": vs = ansistring.split(";") if vs==[""]: ansist=0 ansisb=sh elif len(vs)==1: ansist=int(*vs)+1 else: ansist, ansisb = int(vs[0])-1, int(vs[1])-1 elif char=="s": ansicx=WConio.wherex() ansicy=WConio.wherey() elif char=="u": WConio.gotoxy(ansicx, ansicy) elif char=="": ansistring="" ansiesclb=False return ansistring="" ansiesclb=False ansiesc=False elif ansiesc: if char=="[": ansiesclb=True ansiesc=False elif char=="D": WConio.puttext(0, ansist, sw, ansisb, WConio.gettext(0, ansist+1, sw, ansisb) + (" "+chr(ansiattr))*(sw+1)) elif char=="M": WConio.puttext(0, ansist, sw, ansisb, (" "+chr(ansiattr))*(sw+1)+WConio.gettext(0, ansist, sw, ansisb-1)) elif char=="": pass else: ansiesc=False else: if char=="": ansiesc=True else: WConio.cputs(char) #nt = time.time()+45 while 1: # if time.time()>=nt: # nt=nt+45 # c.write(random.choice(open("words.txt","r").readlines())) try: s = c.read_eager() except EOFError: print sys.exc_value break buffer=buffer+s[-(backbuffsize+len(s)):] for char in s: doansi(char) while WConio.kbhit(): char = WConio.getch() if char[0]==0 or char[0]==224: char = WConio.getch()[0] if char in trans: char = seq[trans.index(char)] else: char = chr(0)+chr(char) else: char = char[1] c.write(char)