grider.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. try:
  34. data = conn.recv(4096).decode("utf-8")
  35. except:
  36. data = None
  37. pass
  38. print ("[Info] [AI] Stream received:", repr(data))
  39. except socket.timeout:
  40. print("[Info] [AI] Socket listening...")
  41. pass
  42. except socket.error as e:
  43. if self.clean == False:
  44. print("[Error] [AI] Socket Error /return : "+str(e))
  45. return
  46. else:
  47. print("[Error] [AI] Socket Error /break : "+str(e))
  48. break
  49. else:
  50. if data:
  51. l=len(data)
  52. print("[Info] [AI] Received data...\n")
  53. if data.find('\n')==-1 and data.find('\r')==-1:
  54. if data.find("#?#")!=-1:
  55. print("[Info] [AI] Adding to grid")
  56. fc=open(self.parent.target_dir+"grid.txt","a")
  57. fc.write(data+"\n")
  58. fc.close()
  59. elif data.find("#!#")!=-1:
  60. print("[Info] [AI] Adding to board")
  61. fc=open(self.parent.target_dir+"board.txt","a")
  62. fc.write(data+"\n")
  63. fc.close()
  64. elif data.find("#-#")!=-1:
  65. print("[Info] [AI] Adding to wargames")
  66. fc=open(self.parent.target_dir+"wargames.txt","a")
  67. fc.write(data+"\n")
  68. fc.close()
  69. elif data.find("#L#")!=-1:
  70. print("[Info] [AI] Adding to links")
  71. fc=open(self.parent.target_dir+"links.txt","a")
  72. fc.write(data+"\n")
  73. fc.close()
  74. elif data.find("#S#")!=-1:
  75. print("[Info] [AI] Adding to streams")
  76. fc=open(self.parent.target_dir+"streams.txt","a")
  77. fc.write(data+"\n")
  78. fc.close()
  79. elif data.find("#$#")!=-1:
  80. print("[Info] [AI] Adding to globalnet")
  81. fc=open(self.parent.target_dir+"globalnet.txt","a")
  82. fc.write(data+"\n")
  83. fc.close()
  84. else:
  85. print("[Error] [AI] Unknown data...")
  86. conn.close()
  87. print('[Info] [AI] Done!!!')
  88. self.sock.close()
  89. class Grider ( Thread ):
  90. def __init__(self):
  91. Thread.__init__( self )
  92. self.daemon = True
  93. self.awake = True
  94. self.tmp_dir = "/tmp/"
  95. self.target_dir = '/var/www/ufonet/'
  96. self.blackray = None
  97. self.absorber = None
  98. self.computer = None
  99. def dream(self):
  100. if not os.path.exists(self.target_dir+"grid.txt"):
  101. grid_fail = 0
  102. try:
  103. fc = open(self.target_dir+'grid.txt', 'wb')
  104. fc.close()
  105. except:
  106. print("[Error] [AI] No 'grid.txt' file in "+self.target_dir)
  107. grid_fail = grid_fail + 1
  108. else:
  109. grid_fail = 0
  110. if not os.path.exists(self.target_dir+"board.txt"):
  111. board_fail = 0
  112. try:
  113. fc = open(self.target_dir+'board.txt', 'wb')
  114. fc.close()
  115. except:
  116. print("[Error] [AI] No 'board.txt' file in "+self.target_dir)
  117. board_fail = board_fail + 1
  118. else:
  119. board_fail = 0
  120. if not os.path.exists(self.target_dir+"wargames.txt"):
  121. wargames_fail = 0
  122. try:
  123. fc = open(self.target_dir+'wargames.txt', 'wb')
  124. fc.close()
  125. except:
  126. print("[Error] [AI] No 'wargames.txt' file in "+self.target_dir)
  127. wargames_fail = wargames_fail + 1
  128. else:
  129. wargames_fail = 0
  130. if not os.path.exists(self.target_dir+"links.txt"):
  131. links_fail = 0
  132. try:
  133. fc = open(self.target_dir+'links.txt', 'wb')
  134. fc.close()
  135. except:
  136. print("[Error] [AI] No 'links.txt' file in "+self.target_dir)
  137. links_fail = links_fail + 1
  138. else:
  139. links_fail = 0
  140. if not os.path.exists(self.target_dir+"streams.txt"):
  141. streams_fail = 0
  142. try:
  143. fc = open(self.target_dir+'streams.txt', 'wb')
  144. fc.close()
  145. except:
  146. print("[Error] [AI] No 'streams.txt' file in "+self.target_dir)
  147. streams_fail = streams_fail + 1
  148. else:
  149. streams_fail = 0
  150. if not os.path.exists(self.target_dir+"globalnet.txt"):
  151. globalnet_fail = 0
  152. try:
  153. fc = open(self.target_dir+'globalnet.txt', 'wb')
  154. fc.close()
  155. except:
  156. print("[Error] [AI] No 'globalnet.txt' file in "+self.target_dir)
  157. globalnet_fail = globalnet_fail + 1
  158. else:
  159. globalnet_fail = 0
  160. if not os.access(self.target_dir+"grid.txt",os.W_OK):
  161. print("[Error] [AI] Write access denied for grid file in "+self.target_dir)
  162. grid_fail = grid_fail + 1
  163. if not os.access(self.target_dir+"board.txt",os.W_OK):
  164. print("[Error] [AI] Write access denied for board file in "+self.target_dir)
  165. board_fail = board_fail + 1
  166. if not os.access(self.target_dir+"wargames.txt",os.W_OK):
  167. print("[Error] [AI] Write access denied for wargames file in "+self.target_dir)
  168. wargames_fail = wargames_fail + 1
  169. if not os.access(self.target_dir+"links.txt",os.W_OK):
  170. print("[Error] [AI] Write access denied for links file in "+self.target_dir)
  171. links_fail = links_fail + 1
  172. if not os.access(self.target_dir+"streams.txt",os.W_OK):
  173. print("[Error] [AI] Write access denied for streams file in "+self.target_dir)
  174. streams_fail = streams_fail + 1
  175. if not os.access(self.target_dir+"globalnet.txt",os.W_OK):
  176. print("[Error] [AI] Write access denied for globalnet file in "+self.target_dir)
  177. globalnet_fail = globalnet_fail + 1
  178. if grid_fail > 0 and board_fail > 0 and wargames_fail > 0 and links_fail > 0 and streams_fail > 0 and globalnet_fail > 0:
  179. print("\n[Error] [AI] 'Grid', 'board', 'wargames', 'links', 'streams' and 'globalnet' are unuseable... -> [Aborting!]")
  180. print("\n[Info] [AI] Suspend [Grider] with: Ctrl+z")
  181. sys.exit(2)
  182. self.paster = Paster(self)
  183. self.awake = False
  184. print("[Info] [AI] [Grider] Having sweet dreams...")
  185. def try_bind(self, port):
  186. s=None
  187. try:
  188. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  189. s.settimeout(30)
  190. s.bind(('', port))
  191. except socket.error as e:
  192. if e.errno == 98: # if is in use wait a bit and retry
  193. time.sleep(3)
  194. return self.try_bind(port)
  195. print("[Error] [AI] [Grider] Socket busy, connection failed on port " + str(port))
  196. return s
  197. def run(self):
  198. self.dream()
  199. try:
  200. self.paster.start()
  201. if self.paster.clean:
  202. print("[Info] [AI] [Grider] Advancing time in another space (waiting for server)"+os.linesep)
  203. time.sleep(1)
  204. while self.paster.clean:
  205. print("[Info] [AI] [Grider] Advancing time in another space (waiting for server)"+os.linesep)
  206. time.sleep(1)
  207. print("\n[Info] [AI] [Grider] Sheets are all up and ready...")
  208. while self.paster.clean:
  209. time.sleep(1)
  210. except:
  211. traceback.print_exc()
  212. self.cut()
  213. print("[Info] [AI] [Grider] Finished!!!")
  214. def cut(self):
  215. self.paster.clean=False
  216. self.paster.join()
  217. if __name__ == "__main__":
  218. try:
  219. print("\n[Info] [AI] Initiating copy/paste functions ...\n")
  220. print('='*22 + '\n')
  221. app = Grider()
  222. app.start()
  223. while True: time.sleep(1)
  224. except KeyboardInterrupt:
  225. print("\n[Info] [AI] Terminating copy/paste functions...\n")
  226. app.cut()
  227. except Exception as e:
  228. traceback.print_exc()
  229. print ("\n[Error] [AI] Something wrong trying to copy/paste to the 'grid'... -> [Passing!]\n")