Class: Fargo::Protocol::Hub

Inherits:
EventMachine::Connection
  • Object
show all
Includes:
DC, Utils
Defined in:
lib/fargo/protocol/hub.rb

Instance Attribute Summary collapse

Attributes included from DC

#client

Instance Method Summary collapse

Methods included from Utils

#encode_char, #generate_key, #generate_lock

Methods included from DC

#post_init, #publish_args, #receive_data, #send_message, #unbind

Methods included from Fargo::Parser

#parse_command_message, #parse_message

Instance Attribute Details

#hubnameObject (readonly)

Returns the value of attribute hubname.



8
9
10
# File 'lib/fargo/protocol/hub.rb', line 8

def hubname
  @hubname
end

Instance Method Details

#receive_message(type, message) ⇒ Object

See <www.teamfair.info/DC-Protocol.htm> for specifics on the DC protocol



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
# File 'lib/fargo/protocol/hub.rb', line 12

def receive_message type, message
  case type
    when :lock
      @validated = false
      send_message 'Key', generate_key(message[:lock])
    when :hubname
      @hubname = message[:name]
      send_message 'ValidateNick', @client.config.nick unless @validated
    when :getpass
      send_message 'MyPass', @client.config.password
    when :badpass, :hubfull
      Fargo.logger.warn "Disconnecting because of: #{message.inspect}"
      close_connection_after_writing
    when :hello
      if message[:who] == @client.config.nick
        Fargo.logger.info "Connected to DC Hub #{@hubname}"
        @validated = true

        send_message 'Version', '1,0091'
        send_message 'MyINFO', "$ALL #{@client.config.nick} " +
          "#{@client.description}$ $#{@client.config.speed}" +
          "#{@status || 1.chr}$#{@client.config.email}" +
          "$#{@client.share_size}$"
        send_message 'GetNickList'
      end

    when :connect_to_me
      if !@client.nicks.include?(message[:nick])
        Fargo.logger.info "Invalid connect_to_me request from: #{message[:nick]}"
        return
      end

      EventMachine.connect message[:address], message[:port],
          Fargo::Protocol::Download do |conn|
        conn.client     = @client
        conn.send_lock # We connect first, we send lock first
      end

    when :search
      # Let the client handle the results
      @results = @client.search_files message

      # Send all the results to the peer. Take care of active/passive
      # connections
      @results.each { |r|
        if message[:address]
          r.active_send @client.config.nick, message[:ip], message[:port]
        else
          send_message 'SR', "#{@client.config.nick} #{r}"
        end
      }

    when :revconnect
      if @client.config.passive
        send_message 'RevConnectToMe',
            "#{@client.config.nick} #{message[:who]}"
      else
        send_message 'ConnectToMe', "#{@client.config.nick} #{@client.config.address}:#{@client.config.extport}"
      end

    # proxy this message on up the stack if we don't handle it
    else
      super

  end
end