Class: Mutt::Ldap::Query::Worker

Inherits:
Object
  • Object
show all
Defined in:
lib/mutt/ldap/query/worker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query) ⇒ Worker

Returns a new instance of Worker.



14
15
16
# File 'lib/mutt/ldap/query/worker.rb', line 14

def initialize(query)
  @query = query
end

Instance Attribute Details

#queryObject (readonly)

Returns the value of attribute query.



12
13
14
# File 'lib/mutt/ldap/query/worker.rb', line 12

def query
  @query
end

Instance Method Details

#configObject



27
28
29
# File 'lib/mutt/ldap/query/worker.rb', line 27

def config
  @config ||= YAML.load_file(config_path)
end

#config_pathObject



18
19
20
21
# File 'lib/mutt/ldap/query/worker.rb', line 18

def config_path
  @xdg ||= XDG::Environment.new
  File.join(@xdg.config_home, 'mutt-ldap-query', 'config.yml')
end

#configured?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/mutt/ldap/query/worker.rb', line 23

def configured?
  File.exist?(config_path)
end

#edit_configObject



65
66
67
# File 'lib/mutt/ldap/query/worker.rb', line 65

def edit_config
  system("${EDITOR:-vi} #{config_path}")
end

#gen_configObject



58
59
60
61
62
63
# File 'lib/mutt/ldap/query/worker.rb', line 58

def gen_config
  raise "#{config_path} already exist." if configured?

  FileUtils.mkdir_p(File.dirname(config_path))
  File.write(config_path, sample_config, perm: 0o600)
end

#mail_attributesObject



54
55
56
# File 'lib/mutt/ldap/query/worker.rb', line 54

def mail_attributes
  %w[mail mailAlternateAddress]
end

#sample_configObject



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/mutt/ldap/query/worker.rb', line 69

def sample_config
  <<~CONFIG
    :host: ldap.example.com
    :port: 389
    :base: ou=people,dc=example,dc=com
    :auth:
      :method: :simple
      :username: cn=mutt-ldap-query,ou=services,dc=example,dc=com
      :password: secret
    :encryption:
      :method: :start_tls
  CONFIG
end

#searchObject



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/mutt/ldap/query/worker.rb', line 31

def search
  result = []

  ldap = Net::LDAP.new(config)
  ldap.search(attributes: search_attributes, filter: search_filter) do |entry|
    mail_attributes.map { |x| entry[x] }.flatten.each do |mail|
      result << "#{mail}\t#{entry['cn'].first}\t#{entry['description'].first}"
    end
  end

  result
end

#search_attributesObject



44
45
46
# File 'lib/mutt/ldap/query/worker.rb', line 44

def search_attributes
  %w[cn mail mailAlternateAddress description]
end

#search_filterObject



48
49
50
51
52
# File 'lib/mutt/ldap/query/worker.rb', line 48

def search_filter
  res = Net::LDAP::Filter.eq('objectClass', 'person')
  res &= query.map { |q| search_attributes.map { |a| Net::LDAP::Filter.contains(a, q) } }.flatten.inject(:|) if query.any?
  res
end