central.rb 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #!/usr/bin/ruby
  2. # vim: expandtab tabstop=2 shiftwidth=2 softtabstop=2 autoindent:
  3. require 'rubygems'
  4. require 'digest/md5'
  5. require 'open3'
  6. #STDIN.sync = true
  7. STDOUT.sync = true
  8. $role_receives = ["ASSIGN_RE", "UNASSIGN_RE", "PING_RE", "ASSIGNMENTS_RE", "REQ_BC", "BC_RE"]
  9. def put_debug(msg)
  10. if $debug == 1
  11. puts msg
  12. end
  13. end
  14. def put_log(msg)
  15. puts "Z #{Time.now.strftime("%Y-%m-%d %H:%M:%S %z")}: #{msg}"
  16. end
  17. def allowed(inputcmd)
  18. $role_receives.each{
  19. |c|
  20. #print "A #{c} B #{inputcmd}\n"
  21. if inputcmd =~ /^#{c}/
  22. return true
  23. end
  24. }
  25. return false
  26. end
  27. def put_to(to, cmd, payload)
  28. if to && cmd && payload
  29. puts "to:#{to}: #{cmd} #{payload}"
  30. $s.puts "to:#{to}: #{cmd} #{payload}"
  31. end
  32. end
  33. def put_not_to(to, cmd, payload)
  34. puts "!to:#{to}: #{cmd} #{payload}"
  35. $s.puts "!to:#{to}: #{cmd} #{payload}"
  36. end
  37. def work(line)
  38. put_log "GOT LINE: #{line}"
  39. line =~ /^\d+-\d+-\d+ \d+:\d+:\d+ \+\d+: (.+)$/
  40. fromto = ""
  41. from = ""
  42. cmd = ""
  43. payload = ""
  44. if not $& == nil
  45. line = $1
  46. if line =~ /^(from|to):([a-zA-Z_]+): (.+)/
  47. fromto = $1
  48. from = $2
  49. line = $3
  50. end
  51. if line =~ /^([A-Z_]+)/
  52. cmd = $1
  53. end
  54. if allowed(cmd)
  55. put_log "from: #{from}"
  56. put_log "cmd: #{cmd}"
  57. put_log "line: #{line}"
  58. payload = ""
  59. if line =~ /^[A-Z_]+ (.+)/
  60. payload = $1
  61. end
  62. # an allowed commands with a seemingly valid payload has been received
  63. puts cmd
  64. puts payload
  65. if cmd == "REQ_BC"
  66. payload =~ /^(.+),(.+),(.+),'(.+)','(.+)'$/
  67. # n f s ni txt
  68. # $&
  69. # 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 read-only.
  70. if not $& == nil
  71. network = $1
  72. freqname = $2
  73. source = $3
  74. nickname = $4
  75. text = $5
  76. # we can fiddle with source here!
  77. bcid = Digest::MD5.hexdigest("%d %s %s %s %s %s" % [Time.now.utc.to_i, network, freqname, source, nickname, text])
  78. # so it only reaches the issuer of REQ_BC
  79. put_to(from, "BC_ID", "#{bcid} for: #{network},#{freqname},#{source}")
  80. #$broadcasts[bcid] = m_user
  81. finalmessage = "%s %s,%s,%s,'%s','%s'" % [bcid, network, freqname, source, nickname, text]
  82. put_not_to(from, "BC", finalmessage)
  83. end # of match not nil
  84. end # of REQ_BC
  85. else
  86. put_log "UH '#{line}'"
  87. end
  88. end
  89. end
  90. def consuming()
  91. put_log "SYS Starting reading thread\n"
  92. loop do
  93. begin
  94. line = $s.gets.chomp
  95. work(line)
  96. rescue
  97. end
  98. end
  99. end
  100. def giving
  101. inputmessage = ""
  102. while inputmessage != "PART"
  103. inputmessage = ""
  104. inputmessage = STDIN.gets.chomp
  105. # put to ssh
  106. $s.puts inputmessage
  107. end
  108. end
  109. def main
  110. raise "ARGV[0] must be valid ssh commands with the uri (user@host) in the beginning!" unless ARGV[0] =~ /^.+@.+\..+$/
  111. sshcmd = "%s" % [ARGV[0]]
  112. $s = IO.popen("ssh #{sshcmd}", "r+")
  113. thread_consuming = Thread.new {consuming}
  114. giving
  115. end
  116. main
  117. =begin
  118. #client sending message and awaiting replies
  119. #stomp.subscribe("/temp-queue/random_replies")
  120. #stomp.publish("/queue/random_generator", "random", {"reply-to" => "/temp-queue/random_replies"})
  121. #Timeout::timeout(2) do
  122. # msg = stomp.receive
  123. # puts "Got the response: #{msg.body}"
  124. #end
  125. #
  126. #stomp.publish(msg.headers["reply-to"], number)
  127. #
  128. #
  129. =end