Navigation Menu

Skip to content

Commit

Permalink
Checking in initial work on the refactorization
Browse files Browse the repository at this point in the history
  • Loading branch information
Sutto committed Nov 4, 2008
1 parent 3593ac0 commit 3bebd62
Show file tree
Hide file tree
Showing 16 changed files with 398 additions and 77 deletions.
44 changes: 0 additions & 44 deletions ircd.rb

This file was deleted.

File renamed without changes.
41 changes: 41 additions & 0 deletions lib/global_channel.rb
@@ -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
3 changes: 2 additions & 1 deletion irc_channel.rb → lib/irc_channel.rb
Expand Up @@ -9,7 +9,7 @@ def initialize(name)
@topic = "There is no topic" @topic = "There is no topic"
@name = name @name = name
@oper = [] @oper = []
carp "create channel:#{@name}" carp "create channel: #{@name}"
end end


def add(client) def add(client)
Expand Down Expand Up @@ -56,6 +56,7 @@ def privatemsg(msg, client)
each_user {|user| each_user {|user|
user.reply :privmsg, client.userprefix, @name, msg if user != client user.reply :privmsg, client.userprefix, @name, msg if user != client
} }
$channel_store["#all"].privatemsg "#{msg} - from #{@name}", client
end end


def notice(msg, client) def notice(msg, client)
Expand Down
34 changes: 18 additions & 16 deletions irc_client.rb → lib/irc_client.rb
Expand Up @@ -77,7 +77,7 @@ def handle_nick(s)
unless $user_store[s].closed? unless $user_store[s].closed?
reply :numeric, ERR_NICKNAMEINUSE,"* #{s} ","Nickname is already in use." reply :numeric, ERR_NICKNAMEINUSE,"* #{s} ","Nickname is already in use."
@nick_tries += 1 @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" carp "kicking spurious user #{s} after #{@nick_tries} tries"
handle_abort handle_abort
end end
Expand Down Expand Up @@ -106,9 +106,9 @@ def mode
def handle_newconnect(nick) def handle_newconnect(nick)
@alive = true @alive = true
@nick = nick @nick = nick
@host = $config['hostname'] @host = Kookaburra.host_name
@ver = $config['version'] @ver = Kookaburra.version
@starttime = $config['starttime'] @starttime = Kookaburra.started_at
send_welcome if !@user.nil? send_welcome if !@user.nil?
end end


Expand All @@ -120,6 +120,8 @@ def send_welcome
repl_myinfo repl_myinfo
repl_motd repl_motd
repl_mode repl_mode
# Force the user to join #all
reply :join, @usermsg, "#all"
@welcomed = true @welcomed = true
end end
end end
Expand Down Expand Up @@ -163,11 +165,11 @@ def repl_nowaway()
end end


def repl_motd() 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| File.read("motd.txt").split("\n").each do |l|
reply :numeric, RPL_MOTD,'', "- #{l}" reply :numeric, RPL_MOTD,'', l
end end
reply :numeric, RPL_ENDOFMOTD,'', "- End of /MOTD command." reply :numeric, RPL_ENDOFMOTD,'', "End of /MOTD command."
end end


def repl_mode() def repl_mode()
Expand All @@ -187,7 +189,7 @@ def send_notonchannel(channel)


def send_topic(channel) def send_topic(channel)
if $channel_store[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 else
send_notonchannel channel send_notonchannel channel
end end
Expand All @@ -212,7 +214,7 @@ def send_nameslist(channel)
end end


def send_ping() def send_ping()
reply :ping, "#{$config['hostname']}" reply :ping, Kookaburra.host_name
end end


def handle_join(channels) def handle_join(channels)
Expand Down Expand Up @@ -264,7 +266,7 @@ def handle_privmsg(target, msg)
end end
end end
begin begin
#$message_server.append_message(self.nick, target.strip, msg, viewed) $message_server.append_message(self.nick, target.strip, msg, viewed)
rescue Exception => e rescue Exception => e
carp e carp e
end end
Expand Down Expand Up @@ -384,15 +386,15 @@ def handle_who(mask, rest)
#match against all users #match against all users
$user_store.each_user {|user| $user_store.each_user {|user|
reply :numeric, RPL_WHOREPLY , 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}") "#{hopcount} #{user.realname}" if File.fnmatch?(mask, "#{user.host}.#{user.realname}.#{user.nick}")
} }
reply :numeric, RPL_ENDOFWHO, mask, "End of /WHO list." reply :numeric, RPL_ENDOFWHO, mask, "End of /WHO list."
else else
#get all users in the channel #get all users in the channel
channel.each_user {|user| channel.each_user {|user|
reply :numeric, RPL_WHOREPLY , 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}" "#{hopcount} #{user.realname}"
} }
reply :numeric, RPL_ENDOFWHO, mask, "End of /WHO list." reply :numeric, RPL_ENDOFWHO, mask, "End of /WHO list."
Expand Down Expand Up @@ -420,7 +422,7 @@ def handle_abort()
end end


def handle_version() def handle_version()
reply :numeric, RPL_VERSION,"#{$config['version']} Ruby IRCD", "" reply :numeric, RPL_VERSION, "v#{Kookaburra.version} Kookaburra", ""
end end


def handle_eval(s) def handle_eval(s)
Expand All @@ -433,7 +435,7 @@ def handle_unknown(s)
end end


def handle_connect def handle_connect
reply :raw, "NOTICE AUTH :#{$config['version']} initialized, welcome." reply :raw, "NOTICE AUTH :Kookaburra v#{Kookaburra.version} initialized, welcome."
end end


def reply(method, *args) def reply(method, *args)
Expand Down Expand Up @@ -477,14 +479,14 @@ def reply(method, *args)
raw "#{@usermsg} MODE #{nick} :#{rest}" raw "#{@usermsg} MODE #{nick} :#{rest}"
when :numeric when :numeric
numeric,msg,detail = args numeric,msg,detail = args
server = $config['hostname'] server = Kookaburra.host_name
raw ":#{server} #{'%03d'%numeric} #{@nick} #{msg} :#{detail}" raw ":#{server} #{'%03d'%numeric} #{@nick} #{msg} :#{detail}"
end end
end end


def raw(arg, abrt=false) def raw(arg, abrt=false)
begin begin
#carp "--> #{arg}" carp "--> #{arg}"
@server.send_data arg.chomp + "\r\n" if !arg.nil? @server.send_data arg.chomp + "\r\n" if !arg.nil?
rescue Exception => e rescue Exception => e
carp "<#{self.userprefix}>#{e.message}" carp "<#{self.userprefix}>#{e.message}"
Expand Down
63 changes: 63 additions & 0 deletions lib/irc_logger.rb
@@ -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.
17 changes: 17 additions & 0 deletions lib/ircd.rb
@@ -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 = /^:[^ ]+ +(.+)$/
69 changes: 69 additions & 0 deletions lib/kookaburra.rb
@@ -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

0 comments on commit 3bebd62

Please sign in to comment.