Permalink
Browse files

Checking in initial work on the refactorization

  • Loading branch information...
1 parent 3593ac0 commit 3bebd62e03b56a6ff42c8073afb2943f988ece2f @Sutto committed Nov 4, 2008
View
44 ircd.rb
@@ -1,44 +0,0 @@
-#!/usr/bin/env ruby
-require 'rubygems'
-require 'eventmachine'
-require 'thread'
-
-DIR = File.dirname(__FILE__)
-%w(irc_replies net_utils message_server synchronized_store default_stores irc_channel irc_client irc_server).each do |f|
- require File.join(f)
-end
-
-include IRCReplies
-
-$config ||= {}
-$config['version'] = '0.1'
-$config['timeout'] = 10
-$config['port'] = 6667
-$config['hostname'] = Socket.gethostname.split(/\./).shift
-$config['starttime'] = Time.now.to_s
-$config['nick-tries'] = 5
-
-$verbose = ARGV.shift || false
-
-CHANNEL = /^[#\$&]+/
-PREFIX = /^:[^ ]+ +(.+)$/
-
-if __FILE__ == $0
- begin
- $verbose ||= !ARGV.detect { |c| c =~ /-v/ }.nil?
- trap("INT") do
- $message_server.dump
- system("kill -9 #{$$}")
- end
-
- MessageServer.start
-
- EventMachine::run do
- EventMachine::add_periodic_timer(300) { IRCServer.ping_all }
- EventMachine::start_server "0.0.0.0", 6667, IRCServer
- end
- rescue Exception => e
- class P; include NetUtils; end
- P.new.carp e
- end
-end
File renamed without changes.
View
@@ -0,0 +1,41 @@
+class GlobalChannel
+ include NetUtils
+
+ def privatemsg(msg, client)
+ # Broadcast to all users
+ carp "#{msg} from #{client}"
+ $user_store.each_user do |user|
+ user.reply :privmsg, client.userprefix, "#all", msg
+ end
+ end
+
+ def notice(msg, client)
+ $user_store.each_user {|user|
+ user.reply :notice, client.userprefix, @name, msg
+ }
+ end
+
+ def each_user(&blk)
+ $user_store.each_user(&blk)
+ end
+
+ def name; "#all"; end
+
+ def nicks; $user_store.nicks + ["Steve"]; end
+
+ def topic
+ "All that is posted on Kookaburra"
+ end
+
+ def join(client); true; end
+
+ def part(client, msg); true; end
+
+ def is_member?(user); true; end
+ alias has_nick? is_member?
+
+ def mode(u)
+ u == "Steve" ? "@" : " "
+ end
+
+end
@@ -9,7 +9,7 @@ def initialize(name)
@topic = "There is no topic"
@name = name
@oper = []
- carp "create channel:#{@name}"
+ carp "create channel: #{@name}"
end
def add(client)
@@ -56,6 +56,7 @@ def privatemsg(msg, client)
each_user {|user|
user.reply :privmsg, client.userprefix, @name, msg if user != client
}
+ $channel_store["#all"].privatemsg "#{msg} - from #{@name}", client
end
def notice(msg, client)
@@ -77,7 +77,7 @@ def handle_nick(s)
unless $user_store[s].closed?
reply :numeric, ERR_NICKNAMEINUSE,"* #{s} ","Nickname is already in use."
@nick_tries += 1
- if @nick_tries > $config['nick-tries']
+ if @nick_tries > Kookaburra.max_nick_tries
carp "kicking spurious user #{s} after #{@nick_tries} tries"
handle_abort
end
@@ -106,9 +106,9 @@ def mode
def handle_newconnect(nick)
@alive = true
@nick = nick
- @host = $config['hostname']
- @ver = $config['version']
- @starttime = $config['starttime']
+ @host = Kookaburra.host_name
+ @ver = Kookaburra.version
+ @starttime = Kookaburra.started_at
send_welcome if !@user.nil?
end
@@ -120,6 +120,8 @@ def send_welcome
repl_myinfo
repl_motd
repl_mode
+ # Force the user to join #all
+ reply :join, @usermsg, "#all"
@welcomed = true
end
end
@@ -163,11 +165,11 @@ def repl_nowaway()
end
def repl_motd()
- reply :numeric, RPL_MOTDSTART,'', "- Message of the Day"
+ reply :numeric, RPL_MOTDSTART,'', "MOTD"
File.read("motd.txt").split("\n").each do |l|
- reply :numeric, RPL_MOTD,'', "- #{l}"
+ reply :numeric, RPL_MOTD,'', l
end
- reply :numeric, RPL_ENDOFMOTD,'', "- End of /MOTD command."
+ reply :numeric, RPL_ENDOFMOTD,'', "End of /MOTD command."
end
def repl_mode()
@@ -187,7 +189,7 @@ def send_notonchannel(channel)
def send_topic(channel)
if $channel_store[channel]
- reply :numeric, RPL_TOPIC,channel, "#{$channel_store[channel].topic}"
+ reply :numeric, RPL_TOPIC,channel, $channel_store[channel].topic.to_s
else
send_notonchannel channel
end
@@ -212,7 +214,7 @@ def send_nameslist(channel)
end
def send_ping()
- reply :ping, "#{$config['hostname']}"
+ reply :ping, Kookaburra.host_name
end
def handle_join(channels)
@@ -264,7 +266,7 @@ def handle_privmsg(target, msg)
end
end
begin
- #$message_server.append_message(self.nick, target.strip, msg, viewed)
+ $message_server.append_message(self.nick, target.strip, msg, viewed)
rescue Exception => e
carp e
end
@@ -384,15 +386,15 @@ def handle_who(mask, rest)
#match against all users
$user_store.each_user {|user|
reply :numeric, RPL_WHOREPLY ,
- "#{user.channels[0]} #{user.userprefix} #{user.host} #{$config['hostname']} #{user.nick} H" ,
+ "#{user.channels[0]} #{user.userprefix} #{user.host} #{Kookaburra.host_name} #{user.nick} H" ,
"#{hopcount} #{user.realname}" if File.fnmatch?(mask, "#{user.host}.#{user.realname}.#{user.nick}")
}
reply :numeric, RPL_ENDOFWHO, mask, "End of /WHO list."
else
#get all users in the channel
channel.each_user {|user|
reply :numeric, RPL_WHOREPLY ,
- "#{mask} #{user.userprefix} #{user.host} #{$config['hostname']} #{user.nick} H" ,
+ "#{mask} #{user.userprefix} #{user.host} #{Kookaburra.host_name} #{user.nick} H" ,
"#{hopcount} #{user.realname}"
}
reply :numeric, RPL_ENDOFWHO, mask, "End of /WHO list."
@@ -420,7 +422,7 @@ def handle_abort()
end
def handle_version()
- reply :numeric, RPL_VERSION,"#{$config['version']} Ruby IRCD", ""
+ reply :numeric, RPL_VERSION, "v#{Kookaburra.version} Kookaburra", ""
end
def handle_eval(s)
@@ -433,7 +435,7 @@ def handle_unknown(s)
end
def handle_connect
- reply :raw, "NOTICE AUTH :#{$config['version']} initialized, welcome."
+ reply :raw, "NOTICE AUTH :Kookaburra v#{Kookaburra.version} initialized, welcome."
end
def reply(method, *args)
@@ -477,14 +479,14 @@ def reply(method, *args)
raw "#{@usermsg} MODE #{nick} :#{rest}"
when :numeric
numeric,msg,detail = args
- server = $config['hostname']
+ server = Kookaburra.host_name
raw ":#{server} #{'%03d'%numeric} #{@nick} #{msg} :#{detail}"
end
end
def raw(arg, abrt=false)
begin
- #carp "--> #{arg}"
+ carp "--> #{arg}"
@server.send_data arg.chomp + "\r\n" if !arg.nil?
rescue Exception => e
carp "<#{self.userprefix}>#{e.message}"
View
@@ -0,0 +1,63 @@
+# Logger partially based on Merb logger
+class IRCLogger
+
+ LEVELS = {
+ :fatal => 7,
+ :error => 6,
+ :warn => 4,
+ :info => 3,
+ :debug => 0
+ }
+
+ COLOURS = {
+ :fatal => 31, # red
+ :error => 33, # yellow
+ :warn => 35, # magenta
+ :info => 32, # green
+ :debug => 34 # white
+ }
+
+ attr_accessor :level, :file, :verbose
+
+ def initialize(level = :info, verbose = false)
+ self.level = level.to_sym
+ self.verbose = verbose
+ self.file = File.open(File.join(Kookaburra.root, "log/ircd.log"), "a+")
+ end
+
+ def close!
+ self.file.close
+ end
+
+ LEVELS.each do |name, value|
+
+ define_method(name) do |message|
+ write "[#{name.to_s.upcase}] #{message}", name if LEVELS[self.level] <= value
+ end
+
+ define_method(:"#{name}?") do
+ LEVELS[self.level] <= value
+ end
+ end
+
+ def debug_exception(exception)
+
+ debug "Exception: #{exception}"
+ exception.backtrace.each do |l|
+ debug ">> #{l}"
+ end
+
+ end
+
+ private
+
+ def write(message, level = self.level)
+ self.file.puts message
+ STDOUT.puts colourize(message, level) if self.verbose
+ end
+
+ def colourize(message, level)
+ "\033[1;#{COLOURS[level]}m#{message}\033[0m"
+ end
+
+end
File renamed without changes.
File renamed without changes.
View
@@ -0,0 +1,17 @@
+#!/usr/bin/env ruby
+require 'rubygems'
+require 'eventmachine'
+require 'thread'
+
+DIR = File.dirname(__FILE__)
+%w(irc_replies net_utils message_server synchronized_store default_stores
+ irc_channel irc_client irc_server global_channel irc_logger kookaburra).each do |f|
+ require File.join(DIR, f)
+end
+
+$channel_store["#all"] = GlobalChannel.new
+
+include IRCReplies
+
+CHANNEL = /^[#\$&]+/
+PREFIX = /^:[^ ]+ +(.+)$/
View
@@ -0,0 +1,69 @@
+class Kookaburra
+ class << self
+
+ def version; "0.1"; end
+
+ def logger
+ @@logger ||= setup_logger!
+ end
+
+ def verbose?
+ @@verbose ||= false
+ end
+
+ def verbose=(value)
+ @@verbose = !!value
+ end
+
+ def root
+ @@root || File.expand_path(File.join(File.dirname(__FILE__), ".."))
+ end
+
+ def root=(path)
+ @@root = path
+ end
+
+ def host_name
+ @@host_name ||= Socket.gethostname.split(/\./).shift
+ end
+
+ def started_at; @@started_at ||= Time.now.to_s; end
+
+ def max_nick_tries; 5; end
+
+ # Initialization
+
+ def setup_logger!
+ log_level = :debug
+ if ARGV.detect { |c| c.to_s =~ /^(\-\-log\-level(.*))/i }
+ cmd = $1
+ if cmd.include?("=")
+ log_level = cmd.split("=", 2)[1].to_sym
+ else
+ log_level = ARGV[ARGV.index(cmd) + 1].to_sym
+ end
+ end
+ @@logger ||= IRCLogger.new((log_level || :debug).to_sym, self.verbose?)
+ end
+
+ def setup_traps!
+ trap("INT") do
+ $message_server.dump
+ system("kill -9 #{$$}")
+ end
+ end
+
+ def setup_verbosity!
+ self.verbose = !ARGV.detect { |c| c =~ /-v/ }.nil? || false
+ end
+
+ def setup!
+ @@started_at = Time.now.to_s
+ setup_verbosity!
+ setup_logger!
+ setup_traps!
+ end
+
+
+ end
+end
Oops, something went wrong.

0 comments on commit 3bebd62

Please sign in to comment.