summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiuseppe Bilotta <giuseppe.bilotta@gmail.com>2008-04-15 01:21:34 +0200
committerGiuseppe Bilotta <giuseppe.bilotta@gmail.com>2008-04-15 01:22:14 +0200
commitfcda5169000a33c237bb6b0ed28afcf29a674ffd (patch)
tree3d932add2588b6e840603e0d9bc921002aca44a6
parent5a6c7f42cd079fe52d0927004148081fa8e939b1 (diff)
IRC logging refactoring
-rw-r--r--lib/rbot/core/config.rb4
-rw-r--r--lib/rbot/core/irclog.rb164
-rw-r--r--lib/rbot/ircbot.rb140
-rw-r--r--lib/rbot/message.rb7
4 files changed, 192 insertions, 123 deletions
diff --git a/lib/rbot/core/config.rb b/lib/rbot/core/config.rb
index 203cf1de..cfc6588b 100644
--- a/lib/rbot/core/config.rb
+++ b/lib/rbot/core/config.rb
@@ -205,10 +205,10 @@ class ConfigModule < CoreBotModule
case m.ctcp.intern
when :VERSION
m.ctcp_reply version_string
- @bot.irclog "@ #{m.source} asked #{who} about version info"
+ # @bot.irclog "@ #{m.source} asked #{who} about version info"
when :SOURCE
m.ctcp_reply Irc::Bot::SOURCE_URL
- @bot.irclog "@ #{m.source} asked #{who} about source info"
+ # @bot.irclog "@ #{m.source} asked #{who} about source info"
end
end
diff --git a/lib/rbot/core/irclog.rb b/lib/rbot/core/irclog.rb
new file mode 100644
index 00000000..6058a17a
--- /dev/null
+++ b/lib/rbot/core/irclog.rb
@@ -0,0 +1,164 @@
+#-- vim:sw=2:et
+#++
+#
+# :title: rbot IRC logging facilities
+#
+# Author:: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
+# Copyright:: (C) 2008 Giuseppe Bilotta
+# License:: GPL v2
+
+class IrcLogModule < CoreBotModule
+
+ def initialize
+ super
+ @logs = Hash.new
+ Dir.mkdir("#{@bot.botclass}/logs") unless File.exist?("#{@bot.botclass}/logs")
+ end
+
+ # log IRC-related message +message+ to a file determined by +where+.
+ # +where+ can be a channel name, or a nick for private message logging
+ def irclog(message, where="server")
+ message = message.chomp
+ stamp = Time.now.strftime("%Y/%m/%d %H:%M:%S")
+ if where.class <= Server
+ where_str = "server"
+ else
+ where_str = where.downcase.gsub(/[:!?$*()\/\\<>|"']/, "_")
+ end
+ unless(@logs.has_key?(where_str))
+ @logs[where_str] = File.new("#{@bot.botclass}/logs/#{where_str}", "a")
+ @logs[where_str].sync = true
+ end
+ @logs[where_str].puts "[#{stamp}] #{message}"
+ #debug "[#{stamp}] <#{where}> #{message}"
+ end
+
+ def sent(m)
+ case m
+ when NoticeMessage
+ if m.public?
+ irclog "-=#{m.source}=- #{m.message}", m.target
+ else
+ irclog "[-=#{m.source}=-] #{m.message}", m.target
+ end
+ when PrivMessage
+ if m.public?
+ irclog "<#{m.source}> #{m.message}", m.target
+ else
+ irclog "[msg(#{m.target})] #{m.message}", m.target
+ end
+ when QuitMessage
+ m.was_on.each { |ch|
+ irclog "@ quit (#{m.message})", ch
+ }
+ end
+ end
+
+ def welcome(m)
+ irclog "joined server #{m.server} as #{m.target}", "server"
+ end
+
+ def message(m)
+ if(m.action?)
+ if(m.private?)
+ irclog "* [#{m.source}(#{m.sourceaddress})] #{m.logmessage}", m.source
+ else
+ irclog "* #{m.source} #{m.logmessage}", m.target
+ end
+ else
+ if(m.public?)
+ irclog "<#{m.source}> #{m.logmessage}", m.target
+ else
+ irclog "[#{m.source}(#{m.sourceaddress})] #{m.logmessage}", m.source
+ end
+ end
+ end
+
+ def notice(m)
+ if m.private?
+ irclog "-#{m.source}- #{m.message}", m.source
+ else
+ irclog "-#{m.source}- #{m.message}", m.target
+ end
+ end
+
+ def motd(m)
+ m.message.each_line { |line|
+ irclog "MOTD: #{line}", "server"
+ }
+ end
+
+ def nick(m)
+ m.is_on.each { |ch|
+ irclog "@ #{m.oldnick} is now known as #{m.newnick}", ch
+ }
+ end
+
+ def quit(m)
+ m.was_on.each { |ch|
+ irclog "@ Quit: #{m.source}: #{m.message}", ch
+ }
+ end
+
+ def modechange(m)
+ irclog "@ Mode #{m.message} by #{m.source}", m.target
+ end
+
+ def join(m)
+ if m.address?
+ debug "joined channel #{m.channel}"
+ irclog "@ Joined channel #{m.channel}", m.channel
+ else
+ irclog "@ #{m.source} joined channel #{m.channel}", m.channel
+ end
+ end
+
+ def part(m)
+ if(m.address?)
+ debug "left channel #{m.channel}"
+ irclog "@ Left channel #{m.channel} (#{m.logmessage})", m.channel
+ else
+ irclog "@ #{m.source} left channel #{m.channel} (#{m.logmessage})", m.channel
+ end
+ end
+
+ def kick(m)
+ if(m.address?)
+ debug "kicked from channel #{m.channel}"
+ irclog "@ You have been kicked from #{m.channel} by #{m.source} (#{m.logmessage})", m.channel
+ else
+ irclog "@ #{m.target} has been kicked from #{m.channel} by #{m.source} (#{m.logmessage})", m.channel
+ end
+ end
+
+ # def invite(m)
+ # # TODO
+ # end
+
+ def topic(m)
+ case m.info_or_set
+ when :set
+ if m.source == @bot.myself
+ irclog "@ I set topic \"#{m.topic}\"", m.channel
+ else
+ irclog "@ #{m.source} set topic \"#{m.topic}\"", m.channel
+ end
+ when :info
+ topic = m.channel.topic
+ irclog "@ Topic is \"#{m.topic}\"", m.channel
+ irclog "@ Topic set by #{topic.set_by} on #{topic.set_on}", m.channel
+ end
+ end
+
+ # def names(m)
+ # # TODO
+ # end
+
+ def unknown_message(m)
+ irclog m.message, ".unknown"
+ end
+end
+
+ilm = IrcLogModule.new
+ilm.priority = -1
+
diff --git a/lib/rbot/ircbot.rb b/lib/rbot/ircbot.rb
index 49459a7f..d95eb18d 100644
--- a/lib/rbot/ircbot.rb
+++ b/lib/rbot/ircbot.rb
@@ -394,7 +394,6 @@ class Bot
FileUtils.cp_r Config::datadir+'/templates', botclass
end
- Dir.mkdir("#{botclass}/logs") unless File.exist?("#{botclass}/logs")
Dir.mkdir("#{botclass}/registry") unless File.exist?("#{botclass}/registry")
Dir.mkdir("#{botclass}/safe_save") unless File.exist?("#{botclass}/safe_save")
@@ -484,8 +483,6 @@ class Bot
end
@quit_mutex = Mutex.new
- @logs = Hash.new
-
@plugins = nil
@lang = Language.new(self, @config['core.language'])
@@ -530,8 +527,6 @@ class Bot
@client[:welcome] = proc {|data|
m = WelcomeMessage.new(self, server, data[:source], data[:target], data[:message])
- irclog "joined server #{@client.server} as #{myself}", "server"
-
@plugins.delegate("welcome", m)
@plugins.delegate("connect")
@@ -575,9 +570,7 @@ class Bot
end
}
- irclogprivmsg(m)
-
- @plugins.irc_delegate('privmsg', m) unless m.ignored?
+ @plugins.irc_delegate('privmsg', m)
}
@client[:notice] = proc { |data|
message = NoticeMessage.new(self, server, data[:source], data[:target], data[:message])
@@ -587,9 +580,6 @@ class Bot
}
@client[:motd] = proc { |data|
m = MotdMessage.new(self, server, data[:source], data[:target], data[:motd])
- data[:motd].each_line { |line|
- irclog "MOTD: #{line}", "server"
- }
@plugins.delegate "motd", m
}
@client[:nicktaken] = proc { |data|
@@ -620,68 +610,54 @@ class Bot
old = data[:oldnick]
new = data[:newnick]
m = NickMessage.new(self, server, source, old, new)
+ m.is_on = data[:is_on]
if source == myself
debug "my nick is now #{new}"
end
- data[:is_on].each { |ch|
- irclog "@ #{old} is now known as #{new}", ch
- }
@plugins.irc_delegate("nick", m)
}
@client[:quit] = proc {|data|
source = data[:source]
message = data[:message]
m = QuitMessage.new(self, server, source, source, message)
- data[:was_on].each { |ch|
- irclog "@ Quit: #{source}: #{message}", ch
- }
+ m.was_on = data[:was_on]
@plugins.irc_delegate("quit", m)
}
@client[:mode] = proc {|data|
m = ModeChangeMessage.new(self, server, data[:source], data[:target], data[:modestring])
m.modes = data[:modes]
- irclog "@ Mode #{data[:modestring]} by #{data[:source]}", data[:target]
@plugins.delegate "modechange", m
}
@client[:join] = proc {|data|
m = JoinMessage.new(self, server, data[:source], data[:channel], data[:message])
- irclogjoin(m)
-
@plugins.irc_delegate("join", m)
sendq("WHO #{data[:channel]}", data[:channel], 2) if m.address?
}
@client[:part] = proc {|data|
m = PartMessage.new(self, server, data[:source], data[:channel], data[:message])
- irclogpart(m)
-
@plugins.irc_delegate("part", m)
}
@client[:kick] = proc {|data|
m = KickMessage.new(self, server, data[:source], data[:target], data[:channel],data[:message])
- irclogkick(m)
-
@plugins.irc_delegate("kick", m)
}
@client[:invite] = proc {|data|
m = InviteMessage.new(self, server, data[:source], data[:target], data[:channel])
-
@plugins.irc_delegate("invite", m)
}
@client[:changetopic] = proc {|data|
m = TopicMessage.new(self, server, data[:source], data[:channel], data[:topic])
- irclogtopic(m)
-
+ m.info_or_set = :set
@plugins.irc_delegate("topic", m)
}
- @client[:topic] = proc { |data|
- irclog "@ Topic is \"#{data[:topic]}\"", data[:channel]
- }
+ # @client[:topic] = proc { |data|
+ # irclog "@ Topic is \"#{data[:topic]}\"", data[:channel]
+ # }
@client[:topicinfo] = proc { |data|
channel = data[:channel]
topic = channel.topic
- irclog "@ Topic set by #{topic.set_by} on #{topic.set_on}", channel
m = TopicMessage.new(self, server, data[:source], channel, topic)
-
+ m.info_or_set = :info
@plugins.irc_delegate("topic", m)
}
@client[:names] = proc { |data|
@@ -692,7 +668,6 @@ class Bot
@client[:unknown] = proc { |data|
#debug "UNKNOWN: #{data[:serverstring]}"
m = UnknownMessage.new(self, server, server, nil, data[:serverstring])
- irclog data[:serverstring], ".unknown"
@plugins.delegate "unknown_message", m
}
@@ -968,7 +943,7 @@ class Bot
lines.each { |line|
sendq "#{fixed}#{line}", chan, ring
- log_sent(type, where, line)
+ delegate_sent(type, where, line)
}
end
@@ -1010,24 +985,6 @@ class Bot
say where, @lang.get("okay")
end
- # log IRC-related message +message+ to a file determined by +where+.
- # +where+ can be a channel name, or a nick for private message logging
- def irclog(message, where="server")
- message = message.chomp
- stamp = Time.now.strftime("%Y/%m/%d %H:%M:%S")
- if where.class <= Server
- where_str = "server"
- else
- where_str = where.downcase.gsub(/[:!?$*()\/\\<>|"']/, "_")
- end
- unless(@logs.has_key?(where_str))
- @logs[where_str] = File.new("#{@botclass}/logs/#{where_str}", "a")
- @logs[where_str].sync = true
- end
- @logs[where_str].puts "[#{stamp}] #{message}"
- #debug "[#{stamp}] <#{where}> #{message}"
- end
-
# set topic of channel +where+ to +topic+
def topic(where, topic)
sendq "TOPIC #{where} :#{topic}", where, 2
@@ -1041,6 +998,8 @@ class Bot
@socket.clearq
debug "Sending quit message"
@socket.emergency_puts "QUIT :#{message}"
+ debug "Logging quits"
+ delegate_sent('QUIT', @bot.myself, message)
debug "Flushing socket"
@socket.flush
rescue SocketError => e
@@ -1049,10 +1008,6 @@ class Bot
debug "Shutting down socket"
@socket.shutdown
end
- debug "Logging quits"
- server.channels.each { |ch|
- irclog "@ quit (#{message})", ch
- }
stop_server_pings
@client.reset
end
@@ -1230,75 +1185,18 @@ class Bot
private
- def irclogprivmsg(m)
- if(m.action?)
- if(m.private?)
- irclog "* [#{m.source}(#{m.sourceaddress})] #{m.logmessage}", m.source
- else
- irclog "* #{m.source} #{m.logmessage}", m.target
- end
- else
- if(m.public?)
- irclog "<#{m.source}> #{m.logmessage}", m.target
- else
- irclog "[#{m.source}(#{m.sourceaddress})] #{m.logmessage}", m.source
- end
- end
- end
-
- # log a message. Internal use only.
- def log_sent(type, where, message)
+ # delegate sent messages
+ def delegate_sent(type, where, message)
+ args = [self, server, myself, server.user_or_channel(where.to_s), message]
case type
when "NOTICE"
- case where
- when Channel
- irclog "-=#{myself}=- #{message}", where
- else
- irclog "[-=#{where}=-] #{message}", where
- end
+ m = NoticeMessage.new(*args)
when "PRIVMSG"
- case where
- when Channel
- irclog "<#{myself}> #{message}", where
- else
- irclog "[msg(#{where})] #{message}", where
- end
- end
- end
-
- def irclogjoin(m)
- if m.address?
- debug "joined channel #{m.channel}"
- irclog "@ Joined channel #{m.channel}", m.channel
- else
- irclog "@ #{m.source} joined channel #{m.channel}", m.channel
- end
- end
-
- def irclogpart(m)
- if(m.address?)
- debug "left channel #{m.channel}"
- irclog "@ Left channel #{m.channel} (#{m.logmessage})", m.channel
- else
- irclog "@ #{m.source} left channel #{m.channel} (#{m.logmessage})", m.channel
- end
- end
-
- def irclogkick(m)
- if(m.address?)
- debug "kicked from channel #{m.channel}"
- irclog "@ You have been kicked from #{m.channel} by #{m.source} (#{m.logmessage})", m.channel
- else
- irclog "@ #{m.target} has been kicked from #{m.channel} by #{m.source} (#{m.logmessage})", m.channel
- end
- end
-
- def irclogtopic(m)
- if m.source == myself
- irclog "@ I set topic \"#{m.topic}\"", m.channel
- else
- irclog "@ #{m.source} set topic \"#{m.topic}\"", m.channel
+ m = PrivMessage.new(*args)
+ when "QUIT"
+ m = QuitMessage.new(*args)
end
+ @plugins.delegate('sent', m)
end
end
diff --git a/lib/rbot/message.rb b/lib/rbot/message.rb
index 3b2fbc68..670051ed 100644
--- a/lib/rbot/message.rb
+++ b/lib/rbot/message.rb
@@ -449,8 +449,10 @@ module Irc
# class to pass IRC Nick changes in. @message contains the old nickame,
# @sourcenick contains the new one.
class NickMessage < BasicUserMessage
+ attr_accessor :is_on
def initialize(bot, server, source, oldnick, newnick)
super(bot, server, source, oldnick, newnick)
+ @is_on = []
end
def oldnick
@@ -482,8 +484,10 @@ module Irc
end
class QuitMessage < BasicUserMessage
+ attr_accessor :was_on
def initialize(bot, server, source, target, message="")
super(bot, server, source, target, message)
+ @was_on = []
end
end
@@ -495,11 +499,14 @@ module Irc
# topic set on channel
attr_reader :channel
+ # :info if topic info, :set if topic set
+ attr_accessor :info_or_set
def initialize(bot, server, source, channel, topic=ChannelTopic.new)
super(bot, server, source, channel, topic.text)
@topic = topic
@timestamp = topic.set_on
@channel = channel
+ @info_or_set = nil
end
end