Advertisement
swarley

OrchidIRC mainloop

Sep 10th, 2011
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.42 KB | None | 0 0
  1. #!/usr/bin/ruby1.9.1
  2. #Main.rb
  3. #Main portion of the Orchid::IRC module, contains the server's core as well as some base modules, however, any module defined here can be overridden in the
  4. #module API. This product is protected by use un the BSD terms of use Clase
  5.  
  6. require 'socket'
  7. require 'resolv'
  8. require 'digest'
  9. load './lib/rehash.rb'
  10. $loads = ['numeric','privmsg','user','quit','channel','config','join','init','motd','server_notice','part','hash_changes', 'ping', 'names', 'who', 'unix_time', 'topic', 'nick', 'parse', 'new_client', 'load_mod', 'mode']
  11. $loads.push('resolv_host')
  12. $loads.each do |loadz|
  13.   load './lib/' + loadz + '.rb'
  14.   puts "Loaded module: #{'./lib/' + loadz + '.rb'}"
  15. end
  16.  
  17.  
  18. class IRC
  19.     def initialize
  20.       @config = config('./config/ircd.json')
  21.       @s_conf = @config["server"]
  22.       @u_conf = @config["user"]
  23.       @c_conf = @config["channel"]
  24.       @server        = {:socket => TCPServer.open(@s_conf["port"]), :name => @s_conf["s_name"], :port => @s_conf["port"], :id => @s_conf["sid"], :net_name =>@s_conf["name"]}
  25.       $server_name   = @server[:name]
  26.       @clients       = [@server]
  27.       @servers       = []
  28.       @modes         = []
  29.       @nicks         = []
  30.       @channels      = []
  31.       @modules       = ['load_mod', 'nick', 'ping', 'privmsg', 'user', 'join', 'quit', 'mode', 'names', 'topic', 'who', 'part', 'rehash']
  32.       @channel_names = []
  33.     end
  34.    
  35.     def panic
  36.       begin
  37.           file = File.open('clients.log', 'a+')
  38.           file.puts 'RAW HASH'
  39.           file.puts @clients
  40.           file.puts 'EOFS'
  41.           l = []
  42.           m = @clients
  43.           m.each do |n|
  44.              puts n
  45.              unless n == @server
  46.              $current =  n
  47.              l.push m[:socket].eof.to_s
  48.              end
  49.           end
  50.           file.puts l
  51.           file.close
  52.       rescue Exception=>detail
  53.          puts detail
  54.          puts $current
  55.          $problem = $current
  56.          okie()
  57.       end
  58.     end
  59.    
  60.     def okie
  61.       begin
  62.          $problem[:socket].eof
  63.       rescue Exception
  64.          puts "this may fix teh problem ^._.^"
  65.       end
  66.     end
  67.    
  68.     def main_loop
  69.       print 'Orchid> '
  70.         loop do
  71.             k = select(@clients + [$stdin], nil, nil, nil)
  72.             unless k == nil
  73.                 k[0].each do |client|
  74.                   client[:idle] = unix_time unless client == $stdin
  75.                     if client == @server
  76.                         @clients.push(new_client(@server[:socket].accept))
  77.                         init(@clients.last)
  78.                     elsif client != $stdin
  79.                       if client[:socket].eof != true
  80.                          begin
  81.                             parse(client)
  82.                          rescue Exception => detail
  83.                             puts detail
  84.                          end
  85.                       else
  86.                          quit("SOCKET EOF", client)
  87.                       end
  88.                     else
  89.                       k=$stdin.gets.chomp
  90.                       begin
  91.                          case k
  92.                          when '.channels'
  93.                             p @channels
  94.                          when '.num_of_channels'
  95.                             puts @channels.length
  96.                          when '.clients'
  97.                             p @clients
  98.                          when '.num_of_clients'
  99.                             puts @clients.length
  100.                          when '.rehash'
  101.                             $loads.each do |loadz|
  102.                               load './lib/' + loadz + '.rb'
  103.                               puts "Loaded module: #{'./lib/' + loadz + '.rb'}"
  104.                             end
  105.                          when '.cmds'
  106.                             p ['.channels','.num_of_channels','.clients', '.num_of_clients', '.rehash']
  107.                          when /^\./
  108.                             puts "Unidentified command."
  109.                          else
  110.                             puts "=> #{eval(k)}"
  111.                          end
  112.                          print 'Orchid> '
  113.                       rescue Exception => detail
  114.                          puts detail.message()
  115.                          print detail.backtrace.join("\n")
  116.                          puts 'Error'
  117.                       end
  118.                     end
  119.                 end
  120.             end
  121.         end
  122.     end
  123.    
  124.     def close
  125.       @server[:socket].close
  126.     end
  127.  
  128. end
  129.  
  130. k = IRC.new
  131.  
  132. begin
  133.   k.main_loop
  134. rescue Interrupt
  135.      k.close
  136.      puts ''
  137.      puts 'Shutdown sucessful.'
  138. rescue Exception => detail
  139.      k.panic
  140.      puts detail.message()
  141.      print detail.backtrace.join("\n")
  142.      retry
  143. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement