Class: IrcLogModule

Inherits:
CoreBotModule
  • Object
show all
Defined in:
lib/rbot/core/irclog.rb

Overview

– vim:sw=2:et ++

:title: rbot IRC logging facilities

Author

Giuseppe “Oblomov” Bilotta <[email protected]>

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeIrcLogModule

Returns a new instance of IrcLogModule.



28
29
30
31
32
33
34
35
36
# File 'lib/rbot/core/irclog.rb', line 28

def initialize
  super
  @queue = Queue.new
  @thread = Thread.new { loggers_thread }
  @logs = Hash.new
  Dir.mkdir("#{@bot.botclass}/logs") unless File.exist?("#{@bot.botclass}/logs")
  event_irclog_list_changed(@bot.config['irclog.no_log'], @bot.config['irclog.do_log'])
  @fn_format = @bot.config['irclog.filename_format']
end

Instance Attribute Details

#nolog_rxObject (readonly)

Returns the value of attribute nolog_rx.



27
28
29
# File 'lib/rbot/core/irclog.rb', line 27

def nolog_rx
  @nolog_rx
end

Instance Method Details

#can_log_on(where) ⇒ Object



38
39
40
41
42
# File 'lib/rbot/core/irclog.rb', line 38

def can_log_on(where)
  return true if @dolog_rx and where.match @dolog_rx
  return false if @nolog_rx and where.match @nolog_rx
  return true
end

#cleanupObject



68
69
70
71
72
# File 'lib/rbot/core/irclog.rb', line 68

def cleanup
  @queue << nil
  @thread.join
  @thread = nil
end

#event_irclog_list_changed(nolist, dolist) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/rbot/core/irclog.rb', line 44

def event_irclog_list_changed(nolist, dolist)
  @nolog_rx = nolist.empty? ? nil : Regexp.union(*(nolist.map { |r| r.to_irc_regexp }))
  debug "no log: #{@nolog_rx}"
  @dolog_rx = dolist.empty? ? nil : Regexp.union(*(dolist.map { |r| r.to_irc_regexp }))
  debug "do log: #{@dolog_rx}"
  @logs.inject([]) { |ar, kv|
    ar << kv.first unless can_log_on(kv.first)
    ar
  }.each { |w| logfile_close(w, 'logging disabled here') }
end

#irclog(message, where = "server") ⇒ Object

log IRC-related message message to a file determined by where. where can be a channel name, or a nick for private message logging



64
65
66
# File 'lib/rbot/core/irclog.rb', line 64

def irclog(message, where="server")
  @queue.push [message, where]
end

#listen(m) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/rbot/core/irclog.rb', line 109

def listen(m)
  case m
  when PrivMessage
    method = 'log_message'
  else
    method = 'log_' + m.class.name.downcase.match(/^irc::(\w+)message$/).captures.first
  end
  if self.respond_to?(method)
    self.__send__(method, m)
  else
    warning "unhandled logging for #{m.pretty_inspect} (no such method #{method})"
    unknown_message(m)
  end
end

#log_join(m) ⇒ Object



185
186
187
188
189
190
191
192
# File 'lib/rbot/core/irclog.rb', line 185

def log_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

#log_kick(m) ⇒ Object



203
204
205
206
207
208
209
210
# File 'lib/rbot/core/irclog.rb', line 203

def log_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

#log_message(m) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/rbot/core/irclog.rb', line 124

def log_message(m)
  if m.ctcp
    who = m.private? ? "me" : m.target
    logtarget = m.private? ? m.source : m.target
    case m.ctcp.intern
    when :ACTION
      if m.public?
        irclog "* #{m.source} #{m.logmessage}", m.target
      else
        irclog "* #{m.source}(#{m.sourceaddress}) #{m.logmessage}", m.source
      end
    when :VERSION
      irclog "@ #{m.source} asked #{who} about version info", logtarget
    when :SOURCE
      irclog "@ #{m.source} asked #{who} about source info", logtarget
    when :PING
      irclog "@ #{m.source} pinged #{who}", logtarget
    when :TIME
      irclog "@ #{m.source} asked #{who} what time it is", logtarget
    else
      irclog "@ #{m.source} asked #{who} about #{[m.ctcp, m.message].join(' ')}", logtarget
    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_nick(m) ⇒ Object



169
170
171
172
173
# File 'lib/rbot/core/irclog.rb', line 169

def log_nick(m)
  m.is_on.each { |ch|
    irclog "@ #{m.oldnick} is now known as #{m.newnick}", ch
  }
end

#log_notice(m) ⇒ Object



155
156
157
158
159
160
161
# File 'lib/rbot/core/irclog.rb', line 155

def log_notice(m)
  if m.private?
    irclog "-#{m.source}(#{m.sourceaddress})- #{m.logmessage}", m.source
  else
    irclog "-#{m.source}- #{m.logmessage}", m.target
  end
end

#log_part(m) ⇒ Object



194
195
196
197
198
199
200
201
# File 'lib/rbot/core/irclog.rb', line 194

def log_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

#log_quit(m) ⇒ Object



175
176
177
178
179
# File 'lib/rbot/core/irclog.rb', line 175

def log_quit(m)
  m.was_on.each { |ch|
    irclog "@ Quit: #{m.source}: #{m.logmessage}", ch
  }
end

#log_topic(m) ⇒ Object

def log_invite(m)

# TODO

end



216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/rbot/core/irclog.rb', line 216

def log_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

#logfile_close(where_str, reason = 'unknown reason') ⇒ Object



55
56
57
58
59
60
# File 'lib/rbot/core/irclog.rb', line 55

def logfile_close(where_str, reason = 'unknown reason')
  f = @logs.delete(where_str) or return
  stamp = Time.now.strftime '%Y/%m/%d %H:%M:%S'
  f[1].puts "[#{stamp}] @ Log closed by #{@bot.myself.nick} (#{reason})"
  f[1].close
end

#logfilepath(where_str, now) ⇒ Object



239
240
241
# File 'lib/rbot/core/irclog.rb', line 239

def logfilepath(where_str, now)
  File.join(@bot.botclass, 'logs', now.strftime(@fn_format) % { :where => where_str })
end

#modechange(m) ⇒ Object



181
182
183
# File 'lib/rbot/core/irclog.rb', line 181

def modechange(m)
  irclog "@ Mode #{m.logmessage} by #{m.source}", m.target
end

#motd(m) ⇒ Object



163
164
165
166
167
# File 'lib/rbot/core/irclog.rb', line 163

def motd(m)
  m.message.each_line { |line|
    irclog "MOTD: #{line}", "server"
  }
end

#sent(m) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/rbot/core/irclog.rb', line 74

def sent(m)
  case m
  when NoticeMessage
    irclog "-#{m.source}- #{m.message}", m.target
  when PrivMessage
    logtarget = who = m.target
    if m.ctcp
      case m.ctcp.intern
      when :ACTION
        irclog "* #{m.source} #{m.logmessage}", logtarget
      when :VERSION
        irclog "@ #{m.source} asked #{who} about version info", logtarget
      when :SOURCE
        irclog "@ #{m.source} asked #{who} about source info", logtarget
      when :PING
        irclog "@ #{m.source} pinged #{who}", logtarget
      when :TIME
        irclog "@ #{m.source} asked #{who} what time it is", logtarget
      else
        irclog "@ #{m.source} asked #{who} about #{[m.ctcp, m.message].join(' ')}", logtarget
      end
    else
      irclog "<#{m.source}> #{m.logmessage}", logtarget
    end
  when QuitMessage
    m.was_on.each { |ch|
      irclog "@ quit (#{m.message})", ch
    }
  end
end

#unknown_message(m) ⇒ Object

def names(m)

# TODO

end



235
236
237
# File 'lib/rbot/core/irclog.rb', line 235

def unknown_message(m)
  irclog m.logmessage, ".unknown"
end

#welcome(m) ⇒ Object



105
106
107
# File 'lib/rbot/core/irclog.rb', line 105

def welcome(m)
  irclog "joined server #{m.server} as #{m.target}", "server"
end