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"
21 ColorRx = /#{Color}\d\d?(?:,\d\d?)?/
23 # base user message class, all user messages derive from this
24 # (a user message is defined as having a source hostmask, a target
25 # nick/channel and a message part)
26 class BasicUserMessage
34 # when the message was received
37 # User that originated the message
40 # User/Channel message was sent to
43 # contents of the message
44 attr_accessor :message
46 # has the message been replied to/handled by a plugin?
47 attr_accessor :replied
49 # instantiate a new Message
50 # bot:: associated bot class
51 # server:: Server where the message took place
52 # source:: User that sent the message
53 # target:: User/Channel is destined for
54 # message:: actual message
55 def initialize(bot, server, source, target, message)
56 @msg_wants_id = false unless defined? @msg_wants_id
63 @message = BasicUserMessage.stripcolour message
68 if @msg_wants_id && @server.capabilities[:"identify-msg"]
69 if @message =~ /^([-+])(.*)/
70 @identified = ($1=="+")
73 warning "Message does not have identification"
77 if target && target == @bot.myself
83 # Access the nick of the source
89 # Access the user@host of the source
92 "#{@source.user}@#{@source.host}"
95 # Was the message from an identified user?
100 # returns true if the message was addressed to the bot.
101 # This includes any private message to the bot, or any public message
102 # which looks like it's addressed to the bot, e.g. "bot: foo", "bot, foo",
103 # a kick message when bot was kicked etc.
108 # has this message been replied to by a plugin?
113 # strip mIRC colour escapes from a string
114 def BasicUserMessage.stripcolour(string)
115 return "" unless string
116 ret = string.gsub(ColorRx, "")
117 #ret.tr!("\x00-\x1f", "")
123 # class for handling IRC user messages. Includes some utilities for handling
124 # the message, for example in plugins.
125 # The +message+ member will have any bot addressing "^bot: " removed
126 # (address? will return true in this case)
127 class UserMessage < BasicUserMessage
129 # for plugin messages, the name of the plugin invoked by the message
132 # for plugin messages, the rest of the message, with the plugin name
136 # convenience member. Who to reply to (i.e. would be sourcenick for a
137 # privately addressed message, or target (the channel) for a publicly
141 # channel the message was in, nil for privately addressed messages
144 # for PRIVMSGs, true if the message was a CTCP ACTION (CTCP stuff
145 # will be stripped from the message)
148 # instantiate a new UserMessage
149 # bot:: associated bot class
150 # source:: hostmask of the message source
151 # target:: nick/channel message is destined for
152 # message:: message part
153 def initialize(bot, server, source, target, message)
154 super(bot, server, source, target, message)
160 if target == @bot.myself
170 # check for option extra addressing prefixes, e.g "|search foo", or
171 # "!version" - first match wins
172 bot.config['core.address_prefix'].each {|mprefix|
173 if @message.gsub!(/^#{Regexp.escape(mprefix)}\s*/, "")
179 # even if they used above prefixes, we allow for silly people who
180 # combine all possible types, e.g. "|rbot: hello", or
181 # "/msg rbot rbot: hello", etc
182 if @message.gsub!(/^\s*#{Regexp.escape(bot.nick)}\s*([:;,>]|\s)\s*/i, "")
186 if(@message =~ /^\001ACTION\s(.+)\001/)
191 # free splitting for plugins
192 @params = @message.dup
193 if @params.gsub!(/^\s*(\S+)[\s$]*/, "")
194 @plugin = $1.downcase
195 @params = nil unless @params.length > 0
199 # returns true for private messages, e.g. "/msg bot hello"
204 # returns true if the message was in a channel
213 # convenience method to reply to a message, useful in plugins. It's the
215 # <tt>@bot.say m.replyto, string</tt>
216 # So if the message is private, it will reply to the user. If it was
217 # in a channel, it will reply in the channel.
218 def plainreply(string, options={})
219 @bot.say @replyto, string, options
223 # Same as reply, but when replying in public it adds the nick of the user
224 # the bot is replying to
225 def nickreply(string, options={})
226 extra = self.public? ? "#{@source}#{@bot.config['core.nick_postfix']} " : ""
227 @bot.say @replyto, extra + string, options
231 # the default reply style is to nickreply unless the reply already contains
232 # the nick or core.reply_with_nick is set to false
234 def reply(string, options={})
235 if @bot.config['core.reply_with_nick'] and not string =~ /\b#{@source}\b/
236 return nickreply(string, options)
238 plainreply(string, options)
241 # convenience method to reply to a message with an action. It's the
243 # <tt>@bot.action m.replyto, string</tt>
244 # So if the message is private, it will reply to the user. If it was
245 # in a channel, it will reply in the channel.
246 def act(string, options={})
247 @bot.action @replyto, string, options
251 # convenience method to reply "okay" in the current language to the
254 self.plainreply @bot.lang.get("okay")
257 # Like the above, but append the username
259 str = @bot.lang.get("okay").dup
261 # remove final punctuation
262 str.gsub!(/[!,.]$/,"")
263 str += ", #{@source}"
268 # the default okay style is the same as the default reply style
271 if @bot.config['core.reply_with_nick']
279 # class to manage IRC PRIVMSGs
280 class PrivMessage < UserMessage
281 def initialize(bot, server, source, target, message)
287 # class to manage IRC NOTICEs
288 class NoticeMessage < UserMessage
289 def initialize(bot, server, source, target, message)
295 # class to manage IRC KICKs
296 # +address?+ can be used as a shortcut to see if the bot was kicked,
297 # basically, +target+ was kicked from +channel+ by +source+ with +message+
298 class KickMessage < BasicUserMessage
299 # channel user was kicked from
302 def initialize(bot, server, source, target, channel, message="")
303 super(bot, server, source, target, message)
308 # class to pass IRC Nick changes in. @message contains the old nickame,
309 # @sourcenick contains the new one.
310 class NickMessage < BasicUserMessage
311 def initialize(bot, server, source, oldnick, newnick)
312 super(bot, server, source, oldnick, newnick)
324 class QuitMessage < BasicUserMessage
325 def initialize(bot, server, source, target, message="")
326 super(bot, server, source, target, message)
330 class TopicMessage < BasicUserMessage
333 # topic set at (unixtime)
334 attr_reader :timestamp
335 # topic set on channel
338 def initialize(bot, server, source, channel, topic=ChannelTopic.new)
339 super(bot, server, source, channel, topic.text)
341 @timestamp = topic.set_on
346 # class to manage channel joins
347 class JoinMessage < BasicUserMessage
350 def initialize(bot, server, source, channel, message="")
351 super(bot, server, source, channel, message)
353 # in this case sourcenick is the nick that could be the bot
354 @address = (source == @bot.myself)
358 # class to manage channel parts
359 # same as a join, but can have a message too
360 class PartMessage < JoinMessage