summaryrefslogtreecommitdiff
path: root/lib/nickserver/hkp/source.rb
blob: 9c7954600af1de411b8c9faa2b2a27d868330872 (plain)
1
2
3
4
5
6
7
8
9
10
11
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
require 'nickserver/source'
require 'nickserver/response'
require 'nickserver/hkp/response'
require 'nickserver/hkp/client'
require "nickserver/hkp/parse_key_info"
require "nickserver/hkp/key_info"


#
# Fetch keys via HKP
# http://tools.ietf.org/html/draft-shaw-openpgp-hkp-00
#

module Nickserver; module Hkp
  class Source < Nickserver::Source

    def query(nick, &block)
      search(nick) do |status, response|
        if status == 200
          best = pick_best_key(response)
          get_key_by_fingerprint(nick, best.keyid, &block)
        else
          yield Nickserver::Response.new(status, response)
        end
      end
    end

    def search(nick, &block)
      client.get_key_infos_by_email(nick) do |status, response|
        parser = ParseKeyInfo.new status, response
        yield parser.status_for(nick), parser.response_for(nick)
      end
    end

    protected

    #
    # for now, just pick the newest key.
    #
    # in the future, we should perhaps pick the newest key
    # that is signed by the oldest key.
    #
    def pick_best_key(key_info_list)
      key_info_list.sort {|a,b| a.creationdate <=> b.creationdate}.last
    end

    def get_key_by_fingerprint(nick, fingerprint)
      client.get_key_by_fingerprint fingerprint do |status, response|
        if status == 200
          yield Response.new nick, response
        else
          yield Nickserver::Response.new status, "HKP Request failed"
        end
      end
    end

    def client
      @client ||= Client.new(adapter)
    end
  end
end; end