#!/usr/bin/env python import socket import sys import time import signal from threading import Thread from threading import Lock import fcntl import os import curses global mainwin global width global height #host="localhost" host="chat.mikekohn.net" port="6666" name="pynaken" lock=None class PyNakenThread(Thread): def __init__(self, sock): Thread.__init__(self) self.connected=1 self.buff="" self.sock=sock def readline(self): while self.connected==1: if self.buff.find("\n")!=-1: pos=self.buff.find("\n") line=self.buff[:pos].strip() self.buff=self.buff[pos+1:] return line try: self.buff+=self.sock.recv(1024) except: self.connected=0 return None return None def run(self): global lock while self.connected==1: line=self.readline() if line==None: break lock.acquire() if line=="": mainwin.addstr(line+"\n") elif line[0] == '#': mainwin.addstr(line+"\n",curses.color_pair(1)) elif line[0] == '<': mainwin.addstr(line+"\n",curses.color_pair(2)) elif line[0] == '>': mainwin.addstr(line+"\n",curses.color_pair(3)) else: mainwin.addstr(line+"\n",curses.color_pair(4)) mainwin.hline(height-5,0,"-",width) mainwin.refresh() lock.release() lock.acquire() mainwin.addstr("Thread is dead!",curses.color_pair(4)) mainwin.refresh() lock.release() sys.stdout.write("Nickname: ") sys.stdout.flush() name=sys.stdin.readline().strip() stdscr=curses.initscr() height,width=stdscr.getmaxyx() curses.noecho() #print str(height)+" "+str(width) curses.start_color() curses.init_pair(1,curses.COLOR_RED, curses.COLOR_BLACK) curses.init_pair(2,curses.COLOR_MAGENTA, curses.COLOR_BLACK) curses.init_pair(3,curses.COLOR_GREEN, curses.COLOR_BLACK) curses.init_pair(4,curses.COLOR_WHITE, curses.COLOR_BLACK) mainwin=stdscr.subwin(height-4,width,0,0) mainwin.idlok(1) mainwin.scrollok(1) inputwin=stdscr.subwin(4,80,height-4,0) inputwin.idlok(1) inputwin.scrollok(1) count=0 for arg in sys.argv: if count==1: host=str(arg) elif count==2: port=int(arg) count=count+1 lock=Lock() lock.acquire() mainwin.hline(height-5,0,"-",width) mainwin.addstr("Connecting to host "+host+" at port "+str(port)+"..."+"\n") mainwin.refresh() lock.release() try: sock=socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((host,int(port))) except socket.error, e: print "Could not connect to server..." sys.exit() naken_thread=PyNakenThread(sock) naken_thread.start() sock.send('.n '+name+'\n') sock.send('% is using PyNaken 0.90\n') line="" lock.acquire() inputwin.leaveok(1) lock.release() while naken_thread.connected==1: try: ch=stdscr.getch() except: break if ch in (curses.KEY_DC,curses.KEY_BACKSPACE,127,27): y,x=inputwin.getyx() if x==0: if y==0: continue y=y-1 x=width-1 else: x=x-1 lock.acquire() inputwin.move(y,x) inputwin.delch(y,x) inputwin.refresh() lock.release() if len(line)==0: continue line=line[0:len(line)-1] continue elif ch in (curses.KEY_ENTER,10): try: if naken_thread.connected==0: break sock.send(line.strip()+"\n") line="" lock.acquire() inputwin.erase() inputwin.refresh() lock.release() continue except socket.error, e: naken_thread.connected=0 print "Connect closed." break line=line+chr(ch) lock.acquire() inputwin.addch(ch) inputwin.refresh() lock.release() sock.close() mainwin.addstr(">> Connection lost. Press enter to exit.",curses.color_pair(1)) mainwin.refresh() curses.endwin()