import socket
import time

UpArrow    = '\x1b0A'
DownArrow  = '\x1b0B'
RightArrow = '\x1b0C'
LeftArrow  = '\x1b0D'

class PTTBot:
   def __init__(self):
      self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
      self.socket.settimeout(60)

      self.leaveCount = 0

      self.debug = 1
   def login(self, account, password):
      self.socket.connect(('ptt.cc',23))
      self.recvMsg()

      self.sendMsg(account + '\r' + password + '\r')
      self.recvMsg()

      # Jump to main menu
      self.sendMsg('\r')
      self.recvMsg()

   def sendMsg(self, message):
      if self.debug is 1:
         print message
      self.socket.send(message)

   def recvMsg(self):
      if self.debug is 1:
         time.sleep(3)
      else:
         time.sleep(1)

      msg = self.socket.recv(65535).decode('big5', 'ignore')

      if self.debug is 1:
         self.showScreen(msg)

      return msg

   def leave(self):

      # If there is a search, we need more left arrow to leave
      for i in range(0, self.leaveCount):
         self.sendMsg(LeftArrow)
         self.recvMsg()

      # left arrow
      self.sendMsg(LeftArrow)
      self.recvMsg()

      self.sendMsg('q')
      self.recvMsg()

      self.sendMsg(LeftArrow)
      self.recvMsg()

      self.sendMsg('\r')
      self.recvMsg()

      self.sendMsg('y')
      self.recvMsg()

      self.sendMsg('\r')
      self.recvMsg()

      self.socket.close()
   def goBoard(self, board):
      self.sendMsg('s')
      self.recvMsg()

      self.sendMsg(board + '\r')
      self.recvMsg()
   def search(self, rule):
      # Fix, we need skip animation
      self.sendMsg('Z')
      self.recvMsg()

      self.sendMsg('Z')
      self.recvMsg()

      self.sendMsg(rule + '\r')
      self.recvMsg()

      self.leaveCount = self.leaveCount + 1

#   def backup(self):
#
   def showScreen(self, message):
      print message

@@ -18,16 +18,30 @@
 ort time
+%0Aimport select
 %0A%0AUpArro
@@ -822,44 +822,132 @@
 (3)%0A
+%0A
       
-else:%0A         time.sleep(1)%0A%0A
+timeout_in_seconds = 1%0A      ready = select.select(%5Bself.socket%5D, %5B%5D, %5B%5D, timeout_in_seconds)%0A%0A      if ready%5B0%5D:%0A   
     
@@ -1003,17 +1003,19 @@
 gnore')%0A
-%0A
+   
       if
@@ -1037,24 +1037,27 @@
 1:%0A         
+   
 self.showScr
@@ -1065,17 +1065,19 @@
 en(msg)%0A
-%0A
+   
       re

add receive time out