3 # base user message class, all user messages derive from this
4 # (a user message is defined as having a source hostmask, a target
5 # nick/channel and a message part)
8 # when the message was received
11 # hostmask of message source
14 # nick of message source
15 attr_reader :sourcenick
17 # url part of message source
18 attr_reader :sourceaddress
20 # nick/channel message was sent to
23 # contents of the message
24 attr_accessor :message
26 # has the message been replied to/handled by a plugin?
27 attr_accessor :replied
29 # instantiate a new Message
30 # bot:: associated bot class
31 # source:: hostmask of the message source
32 # target:: nick/channel message is destined for
33 # message:: message part
34 def initialize(bot, source, target, message)
40 @message = BasicUserMessage.stripcolour message
43 # split source into consituent parts
44 if source =~ /^((\S+)!(\S+))$/
49 if target && target.downcase == @bot.nick.downcase
55 # returns true if the message was addressed to the bot.
56 # This includes any private message to the bot, or any public message
57 # which looks like it's addressed to the bot, e.g. "bot: foo", "bot, foo",
58 # a kick message when bot was kicked etc.
63 # has this message been replied to by a plugin?
68 # strip mIRC colour escapes from a string
69 def BasicUserMessage.stripcolour(string)
70 return "" unless string
71 ret = string.gsub(/\cC\d\d?(?:,\d\d?)?/, "")
72 #ret.tr!("\x00-\x1f", "")
78 # class for handling IRC user messages. Includes some utilities for handling
79 # the message, for example in plugins.
80 # The +message+ member will have any bot addressing "^bot: " removed
81 # (address? will return true in this case)
82 class UserMessage < BasicUserMessage
84 # for plugin messages, the name of the plugin invoked by the message
87 # for plugin messages, the rest of the message, with the plugin name
91 # convenience member. Who to reply to (i.e. would be sourcenick for a
92 # privately addressed message, or target (the channel) for a publicly
96 # channel the message was in, nil for privately addressed messages
99 # for PRIVMSGs, true if the message was a CTCP ACTION (CTCP stuff
100 # will be stripped from the message)
103 # instantiate a new UserMessage
104 # bot:: associated bot class
105 # source:: hostmask of the message source
106 # target:: nick/channel message is destined for
107 # message:: message part
108 def initialize(bot, source, target, message)
109 super(bot, source, target, message)
115 if target.downcase == @bot.nick.downcase
119 @replyto = @sourcenick
125 # check for option extra addressing prefixes, e.g "|search foo", or
126 # "!version" - first match wins
127 bot.addressing_prefixes.each {|mprefix|
128 if @message.gsub!(/^#{Regexp.escape(mprefix)}\s*/, "")
134 # even if they used above prefixes, we allow for silly people who
135 # combine all possible types, e.g. "|rbot: hello", or
136 # "/msg rbot rbot: hello", etc
137 if @message.gsub!(/^\s*#{bot.nick}\s*([:;,>]|\s)\s*/, "")
141 if(@message =~ /^\001ACTION\s(.+)\001/)
146 # free splitting for plugins
147 @params = @message.dup
148 if @params.gsub!(/^\s*(\S+)[\s$]*/, "")
149 @plugin = $1.downcase
150 @params = nil unless @params.length > 0
154 # returns true for private messages, e.g. "/msg bot hello"
159 # returns true if the message was in a channel
168 # convenience method to reply to a message, useful in plugins. It's the
170 # <tt>@bot.say m.replyto, string</tt>
171 # So if the message is private, it will reply to the user. If it was
172 # in a channel, it will reply in the channel.
174 @bot.say @replyto, string
180 # class to manage IRC PRIVMSGs
181 class PrivMessage < UserMessage
184 # class to manage IRC NOTICEs
185 class NoticeMessage < UserMessage
188 # class to manage IRC KICKs
189 # +address?+ can be used as a shortcut to see if the bot was kicked,
190 # basically, +target+ was kicked from +channel+ by +source+ with +message+
191 class KickMessage < BasicUserMessage
192 # channel user was kicked from
195 def initialize(bot, source, target, channel, message="")
196 super(bot, source, target, message)
201 # class to pass IRC Nick changes in. @message contains the old nickame,
202 # @sourcenick contains the new one.
203 class NickMessage < BasicUserMessage
204 def initialize(bot, source, oldnick, newnick)
205 super(bot, source, oldnick, newnick)
209 class QuitMessage < BasicUserMessage
210 def initialize(bot, source, target, message="")
211 super(bot, source, target, message)
215 class TopicMessage < BasicUserMessage
218 # topic set at (unixtime)
219 attr_reader :timestamp
220 # topic set on channel
223 def initialize(bot, source, channel, timestamp, topic="")
224 super(bot, source, channel, topic)
226 @timestamp = timestamp
231 # class to manage channel joins
232 class JoinMessage < BasicUserMessage
235 def initialize(bot, source, channel, message="")
236 super(bot, source, channel, message)
238 # in this case sourcenick is the nick that could be the bot
239 @address = (sourcenick.downcase == @bot.nick.downcase)
243 # class to manage channel parts
244 # same as a join, but can have a message too
245 class PartMessage < JoinMessage