]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - lib/rbot/ircbot.rb
logger: fix STDERR recursion
[user/henk/code/ruby/rbot.git] / lib / rbot / ircbot.rb
1 # encoding: UTF-8
2 #-- vim:sw=2:et
3 #++
4 #
5 # :title: rbot core
6
7 require 'thread'
8
9 require 'etc'
10 require 'date'
11 require 'fileutils'
12
13 require 'pp'
14
15 unless Kernel.respond_to? :pretty_inspect
16   def pretty_inspect
17     PP.pp(self, '')
18   end
19   public :pretty_inspect
20 end
21
22 class Exception
23   def pretty_print(q)
24     q.group(1, "#<%s: %s" % [self.class, self.message], ">") {
25       if self.backtrace and not self.backtrace.empty?
26         q.text "\n"
27         q.seplist(self.backtrace, lambda { q.text "\n" } ) { |l| q.text l }
28       end
29     }
30   end
31 end
32
33 class ServerError < RuntimeError
34 end
35
36 # The following global is used for the improved signal handling.
37 $interrupted = 0
38
39 # these first
40 require 'rbot/logger'
41 require 'rbot/rbotconfig'
42 require 'rbot/load-gettext'
43 require 'rbot/config'
44 require 'rbot/irc'
45 require 'rbot/rfc2812'
46 require 'rbot/ircsocket'
47 require 'rbot/botuser'
48 require 'rbot/timer'
49 require 'rbot/registry'
50 require 'rbot/plugins'
51 require 'rbot/message'
52 require 'rbot/language'
53 require 'rbot/httputil'
54
55 module Irc
56
57 # Main bot class, which manages the various components, receives messages,
58 # handles them or passes them to plugins, and contains core functionality.
59 class Bot
60   COPYRIGHT_NOTICE = "(c) Giuseppe Bilotta and the rbot development team"
61   SOURCE_URL = "https://ruby-rbot.org"
62   # the bot's Auth data
63   attr_reader :auth
64
65   # the bot's Config data
66   attr_reader :config
67
68   # the botclass for this bot (determines configdir among other things)
69   attr_reader :botclass
70
71   # used to perform actions periodically (saves configuration once per minute
72   # by default)
73   attr_reader :timer
74
75   # synchronize with this mutex while touching permanent data files:
76   # saving, flushing, cleaning up ...
77   attr_reader :save_mutex
78
79   # bot's Language data
80   attr_reader :lang
81
82   # bot's irc socket
83   # TODO multiserver
84   attr_reader :socket
85
86   # bot's plugins. This is an instance of class Plugins
87   attr_reader :plugins
88
89   # bot's httputil helper object, for fetching resources via http. Sets up
90   # proxies etc as defined by the bot configuration/environment
91   attr_accessor :httputil
92
93   # mechanize agent factory
94   attr_accessor :agent
95
96   # loads and opens new registry databases, used by the plugins
97   attr_accessor :registry_factory
98
99   # web service
100   attr_accessor :webservice
101
102   # server we are connected to
103   # TODO multiserver
104   def server
105     @client.server
106   end
107
108   # bot User in the client/server connection
109   # TODO multiserver
110   def myself
111     @client.user
112   end
113
114   # bot nick in the client/server connection
115   def nick
116     myself.nick
117   end
118
119   # bot channels in the client/server connection
120   def channels
121     myself.channels
122   end
123
124   # nick wanted by the bot. This defaults to the irc.nick config value,
125   # but may be overridden by a manual !nick command
126   def wanted_nick
127     @wanted_nick || config['irc.nick']
128   end
129
130   # set the nick wanted by the bot
131   def wanted_nick=(wn)
132     if wn.nil? or wn.to_s.downcase == config['irc.nick'].downcase
133       @wanted_nick = nil
134     else
135       @wanted_nick = wn.to_s.dup
136     end
137   end
138
139
140   # bot inspection
141   # TODO multiserver
142   def inspect
143     ret = self.to_s[0..-2]
144     ret << ' version=' << $version.inspect
145     ret << ' botclass=' << botclass.inspect
146     ret << ' lang="' << lang.language
147     if defined?(GetText)
148       ret << '/' << locale
149     end
150     ret << '"'
151     ret << ' nick=' << nick.inspect
152     ret << ' server='
153     if server
154       ret << (server.to_s + (socket ?
155         ' [' << socket.server_uri.to_s << ']' : '')).inspect
156       unless server.channels.empty?
157         ret << " channels="
158         ret << server.channels.map { |c|
159           "%s%s" % [c.modes_of(nick).map { |m|
160             server.prefix_for_mode(m)
161           }, c.name]
162         }.inspect
163       end
164     else
165       ret << '(none)'
166     end
167     ret << ' plugins=' << plugins.inspect
168     ret << ">"
169   end
170
171
172   # create a new Bot with botclass +botclass+
173   def initialize(botclass, params = {})
174     # Config for the core bot
175     # TODO should we split socket stuff into ircsocket, etc?
176     Config.register Config::ArrayValue.new('server.list',
177       :default => ['irc://localhost'], :wizard => true,
178       :requires_restart => true,
179       :desc => "List of irc servers rbot should try to connect to. Use comma to separate values. Servers are in format 'server.doma.in:port'. If port is not specified, default value (6667) is used.")
180     Config.register Config::BooleanValue.new('server.ssl',
181       :default => false, :requires_restart => true, :wizard => true,
182       :desc => "Use SSL to connect to this server?")
183     Config.register Config::BooleanValue.new('server.ssl_verify',
184       :default => false, :requires_restart => true,
185       :desc => "Verify the SSL connection?",
186       :wizard => true)
187     Config.register Config::StringValue.new('server.ssl_ca_file',
188       :default => default_ssl_ca_file, :requires_restart => true,
189       :desc => "The CA file used to verify the SSL connection.",
190       :wizard => true)
191     Config.register Config::StringValue.new('server.ssl_ca_path',
192       :default => default_ssl_ca_path, :requires_restart => true,
193       :desc => "Alternativly a directory that includes CA PEM files used to verify the SSL connection.",
194       :wizard => true)
195     Config.register Config::StringValue.new('server.password',
196       :default => false, :requires_restart => true,
197       :desc => "Password for connecting to this server (if required)",
198       :wizard => true)
199     Config.register Config::StringValue.new('server.bindhost',
200       :default => false, :requires_restart => true,
201       :desc => "Specific local host or IP for the bot to bind to (if required)",
202       :wizard => true)
203     Config.register Config::IntegerValue.new('server.reconnect_wait',
204       :default => 5, :validate => Proc.new{|v| v >= 0},
205       :desc => "Seconds to wait before attempting to reconnect, on disconnect")
206     Config.register Config::IntegerValue.new('server.ping_timeout',
207       :default => 30, :validate => Proc.new{|v| v >= 0},
208       :desc => "reconnect if server doesn't respond to PING within this many seconds (set to 0 to disable)")
209     Config.register Config::ArrayValue.new('server.nocolor_modes',
210       :default => ['c'], :wizard => false,
211       :requires_restart => false,
212       :desc => "List of channel modes that require messages to be without colors")
213
214     Config.register Config::StringValue.new('irc.nick', :default => "rbot",
215       :desc => "IRC nickname the bot should attempt to use", :wizard => true,
216       :on_change => Proc.new{|bot, v| bot.sendq "NICK #{v}" })
217     Config.register Config::StringValue.new('irc.name',
218       :default => "Ruby bot", :requires_restart => true,
219       :desc => "IRC realname the bot should use")
220     Config.register Config::BooleanValue.new('irc.name_copyright',
221       :default => true, :requires_restart => true,
222       :desc => "Append copyright notice to bot realname? (please don't disable unless it's really necessary)")
223     Config.register Config::StringValue.new('irc.user', :default => "rbot",
224       :requires_restart => true,
225       :desc => "local user the bot should appear to be", :wizard => true)
226     Config.register Config::ArrayValue.new('irc.join_channels',
227       :default => [], :wizard => true,
228       :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'")
229     Config.register Config::ArrayValue.new('irc.ignore_users',
230       :default => [],
231       :desc => "Which users to ignore input from. This is mainly to avoid bot-wars triggered by creative people")
232     Config.register Config::ArrayValue.new('irc.ignore_channels',
233       :default => [],
234       :desc => "Which channels to ignore input in. This is mainly to turn the bot into a logbot that doesn't interact with users in any way (in the specified channels)")
235
236     Config.register Config::IntegerValue.new('core.save_every',
237       :default => 60, :validate => Proc.new{|v| v >= 0},
238       :on_change => Proc.new { |bot, v|
239         if @save_timer
240           if v > 0
241             @timer.reschedule(@save_timer, v)
242             @timer.unblock(@save_timer)
243           else
244             @timer.block(@save_timer)
245           end
246         else
247           if v > 0
248             @save_timer = @timer.add(v) { bot.save }
249           end
250           # Nothing to do when v == 0
251         end
252       },
253       :desc => "How often the bot should persist all configuration to disk (in case of a server crash, for example)")
254
255     Config.register Config::BooleanValue.new('core.run_as_daemon',
256       :default => false, :requires_restart => true,
257       :desc => "Should the bot run as a daemon?")
258
259     Config.register Config::StringValue.new('log.file',
260       :default => false, :requires_restart => true,
261       :desc => "Name of the logfile to which console messages will be redirected when the bot is run as a daemon")
262     Config.register Config::IntegerValue.new('log.level',
263       :default => 1, :requires_restart => false,
264       :validate => Proc.new { |v| (0..5).include?(v) },
265       :on_change => Proc.new { |bot, v|
266         LoggerManager.instance.set_level(v)
267       },
268       :desc => "The minimum logging level (0=DEBUG,1=INFO,2=WARN,3=ERROR,4=FATAL) for console messages")
269     Config.register Config::IntegerValue.new('log.keep',
270       :default => 1, :requires_restart => true,
271       :validate => Proc.new { |v| v >= 0 },
272       :desc => "How many old console messages logfiles to keep")
273     Config.register Config::IntegerValue.new('log.max_size',
274       :default => 10, :requires_restart => true,
275       :validate => Proc.new { |v| v > 0 },
276       :desc => "Maximum console messages logfile size (in megabytes)")
277
278     Config.register Config::ArrayValue.new('plugins.path',
279       :wizard => true, :default => ['(default)', '(default)/games', '(default)/contrib'],
280       :requires_restart => false,
281       :on_change => Proc.new { |bot, v| bot.setup_plugins_path },
282       :desc => "Where the bot should look for plugins. List multiple directories using commas to separate. Use '(default)' for default prepackaged plugins collection, '(default)/contrib' for prepackaged unsupported plugins collection")
283
284     Config.register Config::EnumValue.new('send.newlines',
285       :values => ['split', 'join'], :default => 'split',
286       :on_change => Proc.new { |bot, v|
287         bot.set_default_send_options :newlines => v.to_sym
288       },
289       :desc => "When set to split, messages with embedded newlines will be sent as separate lines. When set to join, newlines will be replaced by the value of join_with")
290     Config.register Config::StringValue.new('send.join_with',
291       :default => ' ',
292       :on_change => Proc.new { |bot, v|
293         bot.set_default_send_options :join_with => v.dup
294       },
295       :desc => "String used to replace newlines when send.newlines is set to join")
296     Config.register Config::IntegerValue.new('send.max_lines',
297       :default => 5,
298       :validate => Proc.new { |v| v >= 0 },
299       :on_change => Proc.new { |bot, v|
300         bot.set_default_send_options :max_lines => v
301       },
302       :desc => "Maximum number of IRC lines to send for each message (set to 0 for no limit)")
303     Config.register Config::EnumValue.new('send.overlong',
304       :values => ['split', 'truncate'], :default => 'split',
305       :on_change => Proc.new { |bot, v|
306         bot.set_default_send_options :overlong => v.to_sym
307       },
308       :desc => "When set to split, messages which are too long to fit in a single IRC line are split into multiple lines. When set to truncate, long messages are truncated to fit the IRC line length")
309     Config.register Config::StringValue.new('send.split_at',
310       :default => '\s+',
311       :on_change => Proc.new { |bot, v|
312         bot.set_default_send_options :split_at => Regexp.new(v)
313       },
314       :desc => "A regular expression that should match the split points for overlong messages (see send.overlong)")
315     Config.register Config::BooleanValue.new('send.purge_split',
316       :default => true,
317       :on_change => Proc.new { |bot, v|
318         bot.set_default_send_options :purge_split => v
319       },
320       :desc => "Set to true if the splitting boundary (set in send.split_at) should be removed when splitting overlong messages (see send.overlong)")
321     Config.register Config::StringValue.new('send.truncate_text',
322       :default => "#{Reverse}...#{Reverse}",
323       :on_change => Proc.new { |bot, v|
324         bot.set_default_send_options :truncate_text => v.dup
325       },
326       :desc => "When truncating overlong messages (see send.overlong) or when sending too many lines per message (see send.max_lines) replace the end of the last line with this text")
327     Config.register Config::IntegerValue.new('send.penalty_pct',
328       :default => 100,
329       :validate => Proc.new { |v| v >= 0 },
330       :on_change => Proc.new { |bot, v|
331         bot.socket.penalty_pct = v
332       },
333       :desc => "Percentage of IRC penalty to consider when sending messages to prevent being disconnected for excess flood. Set to 0 to disable penalty control.")
334     Config.register Config::StringValue.new('core.db',
335       :default => default_db, :store_default => true,
336       :wizard => true,
337       :validate => Proc.new { |v| Registry::formats.include? v },
338       :requires_restart => true,
339       :desc => "DB adaptor to use for storing the plugin data/registries. Options: " + Registry::formats.join(', '))
340
341     @argv = params[:argv]
342     @run_dir = params[:run_dir] || Dir.pwd
343
344     unless FileTest.directory? Config::coredir
345       error "core directory '#{Config::coredir}' not found, did you setup.rb?"
346       exit 2
347     end
348
349     unless FileTest.directory? Config::datadir
350       error "data directory '#{Config::datadir}' not found, did you setup.rb?"
351       exit 2
352     end
353
354     unless botclass and not botclass.empty?
355       # We want to find a sensible default.
356       # * On POSIX systems we prefer ~/.rbot for the effective uid of the process
357       # * On Windows (at least the NT versions) we want to put our stuff in the
358       #   Application Data folder.
359       # We don't use any particular O/S detection magic, exploiting the fact that
360       # Etc.getpwuid is nil on Windows
361       if Etc.getpwuid(Process::Sys.geteuid)
362         botclass = Etc.getpwuid(Process::Sys.geteuid)[:dir].dup
363       else
364         if ENV.has_key?('APPDATA')
365           botclass = ENV['APPDATA'].dup
366           botclass.gsub!("\\","/")
367         end
368       end
369       botclass = File.join(botclass, ".rbot")
370     end
371     botclass = File.expand_path(botclass)
372     @botclass = botclass.gsub(/\/$/, "")
373
374     repopulate_botclass_directory
375
376     # Time at which the last PING was sent
377     @last_ping = nil
378     # Time at which the last line was RECV'd from the server
379     @last_rec = nil
380
381     @startup_time = Time.new
382
383     begin
384       @config = Config.manager
385       @config.bot_associate(self)
386     rescue Exception => e
387       fatal e
388       exit 2
389     end
390
391     if @config['core.run_as_daemon']
392       $daemonize = true
393     end
394
395     @registry_factory = Registry.new @config['core.db']
396     @registry_factory.migrate_registry_folder(path)
397
398     @logfile = @config['log.file']
399     if @logfile.class != String || @logfile.empty?
400       logfname =  File.basename(botclass).gsub(/^\.+/,'')
401       logfname << ".log"
402       @logfile = File.join(botclass, logfname)
403       debug "Using `#{@logfile}' as debug log"
404     end
405
406     LoggerManager.instance.flush
407
408     # See http://blog.humlab.umu.se/samuel/archives/000107.html
409     # for the backgrounding code
410     if $daemonize
411       begin
412         exit if fork
413         Process.setsid
414         exit if fork
415       rescue NotImplementedError
416         warning "Could not background, fork not supported"
417       rescue SystemExit
418         exit 0
419       rescue Exception => e
420         warning "Could not background. #{e.pretty_inspect}"
421       end
422       Dir.chdir botclass
423       # File.umask 0000                # Ensure sensible umask. Adjust as needed.
424     end
425
426     # setup logger based on bot configuration, if not set from the command line
427     loglevel_set = $opts.has_key?('debug') or $opts.has_key?('loglevel')
428     LoggerManager.instance.set_level(@config['log.level']) unless loglevel_set
429
430     # Set the logfile
431     LoggerManager.instance.set_logfile(@logfile, @config['log.keep'], @config['log.max_size'])
432
433     if $daemonize
434       log "Redirecting standard input/output/error, console logger disabled"
435       LoggerManager.instance.flush
436       LoggerManager.instance.disable_console_logger
437
438       [$stdin, $stdout, $stderr].each do |fd|
439         begin
440           fd.reopen "/dev/null"
441         rescue Errno::ENOENT
442           # On Windows, there's not such thing as /dev/null
443           fd.reopen "NUL"
444         end
445       end
446
447       def $stdout.write(str=nil)
448         log str, 2
449         return str.to_s.size
450       end
451       def $stderr.write(str=nil)
452         if str.to_s.match(/:\d+: warning:/)
453           warning str, 2
454         else
455           error str, 2
456         end
457         return str.to_s.size
458       end
459     end
460
461     File.open($opts['pidfile'] || File.join(@botclass, 'rbot.pid'), 'w') do |pf|
462       pf << "#{$$}\n"
463     end
464
465     @timer = Timer.new
466     @save_mutex = Mutex.new
467     if @config['core.save_every'] > 0
468       @save_timer = @timer.add(@config['core.save_every']) { save }
469     else
470       @save_timer = nil
471     end
472     @quit_mutex = Mutex.new
473
474     @plugins = nil
475     @lang = Language.new(self, @config['core.language'])
476     @httputil = Utils::HttpUtil.new(self)
477
478     begin
479       @auth = Auth::manager
480       @auth.bot_associate(self)
481       # @auth.load("#{botclass}/botusers.yaml")
482     rescue Exception => e
483       fatal e
484       exit 2
485     end
486     @auth.everyone.set_default_permission("*", true)
487     @auth.botowner.password= @config['auth.password']
488
489     @plugins = Plugins::manager
490     @plugins.bot_associate(self)
491     setup_plugins_path()
492
493     if @config['server.name']
494         debug "upgrading configuration (server.name => server.list)"
495         srv_uri = 'irc://' + @config['server.name']
496         srv_uri += ":#{@config['server.port']}" if @config['server.port']
497         @config.items['server.list'.to_sym].set_string(srv_uri)
498         @config.delete('server.name'.to_sym)
499         @config.delete('server.port'.to_sym)
500         debug "server.list is now #{@config['server.list'].inspect}"
501     end
502
503     @socket = Irc::Socket.new(@config['server.list'], @config['server.bindhost'], 
504                               :ssl => @config['server.ssl'],
505                               :ssl_verify => @config['server.ssl_verify'],
506                               :ssl_ca_file => @config['server.ssl_ca_file'],
507                               :ssl_ca_path => @config['server.ssl_ca_path'],
508                               :penalty_pct => @config['send.penalty_pct'])
509     @client = Client.new
510
511     @plugins.scan
512
513     # Channels where we are quiet
514     # Array of channels names where the bot should be quiet
515     # '*' means all channels
516     #
517     @quiet = Set.new
518     # but we always speak here
519     @not_quiet = Set.new
520
521     # the nick we want, if it's different from the irc.nick config value
522     # (e.g. as set by a !nick command)
523     @wanted_nick = nil
524
525     @client[:welcome] = proc {|data|
526       m = WelcomeMessage.new(self, server, data[:source], data[:target], data[:message])
527
528       @plugins.delegate("welcome", m)
529       @plugins.delegate("connect")
530     }
531
532     # TODO the next two @client should go into rfc2812.rb, probably
533     # Since capabs are two-steps processes, server.supports[:capab]
534     # should be a three-state: nil, [], [....]
535     asked_for = { :"identify-msg" => false }
536     @client[:isupport] = proc { |data|
537       if server.supports[:capab] and !asked_for[:"identify-msg"]
538         sendq "CAPAB IDENTIFY-MSG"
539         asked_for[:"identify-msg"] = true
540       end
541     }
542     @client[:datastr] = proc { |data|
543       if data[:text] == "IDENTIFY-MSG"
544         server.capabilities[:"identify-msg"] = true
545       else
546         debug "Not handling RPL_DATASTR #{data[:servermessage]}"
547       end
548     }
549
550     @client[:privmsg] = proc { |data|
551       m = PrivMessage.new(self, server, data[:source], data[:target], data[:message], :handle_id => true)
552       # debug "Message source is #{data[:source].inspect}"
553       # debug "Message target is #{data[:target].inspect}"
554       # debug "Bot is #{myself.inspect}"
555
556       @config['irc.ignore_channels'].each { |channel|
557         if m.target.downcase == channel.downcase
558           m.ignored = true
559           break
560         end
561       }
562       @config['irc.ignore_users'].each { |mask|
563         if m.source.matches?(server.new_netmask(mask))
564           m.ignored = true
565           break
566         end
567       } unless m.ignored
568
569       @plugins.irc_delegate('privmsg', m)
570     }
571     @client[:notice] = proc { |data|
572       message = NoticeMessage.new(self, server, data[:source], data[:target], data[:message], :handle_id => true)
573       # pass it off to plugins that want to hear everything
574       @plugins.irc_delegate "notice", message
575     }
576     @client[:motd] = proc { |data|
577       m = MotdMessage.new(self, server, data[:source], data[:target], data[:motd])
578       @plugins.delegate "motd", m
579     }
580     @client[:nicktaken] = proc { |data|
581       new = "#{data[:nick]}_"
582       nickchg new
583       # If we're setting our nick at connection because our choice was taken,
584       # we have to fix our nick manually, because there will be no NICK message
585       # to inform us that our nick has been changed.
586       if data[:target] == '*'
587         debug "setting my connection nick to #{new}"
588         @client.user.nick = new
589       end
590       @plugins.delegate "nicktaken", data[:nick]
591     }
592     @client[:badnick] = proc {|data|
593       warning "bad nick (#{data[:nick]})"
594     }
595     @client[:ping] = proc {|data|
596       sendq "PONG #{data[:pingid]}"
597     }
598     @client[:pong] = proc {|data|
599       @last_ping = nil
600     }
601     @client[:nick] = proc {|data|
602       # debug "Message source is #{data[:source].inspect}"
603       # debug "Bot is #{myself.inspect}"
604       source = data[:source]
605       old = data[:oldnick]
606       new = data[:newnick]
607       m = NickMessage.new(self, server, source, old, new)
608       m.is_on = data[:is_on]
609       if source == myself
610         debug "my nick is now #{new}"
611       end
612       @plugins.irc_delegate("nick", m)
613     }
614     @client[:quit] = proc {|data|
615       source = data[:source]
616       message = data[:message]
617       m = QuitMessage.new(self, server, source, source, message)
618       m.was_on = data[:was_on]
619       @plugins.irc_delegate("quit", m)
620     }
621     @client[:mode] = proc {|data|
622       m = ModeChangeMessage.new(self, server, data[:source], data[:target], data[:modestring])
623       m.modes = data[:modes]
624       @plugins.delegate "modechange", m
625     }
626     @client[:whois] = proc {|data|
627       source = data[:source]
628       target = server.get_user(data[:whois][:nick])
629       m = WhoisMessage.new(self, server, source, target, data[:whois])
630       @plugins.delegate "whois", m
631     }
632     @client[:list] = proc {|data|
633       source = data[:source]
634       m = ListMessage.new(self, server, source, source, data[:list])
635       @plugins.delegate "irclist", m
636     }
637     @client[:join] = proc {|data|
638       m = JoinMessage.new(self, server, data[:source], data[:channel], data[:message])
639       sendq("MODE #{data[:channel]}", nil, 0) if m.address?
640       @plugins.irc_delegate("join", m)
641       sendq("WHO #{data[:channel]}", data[:channel], 2) if m.address?
642     }
643     @client[:part] = proc {|data|
644       m = PartMessage.new(self, server, data[:source], data[:channel], data[:message])
645       @plugins.irc_delegate("part", m)
646     }
647     @client[:kick] = proc {|data|
648       m = KickMessage.new(self, server, data[:source], data[:target], data[:channel],data[:message])
649       @plugins.irc_delegate("kick", m)
650     }
651     @client[:invite] = proc {|data|
652       m = InviteMessage.new(self, server, data[:source], data[:target], data[:channel])
653       @plugins.irc_delegate("invite", m)
654     }
655     @client[:changetopic] = proc {|data|
656       m = TopicMessage.new(self, server, data[:source], data[:channel], data[:topic])
657       m.info_or_set = :set
658       @plugins.irc_delegate("topic", m)
659     }
660     # @client[:topic] = proc { |data|
661     #   irclog "@ Topic is \"#{data[:topic]}\"", data[:channel]
662     # }
663     @client[:topicinfo] = proc { |data|
664       channel = data[:channel]
665       topic = channel.topic
666       m = TopicMessage.new(self, server, data[:source], channel, topic)
667       m.info_or_set = :info
668       @plugins.irc_delegate("topic", m)
669     }
670     @client[:names] = proc { |data|
671       m = NamesMessage.new(self, server, server, data[:channel])
672       m.users = data[:users]
673       @plugins.delegate "names", m
674     }
675     @client[:banlist] = proc { |data|
676       m = BanlistMessage.new(self, server, server, data[:channel])
677       m.bans = data[:bans]
678       @plugins.delegate "banlist", m
679     }
680     @client[:nosuchtarget] = proc { |data|
681       m = NoSuchTargetMessage.new(self, server, server, data[:target], data[:message])
682       @plugins.delegate "nosuchtarget", m
683     }
684     @client[:error] = proc { |data|
685       raise ServerError, data[:message]
686     }
687     @client[:unknown] = proc { |data|
688       #debug "UNKNOWN: #{data[:serverstring]}"
689       m = UnknownMessage.new(self, server, server, nil, data[:serverstring])
690       @plugins.delegate "unknown_message", m
691     }
692
693     set_default_send_options :newlines => @config['send.newlines'].to_sym,
694       :join_with => @config['send.join_with'].dup,
695       :max_lines => @config['send.max_lines'],
696       :overlong => @config['send.overlong'].to_sym,
697       :split_at => Regexp.new(@config['send.split_at']),
698       :purge_split => @config['send.purge_split'],
699       :truncate_text => @config['send.truncate_text'].dup
700
701     trap_signals
702   end
703
704   # Determine (if possible) a valid path to a CA certificate bundle. 
705   def default_ssl_ca_file
706     [ '/etc/ssl/certs/ca-certificates.crt', # Ubuntu/Debian
707       '/etc/ssl/certs/ca-bundle.crt', # Amazon Linux
708       '/etc/ssl/ca-bundle.pem', # OpenSUSE
709       '/etc/pki/tls/certs/ca-bundle.crt' # Fedora/RHEL
710     ].find do |file|
711       File.readable? file
712     end
713   end
714
715   def default_ssl_ca_path
716     file = default_ssl_ca_file
717     File.dirname file if file
718   end
719
720   # Determine if tokyocabinet is installed, if it is use it as a default.
721   def default_db
722     begin
723       require 'tokyocabinet'
724       return 'tc'
725     rescue LoadError
726       return 'dbm'
727     end
728   end
729
730   def repopulate_botclass_directory
731     template_dir = File.join Config::datadir, 'templates'
732     if FileTest.directory? @botclass
733       # compare the templates dir with the current botclass dir, filling up the
734       # latter with any missing file. Sadly, FileUtils.cp_r doesn't have an
735       # :update option, so we have to do it manually.
736       # Note that we use the */** pattern because we don't want to match
737       # keywords.rbot, which gets deleted on load and would therefore be missing
738       # always
739       missing = Dir.chdir(template_dir) { Dir.glob('*/**') } - Dir.chdir(@botclass) { Dir.glob('*/**') }
740       missing.map do |f|
741         dest = File.join(@botclass, f)
742         FileUtils.mkdir_p(File.dirname(dest))
743         FileUtils.cp File.join(template_dir, f), dest
744       end
745     else
746       log "no #{@botclass} directory found, creating from templates..."
747       if FileTest.exist? @botclass
748         error "file #{@botclass} exists but isn't a directory"
749         exit 2
750       end
751       FileUtils.cp_r template_dir, @botclass
752     end
753   end
754
755   # Return a path under the current botclass by joining the mentioned
756   # components. The components are automatically converted to String
757   def path(*components)
758     File.join(@botclass, *(components.map {|c| c.to_s}))
759   end
760
761   def setup_plugins_path
762     plugdir_default = File.join(Config::datadir, 'plugins')
763     plugdir_local = File.join(@botclass, 'plugins')
764     Dir.mkdir(plugdir_local) unless File.exist?(plugdir_local)
765
766     @plugins.clear_botmodule_dirs
767     @plugins.add_core_module_dir(File.join(Config::coredir, 'utils'))
768     @plugins.add_core_module_dir(Config::coredir)
769     if FileTest.directory? plugdir_local
770       @plugins.add_plugin_dir(plugdir_local)
771     else
772       warning "local plugin location #{plugdir_local} is not a directory"
773     end
774
775     @config['plugins.path'].each do |_|
776         path = _.sub(/^\(default\)/, plugdir_default)
777         @plugins.add_plugin_dir(path)
778     end
779   end
780
781   def set_default_send_options(opts={})
782     # Default send options for NOTICE and PRIVMSG
783     unless defined? @default_send_options
784       @default_send_options = {
785         :queue_channel => nil,      # use default queue channel
786         :queue_ring => nil,         # use default queue ring
787         :newlines => :split,        # or :join
788         :join_with => ' ',          # by default, use a single space
789         :max_lines => 0,          # maximum number of lines to send with a single command
790         :overlong => :split,        # or :truncate
791         # TODO an array of splitpoints would be preferrable for this option:
792         :split_at => /\s+/,         # by default, split overlong lines at whitespace
793         :purge_split => true,       # should the split string be removed?
794         :truncate_text => "#{Reverse}...#{Reverse}"  # text to be appened when truncating
795       }
796     end
797     @default_send_options.update opts unless opts.empty?
798   end
799
800   # checks if we should be quiet on a channel
801   def quiet_on?(channel)
802     ch = channel.downcase
803     return (@quiet.include?('*') && !@not_quiet.include?(ch)) || @quiet.include?(ch)
804   end
805
806   def set_quiet(channel = nil)
807     if channel
808       ch = channel.downcase.dup
809       @not_quiet.delete(ch)
810       @quiet << ch
811     else
812       @quiet.clear
813       @not_quiet.clear
814       @quiet << '*'
815     end
816   end
817
818   def reset_quiet(channel = nil)
819     if channel
820       ch = channel.downcase.dup
821       @quiet.delete(ch)
822       @not_quiet << ch
823     else
824       @quiet.clear
825       @not_quiet.clear
826     end
827   end
828
829   # things to do when we receive a signal
830   def handle_signal(sig)
831     func = case sig
832            when 'SIGHUP'
833              :restart
834            when 'SIGUSR1'
835              :reconnect
836            else
837              :quit
838            end
839     debug "received #{sig}, queueing #{func}"
840     # this is not an interruption if we just need to reconnect
841     $interrupted += 1 unless func == :reconnect
842     self.send(func) unless @quit_mutex.locked?
843     debug "interrupted #{$interrupted} times"
844     if $interrupted >= 3
845       debug "drastic!"
846       exit 2
847     end
848   end
849
850   # trap signals
851   def trap_signals
852     begin
853       %w(SIGINT SIGTERM SIGHUP SIGUSR1).each do |sig|
854         trap(sig) { Thread.new { handle_signal sig } }
855       end
856     rescue ArgumentError => e
857       debug "failed to trap signals (#{e.pretty_inspect}): running on Windows?"
858     rescue Exception => e
859       debug "failed to trap signals: #{e.pretty_inspect}"
860     end
861   end
862
863   # connect the bot to IRC
864   def connect
865     # make sure we don't have any spurious ping checks running
866     # (and initialize the vars if this is the first time we connect)
867     stop_server_pings
868     begin
869       quit if $interrupted > 0
870       @socket.connect
871       @last_rec = Time.now
872     rescue Exception => e
873       uri = @socket.server_uri || '<unknown>'
874       error "failed to connect to IRC server at #{uri}"
875       error e
876       raise
877     end
878     quit if $interrupted > 0
879
880     realname = @config['irc.name'].clone || 'Ruby bot'
881     realname << ' ' + COPYRIGHT_NOTICE if @config['irc.name_copyright']
882
883     @socket.emergency_puts "PASS " + @config['server.password'] if @config['server.password']
884     @socket.emergency_puts "NICK #{@config['irc.nick']}\nUSER #{@config['irc.user']} 4 #{@socket.server_uri.host} :#{realname}"
885     quit if $interrupted > 0
886     myself.nick = @config['irc.nick']
887     myself.user = @config['irc.user']
888   end
889
890   # disconnect the bot from IRC, if connected, and then connect (again)
891   def reconnect(message=nil, too_fast=0)
892     # we will wait only if @last_rec was not nil, i.e. if we were connected or
893     # got disconnected by a network error
894     # if someone wants to manually call disconnect() _and_ reconnect(), they
895     # will have to take care of the waiting themselves
896     will_wait = !!@last_rec
897
898     if @socket.connected?
899       disconnect(message)
900     end
901
902     begin
903       if will_wait
904         log "\n\nDisconnected\n\n"
905
906         quit if $interrupted > 0
907
908         log "\n\nWaiting to reconnect\n\n"
909         sleep @config['server.reconnect_wait']
910         if too_fast > 0
911           tf = too_fast*@config['server.reconnect_wait']
912           tfu = Utils.secs_to_string(tf)
913           log "Will sleep for an extra #{tf}s (#{tfu})"
914           sleep tf
915         end
916       end
917
918       connect
919     rescue SystemExit
920       exit 0
921     rescue Exception => e
922       error e
923       will_wait = true
924       retry
925     end
926   end
927
928   # begin event handling loop
929   def mainloop
930     while true
931       too_fast = 0
932       quit_msg = nil
933       valid_recv = false # did we receive anything (valid) from the server yet?
934       begin
935         reconnect(quit_msg, too_fast)
936         quit if $interrupted > 0
937         valid_recv = false
938         while @socket.connected?
939           quit if $interrupted > 0
940
941           # Wait for messages and process them as they arrive. If nothing is
942           # received, we call the ping_server() method that will PING the
943           # server if appropriate, or raise a TimeoutError if no PONG has been
944           # received in the user-chosen timeout since the last PING sent.
945           if @socket.select(1)
946             break unless reply = @socket.gets
947             @last_rec = Time.now
948             @client.process reply
949             valid_recv = true
950             too_fast = 0
951           else
952             ping_server
953           end
954         end
955
956       # I despair of this. Some of my users get "connection reset by peer"
957       # exceptions that ARENT SocketError's. How am I supposed to handle
958       # that?
959       rescue SystemExit
960         exit 0
961       rescue Errno::ETIMEDOUT, Errno::ECONNABORTED, TimeoutError, SocketError => e
962         error "network exception: #{e.pretty_inspect}"
963         quit_msg = e.to_s
964         too_fast += 10 if valid_recv
965       rescue ServerMessageParseError => e
966         # if the bot tried reconnecting too often, we can get forcefully
967         # disconnected by the server, while still receiving an empty message
968         # wait at least 10 minutes in this case
969         if e.message.empty?
970           oldtf = too_fast
971           too_fast = [too_fast, 300].max
972           too_fast*= 2
973           log "Empty message from server, extra delay multiplier #{oldtf} -> #{too_fast}"
974         end
975         quit_msg = "Unparseable Server Message: #{e.message.inspect}"
976         retry
977       rescue ServerError => e
978         quit_msg = "server ERROR: " + e.message
979         debug quit_msg
980         idx = e.message.index("connect too fast")
981         debug "'connect too fast' @ #{idx}"
982         if idx
983           oldtf = too_fast
984           too_fast += (idx+1)*2
985           log "Reconnecting too fast, extra delay multiplier #{oldtf} -> #{too_fast}"
986         end
987         idx = e.message.index(/a(uto)kill/i)
988         debug "'autokill' @ #{idx}"
989         if idx
990           # we got auto-killed. since we don't have an easy way to tell
991           # if it's permanent or temporary, we just set a rather high
992           # reconnection timeout
993           oldtf = too_fast
994           too_fast += (idx+1)*5
995           log "Killed by server, extra delay multiplier #{oldtf} -> #{too_fast}"
996         end
997         retry
998       rescue Exception => e
999         error "non-net exception: #{e.pretty_inspect}"
1000         quit_msg = e.to_s
1001       rescue => e
1002         fatal "unexpected exception: #{e.pretty_inspect}"
1003         exit 2
1004       end
1005     end
1006   end
1007
1008   # type:: message type
1009   # where:: message target
1010   # message:: message text
1011   # send message +message+ of type +type+ to target +where+
1012   # Type can be PRIVMSG, NOTICE, etc, but those you should really use the
1013   # relevant say() or notice() methods. This one should be used for IRCd
1014   # extensions you want to use in modules.
1015   def sendmsg(original_type, original_where, original_message, options={})
1016
1017     # filter message with sendmsg filters
1018     ds = DataStream.new original_message.to_s.dup,
1019       :type => original_type, :dest => original_where,
1020       :options => @default_send_options.merge(options)
1021     filters = filter_names(:sendmsg)
1022     filters.each do |fname|
1023       debug "filtering #{ds[:text]} with sendmsg filter #{fname}"
1024       ds.merge! filter(self.global_filter_name(fname, :sendmsg), ds)
1025     end
1026
1027     opts = ds[:options]
1028     type = ds[:type]
1029     where = ds[:dest]
1030     filtered = ds[:text]
1031
1032     if defined? WebServiceUser and where.instance_of? WebServiceUser
1033       debug 'sendmsg to web service!'
1034       where.response << filtered
1035       return
1036     end
1037
1038     # For starters, set up appropriate queue channels and rings
1039     mchan = opts[:queue_channel]
1040     mring = opts[:queue_ring]
1041     if mchan
1042       chan = mchan
1043     else
1044       chan = where
1045     end
1046     if mring
1047       ring = mring
1048     else
1049       case where
1050       when User
1051         ring = 1
1052       else
1053         ring = 2
1054       end
1055     end
1056
1057     multi_line = filtered.gsub(/[\r\n]+/, "\n")
1058
1059     # if target is a channel with nocolor modes, strip colours
1060     if where.kind_of?(Channel) and where.mode.any?(*config['server.nocolor_modes'])
1061       multi_line.replace BasicUserMessage.strip_formatting(multi_line)
1062     end
1063
1064     messages = Array.new
1065     case opts[:newlines]
1066     when :join
1067       messages << [multi_line.gsub("\n", opts[:join_with])]
1068     when :split
1069       multi_line.each_line { |line|
1070         line.chomp!
1071         next unless(line.size > 0)
1072         messages << line
1073       }
1074     else
1075       raise "Unknown :newlines option #{opts[:newlines]} while sending #{original_message.inspect}"
1076     end
1077
1078     # The IRC protocol requires that each raw message must be not longer
1079     # than 512 characters. From this length with have to subtract the EOL
1080     # terminators (CR+LF) and the length of ":botnick!botuser@bothost "
1081     # that will be prepended by the server to all of our messages.
1082
1083     # The maximum raw message length we can send is therefore 512 - 2 - 2
1084     # minus the length of our hostmask.
1085
1086     max_len = 508 - myself.fullform.size
1087
1088     # On servers that support IDENTIFY-MSG, we have to subtract 1, because messages
1089     # will have a + or - prepended
1090     if server.capabilities[:"identify-msg"]
1091       max_len -= 1
1092     end
1093
1094     # When splitting the message, we'll be prefixing the following string:
1095     # (e.g. "PRIVMSG #rbot :")
1096     fixed = "#{type} #{where} :"
1097
1098     # And this is what's left
1099     left = max_len - fixed.size
1100
1101     truncate = opts[:truncate_text]
1102     truncate = @default_send_options[:truncate_text] if truncate.size > left
1103     truncate = "" if truncate.size > left
1104
1105     all_lines = messages.map { |line|
1106       if line.size < left
1107         line
1108       else
1109         case opts[:overlong]
1110         when :split
1111           msg = line.dup
1112           sub_lines = Array.new
1113           begin
1114             sub_lines << msg.slice!(0, left)
1115             break if msg.empty?
1116             lastspace = sub_lines.last.rindex(opts[:split_at])
1117             if lastspace
1118               msg.replace sub_lines.last.slice!(lastspace, sub_lines.last.size) + msg
1119               msg.gsub!(/^#{opts[:split_at]}/, "") if opts[:purge_split]
1120             end
1121           end until msg.empty?
1122           sub_lines
1123         when :truncate
1124           line.slice(0, left - truncate.size) << truncate
1125         else
1126           raise "Unknown :overlong option #{opts[:overlong]} while sending #{original_message.inspect}"
1127         end
1128       end
1129     }.flatten
1130
1131     if opts[:max_lines] > 0 and all_lines.length > opts[:max_lines]
1132       lines = all_lines[0...opts[:max_lines]]
1133       new_last = lines.last.slice(0, left - truncate.size) << truncate
1134       lines.last.replace(new_last)
1135     else
1136       lines = all_lines
1137     end
1138
1139     lines.each { |line|
1140       sendq "#{fixed}#{line}", chan, ring
1141       delegate_sent(type, where, line)
1142     }
1143   end
1144
1145   # queue an arbitraty message for the server
1146   def sendq(message="", chan=nil, ring=0)
1147     # temporary
1148     @socket.queue(message, chan, ring)
1149   end
1150
1151   # send a notice message to channel/nick +where+
1152   def notice(where, message, options={})
1153     return if where.kind_of?(Channel) and quiet_on?(where)
1154     sendmsg "NOTICE", where, message, options
1155   end
1156
1157   # say something (PRIVMSG) to channel/nick +where+
1158   def say(where, message, options={})
1159     return if where.kind_of?(Channel) and quiet_on?(where)
1160     sendmsg "PRIVMSG", where, message, options
1161   end
1162
1163   def ctcp_notice(where, command, message, options={})
1164     return if where.kind_of?(Channel) and quiet_on?(where)
1165     sendmsg "NOTICE", where, "\001#{command} #{message}\001", options
1166   end
1167
1168   def ctcp_say(where, command, message, options={})
1169     return if where.kind_of?(Channel) and quiet_on?(where)
1170     sendmsg "PRIVMSG", where, "\001#{command} #{message}\001", options
1171   end
1172
1173   # perform a CTCP action with message +message+ to channel/nick +where+
1174   def action(where, message, options={})
1175     ctcp_say(where, 'ACTION', message, options)
1176   end
1177
1178   # quick way to say "okay" (or equivalent) to +where+
1179   def okay(where)
1180     say where, @lang.get("okay")
1181   end
1182
1183   # set topic of channel +where+ to +topic+
1184   # can also be used to retrieve the topic of channel +where+
1185   # by omitting the last argument
1186   def topic(where, topic=nil)
1187     if topic.nil?
1188       sendq "TOPIC #{where}", where, 2
1189     else
1190       sendq "TOPIC #{where} :#{topic}", where, 2
1191     end
1192   end
1193
1194   def disconnect(message=nil)
1195     message = @lang.get("quit") if (!message || message.empty?)
1196     if @socket.connected?
1197       begin
1198         debug "Clearing socket"
1199         @socket.clearq
1200         debug "Sending quit message"
1201         @socket.emergency_puts "QUIT :#{message}"
1202         debug "Logging quits"
1203         delegate_sent('QUIT', myself, message)
1204         debug "Flushing socket"
1205         @socket.flush
1206       rescue SocketError => e
1207         error "error while disconnecting socket: #{e.pretty_inspect}"
1208       end
1209       debug "Shutting down socket"
1210       @socket.shutdown
1211     end
1212     stop_server_pings
1213     @client.reset
1214   end
1215
1216   # disconnect from the server and cleanup all plugins and modules
1217   def shutdown(message=nil)
1218     @quit_mutex.synchronize do
1219       debug "Shutting down: #{message}"
1220       ## No we don't restore them ... let everything run through
1221       # begin
1222       #   trap("SIGINT", "DEFAULT")
1223       #   trap("SIGTERM", "DEFAULT")
1224       #   trap("SIGHUP", "DEFAULT")
1225       # rescue => e
1226       #   debug "failed to restore signals: #{e.inspect}\nProbably running on windows?"
1227       # end
1228       debug "\tdisconnecting..."
1229       disconnect(message)
1230       debug "\tstopping timer..."
1231       @timer.stop
1232       debug "\tsaving ..."
1233       save
1234       debug "\tcleaning up ..."
1235       @save_mutex.synchronize do
1236         begin
1237           @plugins.cleanup
1238         rescue
1239           debug "\tignoring cleanup error: #{$!}"
1240         end
1241       end
1242       @httputil.cleanup
1243       # debug "\tstopping timers ..."
1244       # @timer.stop
1245       # debug "Closing registries"
1246       # @registry.close
1247       log "rbot quit (#{message})"
1248     end
1249   end
1250
1251   # message:: optional IRC quit message
1252   # quit IRC, shutdown the bot
1253   def quit(message=nil)
1254     begin
1255       shutdown(message)
1256     ensure
1257       exit 0
1258     end
1259   end
1260
1261   # totally shutdown and respawn the bot
1262   def restart(message=nil)
1263     message = _("restarting, back in %{wait}...") % {
1264       :wait => @config['server.reconnect_wait']
1265     } if (!message || message.empty?)
1266     shutdown(message)
1267     sleep @config['server.reconnect_wait']
1268     begin
1269       # now we re-exec
1270       # Note, this fails on Windows
1271       debug "going to exec #{$0} #{@argv.inspect} from #{@run_dir}"
1272       Dir.chdir(@run_dir)
1273       exec($0, *@argv)
1274     rescue Errno::ENOENT
1275       exec("ruby", *(@argv.unshift $0))
1276     rescue Exception => e
1277       $interrupted += 1
1278       raise e
1279     end
1280   end
1281
1282   # call the save method for all or the specified botmodule
1283   #
1284   # :botmodule ::
1285   #   optional botmodule to save
1286   def save(botmodule=nil)
1287     @save_mutex.synchronize do
1288       @plugins.save(botmodule)
1289     end
1290   end
1291
1292   # call the rescan method for all or just the specified botmodule
1293   #
1294   # :botmodule ::
1295   #   instance of the botmodule to rescan
1296   def rescan(botmodule=nil)
1297     debug "\tstopping timer..."
1298     @timer.stop
1299     @save_mutex.synchronize do
1300       # @lang.rescan
1301       @plugins.rescan(botmodule)
1302     end
1303     @timer.start
1304   end
1305
1306   # channel:: channel to join
1307   # key::     optional channel key if channel is +s
1308   # join a channel
1309   def join(channel, key=nil)
1310     if(key)
1311       sendq "JOIN #{channel} :#{key}", channel, 2
1312     else
1313       sendq "JOIN #{channel}", channel, 2
1314     end
1315   end
1316
1317   # part a channel
1318   def part(channel, message="")
1319     sendq "PART #{channel} :#{message}", channel, 2
1320   end
1321
1322   # attempt to change bot's nick to +name+
1323   def nickchg(name)
1324     sendq "NICK #{name}"
1325   end
1326
1327   # changing mode
1328   def mode(channel, mode, target=nil)
1329     sendq "MODE #{channel} #{mode} #{target}", channel, 2
1330   end
1331
1332   # asking whois
1333   def whois(nick, target=nil)
1334     sendq "WHOIS #{target} #{nick}", nil, 0
1335   end
1336
1337   # kicking a user
1338   def kick(channel, user, msg)
1339     sendq "KICK #{channel} #{user} :#{msg}", channel, 2
1340   end
1341
1342   # m::     message asking for help
1343   # topic:: optional topic help is requested for
1344   # respond to online help requests
1345   def help(topic=nil)
1346     topic = nil if topic == ""
1347     case topic
1348     when nil
1349       helpstr = _("help topics: ")
1350       helpstr += @plugins.helptopics
1351       helpstr += _(" (help <topic> for more info)")
1352     else
1353       unless(helpstr = @plugins.help(topic))
1354         helpstr = _("no help for topic %{topic}") % { :topic => topic }
1355       end
1356     end
1357     return helpstr
1358   end
1359
1360   # returns a string describing the current status of the bot (uptime etc)
1361   def status
1362     secs_up = Time.new - @startup_time
1363     uptime = Utils.secs_to_string secs_up
1364     # return "Uptime #{uptime}, #{@plugins.length} plugins active, #{@registry.length} items stored in registry, #{@socket.lines_sent} lines sent, #{@socket.lines_received} received."
1365     return (_("Uptime %{up}, %{plug} plugins active, %{sent} lines sent, %{recv} received.") %
1366              {
1367                :up => uptime, :plug => @plugins.length,
1368                :sent => @socket.lines_sent, :recv => @socket.lines_received
1369              })
1370   end
1371
1372   # We want to respond to a hung server in a timely manner. If nothing was received
1373   # in the user-selected timeout and we haven't PINGed the server yet, we PING
1374   # the server. If the PONG is not received within the user-defined timeout, we
1375   # assume we're in ping timeout and act accordingly.
1376   def ping_server
1377     act_timeout = @config['server.ping_timeout']
1378     return if act_timeout <= 0
1379     now = Time.now
1380     if @last_rec && now > @last_rec + act_timeout
1381       if @last_ping.nil?
1382         # No previous PING pending, send a new one
1383         sendq "PING :rbot"
1384         @last_ping = Time.now
1385       else
1386         diff = now - @last_ping
1387         if diff > act_timeout
1388           debug "no PONG from server in #{diff} seconds, reconnecting"
1389           # the actual reconnect is handled in the main loop:
1390           raise TimeoutError, "no PONG from server in #{diff} seconds"
1391         end
1392       end
1393     end
1394   end
1395
1396   def stop_server_pings
1397     # cancel previous PINGs and reset time of last RECV
1398     @last_ping = nil
1399     @last_rec = nil
1400   end
1401
1402   private
1403
1404   # delegate sent messages
1405   def delegate_sent(type, where, message)
1406     args = [self, server, myself, server.user_or_channel(where.to_s), message]
1407     case type
1408       when "NOTICE"
1409         m = NoticeMessage.new(*args)
1410       when "PRIVMSG"
1411         m = PrivMessage.new(*args)
1412       when "QUIT"
1413         m = QuitMessage.new(*args)
1414         m.was_on = myself.channels
1415     end
1416     @plugins.delegate('sent', m)
1417   end
1418
1419 end
1420
1421 end