#!/usr/bin/ruby # vim: expandtab tabstop=2 shiftwidth=2 softtabstop=2 autoindent: require 'rubygems' require 'digest/md5' STDOUT.sync = true $role_receives = ["ASSIGN_RE", "UNASSIGN_RE", "PING_RE", "ASSIGNMENTS_RE", "REQ_BC", "BC_RE"] def put_debug(msg) if $debug == 1 puts msg end end def put_log(msg) puts "Z #{Time.now.strftime("%Y-%m-%d %H:%M:%S %z")}: #{msg}" end def allowed(inputcmd) $role_receives.each{ |c| #print "A #{c} B #{inputcmd}\n" if inputcmd =~ /^#{c}/ return true end } return false end def put_to(to, cmd, payload) if to && cmd && payload puts "to:#{to}: #{cmd} #{payload}" $s.puts "to:#{to}: #{cmd} #{payload}" end end def put_not_to(to, cmd, payload) puts "!to:#{to}: #{cmd} #{payload}" $s.puts "!to:#{to}: #{cmd} #{payload}" end def work(line) #put_log "GOT LINE: #{line}" fromto = "" from = "" cmd = "" payload = "" if line =~ /^\d+-\d+-\d+ \d+:\d+:\d+ \+\d+: (.+)$/ line = $1 if line =~ /^(from|to):([a-zA-Z\._]+): (.+)/ fromto = $1 from = $2 line = $3 end if line =~ /^([A-Z_]+)/ cmd = $1 end if line =~ /^[A-Z_]+ (.+)/ payload = $1 end if allowed(cmd) # an allowed command with a seemingly valid payload has been received put_log "from: #{from}" put_log "cmd: #{cmd}" put_log "line: #{line}" put_log "payload #{payload}" if cmd == "REQ_BC" if payload =~ /^(.+),(.+),(.+),'(.+)','(.+)'$/ # n f s ni txt # $& # 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. network = $1 freqname = $2 source = $3 nickname = $4 text = $5 # we can fiddle with source here! bcid = Digest::MD5.hexdigest("%d %s %s %s %s %s" % [Time.now.utc.to_i, network, freqname, source, nickname, text]) # so it only reaches the issuer of REQ_BC put_to(from, "BC_ID", "#{bcid} for: #{network},#{freqname},#{source}") #$broadcasts[bcid] = from finalmessage = "%s %s,%s,%s,'%s','%s'" % [bcid, network, freqname, source, nickname, text] put_not_to(from, "BC", finalmessage) end # of match not nil end # of REQ_BC else # of allowed cmd - means .. we don't handle these: put_log "'#{line}'" end end end def consuming() put_log "SYS Starting reading thread\n" loop do begin line = $s.gets.chomp work(line) rescue end end end def giving inputmessage = "" while inputmessage != "quit" inputmessage = "" inputmessage = STDIN.gets.chomp # put to ssh $s.puts inputmessage end end def main raise "ARGV[0] must be valid ssh commands with the uri (user@host) in the beginning!" unless ARGV[0] =~ /^.+@.+$/ sshcmd = "%s" % [ARGV[0]] $s = IO.popen("ssh #{sshcmd}", "r+") thread_consuming = Thread.new {consuming} giving end main =begin #client sending message and awaiting replies #stomp.subscribe("/temp-queue/random_replies") #stomp.publish("/queue/random_generator", "random", {"reply-to" => "/temp-queue/random_replies"}) #Timeout::timeout(2) do # msg = stomp.receive # puts "Got the response: #{msg.body}" #end # #stomp.publish(msg.headers["reply-to"], number) # # =end