5 $debug = false unless $debug
6 # print +message+ if debugging is enabled
9 stamp = Time.now.strftime("%Y/%m/%d %H:%M:%S")
10 print "D: [#{stamp}] #{message}\n"
17 require 'rbot/rbotconfig'
21 require 'rbot/rfc2812'
22 require 'rbot/keywords'
23 require 'rbot/ircsocket'
26 require 'rbot/plugins'
27 require 'rbot/channel'
28 require 'rbot/message'
29 require 'rbot/language'
31 require 'rbot/registry'
32 require 'rbot/httputil'
36 # Main bot class, which manages the various components, receives messages,
37 # handles them or passes them to plugins, and contains core functionality.
39 # the bot's current nickname
42 # the bot's IrcAuth data
45 # the bot's BotConfig data
48 # the botclass for this bot (determines configdir among other things)
51 # used to perform actions periodically (saves configuration once per minute
58 # channel info for channels the bot is in
64 # bot's object registry, plugins get an interface to this for persistant
65 # storage (hash interface tied to a bdb file, plugins use Accessors to store
66 # and restore objects in their own namespaces.)
69 # bot's httputil help object, for fetching resources via http. Sets up
70 # proxies etc as defined by the bot configuration/environment
73 # create a new IrcBot with botclass +botclass+
74 def initialize(botclass, params = {})
75 # BotConfig for the core bot
76 BotConfig.register BotConfigStringValue.new('server.name',
77 :default => "localhost", :requires_restart => true,
78 :desc => "What server should the bot connect to?",
80 BotConfig.register BotConfigIntegerValue.new('server.port',
81 :default => 6667, :type => :integer, :requires_restart => true,
82 :desc => "What port should the bot connect to?",
83 :validate => Proc.new {|v| v > 0}, :wizard => true)
84 BotConfig.register BotConfigStringValue.new('server.password',
85 :default => false, :requires_restart => true,
86 :desc => "Password for connecting to this server (if required)",
88 BotConfig.register BotConfigStringValue.new('server.bindhost',
89 :default => false, :requires_restart => true,
90 :desc => "Specific local host or IP for the bot to bind to (if required)",
92 BotConfig.register BotConfigIntegerValue.new('server.reconnect_wait',
93 :default => 5, :validate => Proc.new{|v| v >= 0},
94 :desc => "Seconds to wait before attempting to reconnect, on disconnect")
95 BotConfig.register BotConfigStringValue.new('irc.nick', :default => "rbot",
96 :desc => "IRC nickname the bot should attempt to use", :wizard => true,
97 :on_change => Proc.new{|bot, v| bot.sendq "NICK #{v}" })
98 BotConfig.register BotConfigStringValue.new('irc.user', :default => "rbot",
99 :requires_restart => true,
100 :desc => "local user the bot should appear to be", :wizard => true)
101 BotConfig.register BotConfigArrayValue.new('irc.join_channels',
102 :default => [], :wizard => true,
103 :desc => "What channels the bot should always join at startup. List multiple channels using commas to separate. If a channel requires a password, use a space after the channel name. e.g: '#chan1, #chan2, #secretchan secritpass, #chan3'")
104 BotConfig.register BotConfigIntegerValue.new('core.save_every',
105 :default => 60, :validate => Proc.new{|v| v >= 0},
106 # TODO change timer via on_change proc
107 :desc => "How often the bot should persist all configuration to disk (in case of a server crash, for example")
108 BotConfig.register BotConfigFloatValue.new('server.sendq_delay',
109 :default => 2.0, :validate => Proc.new{|v| v >= 0},
110 :desc => "(flood prevention) the delay between sending messages to the server (in seconds)",
111 :on_change => Proc.new {|bot, v| bot.socket.sendq_delay = v })
112 BotConfig.register BotConfigIntegerValue.new('server.sendq_burst',
113 :default => 4, :validate => Proc.new{|v| v >= 0},
114 :desc => "(flood prevention) max lines to burst to the server before throttling. Most ircd's allow bursts of up 5 lines, with non-burst limits of 512 bytes/2 seconds",
115 :on_change => Proc.new {|bot, v| bot.socket.sendq_burst = v })
116 BotConfig.register BotConfigIntegerValue.new('server.ping_timeout',
117 :default => 10, :validate => Proc.new{|v| v >= 0},
118 :on_change => Proc.new {|bot, v| bot.start_server_pings},
119 :desc => "reconnect if server doesn't respond to PING within this many seconds (set to 0 to disable)")
121 @argv = params[:argv]
123 unless FileTest.directory? Config::datadir
124 puts "data directory '#{Config::datadir}' not found, did you setup.rb?"
128 botclass = "#{Etc.getpwuid(Process::Sys.geteuid)[:dir]}/.rbot" unless botclass
129 #botclass = "#{ENV['HOME']}/.rbot" unless botclass
130 botclass = File.expand_path(botclass)
131 @botclass = botclass.gsub(/\/$/, "")
133 unless FileTest.directory? botclass
134 puts "no #{botclass} directory found, creating from templates.."
135 if FileTest.exist? botclass
136 puts "Error: file #{botclass} exists but isn't a directory"
139 FileUtils.cp_r Config::datadir+'/templates', botclass
142 Dir.mkdir("#{botclass}/logs") unless File.exist?("#{botclass}/logs")
143 Dir.mkdir("#{botclass}/registry") unless File.exist?("#{botclass}/registry")
148 @startup_time = Time.new
149 @config = BotConfig.new(self)
150 # TODO background self after botconfig has a chance to run wizard
151 @timer = Timer::Timer.new(1.0) # only need per-second granularity
152 @registry = BotRegistry.new self
153 @timer.add(@config['core.save_every']) { save } if @config['core.save_every']
156 @httputil = Utils::HttpUtil.new(self)
157 @lang = Language::Language.new(@config['core.language'])
158 @keywords = Keywords.new(self)
159 @auth = IrcAuth.new(self)
161 Dir.mkdir("#{botclass}/plugins") unless File.exist?("#{botclass}/plugins")
162 @plugins = Plugins::Plugins.new(self, ["#{botclass}/plugins"])
164 @socket = IrcSocket.new(@config['server.name'], @config['server.port'], @config['server.bindhost'], @config['server.sendq_delay'], @config['server.sendq_burst'])
165 @nick = @config['irc.nick']
167 @client = IrcClient.new
168 @client[:privmsg] = proc { |data|
169 message = PrivMessage.new(self, data[:source], data[:target], data[:message])
172 @client[:notice] = proc { |data|
173 message = NoticeMessage.new(self, data[:source], data[:target], data[:message])
174 # pass it off to plugins that want to hear everything
175 @plugins.delegate "listen", message
177 @client[:motd] = proc { |data|
178 data[:motd].each_line { |line|
179 log "MOTD: #{line}", "server"
182 @client[:nicktaken] = proc { |data|
183 nickchg "#{data[:nick]}_"
184 @plugins.delegate "nicktaken", data[:nick]
186 @client[:badnick] = proc {|data|
187 puts "WARNING, bad nick (#{data[:nick]})"
189 @client[:ping] = proc {|data|
190 # (jump the queue for pongs)
191 @socket.puts "PONG #{data[:pingid]}"
193 @client[:pong] = proc {|data|
196 @client[:nick] = proc {|data|
197 sourcenick = data[:sourcenick]
199 m = NickMessage.new(self, data[:source], data[:sourcenick], data[:nick])
200 if(sourcenick == @nick)
201 debug "my nick is now #{nick}"
204 @channels.each {|k,v|
205 if(v.users.has_key?(sourcenick))
206 log "@ #{sourcenick} is now known as #{nick}", k
207 v.users[nick] = v.users[sourcenick]
208 v.users.delete(sourcenick)
211 @plugins.delegate("listen", m)
212 @plugins.delegate("nick", m)
214 @client[:quit] = proc {|data|
215 source = data[:source]
216 sourcenick = data[:sourcenick]
217 sourceurl = data[:sourceaddress]
218 message = data[:message]
219 m = QuitMessage.new(self, data[:source], data[:sourcenick], data[:message])
220 if(data[:sourcenick] =~ /#{Regexp.escape(@nick)}/i)
222 @channels.each {|k,v|
223 if(v.users.has_key?(sourcenick))
224 log "@ Quit: #{sourcenick}: #{message}", k
225 v.users.delete(sourcenick)
229 @plugins.delegate("listen", m)
230 @plugins.delegate("quit", m)
232 @client[:mode] = proc {|data|
233 source = data[:source]
234 sourcenick = data[:sourcenick]
235 sourceurl = data[:sourceaddress]
236 channel = data[:channel]
237 targets = data[:targets]
238 modestring = data[:modestring]
239 log "@ Mode #{modestring} #{targets} by #{sourcenick}", channel
241 @client[:welcome] = proc {|data|
242 log "joined server #{data[:source]} as #{data[:nick]}", "server"
243 debug "I think my nick is #{@nick}, server thinks #{data[:nick]}"
244 if data[:nick] && data[:nick].length > 0
248 @plugins.delegate("connect")
250 @config['irc.join_channels'].each {|c|
251 debug "autojoining channel #{c}"
252 if(c =~ /^(\S+)\s+(\S+)$/i)
259 @client[:join] = proc {|data|
260 m = JoinMessage.new(self, data[:source], data[:channel], data[:message])
263 @client[:part] = proc {|data|
264 m = PartMessage.new(self, data[:source], data[:channel], data[:message])
267 @client[:kick] = proc {|data|
268 m = KickMessage.new(self, data[:source], data[:target],data[:channel],data[:message])
271 @client[:invite] = proc {|data|
272 if(data[:target] =~ /^#{Regexp.escape(@nick)}$/i)
273 join data[:channel] if (@auth.allow?("join", data[:source], data[:sourcenick]))
276 @client[:changetopic] = proc {|data|
277 channel = data[:channel]
278 sourcenick = data[:sourcenick]
280 timestamp = data[:unixtime] || Time.now.to_i
281 if(sourcenick == @nick)
282 log "@ I set topic \"#{topic}\"", channel
284 log "@ #{sourcenick} set topic \"#{topic}\"", channel
286 m = TopicMessage.new(self, data[:source], data[:channel], timestamp, data[:topic])
289 @plugins.delegate("listen", m)
290 @plugins.delegate("topic", m)
292 @client[:topic] = @client[:topicinfo] = proc {|data|
293 channel = data[:channel]
294 m = TopicMessage.new(self, data[:source], data[:channel], data[:unixtime], data[:topic])
297 @client[:names] = proc {|data|
298 channel = data[:channel]
300 unless(@channels[channel])
301 puts "bug: got names for channel '#{channel}' I didn't think I was in\n"
304 @channels[channel].users.clear
306 @channels[channel].users[u[0].sub(/^[@&~+]/, '')] = ["mode", u[1]]
308 @plugins.delegate "names", data[:channel], data[:users]
310 @client[:unknown] = proc {|data|
311 #debug "UNKNOWN: #{data[:serverstring]}"
312 log data[:serverstring], ".unknown"
316 # connect the bot to IRC
319 trap("SIGINT") { quit }
320 trap("SIGTERM") { quit }
321 trap("SIGHUP") { quit }
323 debug "failed to trap signals, probably running on windows?"
328 raise "failed to connect to IRC server at #{@config['server.name']} #{@config['server.port']}: " + e
330 @socket.puts "PASS " + @config['server.password'] if @config['server.password']
331 @socket.puts "NICK #{@nick}\nUSER #{@config['irc.user']} 4 #{@config['server.name']} :Ruby bot. (c) Tom Gilbert"
335 # begin event handling loop
342 while @socket.connected?
344 break unless reply = @socket.gets
345 @client.process reply
348 # I despair of this. Some of my users get "connection reset by peer"
349 # exceptions that ARENT SocketError's. How am I supposed to handle
351 #rescue TimeoutError, SocketError => e
354 rescue TimeoutError => e
356 rescue Exception => e
357 puts "network exception: #{e.inspect}"
358 puts e.backtrace.join("\n")
359 @socket.shutdown # now we reconnect
361 puts "unexpected exception: connection closed: #{e.inspect}"
362 puts e.backtrace.join("\n")
371 puts "waiting to reconnect"
372 sleep @config['server.reconnect_wait']
376 # type:: message type
377 # where:: message target
378 # message:: message text
379 # send message +message+ of type +type+ to target +where+
380 # Type can be PRIVMSG, NOTICE, etc, but those you should really use the
381 # relevant say() or notice() methods. This one should be used for IRCd
382 # extensions you want to use in modules.
383 def sendmsg(type, where, message)
384 # limit it 440 chars + CRLF.. so we have to split long lines
385 left = 440 - type.length - where.length - 3
387 if(left >= message.length)
388 sendq("#{type} #{where} :#{message}")
389 log_sent(type, where, message)
392 line = message.slice!(0, left)
393 lastspace = line.rindex(/\s+/)
395 message = line.slice!(lastspace, line.length) + message
396 message.gsub!(/^\s+/, "")
398 sendq("#{type} #{where} :#{line}")
399 log_sent(type, where, line)
400 end while(message.length > 0)
403 # queue an arbitraty message for the server
404 def sendq(message="")
406 @socket.queue(message)
409 # send a notice message to channel/nick +where+
410 def notice(where, message)
411 message.each_line { |line|
413 next unless(line.length > 0)
414 sendmsg("NOTICE", where, line)
418 # say something (PRIVMSG) to channel/nick +where+
419 def say(where, message)
420 message.to_s.gsub(/[\r\n]+/, "\n").each_line { |line|
422 next unless(line.length > 0)
423 unless((where =~ /^#/) && (@channels.has_key?(where) && @channels[where].quiet))
424 sendmsg("PRIVMSG", where, line)
429 # perform a CTCP action with message +message+ to channel/nick +where+
430 def action(where, message)
431 sendq("PRIVMSG #{where} :\001ACTION #{message}\001")
433 log "* #{@nick} #{message}", where
434 elsif (where =~ /^(\S*)!.*$/)
435 log "* #{@nick}[#{where}] #{message}", $1
437 log "* #{@nick}[#{where}] #{message}", where
441 # quick way to say "okay" (or equivalent) to +where+
443 say where, @lang.get("okay")
446 # log message +message+ to a file determined by +where+. +where+ can be a
447 # channel name, or a nick for private message logging
448 def log(message, where="server")
449 message = message.chomp
450 stamp = Time.now.strftime("%Y/%m/%d %H:%M:%S")
451 where = where.gsub(/[:!?$*()\/\\<>|"']/, "_")
452 unless(@logs.has_key?(where))
453 @logs[where] = File.new("#{@botclass}/logs/#{where}", "a")
454 @logs[where].sync = true
456 @logs[where].puts "[#{stamp}] #{message}"
457 #debug "[#{stamp}] <#{where}> #{message}"
460 # set topic of channel +where+ to +topic+
461 def topic(where, topic)
462 sendq "TOPIC #{where} :#{topic}"
465 # disconnect from the server and cleanup all plugins and modules
466 def shutdown(message = nil)
468 trap("SIGINT", "DEFAULT")
469 trap("SIGTERM", "DEFAULT")
470 trap("SIGHUP", "DEFAULT")
472 debug "failed to trap signals, probably running on windows?"
474 message = @lang.get("quit") if (message.nil? || message.empty?)
475 debug "Clearing socket"
481 debug "Logging quits"
482 @channels.each_value {|v|
483 log "@ quit (#{message})", v.name
485 # debug "Closing registries"
487 debug "Cleaning up the db environment"
489 debug "Sending quit message"
490 @socket.puts "QUIT :#{message}"
491 debug "Flushing socket"
493 debug "Shutting down socket"
495 puts "rbot quit (#{message})"
498 # message:: optional IRC quit message
499 # quit IRC, shutdown the bot
500 def quit(message=nil)
508 # totally shutdown and respawn the bot
509 def restart(message = false)
510 msg = message ? message : "restarting, back in #{@config['server.reconnect_wait']}..."
512 sleep @config['server.reconnect_wait']
517 # call the save method for bot's config, keywords, auth and all plugins
526 # call the rescan method for the bot's lang, keywords and all plugins
533 # channel:: channel to join
534 # key:: optional channel key if channel is +s
536 def join(channel, key=nil)
538 sendq "JOIN #{channel} :#{key}"
540 sendq "JOIN #{channel}"
545 def part(channel, message="")
546 sendq "PART #{channel} :#{message}"
549 # attempt to change bot's nick to +name+
555 def mode(channel, mode, target)
556 sendq "MODE #{channel} #{mode} #{target}"
559 # m:: message asking for help
560 # topic:: optional topic help is requested for
561 # respond to online help requests
563 topic = nil if topic == ""
566 helpstr = "help topics: core, auth, keywords"
567 helpstr += @plugins.helptopics
568 helpstr += " (help <topic> for more info)"
571 when /^core\s+(.+)$/i
572 helpstr = corehelp $1
575 when /^auth\s+(.+)$/i
576 helpstr = @auth.help $1
578 helpstr = @keywords.help
579 when /^keywords\s+(.+)$/i
580 helpstr = @keywords.help $1
582 unless(helpstr = @plugins.help(topic))
583 helpstr = "no help for topic #{topic}"
589 # returns a string describing the current status of the bot (uptime etc)
591 secs_up = Time.new - @startup_time
592 uptime = Utils.secs_to_string secs_up
593 # return "Uptime #{uptime}, #{@plugins.length} plugins active, #{@registry.length} items stored in registry, #{@socket.lines_sent} lines sent, #{@socket.lines_received} received."
594 return "Uptime #{uptime}, #{@plugins.length} plugins active, #{@socket.lines_sent} lines sent, #{@socket.lines_received} received."
597 # we'll ping the server every 30 seconds or so, and expect a response
598 # before the next one come around..
599 def start_server_pings
601 # stop existing timers if running
602 unless @ping_timer.nil?
603 @timer.remove @ping_timer
606 unless @pong_timer.nil?
607 @timer.remove @pong_timer
610 return unless @config['server.ping_timeout'] > 0
611 # we want to respond to a hung server within 30 secs or so
612 @ping_timer = @timer.add(30) {
613 @last_ping = Time.now
614 @socket.puts "PING :rbot"
616 @pong_timer = @timer.add(10) {
617 unless @last_ping.nil?
618 diff = Time.now - @last_ping
619 unless diff < @config['server.ping_timeout']
620 debug "no PONG from server for #{diff} seconds, reconnecting"
624 debug "couldn't shutdown connection (already shutdown?)"
626 raise TimeoutError, "no PONG from server in #{diff} seconds"
636 # handle help requests for "core" topics
637 def corehelp(topic="")
640 return "quit [<message>] => quit IRC with message <message>"
642 return "restart => completely stop and restart the bot (including reconnect)"
644 return "join <channel> [<key>] => join channel <channel> with secret key <key> if specified. #{@nick} also responds to invites if you have the required access level"
646 return "part <channel> => part channel <channel>"
648 return "hide => part all channels"
650 return "save => save current dynamic data and configuration"
652 return "rescan => reload modules and static facts"
654 return "nick <nick> => attempt to change nick to <nick>"
656 return "say <channel>|<nick> <message> => say <message> to <channel> or in private message to <nick>"
658 return "action <channel>|<nick> <message> => does a /me <message> to <channel> or in private message to <nick>"
660 # return "topic <channel> <message> => set topic of <channel> to <message>"
662 return "quiet [in here|<channel>] => with no arguments, stop speaking in all channels, if \"in here\", stop speaking in this channel, or stop speaking in <channel>"
664 return "talk [in here|<channel>] => with no arguments, resume speaking in all channels, if \"in here\", resume speaking in this channel, or resume speaking in <channel>"
666 return "version => describes software version"
668 return "botsnack => reward #{@nick} for being good"
670 return "hello|hi|hey|yo [#{@nick}] => greet the bot"
672 return "Core help topics: quit, restart, config, join, part, hide, save, rescan, nick, say, action, topic, quiet, talk, version, botsnack, hello"
676 # handle incoming IRC PRIVMSG +m+
681 log "* [#{m.sourcenick}(#{m.sourceaddress})] #{m.message}", m.sourcenick
683 log "* #{m.sourcenick} #{m.message}", m.target
687 log "<#{m.sourcenick}> #{m.message}", m.target
689 log "[#{m.sourcenick}(#{m.sourceaddress})] #{m.message}", m.sourcenick
693 # pass it off to plugins that want to hear everything
694 @plugins.delegate "listen", m
696 if(m.private? && m.message =~ /^\001PING\s+(.+)\001/)
697 notice m.sourcenick, "\001PING #$1\001"
698 log "@ #{m.sourcenick} pinged me"
705 when (/^join\s+(\S+)\s+(\S+)$/i)
706 join $1, $2 if(@auth.allow?("join", m.source, m.replyto))
707 when (/^join\s+(\S+)$/i)
708 join $1 if(@auth.allow?("join", m.source, m.replyto))
710 part m.target if(m.public? && @auth.allow?("join", m.source, m.replyto))
711 when (/^part\s+(\S+)$/i)
712 part $1 if(@auth.allow?("join", m.source, m.replyto))
713 when (/^quit(?:\s+(.*))?$/i)
714 quit $1 if(@auth.allow?("quit", m.source, m.replyto))
715 when (/^restart(?:\s+(.*))?$/i)
716 restart $1 if(@auth.allow?("quit", m.source, m.replyto))
718 join 0 if(@auth.allow?("join", m.source, m.replyto))
720 if(@auth.allow?("config", m.source, m.replyto))
724 when (/^nick\s+(\S+)$/i)
725 nickchg($1) if(@auth.allow?("nick", m.source, m.replyto))
726 when (/^say\s+(\S+)\s+(.*)$/i)
727 say $1, $2 if(@auth.allow?("say", m.source, m.replyto))
728 when (/^action\s+(\S+)\s+(.*)$/i)
729 action $1, $2 if(@auth.allow?("say", m.source, m.replyto))
730 # when (/^topic\s+(\S+)\s+(.*)$/i)
731 # topic $1, $2 if(@auth.allow?("topic", m.source, m.replyto))
732 when (/^mode\s+(\S+)\s+(\S+)\s+(.*)$/i)
733 mode $1, $2, $3 if(@auth.allow?("mode", m.source, m.replyto))
735 say m.replyto, "pong"
737 if(@auth.allow?("config", m.source, m.replyto))
740 m.reply "Rescanning ..."
745 if(auth.allow?("talk", m.source, m.replyto))
747 @channels.each_value {|c| c.quiet = true }
749 when (/^quiet in (\S+)$/i)
751 if(auth.allow?("talk", m.source, m.replyto))
753 where.gsub!(/^here$/, m.target) if m.public?
754 @channels[where].quiet = true if(@channels.has_key?(where))
757 if(auth.allow?("talk", m.source, m.replyto))
758 @channels.each_value {|c| c.quiet = false }
761 when (/^talk in (\S+)$/i)
763 if(auth.allow?("talk", m.source, m.replyto))
764 where.gsub!(/^here$/, m.target) if m.public?
765 @channels[where].quiet = false if(@channels.has_key?(where))
768 when (/^status\??$/i)
769 m.reply status if auth.allow?("status", m.source, m.replyto)
770 when (/^registry stats$/i)
771 if auth.allow?("config", m.source, m.replyto)
772 m.reply @registry.stat.inspect
774 when (/^(help\s+)?config(\s+|$)/)
776 when (/^(version)|(introduce yourself)$/i)
777 say m.replyto, "I'm a v. #{$version} rubybot, (c) Tom Gilbert - http://linuxbrit.co.uk/rbot/"
778 when (/^help(?:\s+(.*))?$/i)
779 say m.replyto, help($1)
780 #TODO move these to a "chatback" plugin
781 when (/^(botsnack|ciggie)$/i)
782 say m.replyto, @lang.get("thanks_X") % m.sourcenick if(m.public?)
783 say m.replyto, @lang.get("thanks") if(m.private?)
784 when (/^(hello|howdy|hola|salut|bonjour|sup|niihau|hey|hi(\W|$)|yo(\W|$)).*/i)
785 say m.replyto, @lang.get("hello_X") % m.sourcenick if(m.public?)
786 say m.replyto, @lang.get("hello") if(m.private?)
789 # stuff to handle when not addressed
791 when (/^\s*(hello|howdy|hola|salut|bonjour|sup|niihau|hey|hi|yo(\W|$))[\s,-.]+#{Regexp.escape(@nick)}$/i)
792 say m.replyto, @lang.get("hello_X") % m.sourcenick
793 when (/^#{Regexp.escape(@nick)}!*$/)
794 say m.replyto, @lang.get("hello_X") % m.sourcenick
801 # log a message. Internal use only.
802 def log_sent(type, where, message)
806 log "-=#{@nick}=- #{message}", where
807 elsif (where =~ /(\S*)!.*/)
808 log "[-=#{where}=-] #{message}", $1
810 log "[-=#{where}=-] #{message}"
814 log "<#{@nick}> #{message}", where
815 elsif (where =~ /^(\S*)!.*$/)
816 log "[msg(#{where})] #{message}", $1
818 log "[msg(#{where})] #{message}", where
824 @channels[m.channel] = IRCChannel.new(m.channel) unless(@channels.has_key?(m.channel))
826 debug "joined channel #{m.channel}"
827 log "@ Joined channel #{m.channel}", m.channel
829 log "@ #{m.sourcenick} joined channel #{m.channel}", m.channel
830 @channels[m.channel].users[m.sourcenick] = Hash.new
831 @channels[m.channel].users[m.sourcenick]["mode"] = ""
834 @plugins.delegate("listen", m)
835 @plugins.delegate("join", m)
840 debug "left channel #{m.channel}"
841 log "@ Left channel #{m.channel} (#{m.message})", m.channel
842 @channels.delete(m.channel)
844 log "@ #{m.sourcenick} left channel #{m.channel} (#{m.message})", m.channel
845 if @channels.has_key?(m.channel)
846 @channels[m.channel].users.delete(m.sourcenick)
848 puts "bug: got part for channel '#{channel}' I didn't think I was in\n"
853 # delegate to plugins
854 @plugins.delegate("listen", m)
855 @plugins.delegate("part", m)
858 # respond to being kicked from a channel
861 debug "kicked from channel #{m.channel}"
862 @channels.delete(m.channel)
863 log "@ You have been kicked from #{m.channel} by #{m.sourcenick} (#{m.message})", m.channel
865 @channels[m.channel].users.delete(m.sourcenick)
866 log "@ #{m.target} has been kicked from #{m.channel} by #{m.sourcenick} (#{m.message})", m.channel
869 @plugins.delegate("listen", m)
870 @plugins.delegate("kick", m)
874 @channels[m.channel] = IRCChannel.new(m.channel) unless(@channels.has_key?(m.channel))
875 @channels[m.channel].topic = m.topic if !m.topic.nil?
876 @channels[m.channel].topic.timestamp = m.timestamp if !m.timestamp.nil?
877 @channels[m.channel].topic.by = m.source if !m.source.nil?
879 debug "topic of channel #{m.channel} is now #{@channels[m.channel].topic}"
882 # delegate a privmsg to auth, keyword or plugin handlers
883 def delegate_privmsg(message)
884 [@auth, @plugins, @keywords].each {|m|
885 break if m.privmsg(message)