sockcentral.rb 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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.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 displaying(client, user)
  40. #client.puts "#{user} connected."
  41. begin
  42. loop do
  43. while line = $display_queue[user][0]
  44. client.puts line
  45. $display_queue[user].delete_at(0)
  46. end
  47. sleep 0.1
  48. end
  49. rescue Exception => e
  50. puts e.message
  51. puts e.backtrace.inspect
  52. end
  53. end
  54. def write_user(user, input)
  55. if online_users.user(user)
  56. sometime = "#{Time.now.strftime("%Y-%m-%d %H:%M:%S %z")}"
  57. line = "#{sometime}: #{input}"
  58. $display_queue[user].push(line)
  59. end
  60. end
  61. def inputting(user, input)
  62. write_user(user, "You said: #{input}")
  63. if input =~ /^w (.+)/
  64. input = $1
  65. write_user("paul_dev_eggdrop", "he said: #{input}")
  66. end
  67. end
  68. def kill_conn(c, user, t_display)
  69. sleep 0.5
  70. # delete the key out off $display_queue
  71. $display_queue.delete(user)
  72. # shut down displaying thread and connection
  73. t_display.exit
  74. # shut down connection
  75. c.close
  76. end
  77. def online_users
  78. def user(user)
  79. if online_users.include? user
  80. return true
  81. else
  82. return false
  83. end
  84. end
  85. ousers = Array.new
  86. $display_queue.each_key {|k| ousers.push(k)}
  87. return ousers
  88. end
  89. def write_role(role, input)
  90. #find users with role
  91. users = Array.new
  92. $user_roles.each {|k, v|
  93. if v.include? role
  94. users.push(k)
  95. end
  96. }
  97. # find users that are online and inside Array "users"
  98. lala = Array.new
  99. users.each {|v|
  100. if $display_queue.has_key? v
  101. lala.push(v)
  102. end
  103. }
  104. # now write to them
  105. lala.each {|user|
  106. write_user(user, input)
  107. }
  108. end
  109. def thinking(c, user, t_display)
  110. put_log "#{user} connected. Online now: #{online_users.join(", ")}"
  111. if not $user_roles.any? {|k, v| k.include? user}
  112. put_log "SYS User '#{user}' unknown to me. Saying bye."
  113. write_user(user, "SYS User '#{user}' unknown to me. Bye.")
  114. return false
  115. end
  116. my_roles = $user_roles[user]
  117. my_cmds = my_roles.collect {|v| $role_commands[v]}.flatten
  118. write_user(user, "HELLO Hi user '#{user}'! How are you? I'm #{$version}")
  119. write_user(user, "ROLES #{my_roles.join(", ")}")
  120. write_user(user, "COMMANDS #{my_cmds.join(", ")}")
  121. write_user(user, "SYS User '#{user}' just joined the party.")
  122. write_role("broadcast", "to the broadcasting role")
  123. # reading the client input ... to write it somewhere, possibly to the client himself.
  124. begin
  125. while input = c.gets.chomp
  126. inputting(user, input)
  127. end
  128. rescue Exception => e
  129. #puts e.message
  130. #puts e.backtrace.inspect
  131. put_log "#{user} lost connection."
  132. end
  133. end
  134. def spawn_server
  135. $display_queue = Hash.new
  136. server = TCPServer.new 7337
  137. Thread.abort_on_exception = true
  138. loop do
  139. Thread.start(server.accept) do |c|
  140. user = c.gets.chomp
  141. # only one connection from a user allowed - we don't handle multiple display_queues for one user!!!!!11111
  142. if online_users.user(user)
  143. c.close
  144. else
  145. $display_queue[user] = Array.new
  146. #p $display_queue # ok we can take $display_queue to check for online users.
  147. # this thread reads what others want you to read ..
  148. t_user_display = Thread.new{ displaying(c, user) }
  149. begin
  150. execution = thinking(c, user, t_user_display)
  151. if not execution
  152. # shut down displaying thread and connection
  153. kill_conn(c, user, t_user_display)
  154. end
  155. rescue Exception => e
  156. puts e.message
  157. puts e.backtrace.inspect
  158. kill_conn(c, user, t_user_display)
  159. end # of rescue
  160. put_log "Online now: #{online_users.join(", ")}"
  161. end
  162. end
  163. end
  164. end
  165. def put_debug(msg)
  166. if $debug == 1
  167. puts msg
  168. end
  169. end
  170. def put_log(msg)
  171. puts "#{Time.now.strftime("%Y-%m-%d %H:%M:%S %z")}: #{msg}"
  172. end
  173. spawn_server