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



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# 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 = []
  if @conf['sasl'] and @conf['sasl_user'] and @conf['pass']
    @use_sasl = true
  else
    @use_sasl = false
  end
end

Instance Attribute Details

#botProtonBot::Bot (readonly)



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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# 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, :rnam, :is_ssl
  attr_accessor :running, :nick, :use_sasl

  # @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 = []
    if @conf['sasl'] and @conf['sasl_user'] and @conf['pass']
      @use_sasl = true
    else
      @use_sasl = false
    end
  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

    self
  end

  # Logs given error object to cosole
  # @param e [Exception]
  # @return [Plug] self
  def log_err(e)
    @log.error('Error!')
    @log.error("> Inspect: #{e.inspect}")
    @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

  # @!api private
  def thrjoin
    until @rloop && @rloop.status == 'run'
      sleep(0.1)
    end
    until @wloop && @wloop.status == 'run'
      sleep(0.1)
    end
    @bot.plugthrs[@name].join
    @rloop.join
    @wloop.join
  end
end

#chansHash<String> (readonly)



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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# 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, :rnam, :is_ssl
  attr_accessor :running, :nick, :use_sasl

  # @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 = []
    if @conf['sasl'] and @conf['sasl_user'] and @conf['pass']
      @use_sasl = true
    else
      @use_sasl = false
    end
  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

    self
  end

  # Logs given error object to cosole
  # @param e [Exception]
  # @return [Plug] self
  def log_err(e)
    @log.error('Error!')
    @log.error("> Inspect: #{e.inspect}")
    @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

  # @!api private
  def thrjoin
    until @rloop && @rloop.status == 'run'
      sleep(0.1)
    end
    until @wloop && @wloop.status == 'run'
      sleep(0.1)
    end
    @bot.plugthrs[@name].join
    @rloop.join
    @wloop.join
  end
end

#confHash<String> (readonly)



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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# 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, :rnam, :is_ssl
  attr_accessor :running, :nick, :use_sasl

  # @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 = []
    if @conf['sasl'] and @conf['sasl_user'] and @conf['pass']
      @use_sasl = true
    else
      @use_sasl = false
    end
  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

    self
  end

  # Logs given error object to cosole
  # @param e [Exception]
  # @return [Plug] self
  def log_err(e)
    @log.error('Error!')
    @log.error("> Inspect: #{e.inspect}")
    @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

  # @!api private
  def thrjoin
    until @rloop && @rloop.status == 'run'
      sleep(0.1)
    end
    until @wloop && @wloop.status == 'run'
      sleep(0.1)
    end
    @bot.plugthrs[@name].join
    @rloop.join
    @wloop.join
  end
end

#dbHeliodor::DB (readonly)

Returns Plug’s Heliodor-powered database.



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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# 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, :rnam, :is_ssl
  attr_accessor :running, :nick, :use_sasl

  # @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 = []
    if @conf['sasl'] and @conf['sasl_user'] and @conf['pass']
      @use_sasl = true
    else
      @use_sasl = false
    end
  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

    self
  end

  # Logs given error object to cosole
  # @param e [Exception]
  # @return [Plug] self
  def log_err(e)
    @log.error('Error!')
    @log.error("> Inspect: #{e.inspect}")
    @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

  # @!api private
  def thrjoin
    until @rloop && @rloop.status == 'run'
      sleep(0.1)
    end
    until @wloop && @wloop.status == 'run'
      sleep(0.1)
    end
    @bot.plugthrs[@name].join
    @rloop.join
    @wloop.join
  end
end

#event_locksArray<EventLock> (readonly)



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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# 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, :rnam, :is_ssl
  attr_accessor :running, :nick, :use_sasl

  # @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 = []
    if @conf['sasl'] and @conf['sasl_user'] and @conf['pass']
      @use_sasl = true
    else
      @use_sasl = false
    end
  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

    self
  end

  # Logs given error object to cosole
  # @param e [Exception]
  # @return [Plug] self
  def log_err(e)
    @log.error('Error!')
    @log.error("> Inspect: #{e.inspect}")
    @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

  # @!api private
  def thrjoin
    until @rloop && @rloop.status == 'run'
      sleep(0.1)
    end
    until @wloop && @wloop.status == 'run'
      sleep(0.1)
    end
    @bot.plugthrs[@name].join
    @rloop.join
    @wloop.join
  end
end

#is_sslBoolean (readonly)



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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# 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, :rnam, :is_ssl
  attr_accessor :running, :nick, :use_sasl

  # @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 = []
    if @conf['sasl'] and @conf['sasl_user'] and @conf['pass']
      @use_sasl = true
    else
      @use_sasl = false
    end
  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

    self
  end

  # Logs given error object to cosole
  # @param e [Exception]
  # @return [Plug] self
  def log_err(e)
    @log.error('Error!')
    @log.error("> Inspect: #{e.inspect}")
    @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

  # @!api private
  def thrjoin
    until @rloop && @rloop.status == 'run'
      sleep(0.1)
    end
    until @wloop && @wloop.status == 'run'
      sleep(0.1)
    end
    @bot.plugthrs[@name].join
    @rloop.join
    @wloop.join
  end
end

#logProtonBot::LogWrapper (readonly)



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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# 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, :rnam, :is_ssl
  attr_accessor :running, :nick, :use_sasl

  # @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 = []
    if @conf['sasl'] and @conf['sasl_user'] and @conf['pass']
      @use_sasl = true
    else
      @use_sasl = false
    end
  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

    self
  end

  # Logs given error object to cosole
  # @param e [Exception]
  # @return [Plug] self
  def log_err(e)
    @log.error('Error!')
    @log.error("> Inspect: #{e.inspect}")
    @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

  # @!api private
  def thrjoin
    until @rloop && @rloop.status == 'run'
      sleep(0.1)
    end
    until @wloop && @wloop.status == 'run'
      sleep(0.1)
    end
    @bot.plugthrs[@name].join
    @rloop.join
    @wloop.join
  end
end

#nameString (readonly)



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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# 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, :rnam, :is_ssl
  attr_accessor :running, :nick, :use_sasl

  # @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 = []
    if @conf['sasl'] and @conf['sasl_user'] and @conf['pass']
      @use_sasl = true
    else
      @use_sasl = false
    end
  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

    self
  end

  # Logs given error object to cosole
  # @param e [Exception]
  # @return [Plug] self
  def log_err(e)
    @log.error('Error!')
    @log.error("> Inspect: #{e.inspect}")
    @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

  # @!api private
  def thrjoin
    until @rloop && @rloop.status == 'run'
      sleep(0.1)
    end
    until @wloop && @wloop.status == 'run'
      sleep(0.1)
    end
    @bot.plugthrs[@name].join
    @rloop.join
    @wloop.join
  end
end

#nickString



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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# 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, :rnam, :is_ssl
  attr_accessor :running, :nick, :use_sasl

  # @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 = []
    if @conf['sasl'] and @conf['sasl_user'] and @conf['pass']
      @use_sasl = true
    else
      @use_sasl = false
    end
  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

    self
  end

  # Logs given error object to cosole
  # @param e [Exception]
  # @return [Plug] self
  def log_err(e)
    @log.error('Error!')
    @log.error("> Inspect: #{e.inspect}")
    @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

  # @!api private
  def thrjoin
    until @rloop && @rloop.status == 'run'
      sleep(0.1)
    end
    until @wloop && @wloop.status == 'run'
      sleep(0.1)
    end
    @bot.plugthrs[@name].join
    @rloop.join
    @wloop.join
  end
end

#queueQueue (readonly)



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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# 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, :rnam, :is_ssl
  attr_accessor :running, :nick, :use_sasl

  # @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 = []
    if @conf['sasl'] and @conf['sasl_user'] and @conf['pass']
      @use_sasl = true
    else
      @use_sasl = false
    end
  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

    self
  end

  # Logs given error object to cosole
  # @param e [Exception]
  # @return [Plug] self
  def log_err(e)
    @log.error('Error!')
    @log.error("> Inspect: #{e.inspect}")
    @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

  # @!api private
  def thrjoin
    until @rloop && @rloop.status == 'run'
      sleep(0.1)
    end
    until @wloop && @wloop.status == 'run'
      sleep(0.1)
    end
    @bot.plugthrs[@name].join
    @rloop.join
    @wloop.join
  end
end

#rloopThread (readonly)



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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# 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, :rnam, :is_ssl
  attr_accessor :running, :nick, :use_sasl

  # @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 = []
    if @conf['sasl'] and @conf['sasl_user'] and @conf['pass']
      @use_sasl = true
    else
      @use_sasl = false
    end
  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

    self
  end

  # Logs given error object to cosole
  # @param e [Exception]
  # @return [Plug] self
  def log_err(e)
    @log.error('Error!')
    @log.error("> Inspect: #{e.inspect}")
    @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

  # @!api private
  def thrjoin
    until @rloop && @rloop.status == 'run'
      sleep(0.1)
    end
    until @wloop && @wloop.status == 'run'
      sleep(0.1)
    end
    @bot.plugthrs[@name].join
    @rloop.join
    @wloop.join
  end
end

#rnamString (readonly)



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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# 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, :rnam, :is_ssl
  attr_accessor :running, :nick, :use_sasl

  # @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 = []
    if @conf['sasl'] and @conf['sasl_user'] and @conf['pass']
      @use_sasl = true
    else
      @use_sasl = false
    end
  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

    self
  end

  # Logs given error object to cosole
  # @param e [Exception]
  # @return [Plug] self
  def log_err(e)
    @log.error('Error!')
    @log.error("> Inspect: #{e.inspect}")
    @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

  # @!api private
  def thrjoin
    until @rloop && @rloop.status == 'run'
      sleep(0.1)
    end
    until @wloop && @wloop.status == 'run'
      sleep(0.1)
    end
    @bot.plugthrs[@name].join
    @rloop.join
    @wloop.join
  end
end

#rsockTCPSocket (readonly)



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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# 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, :rnam, :is_ssl
  attr_accessor :running, :nick, :use_sasl

  # @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 = []
    if @conf['sasl'] and @conf['sasl_user'] and @conf['pass']
      @use_sasl = true
    else
      @use_sasl = false
    end
  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

    self
  end

  # Logs given error object to cosole
  # @param e [Exception]
  # @return [Plug] self
  def log_err(e)
    @log.error('Error!')
    @log.error("> Inspect: #{e.inspect}")
    @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

  # @!api private
  def thrjoin
    until @rloop && @rloop.status == 'run'
      sleep(0.1)
    end
    until @wloop && @wloop.status == 'run'
      sleep(0.1)
    end
    @bot.plugthrs[@name].join
    @rloop.join
    @wloop.join
  end
end

#runningBoolean



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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# 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, :rnam, :is_ssl
  attr_accessor :running, :nick, :use_sasl

  # @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 = []
    if @conf['sasl'] and @conf['sasl_user'] and @conf['pass']
      @use_sasl = true
    else
      @use_sasl = false
    end
  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

    self
  end

  # Logs given error object to cosole
  # @param e [Exception]
  # @return [Plug] self
  def log_err(e)
    @log.error('Error!')
    @log.error("> Inspect: #{e.inspect}")
    @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

  # @!api private
  def thrjoin
    until @rloop && @rloop.status == 'run'
      sleep(0.1)
    end
    until @wloop && @wloop.status == 'run'
      sleep(0.1)
    end
    @bot.plugthrs[@name].join
    @rloop.join
    @wloop.join
  end
end

#sockTCPSocket, SSLSocket (readonly)



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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# 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, :rnam, :is_ssl
  attr_accessor :running, :nick, :use_sasl

  # @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 = []
    if @conf['sasl'] and @conf['sasl_user'] and @conf['pass']
      @use_sasl = true
    else
      @use_sasl = false
    end
  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

    self
  end

  # Logs given error object to cosole
  # @param e [Exception]
  # @return [Plug] self
  def log_err(e)
    @log.error('Error!')
    @log.error("> Inspect: #{e.inspect}")
    @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

  # @!api private
  def thrjoin
    until @rloop && @rloop.status == 'run'
      sleep(0.1)
    end
    until @wloop && @wloop.status == 'run'
      sleep(0.1)
    end
    @bot.plugthrs[@name].join
    @rloop.join
    @wloop.join
  end
end

#use_saslObject

Returns the value of attribute use_sasl.



44
45
46
# File 'lib/protonbot/plug.rb', line 44

def use_sasl
  @use_sasl
end

#userString (readonly)



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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# 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, :rnam, :is_ssl
  attr_accessor :running, :nick, :use_sasl

  # @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 = []
    if @conf['sasl'] and @conf['sasl_user'] and @conf['pass']
      @use_sasl = true
    else
      @use_sasl = false
    end
  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

    self
  end

  # Logs given error object to cosole
  # @param e [Exception]
  # @return [Plug] self
  def log_err(e)
    @log.error('Error!')
    @log.error("> Inspect: #{e.inspect}")
    @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

  # @!api private
  def thrjoin
    until @rloop && @rloop.status == 'run'
      sleep(0.1)
    end
    until @wloop && @wloop.status == 'run'
      sleep(0.1)
    end
    @bot.plugthrs[@name].join
    @rloop.join
    @wloop.join
  end
end

#usersHash<String> (readonly)



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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# 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, :rnam, :is_ssl
  attr_accessor :running, :nick, :use_sasl

  # @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 = []
    if @conf['sasl'] and @conf['sasl_user'] and @conf['pass']
      @use_sasl = true
    else
      @use_sasl = false
    end
  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

    self
  end

  # Logs given error object to cosole
  # @param e [Exception]
  # @return [Plug] self
  def log_err(e)
    @log.error('Error!')
    @log.error("> Inspect: #{e.inspect}")
    @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

  # @!api private
  def thrjoin
    until @rloop && @rloop.status == 'run'
      sleep(0.1)
    end
    until @wloop && @wloop.status == 'run'
      sleep(0.1)
    end
    @bot.plugthrs[@name].join
    @rloop.join
    @wloop.join
  end
end

#wloopThread (readonly)



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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# 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, :rnam, :is_ssl
  attr_accessor :running, :nick, :use_sasl

  # @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 = []
    if @conf['sasl'] and @conf['sasl_user'] and @conf['pass']
      @use_sasl = true
    else
      @use_sasl = false
    end
  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

    self
  end

  # Logs given error object to cosole
  # @param e [Exception]
  # @return [Plug] self
  def log_err(e)
    @log.error('Error!')
    @log.error("> Inspect: #{e.inspect}")
    @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

  # @!api private
  def thrjoin
    until @rloop && @rloop.status == 'run'
      sleep(0.1)
    end
    until @wloop && @wloop.status == 'run'
      sleep(0.1)
    end
    @bot.plugthrs[@name].join
    @rloop.join
    @wloop.join
  end
end

Class Method Details

.process_colors(string) ⇒ String

Colorizes given string.



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_io.rb', line 74

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



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

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

#action(target, msg) ⇒ Plug

Sends CTCP ACTION to given target



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

def action(target, msg)
  ctcp(target, "ACTION #{msg}")
  self
end

#ban(chan, nick) ⇒ Plug

Sets ban on given user at given channel



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

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



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



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

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

#connect!Plug

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



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
# File 'lib/protonbot/plug.rb', line 73

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

  self
end

#ctcp(target, msg) ⇒ Plug

Sends CTCP to given target.



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

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

#deop(chan, nick) ⇒ Plug

Deops given user at given channel



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

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

#devoice(chan, nick) ⇒ Plug

Devoices on given user at given channel



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

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

#emit(dat = {}) ⇒ Plug

Emits passed event - calls first matching hook from each plugin



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

def emit(dat = {})
  hooks = []
  bot.plugins.each do |_, p|
    hooks += p.hooks
  end
  hooks = hooks.keep_if do |hook|
    dat >= hook.pattern
  end
  hooks.each do |h|
    canrun = true
    h.chain.each do |l|
      next unless canrun
      canrun = l.call(dat, h)
    end
    h.block.call(dat) if canrun
  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



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

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



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

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

#gethost(nick) ⇒ String

Gets hostname for given nickname using WHOIS and event lock



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



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



119
120
121
# File 'lib/protonbot/plug.rb', line 119

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
53
# File 'lib/protonbot/plug_io.rb', line 48

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

#invite(chan, nick) ⇒ Plug

Invites user to given channel



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

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

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

Joins given channel and uses password if it’s provided



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



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

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



107
108
109
110
111
112
113
114
115
116
# File 'lib/protonbot/plug.rb', line 107

def log_err(e)
  @log.error('Error!')
  @log.error("> Inspect: #{e.inspect}")
  @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.



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

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

#notice(target, msg) ⇒ Plug

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



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



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

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

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

Parts given channel with given reason



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.



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



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

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)
      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



247
248
249
250
# File 'lib/protonbot/plug_utils.rb', line 247

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

#thrjoinObject



124
125
126
127
128
129
130
131
132
133
134
# File 'lib/protonbot/plug.rb', line 124

def thrjoin
  until @rloop && @rloop.status == 'run'
    sleep(0.1)
  end
  until @wloop && @wloop.status == 'run'
    sleep(0.1)
  end
  @bot.plugthrs[@name].join
  @rloop.join
  @wloop.join
end

#unban(chan, nick) ⇒ Plug

Removes ban on given user at given channel



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

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

#unexcempt(chan, nick) ⇒ Plug

Removes excempt on given user at given channel



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

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

#unquiet(chan, nick) ⇒ Plug

Removes quiet on given user at given channel



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

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

#usermode(chan, nick, mode) ⇒ Plug

Sets mode on given user at given channel



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

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

#voice(chan, nick) ⇒ Plug

Voices given user at given channel



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

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

#wait_for(pattern) ⇒ Plug

Creates EventLock with given pattern.



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

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

#whois(nick) ⇒ Plug

Sends WHOIS message to the server



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



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.



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