123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- require 'erubis'
- module Dovecot
- module Conf
- def self.name(str)
- if str =~ /\s/
- "\"#{str}\""
- else
- str
- end
- end
- def self.value(v, default = nil)
- if v.nil?
- default.to_s
- elsif v === true
- 'yes'
- elsif v === false
- 'no'
- elsif v.kind_of?(Array)
- v.join(' ')
- else
- v.to_s
- end
- end
- def self.attribute(conf, k, default = nil)
- v = conf[k]
- if v.nil?
- "##{k} = #{value(default)}"
- else
- "#{k} = #{value(v)}"
- end
- end
- def self.protocols(conf)
- # dovecot: config: Fatal: Error in configuration file /etc/dovecot/dovecot.conf: protocols: Unknown protocol: lda
- ignore_protos = [ 'lda' ]
- protos = Dovecot::Protocols.list(conf) - ignore_protos
- protos.empty? ? 'none' : protos.join(' ')
- end
- def self.authdb(driver, type, conf)
- template =
- '<% confs = @conf.kind_of?(Array)? @conf : [ @conf ]
- confs.each do |conf| -%>
- <%= @Dovecot_Conf.name(@type) %> {
- <% unless conf.has_key?("driver") -%>
- driver = <%= @driver %>
- <% end -%>
- <% conf.sort.each do |key, value|
- unless value.nil?
- -%>
- <%= key %> = <%= @Dovecot_Conf.value(value) %>
- <% end
- end
- -%>
- }
- <% end -%>'
- eruby = Erubis::Eruby.new(template)
- eruby.evaluate(
- :driver => driver,
- :type => type,
- :conf => conf,
- :Dovecot_Conf => Dovecot::Conf
- )
- end
- def self.plugin(name, conf)
- template =
- 'plugin {
- <% @conf.sort.each do |key, value|
- unless value.nil?
- -%>
- <%= key %> = <%= @Dovecot_Conf.value(value) %>
- <% end
- end -%>
- }'
- eruby = Erubis::Eruby.new(template)
- eruby.evaluate(
- :conf => conf,
- :Dovecot_Conf => Dovecot::Conf
- )
- end
- def self.namespace(ns)
- template =
- 'namespace <%= @Dovecot_Conf.name(@ns["name"]) %> {
- <% if @ns["mailboxes"].kind_of?(Array) or @ns["mailboxes"].kind_of?(Hash)
- mailboxes = @ns["mailboxes"].kind_of?(Array) ? @ns["mailboxes"] : [ @ns["mailboxes"] ]
- mailboxes.each do |mailbox|
- mailbox.sort.each do |key, values|
- -%>
- mailbox <%= @Dovecot_Conf.name(key) %> {
- <% values.sort.each do |key, value| -%>
- <%= key %> = <%= @Dovecot_Conf.value(value) %>
- <% end -%>
- }
- <% end -%>
- <% end -%>
- <% end -%>
- <% @ns.sort.each do |key, value|
- next if key == "mailboxes"
- -%>
- <%= key %> = <%= @Dovecot_Conf.value(value) %>
- <% end -%>
- }'
- eruby = Erubis::Eruby.new(template)
- eruby.evaluate(
- :ns => ns,
- :Dovecot_Conf => Dovecot::Conf
- )
- end
- def self.protocol(name, conf)
- template =
- 'protocol <%= @Dovecot_Conf.name(@name) %> {
- <% @conf.sort.each do |key, value| -%>
- <%= key %> = <%= @Dovecot_Conf.value(value) %>
- <% end -%>
- }'
- eruby = Erubis::Eruby.new(template)
- eruby.evaluate(
- :name => name,
- :conf => conf,
- :Dovecot_Conf => Dovecot::Conf
- )
- end
- def self.service(name, conf)
- template =
- 'service <%= @Dovecot_Conf.name(@name) %> {
- <% if @conf["listeners"].kind_of?(Array) or @conf["listeners"].kind_of?(Hash)
- listeners = @conf["listeners"].kind_of?(Array) ? @conf["listeners"] : [ @conf["listeners"] ]
- listeners.each do |listener|
- listener.sort.each do |service, values|
- service_proto = service.split(":")[0]
- service_name = service.split(":")[1]
- -%>
- <%= service_proto %>_listener <%= @Dovecot_Conf.name(service_name) %> {
- <% values.sort.each do |key, value|-%>
- <%= key %> = <%= @Dovecot_Conf.value(value) %>
- <% end -%>
- }
- <% end -%>
- <% end -%>
- <% end -%>
- <% @conf.sort.each do |key, value|
- next if key == "listeners"
- -%>
- <%= key %> = <%= @Dovecot_Conf.value(value) %>
- <% end -%>
- }'
- eruby = Erubis::Eruby.new(template)
- eruby.evaluate(
- :name => name,
- :conf => conf,
- :Dovecot_Conf => Dovecot::Conf
- )
- end
- def self.map(map)
- template =
- 'map {
- <% @map.sort.each do |k, v|
- if v.kind_of?(Hash)
- -%>
- <%= @Dovecot_Conf.name(k) %> {
- <%
- v.sort.each do |k2, v2|
- -%>
- <%= k2 %> = <%= @Dovecot_Conf.value(v2) %>
- <% end -%>
- }
- <% else -%>
- <%= k %> = <%= @Dovecot_Conf.value(v) %>
- <% end
- end
- -%>
- }'
- eruby = Erubis::Eruby.new(template)
- eruby.evaluate(
- :map => map,
- :Dovecot_Conf => Dovecot::Conf
- )
- end
- end
- end
|