2 BotConfig.register BotConfigArrayValue.new('core.address_prefix',
3 :default => [], :wizard => true,
4 :desc => "what non nick-matching prefixes should the bot respond to as if addressed (e.g !, so that '!foo' is treated like 'rbot: foo')"
7 BotConfig.register BotConfigBooleanValue.new('core.reply_with_nick',
8 :default => false, :wizard => true,
9 :desc => "if true, the bot will prepend the nick to what he has to say when replying (e.g. 'markey: you can't do that!')"
12 BotConfig.register BotConfigStringValue.new('core.nick_postfix',
13 :default => ':', :wizard => true,
14 :desc => "when replying with nick put this character after the nick of the user the bot is replying to"
17 # Define standard IRC attriubtes (not so standard actually,
18 # but the closest thing we have ...)
25 # Color is prefixed by \003 and followed by optional
26 # foreground and background specifications, two-digits-max
27 # numbers separated by a comma. One of the two parts
30 ColorRx = /#{Color}\d?\d?(?:,\d\d?)?/
32 # Standard color codes
59 # Convert a String or Symbol into a color number
60 def Irc.find_color(data)
64 f = if String === data
77 # Insert the full color code for a given
78 # foreground/background combination.
79 def Irc.color(fg=nil,bg=nil)
82 str << Irc.find_color(fg).to_s
85 str << "," << Irc.find_color(bg).to_s
90 # base user message class, all user messages derive from this
91 # (a user message is defined as having a source hostmask, a target
92 # nick/channel and a message part)
93 class BasicUserMessage
101 # when the message was received
104 # User that originated the message
107 # User/Channel message was sent to
110 # contents of the message
111 attr_accessor :message
113 # has the message been replied to/handled by a plugin?
114 attr_accessor :replied
116 # instantiate a new Message
117 # bot:: associated bot class
118 # server:: Server where the message took place
119 # source:: User that sent the message
120 # target:: User/Channel is destined for
121 # message:: actual message
122 def initialize(bot, server, source, target, message)
123 @msg_wants_id = false unless defined? @msg_wants_id
130 @message = BasicUserMessage.stripcolour message
135 if @msg_wants_id && @server.capabilities[:"identify-msg"]
136 if @message =~ /^([-+])(.*)/
137 @identified = ($1=="+")
140 warning "Message does not have identification"
144 if target && target == @bot.myself
150 # Access the nick of the source
156 # Access the user@host of the source
159 "#{@source.user}@#{@source.host}"
162 # Was the message from an identified user?
167 # returns true if the message was addressed to the bot.
168 # This includes any private message to the bot, or any public message
169 # which looks like it's addressed to the bot, e.g. "bot: foo", "bot, foo",
170 # a kick message when bot was kicked etc.
175 # has this message been replied to by a plugin?
180 # strip mIRC colour escapes from a string
181 def BasicUserMessage.stripcolour(string)
182 return "" unless string
183 ret = string.gsub(ColorRx, "")
184 #ret.tr!("\x00-\x1f", "")
190 # class for handling IRC user messages. Includes some utilities for handling
191 # the message, for example in plugins.
192 # The +message+ member will have any bot addressing "^bot: " removed
193 # (address? will return true in this case)
194 class UserMessage < BasicUserMessage
196 # for plugin messages, the name of the plugin invoked by the message
199 # for plugin messages, the rest of the message, with the plugin name
203 # convenience member. Who to reply to (i.e. would be sourcenick for a
204 # privately addressed message, or target (the channel) for a publicly
208 # channel the message was in, nil for privately addressed messages
211 # for PRIVMSGs, true if the message was a CTCP ACTION (CTCP stuff
212 # will be stripped from the message)
215 # instantiate a new UserMessage
216 # bot:: associated bot class
217 # source:: hostmask of the message source
218 # target:: nick/channel message is destined for
219 # message:: message part
220 def initialize(bot, server, source, target, message)
221 super(bot, server, source, target, message)
227 if target == @bot.myself
237 # check for option extra addressing prefixes, e.g "|search foo", or
238 # "!version" - first match wins
239 bot.config['core.address_prefix'].each {|mprefix|
240 if @message.gsub!(/^#{Regexp.escape(mprefix)}\s*/, "")
246 # even if they used above prefixes, we allow for silly people who
247 # combine all possible types, e.g. "|rbot: hello", or
248 # "/msg rbot rbot: hello", etc
249 if @message.gsub!(/^\s*#{Regexp.escape(bot.nick)}\s*([:;,>]|\s)\s*/i, "")
253 if(@message =~ /^\001ACTION\s(.+)\001/)
258 # free splitting for plugins
259 @params = @message.dup
260 if @params.gsub!(/^\s*(\S+)[\s$]*/, "")
261 @plugin = $1.downcase
262 @params = nil unless @params.length > 0
266 # returns true for private messages, e.g. "/msg bot hello"
271 # returns true if the message was in a channel
280 # convenience method to reply to a message, useful in plugins. It's the
282 # <tt>@bot.say m.replyto, string</tt>
283 # So if the message is private, it will reply to the user. If it was
284 # in a channel, it will reply in the channel.
285 def plainreply(string, options={})
286 @bot.say @replyto, string, options
290 # Same as reply, but when replying in public it adds the nick of the user
291 # the bot is replying to
292 def nickreply(string, options={})
293 extra = self.public? ? "#{@source}#{@bot.config['core.nick_postfix']} " : ""
294 @bot.say @replyto, extra + string, options
298 # the default reply style is to nickreply unless the reply already contains
299 # the nick or core.reply_with_nick is set to false
301 def reply(string, options={})
302 if @bot.config['core.reply_with_nick'] and not string =~ /\b#{@source}\b/
303 return nickreply(string, options)
305 plainreply(string, options)
308 # convenience method to reply to a message with an action. It's the
310 # <tt>@bot.action m.replyto, string</tt>
311 # So if the message is private, it will reply to the user. If it was
312 # in a channel, it will reply in the channel.
313 def act(string, options={})
314 @bot.action @replyto, string, options
318 # convenience method to reply "okay" in the current language to the
321 self.plainreply @bot.lang.get("okay")
324 # Like the above, but append the username
326 str = @bot.lang.get("okay").dup
328 # remove final punctuation
329 str.gsub!(/[!,.]$/,"")
330 str += ", #{@source}"
335 # the default okay style is the same as the default reply style
338 if @bot.config['core.reply_with_nick']
346 # class to manage IRC PRIVMSGs
347 class PrivMessage < UserMessage
348 def initialize(bot, server, source, target, message)
354 # class to manage IRC NOTICEs
355 class NoticeMessage < UserMessage
356 def initialize(bot, server, source, target, message)
362 # class to manage IRC KICKs
363 # +address?+ can be used as a shortcut to see if the bot was kicked,
364 # basically, +target+ was kicked from +channel+ by +source+ with +message+
365 class KickMessage < BasicUserMessage
366 # channel user was kicked from
369 def initialize(bot, server, source, target, channel, message="")
370 super(bot, server, source, target, message)
375 # class to pass IRC Nick changes in. @message contains the old nickame,
376 # @sourcenick contains the new one.
377 class NickMessage < BasicUserMessage
378 def initialize(bot, server, source, oldnick, newnick)
379 super(bot, server, source, oldnick, newnick)
391 class QuitMessage < BasicUserMessage
392 def initialize(bot, server, source, target, message="")
393 super(bot, server, source, target, message)
397 class TopicMessage < BasicUserMessage
400 # topic set at (unixtime)
401 attr_reader :timestamp
402 # topic set on channel
405 def initialize(bot, server, source, channel, topic=ChannelTopic.new)
406 super(bot, server, source, channel, topic.text)
408 @timestamp = topic.set_on
413 # class to manage channel joins
414 class JoinMessage < BasicUserMessage
417 def initialize(bot, server, source, channel, message="")
418 super(bot, server, source, channel, message)
420 # in this case sourcenick is the nick that could be the bot
421 @address = (source == @bot.myself)
425 # class to manage channel parts
426 # same as a join, but can have a message too
427 class PartMessage < JoinMessage