Class: MailManager::Lib

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

Overview

:nodoc:all

Instance Method Summary collapse

Instance Method Details

#add_member(list, member) ⇒ Object



42
43
44
45
46
# File 'lib/mailmanager/lib.rb', line 42

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



75
76
77
78
79
80
81
82
83
# File 'lib/mailmanager/lib.rb', line 75

def add_moderator(list, email)
  if moderators(list)['return'].include?(email)
    return {'result' => 'already_a_moderator'}
  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



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

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



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

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



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/mailmanager/lib.rb', line 106

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.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 :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



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

def create_list(params)
  cmd = :newlist
  out = command(cmd, params)
  parse_output(cmd, out)
end

#delete_member(list, email) ⇒ Object



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

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



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

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

#digest_members(list) ⇒ Object



36
37
38
39
40
# File 'lib/mailmanager/lib.rb', line 36

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

#escape(s) ⇒ Object



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

def escape(s)
  # no idea what this does, stole it from the ruby-git gem
  escaped = s.to_s.gsub('\'', '\'\\\'\'')
  %Q{"#{escaped}"}
end

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



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

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



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

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

#listsObject



12
13
14
15
16
# File 'lib/mailmanager/lib.rb', line 12

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

#mailmanagerObject



8
9
10
# File 'lib/mailmanager/lib.rb', line 8

def mailmanager
  MailManager::Base.instance
end

#moderators(list) ⇒ Object



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

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

#parse_json_output(json) ⇒ Object



192
193
194
195
196
197
198
# File 'lib/mailmanager/lib.rb', line 192

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



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/mailmanager/lib.rb', line 166

def parse_output(mailman_cmd, output)
  case mailman_cmd
  when :newlist
    list_name = nil
    output.split("\n").each do |line|
      if match = /^##\s+(.+?)mailing\s+list\s*$/.match(line)
        list_name = match[1]
      end
    end
    raise "Error getting name of newly created list" if list_name.nil?
    return_obj = MailManager::List.new(list_name)
  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



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

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

#run_command(mailman_cmd, stdindata = nil) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
# File 'lib/mailmanager/lib.rb', line 148

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

#web_page_url(list) ⇒ Object



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

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