Class: Irc::BasicUserMessage

Inherits:
Object
  • Object
show all
Defined in:
lib/rbot/message.rb,
lib/rbot/core/utils/extends.rb

Overview

base user message class, all user messages derive from this (a user message is defined as having a source hostmask, a target nick/channel and a message part)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bot, server, source, target, message) ⇒ BasicUserMessage

instantiate a new Message

bot

associated bot class

server

Server where the message took place

source

User that sent the message

target

User/Channel is destined for

message

actual message



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/rbot/message.rb', line 179

def initialize(bot, server, source, target, message)
  @msg_wants_id = false unless defined? @msg_wants_id

  @time = Time.now
  @bot = bot
  @source = source
  @address = false
  @prefixed = false
  @target = target
  @message = message || ""
  @replied = false
  @server = server
  @ignored = false
  @in_thread = false

  @identified = false
  if @msg_wants_id && @server.capabilities[:"identify-msg"]
    if @message =~ /^([-+])(.*)/
      @identified = ($1=="+")
      @message = $2
    else
      warning "Message does not have identification"
    end
  end
  @logmessage = @message.dup
  @plainmessage = BasicUserMessage.strip_formatting(@message)
  @message = BasicUserMessage.strip_initial_formatting(@message)

  if target && target == @bot.myself
    @address = true
  end

end

Instance Attribute Details

#botObject (readonly)

associated bot



115
116
117
# File 'lib/rbot/message.rb', line 115

def bot
  @bot
end

#ignoredObject Also known as: ignored?

should the message be ignored?



143
144
145
# File 'lib/rbot/message.rb', line 143

def ignored
  @ignored
end

#in_threadObject Also known as: in_thread?

set this to true if the method that delegates the message is run in a thread



147
148
149
# File 'lib/rbot/message.rb', line 147

def in_thread
  @in_thread
end

#logmessageObject

contents of the message (for logging purposes)



133
134
135
# File 'lib/rbot/message.rb', line 133

def logmessage
  @logmessage
end

#messageObject

contents of the message (stripped of initial/final format codes)



130
131
132
# File 'lib/rbot/message.rb', line 130

def message
  @message
end

#plainmessageObject

contents of the message (stripped of all formatting)



136
137
138
# File 'lib/rbot/message.rb', line 136

def plainmessage
  @plainmessage
end

#repliedObject Also known as: replied?

has the message been replied to/handled by a plugin?



139
140
141
# File 'lib/rbot/message.rb', line 139

def replied
  @replied
end

#serverObject (readonly)

associated server



118
119
120
# File 'lib/rbot/message.rb', line 118

def server
  @server
end

#sourceObject (readonly)

User that originated the message



124
125
126
# File 'lib/rbot/message.rb', line 124

def source
  @source
end

#targetObject (readonly)

User/Channel message was sent to



127
128
129
# File 'lib/rbot/message.rb', line 127

def target
  @target
end

#timeObject (readonly)

when the message was received



121
122
123
# File 'lib/rbot/message.rb', line 121

def time
  @time
end

Class Method Details

.strip_formatting(string) ⇒ Object



264
265
266
# File 'lib/rbot/message.rb', line 264

def BasicUserMessage.strip_formatting(string)
  string.gsub(FormattingRx,"")
end

.strip_initial_formatting(string) ⇒ Object



259
260
261
262
# File 'lib/rbot/message.rb', line 259

def BasicUserMessage.strip_initial_formatting(string)
  return "" unless string
  ret = string.gsub(/^#{FormattingRx}|#{FormattingRx}$/,"")
end

.stripcolour(string) ⇒ Object

strip mIRC colour escapes from a string



252
253
254
255
256
257
# File 'lib/rbot/message.rb', line 252

def BasicUserMessage.stripcolour(string)
  return "" unless string
  ret = string.gsub(ColorRx, "")
  #ret.tr!("\x00-\x1f", "")
  ret
end

Instance Method Details

#address?Boolean

returns true if the message was addressed to the bot. This includes any private message to the bot, or any public message which looks like it’s addressed to the bot, e.g. “bot: foo”, “bot, foo”, a kick message when bot was kicked etc.

Returns:

  • (Boolean)


241
242
243
# File 'lib/rbot/message.rb', line 241

def address?
  return @address
end

#botuserObject

Access the botuser corresponding to the source, if any



227
228
229
# File 'lib/rbot/message.rb', line 227

def botuser
  source.botuser rescue @bot.auth.everyone
end

#identified?Boolean

Was the message from an identified user?

Returns:

  • (Boolean)


233
234
235
# File 'lib/rbot/message.rb', line 233

def identified?
  return @identified
end

#inspect(fields = nil) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/rbot/message.rb', line 150

def inspect(fields=nil)
  ret = self.__to_s__[0..-2]
  ret << ' bot=' << @bot.__to_s__
  ret << ' server=' << server.to_s
  ret << ' time=' << time.to_s
  ret << ' source=' << source.to_s
  ret << ' target=' << target.to_s
  ret << ' message=' << message.inspect
  ret << ' logmessage=' << logmessage.inspect
  ret << ' plainmessage=' << plainmessage.inspect
  ret << fields if fields
  ret << ' (identified)' if identified?
  if address?
    ret << ' (addressed to me'
    ret << ', with prefix' if prefixed?
    ret << ')'
  end
  ret << ' (replied)' if replied?
  ret << ' (ignored)' if ignored?
  ret << ' (in thread)' if in_thread?
  ret << '>'
end

#parse_channel_list(string) ⇒ Object

We extend the BasicUserMessage class with a method that parses a string which is a channel list as matched by IN_CHAN(_LIST) and co. The method returns an array of channel names, where ‘private’ or ‘pvt’ is replaced by the Symbol :“?”, ‘here’ is replaced by the channel of the message or by :“?” (depending on whether the message target is the bot or a Channel), and ‘anywhere’ and ‘everywhere’ are replaced by Symbol :*



421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
# File 'lib/rbot/core/utils/extends.rb', line 421

def parse_channel_list(string)
  return [:*] if [:anywhere, :everywhere].include? string.to_sym
  string.scan(
  /(?:^|,?(?:\s+and)?\s+)(?:in|on\s+)?(#{Regexp::Irc::GEN_CHAN}|here|private|pvt)/
             ).map { |chan_ar|
    chan = chan_ar.first
    case chan.to_sym
    when :private, :pvt
      :"?"
    when :here
      case self.target
      when Channel
        self.target.name
      else
        :"?"
      end
    else
      chan
    end
  }.uniq
end

#prefixed?Boolean

returns true if the messaged was addressed to the bot via the address prefix. This can be used to tell appart “!do this” from “botname, do this”

Returns:

  • (Boolean)


247
248
249
# File 'lib/rbot/message.rb', line 247

def prefixed?
  return @prefixed
end

#recurse_depthObject

The recurse depth of a message, for fake messages. 0 means an original message



445
446
447
448
449
450
# File 'lib/rbot/core/utils/extends.rb', line 445

def recurse_depth
  unless defined? @recurse_depth
    @recurse_depth = 0
  end
  @recurse_depth
end

#recurse_depth=(val) ⇒ Object

Set the recurse depth of a message, for fake messages. 0 should only be used by original messages



454
455
456
# File 'lib/rbot/core/utils/extends.rb', line 454

def recurse_depth=(val)
  @recurse_depth = val
end

#sourceaddressObject

Access the user@host of the source



221
222
223
# File 'lib/rbot/message.rb', line 221

def sourceaddress
  "#{@source.user}@#{@source.host}" rescue @source.to_s
end

#sourcenickObject

Access the nick of the source



215
216
217
# File 'lib/rbot/message.rb', line 215

def sourcenick
  @source.nick rescue @source.to_s
end