conf.rb 3.8 KB

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