conf.rb 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. (Dovecot::Protocols.list(conf) - ignore_protos).join(' ')
  29. end
  30. def self.authdb(driver, type, conf)
  31. template =
  32. '<% confs = [ @conf ].flatten(1)
  33. confs.each do |conf| -%>
  34. <%= @type %> {
  35. <% unless conf.has_key?("driver") -%>
  36. driver = <%= @driver %>
  37. <% end -%>
  38. <% conf.sort.each do |key, value|
  39. unless value.nil?
  40. -%>
  41. <%= key %> = <%= @Dovecot_Conf.value(value) %>
  42. <% end
  43. end
  44. end -%>
  45. }'
  46. eruby = Erubis::Eruby.new(template)
  47. eruby.evaluate(
  48. :driver => driver,
  49. :type => type,
  50. :conf => conf,
  51. :Dovecot_Conf => Dovecot::Conf
  52. )
  53. end
  54. def self.plugin(name, conf)
  55. template =
  56. 'plugin {
  57. <% @conf.sort.each do |key, value|
  58. unless value.nil?
  59. -%>
  60. <%= key %> = <%= @Dovecot_Conf.value(value) %>
  61. <% end
  62. end -%>
  63. }'
  64. eruby = Erubis::Eruby.new(template)
  65. eruby.evaluate(
  66. :conf => conf,
  67. :Dovecot_Conf => Dovecot::Conf
  68. )
  69. end
  70. def self.namespace(ns)
  71. template =
  72. 'namespace <%= @ns["name"] %> {
  73. <% @ns.sort.each do |key, value|
  74. if key != "name"
  75. -%>
  76. <%= key %> = <%= @Dovecot_Conf.value(value) %>
  77. <% end
  78. end
  79. -%>
  80. }'
  81. eruby = Erubis::Eruby.new(template)
  82. eruby.evaluate(
  83. :ns => ns,
  84. :Dovecot_Conf => Dovecot::Conf
  85. )
  86. end
  87. def self.protocol(name, conf)
  88. template =
  89. 'protocol <%= @name %> {
  90. <% @conf.sort.each do |key, value| -%>
  91. <%= key %> = <%= @Dovecot_Conf.value(value) %>
  92. <% end -%>
  93. }'
  94. eruby = Erubis::Eruby.new(template)
  95. eruby.evaluate(
  96. :name => name,
  97. :conf => conf,
  98. :Dovecot_Conf => Dovecot::Conf
  99. )
  100. end
  101. def self.service(name, conf)
  102. template =
  103. 'service <%= @name %> {
  104. <% if @conf["listeners"].kind_of?(Array) or @conf["listeners"].kind_of?(Hash)
  105. listeners = [ @conf["listeners"] ].flatten(1)
  106. listeners.each do |listener|
  107. listener.sort.each do |service, values|
  108. service_proto = service.split(":")[0]
  109. service_name = service.split(":")[1]
  110. -%>
  111. <%= service_proto %>_listener <%= service_name %> {
  112. <% values.sort.each do |key, value|-%>
  113. <%= key %> = <%= @Dovecot_Conf.value(value) %>
  114. <% end -%>
  115. }
  116. <% end -%>
  117. <% end -%>
  118. <% end -%>
  119. <% @conf.sort.each do |key, value|
  120. if key != "listeners"
  121. -%>
  122. <%= key %> = <%= @Dovecot_Conf.value(value) %>
  123. <% end -%>
  124. <% end -%>
  125. }'
  126. eruby = Erubis::Eruby.new(template)
  127. eruby.evaluate(
  128. :name => name,
  129. :conf => conf,
  130. :Dovecot_Conf => Dovecot::Conf
  131. )
  132. end
  133. def self.map(map)
  134. template =
  135. 'map {
  136. <% @map.sort.each do |k, v|
  137. if v.kind_of?(Hash)
  138. -%>
  139. <%= k %> {
  140. <%
  141. v.sort.each do |k2, v2|
  142. -%>
  143. <%= k2 %> = <%= @Dovecot_Conf.value(v2) %>
  144. <% end -%>
  145. }
  146. <% else -%>
  147. <%= k %> = <%= @Dovecot_Conf.value(v) %>
  148. <% end
  149. end
  150. -%>
  151. }'
  152. eruby = Erubis::Eruby.new(template)
  153. eruby.evaluate(
  154. :map => map,
  155. :Dovecot_Conf => Dovecot::Conf
  156. )
  157. end
  158. end
  159. end