Skip to content

Commit

Permalink
admin command to list open connections
Browse files Browse the repository at this point in the history
  • Loading branch information
codekitchen committed Jun 21, 2011
1 parent c1f7fb8 commit e3d6d85
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 8 deletions.
10 changes: 6 additions & 4 deletions lib/tkellem/bouncer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module Tkellem
class Bouncer
include Tkellem::EasyLogger

attr_reader :user, :network, :nick, :network_user
attr_reader :user, :network, :nick, :network_user, :connected_at
cattr_accessor :plugins
self.plugins = []

Expand Down Expand Up @@ -175,13 +175,14 @@ def connection_established(conn)
# TODO: support sending a real username, realname, etc
send_msg("USER #{@user.username} somehost tkellem :#{@user.name}@tkellem")
change_nick(@nick, true)
check_away_status
@connected_at = Time.now
end

def disconnected!
debug "OMG we got disconnected."
@conn = nil
@connected = false
@connected_at = nil
@active_conns.each { |c,s| c.unbind }
connect!
end
Expand Down Expand Up @@ -215,13 +216,14 @@ def connect!
end

def ready!
return if @joined_rooms
@joined_rooms = true
@rooms.each do |room|
send_msg("JOIN #{room}")
end

check_away_status

# We're all initialized, allow connections
@connected_at = Time.now
@connected = true

@network_user.combined_at_connect.each do |line|
Expand Down
3 changes: 2 additions & 1 deletion lib/tkellem/bouncer_connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ def initialize(tkellem_server, do_ssl)
@state = :auth
@name = 'new-conn'
@data = {}
@connected_at = Time.now
end
attr_reader :ssl, :bouncer, :name, :device_name, :connecting_nick
attr_reader :ssl, :bouncer, :name, :device_name, :connecting_nick, :connected_at
alias_method :log_name, :name

def nick
Expand Down
15 changes: 15 additions & 0 deletions lib/tkellem/tkellem_bot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,21 @@ def execute
end
end
end

class ConnectionsCommand < Command
register 'connections'

def execute
require 'socket'
$tkellem_server.bouncers.each do |k, bouncer|
respond "#{bouncer.user.username}@#{bouncer.network.name} (#{bouncer.connected? ? 'connected' : 'connecting'}) #{"since #{bouncer.connected_at}" if bouncer.connected?}"
bouncer.active_conns.each do |conn|
port, addr = Socket.unpack_sockaddr_in(conn.get_peername)
respond " #{addr} device=#{conn.device_name} since #{conn.connected_at}"
end
end
end
end
end

end
1 change: 1 addition & 0 deletions lib/tkellem/tkellem_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class TkellemServer
def initialize
@listeners = {}
@bouncers = {}
$tkellem_server = self

ListenAddress.all.each { |a| listen(a) }
NetworkUser.find_each { |nu| add_bouncer(Bouncer.new(nu)) }
Expand Down
5 changes: 2 additions & 3 deletions spec/irc_server_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def make_server
end

def send_welcome(s, &just_before_last)
s.should_receive(:send_msg).with("USER speccer somehost tkellem :speccer")
s.should_receive(:send_msg).with("USER speccer somehost tkellem :speccer@tkellem")
s.should_receive(:send_msg).with("NICK speccer")
s.should_receive(:send_msg).with("AWAY :Away")
s.connection_established(nil)
Expand All @@ -35,9 +35,8 @@ def connected_server
it "should connect to the server on creation" do
s = make_server
s.connected?.should_not be_true
s.should_receive(:send_msg).with("USER speccer somehost tkellem :speccer")
s.should_receive(:send_msg).with("USER speccer somehost tkellem :speccer@tkellem")
s.should_receive(:send_msg).with("NICK speccer")
s.should_receive(:send_msg).with("AWAY :Away")
s.connection_established(nil)
end

Expand Down

0 comments on commit e3d6d85

Please sign in to comment.