Class: Tkellem::Bouncer

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

Defined Under Namespace

Classes: Room

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.



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

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 = {}
  # maps { client_conn => away_status_or_nil }
  @away = {}
  # plugin data
  @data = {}
  # clients waiting for us to connect to the irc server
  @waiting_clients = []

  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



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

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

Instance Method Details

#active_connsObject



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

def active_conns
  @active_conns.keys
end

#check_away_statusObject

Away Statuses



163
164
165
166
167
168
169
170
171
172
173
# File 'lib/tkellem/bouncer.rb', line 163

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



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

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
  else
    true
  end

  if forward
    # send to server
    send_msg(msg)
  end
end

#connect_client(client) ⇒ Object



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

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)


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

def connected?
  !!@connected
end

#connection_established(conn) ⇒ Object



187
188
189
190
191
192
193
# File 'lib/tkellem/bouncer.rb', line 187

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



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

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

#disconnect_client(client) ⇒ Object



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

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

#disconnected!Object



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

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

#kill!Object



204
205
206
# File 'lib/tkellem/bouncer.rb', line 204

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

#nameObject Also known as: log_name



176
177
178
# File 'lib/tkellem/bouncer.rb', line 176

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

#send_msg(msg) ⇒ Object



181
182
183
184
185
# File 'lib/tkellem/bouncer.rb', line 181

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

#server_msg(msg) ⇒ Object



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

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'
    @rooms[msg.args.first] = Room.new(msg.args.first) if msg.target_user == @nick
    true
  when 'PART'
    @rooms.delete(msg.args.first) if msg.target_user == @nick
    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
  else
    true
  end

  if forward
    # send to clients
    @active_conns.each { |c,s| c.send_msg(msg) }
  end
end