grider.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-"
  3. """
  4. This file is part of the UFONet project, https://ufonet.03c8.net
  5. Copyright (c) 2013/2020 | psy <epsylon@riseup.net>
  6. You should have received a copy of the GNU General Public License along
  7. with UFONet; if not, write to the Free Software Foundation, Inc., 51
  8. Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  9. """
  10. import socket, re, time, string, sys, os, traceback
  11. from threading import *
  12. class Paster(Thread):
  13. def __init__(self, parent):
  14. Thread.__init__(self)
  15. self.parent = parent
  16. self.active = False
  17. self.sock = None
  18. self.clean = False
  19. def run( self ):
  20. conn = None
  21. addr = None
  22. self.sock = self.parent.try_bind(9992)
  23. if self.sock is not None:
  24. self.sock.listen(1)
  25. print('[Info] [AI] Clean on port 9992')
  26. self.clean = True
  27. else:
  28. print('[Error] [AI] No paste on port 9992')
  29. while self.clean:
  30. try:
  31. conn,addr = self.sock.accept()
  32. print('[Info] [AI] Got copy from', addr)
  33. except socket.timeout:
  34. pass
  35. except socket.error as e:
  36. if self.clean == False:
  37. print("[Error] [AI] Socket Error /return : "+str(e))
  38. return
  39. else:
  40. print("[Error] [AI] Socket Error /break : "+str(e))
  41. break
  42. else:
  43. data = conn.recv(4096)
  44. if data :
  45. l=len(data)
  46. print("[Info] [AI] Received data : "+data+"/"+str(l)+"/"+str(data.find("\n")))
  47. if data.find('\n')==-1 and data.find('\r')==-1:
  48. if data.find("#?#")!=-1:
  49. print("[Info] [AI] Adding to grid")
  50. fc=open(self.parent.target_dir+"grid.txt","a")
  51. fc.write(data+"\n")
  52. fc.close()
  53. elif data.find("#!#")!=-1:
  54. print("[Info] [AI] Adding to board")
  55. fc=open(self.parent.target_dir+"board.txt","a")
  56. fc.write(data+"\n")
  57. fc.close()
  58. elif data.find("#-#")!=-1:
  59. print("[Info] [AI] Adding to wargames")
  60. fc=open(self.parent.target_dir+"wargames.txt","a")
  61. fc.write(data+"\n")
  62. fc.close()
  63. conn.close()
  64. print('[Info] [AI] Done!!!')
  65. self.sock.close()
  66. class Grider ( Thread ):
  67. def __init__(self):
  68. Thread.__init__( self )
  69. self.daemon = True
  70. self.awake = True
  71. self.tmp_dir = "/tmp/"
  72. self.target_dir = '/var/www/ufonet/'
  73. self.blackray = None
  74. self.absorber = None
  75. self.computer = None
  76. def dream(self):
  77. if not os.path.exists(self.target_dir+"grid.txt"):
  78. grid_fail = 0
  79. try:
  80. fc = open(self.target_dir+'grid.txt', 'wb')
  81. fc.close()
  82. except:
  83. print("[Error] [AI] No 'grid.txt' file in "+self.target_dir)
  84. grid_fail = grid_fail + 1
  85. else:
  86. grid_fail = 0
  87. if not os.path.exists(self.target_dir+"board.txt"):
  88. board_fail = 0
  89. try:
  90. fc = open(self.target_dir+'board.txt', 'wb')
  91. fc.close()
  92. except:
  93. print("[Error] [AI] No 'board.txt' file in "+self.target_dir)
  94. board_fail = board_fail + 1
  95. else:
  96. board_fail = 0
  97. if not os.path.exists(self.target_dir+"wargames.txt"):
  98. wargames_fail = 0
  99. try:
  100. fc = open(self.target_dir+'wargames.txt', 'wb')
  101. fc.close()
  102. except:
  103. print("[Error] [AI] No 'wargames.txt' file in "+self.target_dir)
  104. wargames_fail = wargames_fail + 1
  105. else:
  106. wargames_fail = 0
  107. if not os.access(self.target_dir+"grid.txt",os.W_OK):
  108. print("[Error] [AI] Write access denied for grid file in "+self.target_dir)
  109. grid_fail = grid_fail + 1
  110. if not os.access(self.target_dir+"board.txt",os.W_OK):
  111. print("[Error] [AI] Write access denied for board file in "+self.target_dir)
  112. board_fail = board_fail + 1
  113. if not os.access(self.target_dir+"wargames.txt",os.W_OK):
  114. print("[Error] [AI] Write access denied for wargames file in "+self.target_dir)
  115. wargames_fail = wargames_fail + 1
  116. if grid_fail > 0 and board_fail > 0 and wargames_fail > 0:
  117. print("\n[Error] [AI] 'Grid', 'board' and 'wargames' are unuseable... -> [Aborting!]")
  118. print("\n[Info] [AI] Suspend [Grider] with: Ctrl+z")
  119. sys.exit(2)
  120. self.paster = Paster(self)
  121. self.awake = False
  122. print("[Info] [AI] [Grider] Having sweet dreams...")
  123. def try_bind(self, port):
  124. s=None
  125. try:
  126. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  127. s.settimeout(10)
  128. s.bind(('', port))
  129. except socket.error as e:
  130. if e.errno == 98: # if is in use wait a bit and retry
  131. time.sleep(3)
  132. return self.try_bind(port)
  133. print("[Error] [AI] [Grider] Socket busy, connection failed on port " + str(port))
  134. return s
  135. def run(self):
  136. self.dream()
  137. try:
  138. self.paster.start()
  139. if self.paster.clean:
  140. print("[Info] [AI] [Grider] Advancing time in another space (waiting for server)"+os.linesep)
  141. time.sleep(1)
  142. while self.paster.clean:
  143. print("[Info] [AI] [Grider] Advancing time in another space (waiting for server)"+os.linesep)
  144. time.sleep(1)
  145. print("\n[Info] [AI] [Grider] Sheets are all up and ready...")
  146. while self.paster.clean:
  147. time.sleep(1)
  148. except:
  149. traceback.print_exc()
  150. self.cut()
  151. print("[Info] [AI] [Grider] Finished!!!")
  152. def cut(self):
  153. self.paster.clean=False
  154. self.paster.join()
  155. if __name__ == "__main__":
  156. try:
  157. print("\n[Info] [AI] Initiating copy/paste functions ...\n")
  158. print('='*22 + '\n')
  159. app = Grider()
  160. app.start()
  161. while True: time.sleep(1)
  162. except KeyboardInterrupt:
  163. print("\n[Info] [AI] Terminating copy/paste functions...\n")
  164. app.cut()
  165. except Exception as e:
  166. traceback.print_exc()
  167. print ("\n[Error] [AI] Something wrong trying to copy/paste to the 'grid'... -> [Passing!]\n")