conf.rb 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. require 'erubis'
  2. module Dovecot
  3. module Conf
  4. def self.name(str)
  5. if str =~ /\s/
  6. "\"#{str}\""
  7. else
  8. str
  9. end
  10. end
  11. def self.value(v, default = nil)
  12. if v.nil?
  13. default.to_s
  14. elsif v === true
  15. 'yes'
  16. elsif v === false
  17. 'no'
  18. elsif v.kind_of?(Array)
  19. v.join(' ')
  20. else
  21. v.to_s
  22. end
  23. end
  24. def self.attribute(conf, k, default = nil)
  25. v = conf[k]
  26. if v.nil?
  27. "##{k} = #{value(default)}"
  28. else
  29. "#{k} = #{value(v)}"
  30. end
  31. end
  32. def self.protocols(conf)
  33. # dovecot: config: Fatal: Error in configuration file /etc/dovecot/dovecot.conf: protocols: Unknown protocol: lda
  34. ignore_protos = [ 'lda' ]
  35. protos = Dovecot::Protocols.list(conf) - ignore_protos
  36. protos.empty? ? 'none' : protos.join(' ')
  37. end
  38. def self.authdb(driver, type, conf)
  39. template =
  40. '<% confs = @conf.kind_of?(Array)? @conf : [ @conf ]
  41. confs.each do |conf| -%>
  42. <%= @Dovecot_Conf.name(@type) %> {
  43. <% unless conf.has_key?("driver") -%>
  44. driver = <%= @driver %>
  45. <% end -%>
  46. <% conf.sort.each do |key, value|
  47. unless value.nil?
  48. -%>
  49. <%= key %> = <%= @Dovecot_Conf.value(value) %>
  50. <% end
  51. end
  52. end -%>
  53. }'
  54. eruby = Erubis::Eruby.new(template)
  55. eruby.evaluate(
  56. :driver => driver,
  57. :type => type,
  58. :conf => conf,
  59. :Dovecot_Conf => Dovecot::Conf
  60. )
  61. end
  62. def self.plugin(name, conf)
  63. template =
  64. 'plugin {
  65. <% @conf.sort.each do |key, value|
  66. unless value.nil?
  67. -%>
  68. <%= key %> = <%= @Dovecot_Conf.value(value) %>
  69. <% end
  70. end -%>
  71. }'
  72. eruby = Erubis::Eruby.new(template)
  73. eruby.evaluate(
  74. :conf => conf,
  75. :Dovecot_Conf => Dovecot::Conf
  76. )
  77. end
  78. def self.namespace(ns)
  79. template =
  80. 'namespace <%= @Dovecot_Conf.name(@ns["name"]) %> {
  81. <% if @ns["mailboxes"].kind_of?(Array) or @ns["mailboxes"].kind_of?(Hash)
  82. mailboxes = @ns["mailboxes"].kind_of?(Array) ? @ns["mailboxes"] : [ @ns["mailboxes"] ]
  83. mailboxes.each do |mailbox|
  84. mailbox.sort.each do |key, values|
  85. -%>
  86. mailbox <%= @Dovecot_Conf.name(key) %> {
  87. <% values.sort.each do |key, value| -%>
  88. <%= key %> = <%= @Dovecot_Conf.value(value) %>
  89. <% end -%>
  90. }
  91. <% end -%>
  92. <% end -%>
  93. <% end -%>
  94. <% @ns.sort.each do |key, value|
  95. next if key == "mailboxes"
  96. -%>
  97. <%= key %> = <%= @Dovecot_Conf.value(value) %>
  98. <% end -%>
  99. }'
  100. eruby = Erubis::Eruby.new(template)
  101. eruby.evaluate(
  102. :ns => ns,
  103. :Dovecot_Conf => Dovecot::Conf
  104. )
  105. end
  106. def self.protocol(name, conf)
  107. template =
  108. 'protocol <%= @Dovecot_Conf.name(@name) %> {
  109. <% @conf.sort.each do |key, value| -%>
  110. <%= key %> = <%= @Dovecot_Conf.value(value) %>
  111. <% end -%>
  112. }'
  113. eruby = Erubis::Eruby.new(template)
  114. eruby.evaluate(
  115. :name => name,
  116. :conf => conf,
  117. :Dovecot_Conf => Dovecot::Conf
  118. )
  119. end
  120. def self.service(name, conf)
  121. template =
  122. 'service <%= @Dovecot_Conf.name(@name) %> {
  123. <% if @conf["listeners"].kind_of?(Array) or @conf["listeners"].kind_of?(Hash)
  124. listeners = @conf["listeners"].kind_of?(Array) ? @conf["listeners"] : [ @conf["listeners"] ]
  125. listeners.each do |listener|
  126. listener.sort.each do |service, values|
  127. service_proto = service.split(":")[0]
  128. service_name = service.split(":")[1]
  129. -%>
  130. <%= service_proto %>_listener <%= @Dovecot_Conf.name(service_name) %> {
  131. <% values.sort.each do |key, value|-%>
  132. <%= key %> = <%= @Dovecot_Conf.value(value) %>
  133. <% end -%>
  134. }
  135. <% end -%>
  136. <% end -%>
  137. <% end -%>
  138. <% @conf.sort.each do |key, value|
  139. next if key == "listeners"
  140. -%>
  141. <%= key %> = <%= @Dovecot_Conf.value(value) %>
  142. <% end -%>
  143. }'
  144. eruby = Erubis::Eruby.new(template)
  145. eruby.evaluate(
  146. :name => name,
  147. :conf => conf,
  148. :Dovecot_Conf => Dovecot::Conf
  149. )
  150. end
  151. def self.map(map)
  152. template =
  153. 'map {
  154. <% @map.sort.each do |k, v|
  155. if v.kind_of?(Hash)
  156. -%>
  157. <%= @Dovecot_Conf.name(k) %> {
  158. <%
  159. v.sort.each do |k2, v2|
  160. -%>
  161. <%= k2 %> = <%= @Dovecot_Conf.value(v2) %>
  162. <% end -%>
  163. }
  164. <% else -%>
  165. <%= k %> = <%= @Dovecot_Conf.value(v) %>
  166. <% end
  167. end
  168. -%>
  169. }'
  170. eruby = Erubis::Eruby.new(template)
  171. eruby.evaluate(
  172. :map => map,
  173. :Dovecot_Conf => Dovecot::Conf
  174. )
  175. end
  176. end
  177. end