Class: Tkellem::Bouncer

Inherits:
Object
  • Object
show all
Includes:
EasyLogger
Defined in:
lib/tkellem/bouncer.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from EasyLogger

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

Constructor Details

#initialize(network_user) ⇒ Bouncer

Returns a new instance of Bouncer.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/tkellem/bouncer.rb', line 16

def initialize(network_user)
  @network_user = network_user
  @user = network_user.user
  @network = network_user.network

  @nick = network_user.nick
  # maps { client_conn => state_hash }
  @active_conns = {}
  @welcomes = []
  @rooms = Room.where(network_user_id: network_user.id).each_with_object({}) { |room,h| h[room.name] = room }
  # maps { client_conn => away_status_or_nil }
  @away = {}
  # plugin data
  @data = {}
  # clients waiting for us to connect to the irc server
  @waiting_clients = []
  @awaiting_replies = {}

  connect!
end

Instance Attribute Details

#connected_atObject (readonly)

Returns the value of attribute connected_at.



12
13
14
# File 'lib/tkellem/bouncer.rb', line 12

def connected_at
  @connected_at
end

#networkObject (readonly)

Returns the value of attribute network.



12
13
14
# File 'lib/tkellem/bouncer.rb', line 12

def network
  @network
end

#network_userObject (readonly)

Returns the value of attribute network_user.



12
13
14
# File 'lib/tkellem/bouncer.rb', line 12

def network_user
  @network_user
end

#nickObject (readonly)

Returns the value of attribute nick.



12
13
14
# File 'lib/tkellem/bouncer.rb', line 12

def nick
  @nick
end

#userObject (readonly)

Returns the value of attribute user.



12
13
14
# File 'lib/tkellem/bouncer.rb', line 12

def user
  @user
end

Class Method Details

.add_plugin(plugin) ⇒ Object



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

def self.add_plugin(plugin)
  self.plugins << plugin
end

Instance Method Details

#active_connsObject



41
42
43
# File 'lib/tkellem/bouncer.rb', line 41

def active_conns
  @active_conns.keys
end

#check_away_statusObject

Away Statuses



194
195
196
197
198
199
200
201
202
203
204
# File 'lib/tkellem/bouncer.rb', line 194

def check_away_status
  # for now we pretty much randomly pick an away status if multiple are set
  # by clients
  if @away.any? { |k,v| !v }
    # we have a client who isn't away
    send_msg("AWAY")
  else
    message = @away.values.first || "Away"
    send_msg("AWAY :#{message}")
  end
end

#client_msg(client, 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/tkellem/bouncer.rb', line 79

def client_msg(client, msg)
  return if plugins.any? do |plugin|
    !plugin.client_msg(self, client, msg)
  end

  forward = case msg.command
  when 'PING'
    client.send_msg(":tkellem!tkellem PONG tkellem :#{msg.args.last}")
    false
  when 'AWAY'
    @away[client] = msg.args.last
    check_away_status
    false
  when 'NICK'
    @nick = msg.args.last
    true
  when 'WHO'
    client
  else
    true
  end

  if forward
    # send to server
    send_msg(msg)

    # replay to other connected clients
    if msg.command == "PRIVMSG" && (!msg.ctcp? || msg.action?)
      msg.readdress_to(nick)

      @active_conns.each do |c,s|
        next if c == client
        c.send_msg(msg)
      end
    end

    flag_for_reply(msg.command, forward) if forward != true
  end
end

#connect_client(client) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/tkellem/bouncer.rb', line 53

def connect_client(client)
  @active_conns[client] = {}
  @away[client] = nil

  if !connected?
    @waiting_clients << client
    client.say_as_tkellem("Connecting you to the IRC server. Please wait...")
    return
  end

  # force the client nick
  client.send_msg(":#{client.connecting_nick} NICK #{nick}") if client.connecting_nick != nick
  send_welcome(client)
  # make the client join all the rooms that we're in
  @rooms.each_value { |room| client.simulate_join(room) }

  plugins.each { |plugin| plugin.new_client_connected(self, client) }
  check_away_status
end

#connected?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/tkellem/bouncer.rb', line 49

def connected?
  !!@connected
end

#connection_established(conn) ⇒ Object



218
219
220
221
222
223
224
# File 'lib/tkellem/bouncer.rb', line 218

def connection_established(conn)
  @conn = conn
  # TODO: support sending a real username, realname, etc
  send_msg("USER #{@user.username} somehost tkellem :#{@user.name}@tkellem")
  change_nick(@nick, true)
  @connected_at = Time.now
end

#data(key) ⇒ Object



37
38
39
# File 'lib/tkellem/bouncer.rb', line 37

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

#disconnect_client(client) ⇒ Object



73
74
75
76
77
# File 'lib/tkellem/bouncer.rb', line 73

def disconnect_client(client)
  @away.delete(client)
  check_away_status
  @active_conns.delete(client)
end

#disconnected!Object



226
227
228
229
230
231
232
233
# File 'lib/tkellem/bouncer.rb', line 226

def disconnected!
  debug "OMG we got disconnected."
  @conn = nil
  @connected = false
  @connected_at = nil
  @active_conns.each { |c,s| c.close_connection }
  connect!
end

#flag_for_reply(command, conn) ⇒ Object



119
120
121
# File 'lib/tkellem/bouncer.rb', line 119

def flag_for_reply(command, conn)
  @awaiting_replies[command] = conn
end

#kill!Object



235
236
237
# File 'lib/tkellem/bouncer.rb', line 235

def kill!
  @active_conns.each { |c,s| c.close_connection }
end

#nameObject Also known as: log_name



207
208
209
# File 'lib/tkellem/bouncer.rb', line 207

def name
  "#{user.name}-#{network.name}"
end

#send_msg(msg) ⇒ Object



212
213
214
215
216
# File 'lib/tkellem/bouncer.rb', line 212

def send_msg(msg)
  return unless @conn
  trace "to server: #{msg}"
  @conn.send_data("#{msg}\r\n")
end

#server_msg(msg) ⇒ Object



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
155
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
# File 'lib/tkellem/bouncer.rb', line 123

def server_msg(msg)
  return if plugins.any? do |plugin|
    !plugin.server_msg(self, msg)
  end

  forward = case msg.command
  when /0\d\d/, /2[56]\d/, /37[256]/
    @welcomes << msg
    ready! if msg.command == "376" # end of MOTD
    false
  when 'JOIN'
    room_name = msg.args.first
    if msg.target_user == @nick && !@rooms[room_name]
      room = Room.create!(network_user_id: network_user.id, name: room_name)
      @rooms[room_name] = room
    end
    true
  when 'PART'
    room_name = msg.args.first
    if msg.target_user == @nick
      room = @rooms.delete(room_name)
      room.destroy
    end
    true
  when 'TOPIC'
    if room = @rooms[msg.args.first]
      room.topic = msg.args.last
    end
  when '332' # topic replay
    if room = @rooms[msg.args[1]]
      room.topic = msg.args.last
    end
  when '333' # topic timestamp
    if room = @rooms[msg.args[1]]
      room.topic_setter = msg.args[2]
      room.topic_time = msg.args[3]
    end
  when 'PING'
    send_msg("PONG tkellem!tkellem :#{msg.args.last}")
    false
  when 'PONG'
    # swallow it, we handle ping-pong from clients separately, in
    # BouncerConnection
    false
  when '433'
    # nick already in use, try another
    change_nick("#{@nick}_")
    false
  when 'NICK'
    if msg.prefix == nick
      @nick = msg.args.last
    end
    true
  when '352'
    @awaiting_replies['WHO'] || true
  when '315'
    @awaiting_replies.delete('WHO') || true
  else
    true
  end

  if forward == true
    # send to clients
    @active_conns.each { |c,s| c.send_msg(msg) }
  elsif forward && @active_conns.include?(forward)
    forward.send_msg(msg)
  end
end