]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - lib/rbot/ircbot.rb
2dcede3ea924749201c65661e95958056c229fde
[user/henk/code/ruby/rbot.git] / lib / rbot / ircbot.rb
1 # Copyright (C) 2002 Tom Gilbert.
2 #
3 # Permission is hereby granted, free of charge, to any person obtaining a copy
4 # of this software and associated documentation files (the "Software"), to
5 # deal in the Software without restriction, including without limitation the
6 # rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 # sell copies of the Software, and to permit persons to whom the Software is
8 # furnished to do so, subject to the following conditions:
9 #
10 # The above copyright notice and this permission notice shall be included in
11 # all copies of the Software and its documentation and acknowledgment shall be
12 # given in the documentation and software packages that this Software was
13 # used.
14 #
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 # THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 # IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22 require 'thread'
23 require 'etc'
24 require 'fileutils'
25
26 # these first
27 require 'rbot/rbotconfig'
28 require 'rbot/config'
29 require 'rbot/utils'
30
31 require 'rbot/rfc2812'
32 require 'rbot/keywords'
33 require 'rbot/ircsocket'
34 require 'rbot/auth'
35 require 'rbot/timer'
36 require 'rbot/plugins'
37 require 'rbot/channel'
38 require 'rbot/message'
39 require 'rbot/language'
40 require 'rbot/dbhash'
41 require 'rbot/registry'
42 require 'rbot/httputil'
43
44 module Irc
45
46 # Main bot class, which receives messages, handles them or passes them to
47 # plugins, and stores runtime data
48 class IrcBot
49   # the bot's current nickname
50   attr_reader :nick
51   
52   # the bot's IrcAuth data
53   attr_reader :auth
54   
55   # the bot's BotConfig data
56   attr_reader :config
57   
58   # the botclass for this bot (determines configdir among other things)
59   attr_reader :botclass
60   
61   # used to perform actions periodically (saves configuration once per minute
62   # by default)
63   attr_reader :timer
64   
65   # bot's Language data
66   attr_reader :lang
67
68   # bot's configured addressing prefixes
69   attr_reader :addressing_prefixes
70
71   # channel info for channels the bot is in
72   attr_reader :channels
73
74   # bot's irc socket
75   attr_reader :socket
76
77   # bot's object registry, plugins get an interface to this for persistant
78   # storage (hash interface tied to a bdb file, plugins use Accessors to store
79   # and restore objects in their own namespaces.)
80   attr_reader :registry
81
82   # bot's httputil help object, for fetching resources via http. Sets up
83   # proxies etc as defined by the bot configuration/environment
84   attr_reader :httputil
85
86   # create a new IrcBot with botclass +botclass+
87   def initialize(botclass, params = {})
88     # BotConfig for the core bot
89     BotConfig.register BotConfigStringValue.new('server.name',
90       :default => "localhost", :requires_restart => true,
91       :desc => "What server should the bot connect to?",
92       :wizard => true)
93     BotConfig.register BotConfigIntegerValue.new('server.port',
94       :default => 6667, :type => :integer, :requires_restart => true,
95       :desc => "What port should the bot connect to?", 
96       :validate => Proc.new {|v| v > 0}, :wizard => true)
97     BotConfig.register BotConfigStringValue.new('server.password',
98       :default => false, :requires_restart => true,
99       :desc => "Password for connecting to this server (if required)",
100       :wizard => true)
101     BotConfig.register BotConfigStringValue.new('server.bindhost',
102       :default => false, :requires_restart => true,
103       :desc => "Specific local host or IP for the bot to bind to (if required)",
104       :wizard => true)
105     BotConfig.register BotConfigIntegerValue.new('server.reconnect_wait',
106       :default => 5, :validate => Proc.new{|v| v >= 0},
107       :desc => "Seconds to wait before attempting to reconnect, on disconnect")
108     BotConfig.register BotConfigStringValue.new('irc.nick', :default => "rbot",
109       :desc => "IRC nickname the bot should attempt to use", :wizard => true,
110       :on_change => Proc.new{|bot, v| bot.sendq "NICK #{v}" })
111     BotConfig.register BotConfigStringValue.new('irc.user', :default => "rbot",
112       :requires_restart => true,
113       :desc => "local user the bot should appear to be", :wizard => true)
114     BotConfig.register BotConfigArrayValue.new('irc.join_channels',
115       :default => [], :wizard => true,
116       :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'")
117     BotConfig.register BotConfigIntegerValue.new('core.save_every',
118       :default => 60, :validate => Proc.new{|v| v >= 0},
119       # TODO change timer via on_change proc
120       :desc => "How often the bot should persist all configuration to disk (in case of a server crash, for example")
121     BotConfig.register BotConfigFloatValue.new('server.sendq_delay',
122       :default => 2.0, :validate => Proc.new{|v| v >= 0},
123       :desc => "(flood prevention) the delay between sending messages to the server (in seconds)",
124       :on_change => Proc.new {|bot, v| bot.socket.sendq_delay = v })
125     BotConfig.register BotConfigIntegerValue.new('server.sendq_burst',
126       :default => 4, :validate => Proc.new{|v| v >= 0},
127       :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",
128       :on_change => Proc.new {|bot, v| bot.socket.sendq_burst = v })
129
130     @argv = params[:argv]
131
132     unless FileTest.directory? Config::DATADIR
133       puts "data directory '#{Config::DATADIR}' not found, did you install.rb?"
134       exit 2
135     end
136     
137     botclass = "/home/#{Etc.getlogin}/.rbot" unless botclass
138     @botclass = botclass.gsub(/\/$/, "")
139
140     unless FileTest.directory? botclass
141       puts "no #{botclass} directory found, creating from templates.."
142       if FileTest.exist? botclass
143         puts "Error: file #{botclass} exists but isn't a directory"
144         exit 2
145       end
146       FileUtils.cp_r Config::DATADIR+'/templates', botclass
147     end
148     
149     Dir.mkdir("#{botclass}/logs") unless File.exist?("#{botclass}/logs")
150
151     @startup_time = Time.new
152     @config = BotConfig.new(self)
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']
156     @channels = Hash.new
157     @logs = Hash.new
158     
159     @httputil = Utils::HttpUtil.new(self)
160     @lang = Language::Language.new(@config['core.language'])
161     @keywords = Keywords.new(self)
162     @auth = IrcAuth.new(self)
163
164     Dir.mkdir("#{botclass}/plugins") unless File.exist?("#{botclass}/plugins")
165     @plugins = Plugins::Plugins.new(self, ["#{botclass}/plugins"])
166
167     @socket = IrcSocket.new(@config['server.name'], @config['server.port'], @config['server.bindhost'], @config['server.sendq_delay'], @config['server.sendq_burst'])
168     @nick = @config['irc.nick']
169     if @config['core.address_prefix']
170       @addressing_prefixes = @config['core.address_prefix'].split(" ")
171     else
172       @addressing_prefixes = Array.new
173     end
174     
175     @client = IrcClient.new
176     @client["PRIVMSG"] = proc { |data|
177       message = PrivMessage.new(self, data["SOURCE"], data["TARGET"], data["MESSAGE"])
178       onprivmsg(message)
179     }
180     @client["NOTICE"] = proc { |data|
181       message = NoticeMessage.new(self, data["SOURCE"], data["TARGET"], data["MESSAGE"])
182       # pass it off to plugins that want to hear everything
183       @plugins.delegate "listen", message
184     }
185     @client["MOTD"] = proc { |data|
186       data['MOTD'].each_line { |line|
187         log "MOTD: #{line}", "server"
188       }
189     }
190     @client["NICKTAKEN"] = proc { |data| 
191       nickchg "#{@nick}_"
192     }
193     @client["BADNICK"] = proc {|data| 
194       puts "WARNING, bad nick (#{data['NICK']})"
195     }
196     @client["PING"] = proc {|data|
197       # (jump the queue for pongs)
198       @socket.puts "PONG #{data['PINGID']}"
199     }
200     @client["NICK"] = proc {|data|
201       sourcenick = data["SOURCENICK"]
202       nick = data["NICK"]
203       m = NickMessage.new(self, data["SOURCE"], data["SOURCENICK"], data["NICK"])
204       if(sourcenick == @nick)
205         @nick = nick
206       end
207       @channels.each {|k,v|
208         if(v.users.has_key?(sourcenick))
209           log "@ #{sourcenick} is now known as #{nick}", k
210           v.users[nick] = v.users[sourcenick]
211           v.users.delete(sourcenick)
212         end
213       }
214       @plugins.delegate("listen", m)
215       @plugins.delegate("nick", m)
216     }
217     @client["QUIT"] = proc {|data|
218       source = data["SOURCE"]
219       sourcenick = data["SOURCENICK"]
220       sourceurl = data["SOURCEADDRESS"]
221       message = data["MESSAGE"]
222       m = QuitMessage.new(self, data["SOURCE"], data["SOURCENICK"], data["MESSAGE"])
223       if(data["SOURCENICK"] =~ /#{@nick}/i)
224       else
225         @channels.each {|k,v|
226           if(v.users.has_key?(sourcenick))
227             log "@ Quit: #{sourcenick}: #{message}", k
228             v.users.delete(sourcenick)
229           end
230         }
231       end
232       @plugins.delegate("listen", m)
233       @plugins.delegate("quit", m)
234     }
235     @client["MODE"] = proc {|data|
236       source = data["SOURCE"]
237       sourcenick = data["SOURCENICK"]
238       sourceurl = data["SOURCEADDRESS"]
239       channel = data["CHANNEL"]
240       targets = data["TARGETS"]
241       modestring = data["MODESTRING"]
242       log "@ Mode #{modestring} #{targets} by #{sourcenick}", channel
243     }
244     @client["WELCOME"] = proc {|data|
245       log "joined server #{data['SOURCE']} as #{data['NICK']}", "server"
246       debug "I think my nick is #{@nick}, server thinks #{data['NICK']}"
247       if data['NICK'] && data['NICK'].length > 0
248         @nick = data['NICK']
249       end
250       if(@config['irc.quser'])
251         # TODO move this to a plugin
252         debug "authing with Q using  #{@config['quakenet.user']} #{@config['quakenet.auth']}"
253         @socket.puts "PRIVMSG Q@CServe.quakenet.org :auth #{@config['quakenet.user']} #{@config['quakenet.auth']}"
254       end
255
256       @config['irc.join_channels'].each {|c|
257         debug "autojoining channel #{c}"
258         if(c =~ /^(\S+)\s+(\S+)$/i)
259           join $1, $2
260         else
261           join c if(c)
262         end
263       }
264     }
265     @client["JOIN"] = proc {|data|
266       m = JoinMessage.new(self, data["SOURCE"], data["CHANNEL"], data["MESSAGE"])
267       onjoin(m)
268     }
269     @client["PART"] = proc {|data|
270       m = PartMessage.new(self, data["SOURCE"], data["CHANNEL"], data["MESSAGE"])
271       onpart(m)
272     }
273     @client["KICK"] = proc {|data|
274       m = KickMessage.new(self, data["SOURCE"], data["TARGET"],data["CHANNEL"],data["MESSAGE"]) 
275       onkick(m)
276     }
277     @client["INVITE"] = proc {|data|
278       if(data["TARGET"] =~ /^#{@nick}$/i)
279         join data["CHANNEL"] if (@auth.allow?("join", data["SOURCE"], data["SOURCENICK"]))
280       end
281     }
282     @client["CHANGETOPIC"] = proc {|data|
283       channel = data["CHANNEL"]
284       sourcenick = data["SOURCENICK"]
285       topic = data["TOPIC"]
286       timestamp = data["UNIXTIME"] || Time.now.to_i
287       if(sourcenick == @nick)
288         log "@ I set topic \"#{topic}\"", channel
289       else
290         log "@ #{sourcenick} set topic \"#{topic}\"", channel
291       end
292       m = TopicMessage.new(self, data["SOURCE"], data["CHANNEL"], timestamp, data["TOPIC"])
293
294       ontopic(m)
295       @plugins.delegate("listen", m)
296       @plugins.delegate("topic", m)
297     }
298     @client["TOPIC"] = @client["TOPICINFO"] = proc {|data|
299       channel = data["CHANNEL"]
300       m = TopicMessage.new(self, data["SOURCE"], data["CHANNEL"], data["UNIXTIME"], data["TOPIC"])
301         ontopic(m)
302     }
303     @client["NAMES"] = proc {|data|
304       channel = data["CHANNEL"]
305       users = data["USERS"]
306       unless(@channels[channel])
307         puts "bug: got names for channel '#{channel}' I didn't think I was in\n"
308         exit 2
309       end
310       @channels[channel].users.clear
311       users.each {|u|
312         @channels[channel].users[u[0].sub(/^[@&~+]/, '')] = ["mode", u[1]]
313       }
314     }
315     @client["UNKNOWN"] = proc {|data|
316       debug "UNKNOWN: #{data['SERVERSTRING']}"
317     }
318   end
319
320   # connect the bot to IRC
321   def connect
322     trap("SIGTERM") { quit }
323     trap("SIGHUP") { quit }
324     trap("SIGINT") { quit }
325     begin
326       @socket.connect
327       rescue => e
328       raise "failed to connect to IRC server at #{@config['server.name']} #{@config['server.port']}: " + e
329     end
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"
332   end
333
334   # begin event handling loop
335   def mainloop
336     while true
337       connect
338       @timer.start
339       
340       begin
341         while true
342           if @socket.select
343             break unless reply = @socket.gets
344             @client.process reply
345           end
346         end
347       rescue TimeoutError, SocketError => e
348         puts "network exception: connection closed: #{e}"
349         puts e.backtrace.join("\n")
350         @socket.close # now we reconnect
351       rescue => e # TODO be selective, only grab Network errors
352         puts "unexpected exception: connection closed: #{e}"
353         puts e.backtrace.join("\n")
354         exit 2
355       end
356       
357       puts "disconnected"
358       @channels.clear
359       @socket.clearq
360       
361       puts "waiting to reconnect"
362       sleep @config['server.reconnect_wait']
363     end
364   end
365   
366   # type:: message type
367   # where:: message target
368   # message:: message text
369   # send message +message+ of type +type+ to target +where+
370   # Type can be PRIVMSG, NOTICE, etc, but those you should really use the
371   # relevant say() or notice() methods. This one should be used for IRCd
372   # extensions you want to use in modules.
373   def sendmsg(type, where, message)
374     # limit it 440 chars + CRLF.. so we have to split long lines
375     left = 440 - type.length - where.length - 3
376     begin
377       if(left >= message.length)
378         sendq("#{type} #{where} :#{message}")
379         log_sent(type, where, message)
380         return
381       end
382       line = message.slice!(0, left)
383       lastspace = line.rindex(/\s+/)
384       if(lastspace)
385         message = line.slice!(lastspace, line.length) + message
386         message.gsub!(/^\s+/, "")
387       end
388       sendq("#{type} #{where} :#{line}")
389       log_sent(type, where, line)
390     end while(message.length > 0)
391   end
392
393   def sendq(message="")
394     # temporary
395     @socket.queue(message)
396   end
397
398   # send a notice message to channel/nick +where+
399   def notice(where, message)
400     message.each_line { |line|
401       line.chomp!
402       next unless(line.length > 0)
403       sendmsg("NOTICE", where, line)
404     }
405   end
406
407   # say something (PRIVMSG) to channel/nick +where+
408   def say(where, message)
409     message.to_s.gsub(/[\r\n]+/, "\n").each_line { |line|
410       line.chomp!
411       next unless(line.length > 0)
412       unless((where =~ /^#/) && (@channels.has_key?(where) && @channels[where].quiet))
413         sendmsg("PRIVMSG", where, line)
414       end
415     }
416   end
417
418   # perform a CTCP action with message +message+ to channel/nick +where+
419   def action(where, message)
420     sendq("PRIVMSG #{where} :\001ACTION #{message}\001")
421     if(where =~ /^#/)
422       log "* #{@nick} #{message}", where
423     elsif (where =~ /^(\S*)!.*$/)
424          log "* #{@nick}[#{where}] #{message}", $1
425     else
426          log "* #{@nick}[#{where}] #{message}", where
427     end
428   end
429
430   # quick way to say "okay" (or equivalent) to +where+
431   def okay(where)
432     say where, @lang.get("okay")
433   end
434
435   # log message +message+ to a file determined by +where+. +where+ can be a
436   # channel name, or a nick for private message logging
437   def log(message, where="server")
438     message.chomp!
439     stamp = Time.now.strftime("%Y/%m/%d %H:%M:%S")
440     unless(@logs.has_key?(where))
441       @logs[where] = File.new("#{@botclass}/logs/#{where}", "a")
442       @logs[where].sync = true
443     end
444     @logs[where].puts "[#{stamp}] #{message}"
445     #debug "[#{stamp}] <#{where}> #{message}"
446   end
447   
448   # set topic of channel +where+ to +topic+
449   def topic(where, topic)
450     sendq "TOPIC #{where} :#{topic}"
451   end
452
453   def shutdown(message = nil)
454     trap("SIGTERM", "DEFAULT")
455     trap("SIGHUP", "DEFAULT")
456     trap("SIGINT", "DEFAULT")
457     message = @lang.get("quit") if (message.nil? || message.empty?)
458     @socket.clearq
459     save
460     @plugins.cleanup
461     @channels.each_value {|v|
462       log "@ quit (#{message})", v.name
463     }
464     @socket.puts "QUIT :#{message}"
465     @socket.flush
466     @socket.shutdown
467     @registry.close
468     puts "rbot quit (#{message})"
469   end
470   
471   # message:: optional IRC quit message
472   # quit IRC, shutdown the bot
473   def quit(message=nil)
474     shutdown(message)
475     exit 0
476   end
477
478   # totally shutdown and respawn the bot
479   def restart
480     shutdown("restarting, back in #{@config['server.reconnect_wait']}...")
481     sleep @config['server.reconnect_wait']
482     # now we re-exec
483     exec($0, *@argv)
484   end
485
486   # call the save method for bot's config, keywords, auth and all plugins
487   def save
488     @registry.flush
489     @config.save
490     @keywords.save
491     @auth.save
492     @plugins.save
493   end
494
495   # call the rescan method for the bot's lang, keywords and all plugins
496   def rescan
497     @lang.rescan
498     @plugins.rescan
499     @keywords.rescan
500   end
501   
502   # channel:: channel to join
503   # key::     optional channel key if channel is +s
504   # join a channel
505   def join(channel, key=nil)
506     if(key)
507       sendq "JOIN #{channel} :#{key}"
508     else
509       sendq "JOIN #{channel}"
510     end
511   end
512
513   # part a channel
514   def part(channel, message="")
515     sendq "PART #{channel} :#{message}"
516   end
517
518   # attempt to change bot's nick to +name+
519   # FIXME
520   # if rbot is already taken, this happens:
521   #   <giblet> rbot_, nick rbot
522   #   --- rbot_ is now known as rbot__
523   # he should of course just keep his existing nick and report the error :P
524   def nickchg(name)
525       sendq "NICK #{name}"
526   end
527
528   # changing mode
529   def mode(channel, mode, target)
530       sendq "MODE #{channel} #{mode} #{target}"
531   end
532   
533   # m::     message asking for help
534   # topic:: optional topic help is requested for
535   # respond to online help requests
536   def help(topic=nil)
537     topic = nil if topic == ""
538     case topic
539     when nil
540       helpstr = "help topics: core, auth, keywords"
541       helpstr += @plugins.helptopics
542       helpstr += " (help <topic> for more info)"
543     when /^core$/i
544       helpstr = corehelp
545     when /^core\s+(.+)$/i
546       helpstr = corehelp $1
547     when /^auth$/i
548       helpstr = @auth.help
549     when /^auth\s+(.+)$/i
550       helpstr = @auth.help $1
551     when /^keywords$/i
552       helpstr = @keywords.help
553     when /^keywords\s+(.+)$/i
554       helpstr = @keywords.help $1
555     else
556       unless(helpstr = @plugins.help(topic))
557         helpstr = "no help for topic #{topic}"
558       end
559     end
560     return helpstr
561   end
562
563   def status
564     secs_up = Time.new - @startup_time
565     uptime = Utils.secs_to_string secs_up
566     return "Uptime #{uptime}, #{@plugins.length} plugins active, #{@registry.length} items stored in registry, #{@socket.lines_sent} lines sent, #{@socket.lines_received} received."
567   end
568
569
570   private
571
572   # handle help requests for "core" topics
573   def corehelp(topic="")
574     case topic
575       when "quit"
576         return "quit [<message>] => quit IRC with message <message>"
577       when "restart"
578         return "restart => completely stop and restart the bot (including reconnect)"
579       when "join"
580         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"
581       when "part"
582         return "part <channel> => part channel <channel>"
583       when "hide"
584         return "hide => part all channels"
585       when "save"
586         return "save => save current dynamic data and configuration"
587       when "rescan"
588         return "rescan => reload modules and static facts"
589       when "nick"
590         return "nick <nick> => attempt to change nick to <nick>"
591       when "say"
592         return "say <channel>|<nick> <message> => say <message> to <channel> or in private message to <nick>"
593       when "action"
594         return "action <channel>|<nick> <message> => does a /me <message> to <channel> or in private message to <nick>"
595       when "topic"
596         return "topic <channel> <message> => set topic of <channel> to <message>"
597       when "quiet"
598         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>"
599       when "talk"
600         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>"
601       when "version"
602         return "version => describes software version"
603       when "botsnack"
604         return "botsnack => reward #{@nick} for being good"
605       when "hello"
606         return "hello|hi|hey|yo [#{@nick}] => greet the bot"
607       else
608         return "Core help topics: quit, restart, config, join, part, hide, save, rescan, nick, say, action, topic, quiet, talk, version, botsnack, hello"
609     end
610   end
611
612   # handle incoming IRC PRIVMSG +m+
613   def onprivmsg(m)
614     # log it first
615     if(m.action?)
616       if(m.private?)
617         log "* [#{m.sourcenick}(#{m.sourceaddress})] #{m.message}", m.sourcenick
618       else
619         log "* #{m.sourcenick} #{m.message}", m.target
620       end
621     else
622       if(m.public?)
623         log "<#{m.sourcenick}> #{m.message}", m.target
624       else
625         log "[#{m.sourcenick}(#{m.sourceaddress})] #{m.message}", m.sourcenick
626       end
627     end
628
629     # pass it off to plugins that want to hear everything
630     @plugins.delegate "listen", m
631
632     if(m.private? && m.message =~ /^\001PING\s+(.+)\001/)
633       notice m.sourcenick, "\001PING #$1\001"
634       log "@ #{m.sourcenick} pinged me"
635       return
636     end
637
638     if(m.address?)
639       case m.message
640         when (/^join\s+(\S+)\s+(\S+)$/i)
641           join $1, $2 if(@auth.allow?("join", m.source, m.replyto))
642         when (/^join\s+(\S+)$/i)
643           join $1 if(@auth.allow?("join", m.source, m.replyto))
644         when (/^part$/i)
645           part m.target if(m.public? && @auth.allow?("join", m.source, m.replyto))
646         when (/^part\s+(\S+)$/i)
647           part $1 if(@auth.allow?("join", m.source, m.replyto))
648         when (/^quit(?:\s+(.*))?$/i)
649           quit $1 if(@auth.allow?("quit", m.source, m.replyto))
650         when (/^restart$/i)
651           restart if(@auth.allow?("quit", m.source, m.replyto))
652         when (/^hide$/i)
653           join 0 if(@auth.allow?("join", m.source, m.replyto))
654         when (/^save$/i)
655           if(@auth.allow?("config", m.source, m.replyto))
656             save
657             m.okay
658           end
659         when (/^nick\s+(\S+)$/i)
660           nickchg($1) if(@auth.allow?("nick", m.source, m.replyto))
661         when (/^say\s+(\S+)\s+(.*)$/i)
662           say $1, $2 if(@auth.allow?("say", m.source, m.replyto))
663         when (/^action\s+(\S+)\s+(.*)$/i)
664           action $1, $2 if(@auth.allow?("say", m.source, m.replyto))
665         when (/^topic\s+(\S+)\s+(.*)$/i)
666           topic $1, $2 if(@auth.allow?("topic", m.source, m.replyto))
667         when (/^mode\s+(\S+)\s+(\S+)\s+(.*)$/i)
668           mode $1, $2, $3 if(@auth.allow?("mode", m.source, m.replyto))
669         when (/^ping$/i)
670           say m.replyto, "pong"
671         when (/^rescan$/i)
672           if(@auth.allow?("config", m.source, m.replyto))
673             m.okay
674             rescan
675           end
676         when (/^quiet$/i)
677           if(auth.allow?("talk", m.source, m.replyto))
678             m.okay
679             @channels.each_value {|c| c.quiet = true }
680           end
681         when (/^quiet in (\S+)$/i)
682           where = $1
683           if(auth.allow?("talk", m.source, m.replyto))
684             m.okay
685             where.gsub!(/^here$/, m.target) if m.public?
686             @channels[where].quiet = true if(@channels.has_key?(where))
687           end
688         when (/^talk$/i)
689           if(auth.allow?("talk", m.source, m.replyto))
690             @channels.each_value {|c| c.quiet = false }
691             m.okay
692           end
693         when (/^talk in (\S+)$/i)
694           where = $1
695           if(auth.allow?("talk", m.source, m.replyto))
696             where.gsub!(/^here$/, m.target) if m.public?
697             @channels[where].quiet = false if(@channels.has_key?(where))
698             m.okay
699           end
700         when (/^status\??$/i)
701           m.reply status if auth.allow?("status", m.source, m.replyto)
702         when (/^registry stats$/i)
703           if auth.allow?("config", m.source, m.replyto)
704             m.reply @registry.stat.inspect
705           end
706         when (/^(help\s+)?config(\s+|$)/)
707           @config.privmsg(m)
708         when (/^(version)|(introduce yourself)$/i)
709           say m.replyto, "I'm a v. #{$version} rubybot, (c) Tom Gilbert - http://linuxbrit.co.uk/rbot/"
710         when (/^help(?:\s+(.*))?$/i)
711           say m.replyto, help($1)
712           #TODO move these to a "chatback" plugin
713         when (/^(botsnack|ciggie)$/i)
714           say m.replyto, @lang.get("thanks_X") % m.sourcenick if(m.public?)
715           say m.replyto, @lang.get("thanks") if(m.private?)
716         when (/^(hello|howdy|hola|salut|bonjour|sup|niihau|hey|hi(\W|$)|yo(\W|$)).*/i)
717           say m.replyto, @lang.get("hello_X") % m.sourcenick if(m.public?)
718           say m.replyto, @lang.get("hello") if(m.private?)
719         else
720           delegate_privmsg(m)
721       end
722     else
723       # stuff to handle when not addressed
724       case m.message
725         when (/^\s*(hello|howdy|hola|salut|bonjour|sup|niihau|hey|hi(\W|$)|yo(\W|$))[\s,-.]+#{@nick}$/i)
726           say m.replyto, @lang.get("hello_X") % m.sourcenick
727         when (/^#{@nick}!*$/)
728           say m.replyto, @lang.get("hello_X") % m.sourcenick
729         else
730           @keywords.privmsg(m)
731       end
732     end
733   end
734
735   # log a message. Internal use only.
736   def log_sent(type, where, message)
737     case type
738       when "NOTICE"
739         if(where =~ /^#/)
740           log "-=#{@nick}=- #{message}", where
741         elsif (where =~ /(\S*)!.*/)
742              log "[-=#{where}=-] #{message}", $1
743         else
744              log "[-=#{where}=-] #{message}"
745         end
746       when "PRIVMSG"
747         if(where =~ /^#/)
748           log "<#{@nick}> #{message}", where
749         elsif (where =~ /^(\S*)!.*$/)
750           log "[msg(#{where})] #{message}", $1
751         else
752           log "[msg(#{where})] #{message}", where
753         end
754     end
755   end
756
757   def onjoin(m)
758     @channels[m.channel] = IRCChannel.new(m.channel) unless(@channels.has_key?(m.channel))
759     if(m.address?)
760       debug "joined channel #{m.channel}"
761       log "@ Joined channel #{m.channel}", m.channel
762     else
763       log "@ #{m.sourcenick} joined channel #{m.channel}", m.channel
764       @channels[m.channel].users[m.sourcenick] = Hash.new
765       @channels[m.channel].users[m.sourcenick]["mode"] = ""
766     end
767
768     @plugins.delegate("listen", m)
769     @plugins.delegate("join", m)
770   end
771
772   def onpart(m)
773     if(m.address?)
774       debug "left channel #{m.channel}"
775       log "@ Left channel #{m.channel} (#{m.message})", m.channel
776       @channels.delete(m.channel)
777     else
778       log "@ #{m.sourcenick} left channel #{m.channel} (#{m.message})", m.channel
779       @channels[m.channel].users.delete(m.sourcenick)
780     end
781     
782     # delegate to plugins
783     @plugins.delegate("listen", m)
784     @plugins.delegate("part", m)
785   end
786
787   # respond to being kicked from a channel
788   def onkick(m)
789     if(m.address?)
790       debug "kicked from channel #{m.channel}"
791       @channels.delete(m.channel)
792       log "@ You have been kicked from #{m.channel} by #{m.sourcenick} (#{m.message})", m.channel
793     else
794       @channels[m.channel].users.delete(m.sourcenick)
795       log "@ #{m.target} has been kicked from #{m.channel} by #{m.sourcenick} (#{m.message})", m.channel
796     end
797
798     @plugins.delegate("listen", m)
799     @plugins.delegate("kick", m)
800   end
801
802   def ontopic(m)
803     @channels[m.channel] = IRCChannel.new(m.channel) unless(@channels.has_key?(m.channel))
804     @channels[m.channel].topic = m.topic if !m.topic.nil?
805     @channels[m.channel].topic.timestamp = m.timestamp if !m.timestamp.nil?
806     @channels[m.channel].topic.by = m.source if !m.source.nil?
807
808           debug "topic of channel #{m.channel} is now #{@channels[m.channel].topic}"
809   end
810
811   # delegate a privmsg to auth, keyword or plugin handlers
812   def delegate_privmsg(message)
813     [@auth, @plugins, @keywords].each {|m|
814       break if m.privmsg(message)
815     }
816   end
817
818 end
819
820 end