conf.rb 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. -%>
  53. }
  54. <% end -%>'
  55. eruby = Erubis::Eruby.new(template)
  56. eruby.evaluate(
  57. :driver => driver,
  58. :type => type,
  59. :conf => conf,
  60. :Dovecot_Conf => Dovecot::Conf
  61. )
  62. end
  63. def self.plugin(name, conf)
  64. template =
  65. 'plugin {
  66. <% @conf.sort.each do |key, value|
  67. unless value.nil?
  68. -%>
  69. <%= key %> = <%= @Dovecot_Conf.value(value) %>
  70. <% end
  71. end -%>
  72. }'
  73. eruby = Erubis::Eruby.new(template)
  74. eruby.evaluate(
  75. :conf => conf,
  76. :Dovecot_Conf => Dovecot::Conf
  77. )
  78. end
  79. def self.namespace(ns)
  80. template =
  81. 'namespace <%= @Dovecot_Conf.name(@ns["name"]) %> {
  82. <% if @ns["mailboxes"].kind_of?(Array) or @ns["mailboxes"].kind_of?(Hash)
  83. mailboxes = @ns["mailboxes"].kind_of?(Array) ? @ns["mailboxes"] : [ @ns["mailboxes"] ]
  84. mailboxes.each do |mailbox|
  85. mailbox.sort.each do |key, values|
  86. -%>
  87. mailbox <%= @Dovecot_Conf.name(key) %> {
  88. <% values.sort.each do |key, value| -%>
  89. <%= key %> = <%= @Dovecot_Conf.value(value) %>
  90. <% end -%>
  91. }
  92. <% end -%>
  93. <% end -%>
  94. <% end -%>
  95. <% @ns.sort.each do |key, value|
  96. next if key == "mailboxes"
  97. -%>
  98. <%= key %> = <%= @Dovecot_Conf.value(value) %>
  99. <% end -%>
  100. }'
  101. eruby = Erubis::Eruby.new(template)
  102. eruby.evaluate(
  103. :ns => ns,
  104. :Dovecot_Conf => Dovecot::Conf
  105. )
  106. end
  107. def self.protocol(name, conf)
  108. template =
  109. 'protocol <%= @Dovecot_Conf.name(@name) %> {
  110. <% @conf.sort.each do |key, value| -%>
  111. <%= key %> = <%= @Dovecot_Conf.value(value) %>
  112. <% end -%>
  113. }'
  114. eruby = Erubis::Eruby.new(template)
  115. eruby.evaluate(
  116. :name => name,
  117. :conf => conf,
  118. :Dovecot_Conf => Dovecot::Conf
  119. )
  120. end
  121. def self.service(name, conf)
  122. template =
  123. 'service <%= @Dovecot_Conf.name(@name) %> {
  124. <% if @conf["listeners"].kind_of?(Array) or @conf["listeners"].kind_of?(Hash)
  125. listeners = @conf["listeners"].kind_of?(Array) ? @conf["listeners"] : [ @conf["listeners"] ]
  126. listeners.each do |listener|
  127. listener.sort.each do |service, values|
  128. service_proto = service.split(":")[0]
  129. service_name = service.split(":")[1]
  130. -%>
  131. <%= service_proto %>_listener <%= @Dovecot_Conf.name(service_name) %> {
  132. <% values.sort.each do |key, value|-%>
  133. <%= key %> = <%= @Dovecot_Conf.value(value) %>
  134. <% end -%>
  135. }
  136. <% end -%>
  137. <% end -%>
  138. <% end -%>
  139. <% @conf.sort.each do |key, value|
  140. next if key == "listeners"
  141. -%>
  142. <%= key %> = <%= @Dovecot_Conf.value(value) %>
  143. <% end -%>
  144. }'
  145. eruby = Erubis::Eruby.new(template)
  146. eruby.evaluate(
  147. :name => name,
  148. :conf => conf,
  149. :Dovecot_Conf => Dovecot::Conf
  150. )
  151. end
  152. def self.map(map)
  153. template =
  154. 'map {
  155. <% @map.sort.each do |k, v|
  156. if v.kind_of?(Hash)
  157. -%>
  158. <%= @Dovecot_Conf.name(k) %> {
  159. <%
  160. v.sort.each do |k2, v2|
  161. -%>
  162. <%= k2 %> = <%= @Dovecot_Conf.value(v2) %>
  163. <% end -%>
  164. }
  165. <% else -%>
  166. <%= k %> = <%= @Dovecot_Conf.value(v) %>
  167. <% end
  168. end
  169. -%>
  170. }'
  171. eruby = Erubis::Eruby.new(template)
  172. eruby.evaluate(
  173. :map => map,
  174. :Dovecot_Conf => Dovecot::Conf
  175. )
  176. end
  177. end
  178. end