sockcentral.rb 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. #!/usr/bin/ruby
  2. # vim: expandtab tabstop=2 shiftwidth=2 softtabstop=2 autoindent:
  3. require 'rubygems'
  4. require 'socket'
  5. require 'digest/md5'
  6. # .. allowed commands .. ROLES = CAPABILITIES
  7. # normal users have ROLE broadcast. Roles are defined on a per-user basis .. in a config.
  8. $version = "0.15TCPServer"
  9. $debug = 0
  10. $role_commands = Hash[
  11. "everyone" => ["PING", "WHO", "C", "PART"],
  12. "broadcast_admin" => ["BC_ID", "BC", "BC_ENDCOUNT"],
  13. "broadcast" => ["REQ_BC", "BC_RE"],
  14. "specbot_admin" => ["REQ_ASSIGN", "REQ_UNASSIGN", "REQ_PING", "REQ_ASSIGNMENTS"],
  15. "specbot" => ["ASSIGN_RE", "UNASSIGN_RE", "PING_RE", "ASSIGNMENTS_RE"],
  16. ]
  17. $default_role = "everyone"
  18. # which role is talking to which role?
  19. # effectively it says: this (local) command is sent to that (remote) topic .. that certain topic is read by that user with that role.
  20. $role_dialogs = Hash[
  21. "everyone" => ["everyone"],
  22. "broadcast_admin" => ["broadcast"],
  23. "broadcast" => ["broadcast_admin"],
  24. "specbot_admin" => ["specbot"],
  25. "specbot" => ["specbot_admin"],
  26. ]
  27. $user_roles = Hash[
  28. "paul_dev_eggdrop" => ["everyone", "broadcast"],
  29. "paul_eggdrop" => ["everyone", "broadcast"],
  30. "paul_dev_specbot" => ["everyone", "broadcast", "specbot"],
  31. "paul_specbot" => ["everyone", "broadcast", "specbot"],
  32. "qw.nu" => ["everyone", "broadcast"],
  33. "qw.nu_poster" => ["everyone", "broadcast"],
  34. "mihawk_dev_specbot" => ["everyone", "broadcast", "specbot"],
  35. "mihawk_specbot" => ["everyone", "broadcast", "specbot"],
  36. "central_brain" => ["everyone", "broadcast_admin", "specbot_admin"],
  37. ]
  38. STDOUT.sync = true
  39. # display loop ... it is displaying the display_queue
  40. def displaying(client, user)
  41. #client.puts "#{user} connected."
  42. begin
  43. loop do
  44. while line = $display_queue[user][0]
  45. client.puts line
  46. $display_queue[user].delete_at(0)
  47. end
  48. sleep 0.1
  49. end
  50. rescue Exception => e
  51. puts e.message
  52. puts e.backtrace.inspect
  53. end
  54. end
  55. def write_user(user, input)
  56. if online_users.user(user)
  57. sometime = "#{Time.now.utc.strftime("%Y-%m-%d %H:%M:%S %z")}"
  58. line = "#{sometime}: #{input}"
  59. $display_queue[user].push(line)
  60. end
  61. end
  62. def kill_conn(c, user, t_display)
  63. sleep 0.5
  64. # delete the key out off $display_queue
  65. $display_queue.delete(user)
  66. # shut down displaying thread and connection
  67. t_display.exit
  68. # shut down connection
  69. c.close
  70. end
  71. def online_users
  72. def user(user)
  73. if online_users.include? user
  74. return true
  75. else
  76. return false
  77. end
  78. end
  79. ousers = Array.new
  80. $display_queue.each_key {|k| ousers.push(k)}
  81. return ousers
  82. end
  83. def write_role(role, input, *exempts)
  84. #find users with role
  85. users = Array.new
  86. $user_roles.each {|k, v|
  87. if v.include? role
  88. users.push(k)
  89. end
  90. }
  91. # find users that are online and inside Array "users"
  92. lala = Array.new
  93. users.each {|v|
  94. if $display_queue.has_key? v
  95. lala.push(v)
  96. end
  97. }
  98. # now write to them
  99. lala.each {|user|
  100. if not exempts.include? user then
  101. write_user(user, input)
  102. end
  103. }
  104. end
  105. def allowed_cmd(user, inputmessage)
  106. my_cmds(user).each{|c|
  107. #print "A #{c} B #{inputmessage}\n"
  108. if inputmessage =~ /^#{c}/
  109. return true
  110. end
  111. }
  112. return false
  113. end
  114. # what happens with what input?!
  115. def inputting(user, input)
  116. write_user(user, "SYS You typed: #{input}")
  117. if input =~ /^([A-Z_]+)/
  118. cmd = $1
  119. end
  120. # now we have the cmd .. or not ;)
  121. if input =~ /^[A-Z_]+ (.+)/
  122. payload = $1
  123. end
  124. # now we can use payload
  125. if allowed_cmd(user, cmd)
  126. ### typical system commands follow
  127. if cmd == "PING"
  128. write_user(user, "PONG")
  129. elsif cmd == "C"
  130. if payload =~ /^(.+)$/
  131. write_role($default_role, "C #{user}: #{$1}")
  132. else
  133. write_user(user, "SYS Format is C <chattext>")
  134. end
  135. elsif cmd == "WHO"
  136. write_user(user, "WHO_RE #{online_users.join(", ")}")
  137. elsif cmd == "PART"
  138. write_user(user, "SYS Goodbye '#{user}'.")
  139. write_role($default_role, "PARTED User '#{user}' just left the party.", user)
  140. return false
  141. ###now for the good stuff, broadcasting
  142. elsif cmd == "REQ_BC"
  143. # help with format .. but send the raw payload
  144. if payload =~ /^(.+),(.+),(.+),'(.+)','(.+)'$/
  145. # n f s ni txt
  146. error = 0
  147. # $&
  148. # The string matched by the last successful pattern match in this scope, or nil if the last pattern match failed. (Mnemonic: like & in some editors.) This variable is r
  149. network = $1
  150. freqname = $2
  151. source = $3
  152. nickname = $4
  153. text = $5
  154. if not network =~ /^(QWalt)|(QDEV)/
  155. write_user(user, "SYS Network name #{network} is unknown: QWalt or QDEV allowed.")
  156. error = 1
  157. end
  158. if not freqname =~ /^(-qw-)|(-spam-)|(-dev-)/
  159. write_user(user, "SYS Frequency name is unknown #{freqname}")
  160. error = 1
  161. end
  162. if not source =~ /^(#.+)|(qw:\/\/)|(http:\/\/)/
  163. write_user(user, "SYS Source string is not in the form ^(#.+)|(qw:\/\/)|(http:\/\/) was: #{source}")
  164. error = 1
  165. end
  166. if not nickname =~ /^.+/
  167. write_user(user, "SYS Nickname string is not in the form ^.+ #{nickname}")
  168. error = 1
  169. end
  170. if not text =~ /^.+/
  171. write_user(user, "SYS Message string is not in the form ^.+ #{text}")
  172. error = 1
  173. end
  174. else # of check syntax
  175. write_user(user, "SYS Command format is REQ_BC <network>,<frequency>,<source/channel>,'<nickname>','<message>'")
  176. error = 1
  177. end
  178. if error == 0
  179. # send REQ_BC to the corresponding role.
  180. bcid = Digest::MD5.hexdigest("%d %s %s %s %s %s" % [Time.now.utc.to_i, network, freqname, source, nickname, text])
  181. # so it only reaches the issuer of REQ_BC
  182. write_user(user, "BC_ID #{bcid} for: #{network},#{freqname},#{source}")
  183. $broadcasts[bcid] = user
  184. finalmessage = "BC %s %s,%s,%s,'%s','%s'" % [bcid, network, freqname, source, nickname, text]
  185. write_role("broadcast", finalmessage, user) # write to the role with the exempt of myself.
  186. end
  187. #end of REQ_BC here..
  188. elsif cmd == "BC_RE"
  189. if payload =~ /^(.+) (.+)=(\d+),(.+)=(\d+)$/
  190. bcid = $1
  191. user_string = $2
  192. user_count = $3
  193. item_string = $4
  194. item_count = $5
  195. payload = "%s %s=%d,%s=%d" % [bcid, user_string, user_count, item_string, item_count]
  196. # according bcid it is possible to get the originating user.. so send only to his topic!
  197. if $broadcasts[bcid]
  198. to_user = $broadcasts[bcid]
  199. write_user(user, "SYS Broadcast reply bcid: #{bcid} underway to #{to_user}.")
  200. write_user(to_user, "#{cmd} #{payload}")
  201. else
  202. write_user(user, "SYS Broadcast reply bcid: #{bcid} underway.")
  203. write_role("broadcast", "#{cmd} #{payload}", user)
  204. end
  205. else # of bc_re format check
  206. put_log "SYS Format is BC_RE <bcid> <userstring>=<usercount>,<itemstring>=<itemcount>"
  207. end
  208. end
  209. else # of if allowed command
  210. write_user(user, "SYS Command #{input} not allowed, your commands are: #{my_cmds(user).join(", ")}.")
  211. end # of if allowed command
  212. end
  213. def my_roles(user)
  214. return $user_roles[user]
  215. end
  216. def my_cmds(user)
  217. myroles = my_roles(user)
  218. mycmds = myroles.collect {|v| $role_commands[v]}.flatten
  219. return mycmds
  220. end
  221. def thinking(c, user, t_display)
  222. put_log "#{user} connected. Online now: #{online_users.join(", ")}"
  223. if not $user_roles.any? {|k, v| k.include? user}
  224. put_log "SYS User '#{user}' unknown to me. Saying bye."
  225. write_user(user, "SYS User '#{user}' unknown to me. Bye.")
  226. return false
  227. end
  228. write_user(user, "HELLO Hi user '#{user}'! How are you? I'm #{$version}")
  229. write_user(user, "ROLES #{my_roles(user).join(", ")}")
  230. write_user(user, "COMMANDS #{my_cmds(user).join(", ")}")
  231. write_role("everyone", "JOINED User '#{user}' just joined the party.", user)
  232. # reading the client input ... to write it somewhere, possibly to the client himself.
  233. begin
  234. input = ""
  235. while input != "PART" do
  236. input = c.gets.chomp
  237. execution = inputting(user, input)
  238. end
  239. rescue Exception => e
  240. puts e.message
  241. puts e.backtrace.inspect
  242. put_log "#{user} lost connection."
  243. end
  244. end
  245. def spawn_server
  246. $display_queue = Hash.new
  247. $broadcasts = Hash.new
  248. server = TCPServer.new 7337
  249. Thread.abort_on_exception = true
  250. loop do
  251. Thread.start(server.accept) do |c|
  252. user = c.gets.chomp
  253. # only one connection from a user allowed - we don't handle multiple display_queues for one user!!!!!11111
  254. if online_users.user(user)
  255. c.close
  256. else
  257. $display_queue[user] = Array.new
  258. #p $display_queue # ok we can take $display_queue to check for online users.
  259. # this thread reads what others want you to read ..
  260. t_user_display = Thread.new{ displaying(c, user) }
  261. begin
  262. execution = thinking(c, user, t_user_display)
  263. if not execution
  264. # shut down displaying thread and connection
  265. kill_conn(c, user, t_user_display)
  266. end
  267. rescue Exception => e
  268. puts e.message
  269. puts e.backtrace.inspect
  270. kill_conn(c, user, t_user_display)
  271. end # of rescue
  272. put_log "Online now: #{online_users.join(", ")}"
  273. end
  274. end
  275. end
  276. end
  277. def put_debug(msg)
  278. if $debug == 1
  279. puts msg
  280. end
  281. end
  282. def put_log(msg)
  283. puts "#{Time.now.strftime("%Y-%m-%d %H:%M:%S %z")}: #{msg}"
  284. end
  285. spawn_server