5 $debug = false unless $debug
6 # print +message+ if debugging is enabled
9 stamp = Time.now.strftime("%Y/%m/%d %H:%M:%S")
10 message.to_s.each_line { |l|
11 $stdout.puts "D: [#{stamp}] #{l}"
19 require 'rbot/rbotconfig'
23 require 'rbot/rfc2812'
24 require 'rbot/keywords'
25 require 'rbot/ircsocket'
28 require 'rbot/plugins'
29 require 'rbot/channel'
30 require 'rbot/message'
31 require 'rbot/language'
33 require 'rbot/registry'
34 require 'rbot/httputil'
38 # Main bot class, which manages the various components, receives messages,
39 # handles them or passes them to plugins, and contains core functionality.
41 # the bot's current nickname
44 # the bot's IrcAuth data
47 # the bot's BotConfig data
50 # the botclass for this bot (determines configdir among other things)
53 # used to perform actions periodically (saves configuration once per minute
60 # channel info for channels the bot is in
66 # bot's object registry, plugins get an interface to this for persistant
67 # storage (hash interface tied to a bdb file, plugins use Accessors to store
68 # and restore objects in their own namespaces.)
71 # bot's httputil help object, for fetching resources via http. Sets up
72 # proxies etc as defined by the bot configuration/environment
75 # create a new IrcBot with botclass +botclass+
76 def initialize(botclass, params = {})
77 # BotConfig for the core bot
78 BotConfig.register BotConfigStringValue.new('server.name',
79 :default => "localhost", :requires_restart => true,
80 :desc => "What server should the bot connect to?",
82 BotConfig.register BotConfigIntegerValue.new('server.port',
83 :default => 6667, :type => :integer, :requires_restart => true,
84 :desc => "What port should the bot connect to?",
85 :validate => Proc.new {|v| v > 0}, :wizard => true)
86 BotConfig.register BotConfigStringValue.new('server.password',
87 :default => false, :requires_restart => true,
88 :desc => "Password for connecting to this server (if required)",
90 BotConfig.register BotConfigStringValue.new('server.bindhost',
91 :default => false, :requires_restart => true,
92 :desc => "Specific local host or IP for the bot to bind to (if required)",
94 BotConfig.register BotConfigIntegerValue.new('server.reconnect_wait',
95 :default => 5, :validate => Proc.new{|v| v >= 0},
96 :desc => "Seconds to wait before attempting to reconnect, on disconnect")
97 BotConfig.register BotConfigStringValue.new('irc.nick', :default => "rbot",
98 :desc => "IRC nickname the bot should attempt to use", :wizard => true,
99 :on_change => Proc.new{|bot, v| bot.sendq "NICK #{v}" })
100 BotConfig.register BotConfigStringValue.new('irc.user', :default => "rbot",
101 :requires_restart => true,
102 :desc => "local user the bot should appear to be", :wizard => true)
103 BotConfig.register BotConfigArrayValue.new('irc.join_channels',
104 :default => [], :wizard => true,
105 :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'")
106 BotConfig.register BotConfigIntegerValue.new('core.save_every',
107 :default => 60, :validate => Proc.new{|v| v >= 0},
108 # TODO change timer via on_change proc
109 :desc => "How often the bot should persist all configuration to disk (in case of a server crash, for example")
110 BotConfig.register BotConfigFloatValue.new('server.sendq_delay',
111 :default => 2.0, :validate => Proc.new{|v| v >= 0},
112 :desc => "(flood prevention) the delay between sending messages to the server (in seconds)",
113 :on_change => Proc.new {|bot, v| bot.socket.sendq_delay = v })
114 BotConfig.register BotConfigIntegerValue.new('server.sendq_burst',
115 :default => 4, :validate => Proc.new{|v| v >= 0},
116 :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",
117 :on_change => Proc.new {|bot, v| bot.socket.sendq_burst = v })
118 BotConfig.register BotConfigIntegerValue.new('server.ping_timeout',
119 :default => 10, :validate => Proc.new{|v| v >= 0},
120 :on_change => Proc.new {|bot, v| bot.start_server_pings},
121 :desc => "reconnect if server doesn't respond to PING within this many seconds (set to 0 to disable)")
123 @argv = params[:argv]
125 unless FileTest.directory? Config::datadir
126 puts "data directory '#{Config::datadir}' not found, did you setup.rb?"
130 botclass = "#{Etc.getpwuid(Process::Sys.geteuid)[:dir]}/.rbot" unless botclass
131 #botclass = "#{ENV['HOME']}/.rbot" unless botclass
132 botclass = File.expand_path(botclass)
133 @botclass = botclass.gsub(/\/$/, "")
135 unless FileTest.directory? botclass
136 puts "no #{botclass} directory found, creating from templates.."
137 if FileTest.exist? botclass
138 puts "Error: file #{botclass} exists but isn't a directory"
141 FileUtils.cp_r Config::datadir+'/templates', botclass
144 Dir.mkdir("#{botclass}/logs") unless File.exist?("#{botclass}/logs")
145 Dir.mkdir("#{botclass}/registry") unless File.exist?("#{botclass}/registry")
150 @startup_time = Time.new
151 @config = BotConfig.new(self)
152 # TODO background self after botconfig has a chance to run wizard
153 @timer = Timer::Timer.new(1.0) # only need per-second granularity
154 @registry = BotRegistry.new self
155 @timer.add(@config['core.save_every']) { save } if @config['core.save_every']
158 @httputil = Utils::HttpUtil.new(self)
159 @lang = Language::Language.new(@config['core.language'])
160 @keywords = Keywords.new(self)
161 @auth = IrcAuth.new(self)
163 Dir.mkdir("#{botclass}/plugins") unless File.exist?("#{botclass}/plugins")
164 @plugins = Plugins::Plugins.new(self, ["#{botclass}/plugins"])
166 @socket = IrcSocket.new(@config['server.name'], @config['server.port'], @config['server.bindhost'], @config['server.sendq_delay'], @config['server.sendq_burst'])
167 @nick = @config['irc.nick']
169 @client = IrcClient.new
170 @client[:privmsg] = proc { |data|
171 message = PrivMessage.new(self, data[:source], data[:target], data[:message])
174 @client[:notice] = proc { |data|
175 message = NoticeMessage.new(self, data[:source], data[:target], data[:message])
176 # pass it off to plugins that want to hear everything
177 @plugins.delegate "listen", message
179 @client[:motd] = proc { |data|
180 data[:motd].each_line { |line|
181 log "MOTD: #{line}", "server"
184 @client[:nicktaken] = proc { |data|
185 nickchg "#{data[:nick]}_"
186 @plugins.delegate "nicktaken", data[:nick]
188 @client[:badnick] = proc {|data|
189 puts "WARNING, bad nick (#{data[:nick]})"
191 @client[:ping] = proc {|data|
192 # (jump the queue for pongs)
193 @socket.puts "PONG #{data[:pingid]}"
195 @client[:pong] = proc {|data|
198 @client[:nick] = proc {|data|
199 sourcenick = data[:sourcenick]
201 m = NickMessage.new(self, data[:source], data[:sourcenick], data[:nick])
202 if(sourcenick == @nick)
203 debug "my nick is now #{nick}"
206 @channels.each {|k,v|
207 if(v.users.has_key?(sourcenick))
208 log "@ #{sourcenick} is now known as #{nick}", k
209 v.users[nick] = v.users[sourcenick]
210 v.users.delete(sourcenick)
213 @plugins.delegate("listen", m)
214 @plugins.delegate("nick", m)
216 @client[:quit] = proc {|data|
217 source = data[:source]
218 sourcenick = data[:sourcenick]
219 sourceurl = data[:sourceaddress]
220 message = data[:message]
221 m = QuitMessage.new(self, data[:source], data[:sourcenick], data[:message])
222 if(data[:sourcenick] =~ /#{Regexp.escape(@nick)}/i)
224 @channels.each {|k,v|
225 if(v.users.has_key?(sourcenick))
226 log "@ Quit: #{sourcenick}: #{message}", k
227 v.users.delete(sourcenick)
231 @plugins.delegate("listen", m)
232 @plugins.delegate("quit", m)
234 @client[:mode] = proc {|data|
235 source = data[:source]
236 sourcenick = data[:sourcenick]
237 sourceurl = data[:sourceaddress]
238 channel = data[:channel]
239 targets = data[:targets]
240 modestring = data[:modestring]
241 log "@ Mode #{modestring} #{targets} by #{sourcenick}", channel
243 @client[:welcome] = proc {|data|
244 log "joined server #{data[:source]} as #{data[:nick]}", "server"
245 debug "I think my nick is #{@nick}, server thinks #{data[:nick]}"
246 if data[:nick] && data[:nick].length > 0
250 @plugins.delegate("connect")
252 @config['irc.join_channels'].each {|c|
253 debug "autojoining channel #{c}"
254 if(c =~ /^(\S+)\s+(\S+)$/i)
261 @client[:join] = proc {|data|
262 m = JoinMessage.new(self, data[:source], data[:channel], data[:message])
265 @client[:part] = proc {|data|
266 m = PartMessage.new(self, data[:source], data[:channel], data[:message])
269 @client[:kick] = proc {|data|
270 m = KickMessage.new(self, data[:source], data[:target],data[:channel],data[:message])
273 @client[:invite] = proc {|data|
274 if(data[:target] =~ /^#{Regexp.escape(@nick)}$/i)
275 join data[:channel] if (@auth.allow?("join", data[:source], data[:sourcenick]))
278 @client[:changetopic] = proc {|data|
279 channel = data[:channel]
280 sourcenick = data[:sourcenick]
282 timestamp = data[:unixtime] || Time.now.to_i
283 if(sourcenick == @nick)
284 log "@ I set topic \"#{topic}\"", channel
286 log "@ #{sourcenick} set topic \"#{topic}\"", channel
288 m = TopicMessage.new(self, data[:source], data[:channel], timestamp, data[:topic])
291 @plugins.delegate("listen", m)
292 @plugins.delegate("topic", m)
294 @client[:topic] = @client[:topicinfo] = proc {|data|
295 channel = data[:channel]
296 m = TopicMessage.new(self, data[:source], data[:channel], data[:unixtime], data[:topic])
299 @client[:names] = proc {|data|
300 channel = data[:channel]
302 unless(@channels[channel])
303 puts "bug: got names for channel '#{channel}' I didn't think I was in\n"
306 @channels[channel].users.clear
308 @channels[channel].users[u[0].sub(/^[@&~+]/, '')] = ["mode", u[1]]
310 @plugins.delegate "names", data[:channel], data[:users]
312 @client[:unknown] = proc {|data|
313 #debug "UNKNOWN: #{data[:serverstring]}"
314 log data[:serverstring], ".unknown"
318 # connect the bot to IRC
321 trap("SIGINT") { quit }
322 trap("SIGTERM") { quit }
323 trap("SIGHUP") { quit }
325 debug "failed to trap signals, probably running on windows?"
330 raise e.class, "failed to connect to IRC server at #{@config['server.name']} #{@config['server.port']}: " + e
332 @socket.puts "PASS " + @config['server.password'] if @config['server.password']
333 @socket.puts "NICK #{@nick}\nUSER #{@config['irc.user']} 4 #{@config['server.name']} :Ruby bot. (c) Tom Gilbert"
337 # begin event handling loop
344 while @socket.connected?
346 break unless reply = @socket.gets
347 @client.process reply
350 # I despair of this. Some of my users get "connection reset by peer"
351 # exceptions that ARENT SocketError's. How am I supposed to handle
353 #rescue TimeoutError, SocketError => e
356 rescue TimeoutError, SocketError => e
357 puts "#{e.class}: #{e}"
358 debug e.backtrace.join("\n")
359 rescue Exception => e
360 puts "network exception: #{e.inspect}"
361 puts e.backtrace.join("\n")
362 @socket.shutdown # now we reconnect
364 puts "unexpected exception: connection closed: #{e.inspect}"
365 puts e.backtrace.join("\n")
375 puts "waiting to reconnect"
376 sleep @config['server.reconnect_wait']
380 # type:: message type
381 # where:: message target
382 # message:: message text
383 # send message +message+ of type +type+ to target +where+
384 # Type can be PRIVMSG, NOTICE, etc, but those you should really use the
385 # relevant say() or notice() methods. This one should be used for IRCd
386 # extensions you want to use in modules.
387 def sendmsg(type, where, message)
388 # limit it 440 chars + CRLF.. so we have to split long lines
389 left = 440 - type.length - where.length - 3
391 if(left >= message.length)
392 sendq("#{type} #{where} :#{message}")
393 log_sent(type, where, message)
396 line = message.slice!(0, left)
397 lastspace = line.rindex(/\s+/)
399 message = line.slice!(lastspace, line.length) + message
400 message.gsub!(/^\s+/, "")
402 sendq("#{type} #{where} :#{line}")
403 log_sent(type, where, line)
404 end while(message.length > 0)
407 # queue an arbitraty message for the server
408 def sendq(message="")
410 @socket.queue(message)
413 # send a notice message to channel/nick +where+
414 def notice(where, message)
415 message.each_line { |line|
417 next unless(line.length > 0)
418 sendmsg("NOTICE", where, line)
422 # say something (PRIVMSG) to channel/nick +where+
423 def say(where, message)
424 message.to_s.gsub(/[\r\n]+/, "\n").each_line { |line|
426 next unless(line.length > 0)
427 unless((where =~ /^#/) && (@channels.has_key?(where) && @channels[where].quiet))
428 sendmsg("PRIVMSG", where, line)
433 # perform a CTCP action with message +message+ to channel/nick +where+
434 def action(where, message)
435 sendq("PRIVMSG #{where} :\001ACTION #{message}\001")
437 log "* #{@nick} #{message}", where
438 elsif (where =~ /^(\S*)!.*$/)
439 log "* #{@nick}[#{where}] #{message}", $1
441 log "* #{@nick}[#{where}] #{message}", where
445 # quick way to say "okay" (or equivalent) to +where+
447 say where, @lang.get("okay")
450 # log message +message+ to a file determined by +where+. +where+ can be a
451 # channel name, or a nick for private message logging
452 def log(message, where="server")
453 message = message.chomp
454 stamp = Time.now.strftime("%Y/%m/%d %H:%M:%S")
455 where = where.gsub(/[:!?$*()\/\\<>|"']/, "_")
456 unless(@logs.has_key?(where))
457 @logs[where] = File.new("#{@botclass}/logs/#{where}", "a")
458 @logs[where].sync = true
460 @logs[where].puts "[#{stamp}] #{message}"
461 #debug "[#{stamp}] <#{where}> #{message}"
464 # set topic of channel +where+ to +topic+
465 def topic(where, topic)
466 sendq "TOPIC #{where} :#{topic}"
469 # disconnect from the server and cleanup all plugins and modules
470 def shutdown(message = nil)
472 trap("SIGINT", "DEFAULT")
473 trap("SIGTERM", "DEFAULT")
474 trap("SIGHUP", "DEFAULT")
476 debug "failed to trap signals, probably running on windows?"
478 message = @lang.get("quit") if (message.nil? || message.empty?)
479 debug "Clearing socket"
485 debug "Logging quits"
486 @channels.each_value {|v|
487 log "@ quit (#{message})", v.name
489 # debug "Closing registries"
491 debug "Cleaning up the db environment"
493 if @socket.connected?
494 debug "Sending quit message"
495 @socket.puts "QUIT :#{message}"
496 debug "Flushing socket"
498 debug "Shutting down socket"
501 puts "rbot quit (#{message})"
504 # message:: optional IRC quit message
505 # quit IRC, shutdown the bot
506 def quit(message=nil)
514 # totally shutdown and respawn the bot
515 def restart(message = false)
516 msg = message ? message : "restarting, back in #{@config['server.reconnect_wait']}..."
518 sleep @config['server.reconnect_wait']
523 # call the save method for bot's config, keywords, auth and all plugins
532 # call the rescan method for the bot's lang, keywords and all plugins
539 # channel:: channel to join
540 # key:: optional channel key if channel is +s
542 def join(channel, key=nil)
544 sendq "JOIN #{channel} :#{key}"
546 sendq "JOIN #{channel}"
551 def part(channel, message="")
552 sendq "PART #{channel} :#{message}"
555 # attempt to change bot's nick to +name+
561 def mode(channel, mode, target)
562 sendq "MODE #{channel} #{mode} #{target}"
565 # m:: message asking for help
566 # topic:: optional topic help is requested for
567 # respond to online help requests
569 topic = nil if topic == ""
572 helpstr = "help topics: core, auth, keywords"
573 helpstr += @plugins.helptopics
574 helpstr += " (help <topic> for more info)"
577 when /^core\s+(.+)$/i
578 helpstr = corehelp $1
581 when /^auth\s+(.+)$/i
582 helpstr = @auth.help $1
584 helpstr = @keywords.help
585 when /^keywords\s+(.+)$/i
586 helpstr = @keywords.help $1
588 unless(helpstr = @plugins.help(topic))
589 helpstr = "no help for topic #{topic}"
595 # returns a string describing the current status of the bot (uptime etc)
597 secs_up = Time.new - @startup_time
598 uptime = Utils.secs_to_string secs_up
599 # return "Uptime #{uptime}, #{@plugins.length} plugins active, #{@registry.length} items stored in registry, #{@socket.lines_sent} lines sent, #{@socket.lines_received} received."
600 return "Uptime #{uptime}, #{@plugins.length} plugins active, #{@socket.lines_sent} lines sent, #{@socket.lines_received} received."
603 # we'll ping the server every 30 seconds or so, and expect a response
604 # before the next one come around..
605 def start_server_pings
607 return unless @config['server.ping_timeout'] > 0
608 # we want to respond to a hung server within 30 secs or so
609 @ping_timer = @timer.add(30) {
610 @last_ping = Time.now
611 @socket.puts "PING :rbot"
613 @pong_timer = @timer.add(10) {
614 unless @last_ping.nil?
615 diff = Time.now - @last_ping
616 unless diff < @config['server.ping_timeout']
617 debug "no PONG from server for #{diff} seconds, reconnecting"
621 debug "couldn't shutdown connection (already shutdown?)"
624 raise TimeoutError, "no PONG from server in #{diff} seconds"
630 def stop_server_pings
632 # stop existing timers if running
633 unless @ping_timer.nil?
634 @timer.remove @ping_timer
637 unless @pong_timer.nil?
638 @timer.remove @pong_timer
645 # handle help requests for "core" topics
646 def corehelp(topic="")
649 return "quit [<message>] => quit IRC with message <message>"
651 return "restart => completely stop and restart the bot (including reconnect)"
653 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"
655 return "part <channel> => part channel <channel>"
657 return "hide => part all channels"
659 return "save => save current dynamic data and configuration"
661 return "rescan => reload modules and static facts"
663 return "nick <nick> => attempt to change nick to <nick>"
665 return "say <channel>|<nick> <message> => say <message> to <channel> or in private message to <nick>"
667 return "action <channel>|<nick> <message> => does a /me <message> to <channel> or in private message to <nick>"
669 # return "topic <channel> <message> => set topic of <channel> to <message>"
671 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>"
673 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>"
675 return "version => describes software version"
677 return "botsnack => reward #{@nick} for being good"
679 return "hello|hi|hey|yo [#{@nick}] => greet the bot"
681 return "Core help topics: quit, restart, config, join, part, hide, save, rescan, nick, say, action, topic, quiet, talk, version, botsnack, hello"
685 # handle incoming IRC PRIVMSG +m+
690 log "* [#{m.sourcenick}(#{m.sourceaddress})] #{m.message}", m.sourcenick
692 log "* #{m.sourcenick} #{m.message}", m.target
696 log "<#{m.sourcenick}> #{m.message}", m.target
698 log "[#{m.sourcenick}(#{m.sourceaddress})] #{m.message}", m.sourcenick
702 # pass it off to plugins that want to hear everything
703 @plugins.delegate "listen", m
705 if(m.private? && m.message =~ /^\001PING\s+(.+)\001/)
706 notice m.sourcenick, "\001PING #$1\001"
707 log "@ #{m.sourcenick} pinged me"
714 when (/^join\s+(\S+)\s+(\S+)$/i)
715 join $1, $2 if(@auth.allow?("join", m.source, m.replyto))
716 when (/^join\s+(\S+)$/i)
717 join $1 if(@auth.allow?("join", m.source, m.replyto))
719 part m.target if(m.public? && @auth.allow?("join", m.source, m.replyto))
720 when (/^part\s+(\S+)$/i)
721 part $1 if(@auth.allow?("join", m.source, m.replyto))
722 when (/^quit(?:\s+(.*))?$/i)
723 quit $1 if(@auth.allow?("quit", m.source, m.replyto))
724 when (/^restart(?:\s+(.*))?$/i)
725 restart $1 if(@auth.allow?("quit", m.source, m.replyto))
727 join 0 if(@auth.allow?("join", m.source, m.replyto))
729 if(@auth.allow?("config", m.source, m.replyto))
733 when (/^nick\s+(\S+)$/i)
734 nickchg($1) if(@auth.allow?("nick", m.source, m.replyto))
735 when (/^say\s+(\S+)\s+(.*)$/i)
736 say $1, $2 if(@auth.allow?("say", m.source, m.replyto))
737 when (/^action\s+(\S+)\s+(.*)$/i)
738 action $1, $2 if(@auth.allow?("say", m.source, m.replyto))
739 # when (/^topic\s+(\S+)\s+(.*)$/i)
740 # topic $1, $2 if(@auth.allow?("topic", m.source, m.replyto))
741 when (/^mode\s+(\S+)\s+(\S+)\s+(.*)$/i)
742 mode $1, $2, $3 if(@auth.allow?("mode", m.source, m.replyto))
744 say m.replyto, "pong"
746 if(@auth.allow?("config", m.source, m.replyto))
749 m.reply "Rescanning ..."
754 if(auth.allow?("talk", m.source, m.replyto))
756 @channels.each_value {|c| c.quiet = true }
758 when (/^quiet in (\S+)$/i)
760 if(auth.allow?("talk", m.source, m.replyto))
762 where.gsub!(/^here$/, m.target) if m.public?
763 @channels[where].quiet = true if(@channels.has_key?(where))
766 if(auth.allow?("talk", m.source, m.replyto))
767 @channels.each_value {|c| c.quiet = false }
770 when (/^talk in (\S+)$/i)
772 if(auth.allow?("talk", m.source, m.replyto))
773 where.gsub!(/^here$/, m.target) if m.public?
774 @channels[where].quiet = false if(@channels.has_key?(where))
777 when (/^status\??$/i)
778 m.reply status if auth.allow?("status", m.source, m.replyto)
779 when (/^registry stats$/i)
780 if auth.allow?("config", m.source, m.replyto)
781 m.reply @registry.stat.inspect
783 when (/^(help\s+)?config(\s+|$)/)
785 when (/^(version)|(introduce yourself)$/i)
786 say m.replyto, "I'm a v. #{$version} rubybot, (c) Tom Gilbert - http://linuxbrit.co.uk/rbot/"
787 when (/^help(?:\s+(.*))?$/i)
788 say m.replyto, help($1)
789 #TODO move these to a "chatback" plugin
790 when (/^(botsnack|ciggie)$/i)
791 say m.replyto, @lang.get("thanks_X") % m.sourcenick if(m.public?)
792 say m.replyto, @lang.get("thanks") if(m.private?)
793 when (/^(hello|howdy|hola|salut|bonjour|sup|niihau|hey|hi(\W|$)|yo(\W|$)).*/i)
794 say m.replyto, @lang.get("hello_X") % m.sourcenick if(m.public?)
795 say m.replyto, @lang.get("hello") if(m.private?)
798 # stuff to handle when not addressed
800 when (/^\s*(hello|howdy|hola|salut|bonjour|sup|niihau|hey|hi|yo(\W|$))[\s,-.]+#{Regexp.escape(@nick)}$/i)
801 say m.replyto, @lang.get("hello_X") % m.sourcenick
802 when (/^#{Regexp.escape(@nick)}!*$/)
803 say m.replyto, @lang.get("hello_X") % m.sourcenick
810 # log a message. Internal use only.
811 def log_sent(type, where, message)
815 log "-=#{@nick}=- #{message}", where
816 elsif (where =~ /(\S*)!.*/)
817 log "[-=#{where}=-] #{message}", $1
819 log "[-=#{where}=-] #{message}"
823 log "<#{@nick}> #{message}", where
824 elsif (where =~ /^(\S*)!.*$/)
825 log "[msg(#{where})] #{message}", $1
827 log "[msg(#{where})] #{message}", where
833 @channels[m.channel] = IRCChannel.new(m.channel) unless(@channels.has_key?(m.channel))
835 debug "joined channel #{m.channel}"
836 log "@ Joined channel #{m.channel}", m.channel
838 log "@ #{m.sourcenick} joined channel #{m.channel}", m.channel
839 @channels[m.channel].users[m.sourcenick] = Hash.new
840 @channels[m.channel].users[m.sourcenick]["mode"] = ""
843 @plugins.delegate("listen", m)
844 @plugins.delegate("join", m)
849 debug "left channel #{m.channel}"
850 log "@ Left channel #{m.channel} (#{m.message})", m.channel
851 @channels.delete(m.channel)
853 log "@ #{m.sourcenick} left channel #{m.channel} (#{m.message})", m.channel
854 if @channels.has_key?(m.channel)
855 @channels[m.channel].users.delete(m.sourcenick)
857 puts "bug: got part for channel '#{channel}' I didn't think I was in\n"
862 # delegate to plugins
863 @plugins.delegate("listen", m)
864 @plugins.delegate("part", m)
867 # respond to being kicked from a channel
870 debug "kicked from channel #{m.channel}"
871 @channels.delete(m.channel)
872 log "@ You have been kicked from #{m.channel} by #{m.sourcenick} (#{m.message})", m.channel
874 @channels[m.channel].users.delete(m.sourcenick)
875 log "@ #{m.target} has been kicked from #{m.channel} by #{m.sourcenick} (#{m.message})", m.channel
878 @plugins.delegate("listen", m)
879 @plugins.delegate("kick", m)
883 @channels[m.channel] = IRCChannel.new(m.channel) unless(@channels.has_key?(m.channel))
884 @channels[m.channel].topic = m.topic if !m.topic.nil?
885 @channels[m.channel].topic.timestamp = m.timestamp if !m.timestamp.nil?
886 @channels[m.channel].topic.by = m.source if !m.source.nil?
888 debug "topic of channel #{m.channel} is now #{@channels[m.channel].topic}"
891 # delegate a privmsg to auth, keyword or plugin handlers
892 def delegate_privmsg(message)
893 [@auth, @plugins, @keywords].each {|m|
894 break if m.privmsg(message)