Module: Tkellem::BouncerConnection

Includes:
EM::Protocols::LineText2, EasyLogger
Defined in:
lib/tkellem/bouncer_connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from EasyLogger

#failsafe, logger, logger=, trace, #trace, trace=

Instance Attribute Details

#bouncerObject (readonly)

Returns the value of attribute bouncer.



24
25
26
# File 'lib/tkellem/bouncer_connection.rb', line 24

def bouncer
  @bouncer
end

#connected_atObject (readonly)

Returns the value of attribute connected_at.



24
25
26
# File 'lib/tkellem/bouncer_connection.rb', line 24

def connected_at
  @connected_at
end

#connecting_nickObject (readonly)

Returns the value of attribute connecting_nick.



24
25
26
# File 'lib/tkellem/bouncer_connection.rb', line 24

def connecting_nick
  @connecting_nick
end

#device_nameObject (readonly)

Returns the value of attribute device_name.



24
25
26
# File 'lib/tkellem/bouncer_connection.rb', line 24

def device_name
  @device_name
end

#nameObject (readonly) Also known as: log_name

Returns the value of attribute name.



24
25
26
# File 'lib/tkellem/bouncer_connection.rb', line 24

def name
  @name
end

#sslObject (readonly)

Returns the value of attribute ssl.



24
25
26
# File 'lib/tkellem/bouncer_connection.rb', line 24

def ssl
  @ssl
end

Instance Method Details

#connect_to_irc_serverObject



70
71
72
73
74
75
76
77
# File 'lib/tkellem/bouncer_connection.rb', line 70

def connect_to_irc_server
  @bouncer = @tkellem.find_bouncer(@user, @conn_name)
  return error!("Unknown connection: #{@conn_name}") unless @bouncer
  @state = :connected
  info "connected"
  schedule_ping_client
  @bouncer.connect_client(self)
end

#connect_to_tkellem_consoleObject



210
211
212
213
214
# File 'lib/tkellem/bouncer_connection.rb', line 210

def connect_to_tkellem_console
  send_msg(":tkellem 001 #{nick} :Welcome to the Tkellem admin console")
  send_msg(":tkellem 376 #{nick} :End")
  @state = :console
end

#data(key) ⇒ Object



31
32
33
# File 'lib/tkellem/bouncer_connection.rb', line 31

def data(key)
  @data[key] ||= {}
end

#error!(msg) ⇒ Object



63
64
65
66
67
68
# File 'lib/tkellem/bouncer_connection.rb', line 63

def error!(msg)
  info("ERROR :#{msg}")
  say_as_tkellem(msg)
  send_msg("ERROR :#{msg}")
  close_connection(true)
end

#initialize(tkellem_server, do_ssl) ⇒ Object



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

def initialize(tkellem_server, do_ssl)
  set_delimiter "\r\n"

  @ssl = do_ssl
  @tkellem = tkellem_server

  @state = :auth
  @name = 'new-conn'
  @data = {}
  @connected_at = Time.now
end

#maybe_connectObject



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/tkellem/bouncer_connection.rb', line 156

def maybe_connect
  return unless @connecting_nick && @username && !@user
  if @password
    @name = @username
    @user = User.authenticate(@username, @password)
    return error!("Unknown username: #{@username} or bad password.") unless @user

    if @conn_info && !@conn_info.empty?
      @conn_name, @device_name = @conn_info.split(':', 2)
      # 'default' or missing device_name to use the default backlog
      # pass a device_name to have device-independent backlogs
      @device_name = @device_name.presence || 'default'
      @name = "#{@username}-#{@conn_name}"
      @name += "-#{@device_name}" if @device_name
      connect_to_irc_server
    else
      @name = "#{@username}-console"
      connect_to_tkellem_console
    end
  else
    user = User.find_by_username(@username)
    if user || user_registration == 'closed'
      error!("No password given. Make sure to set your password in your IRC client config, and connect again.")
      if user_registration != 'closed'
        error!("If you are trying to register for a new account, this username is already taken. Please select another.")
      end
    else
      @state = :registration
      say_as_tkellem "Welcome to tkellem, #{@username}. If you already have an account and were trying to connect, please check your username, as it wasn't recognized."
      say_as_tkellem "Otherwise, follow these instructions to create an account."
      say_as_tkellem ' '
      if recaptcha = Setting.get('recaptcha_api_key').presence
        @state = :recaptcha
        require 'tkellem/plugins/recaptcha'
        @recaptcha = Recaptcha.new(*recaptcha.split(',', 2))
        say_as_tkellem "First, you'll need to take a captcha test to verify that you aren't an evil robot bent on destroying humankind."
        say_as_tkellem "Visit this URL, and tell me the code you are given after solving the captcha: #{@recaptcha.challenge_url}"
        return
      end
      user_registration_get_password
    end
  end
end

#msg_tkellem(msg) ⇒ Object



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
# File 'lib/tkellem/bouncer_connection.rb', line 79

def msg_tkellem(msg)
  case @state
  when :recaptcha
    if @recaptcha.valid_response?(msg.args.last)
      say_as_tkellem "Looks like you're human. Whew, I hate robots."
      user_registration_get_password
    else
      say_as_tkellem "Nope, that's not right. Please try again."
    end
  when :password
    user = User.create(:username => @username, :password => msg.args.last, :role => 'user')
    if user.errors.any?
      error!("There was an error creating your user account. Please try again, or contact the tkellem admin.")
    else
      @user = user
      say_as_tkellem("Your account has been created. Set your password in your IRC client and re-connect to start using tkellem.")
    end
  else
    if @user
      TkellemBot.run_command(msg.args.join(' '), @bouncer, self) do |response|
        say_as_tkellem(response)
      end
    end
  end
end

#nickObject



27
28
29
# File 'lib/tkellem/bouncer_connection.rb', line 27

def nick
  @bouncer ? @bouncer.nick : @connecting_nick
end

#ping_clientObject



52
53
54
55
56
57
58
59
60
61
# File 'lib/tkellem/bouncer_connection.rb', line 52

def ping_client
  failsafe("ping_client") do
    send_msg("PING tkellem")
    @ping_timer = EM::Timer.new(10) do
      # ping timeout
      info("PING timeout, closing connection")
      close_connection
    end
  end
end

#post_initObject



35
36
37
38
39
40
41
42
43
# File 'lib/tkellem/bouncer_connection.rb', line 35

def post_init
  failsafe(:post_init) do
    if ssl
      start_tls :verify_peer => false
    else
      ssl_handshake_completed
    end
  end
end

#receive_line(line) ⇒ Object



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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/tkellem/bouncer_connection.rb', line 109

def receive_line(line)
  failsafe("message: {#{line}}") do
    line.force_encoding Encoding::UTF_8
    trace "from client: #{line}"
    return if line.blank?
    msg = IrcMessage.parse(line)

    command = msg.command
    if @state != :auth && command == 'PRIVMSG' && msg.args.first == '-tkellem'
      msg_tkellem(IrcMessage.new(nil, 'TKELLEM', [msg.args.last]))
    elsif command == 'TKELLEM' || command == 'TK'
      msg_tkellem(msg)
    elsif command == 'PONG'
      if @ping_timer
        @ping_timer.cancel
        @ping_timer = nil
        # only schedule again if @ping_timer existed, so we don't schedule
        # multiple if the client just randomly sends PONGs
        schedule_ping_client
      end
    elsif command == 'CAP'
      # TODO: full support for CAP -- this just gets mobile colloquy connecting
      if msg.args.first =~ /req/i
        send_msg("CAP NAK")
      end
    elsif command == 'PASS' && @state == :auth
      @password = msg.args.first
    elsif command == 'NICK' && @state == :auth
      @connecting_nick = msg.args.first
      maybe_connect
    elsif command == 'QUIT'
      close_connection
    elsif command == 'USER' && @state == :auth
      unless @username
        @username, @conn_info = msg.args.first.strip.split('@', 2).map { |a| a.downcase }
      end
      maybe_connect
    elsif @state == :auth
      error!("Protocol error. You must authenticate first.")
    elsif @state == :connected
      @bouncer.client_msg(self, msg)
    else
      say_as_tkellem("You must connect to an irc network to do that.")
    end
  end
end

#say_as_tkellem(message) ⇒ Object



105
106
107
# File 'lib/tkellem/bouncer_connection.rb', line 105

def say_as_tkellem(message)
  send_msg(":-tkellem!~tkellem@tkellem PRIVMSG #{nick} :#{message}")
end

#schedule_ping_clientObject



48
49
50
# File 'lib/tkellem/bouncer_connection.rb', line 48

def schedule_ping_client
  EM::Timer.new(60) { ping_client }
end

#send_msg(msg) ⇒ Object



225
226
227
228
# File 'lib/tkellem/bouncer_connection.rb', line 225

def send_msg(msg)
  trace "to client: #{msg}"
  send_data("#{msg}\r\n")
end

#simulate_join(room) ⇒ Object



216
217
218
219
220
221
222
223
# File 'lib/tkellem/bouncer_connection.rb', line 216

def simulate_join(room)
  send_msg(":#{nick} JOIN #{room.name}")
  # TODO: intercept the NAMES response so that only this bouncer gets it
  # Otherwise other clients might show an "in this room" line.
  @bouncer.send_msg("NAMES #{room.name}\r\n")
  send_msg(IrcMessage.new(":tkellem", "332",  [nick, room.name, room.topic])) if room.topic
  send_msg(IrcMessage.new(":tkellem", "333",  [nick, room.name, room.topic_setter, room.topic_time])) if room.topic_setter && room.topic_time
end

#ssl_handshake_completedObject



45
46
# File 'lib/tkellem/bouncer_connection.rb', line 45

def ssl_handshake_completed
end

#unbindObject



230
231
232
233
234
# File 'lib/tkellem/bouncer_connection.rb', line 230

def unbind
  failsafe(:unbind) do
    @bouncer.disconnect_client(self) if @bouncer
  end
end

#user_registrationObject



200
201
202
203
# File 'lib/tkellem/bouncer_connection.rb', line 200

def user_registration
  val = Setting.get('user_registration')
  %(open verified).include?(val) ? val : 'closed'
end

#user_registration_get_passwordObject



205
206
207
208
# File 'lib/tkellem/bouncer_connection.rb', line 205

def user_registration_get_password
  @state = :password
  say_as_tkellem "You need to set an initial password for your account. Enter your password now:"
end