Class: IIRC::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/iirc/event.rb

Direct Known Subclasses

IRCv3::Batch

Constant Summary collapse

CTCP_REGEX =
/^\x01(\w+)(?: ([^\x01]+))?\x01?$/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sender: nil, verb: nil, args: nil, tags: nil) ⇒ Event

Returns a new instance of Event.



5
6
7
8
9
10
# File 'lib/iirc/event.rb', line 5

def initialize(sender: nil, verb: nil, args: nil, tags: nil)
  self.sender = sender
  self.verb = verb
  self.args = args
  self.tags = tags
end

Instance Attribute Details

#argsObject

Returns the value of attribute args.



3
4
5
# File 'lib/iirc/event.rb', line 3

def args
  @args
end

#senderObject

Returns the value of attribute sender.



3
4
5
# File 'lib/iirc/event.rb', line 3

def sender
  @sender
end

#tagsObject

Returns the value of attribute tags.



3
4
5
# File 'lib/iirc/event.rb', line 3

def tags
  @tags
end

#verbObject

Returns the value of attribute verb.



3
4
5
# File 'lib/iirc/event.rb', line 3

def verb
  @verb
end

Instance Method Details

#ctcpArray<Symbol, String?>?

Parses CTCP data in message, returning nil or an Array like [:action, "does the Macarena"], [:version], [:version, "SomeClient v0.1"]

Examples:

Pattern matching (case)

case evt.ctcp
in [:action, msg]
  say "#{evt.nick} did an action: #{italic msg}"
in [:version]
  say "\x01VERSION Ruby IIRC (v#{IIRC::VERSION})\x01"
in [:dcc, type, *]
  say "You want to start a DCC #{type} with me? Are you crazy!?"
else
end

Pattern matching (if)

on(:privmsg, :be_annoying) { |evt|
  act "#{thing} also" if [:action, thing] in evt.ctcp
}

Returns:

  • (Array<Symbol, String?>, nil)

    CTCP verb and text content, if any (e.g. [:action, "says hello"], [:clientinfo], [:version, "SomeClient v0.1"])



62
63
64
# File 'lib/iirc/event.rb', line 62

def ctcp
  [ctcp_verb, ctcp_message].compact if ctcp?
end

#ctcp?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/iirc/event.rb', line 34

def ctcp?
  message&.match?(CTCP_REGEX)
end

#ctcp_messageObject



42
43
44
# File 'lib/iirc/event.rb', line 42

def ctcp_message
  $2 if message =~ CTCP_REGEX
end

#ctcp_verbObject



38
39
40
# File 'lib/iirc/event.rb', line 38

def ctcp_verb
  $1.downcase.to_sym if message =~ CTCP_REGEX
end

#messageObject



28
29
30
# File 'lib/iirc/event.rb', line 28

def message
  args.last
end

#nickObject



20
21
22
# File 'lib/iirc/event.rb', line 20

def nick
  sender&.nick
end

#targetObject



24
25
26
# File 'lib/iirc/event.rb', line 24

def target
  args.first
end