Class: ProtonBot::Plug

Inherits:
Object
  • Object
show all
Defined in:
lib/protonbot/plug.rb,
lib/protonbot/plug_io.rb,
lib/protonbot/plug_utils.rb,
lib/protonbot/plug_events.rb

Overview

Plug. This class includes socket, writer and reader threads, user&channel-data, API for interacting with server etc.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bot, name, conf) ⇒ Plug

Returns a new instance of Plug.

Parameters:

  • bot (Bot)
  • name (String)
  • conf (Hash<String>)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/protonbot/plug.rb', line 49

def initialize(bot, name, conf)
  @bot         = bot
  @name        = name
  @db          = @bot.dbs[@name]
  @log         = @bot._log.wrap("!#{@name}")
  @conf        = conf
  @nick        = conf['nick']
  @user        = conf['user']
  @rnam        = conf['rnam']
  @sock        = nil
  @running     = false
  @queue       = Queue.new
  @chans       = {}
  @users       = {}
  @event_locks = []
end

Instance Attribute Details

#botProtonBot::Bot (readonly)

Returns Plug’s bot.

Returns:



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/protonbot/plug.rb', line 41

class ProtonBot::Plug
  attr_reader :bot, :db, :sock, :rsock, :name, :conf, :rloop, :wloop, :log, :queue, :chans, :users,
              :event_locks, :user, :nick, :rnam, :is_ssl
  attr_accessor :running

  # @param bot [Bot]
  # @param name [String]
  # @param conf [Hash<String>]
  def initialize(bot, name, conf)
    @bot         = bot
    @name        = name
    @db          = @bot.dbs[@name]
    @log         = @bot._log.wrap("!#{@name}")
    @conf        = conf
    @nick        = conf['nick']
    @user        = conf['user']
    @rnam        = conf['rnam']
    @sock        = nil
    @running     = false
    @queue       = Queue.new
    @chans       = {}
    @users       = {}
    @event_locks = []
  end

  # Connects to server, introduces and starts reader and writer threads.
  # @return [Plug] self
  def connect!
    if @conf['ssl'] == nil or @conf['ssl'] == false
      @sock = @rsock = TCPSocket.new(@conf['host'], @conf['port'])
      @is_ssl = false
    else
      @rsock = TCPSocket.new(@conf['host'], @conf['port'])
      @ssl_ctx = OpenSSL::SSL::SSLContext.new
      @ssl_ctx.verify_mode = OpenSSL::SSL::VERIFY_NONE
      @ssl_ctx.ssl_version = :SSLv23
      @ssl_ctx.cert = OpenSSL::X509::Certificate.new(File.read(File.expand_path(@conf['ssl_crt']))) if
        @conf['ssl_crt']
      @ssl_ctx.key = OpenSSL::PKey::RSA.new(File.read(File.expand_path(@conf['ssl_key']))) if
        @conf['ssl_key']
      @sock = OpenSSL::SSL::SSLSocket.new(@rsock, @ssl_ctx)
      @sock.sync = true
      @sock.connect
      @is_ssl = true
    end

    @running = true

    @rloop = Thread.new { readloop }
    @wloop = Thread.new { writeloop }

    log.info("Started plug `#{name}` successfully!")

    introduce

    @rloop.join
    @wloop.join
    self
  end

  # Logs given error object to cosole
  # @param e [Exception]
  # @return [Plug] self
  def log_err(e)
    @log.error('Error!')
    @log.error("> Message: #{e.message}")
    @log.error('> Backtrace:')
    e.backtrace.each do |i|
      @log.error(">> #{i}")
    end
    self
  end

  # @return [String] out
  def inspect
    %(<#ProtonBot::Plug:#{object_id.to_s(16)} @name=#{name} @bot=#{bot}>)
  end
end

#chansHash<String> (readonly)

Returns Channel data.

Returns:

  • (Hash<String>)

    Channel data.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/protonbot/plug.rb', line 41

class ProtonBot::Plug
  attr_reader :bot, :db, :sock, :rsock, :name, :conf, :rloop, :wloop, :log, :queue, :chans, :users,
              :event_locks, :user, :nick, :rnam, :is_ssl
  attr_accessor :running

  # @param bot [Bot]
  # @param name [String]
  # @param conf [Hash<String>]
  def initialize(bot, name, conf)
    @bot         = bot
    @name        = name
    @db          = @bot.dbs[@name]
    @log         = @bot._log.wrap("!#{@name}")
    @conf        = conf
    @nick        = conf['nick']
    @user        = conf['user']
    @rnam        = conf['rnam']
    @sock        = nil
    @running     = false
    @queue       = Queue.new
    @chans       = {}
    @users       = {}
    @event_locks = []
  end

  # Connects to server, introduces and starts reader and writer threads.
  # @return [Plug] self
  def connect!
    if @conf['ssl'] == nil or @conf['ssl'] == false
      @sock = @rsock = TCPSocket.new(@conf['host'], @conf['port'])
      @is_ssl = false
    else
      @rsock = TCPSocket.new(@conf['host'], @conf['port'])
      @ssl_ctx = OpenSSL::SSL::SSLContext.new
      @ssl_ctx.verify_mode = OpenSSL::SSL::VERIFY_NONE
      @ssl_ctx.ssl_version = :SSLv23
      @ssl_ctx.cert = OpenSSL::X509::Certificate.new(File.read(File.expand_path(@conf['ssl_crt']))) if
        @conf['ssl_crt']
      @ssl_ctx.key = OpenSSL::PKey::RSA.new(File.read(File.expand_path(@conf['ssl_key']))) if
        @conf['ssl_key']
      @sock = OpenSSL::SSL::SSLSocket.new(@rsock, @ssl_ctx)
      @sock.sync = true
      @sock.connect
      @is_ssl = true
    end

    @running = true

    @rloop = Thread.new { readloop }
    @wloop = Thread.new { writeloop }

    log.info("Started plug `#{name}` successfully!")

    introduce

    @rloop.join
    @wloop.join
    self
  end

  # Logs given error object to cosole
  # @param e [Exception]
  # @return [Plug] self
  def log_err(e)
    @log.error('Error!')
    @log.error("> Message: #{e.message}")
    @log.error('> Backtrace:')
    e.backtrace.each do |i|
      @log.error(">> #{i}")
    end
    self
  end

  # @return [String] out
  def inspect
    %(<#ProtonBot::Plug:#{object_id.to_s(16)} @name=#{name} @bot=#{bot}>)
  end
end

#confHash<String> (readonly)

Returns Config.

Returns:

  • (Hash<String>)

    Config.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/protonbot/plug.rb', line 41

class ProtonBot::Plug
  attr_reader :bot, :db, :sock, :rsock, :name, :conf, :rloop, :wloop, :log, :queue, :chans, :users,
              :event_locks, :user, :nick, :rnam, :is_ssl
  attr_accessor :running

  # @param bot [Bot]
  # @param name [String]
  # @param conf [Hash<String>]
  def initialize(bot, name, conf)
    @bot         = bot
    @name        = name
    @db          = @bot.dbs[@name]
    @log         = @bot._log.wrap("!#{@name}")
    @conf        = conf
    @nick        = conf['nick']
    @user        = conf['user']
    @rnam        = conf['rnam']
    @sock        = nil
    @running     = false
    @queue       = Queue.new
    @chans       = {}
    @users       = {}
    @event_locks = []
  end

  # Connects to server, introduces and starts reader and writer threads.
  # @return [Plug] self
  def connect!
    if @conf['ssl'] == nil or @conf['ssl'] == false
      @sock = @rsock = TCPSocket.new(@conf['host'], @conf['port'])
      @is_ssl = false
    else
      @rsock = TCPSocket.new(@conf['host'], @conf['port'])
      @ssl_ctx = OpenSSL::SSL::SSLContext.new
      @ssl_ctx.verify_mode = OpenSSL::SSL::VERIFY_NONE
      @ssl_ctx.ssl_version = :SSLv23
      @ssl_ctx.cert = OpenSSL::X509::Certificate.new(File.read(File.expand_path(@conf['ssl_crt']))) if
        @conf['ssl_crt']
      @ssl_ctx.key = OpenSSL::PKey::RSA.new(File.read(File.expand_path(@conf['ssl_key']))) if
        @conf['ssl_key']
      @sock = OpenSSL::SSL::SSLSocket.new(@rsock, @ssl_ctx)
      @sock.sync = true
      @sock.connect
      @is_ssl = true
    end

    @running = true

    @rloop = Thread.new { readloop }
    @wloop = Thread.new { writeloop }

    log.info("Started plug `#{name}` successfully!")

    introduce

    @rloop.join
    @wloop.join
    self
  end

  # Logs given error object to cosole
  # @param e [Exception]
  # @return [Plug] self
  def log_err(e)
    @log.error('Error!')
    @log.error("> Message: #{e.message}")
    @log.error('> Backtrace:')
    e.backtrace.each do |i|
      @log.error(">> #{i}")
    end
    self
  end

  # @return [String] out
  def inspect
    %(<#ProtonBot::Plug:#{object_id.to_s(16)} @name=#{name} @bot=#{bot}>)
  end
end

#dbHeliodor::DB (readonly)

Returns Plug’s Heliodor-powered database.

Returns:

  • (Heliodor::DB)

    Plug’s Heliodor-powered database.

See Also:



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/protonbot/plug.rb', line 41

class ProtonBot::Plug
  attr_reader :bot, :db, :sock, :rsock, :name, :conf, :rloop, :wloop, :log, :queue, :chans, :users,
              :event_locks, :user, :nick, :rnam, :is_ssl
  attr_accessor :running

  # @param bot [Bot]
  # @param name [String]
  # @param conf [Hash<String>]
  def initialize(bot, name, conf)
    @bot         = bot
    @name        = name
    @db          = @bot.dbs[@name]
    @log         = @bot._log.wrap("!#{@name}")
    @conf        = conf
    @nick        = conf['nick']
    @user        = conf['user']
    @rnam        = conf['rnam']
    @sock        = nil
    @running     = false
    @queue       = Queue.new
    @chans       = {}
    @users       = {}
    @event_locks = []
  end

  # Connects to server, introduces and starts reader and writer threads.
  # @return [Plug] self
  def connect!
    if @conf['ssl'] == nil or @conf['ssl'] == false
      @sock = @rsock = TCPSocket.new(@conf['host'], @conf['port'])
      @is_ssl = false
    else
      @rsock = TCPSocket.new(@conf['host'], @conf['port'])
      @ssl_ctx = OpenSSL::SSL::SSLContext.new
      @ssl_ctx.verify_mode = OpenSSL::SSL::VERIFY_NONE
      @ssl_ctx.ssl_version = :SSLv23
      @ssl_ctx.cert = OpenSSL::X509::Certificate.new(File.read(File.expand_path(@conf['ssl_crt']))) if
        @conf['ssl_crt']
      @ssl_ctx.key = OpenSSL::PKey::RSA.new(File.read(File.expand_path(@conf['ssl_key']))) if
        @conf['ssl_key']
      @sock = OpenSSL::SSL::SSLSocket.new(@rsock, @ssl_ctx)
      @sock.sync = true
      @sock.connect
      @is_ssl = true
    end

    @running = true

    @rloop = Thread.new { readloop }
    @wloop = Thread.new { writeloop }

    log.info("Started plug `#{name}` successfully!")

    introduce

    @rloop.join
    @wloop.join
    self
  end

  # Logs given error object to cosole
  # @param e [Exception]
  # @return [Plug] self
  def log_err(e)
    @log.error('Error!')
    @log.error("> Message: #{e.message}")
    @log.error('> Backtrace:')
    e.backtrace.each do |i|
      @log.error(">> #{i}")
    end
    self
  end

  # @return [String] out
  def inspect
    %(<#ProtonBot::Plug:#{object_id.to_s(16)} @name=#{name} @bot=#{bot}>)
  end
end

#event_locksArray<EventLock> (readonly)

Returns Event locks.

Returns:



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/protonbot/plug.rb', line 41

class ProtonBot::Plug
  attr_reader :bot, :db, :sock, :rsock, :name, :conf, :rloop, :wloop, :log, :queue, :chans, :users,
              :event_locks, :user, :nick, :rnam, :is_ssl
  attr_accessor :running

  # @param bot [Bot]
  # @param name [String]
  # @param conf [Hash<String>]
  def initialize(bot, name, conf)
    @bot         = bot
    @name        = name
    @db          = @bot.dbs[@name]
    @log         = @bot._log.wrap("!#{@name}")
    @conf        = conf
    @nick        = conf['nick']
    @user        = conf['user']
    @rnam        = conf['rnam']
    @sock        = nil
    @running     = false
    @queue       = Queue.new
    @chans       = {}
    @users       = {}
    @event_locks = []
  end

  # Connects to server, introduces and starts reader and writer threads.
  # @return [Plug] self
  def connect!
    if @conf['ssl'] == nil or @conf['ssl'] == false
      @sock = @rsock = TCPSocket.new(@conf['host'], @conf['port'])
      @is_ssl = false
    else
      @rsock = TCPSocket.new(@conf['host'], @conf['port'])
      @ssl_ctx = OpenSSL::SSL::SSLContext.new
      @ssl_ctx.verify_mode = OpenSSL::SSL::VERIFY_NONE
      @ssl_ctx.ssl_version = :SSLv23
      @ssl_ctx.cert = OpenSSL::X509::Certificate.new(File.read(File.expand_path(@conf['ssl_crt']))) if
        @conf['ssl_crt']
      @ssl_ctx.key = OpenSSL::PKey::RSA.new(File.read(File.expand_path(@conf['ssl_key']))) if
        @conf['ssl_key']
      @sock = OpenSSL::SSL::SSLSocket.new(@rsock, @ssl_ctx)
      @sock.sync = true
      @sock.connect
      @is_ssl = true
    end

    @running = true

    @rloop = Thread.new { readloop }
    @wloop = Thread.new { writeloop }

    log.info("Started plug `#{name}` successfully!")

    introduce

    @rloop.join
    @wloop.join
    self
  end

  # Logs given error object to cosole
  # @param e [Exception]
  # @return [Plug] self
  def log_err(e)
    @log.error('Error!')
    @log.error("> Message: #{e.message}")
    @log.error('> Backtrace:')
    e.backtrace.each do |i|
      @log.error(">> #{i}")
    end
    self
  end

  # @return [String] out
  def inspect
    %(<#ProtonBot::Plug:#{object_id.to_s(16)} @name=#{name} @bot=#{bot}>)
  end
end

#is_sslBoolean (readonly)

Returns True if connection is SSL-powered.

Returns:

  • (Boolean)

    True if connection is SSL-powered



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/protonbot/plug.rb', line 41

class ProtonBot::Plug
  attr_reader :bot, :db, :sock, :rsock, :name, :conf, :rloop, :wloop, :log, :queue, :chans, :users,
              :event_locks, :user, :nick, :rnam, :is_ssl
  attr_accessor :running

  # @param bot [Bot]
  # @param name [String]
  # @param conf [Hash<String>]
  def initialize(bot, name, conf)
    @bot         = bot
    @name        = name
    @db          = @bot.dbs[@name]
    @log         = @bot._log.wrap("!#{@name}")
    @conf        = conf
    @nick        = conf['nick']
    @user        = conf['user']
    @rnam        = conf['rnam']
    @sock        = nil
    @running     = false
    @queue       = Queue.new
    @chans       = {}
    @users       = {}
    @event_locks = []
  end

  # Connects to server, introduces and starts reader and writer threads.
  # @return [Plug] self
  def connect!
    if @conf['ssl'] == nil or @conf['ssl'] == false
      @sock = @rsock = TCPSocket.new(@conf['host'], @conf['port'])
      @is_ssl = false
    else
      @rsock = TCPSocket.new(@conf['host'], @conf['port'])
      @ssl_ctx = OpenSSL::SSL::SSLContext.new
      @ssl_ctx.verify_mode = OpenSSL::SSL::VERIFY_NONE
      @ssl_ctx.ssl_version = :SSLv23
      @ssl_ctx.cert = OpenSSL::X509::Certificate.new(File.read(File.expand_path(@conf['ssl_crt']))) if
        @conf['ssl_crt']
      @ssl_ctx.key = OpenSSL::PKey::RSA.new(File.read(File.expand_path(@conf['ssl_key']))) if
        @conf['ssl_key']
      @sock = OpenSSL::SSL::SSLSocket.new(@rsock, @ssl_ctx)
      @sock.sync = true
      @sock.connect
      @is_ssl = true
    end

    @running = true

    @rloop = Thread.new { readloop }
    @wloop = Thread.new { writeloop }

    log.info("Started plug `#{name}` successfully!")

    introduce

    @rloop.join
    @wloop.join
    self
  end

  # Logs given error object to cosole
  # @param e [Exception]
  # @return [Plug] self
  def log_err(e)
    @log.error('Error!')
    @log.error("> Message: #{e.message}")
    @log.error('> Backtrace:')
    e.backtrace.each do |i|
      @log.error(">> #{i}")
    end
    self
  end

  # @return [String] out
  def inspect
    %(<#ProtonBot::Plug:#{object_id.to_s(16)} @name=#{name} @bot=#{bot}>)
  end
end

#logProtonBot::LogWrapper (readonly)

Returns Log wrapper.

Returns:



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/protonbot/plug.rb', line 41

class ProtonBot::Plug
  attr_reader :bot, :db, :sock, :rsock, :name, :conf, :rloop, :wloop, :log, :queue, :chans, :users,
              :event_locks, :user, :nick, :rnam, :is_ssl
  attr_accessor :running

  # @param bot [Bot]
  # @param name [String]
  # @param conf [Hash<String>]
  def initialize(bot, name, conf)
    @bot         = bot
    @name        = name
    @db          = @bot.dbs[@name]
    @log         = @bot._log.wrap("!#{@name}")
    @conf        = conf
    @nick        = conf['nick']
    @user        = conf['user']
    @rnam        = conf['rnam']
    @sock        = nil
    @running     = false
    @queue       = Queue.new
    @chans       = {}
    @users       = {}
    @event_locks = []
  end

  # Connects to server, introduces and starts reader and writer threads.
  # @return [Plug] self
  def connect!
    if @conf['ssl'] == nil or @conf['ssl'] == false
      @sock = @rsock = TCPSocket.new(@conf['host'], @conf['port'])
      @is_ssl = false
    else
      @rsock = TCPSocket.new(@conf['host'], @conf['port'])
      @ssl_ctx = OpenSSL::SSL::SSLContext.new
      @ssl_ctx.verify_mode = OpenSSL::SSL::VERIFY_NONE
      @ssl_ctx.ssl_version = :SSLv23
      @ssl_ctx.cert = OpenSSL::X509::Certificate.new(File.read(File.expand_path(@conf['ssl_crt']))) if
        @conf['ssl_crt']
      @ssl_ctx.key = OpenSSL::PKey::RSA.new(File.read(File.expand_path(@conf['ssl_key']))) if
        @conf['ssl_key']
      @sock = OpenSSL::SSL::SSLSocket.new(@rsock, @ssl_ctx)
      @sock.sync = true
      @sock.connect
      @is_ssl = true
    end

    @running = true

    @rloop = Thread.new { readloop }
    @wloop = Thread.new { writeloop }

    log.info("Started plug `#{name}` successfully!")

    introduce

    @rloop.join
    @wloop.join
    self
  end

  # Logs given error object to cosole
  # @param e [Exception]
  # @return [Plug] self
  def log_err(e)
    @log.error('Error!')
    @log.error("> Message: #{e.message}")
    @log.error('> Backtrace:')
    e.backtrace.each do |i|
      @log.error(">> #{i}")
    end
    self
  end

  # @return [String] out
  def inspect
    %(<#ProtonBot::Plug:#{object_id.to_s(16)} @name=#{name} @bot=#{bot}>)
  end
end

#nameString (readonly)

Returns Plug’s name.

Returns:

  • (String)

    Plug’s name.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/protonbot/plug.rb', line 41

class ProtonBot::Plug
  attr_reader :bot, :db, :sock, :rsock, :name, :conf, :rloop, :wloop, :log, :queue, :chans, :users,
              :event_locks, :user, :nick, :rnam, :is_ssl
  attr_accessor :running

  # @param bot [Bot]
  # @param name [String]
  # @param conf [Hash<String>]
  def initialize(bot, name, conf)
    @bot         = bot
    @name        = name
    @db          = @bot.dbs[@name]
    @log         = @bot._log.wrap("!#{@name}")
    @conf        = conf
    @nick        = conf['nick']
    @user        = conf['user']
    @rnam        = conf['rnam']
    @sock        = nil
    @running     = false
    @queue       = Queue.new
    @chans       = {}
    @users       = {}
    @event_locks = []
  end

  # Connects to server, introduces and starts reader and writer threads.
  # @return [Plug] self
  def connect!
    if @conf['ssl'] == nil or @conf['ssl'] == false
      @sock = @rsock = TCPSocket.new(@conf['host'], @conf['port'])
      @is_ssl = false
    else
      @rsock = TCPSocket.new(@conf['host'], @conf['port'])
      @ssl_ctx = OpenSSL::SSL::SSLContext.new
      @ssl_ctx.verify_mode = OpenSSL::SSL::VERIFY_NONE
      @ssl_ctx.ssl_version = :SSLv23
      @ssl_ctx.cert = OpenSSL::X509::Certificate.new(File.read(File.expand_path(@conf['ssl_crt']))) if
        @conf['ssl_crt']
      @ssl_ctx.key = OpenSSL::PKey::RSA.new(File.read(File.expand_path(@conf['ssl_key']))) if
        @conf['ssl_key']
      @sock = OpenSSL::SSL::SSLSocket.new(@rsock, @ssl_ctx)
      @sock.sync = true
      @sock.connect
      @is_ssl = true
    end

    @running = true

    @rloop = Thread.new { readloop }
    @wloop = Thread.new { writeloop }

    log.info("Started plug `#{name}` successfully!")

    introduce

    @rloop.join
    @wloop.join
    self
  end

  # Logs given error object to cosole
  # @param e [Exception]
  # @return [Plug] self
  def log_err(e)
    @log.error('Error!')
    @log.error("> Message: #{e.message}")
    @log.error('> Backtrace:')
    e.backtrace.each do |i|
      @log.error(">> #{i}")
    end
    self
  end

  # @return [String] out
  def inspect
    %(<#ProtonBot::Plug:#{object_id.to_s(16)} @name=#{name} @bot=#{bot}>)
  end
end

#nickString (readonly)

Returns Plug’s nickname.

Returns:

  • (String)

    Plug’s nickname.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/protonbot/plug.rb', line 41

class ProtonBot::Plug
  attr_reader :bot, :db, :sock, :rsock, :name, :conf, :rloop, :wloop, :log, :queue, :chans, :users,
              :event_locks, :user, :nick, :rnam, :is_ssl
  attr_accessor :running

  # @param bot [Bot]
  # @param name [String]
  # @param conf [Hash<String>]
  def initialize(bot, name, conf)
    @bot         = bot
    @name        = name
    @db          = @bot.dbs[@name]
    @log         = @bot._log.wrap("!#{@name}")
    @conf        = conf
    @nick        = conf['nick']
    @user        = conf['user']
    @rnam        = conf['rnam']
    @sock        = nil
    @running     = false
    @queue       = Queue.new
    @chans       = {}
    @users       = {}
    @event_locks = []
  end

  # Connects to server, introduces and starts reader and writer threads.
  # @return [Plug] self
  def connect!
    if @conf['ssl'] == nil or @conf['ssl'] == false
      @sock = @rsock = TCPSocket.new(@conf['host'], @conf['port'])
      @is_ssl = false
    else
      @rsock = TCPSocket.new(@conf['host'], @conf['port'])
      @ssl_ctx = OpenSSL::SSL::SSLContext.new
      @ssl_ctx.verify_mode = OpenSSL::SSL::VERIFY_NONE
      @ssl_ctx.ssl_version = :SSLv23
      @ssl_ctx.cert = OpenSSL::X509::Certificate.new(File.read(File.expand_path(@conf['ssl_crt']))) if
        @conf['ssl_crt']
      @ssl_ctx.key = OpenSSL::PKey::RSA.new(File.read(File.expand_path(@conf['ssl_key']))) if
        @conf['ssl_key']
      @sock = OpenSSL::SSL::SSLSocket.new(@rsock, @ssl_ctx)
      @sock.sync = true
      @sock.connect
      @is_ssl = true
    end

    @running = true

    @rloop = Thread.new { readloop }
    @wloop = Thread.new { writeloop }

    log.info("Started plug `#{name}` successfully!")

    introduce

    @rloop.join
    @wloop.join
    self
  end

  # Logs given error object to cosole
  # @param e [Exception]
  # @return [Plug] self
  def log_err(e)
    @log.error('Error!')
    @log.error("> Message: #{e.message}")
    @log.error('> Backtrace:')
    e.backtrace.each do |i|
      @log.error(">> #{i}")
    end
    self
  end

  # @return [String] out
  def inspect
    %(<#ProtonBot::Plug:#{object_id.to_s(16)} @name=#{name} @bot=#{bot}>)
  end
end

#queueQueue (readonly)

Returns Queue.

Returns:

  • (Queue)

    Queue.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/protonbot/plug.rb', line 41

class ProtonBot::Plug
  attr_reader :bot, :db, :sock, :rsock, :name, :conf, :rloop, :wloop, :log, :queue, :chans, :users,
              :event_locks, :user, :nick, :rnam, :is_ssl
  attr_accessor :running

  # @param bot [Bot]
  # @param name [String]
  # @param conf [Hash<String>]
  def initialize(bot, name, conf)
    @bot         = bot
    @name        = name
    @db          = @bot.dbs[@name]
    @log         = @bot._log.wrap("!#{@name}")
    @conf        = conf
    @nick        = conf['nick']
    @user        = conf['user']
    @rnam        = conf['rnam']
    @sock        = nil
    @running     = false
    @queue       = Queue.new
    @chans       = {}
    @users       = {}
    @event_locks = []
  end

  # Connects to server, introduces and starts reader and writer threads.
  # @return [Plug] self
  def connect!
    if @conf['ssl'] == nil or @conf['ssl'] == false
      @sock = @rsock = TCPSocket.new(@conf['host'], @conf['port'])
      @is_ssl = false
    else
      @rsock = TCPSocket.new(@conf['host'], @conf['port'])
      @ssl_ctx = OpenSSL::SSL::SSLContext.new
      @ssl_ctx.verify_mode = OpenSSL::SSL::VERIFY_NONE
      @ssl_ctx.ssl_version = :SSLv23
      @ssl_ctx.cert = OpenSSL::X509::Certificate.new(File.read(File.expand_path(@conf['ssl_crt']))) if
        @conf['ssl_crt']
      @ssl_ctx.key = OpenSSL::PKey::RSA.new(File.read(File.expand_path(@conf['ssl_key']))) if
        @conf['ssl_key']
      @sock = OpenSSL::SSL::SSLSocket.new(@rsock, @ssl_ctx)
      @sock.sync = true
      @sock.connect
      @is_ssl = true
    end

    @running = true

    @rloop = Thread.new { readloop }
    @wloop = Thread.new { writeloop }

    log.info("Started plug `#{name}` successfully!")

    introduce

    @rloop.join
    @wloop.join
    self
  end

  # Logs given error object to cosole
  # @param e [Exception]
  # @return [Plug] self
  def log_err(e)
    @log.error('Error!')
    @log.error("> Message: #{e.message}")
    @log.error('> Backtrace:')
    e.backtrace.each do |i|
      @log.error(">> #{i}")
    end
    self
  end

  # @return [String] out
  def inspect
    %(<#ProtonBot::Plug:#{object_id.to_s(16)} @name=#{name} @bot=#{bot}>)
  end
end

#rloopThread (readonly)

Returns Reader-loop-thread.

Returns:

  • (Thread)

    Reader-loop-thread.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/protonbot/plug.rb', line 41

class ProtonBot::Plug
  attr_reader :bot, :db, :sock, :rsock, :name, :conf, :rloop, :wloop, :log, :queue, :chans, :users,
              :event_locks, :user, :nick, :rnam, :is_ssl
  attr_accessor :running

  # @param bot [Bot]
  # @param name [String]
  # @param conf [Hash<String>]
  def initialize(bot, name, conf)
    @bot         = bot
    @name        = name
    @db          = @bot.dbs[@name]
    @log         = @bot._log.wrap("!#{@name}")
    @conf        = conf
    @nick        = conf['nick']
    @user        = conf['user']
    @rnam        = conf['rnam']
    @sock        = nil
    @running     = false
    @queue       = Queue.new
    @chans       = {}
    @users       = {}
    @event_locks = []
  end

  # Connects to server, introduces and starts reader and writer threads.
  # @return [Plug] self
  def connect!
    if @conf['ssl'] == nil or @conf['ssl'] == false
      @sock = @rsock = TCPSocket.new(@conf['host'], @conf['port'])
      @is_ssl = false
    else
      @rsock = TCPSocket.new(@conf['host'], @conf['port'])
      @ssl_ctx = OpenSSL::SSL::SSLContext.new
      @ssl_ctx.verify_mode = OpenSSL::SSL::VERIFY_NONE
      @ssl_ctx.ssl_version = :SSLv23
      @ssl_ctx.cert = OpenSSL::X509::Certificate.new(File.read(File.expand_path(@conf['ssl_crt']))) if
        @conf['ssl_crt']
      @ssl_ctx.key = OpenSSL::PKey::RSA.new(File.read(File.expand_path(@conf['ssl_key']))) if
        @conf['ssl_key']
      @sock = OpenSSL::SSL::SSLSocket.new(@rsock, @ssl_ctx)
      @sock.sync = true
      @sock.connect
      @is_ssl = true
    end

    @running = true

    @rloop = Thread.new { readloop }
    @wloop = Thread.new { writeloop }

    log.info("Started plug `#{name}` successfully!")

    introduce

    @rloop.join
    @wloop.join
    self
  end

  # Logs given error object to cosole
  # @param e [Exception]
  # @return [Plug] self
  def log_err(e)
    @log.error('Error!')
    @log.error("> Message: #{e.message}")
    @log.error('> Backtrace:')
    e.backtrace.each do |i|
      @log.error(">> #{i}")
    end
    self
  end

  # @return [String] out
  def inspect
    %(<#ProtonBot::Plug:#{object_id.to_s(16)} @name=#{name} @bot=#{bot}>)
  end
end

#rnamString (readonly)

Returns Plug’s realname.

Returns:

  • (String)

    Plug’s realname.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/protonbot/plug.rb', line 41

class ProtonBot::Plug
  attr_reader :bot, :db, :sock, :rsock, :name, :conf, :rloop, :wloop, :log, :queue, :chans, :users,
              :event_locks, :user, :nick, :rnam, :is_ssl
  attr_accessor :running

  # @param bot [Bot]
  # @param name [String]
  # @param conf [Hash<String>]
  def initialize(bot, name, conf)
    @bot         = bot
    @name        = name
    @db          = @bot.dbs[@name]
    @log         = @bot._log.wrap("!#{@name}")
    @conf        = conf
    @nick        = conf['nick']
    @user        = conf['user']
    @rnam        = conf['rnam']
    @sock        = nil
    @running     = false
    @queue       = Queue.new
    @chans       = {}
    @users       = {}
    @event_locks = []
  end

  # Connects to server, introduces and starts reader and writer threads.
  # @return [Plug] self
  def connect!
    if @conf['ssl'] == nil or @conf['ssl'] == false
      @sock = @rsock = TCPSocket.new(@conf['host'], @conf['port'])
      @is_ssl = false
    else
      @rsock = TCPSocket.new(@conf['host'], @conf['port'])
      @ssl_ctx = OpenSSL::SSL::SSLContext.new
      @ssl_ctx.verify_mode = OpenSSL::SSL::VERIFY_NONE
      @ssl_ctx.ssl_version = :SSLv23
      @ssl_ctx.cert = OpenSSL::X509::Certificate.new(File.read(File.expand_path(@conf['ssl_crt']))) if
        @conf['ssl_crt']
      @ssl_ctx.key = OpenSSL::PKey::RSA.new(File.read(File.expand_path(@conf['ssl_key']))) if
        @conf['ssl_key']
      @sock = OpenSSL::SSL::SSLSocket.new(@rsock, @ssl_ctx)
      @sock.sync = true
      @sock.connect
      @is_ssl = true
    end

    @running = true

    @rloop = Thread.new { readloop }
    @wloop = Thread.new { writeloop }

    log.info("Started plug `#{name}` successfully!")

    introduce

    @rloop.join
    @wloop.join
    self
  end

  # Logs given error object to cosole
  # @param e [Exception]
  # @return [Plug] self
  def log_err(e)
    @log.error('Error!')
    @log.error("> Message: #{e.message}")
    @log.error('> Backtrace:')
    e.backtrace.each do |i|
      @log.error(">> #{i}")
    end
    self
  end

  # @return [String] out
  def inspect
    %(<#ProtonBot::Plug:#{object_id.to_s(16)} @name=#{name} @bot=#{bot}>)
  end
end

#rsockTCPSocket (readonly)

Returns Always non-SSL socket.

Returns:

  • (TCPSocket)

    Always non-SSL socket.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/protonbot/plug.rb', line 41

class ProtonBot::Plug
  attr_reader :bot, :db, :sock, :rsock, :name, :conf, :rloop, :wloop, :log, :queue, :chans, :users,
              :event_locks, :user, :nick, :rnam, :is_ssl
  attr_accessor :running

  # @param bot [Bot]
  # @param name [String]
  # @param conf [Hash<String>]
  def initialize(bot, name, conf)
    @bot         = bot
    @name        = name
    @db          = @bot.dbs[@name]
    @log         = @bot._log.wrap("!#{@name}")
    @conf        = conf
    @nick        = conf['nick']
    @user        = conf['user']
    @rnam        = conf['rnam']
    @sock        = nil
    @running     = false
    @queue       = Queue.new
    @chans       = {}
    @users       = {}
    @event_locks = []
  end

  # Connects to server, introduces and starts reader and writer threads.
  # @return [Plug] self
  def connect!
    if @conf['ssl'] == nil or @conf['ssl'] == false
      @sock = @rsock = TCPSocket.new(@conf['host'], @conf['port'])
      @is_ssl = false
    else
      @rsock = TCPSocket.new(@conf['host'], @conf['port'])
      @ssl_ctx = OpenSSL::SSL::SSLContext.new
      @ssl_ctx.verify_mode = OpenSSL::SSL::VERIFY_NONE
      @ssl_ctx.ssl_version = :SSLv23
      @ssl_ctx.cert = OpenSSL::X509::Certificate.new(File.read(File.expand_path(@conf['ssl_crt']))) if
        @conf['ssl_crt']
      @ssl_ctx.key = OpenSSL::PKey::RSA.new(File.read(File.expand_path(@conf['ssl_key']))) if
        @conf['ssl_key']
      @sock = OpenSSL::SSL::SSLSocket.new(@rsock, @ssl_ctx)
      @sock.sync = true
      @sock.connect
      @is_ssl = true
    end

    @running = true

    @rloop = Thread.new { readloop }
    @wloop = Thread.new { writeloop }

    log.info("Started plug `#{name}` successfully!")

    introduce

    @rloop.join
    @wloop.join
    self
  end

  # Logs given error object to cosole
  # @param e [Exception]
  # @return [Plug] self
  def log_err(e)
    @log.error('Error!')
    @log.error("> Message: #{e.message}")
    @log.error('> Backtrace:')
    e.backtrace.each do |i|
      @log.error(">> #{i}")
    end
    self
  end

  # @return [String] out
  def inspect
    %(<#ProtonBot::Plug:#{object_id.to_s(16)} @name=#{name} @bot=#{bot}>)
  end
end

#runningBoolean

Returns ‘true` if bot still runs and processes messages from server.

Returns:

  • (Boolean)

    Returns ‘true` if bot still runs and processes messages from server.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/protonbot/plug.rb', line 41

class ProtonBot::Plug
  attr_reader :bot, :db, :sock, :rsock, :name, :conf, :rloop, :wloop, :log, :queue, :chans, :users,
              :event_locks, :user, :nick, :rnam, :is_ssl
  attr_accessor :running

  # @param bot [Bot]
  # @param name [String]
  # @param conf [Hash<String>]
  def initialize(bot, name, conf)
    @bot         = bot
    @name        = name
    @db          = @bot.dbs[@name]
    @log         = @bot._log.wrap("!#{@name}")
    @conf        = conf
    @nick        = conf['nick']
    @user        = conf['user']
    @rnam        = conf['rnam']
    @sock        = nil
    @running     = false
    @queue       = Queue.new
    @chans       = {}
    @users       = {}
    @event_locks = []
  end

  # Connects to server, introduces and starts reader and writer threads.
  # @return [Plug] self
  def connect!
    if @conf['ssl'] == nil or @conf['ssl'] == false
      @sock = @rsock = TCPSocket.new(@conf['host'], @conf['port'])
      @is_ssl = false
    else
      @rsock = TCPSocket.new(@conf['host'], @conf['port'])
      @ssl_ctx = OpenSSL::SSL::SSLContext.new
      @ssl_ctx.verify_mode = OpenSSL::SSL::VERIFY_NONE
      @ssl_ctx.ssl_version = :SSLv23
      @ssl_ctx.cert = OpenSSL::X509::Certificate.new(File.read(File.expand_path(@conf['ssl_crt']))) if
        @conf['ssl_crt']
      @ssl_ctx.key = OpenSSL::PKey::RSA.new(File.read(File.expand_path(@conf['ssl_key']))) if
        @conf['ssl_key']
      @sock = OpenSSL::SSL::SSLSocket.new(@rsock, @ssl_ctx)
      @sock.sync = true
      @sock.connect
      @is_ssl = true
    end

    @running = true

    @rloop = Thread.new { readloop }
    @wloop = Thread.new { writeloop }

    log.info("Started plug `#{name}` successfully!")

    introduce

    @rloop.join
    @wloop.join
    self
  end

  # Logs given error object to cosole
  # @param e [Exception]
  # @return [Plug] self
  def log_err(e)
    @log.error('Error!')
    @log.error("> Message: #{e.message}")
    @log.error('> Backtrace:')
    e.backtrace.each do |i|
      @log.error(">> #{i}")
    end
    self
  end

  # @return [String] out
  def inspect
    %(<#ProtonBot::Plug:#{object_id.to_s(16)} @name=#{name} @bot=#{bot}>)
  end
end

#sockTCPSocket, SSLSocket (readonly)

Returns Plug’s socket. May be SSL-socket.

Returns:

  • (TCPSocket, SSLSocket)

    Plug’s socket. May be SSL-socket.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/protonbot/plug.rb', line 41

class ProtonBot::Plug
  attr_reader :bot, :db, :sock, :rsock, :name, :conf, :rloop, :wloop, :log, :queue, :chans, :users,
              :event_locks, :user, :nick, :rnam, :is_ssl
  attr_accessor :running

  # @param bot [Bot]
  # @param name [String]
  # @param conf [Hash<String>]
  def initialize(bot, name, conf)
    @bot         = bot
    @name        = name
    @db          = @bot.dbs[@name]
    @log         = @bot._log.wrap("!#{@name}")
    @conf        = conf
    @nick        = conf['nick']
    @user        = conf['user']
    @rnam        = conf['rnam']
    @sock        = nil
    @running     = false
    @queue       = Queue.new
    @chans       = {}
    @users       = {}
    @event_locks = []
  end

  # Connects to server, introduces and starts reader and writer threads.
  # @return [Plug] self
  def connect!
    if @conf['ssl'] == nil or @conf['ssl'] == false
      @sock = @rsock = TCPSocket.new(@conf['host'], @conf['port'])
      @is_ssl = false
    else
      @rsock = TCPSocket.new(@conf['host'], @conf['port'])
      @ssl_ctx = OpenSSL::SSL::SSLContext.new
      @ssl_ctx.verify_mode = OpenSSL::SSL::VERIFY_NONE
      @ssl_ctx.ssl_version = :SSLv23
      @ssl_ctx.cert = OpenSSL::X509::Certificate.new(File.read(File.expand_path(@conf['ssl_crt']))) if
        @conf['ssl_crt']
      @ssl_ctx.key = OpenSSL::PKey::RSA.new(File.read(File.expand_path(@conf['ssl_key']))) if
        @conf['ssl_key']
      @sock = OpenSSL::SSL::SSLSocket.new(@rsock, @ssl_ctx)
      @sock.sync = true
      @sock.connect
      @is_ssl = true
    end

    @running = true

    @rloop = Thread.new { readloop }
    @wloop = Thread.new { writeloop }

    log.info("Started plug `#{name}` successfully!")

    introduce

    @rloop.join
    @wloop.join
    self
  end

  # Logs given error object to cosole
  # @param e [Exception]
  # @return [Plug] self
  def log_err(e)
    @log.error('Error!')
    @log.error("> Message: #{e.message}")
    @log.error('> Backtrace:')
    e.backtrace.each do |i|
      @log.error(">> #{i}")
    end
    self
  end

  # @return [String] out
  def inspect
    %(<#ProtonBot::Plug:#{object_id.to_s(16)} @name=#{name} @bot=#{bot}>)
  end
end

#userString (readonly)

Returns Plug’s username.

Returns:

  • (String)

    Plug’s username.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/protonbot/plug.rb', line 41

class ProtonBot::Plug
  attr_reader :bot, :db, :sock, :rsock, :name, :conf, :rloop, :wloop, :log, :queue, :chans, :users,
              :event_locks, :user, :nick, :rnam, :is_ssl
  attr_accessor :running

  # @param bot [Bot]
  # @param name [String]
  # @param conf [Hash<String>]
  def initialize(bot, name, conf)
    @bot         = bot
    @name        = name
    @db          = @bot.dbs[@name]
    @log         = @bot._log.wrap("!#{@name}")
    @conf        = conf
    @nick        = conf['nick']
    @user        = conf['user']
    @rnam        = conf['rnam']
    @sock        = nil
    @running     = false
    @queue       = Queue.new
    @chans       = {}
    @users       = {}
    @event_locks = []
  end

  # Connects to server, introduces and starts reader and writer threads.
  # @return [Plug] self
  def connect!
    if @conf['ssl'] == nil or @conf['ssl'] == false
      @sock = @rsock = TCPSocket.new(@conf['host'], @conf['port'])
      @is_ssl = false
    else
      @rsock = TCPSocket.new(@conf['host'], @conf['port'])
      @ssl_ctx = OpenSSL::SSL::SSLContext.new
      @ssl_ctx.verify_mode = OpenSSL::SSL::VERIFY_NONE
      @ssl_ctx.ssl_version = :SSLv23
      @ssl_ctx.cert = OpenSSL::X509::Certificate.new(File.read(File.expand_path(@conf['ssl_crt']))) if
        @conf['ssl_crt']
      @ssl_ctx.key = OpenSSL::PKey::RSA.new(File.read(File.expand_path(@conf['ssl_key']))) if
        @conf['ssl_key']
      @sock = OpenSSL::SSL::SSLSocket.new(@rsock, @ssl_ctx)
      @sock.sync = true
      @sock.connect
      @is_ssl = true
    end

    @running = true

    @rloop = Thread.new { readloop }
    @wloop = Thread.new { writeloop }

    log.info("Started plug `#{name}` successfully!")

    introduce

    @rloop.join
    @wloop.join
    self
  end

  # Logs given error object to cosole
  # @param e [Exception]
  # @return [Plug] self
  def log_err(e)
    @log.error('Error!')
    @log.error("> Message: #{e.message}")
    @log.error('> Backtrace:')
    e.backtrace.each do |i|
      @log.error(">> #{i}")
    end
    self
  end

  # @return [String] out
  def inspect
    %(<#ProtonBot::Plug:#{object_id.to_s(16)} @name=#{name} @bot=#{bot}>)
  end
end

#usersHash<String> (readonly)

Returns User data (by nick).

Returns:

  • (Hash<String>)

    User data (by nick).



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/protonbot/plug.rb', line 41

class ProtonBot::Plug
  attr_reader :bot, :db, :sock, :rsock, :name, :conf, :rloop, :wloop, :log, :queue, :chans, :users,
              :event_locks, :user, :nick, :rnam, :is_ssl
  attr_accessor :running

  # @param bot [Bot]
  # @param name [String]
  # @param conf [Hash<String>]
  def initialize(bot, name, conf)
    @bot         = bot
    @name        = name
    @db          = @bot.dbs[@name]
    @log         = @bot._log.wrap("!#{@name}")
    @conf        = conf
    @nick        = conf['nick']
    @user        = conf['user']
    @rnam        = conf['rnam']
    @sock        = nil
    @running     = false
    @queue       = Queue.new
    @chans       = {}
    @users       = {}
    @event_locks = []
  end

  # Connects to server, introduces and starts reader and writer threads.
  # @return [Plug] self
  def connect!
    if @conf['ssl'] == nil or @conf['ssl'] == false
      @sock = @rsock = TCPSocket.new(@conf['host'], @conf['port'])
      @is_ssl = false
    else
      @rsock = TCPSocket.new(@conf['host'], @conf['port'])
      @ssl_ctx = OpenSSL::SSL::SSLContext.new
      @ssl_ctx.verify_mode = OpenSSL::SSL::VERIFY_NONE
      @ssl_ctx.ssl_version = :SSLv23
      @ssl_ctx.cert = OpenSSL::X509::Certificate.new(File.read(File.expand_path(@conf['ssl_crt']))) if
        @conf['ssl_crt']
      @ssl_ctx.key = OpenSSL::PKey::RSA.new(File.read(File.expand_path(@conf['ssl_key']))) if
        @conf['ssl_key']
      @sock = OpenSSL::SSL::SSLSocket.new(@rsock, @ssl_ctx)
      @sock.sync = true
      @sock.connect
      @is_ssl = true
    end

    @running = true

    @rloop = Thread.new { readloop }
    @wloop = Thread.new { writeloop }

    log.info("Started plug `#{name}` successfully!")

    introduce

    @rloop.join
    @wloop.join
    self
  end

  # Logs given error object to cosole
  # @param e [Exception]
  # @return [Plug] self
  def log_err(e)
    @log.error('Error!')
    @log.error("> Message: #{e.message}")
    @log.error('> Backtrace:')
    e.backtrace.each do |i|
      @log.error(">> #{i}")
    end
    self
  end

  # @return [String] out
  def inspect
    %(<#ProtonBot::Plug:#{object_id.to_s(16)} @name=#{name} @bot=#{bot}>)
  end
end

#wloopThread (readonly)

Returns Writer-loop-thread.

Returns:

  • (Thread)

    Writer-loop-thread.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/protonbot/plug.rb', line 41

class ProtonBot::Plug
  attr_reader :bot, :db, :sock, :rsock, :name, :conf, :rloop, :wloop, :log, :queue, :chans, :users,
              :event_locks, :user, :nick, :rnam, :is_ssl
  attr_accessor :running

  # @param bot [Bot]
  # @param name [String]
  # @param conf [Hash<String>]
  def initialize(bot, name, conf)
    @bot         = bot
    @name        = name
    @db          = @bot.dbs[@name]
    @log         = @bot._log.wrap("!#{@name}")
    @conf        = conf
    @nick        = conf['nick']
    @user        = conf['user']
    @rnam        = conf['rnam']
    @sock        = nil
    @running     = false
    @queue       = Queue.new
    @chans       = {}
    @users       = {}
    @event_locks = []
  end

  # Connects to server, introduces and starts reader and writer threads.
  # @return [Plug] self
  def connect!
    if @conf['ssl'] == nil or @conf['ssl'] == false
      @sock = @rsock = TCPSocket.new(@conf['host'], @conf['port'])
      @is_ssl = false
    else
      @rsock = TCPSocket.new(@conf['host'], @conf['port'])
      @ssl_ctx = OpenSSL::SSL::SSLContext.new
      @ssl_ctx.verify_mode = OpenSSL::SSL::VERIFY_NONE
      @ssl_ctx.ssl_version = :SSLv23
      @ssl_ctx.cert = OpenSSL::X509::Certificate.new(File.read(File.expand_path(@conf['ssl_crt']))) if
        @conf['ssl_crt']
      @ssl_ctx.key = OpenSSL::PKey::RSA.new(File.read(File.expand_path(@conf['ssl_key']))) if
        @conf['ssl_key']
      @sock = OpenSSL::SSL::SSLSocket.new(@rsock, @ssl_ctx)
      @sock.sync = true
      @sock.connect
      @is_ssl = true
    end

    @running = true

    @rloop = Thread.new { readloop }
    @wloop = Thread.new { writeloop }

    log.info("Started plug `#{name}` successfully!")

    introduce

    @rloop.join
    @wloop.join
    self
  end

  # Logs given error object to cosole
  # @param e [Exception]
  # @return [Plug] self
  def log_err(e)
    @log.error('Error!')
    @log.error("> Message: #{e.message}")
    @log.error('> Backtrace:')
    e.backtrace.each do |i|
      @log.error(">> #{i}")
    end
    self
  end

  # @return [String] out
  def inspect
    %(<#ProtonBot::Plug:#{object_id.to_s(16)} @name=#{name} @bot=#{bot}>)
  end
end

Class Method Details

.process_colors(string) ⇒ String

Colorizes given string.

Parameters:

  • string (String)

Returns:

  • (String)

    Output



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/protonbot/plug_io.rb', line 73

def self.process_colors(string)
  string
    .gsub("%C%",     "%C?")
    .gsub(",%",      ",?")
    .gsub("%C",      "\x03")
    .gsub("%B",      "\x02")
    .gsub("%I",      "\x10")
    .gsub("%U",      "\x1F")
    .gsub("%N",      "\x0F")
    .gsub("?WHITE",  "0")
    .gsub("?BLACK",  "1")
    .gsub("?BLUE",   "2")
    .gsub("?GREEN",  "3")
    .gsub("?RED",    "4")
    .gsub("?BROWN",  "5")
    .gsub("?PURPLE", "6")
    .gsub("?ORANGE", "7")
    .gsub("?YELLOW", "8")
    .gsub("?LGREEN", "9")
    .gsub("?CYAN"  , "10")
    .gsub("?LCYAN",  "11")
    .gsub("?LBLUE",  "12")
    .gsub("?PINK",   "13")
    .gsub("?GREY",   "14")
    .gsub("?LGREY",  "15")
end

.ssplit(string) ⇒ String

Splits given string each 399 bytes and on newlines

Parameters:

  • string (String)

Returns:

  • (String)

    Output



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/protonbot/plug_io.rb', line 57

def self.ssplit(string)
  out = []
  arr = string.split("\n\r")
  arr.each do |i|
    items = i.scan(/.{,399}/)
    items.delete('')
    items.each do |i2|
      out << i2
    end
  end
  out
end

Instance Method Details

#ban(chan, nick) ⇒ Plug

Sets ban on given user at given channel

Parameters:

  • chan (String)
  • nick (String)

Returns:



157
158
159
160
# File 'lib/protonbot/plug_utils.rb', line 157

def ban(chan, nick)
  usermode(chan, nick, '+b')
  self
end

#change_nick(to) ⇒ Plug

Changes current nick to given. If successful, ‘unick` event will be emitted

Parameters:

  • to (String)

Returns:



45
46
47
48
# File 'lib/protonbot/plug_utils.rb', line 45

def change_nick(to)
  write("NICK #{to}")
  self
end

#chanmode(chan, mode) ⇒ Plug

Sets mode on given channel

Parameters:

  • chan (String)
  • mode (String)

Returns:



130
131
132
133
# File 'lib/protonbot/plug_utils.rb', line 130

def chanmode(chan, mode)
  write("MODE #{chan} #{mode}")
  self
end

#connect!Plug

Connects to server, introduces and starts reader and writer threads.

Returns:



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/protonbot/plug.rb', line 68

def connect!
  if @conf['ssl'] == nil or @conf['ssl'] == false
    @sock = @rsock = TCPSocket.new(@conf['host'], @conf['port'])
    @is_ssl = false
  else
    @rsock = TCPSocket.new(@conf['host'], @conf['port'])
    @ssl_ctx = OpenSSL::SSL::SSLContext.new
    @ssl_ctx.verify_mode = OpenSSL::SSL::VERIFY_NONE
    @ssl_ctx.ssl_version = :SSLv23
    @ssl_ctx.cert = OpenSSL::X509::Certificate.new(File.read(File.expand_path(@conf['ssl_crt']))) if
      @conf['ssl_crt']
    @ssl_ctx.key = OpenSSL::PKey::RSA.new(File.read(File.expand_path(@conf['ssl_key']))) if
      @conf['ssl_key']
    @sock = OpenSSL::SSL::SSLSocket.new(@rsock, @ssl_ctx)
    @sock.sync = true
    @sock.connect
    @is_ssl = true
  end

  @running = true

  @rloop = Thread.new { readloop }
  @wloop = Thread.new { writeloop }

  log.info("Started plug `#{name}` successfully!")

  introduce

  @rloop.join
  @wloop.join
  self
end

#ctcp(target, msg) ⇒ Plug

Sends CTCP to given target.

Parameters:

  • target (String)
  • msg (String)

Returns:



93
94
95
96
# File 'lib/protonbot/plug_utils.rb', line 93

def ctcp(target, msg)
  write("PRIVMSG #{target} :\x01#{msg}\x01")
  self
end

#deop(chan, nick) ⇒ Plug

Deops given user at given channel

Parameters:

  • chan (String)
  • nick (String)

Returns:



202
203
204
205
# File 'lib/protonbot/plug_utils.rb', line 202

def deop(chan, nick)
  usermode(chan, nick, '-o')
  self
end

#devoice(chan, nick) ⇒ Plug

Devoices on given user at given channel

Parameters:

  • chan (String)
  • nick (String)

Returns:



220
221
222
223
# File 'lib/protonbot/plug_utils.rb', line 220

def devoice(chan, nick)
  usermode(chan, nick, '-v')
  self
end

#emit(dat = {}) ⇒ Plug

Emits passed event - calls first matching hook from each plugin

Parameters:

  • dat (Hash) (defaults to: {})

    Event hash

Returns:



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/protonbot/plug_events.rb', line 5

def emit(dat = {})
  bot.plugins.each do |_, p|
    worked = []
    p.hooks.each do |h|
      next unless dat >= h.pattern && !worked.include?(h.object_id) && worked.empty?
      canrun = true
      h.chain.each do |l|
        next unless canrun
        canrun = l.call(dat, h)
      end
      worked << h.object_id
      h.block.call(dat) if canrun
    end
    worked = []
  end
  event_locks.each_with_index do |el, k|
    if dat >= el.pattern
      event_locks.delete_at(k)
      el.unlock
    end
  end
  self
end

#emitt(dat = {}) ⇒ Plug

Emits passed event in new thread

Parameters:

  • dat (Hash) (defaults to: {})

    Event hash

Returns:



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/protonbot/plug_events.rb', line 32

def emitt(dat = {})
  d = dat.clone
  Thread.new do
    begin
      emit(d)
    rescue => e
      log_err(e)
    end
  end
  self
end

#excempt(chan, nick) ⇒ Plug

Sets excempt on given user at given channel

Parameters:

  • chan (String)
  • nick (String)

Returns:



175
176
177
178
# File 'lib/protonbot/plug_utils.rb', line 175

def excempt(chan, nick)
  usermode(chan, nick, '+e')
  self
end

#gethost(nick) ⇒ String

Gets hostname for given nickname using WHOIS and event lock

Parameters:

  • nick (String)

Returns:

  • (String)

    host



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/protonbot/plug_utils.rb', line 13

def gethost(nick)
  if @users[nick] and @users[nick][:host]
    @users[nick][:host]
  else
    Thread.new do
      sleep(1)
      whois(nick)
    end
    wait_for(type: :code_whoisuser, nick: nick)
  end
  @users[nick][:host]
end

#getuser(nick) ⇒ String

Gets username for given nickname using WHOIS and event lock

Parameters:

  • nick (String)

Returns:

  • (String)

    user



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/protonbot/plug_utils.rb', line 29

def getuser(nick)
  if @users[nick] and @users[nick][:user]
    @users[nick][:user]
  else
    Thread.new do
      sleep(1)
      whois(nick)
    end
    wait_for(type: :code, code: ProtonBot::Numeric::WHOISUSER)
    @users[nick][:user]
  end
end

#inspectString

Returns out.

Returns:

  • (String)

    out



115
116
117
# File 'lib/protonbot/plug.rb', line 115

def inspect
  %(<#ProtonBot::Plug:#{object_id.to_s(16)} @name=#{name} @bot=#{bot}>)
end

#introduceObject

Sends credentials to server (PASS, NICK, USER).



48
49
50
51
52
# File 'lib/protonbot/plug_io.rb', line 48

def introduce
  write_("PASS #{@conf['pass']}") if @conf['pass']
  write_("NICK #{@conf['nick']}")
  write_("USER #{@conf['user']} 0 * :#{@conf['rnam']}")
end

#invite(chan, nick) ⇒ Plug

Invites user to given channel

Parameters:

  • chan (String)
  • nick (String)

Returns:



111
112
113
114
# File 'lib/protonbot/plug_utils.rb', line 111

def invite(chan, nick)
  write("INVITE #{nick} #{chan}")
  self
end

#join(chan, pass = '') ⇒ Plug

Joins given channel and uses password if it’s provided

Parameters:

  • chan (String)
  • pass (String) (defaults to: '')

    Password

Returns:



54
55
56
57
# File 'lib/protonbot/plug_utils.rb', line 54

def join(chan, pass = '')
  write("JOIN #{chan} #{pass}")
  self
end

#kick(chan, nick, reason = 'Default Kick Reason') ⇒ Plug

Kicks given user from given channel

Parameters:

  • chan (String)
  • nick (String)

Returns:



229
230
231
232
# File 'lib/protonbot/plug_utils.rb', line 229

def kick(chan, nick, reason = 'Default Kick Reason')
  write("KICK #{chan} #{nick} :#{reason}")
  self
end

#log_err(e) ⇒ Plug

Logs given error object to cosole

Parameters:

  • e (Exception)

Returns:



104
105
106
107
108
109
110
111
112
# File 'lib/protonbot/plug.rb', line 104

def log_err(e)
  @log.error('Error!')
  @log.error("> Message: #{e.message}")
  @log.error('> Backtrace:')
  e.backtrace.each do |i|
    @log.error(">> #{i}")
  end
  self
end

#nctcp(target, msg) ⇒ Plug

Sends NCTCP to given target.

Parameters:

  • target (String)
  • msg (String)

Returns:



102
103
104
105
# File 'lib/protonbot/plug_utils.rb', line 102

def nctcp(target, msg)
  write("NOTICE #{target} :\x01#{msg}\x01")
  self
end

#notice(target, msg) ⇒ Plug

Sends notice to given target. Splits it each 399 bytes.

Parameters:

  • target (String)
  • msg (String)

Returns:



82
83
84
85
86
87
# File 'lib/protonbot/plug_utils.rb', line 82

def notice(target, msg)
  self.class.ssplit(self.class.process_colors(msg)).each do |m|
    write("NOTICE #{target} :\u200B#{m}")
  end
  self
end

#op(chan, nick) ⇒ Plug

Ops given user at given channel

Parameters:

  • chan (String)
  • nick (String)

Returns:



193
194
195
196
# File 'lib/protonbot/plug_utils.rb', line 193

def op(chan, nick)
  usermode(chan, nick, '+o')
  self
end

#part(chan, reason = 'Parting...') ⇒ Object

Parts given channel with given reason

Parameters:

  • chan (String)
  • reason (String) (defaults to: 'Parting...')


62
63
64
65
# File 'lib/protonbot/plug_utils.rb', line 62

def part(chan, reason = 'Parting...')
  write("PART #{chan} :#{reason}")
  self
end

#privmsg(target, msg) ⇒ Plug

Sends message to given target. Splits it each 399 bytes.

Parameters:

  • target (String)
  • msg (String)

Returns:



71
72
73
74
75
76
# File 'lib/protonbot/plug_utils.rb', line 71

def privmsg(target, msg)
  self.class.ssplit(self.class.process_colors(msg)).each do |m|
    write("PRIVMSG #{target} :\u200B#{m}")
  end
  self
end

#quiet(chan, nick) ⇒ Plug

Sets quiet on given user at given channel

Parameters:

  • chan (String)
  • nick (String)

Returns:



139
140
141
142
# File 'lib/protonbot/plug_utils.rb', line 139

def quiet(chan, nick)
  usermode(chan, nick, '+q')
  self
end

#readloopObject

Main read-loop. Reads data from socket and emits event ‘raw`



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/protonbot/plug_io.rb', line 3

def readloop
  while @running
    if s = @sock.gets
      s = s.force_encoding(@conf['encoding'])
      s = s[0..-3]
      log.info "R > #{s}"
      begin
        emit(type: :raw, raw_data: s.clone, plug: self, db: db, bot: bot, core: bot.plugins['core'])
      rescue => e
        log_err(e)
      end
    else
      @running = false
    end
  end
  log.info 'Connection closed.'
end

#remove(chan, nick, reason = 'Default Remove Reason') ⇒ Plug

Removes given user from given channel

Parameters:

  • chan (String)
  • nick (String)

Returns:



238
239
240
241
# File 'lib/protonbot/plug_utils.rb', line 238

def remove(chan, nick, reason = 'Default Remove Reason')
  write("REMOVE #{chan} #{nick} :#{reason}")
  self
end

#unban(chan, nick) ⇒ Plug

Removes ban on given user at given channel

Parameters:

  • chan (String)
  • nick (String)

Returns:



166
167
168
169
# File 'lib/protonbot/plug_utils.rb', line 166

def unban(chan, nick)
  usermode(chan, nick, '-b')
  self
end

#unexcempt(chan, nick) ⇒ Plug

Removes excempt on given user at given channel

Parameters:

  • chan (String)
  • nick (String)

Returns:



184
185
186
187
# File 'lib/protonbot/plug_utils.rb', line 184

def unexcempt(chan, nick)
  usermode(chan, nick, '-e')
  self
end

#unquiet(chan, nick) ⇒ Plug

Removes quiet on given user at given channel

Parameters:

  • chan (String)
  • nick (String)

Returns:



148
149
150
151
# File 'lib/protonbot/plug_utils.rb', line 148

def unquiet(chan, nick)
  usermode(chan, nick, '-q')
  self
end

#usermode(chan, nick, mode) ⇒ Plug

Sets mode on given user at given channel

Parameters:

  • chan (String)
  • nick (String)
  • mode (String)

Returns:



121
122
123
124
# File 'lib/protonbot/plug_utils.rb', line 121

def usermode(chan, nick, mode)
  write("MODE #{chan} #{mode} #{nick}")
  self
end

#voice(chan, nick) ⇒ Plug

Voices given user at given channel

Parameters:

  • chan (String)
  • nick (String)

Returns:



211
212
213
214
# File 'lib/protonbot/plug_utils.rb', line 211

def voice(chan, nick)
  usermode(chan, nick, '+v')
  self
end

#wait_for(pattern) ⇒ Plug

Creates EventLock with given pattern.

Parameters:

  • pattern (Hash)

Returns:



47
48
49
50
# File 'lib/protonbot/plug_events.rb', line 47

def wait_for(pattern)
  ProtonBot::EventLock.new(self, pattern)
  self
end

#whois(nick) ⇒ Plug

Sends WHOIS message to the server

Parameters:

  • nick (String)

Returns:



5
6
7
8
# File 'lib/protonbot/plug_utils.rb', line 5

def whois(nick)
  write("WHOIS #{nick}")
  self
end

#write(s) ⇒ Plug

Adds given message to the queue

Parameters:

  • s (String)

    Message

Returns:



33
34
35
36
# File 'lib/protonbot/plug_io.rb', line 33

def write(s)
  @queue << s
  self
end

#write_(s) ⇒ Plug

Sends message to server without using queue.

Parameters:

  • s (String)

    Message

Returns:



41
42
43
44
45
# File 'lib/protonbot/plug_io.rb', line 41

def write_(s)
  s = s.encode(@conf['encoding'], s.encoding)
  @sock.puts s
  log.info "W > #{s}"
end

#writeloopObject

Main write-loop. Reads data from queue and sends it to server



22
23
24
25
26
27
28
# File 'lib/protonbot/plug_io.rb', line 22

def writeloop
  while @running || !@wqueue.empty?
    s = @queue.pop
    write_(s)
    sleep(@conf['queue_delay'])
  end
end