Class: IIRC::Bot

Inherits:
Object
  • Object
show all
Includes:
Configure, Hooks, Parsing, Pong, Reading, ReplyTarget, TrackOwnNick
Defined in:
lib/iirc.rb,
lib/iirc/bot.rb

Direct Known Subclasses

IRCv3Bot

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ReplyTarget

#reply_target

Methods included from TrackOwnNick

#configure_own_nick_tracking, #track_own_nick_change, #track_own_nick_from_welcome

Methods included from Pong

#on_ping

Methods included from Configure

#configure!, #configure_actions, included

Methods included from Hooks

#>>, #call, #fire!, #hook, #hook_seq, #hooks, #off, #on, #run

Methods included from Parsing

#parse

Methods included from Reading

#lines

Constructor Details

#initialize(socket = nil, user = {}) ⇒ Bot

Returns a new instance of Bot.



20
21
22
23
24
25
26
27
28
# File 'lib/iirc/bot.rb', line 20

def initialize(socket=nil, user={})
  super() if defined? super
  self.socket = socket
  self.user = user.is_a?(User) ? user : User.new(**user)

  if !user.key?(:nick)
    self.user.nick ||= self.class.name&.split('::')&.last
  end
end

Instance Attribute Details

#socketObject

Returns the value of attribute socket.



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

def socket
  @socket
end

#userObject Also known as: me

Returns the value of attribute user.



4
5
6
# File 'lib/iirc/bot.rb', line 4

def user
  @user
end

Class Method Details

.run(host, port = 6697, local_host: nil, local_port: nil, ssl_context: SSL.default_context, **user_params, &blk) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/iirc.rb', line 47

def self.run(host, port=6697, local_host: nil, local_port: nil, ssl_context: SSL.default_context, **user_params, &blk)
  socket = IIRC.dial(host, port,
    local_host: local_host, local_port: local_port,
    ssl_context: ssl_context)

  new(socket, **user_params).tap { |bot|
    bot.register!
    bot.tap(&blk) if blk
    bot.run
  }
end

Instance Method Details

#<<(text) ⇒ Object



7
8
9
10
# File 'lib/iirc/bot.rb', line 7

def <<(text)
  socket << text.tr("\r", "").tr("\n", "") + "\r\n"
  self
end

#ensure_registration_info!Object (private)

Raises:

  • (ArgumentError)


36
37
38
39
40
# File 'lib/iirc/bot.rb', line 36

private def ensure_registration_info!
  me.username ||= me.nick&.downcase
  me.realname ||= me.nick
  raise ArgumentError.new('no nick given') unless me.nick && !me.nick.empty?
end

#inspectObject



16
17
18
# File 'lib/iirc/bot.rb', line 16

def inspect
  "<#{me}>"
end

#register!Object



30
31
32
33
34
# File 'lib/iirc/bot.rb', line 30

def register!
  ensure_registration_info!
  self << "USER #{me.username} 0 0 :#{me.realname}"
  self << "NICK #{me.nick}"
end

#to_sObject



12
13
14
# File 'lib/iirc/bot.rb', line 12

def to_s
  "[#{self.class.name}:#{user.nick}]"
end