Advertisement
swarley

Orchid2

Sep 16th, 2011
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.55 KB | None | 0 0
  1. class IRCd
  2.  
  3.     # Create a new instance
  4.     def initialize
  5.         @conf   = Conf.new.to_conf
  6.         @server = TCPServer.new(@conf[:server][:port])
  7.         @servers = ServerList.new
  8.         @users   = UserList.new
  9.         @modules = ModuleList.new
  10.         @modules.use(@conf.to_mods)
  11.     end
  12.  
  13.     # Gather the entries in @servers and @users in an array
  14.     def collab(with_serv=nil)
  15.         k = @servers.to_a + @users.to_a
  16.         if with_serv == 0 || with_serv == nil
  17.             return k
  18.         elsif with_serv == 1
  19.             return k + @server
  20.         end
  21.     end
  22.  
  23.      def conf
  24.          return @conf
  25.      end
  26.      
  27.     # IRCd Mainloop
  28.     def main
  29.         begin
  30.             ready = select(collab(1), nil, nil, 1)
  31.             unless ready == nil
  32.                 # Handle what needs to be recieved
  33.                 ready[0][0].each do |client|
  34.                     case client.class
  35.                         when Link
  36.                             @modules.fire(:listen, [client])
  37.                         when User
  38.                             @modules.fire(:parse, [client])
  39.                         when IRCd
  40.                             @modules.fire(:new_user, [])
  41.                     end
  42.                 end
  43.             end
  44.             # Ping clients if needed
  45.             ping_needed = collab().select {|x| x.needs_ping}
  46.             unless ping_needed == nil
  47.                 ping_needed.each do |client|
  48.                     if client.class == Link
  49.                         @modules.fire(:server_ping, [client])
  50.                     else
  51.                         @modules.fire(:client_ping, [client])
  52.                     end
  53.                 end
  54.             end
  55.             # Kill clients who don't return their ping
  56.             ping_timeout = collab().select {|x| x.timeout == true}
  57.             return if ping_timeout == nil
  58.             ping_timeout.each do |client|
  59.                 case client.class
  60.                     when Link
  61.                         @modules.fire(:squit, [client, @conf[:links][:timeout_format]])
  62.                     when User
  63.                         @modules.fire(:quit, [client, @conf[:users][:timeout_format]])
  64.                 end
  65.             end
  66.         # Save from errors
  67.         rescue Exception => e
  68.             if @conf[:logging][:debug] == true
  69.                 File.new("logs/debug.log", "a+").puts(e.traceback.join("\n"))
  70.             end
  71.             if @conf[:debug][:out] == true
  72.                 puts e.backtrace.join("\n")
  73.             end
  74.             retry unless @conf[:debug][:all_fatal] == true
  75.             raise "Error considered fatal.\n #{e.backtrace.join("\n")}"
  76.         end
  77.     end
  78.  
  79. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement