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 # instantiate a new Message
27 # bot:: associated bot class
28 # source:: hostmask of the message source
29 # target:: nick/channel message is destined for
30 # message:: message part
31 def initialize(bot, source, target, message)
37 @message = BasicUserMessage.stripcolour message
39 # split source into consituent parts
40 if source =~ /^((\S+)!(\S+))$/
45 if target && target.downcase == @bot.nick.downcase
51 # returns true if the message was addressed to the bot.
52 # This includes any private message to the bot, or any public message
53 # which looks like it's addressed to the bot, e.g. "bot: foo", "bot, foo",
54 # a kick message when bot was kicked etc.
59 # strip mIRC colour escapes from a string
60 def BasicUserMessage.stripcolour(string)
61 return "" unless string
62 ret = string.gsub(/\cC\d\d?(?:,\d\d?)?/, "")
63 #ret.tr!("\x00-\x1f", "")
69 # class for handling IRC user messages. Includes some utilities for handling
70 # the message, for example in plugins.
71 # The +message+ member will have any bot addressing "^bot: " removed
72 # (address? will return true in this case)
73 class UserMessage < BasicUserMessage
75 # for plugin messages, the name of the plugin invoked by the message
78 # for plugin messages, the rest of the message, with the plugin name
82 # convenience member. Who to reply to (i.e. would be sourcenick for a
83 # privately addressed message, or target (the channel) for a publicly
87 # channel the message was in, nil for privately addressed messages
90 # for PRIVMSGs, true if the message was a CTCP ACTION (CTCP stuff
91 # will be stripped from the message)
94 # instantiate a new UserMessage
95 # bot:: associated bot class
96 # source:: hostmask of the message source
97 # target:: nick/channel message is destined for
98 # message:: message part
99 def initialize(bot, source, target, message)
100 super(bot, source, target, message)
106 if target.downcase == @bot.nick.downcase
110 @replyto = @sourcenick
116 # check for option extra addressing prefixes, e.g "|search foo", or
117 # "!version" - first match wins
118 bot.addressing_prefixes.each {|mprefix|
119 if @message.gsub!(/^#{Regexp.escape(mprefix)}\s*/, "")
125 # even if they used above prefixes, we allow for silly people who
126 # combine all possible types, e.g. "|rbot: hello", or
127 # "/msg rbot rbot: hello", etc
128 if @message.gsub!(/^\s*#{bot.nick}\s*([:;,>]|\s)\s*/, "")
132 if(@message =~ /^\001ACTION\s(.+)\001/)
137 # free splitting for plugins
138 @params = @message.dup
139 if @params.gsub!(/^\s*(\S+)[\s$]*/, "")
140 @plugin = $1.downcase
141 @params = nil unless @params.length > 0
145 # returns true for private messages, e.g. "/msg bot hello"
150 # returns true if the message was in a channel
159 # convenience method to reply to a message, useful in plugins. It's the
161 # <tt>@bot.say m.replyto, string</tt>
162 # So if the message is private, it will reply to the user. If it was
163 # in a channel, it will reply in the channel.
165 @bot.say @replyto, string
170 # class to manage IRC PRIVMSGs
171 class PrivMessage < UserMessage
174 # class to manage IRC NOTICEs
175 class NoticeMessage < UserMessage
178 # class to manage IRC KICKs
179 # +address?+ can be used as a shortcut to see if the bot was kicked,
180 # basically, +target+ was kicked from +channel+ by +source+ with +message+
181 class KickMessage < BasicUserMessage
182 # channel user was kicked from
185 def initialize(bot, source, target, channel, message="")
186 super(bot, source, target, message)
191 # class to pass IRC Nick changes in. @message contains the old nickame,
192 # @sourcenick contains the new one.
193 class NickMessage < BasicUserMessage
194 def initialize(bot, source, oldnick, newnick)
195 super(bot, source, oldnick, newnick)
199 class QuitMessage < BasicUserMessage
200 def initialize(bot, source, target, message="")
201 super(bot, source, target, message)
205 class TopicMessage < BasicUserMessage
208 # topic set at (unixtime)
209 attr_reader :timestamp
210 # topic set on channel
213 def initialize(bot, source, channel, timestamp, topic="")
214 super(bot, source, channel, topic)
216 @timestamp = timestamp
221 # class to manage channel joins
222 class JoinMessage < BasicUserMessage
225 def initialize(bot, source, channel, message="")
226 super(bot, source, channel, message)
228 # in this case sourcenick is the nick that could be the bot
229 @address = (sourcenick.downcase == @bot.nick.downcase)
233 # class to manage channel parts
234 # same as a join, but can have a message too
235 class PartMessage < JoinMessage