4 # :title: rbot IRC logging facilities
6 # Author:: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
7 # Copyright:: (C) 2008 Giuseppe Bilotta
10 class IrcLogModule < CoreBotModule
15 Dir.mkdir("#{@bot.botclass}/logs") unless File.exist?("#{@bot.botclass}/logs")
18 # log IRC-related message +message+ to a file determined by +where+.
19 # +where+ can be a channel name, or a nick for private message logging
20 def irclog(message, where="server")
21 message = message.chomp
22 stamp = Time.now.strftime("%Y/%m/%d %H:%M:%S")
23 if where.class <= Server
26 where_str = where.downcase.gsub(/[:!?$*()\/\\<>|"']/, "_")
28 unless(@logs.has_key?(where_str))
29 f = File.new("#{@bot.botclass}/logs/#{where_str}", "a")
31 f.puts "[#{stamp}] @ Log started by #{@bot.myself.nick}"
34 @logs[where_str].puts "[#{stamp}] #{message}"
35 #debug "[#{stamp}] <#{where}> #{message}"
41 irclog "-#{m.source}- #{m.message}", m.target
43 irclog "<#{m.source}> #{m.message}", m.target
46 irclog "@ quit (#{m.message})", ch
52 irclog "joined server #{m.server} as #{m.target}", "server"
58 method = 'log_message'
60 method = 'log_' + m.class.name.downcase.match(/^irc::(\w+)message$/).captures.first
62 if self.respond_to?(method)
63 self.__send__(method, m)
65 warning "unhandled logging for #{m.pretty_inspect} (no such method #{method})"
72 who = m.private? ? "me" : m.target
73 logtarget = m.private? ? m.source : m.target
77 irclog "* #{m.source} #{m.logmessage}", m.target
79 irclog "* #{m.source}(#{m.sourceaddress}) #{m.logmessage}", m.source
82 irclog "@ #{m.source} asked #{who} about version info", logtarget
84 irclog "@ #{m.source} asked #{who} about source info", logtarget
86 irclog "@ #{m.source} pinged #{who}", logtarget
88 irclog "@ #{m.source} asked #{who} what time it is", logtarget
90 irclog "@ #{m.source} asked #{who} about #{[m.ctcp, m.message].join(' ')}", logtarget
94 irclog "<#{m.source}> #{m.logmessage}", m.target
96 irclog "<#{m.source}(#{m.sourceaddress})> #{m.logmessage}", m.source
103 irclog "-#{m.source}(#{m.sourceaddress})- #{m.logmessage}", m.source
105 irclog "-#{m.source}- #{m.logmessage}", m.target
110 m.message.each_line { |line|
111 irclog "MOTD: #{line}", "server"
117 irclog "@ #{m.oldnick} is now known as #{m.newnick}", ch
123 irclog "@ Quit: #{m.source}: #{m.logmessage}", ch
128 irclog "@ Mode #{m.logmessage} by #{m.source}", m.target
133 debug "joined channel #{m.channel}"
134 irclog "@ Joined channel #{m.channel}", m.channel
136 irclog "@ #{m.source} joined channel #{m.channel}", m.channel
142 debug "left channel #{m.channel}"
143 irclog "@ Left channel #{m.channel} (#{m.logmessage})", m.channel
145 irclog "@ #{m.source} left channel #{m.channel} (#{m.logmessage})", m.channel
151 debug "kicked from channel #{m.channel}"
152 irclog "@ You have been kicked from #{m.channel} by #{m.source} (#{m.logmessage})", m.channel
154 irclog "@ #{m.target} has been kicked from #{m.channel} by #{m.source} (#{m.logmessage})", m.channel
165 if m.source == @bot.myself
166 irclog "@ I set topic \"#{m.topic}\"", m.channel
168 irclog "@ #{m.source} set topic \"#{m.topic}\"", m.channel
171 topic = m.channel.topic
172 irclog "@ Topic is \"#{m.topic}\"", m.channel
173 irclog "@ Topic set by #{topic.set_by} on #{topic.set_on}", m.channel
181 def unknown_message(m)
182 irclog m.logmessage, ".unknown"
186 ilm = IrcLogModule.new