Module: Fargo::Protocol::Hub

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 DC

#parse_data?, #post_init, #publish_args, #receive_data, #receive_data_chunk, #send_message, #unbind

Methods included from Fargo::Parser

#parse_command_message, #parse_message

Methods included from Utils

#encode_char, #generate_key, #generate_lock

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

#connection_typeObject



10
11
12
# File 'lib/fargo/protocol/hub.rb', line 10

def connection_type
  :hub
end

#receive_message(type, message) ⇒ Object

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



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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/fargo/protocol/hub.rb', line 16

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.info "Disconnecting because of: #{message.inspect}"
      close_connection_after_writing
    when :hello
      if message[:nick] == @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
      EventMachine.connect message[:address], message[:port],
          Protocol::Peer do |conn|
        conn.client     = @client
        conn.send_lock # We connect first, we send lock first
      end

    when :search
      # Let the client handle the results
      search = Search.new message
      @listings = @client.search_local_listings search

      @results = @listings.map do |l|
        file = l.name.gsub '/', "\\"
        if File.directory? l.name
          s = file
        else
          s = "#{file}\005#{l.size}"
        end

        s + sprintf(" %d/%d\005%s (%s:%d)", @client.open_upload_slots,
                                            @client.config.upload_slots,
                                            'TTH:' + l.tth,
                                            @client.config.hub_address,
                                            @client.config.hub_port)
      end

      # Send all the results to the peer. Take care of active/passive
      # connections
      if message[:address]
        socket = EventMachine.open_datagram_socket '0.0.0.0', 0
        @results.each{ |r|
          socket.send_datagram "$SR #{@client.config.nick} #{r}|",
            message[:address], message[:port]
        }
        socket.close_connection_after_writing
      else
        @results.each{ |r|
          send_message 'SR',
            "#{@client.config.nick} #{r}\005#{message[:searcher]}"
        }
      end

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

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

  end
end