Class: MailManager::Lib

Inherits:
Object
  • Object
show all
Defined in:
lib/mailmanager/lib.rb

Overview

:nodoc:all

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.escape(s) ⇒ Object



235
236
237
238
# File 'lib/mailmanager/lib.rb', line 235

def self.escape(s)
  escaped = s.to_s.gsub('\'', '\'\\\'\'')
  %Q{"#{escaped}"}
end

Instance Method Details

#add_member(list, member) ⇒ Object



80
81
82
83
84
# File 'lib/mailmanager/lib.rb', line 80

def add_member(list, member)
  cmd = :withlist
  out = command(cmd, :name => list.name, :wlcmd => :AddMember, :arg => member)
  parse_json_output(out)
end

#add_moderator(list, email) ⇒ Object



113
114
115
116
117
118
119
120
121
# File 'lib/mailmanager/lib.rb', line 113

def add_moderator(list, email)
  if moderators(list)['return'].include?(email)
    raise ModeratorAlreadyExistsError, "#{email} is already a moderator of #{list.name}"
  end
  cmd = :withlist
  out = command(cmd, :name => list.name, :wlcmd => 'moderator.append',
                :arg => email)
  parse_json_output(out)
end

#approved_add_member(list, member) ⇒ Object



86
87
88
89
90
91
# File 'lib/mailmanager/lib.rb', line 86

def approved_add_member(list, member)
  cmd = :withlist
  out = command(cmd, :name => list.name, :wlcmd => :ApprovedAddMember,
                :arg => member)
  parse_json_output(out)
end

#approved_delete_member(list, email) ⇒ Object



100
101
102
103
104
105
# File 'lib/mailmanager/lib.rb', line 100

def approved_delete_member(list, email)
  cmd = :withlist
  out = command(cmd, :name => list.name, :wlcmd => :ApprovedDeleteMember,
                :arg => email)
  parse_json_output(out)
end

#command(cmd, opts = {}) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/mailmanager/lib.rb', line 178

def command(cmd, opts = {})
  mailman_cmd = "#{mailmanager.root}/bin/#{cmd.to_s} "
  # delete opts as we handle them explicitly
  stdin = nil
  stdin = opts.delete(:stdin) if opts.respond_to?(:has_key?) && opts.has_key?(:stdin)
  case cmd
  when :newlist
    mailman_cmd += "-q "
    raise ArgumentError, "Missing :name param" if opts[:name].nil?
    raise ArgumentError, "Missing :admin_email param" if opts[:admin_email].nil?
    raise ArgumentError, "Missing :admin_password param" if opts[:admin_password].nil?
    mailman_cmd_suffix = [:name, :admin_email, :admin_password].map { |key|
      escape(opts.delete(key))
    }.join(' ')
    mailman_cmd += "#{mailman_cmd_suffix} "
  when :rmlist
    raise ArgumentError, "Missing :name param" if opts[:name].nil?
    mailman_cmd += "#{escape(opts.delete(:name))} "
  when :withlist
    raise ArgumentError, "Missing :name param" if opts[:name].nil?
    proxy_path = File.dirname(__FILE__)
    mailman_cmd = "PYTHONPATH=#{proxy_path} #{MailManager.python} #{mailman_cmd}"
    mailman_cmd += "-q -r listproxy.command #{escape(opts.delete(:name))} " +
                   "#{opts.delete(:wlcmd)} "
    if !opts[:arg].nil? && opts[:arg].length > 0
      mailman_cmd += "#{escape(opts.delete(:arg))} "
    end
  end

  # assume any leftover opts are POSIX-style args
  mailman_cmd += opts.keys.map { |k| "--#{k}=#{escape(opts[k])}" }.join(' ')
  mailman_cmd += ' ' if mailman_cmd[-1,1] != ' '
  mailman_cmd += "2>&1"
  if MailManager.debug
    puts "Running mailman command: #{mailman_cmd}"
    puts " with stdin: #{stdin}" unless stdin.nil?
  end
  out, process = run_command(mailman_cmd, stdin)

  if process.exitstatus > 0
    raise MailManager::MailmanExecuteError.new(mailman_cmd + ':' + out.to_s)
  end
  out
end

#create_list(params) ⇒ Object

Raises:

  • (ArgumentError)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/mailmanager/lib.rb', line 34

def create_list(params)
  raise ArgumentError, "Missing :name param" if params[:name].nil?
  list_name = params[:name]
  raise ListNameConflictError, "List \"#{list_name}\" already exists" if list_names.include?(list_name)
  cmd = :newlist
  # create the list
  out = command(cmd, params)
  # get the new list
  begin
    get_list(list_name)
  rescue ListNotFoundError
    raise MailmanExecuteError, "List creation failed: #{out}"
  end
end

#delete_list(params) ⇒ Object

Raises:



54
55
56
57
58
59
60
# File 'lib/mailmanager/lib.rb', line 54

def delete_list(params)
  params = {:name => params} unless params.respond_to?(:has_key?)
  raise ListNotFoundError, "#{params[:name]} does not exist" unless list_names.include?(params[:name])
  cmd = :rmlist
  out = command(cmd, params)
  parse_output(cmd, out)
end

#delete_member(list, email) ⇒ Object



93
94
95
96
97
98
# File 'lib/mailmanager/lib.rb', line 93

def delete_member(list, email)
  cmd = :withlist
  out = command(cmd, :name => list.name, :wlcmd => :DeleteMember,
                :arg => email)
  parse_json_output(out)
end

#delete_moderator(list, email) ⇒ Object



123
124
125
126
127
128
129
130
131
# File 'lib/mailmanager/lib.rb', line 123

def delete_moderator(list, email)
  unless moderators(list)['return'].include?(email)
    raise ModeratorNotFoundError, "#{email} is not a moderator of #{list.name}"
  end
  cmd = :withlist
  out = command(cmd, :name => list.name, :wlcmd => 'moderator.remove',
                :arg => email)
  parse_json_output(out)
end

#description(list) ⇒ Object



154
155
156
157
158
# File 'lib/mailmanager/lib.rb', line 154

def description(list)
  cmd = :withlist
  out = command(cmd, :name => list.name, :wlcmd => :description)
  parse_json_output(out)
end

#digest_members(list) ⇒ Object



74
75
76
77
78
# File 'lib/mailmanager/lib.rb', line 74

def digest_members(list)
  cmd = :withlist
  out = command(cmd, :name => list.name, :wlcmd => :getDigestMemberKeys)
  parse_json_output(out)
end

#escape(s) ⇒ Object



240
241
242
# File 'lib/mailmanager/lib.rb', line 240

def escape(s)
  self.class.escape(s)
end

#get_list(list_name) ⇒ Object

Raises:



49
50
51
52
# File 'lib/mailmanager/lib.rb', line 49

def get_list(list_name)
  raise ListNotFoundError, "#{list_name} does not exist" unless list_names.include?(list_name)
  MailManager::List.new(list_name)
end

#inject(list, message, queue = nil) ⇒ Object



133
134
135
136
137
138
# File 'lib/mailmanager/lib.rb', line 133

def inject(list, message, queue=nil)
  cmd = :inject
  params = {:listname => list.name, :stdin => message}
  params[:queue] = queue unless queue.nil?
  command(cmd, params)
end

#list_address(list) ⇒ Object



62
63
64
65
66
# File 'lib/mailmanager/lib.rb', line 62

def list_address(list)
  cmd = :withlist
  out = command(cmd, :name => list.name, :wlcmd => :getListAddress)
  parse_json_output(out)
end

#list_namesObject



30
31
32
# File 'lib/mailmanager/lib.rb', line 30

def list_names
  lists.map { |list| list.name }
end

#listsObject



24
25
26
27
28
# File 'lib/mailmanager/lib.rb', line 24

def lists
  cmd = :list_lists
  out = command(cmd)
  parse_output(cmd, out)
end

#mailmanagerObject



20
21
22
# File 'lib/mailmanager/lib.rb', line 20

def mailmanager
  MailManager::Base.instance
end

#moderators(list) ⇒ Object



107
108
109
110
111
# File 'lib/mailmanager/lib.rb', line 107

def moderators(list)
  cmd = :withlist
  out = command(cmd, :name => list.name, :wlcmd => :moderator)
  parse_json_output(out)
end

#parse_json_output(json) ⇒ Object



263
264
265
266
267
268
269
# File 'lib/mailmanager/lib.rb', line 263

def parse_json_output(json)
  result = JSON.parse(json)
  if result.is_a?(Hash) && !result['error'].nil?
    raise MailmanExecuteError, result['error']
  end
  result
end

#parse_output(mailman_cmd, output) ⇒ Object



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/mailmanager/lib.rb', line 244

def parse_output(mailman_cmd, output)
  case mailman_cmd
  when :rmlist
    return_obj = output =~ /Removing list info/
  when :list_lists
    lists = []
    puts "Output from Mailman:\n#{output}" if MailManager.debug
    output.split("\n").each do |line|
      next if line =~ /^\d+ matching mailing lists found:$/
      /^\s*(.+?)\s+-\s+(.+)$/.match(line) do |m|
        puts "Found list #{m[1]}" if MailManager.debug
        lists << MailManager::List.new(m[1].downcase)
      end
    end
    return_obj = lists
  end
  return_obj
end

#regular_members(list) ⇒ Object



68
69
70
71
72
# File 'lib/mailmanager/lib.rb', line 68

def regular_members(list)
  cmd = :withlist
  out = command(cmd, :name => list.name, :wlcmd => :getRegularMemberKeys)
  parse_json_output(out)
end

#request_email(list) ⇒ Object



148
149
150
151
152
# File 'lib/mailmanager/lib.rb', line 148

def request_email(list)
  cmd = :withlist
  out = command(cmd, :name => list.name, :wlcmd => :GetRequestEmail)
  parse_json_output(out)
end

#run_command(mailman_cmd, stdindata = nil) ⇒ Object



223
224
225
226
227
228
229
230
231
232
233
# File 'lib/mailmanager/lib.rb', line 223

def run_command(mailman_cmd, stdindata=nil)
  output = nil
  process = Open4::popen4(mailman_cmd) do |pid, stdin, stdout, stderr|
    if !stdindata.nil?
      stdin.puts(stdindata)
      stdin.close
    end
    output = stdout.read
  end
  [output, process]
end

#set_description(list, desc) ⇒ Object



166
167
168
169
170
# File 'lib/mailmanager/lib.rb', line 166

def set_description(list, desc)
  cmd = :withlist
  out = command(cmd, :name => list.name, :wlcmd => :description, :arg => desc)
  parse_json_output(out)
end

#set_subject_prefix(list, sp) ⇒ Object



172
173
174
175
176
# File 'lib/mailmanager/lib.rb', line 172

def set_subject_prefix(list, sp)
  cmd = :withlist
  out = command(cmd, :name => list.name, :wlcmd => :subject_prefix, :arg => sp)
  parse_json_output(out)
end

#subject_prefix(list) ⇒ Object



160
161
162
163
164
# File 'lib/mailmanager/lib.rb', line 160

def subject_prefix(list)
  cmd = :withlist
  out = command(cmd, :name => list.name, :wlcmd => :subject_prefix)
  parse_json_output(out)
end

#web_page_url(list) ⇒ Object

TODO: DRY this up!



142
143
144
145
146
# File 'lib/mailmanager/lib.rb', line 142

def web_page_url(list)
  cmd = :withlist
  out = command(cmd, :name => list.name, :wlcmd => 'web_page_url')
  parse_json_output(out)
end