cs_connector.rb 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. #!/usr/bin/ruby
  2. # vim: expandtab tabstop=2 shiftwidth=2 softtabstop=2 autoindent:
  3. require 'rubygems'
  4. require 'stomp'
  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.11big_yadda"
  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. def put_debug(msg)
  40. if $debug == 1
  41. puts msg
  42. end
  43. end
  44. def put_log(msg)
  45. puts "#{Time.now.strftime("%Y-%m-%d %H:%M:%S %z")}: #{msg}"
  46. end
  47. def my_subscribe(client, topic)
  48. put_debug "Subscribing to #{topic}"
  49. client.subscribe("/topic/#{topic}")
  50. end
  51. # check if current text is readable for user
  52. def check_allow_processing(text, user)
  53. ok = false
  54. if not text =~ /^from:#{user}:/
  55. put_debug "may be a command: #{text}"
  56. ok = true
  57. end
  58. if text =~ /^!to:#{user}:/
  59. put_debug "not for #{user} #{text}"
  60. ok = false
  61. elsif text =~ /^to:#{user}:/
  62. put_debug "it is for #{user} #{text}"
  63. ok = true
  64. end
  65. return ok
  66. end
  67. # consuming process with a loop
  68. def consuming()
  69. put_debug "SYS Starting consuming/reading thread\n"
  70. client = Stomp::Connection.new("", "", "localhost", 61613, true)
  71. user = $user
  72. my_subscribe(client, "messages")
  73. my_subscribe(client, "#{user}-replies")
  74. # for each of our own roles, we subscribe
  75. $my_roles.each {
  76. |role|
  77. my_subscribe(client, "#{role}")
  78. }
  79. loop do
  80. begin
  81. msg = client.receive
  82. if check_allow_processing(msg.body, $user)
  83. #put_debug "msg.headers #{msg.headers["reply-to"]}\n"
  84. #put_debug "msg_array #{msg_array}\n"
  85. #put_log "RECEIVED_DBG: #{msg.body}"
  86. message = msg.body
  87. #from:#{$user}: #{cmd} #{payload}
  88. #put_log "message #{message}"
  89. message =~ /^([!fromto]+):([a-zA-Z\._]+): (.+)$/
  90. if not $& == nil
  91. m_sentmode = $1
  92. m_user = $2
  93. message = $3
  94. #put_log "sentmode #{m_sentmode}"
  95. #put_log "m_user #{m_user}"
  96. #put_log "message #{message}"
  97. end
  98. message =~ /^([A-Z_]+) (.*)/
  99. if not $& == nil
  100. m_cmd = $1
  101. m_pload = $2
  102. end
  103. if m_cmd =~ /^JOIN$/
  104. put_log "SYS User '#{m_user}' just joined the party."
  105. elsif m_cmd =~ /^C$/
  106. put_log "C #{m_user}: #{m_pload}"
  107. elsif m_cmd =~ /^WHO$/
  108. reply_to_msg(msg, "WHO_RE", "'#{$user}' on '#{$version}' ROLES: #{$user_roles[$user].join(", ")}")
  109. elsif m_cmd =~ /^PART$/
  110. put_log "PARTED User '#{m_user}' just left the party."
  111. elsif m_cmd =~ /^REQ_BC$/
  112. # this is for central_brain to process... so central needs to know from who.
  113. put_log "from:#{m_user}: #{m_cmd} #{m_pload}"
  114. else
  115. # general output of received commands on stomp on this connection (ssh)
  116. put_log "#{m_cmd} #{m_pload}"
  117. if m_cmd =~ /^BC$/
  118. #sending a fake reply.
  119. if user == "paul_dev_eggdrop"
  120. put_log "debug: sending fake reply based on #{msg.headers["reply-to"]}"
  121. m_pload =~ /^(.+?) /
  122. bcid = $1
  123. reply_to_msg(msg, "BC_RE", "#{bcid} Squirrels=2,Nuts=5")
  124. end
  125. end
  126. end
  127. else # of check_allow_processing
  128. put_debug "RECEIVED from myself. Ignoring."
  129. end # of check_allow_processing
  130. rescue
  131. put_debug "hum?"
  132. retry
  133. end
  134. end
  135. end
  136. def send(path, inputmsg, *header)
  137. begin
  138. finalmessage = ""
  139. Timeout::timeout(2) do
  140. stomp = Stomp::Client.new("", "", "localhost", 61613)
  141. finalmessage = "%s" % [inputmsg]
  142. if header != ""
  143. stomp.publish(path, finalmessage, *header)
  144. else
  145. stomp.publish(path, finalmessage)
  146. end
  147. stomp.close
  148. end
  149. rescue Timeout::Error
  150. put_debug "Failed to send within the 2 second timeout"
  151. exit 1
  152. else
  153. put_debug "SENT to #{path}: #{finalmessage}"
  154. end
  155. end
  156. def allowed_cmd(inputmessage)
  157. $my_cmds.each{|c|
  158. #print "A #{c} B #{inputmessage}\n"
  159. if inputmessage =~ /^#{c}/
  160. return true
  161. end
  162. }
  163. return false
  164. end
  165. def find_role_by_cmd(cmd)
  166. $my_roles.each {
  167. |role|
  168. #print "wah: #{role}"
  169. $role_commands[role].each {
  170. |c|
  171. if c == cmd
  172. #print "wOOh: #{role}"
  173. return role
  174. end
  175. }
  176. }
  177. return false
  178. end
  179. def find_dest_by_role(role)
  180. $role_dialogs.each {
  181. |srcrole, dstrole|
  182. if srcrole == role
  183. return dstrole
  184. end
  185. }
  186. return false
  187. end
  188. def send_cmd(cmd, payload, system=false, sendmode="from", to_user=$user)
  189. #find role by cmd
  190. #$role_commands.each
  191. #$my_cmds = $my_roles.collect {|v| $role_commands[v]}
  192. if system
  193. role = $default_role
  194. else
  195. role = find_role_by_cmd(cmd)
  196. end
  197. dest = find_dest_by_role(role)
  198. put_debug "cmd: #{cmd}"
  199. put_debug "role: #{role}"
  200. put_debug "dest: #{dest}"
  201. put_debug "payload: #{payload}"
  202. if sendmode == "from"
  203. send("/topic/#{dest}", "from:#{to_user}: #{cmd} #{payload}","reply-to" => "/topic/#{$user}-replies")
  204. elsif sendmode == "to" && to_user != ""
  205. dest = "#{to_user}-replies"
  206. send("/topic/#{dest}", "#{cmd} #{payload}","reply-to" => "/topic/#{$user}-replies")
  207. elsif sendmode == "!to" && to_user != ""
  208. send("/topic/#{dest}", "!to:#{to_user}: #{cmd} #{payload}","reply-to" => "/topic/#{$user}-replies")
  209. end
  210. end
  211. def reply_to_msg(msg, cmd, payload)
  212. if msg.headers["reply-to"]
  213. send(msg.headers["reply-to"], "from:#{$user}: #{cmd} #{payload}")
  214. end
  215. end
  216. def main
  217. raise "ARGV[0] must be a userstring - no numbers!" unless ARGV[0] =~ /^[a-zA-Z\._]+$/
  218. $user = "%s" % [ARGV[0]]
  219. if not $user_roles.any? {|k, v| k.include? $user}
  220. put_log "SYS User unknown to me."
  221. exit
  222. end
  223. $my_roles = $user_roles[$user]
  224. $my_cmds = $my_roles.collect {|v| $role_commands[v]}.flatten
  225. $broadcasts = Hash.new
  226. put_log "HELLO Hi user '#{$user}'! How are you? I'm #{$version}"
  227. put_log "ROLES #{$my_roles.join(", ")}"
  228. put_log "COMMANDS #{$my_cmds.join(", ")}"
  229. send_cmd("JOIN", "Hi, I'm all in.", true)
  230. thread_consuming = Thread.new {consuming}
  231. #put_log "SYS Going into input loop"
  232. inputmessage = ""
  233. while inputmessage != "PART"
  234. inputmessage = ""
  235. cmd = ""
  236. payload = ""
  237. sendmode = ""
  238. to_user = ""
  239. inputmessage = STDIN.gets.chomp
  240. if check_allow_processing(inputmessage, $user)
  241. if inputmessage =~ /^([!]?to):([a-zA-Z_\.]+): (.+)/
  242. sendmode = $1
  243. to_user = $2
  244. inputmessage = $3
  245. put_debug "sendmode: #{sendmode}"
  246. put_debug "to_user: #{to_user}"
  247. end
  248. if inputmessage =~ /^([A-Z_]+)/
  249. cmd = $1
  250. end
  251. # now we have the cmd .. or not ;)
  252. if inputmessage =~ /^[A-Z_]+ (.+)/
  253. payload = $1
  254. end
  255. # now we can use payload
  256. if allowed_cmd(cmd)
  257. if cmd == "PING"
  258. put_log "PONG"
  259. elsif cmd == "REQ_BC"
  260. # help with format .. but send the raw payload
  261. if payload =~ /^(.+),(.+),(.+),'(.+)','(.+)'$/
  262. # n f s ni txt
  263. error = 0
  264. # $&
  265. # 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
  266. network = $1
  267. freqname = $2
  268. source = $3
  269. nickname = $4
  270. text = $5
  271. if not network =~ /^(QWalt)|(QDEV)/
  272. put_log "SYS Network name #{network} is unknown: QWalt or QDEV allowed."
  273. error = 1
  274. end
  275. if not freqname =~ /^(-qw-)|(-spam-)|(-dev-)/
  276. put_log "SYS Frequency name is unknown #{freqname}"
  277. error = 1
  278. end
  279. if not source =~ /^(#.+)|(qw:\/\/)|(http:\/\/)/
  280. put_log "SYS Source string is not in the form ^(#.+)|(qw:\/\/)|(http:\/\/) was: #{source}"
  281. error = 1
  282. end
  283. if not nickname =~ /^.+/
  284. put_log "SYS Nickname string is not in the form ^.+ #{nickname}"
  285. error = 1
  286. end
  287. if not text =~ /^.+/
  288. put_log "SYS Message string is not in the form ^.+ #{text}"
  289. error = 1
  290. end
  291. else # of check syntax
  292. put_log "SYS Command format is REQ_BC <network>,<frequency>,<source/channel>,'<nickname>','<message>'"
  293. error = 1
  294. end
  295. if error == 0
  296. payload = "%s,%s,%s,'%s','%s'" % [network, freqname, source, nickname, text]
  297. send_cmd(cmd, payload)
  298. # send REQ_BC to the corresponding role.
  299. end
  300. elsif cmd == "BC_RE"
  301. if payload =~ /^(.+) (.+)=(\d+),(.+)=(\d+)$/
  302. bcid = $1
  303. user_string = $2
  304. user_count = $3
  305. item_string = $4
  306. item_count = $5
  307. payload = "%s %s=%d,%s=%d" % [bcid, user_string, user_count, item_string, item_count]
  308. # according bcid it is possible to get the originating user.. so send only to his topic!
  309. if $broadcasts[bcid]
  310. to_user = $broadcasts[bcid]
  311. put_log "SYS Broadcast reply bcid: #{bcid} underway. To: '#{to_user}'."
  312. send("/topic/#{to_user}-replies", "#{cmd} #{payload}")
  313. #send_cmd(cmd, payload) .. and thus: central_brain has to collect the BC_REs
  314. else
  315. put_log "SYS Broadcast reply bcid: #{bcid} underway."
  316. send("/topic/broadcast", "#{cmd} #{payload}")
  317. #send_cmd(cmd, payload) .. and thus: central_brain has to collect the BC_REs
  318. end
  319. else
  320. put_log "SYS Format is BC_RE <bcid> <userstring>=<usercount>,<itemstring>=<itemcount>"
  321. end
  322. elsif cmd == "C"
  323. if payload =~ /^(.+)$/
  324. put_log "C #{$user}: #{$1}"
  325. send_cmd("C", "#{$1}")
  326. else
  327. put_log "SYS Format is C <chattext>"
  328. end
  329. elsif cmd == "WHO"
  330. put_log "SYS Asking who's here."
  331. send_cmd(cmd, "is here?")
  332. elsif cmd == "PART"
  333. put_log "SYS Goodbye '#{$user}'."
  334. send_cmd(cmd, "The party was fun.")
  335. else
  336. # universal sender for other allowed commands.
  337. if sendmode && to_user
  338. put_debug "sendmode: #{sendmode} to_user: #{to_user}"
  339. send_cmd(cmd, payload, false, sendmode, to_user)
  340. else
  341. send_cmd(cmd, payload)
  342. end
  343. end # of if command == xyz
  344. else # of if allowed command
  345. put_log "SYS Command #{inputmessage} not allowed, your commands are: #{$my_cmds.join(", ")}."
  346. end # of if allowed command
  347. end # of if we should process it
  348. end # of while input != "part"
  349. end # of main
  350. main