Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class IRCd
- # Create a new instance
- def initialize
- @conf = Conf.new.to_conf
- @server = TCPServer.new(@conf[:server][:port])
- @servers = ServerList.new
- @users = UserList.new
- @modules = ModuleList.new
- @modules.use(@conf.to_mods)
- end
- # Gather the entries in @servers and @users in an array
- def collab(with_serv=nil)
- k = @servers.to_a + @users.to_a
- if with_serv == 0 || with_serv == nil
- return k
- elsif with_serv == 1
- return k + @server
- end
- end
- def conf
- return @conf
- end
- # IRCd Mainloop
- def main
- begin
- ready = select(collab(1), nil, nil, 1)
- unless ready == nil
- # Handle what needs to be recieved
- ready[0][0].each do |client|
- case client.class
- when Link
- @modules.fire(:listen, [client])
- when User
- @modules.fire(:parse, [client])
- when IRCd
- @modules.fire(:new_user, [])
- end
- end
- end
- # Ping clients if needed
- ping_needed = collab().select {|x| x.needs_ping}
- unless ping_needed == nil
- ping_needed.each do |client|
- if client.class == Link
- @modules.fire(:server_ping, [client])
- else
- @modules.fire(:client_ping, [client])
- end
- end
- end
- # Kill clients who don't return their ping
- ping_timeout = collab().select {|x| x.timeout == true}
- return if ping_timeout == nil
- ping_timeout.each do |client|
- case client.class
- when Link
- @modules.fire(:squit, [client, @conf[:links][:timeout_format]])
- when User
- @modules.fire(:quit, [client, @conf[:users][:timeout_format]])
- end
- end
- # Save from errors
- rescue Exception => e
- if @conf[:logging][:debug] == true
- File.new("logs/debug.log", "a+").puts(e.traceback.join("\n"))
- end
- if @conf[:debug][:out] == true
- puts e.backtrace.join("\n")
- end
- retry unless @conf[:debug][:all_fatal] == true
- raise "Error considered fatal.\n #{e.backtrace.join("\n")}"
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement