5 $debug = false unless $debug
6 $daemonize = false unless $daemonize
8 def rawlog(code="", message=nil)
9 if !code || code.empty?
12 c = code.to_s[0,1].upcase + ":"
14 stamp = Time.now.strftime("%Y/%m/%d %H:%M:%S")
15 message.to_s.each_line { |l|
16 $stdout.puts "#{c} [#{stamp}] #{l}"
26 log("\n=== #{botclass} session ended ===") if $daemonize
29 def debug(message=nil)
30 rawlog("D", message) if $debug
33 def warning(message=nil)
37 def error(message=nil)
41 # The following global is used for the improved signal handling.
45 require 'rbot/rbotconfig'
49 require 'rbot/rfc2812'
50 require 'rbot/keywords'
51 require 'rbot/ircsocket'
54 require 'rbot/plugins'
55 require 'rbot/channel'
56 require 'rbot/message'
57 require 'rbot/language'
59 require 'rbot/registry'
60 require 'rbot/httputil'
64 # Main bot class, which manages the various components, receives messages,
65 # handles them or passes them to plugins, and contains core functionality.
67 # the bot's current nickname
70 # the bot's IrcAuth data
73 # the bot's BotConfig data
76 # the botclass for this bot (determines configdir among other things)
79 # used to perform actions periodically (saves configuration once per minute
86 # capabilities info for the server
87 attr_reader :capabilities
89 # channel info for channels the bot is in
95 # bot's object registry, plugins get an interface to this for persistant
96 # storage (hash interface tied to a bdb file, plugins use Accessors to store
97 # and restore objects in their own namespaces.)
100 # bot's httputil help object, for fetching resources via http. Sets up
101 # proxies etc as defined by the bot configuration/environment
102 attr_reader :httputil
104 # create a new IrcBot with botclass +botclass+
105 def initialize(botclass, params = {})
106 # BotConfig for the core bot
107 # TODO should we split socket stuff into ircsocket, etc?
108 BotConfig.register BotConfigStringValue.new('server.name',
109 :default => "localhost", :requires_restart => true,
110 :desc => "What server should the bot connect to?",
112 BotConfig.register BotConfigIntegerValue.new('server.port',
113 :default => 6667, :type => :integer, :requires_restart => true,
114 :desc => "What port should the bot connect to?",
115 :validate => Proc.new {|v| v > 0}, :wizard => true)
116 BotConfig.register BotConfigStringValue.new('server.password',
117 :default => false, :requires_restart => true,
118 :desc => "Password for connecting to this server (if required)",
120 BotConfig.register BotConfigStringValue.new('server.bindhost',
121 :default => false, :requires_restart => true,
122 :desc => "Specific local host or IP for the bot to bind to (if required)",
124 BotConfig.register BotConfigIntegerValue.new('server.reconnect_wait',
125 :default => 5, :validate => Proc.new{|v| v >= 0},
126 :desc => "Seconds to wait before attempting to reconnect, on disconnect")
127 BotConfig.register BotConfigFloatValue.new('server.sendq_delay',
128 :default => 2.0, :validate => Proc.new{|v| v >= 0},
129 :desc => "(flood prevention) the delay between sending messages to the server (in seconds)",
130 :on_change => Proc.new {|bot, v| bot.socket.sendq_delay = v })
131 BotConfig.register BotConfigIntegerValue.new('server.sendq_burst',
132 :default => 4, :validate => Proc.new{|v| v >= 0},
133 :desc => "(flood prevention) max lines to burst to the server before throttling. Most ircd's allow bursts of up 5 lines",
134 :on_change => Proc.new {|bot, v| bot.socket.sendq_burst = v })
135 BotConfig.register BotConfigStringValue.new('server.byterate',
136 :default => "400/2", :validate => Proc.new{|v| v.match(/\d+\/\d/)},
137 :desc => "(flood prevention) max bytes/seconds rate to send the server. Most ircd's have limits of 512 bytes/2 seconds",
138 :on_change => Proc.new {|bot, v| bot.socket.byterate = v })
139 BotConfig.register BotConfigIntegerValue.new('server.ping_timeout',
140 :default => 10, :validate => Proc.new{|v| v >= 0},
141 :on_change => Proc.new {|bot, v| bot.start_server_pings},
142 :desc => "reconnect if server doesn't respond to PING within this many seconds (set to 0 to disable)")
144 BotConfig.register BotConfigStringValue.new('irc.nick', :default => "rbot",
145 :desc => "IRC nickname the bot should attempt to use", :wizard => true,
146 :on_change => Proc.new{|bot, v| bot.sendq "NICK #{v}" })
147 BotConfig.register BotConfigStringValue.new('irc.user', :default => "rbot",
148 :requires_restart => true,
149 :desc => "local user the bot should appear to be", :wizard => true)
150 BotConfig.register BotConfigArrayValue.new('irc.join_channels',
151 :default => [], :wizard => true,
152 :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'")
153 BotConfig.register BotConfigArrayValue.new('irc.ignore_users',
155 :desc => "Which users to ignore input from. This is mainly to avoid bot-wars triggered by creative people")
157 BotConfig.register BotConfigIntegerValue.new('core.save_every',
158 :default => 60, :validate => Proc.new{|v| v >= 0},
159 # TODO change timer via on_change proc
160 :desc => "How often the bot should persist all configuration to disk (in case of a server crash, for example)")
161 # BotConfig.register BotConfigBooleanValue.new('core.debug',
162 # :default => false, :requires_restart => true,
163 # :on_change => Proc.new { |v|
164 # debug ((v ? "Enabling" : "Disabling") + " debug output.")
166 # debug (($debug ? "Enabled" : "Disabled") + " debug output.")
168 # :desc => "Should the bot produce debug output?")
169 BotConfig.register BotConfigBooleanValue.new('core.run_as_daemon',
170 :default => false, :requires_restart => true,
171 :desc => "Should the bot run as a daemon?")
172 BotConfig.register BotConfigStringValue.new('core.logfile',
173 :default => false, :requires_restart => true,
174 :desc => "Name of the logfile to which console messages will be redirected when the bot is run as a daemon")
176 @argv = params[:argv]
178 unless FileTest.directory? Config::datadir
179 error "data directory '#{Config::datadir}' not found, did you setup.rb?"
183 botclass = "#{Etc.getpwuid(Process::Sys.geteuid)[:dir]}/.rbot" unless botclass
184 #botclass = "#{ENV['HOME']}/.rbot" unless botclass
185 botclass = File.expand_path(botclass)
186 @botclass = botclass.gsub(/\/$/, "")
188 unless FileTest.directory? botclass
189 log "no #{botclass} directory found, creating from templates.."
190 if FileTest.exist? botclass
191 error "file #{botclass} exists but isn't a directory"
194 FileUtils.cp_r Config::datadir+'/templates', botclass
197 Dir.mkdir("#{botclass}/logs") unless File.exist?("#{botclass}/logs")
198 Dir.mkdir("#{botclass}/registry") unless File.exist?("#{botclass}/registry")
203 @startup_time = Time.new
204 @config = BotConfig.new(self)
205 # background self after botconfig has a chance to run wizard
206 @logfile = @config['core.logfile']
207 if @logfile.class!=String || @logfile.empty?
208 @logfile = File.basename(botclass)+".log"
210 if @config['core.run_as_daemon']
213 # See http://blog.humlab.umu.se/samuel/archives/000107.html
214 # for the backgrounding code
220 rescue NotImplementedError
221 warning "Could not background, fork not supported"
223 warning "Could not background. #{e.inspect}"
226 # File.umask 0000 # Ensure sensible umask. Adjust as needed.
227 log "Redirecting standard input/output/error"
229 STDIN.reopen "/dev/null"
231 # On Windows, there's not such thing as /dev/null
234 STDOUT.reopen @logfile, "a"
236 log "\n=== #{botclass} session started ==="
239 @timer = Timer::Timer.new(1.0) # only need per-second granularity
240 @registry = BotRegistry.new self
241 @timer.add(@config['core.save_every']) { save } if @config['core.save_every']
244 @httputil = Utils::HttpUtil.new(self)
245 @lang = Language::Language.new(@config['core.language'])
246 @keywords = Keywords.new(self)
247 @auth = IrcAuth.new(self)
249 Dir.mkdir("#{botclass}/plugins") unless File.exist?("#{botclass}/plugins")
250 @plugins = Plugins::Plugins.new(self, ["#{botclass}/plugins"])
252 @socket = IrcSocket.new(@config['server.name'], @config['server.port'], @config['server.bindhost'], @config['server.sendq_delay'], @config['server.sendq_burst'])
253 @nick = @config['irc.nick']
255 @client = IrcClient.new
256 @client[:isupport] = proc { |data|
258 sendq "CAPAB IDENTIFY-MSG"
261 @client[:datastr] = proc { |data|
263 if data[:text] == "IDENTIFY-MSG"
264 @capabilities["identify-msg".to_sym] = true
266 debug "Not handling RPL_DATASTR #{data[:servermessage]}"
269 @client[:privmsg] = proc { |data|
270 message = PrivMessage.new(self, data[:source], data[:target], data[:message])
273 @client[:notice] = proc { |data|
274 message = NoticeMessage.new(self, data[:source], data[:target], data[:message])
275 # pass it off to plugins that want to hear everything
276 @plugins.delegate "listen", message
278 @client[:motd] = proc { |data|
279 data[:motd].each_line { |line|
280 irclog "MOTD: #{line}", "server"
283 @client[:nicktaken] = proc { |data|
284 nickchg "#{data[:nick]}_"
285 @plugins.delegate "nicktaken", data[:nick]
287 @client[:badnick] = proc {|data|
288 warning "bad nick (#{data[:nick]})"
290 @client[:ping] = proc {|data|
291 @socket.queue "PONG #{data[:pingid]}"
293 @client[:pong] = proc {|data|
296 @client[:nick] = proc {|data|
297 sourcenick = data[:sourcenick]
299 m = NickMessage.new(self, data[:source], data[:sourcenick], data[:nick])
300 if(sourcenick == @nick)
301 debug "my nick is now #{nick}"
304 @channels.each {|k,v|
305 if(v.users.has_key?(sourcenick))
306 irclog "@ #{sourcenick} is now known as #{nick}", k
307 v.users[nick] = v.users[sourcenick]
308 v.users.delete(sourcenick)
311 @plugins.delegate("listen", m)
312 @plugins.delegate("nick", m)
314 @client[:quit] = proc {|data|
315 source = data[:source]
316 sourcenick = data[:sourcenick]
317 sourceurl = data[:sourceaddress]
318 message = data[:message]
319 m = QuitMessage.new(self, data[:source], data[:sourcenick], data[:message])
320 if(data[:sourcenick] =~ /#{Regexp.escape(@nick)}/i)
322 @channels.each {|k,v|
323 if(v.users.has_key?(sourcenick))
324 irclog "@ Quit: #{sourcenick}: #{message}", k
325 v.users.delete(sourcenick)
329 @plugins.delegate("listen", m)
330 @plugins.delegate("quit", m)
332 @client[:mode] = proc {|data|
333 source = data[:source]
334 sourcenick = data[:sourcenick]
335 sourceurl = data[:sourceaddress]
336 channel = data[:channel]
337 targets = data[:targets]
338 modestring = data[:modestring]
339 irclog "@ Mode #{modestring} #{targets} by #{sourcenick}", channel
341 @client[:welcome] = proc {|data|
342 irclog "joined server #{data[:source]} as #{data[:nick]}", "server"
343 debug "I think my nick is #{@nick}, server thinks #{data[:nick]}"
344 if data[:nick] && data[:nick].length > 0
348 @plugins.delegate("connect")
350 @config['irc.join_channels'].each {|c|
351 debug "autojoining channel #{c}"
352 if(c =~ /^(\S+)\s+(\S+)$/i)
359 @client[:join] = proc {|data|
360 m = JoinMessage.new(self, data[:source], data[:channel], data[:message])
363 @client[:part] = proc {|data|
364 m = PartMessage.new(self, data[:source], data[:channel], data[:message])
367 @client[:kick] = proc {|data|
368 m = KickMessage.new(self, data[:source], data[:target],data[:channel],data[:message])
371 @client[:invite] = proc {|data|
372 if(data[:target] =~ /^#{Regexp.escape(@nick)}$/i)
373 join data[:channel] if (@auth.allow?("join", data[:source], data[:sourcenick]))
376 @client[:changetopic] = proc {|data|
377 channel = data[:channel]
378 sourcenick = data[:sourcenick]
380 timestamp = data[:unixtime] || Time.now.to_i
381 if(sourcenick == @nick)
382 irclog "@ I set topic \"#{topic}\"", channel
384 irclog "@ #{sourcenick} set topic \"#{topic}\"", channel
386 m = TopicMessage.new(self, data[:source], data[:channel], timestamp, data[:topic])
389 @plugins.delegate("listen", m)
390 @plugins.delegate("topic", m)
392 @client[:topic] = @client[:topicinfo] = proc {|data|
393 channel = data[:channel]
394 m = TopicMessage.new(self, data[:source], data[:channel], data[:unixtime], data[:topic])
397 @client[:names] = proc {|data|
398 channel = data[:channel]
400 unless(@channels[channel])
401 warning "got names for channel '#{channel}' I didn't think I was in\n"
404 @channels[channel].users.clear
406 @channels[channel].users[u[0].sub(/^[@&~+]/, '')] = ["mode", u[1]]
408 @plugins.delegate "names", data[:channel], data[:users]
410 @client[:unknown] = proc {|data|
411 #debug "UNKNOWN: #{data[:serverstring]}"
412 irclog data[:serverstring], ".unknown"
417 debug "received #{sig}, queueing quit"
419 debug "interrupted #{$interrupted} times"
424 elsif $interrupted >= 3
430 # connect the bot to IRC
433 trap("SIGINT") { got_sig("SIGINT") }
434 trap("SIGTERM") { got_sig("SIGTERM") }
435 trap("SIGHUP") { got_sig("SIGHUP") }
436 rescue ArgumentError => e
437 debug "failed to trap signals (#{e.inspect}): running on Windows?"
439 debug "failed to trap signals: #{e.inspect}"
442 quit if $interrupted > 0
445 raise e.class, "failed to connect to IRC server at #{@config['server.name']} #{@config['server.port']}: " + e
447 @socket.emergency_puts "PASS " + @config['server.password'] if @config['server.password']
448 @socket.emergency_puts "NICK #{@nick}\nUSER #{@config['irc.user']} 4 #{@config['server.name']} :Ruby bot. (c) Tom Gilbert"
449 @capabilities = Hash.new
453 # begin event handling loop
457 quit if $interrupted > 0
461 while @socket.connected?
463 break unless reply = @socket.gets
464 @client.process reply
466 quit if $interrupted > 0
469 # I despair of this. Some of my users get "connection reset by peer"
470 # exceptions that ARENT SocketError's. How am I supposed to handle
475 rescue Errno::ETIMEDOUT, TimeoutError, SocketError => e
476 error "network exception: #{e.class}: #{e}"
477 debug e.backtrace.join("\n")
478 rescue BDB::Fatal => e
479 error "fatal bdb error: #{e.class}: #{e}"
480 error e.backtrace.join("\n")
482 restart("Oops, we seem to have registry problems ...")
483 rescue Exception => e
484 error "non-net exception: #{e.class}: #{e}"
485 error e.backtrace.join("\n")
487 error "unexpected exception: #{e.class}: #{e}"
488 error e.backtrace.join("\n")
495 if @socket.connected?
502 quit if $interrupted > 0
504 log "waiting to reconnect"
505 sleep @config['server.reconnect_wait']
509 # type:: message type
510 # where:: message target
511 # message:: message text
512 # send message +message+ of type +type+ to target +where+
513 # Type can be PRIVMSG, NOTICE, etc, but those you should really use the
514 # relevant say() or notice() methods. This one should be used for IRCd
515 # extensions you want to use in modules.
516 def sendmsg(type, where, message, chan=nil, ring=0)
517 # limit it according to the byterate, splitting the message
518 # taking into consideration the actual message length
519 # and all the extra stuff
520 # TODO allow something to do for commands that produce too many messages
521 # TODO example: math 10**10000
522 left = @socket.bytes_per - type.length - where.length - 4
524 if(left >= message.length)
525 sendq "#{type} #{where} :#{message}", chan, ring
526 log_sent(type, where, message)
529 line = message.slice!(0, left)
530 lastspace = line.rindex(/\s+/)
532 message = line.slice!(lastspace, line.length) + message
533 message.gsub!(/^\s+/, "")
535 sendq "#{type} #{where} :#{line}", chan, ring
536 log_sent(type, where, line)
537 end while(message.length > 0)
540 # queue an arbitraty message for the server
541 def sendq(message="", chan=nil, ring=0)
543 @socket.queue(message, chan, ring)
546 # send a notice message to channel/nick +where+
547 def notice(where, message, mchan=nil, mring=-1)
562 message.each_line { |line|
564 next unless(line.length > 0)
565 sendmsg "NOTICE", where, line, chan, ring
569 # say something (PRIVMSG) to channel/nick +where+
570 def say(where, message, mchan="", mring=-1)
585 message.to_s.gsub(/[\r\n]+/, "\n").each_line { |line|
587 next unless(line.length > 0)
588 unless((where =~ /^#/) && (@channels.has_key?(where) && @channels[where].quiet))
589 sendmsg "PRIVMSG", where, line, chan, ring
594 # perform a CTCP action with message +message+ to channel/nick +where+
595 def action(where, message, mchan="", mring=-1)
610 sendq "PRIVMSG #{where} :\001ACTION #{message}\001", chan, ring
612 irclog "* #{@nick} #{message}", where
613 elsif (where =~ /^(\S*)!.*$/)
614 irclog "* #{@nick}[#{where}] #{message}", $1
616 irclog "* #{@nick}[#{where}] #{message}", where
620 # quick way to say "okay" (or equivalent) to +where+
622 say where, @lang.get("okay")
625 # log IRC-related message +message+ to a file determined by +where+.
626 # +where+ can be a channel name, or a nick for private message logging
627 def irclog(message, where="server")
628 message = message.chomp
629 stamp = Time.now.strftime("%Y/%m/%d %H:%M:%S")
630 where = where.gsub(/[:!?$*()\/\\<>|"']/, "_")
631 unless(@logs.has_key?(where))
632 @logs[where] = File.new("#{@botclass}/logs/#{where}", "a")
633 @logs[where].sync = true
635 @logs[where].puts "[#{stamp}] #{message}"
636 #debug "[#{stamp}] <#{where}> #{message}"
639 # set topic of channel +where+ to +topic+
640 def topic(where, topic)
641 sendq "TOPIC #{where} :#{topic}", where, 2
644 # disconnect from the server and cleanup all plugins and modules
645 def shutdown(message = nil)
646 debug "Shutting down ..."
647 ## No we don't restore them ... let everything run through
649 # trap("SIGINT", "DEFAULT")
650 # trap("SIGTERM", "DEFAULT")
651 # trap("SIGHUP", "DEFAULT")
653 # debug "failed to restore signals: #{e.inspect}\nProbably running on windows?"
655 message = @lang.get("quit") if (message.nil? || message.empty?)
656 if @socket.connected?
657 debug "Clearing socket"
659 debug "Sending quit message"
660 @socket.emergency_puts "QUIT :#{message}"
661 debug "Flushing socket"
663 debug "Shutting down socket"
666 debug "Logging quits"
667 @channels.each_value {|v|
668 irclog "@ quit (#{message})", v.name
674 # debug "Closing registries"
676 debug "Cleaning up the db environment"
678 log "rbot quit (#{message})"
681 # message:: optional IRC quit message
682 # quit IRC, shutdown the bot
683 def quit(message=nil)
692 # totally shutdown and respawn the bot
693 def restart(message = false)
694 msg = message ? message : "restarting, back in #{@config['server.reconnect_wait']}..."
696 sleep @config['server.reconnect_wait']
698 # Note, this fails on Windows
702 # call the save method for bot's config, keywords, auth and all plugins
711 # call the rescan method for the bot's lang, keywords and all plugins
718 # channel:: channel to join
719 # key:: optional channel key if channel is +s
721 def join(channel, key=nil)
723 sendq "JOIN #{channel} :#{key}", channel, 2
725 sendq "JOIN #{channel}", channel, 2
730 def part(channel, message="")
731 sendq "PART #{channel} :#{message}", channel, 2
734 # attempt to change bot's nick to +name+
740 def mode(channel, mode, target)
741 sendq "MODE #{channel} #{mode} #{target}", channel, 2
744 # m:: message asking for help
745 # topic:: optional topic help is requested for
746 # respond to online help requests
748 topic = nil if topic == ""
751 helpstr = "help topics: core, auth, keywords"
752 helpstr += @plugins.helptopics
753 helpstr += " (help <topic> for more info)"
756 when /^core\s+(.+)$/i
757 helpstr = corehelp $1
760 when /^auth\s+(.+)$/i
761 helpstr = @auth.help $1
763 helpstr = @keywords.help
764 when /^keywords\s+(.+)$/i
765 helpstr = @keywords.help $1
767 unless(helpstr = @plugins.help(topic))
768 helpstr = "no help for topic #{topic}"
774 # returns a string describing the current status of the bot (uptime etc)
776 secs_up = Time.new - @startup_time
777 uptime = Utils.secs_to_string secs_up
778 # return "Uptime #{uptime}, #{@plugins.length} plugins active, #{@registry.length} items stored in registry, #{@socket.lines_sent} lines sent, #{@socket.lines_received} received."
779 return "Uptime #{uptime}, #{@plugins.length} plugins active, #{@socket.lines_sent} lines sent, #{@socket.lines_received} received."
782 # we'll ping the server every 30 seconds or so, and expect a response
783 # before the next one come around..
784 def start_server_pings
786 return unless @config['server.ping_timeout'] > 0
787 # we want to respond to a hung server within 30 secs or so
788 @ping_timer = @timer.add(30) {
789 @last_ping = Time.now
790 @socket.queue "PING :rbot"
792 @pong_timer = @timer.add(10) {
793 unless @last_ping.nil?
794 diff = Time.now - @last_ping
795 unless diff < @config['server.ping_timeout']
796 debug "no PONG from server for #{diff} seconds, reconnecting"
800 debug "couldn't shutdown connection (already shutdown?)"
803 raise TimeoutError, "no PONG from server in #{diff} seconds"
809 def stop_server_pings
811 # stop existing timers if running
812 unless @ping_timer.nil?
813 @timer.remove @ping_timer
816 unless @pong_timer.nil?
817 @timer.remove @pong_timer
824 # handle help requests for "core" topics
825 def corehelp(topic="")
828 return "quit [<message>] => quit IRC with message <message>"
830 return "restart => completely stop and restart the bot (including reconnect)"
832 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"
834 return "part <channel> => part channel <channel>"
836 return "hide => part all channels"
838 return "save => save current dynamic data and configuration"
840 return "rescan => reload modules and static facts"
842 return "nick <nick> => attempt to change nick to <nick>"
844 return "say <channel>|<nick> <message> => say <message> to <channel> or in private message to <nick>"
846 return "action <channel>|<nick> <message> => does a /me <message> to <channel> or in private message to <nick>"
848 # return "topic <channel> <message> => set topic of <channel> to <message>"
850 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>"
852 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>"
854 return "version => describes software version"
856 return "botsnack => reward #{@nick} for being good"
858 return "hello|hi|hey|yo [#{@nick}] => greet the bot"
860 return "Core help topics: quit, restart, config, join, part, hide, save, rescan, nick, say, action, topic, quiet, talk, version, botsnack, hello"
864 # handle incoming IRC PRIVMSG +m+
869 irclog "* [#{m.sourcenick}(#{m.sourceaddress})] #{m.message}", m.sourcenick
871 irclog "* #{m.sourcenick} #{m.message}", m.target
875 irclog "<#{m.sourcenick}> #{m.message}", m.target
877 irclog "[#{m.sourcenick}(#{m.sourceaddress})] #{m.message}", m.sourcenick
881 @config['irc.ignore_users'].each { |mask| return if Irc.netmaskmatch(mask,m.source) }
883 # pass it off to plugins that want to hear everything
884 @plugins.delegate "listen", m
886 if(m.private? && m.message =~ /^\001PING\s+(.+)\001/)
887 notice m.sourcenick, "\001PING #$1\001"
888 irclog "@ #{m.sourcenick} pinged me"
895 when (/^join\s+(\S+)\s+(\S+)$/i)
896 join $1, $2 if(@auth.allow?("join", m.source, m.replyto))
897 when (/^join\s+(\S+)$/i)
898 join $1 if(@auth.allow?("join", m.source, m.replyto))
900 part m.target if(m.public? && @auth.allow?("join", m.source, m.replyto))
901 when (/^part\s+(\S+)$/i)
902 part $1 if(@auth.allow?("join", m.source, m.replyto))
903 when (/^quit(?:\s+(.*))?$/i)
904 quit $1 if(@auth.allow?("quit", m.source, m.replyto))
905 when (/^restart(?:\s+(.*))?$/i)
906 restart $1 if(@auth.allow?("quit", m.source, m.replyto))
908 join 0 if(@auth.allow?("join", m.source, m.replyto))
910 if(@auth.allow?("config", m.source, m.replyto))
914 when (/^nick\s+(\S+)$/i)
915 nickchg($1) if(@auth.allow?("nick", m.source, m.replyto))
916 when (/^say\s+(\S+)\s+(.*)$/i)
917 say $1, $2 if(@auth.allow?("say", m.source, m.replyto))
918 when (/^action\s+(\S+)\s+(.*)$/i)
919 action $1, $2 if(@auth.allow?("say", m.source, m.replyto))
920 # when (/^topic\s+(\S+)\s+(.*)$/i)
921 # topic $1, $2 if(@auth.allow?("topic", m.source, m.replyto))
922 when (/^mode\s+(\S+)\s+(\S+)\s+(.*)$/i)
923 mode $1, $2, $3 if(@auth.allow?("mode", m.source, m.replyto))
925 say m.replyto, "pong"
927 if(@auth.allow?("config", m.source, m.replyto))
930 m.reply "Rescanning ..."
935 if(auth.allow?("talk", m.source, m.replyto))
937 @channels.each_value {|c| c.quiet = true }
939 when (/^quiet in (\S+)$/i)
941 if(auth.allow?("talk", m.source, m.replyto))
943 where.gsub!(/^here$/, m.target) if m.public?
944 @channels[where].quiet = true if(@channels.has_key?(where))
947 if(auth.allow?("talk", m.source, m.replyto))
948 @channels.each_value {|c| c.quiet = false }
951 when (/^talk in (\S+)$/i)
953 if(auth.allow?("talk", m.source, m.replyto))
954 where.gsub!(/^here$/, m.target) if m.public?
955 @channels[where].quiet = false if(@channels.has_key?(where))
958 when (/^status\??$/i)
959 m.reply status if auth.allow?("status", m.source, m.replyto)
960 when (/^registry stats$/i)
961 if auth.allow?("config", m.source, m.replyto)
962 m.reply @registry.stat.inspect
964 when (/^(help\s+)?config(\s+|$)/)
966 when (/^(version)|(introduce yourself)$/i)
967 say m.replyto, "I'm a v. #{$version} rubybot, (c) Tom Gilbert - http://linuxbrit.co.uk/rbot/"
968 when (/^help(?:\s+(.*))?$/i)
969 say m.replyto, help($1)
970 #TODO move these to a "chatback" plugin
971 when (/^(botsnack|ciggie)$/i)
972 say m.replyto, @lang.get("thanks_X") % m.sourcenick if(m.public?)
973 say m.replyto, @lang.get("thanks") if(m.private?)
974 when (/^(hello|howdy|hola|salut|bonjour|sup|niihau|hey|hi(\W|$)|yo(\W|$)).*/i)
975 say m.replyto, @lang.get("hello_X") % m.sourcenick if(m.public?)
976 say m.replyto, @lang.get("hello") if(m.private?)
979 # stuff to handle when not addressed
981 when (/^\s*(hello|howdy|hola|salut|bonjour|sup|niihau|hey|hi|yo(\W|$))[\s,-.]+#{Regexp.escape(@nick)}$/i)
982 say m.replyto, @lang.get("hello_X") % m.sourcenick
983 when (/^#{Regexp.escape(@nick)}!*$/)
984 say m.replyto, @lang.get("hello_X") % m.sourcenick
991 # log a message. Internal use only.
992 def log_sent(type, where, message)
996 irclog "-=#{@nick}=- #{message}", where
997 elsif (where =~ /(\S*)!.*/)
998 irclog "[-=#{where}=-] #{message}", $1
1000 irclog "[-=#{where}=-] #{message}"
1004 irclog "<#{@nick}> #{message}", where
1005 elsif (where =~ /^(\S*)!.*$/)
1006 irclog "[msg(#{where})] #{message}", $1
1008 irclog "[msg(#{where})] #{message}", where
1014 @channels[m.channel] = IRCChannel.new(m.channel) unless(@channels.has_key?(m.channel))
1016 debug "joined channel #{m.channel}"
1017 irclog "@ Joined channel #{m.channel}", m.channel
1019 irclog "@ #{m.sourcenick} joined channel #{m.channel}", m.channel
1020 @channels[m.channel].users[m.sourcenick] = Hash.new
1021 @channels[m.channel].users[m.sourcenick]["mode"] = ""
1024 @plugins.delegate("listen", m)
1025 @plugins.delegate("join", m)
1030 debug "left channel #{m.channel}"
1031 irclog "@ Left channel #{m.channel} (#{m.message})", m.channel
1032 @channels.delete(m.channel)
1034 irclog "@ #{m.sourcenick} left channel #{m.channel} (#{m.message})", m.channel
1035 if @channels.has_key?(m.channel)
1036 @channels[m.channel].users.delete(m.sourcenick)
1038 warning "got part for channel '#{channel}' I didn't think I was in\n"
1043 # delegate to plugins
1044 @plugins.delegate("listen", m)
1045 @plugins.delegate("part", m)
1048 # respond to being kicked from a channel
1051 debug "kicked from channel #{m.channel}"
1052 @channels.delete(m.channel)
1053 irclog "@ You have been kicked from #{m.channel} by #{m.sourcenick} (#{m.message})", m.channel
1055 @channels[m.channel].users.delete(m.sourcenick)
1056 irclog "@ #{m.target} has been kicked from #{m.channel} by #{m.sourcenick} (#{m.message})", m.channel
1059 @plugins.delegate("listen", m)
1060 @plugins.delegate("kick", m)
1064 @channels[m.channel] = IRCChannel.new(m.channel) unless(@channels.has_key?(m.channel))
1065 @channels[m.channel].topic = m.topic if !m.topic.nil?
1066 @channels[m.channel].topic.timestamp = m.timestamp if !m.timestamp.nil?
1067 @channels[m.channel].topic.by = m.source if !m.source.nil?
1069 debug "topic of channel #{m.channel} is now #{@channels[m.channel].topic}"
1072 # delegate a privmsg to auth, keyword or plugin handlers
1073 def delegate_privmsg(message)
1074 [@auth, @plugins, @keywords].each {|m|
1075 break if m.privmsg(message)