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"
22 # base user message class, all user messages derive from this
23 # (a user message is defined as having a source hostmask, a target
24 # nick/channel and a message part)
25 class BasicUserMessage
33 # when the message was received
36 # User that originated the message
39 # User/Channel message was sent to
42 # contents of the message
43 attr_accessor :message
45 # has the message been replied to/handled by a plugin?
46 attr_accessor :replied
48 # instantiate a new Message
49 # bot:: associated bot class
50 # server:: Server where the message took place
51 # source:: User that sent the message
52 # target:: User/Channel is destined for
53 # message:: actual message
54 def initialize(bot, server, source, target, message)
55 @msg_wants_id = false unless defined? @msg_wants_id
62 @message = BasicUserMessage.stripcolour message
67 if @msg_wants_id && @server.capabilities[:"identify-msg"]
68 if @message =~ /([-+])(.*)/
69 @identified = ($1=="+")
72 warning "Message does not have identification"
76 if target && target == @bot.myself
82 # Access the nick of the source
88 # Access the user@host of the source
91 "#{@source.user}@#{@source.host}"
94 # Was the message from an identified user?
99 # returns true if the message was addressed to the bot.
100 # This includes any private message to the bot, or any public message
101 # which looks like it's addressed to the bot, e.g. "bot: foo", "bot, foo",
102 # a kick message when bot was kicked etc.
107 # has this message been replied to by a plugin?
112 # strip mIRC colour escapes from a string
113 def BasicUserMessage.stripcolour(string)
114 return "" unless string
115 ret = string.gsub(/\cC\d\d?(?:,\d\d?)?/, "")
116 #ret.tr!("\x00-\x1f", "")
122 # class for handling IRC user messages. Includes some utilities for handling
123 # the message, for example in plugins.
124 # The +message+ member will have any bot addressing "^bot: " removed
125 # (address? will return true in this case)
126 class UserMessage < BasicUserMessage
128 # for plugin messages, the name of the plugin invoked by the message
131 # for plugin messages, the rest of the message, with the plugin name
135 # convenience member. Who to reply to (i.e. would be sourcenick for a
136 # privately addressed message, or target (the channel) for a publicly
140 # channel the message was in, nil for privately addressed messages
143 # for PRIVMSGs, true if the message was a CTCP ACTION (CTCP stuff
144 # will be stripped from the message)
147 # instantiate a new UserMessage
148 # bot:: associated bot class
149 # source:: hostmask of the message source
150 # target:: nick/channel message is destined for
151 # message:: message part
152 def initialize(bot, server, source, target, message)
153 super(bot, server, source, target, message)
159 if target == @bot.myself
169 # check for option extra addressing prefixes, e.g "|search foo", or
170 # "!version" - first match wins
171 bot.config['core.address_prefix'].each {|mprefix|
172 if @message.gsub!(/^#{Regexp.escape(mprefix)}\s*/, "")
178 # even if they used above prefixes, we allow for silly people who
179 # combine all possible types, e.g. "|rbot: hello", or
180 # "/msg rbot rbot: hello", etc
181 if @message.gsub!(/^\s*#{Regexp.escape(bot.nick)}\s*([:;,>]|\s)\s*/i, "")
185 if(@message =~ /^\001ACTION\s(.+)\001/)
190 # free splitting for plugins
191 @params = @message.dup
192 if @params.gsub!(/^\s*(\S+)[\s$]*/, "")
193 @plugin = $1.downcase
194 @params = nil unless @params.length > 0
198 # returns true for private messages, e.g. "/msg bot hello"
203 # returns true if the message was in a channel
212 # convenience method to reply to a message, useful in plugins. It's the
214 # <tt>@bot.say m.replyto, string</tt>
215 # So if the message is private, it will reply to the user. If it was
216 # in a channel, it will reply in the channel.
217 def plainreply(string, options={})
218 @bot.say @replyto, string, options
222 # Same as reply, but when replying in public it adds the nick of the user
223 # the bot is replying to
224 def nickreply(string, options={})
225 extra = self.public? ? "#{@source}#{@bot.config['core.nick_postfix']} " : ""
226 @bot.say @replyto, extra + string, options
230 # the default reply style is to nickreply unless the reply already contains
231 # the nick or core.reply_with_nick is set to false
233 def reply(string, options={})
234 if @bot.config['core.reply_with_nick'] and not string =~ /\b#{@source}\b/
235 return nickreply(string, options)
237 plainreply(string, options)
240 # convenience method to reply to a message with an action. It's the
242 # <tt>@bot.action m.replyto, string</tt>
243 # So if the message is private, it will reply to the user. If it was
244 # in a channel, it will reply in the channel.
245 def act(string, options={})
246 @bot.action @replyto, string, options
250 # convenience method to reply "okay" in the current language to the
253 self.plainreply @bot.lang.get("okay")
256 # Like the above, but append the username
258 str = @bot.lang.get("okay").dup
260 # remove final punctuation
261 str.gsub!(/[!,.]$/,"")
262 str += ", #{@source}"
267 # the default okay style is the same as the default reply style
270 if @bot.config['core.reply_with_nick']
278 # class to manage IRC PRIVMSGs
279 class PrivMessage < UserMessage
280 def initialize(bot, server, source, target, message)
286 # class to manage IRC NOTICEs
287 class NoticeMessage < UserMessage
288 def initialize(bot, server, source, target, message)
294 # class to manage IRC KICKs
295 # +address?+ can be used as a shortcut to see if the bot was kicked,
296 # basically, +target+ was kicked from +channel+ by +source+ with +message+
297 class KickMessage < BasicUserMessage
298 # channel user was kicked from
301 def initialize(bot, server, source, target, channel, message="")
302 super(bot, server, source, target, message)
307 # class to pass IRC Nick changes in. @message contains the old nickame,
308 # @sourcenick contains the new one.
309 class NickMessage < BasicUserMessage
310 def initialize(bot, server, source, oldnick, newnick)
311 super(bot, server, source, oldnick, newnick)
323 class QuitMessage < BasicUserMessage
324 def initialize(bot, server, source, target, message="")
325 super(bot, server, source, target, message)
329 class TopicMessage < BasicUserMessage
332 # topic set at (unixtime)
333 attr_reader :timestamp
334 # topic set on channel
337 def initialize(bot, server, source, channel, topic=ChannelTopic.new)
338 super(bot, server, source, channel, topic.text)
340 @timestamp = topic.set_on
345 # class to manage channel joins
346 class JoinMessage < BasicUserMessage
349 def initialize(bot, server, source, channel, message="")
350 super(bot, server, source, channel, message)
352 # in this case sourcenick is the nick that could be the bot
353 @address = (source == @bot.myself)
357 # class to manage channel parts
358 # same as a join, but can have a message too
359 class PartMessage < JoinMessage