mail-sympa 1.2.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/mail/sympa.rb CHANGED
@@ -1,21 +1,4 @@
1
- # http://mislav.uniqpath.com/2011/06/ruby-verbose-mode/
2
- module Kernel
3
- def silence_warnings
4
- with_warnings(nil) { yield }
5
- end
6
-
7
- def with_warnings(flag)
8
- old_verbose, $VERBOSE = $VERBOSE, flag
9
- yield
10
- ensure
11
- $VERBOSE = old_verbose
12
- end
13
- end unless Kernel.respond_to? :silence_warnings
14
-
15
- silence_warnings do
16
- gem 'soap4r-ruby1.9'
17
- require 'soap/rpc/driver'
18
- end
1
+ require 'savon'
19
2
 
20
3
 
21
4
  # The Mail module serves as a namespace only.
@@ -48,30 +31,20 @@ module Mail
48
31
  @endpoint = endpoint.to_s # Allow for URI objects
49
32
  @namespace = namespace
50
33
 
51
- @soap = SOAP::RPC::Driver.new(endpoint, namespace)
34
+ @client = Savon.client(
35
+ wsdl: nil,
36
+ endpoint: @endpoint,
37
+ namespace: @namespace,
38
+ element_form_default: :qualified,
39
+ env_namespace: :soap,
40
+ namespace_identifier: :sym,
41
+ namespaces: { 'xmlns:soapenc' => 'http://schemas.xmlsoap.org/soap/encoding/' },
42
+ log: false, # Set to true for debugging
43
+ pretty_print_xml: false
44
+ )
52
45
 
53
46
  @email = nil
54
- @password = nil
55
47
  @cookie = nil
56
-
57
- @soap.add_method('login', 'email', 'password')
58
-
59
- @soap.add_method(
60
- 'authenticateAndRun',
61
- 'email',
62
- 'cookie',
63
- 'service',
64
- 'parameters'
65
- )
66
-
67
- @soap.add_method(
68
- 'authenticateRemoteAppAndRun',
69
- 'appname',
70
- 'apppassword',
71
- 'vars',
72
- 'service',
73
- 'parameters'
74
- )
75
48
  end
76
49
 
77
50
  # Authenticate with the Sympa server. This method must be called before
@@ -83,9 +56,13 @@ module Mail
83
56
  # sympa.login(email, password)
84
57
  #
85
58
  def login(email, password)
86
- @email = email
87
- @password = password
88
- @cookie = @soap.login(email, password)
59
+ @cookie = nil
60
+ email = email.to_s.strip.downcase
61
+ cookie = soap_call(:login, email: email, password: password)
62
+ raise Error, 'Login did not return a session cookie' unless cookie.is_a?(String) && !cookie.empty?
63
+
64
+ @email = email
65
+ @cookie = cookie
89
66
  end
90
67
 
91
68
  # Returns an array of available mailing lists based on +topic+ and
@@ -104,11 +81,63 @@ module Mail
104
81
  #
105
82
  def lists(topic='', sub_topic='')
106
83
  raise Error, 'must login first' unless @cookie
107
- @soap.authenticateAndRun(@email, @cookie, 'lists', [topic, sub_topic])
84
+ authenticate_and_run('lists', [topic, sub_topic])
108
85
  end
109
86
 
87
+ private
88
+
89
+ # Sympa authenticates RPC calls through its session wrapper. The final
90
+ # argument must be a SOAP-encoded array, including when it has one item.
91
+ def authenticate_and_run(service, parameters)
92
+ soap_call(:authenticateAndRun, {
93
+ email: @email,
94
+ cookie: @cookie,
95
+ service: service
96
+ }.merge(soap_parameters(parameters)))
97
+ end
98
+
99
+ def soap_parameters(parameters)
100
+ {
101
+ parameters: { item: parameters.map { |value| value == true ? '1' : value == false ? '0' : value.to_s } },
102
+ :attributes! => {
103
+ parameters: {
104
+ 'xsi:type' => 'soapenc:Array',
105
+ 'soapenc:arrayType' => "xsd:string[#{parameters.length}]"
106
+ }
107
+ }
108
+ }
109
+ end
110
+
111
+ def soap_call(operation, message)
112
+ response = @client.call(operation, soap_action: "#{@namespace}##{operation}", message: message)
113
+ # Sympa names results by operation (result, listInfo, etc.). Preserve
114
+ # false and empty values rather than falling back to the response XML.
115
+ envelope = response.body.values.first
116
+ result = envelope.reject { |key, _| key.to_s.start_with?('@') }.values.first
117
+ decode_result(result)
118
+ rescue Savon::SOAPFault => error
119
+ detail = error.to_hash.dig(:fault, :detail)
120
+ detail = nil unless detail.is_a?(String) && !detail.empty?
121
+ raise Error, ["SOAP Fault: #{error.message}", detail].compact.join(': ')
122
+ rescue StandardError => error
123
+ raise Error, "SOAP request failed: #{error.message}"
124
+ end
125
+
126
+ def decode_result(result)
127
+ return result unless result.is_a?(Hash)
128
+
129
+ values = result.reject { |key, _| key.to_s.start_with?('@') }
130
+ if result.any? { |key, value| key.to_s.start_with?('@') && value.to_s.end_with?(':Array') }
131
+ values.values.flat_map { |value| value.is_a?(Array) ? value : [value] }.map { |value| decode_result(value) }
132
+ else
133
+ values.transform_values { |value| decode_result(value) }
134
+ end
135
+ end
136
+
137
+ public
138
+
110
139
  # Returns an array of available mailing lists in complex object format,
111
- # i.e. these are SOAP::Mapping objects that you can call methods on.
140
+ # i.e. these are hash objects that you can access like methods.
112
141
  #
113
142
  # Example:
114
143
  #
@@ -116,20 +145,20 @@ module Mail
116
145
  # sympa.login(email, password)
117
146
  #
118
147
  # sympa.complex_lists.each{ |list|
119
- # puts list.subject
120
- # puts list.homepage
148
+ # puts list[:subject] || list['subject']
149
+ # puts list[:homepage] || list['homepage']
121
150
  # }
122
151
  #
123
152
  def complex_lists(topic='', sub_topic='')
124
153
  raise Error, 'must login first' unless @cookie
125
154
  args = [topic, sub_topic]
126
- @soap.authenticateAndRun(@email, @cookie, 'complexLists', args)
155
+ authenticate_and_run('complexLists', args)
127
156
  end
128
157
 
129
158
  alias complexLists complex_lists
130
159
 
131
160
  # Returns a description about the given +list_name+. This is a
132
- # SOAP::Mapping object.
161
+ # hash object.
133
162
  #
134
163
  # Example:
135
164
  #
@@ -138,13 +167,14 @@ module Mail
138
167
  #
139
168
  # info = sympa.info(list)
140
169
  #
141
- # puts info.subject
142
- # puts info.homepage
143
- # puts info.isOwner
170
+ # puts info[:subject] || info['subject']
171
+ # puts info[:homepage] || info['homepage']
172
+ # puts info[:isOwner] || info['isOwner']
144
173
  #
145
174
  def info(list_name)
146
175
  raise Error, 'must login first' unless @cookie
147
- @soap.authenticateAndRun(@email, @cookie, 'info', [list_name])
176
+ result = authenticate_and_run('info', [list_name])
177
+ result.is_a?(Array) ? result.first : result
148
178
  end
149
179
 
150
180
  # Returns an array of members that belong to the given +list_name+.
@@ -158,7 +188,7 @@ module Mail
158
188
  #
159
189
  def review(list_name)
160
190
  raise Error, 'must login first' unless @cookie
161
- @soap.authenticateAndRun(@email, @cookie, 'review', [list_name])
191
+ authenticate_and_run('review', [list_name])
162
192
  end
163
193
 
164
194
  # Returns an array of lists that the +user+ is subscribed to. The +user+
@@ -180,15 +210,15 @@ module Mail
180
210
  #
181
211
  def which(user, app_name, app_passwd)
182
212
  raise Error, 'must login first' unless @cookie
183
- @soap.authenticateRemoteAppAndRun(app_name, app_passwd, user, 'which', [''])
213
+ authenticate_remote_app_and_run(app_name, app_passwd, user, 'which', [''])
184
214
  end
185
215
 
186
- # Same as the Sympa#which method, but returns an array of SOAP::Mapping
187
- # objects that you can call methods on.
216
+ # Same as the Sympa#which method, but returns an array of hash
217
+ # objects that you can access like methods.
188
218
  #
189
219
  def complex_which(user, app_name, app_passwd)
190
220
  raise Error, 'must login first' unless @cookie
191
- @soap.authenticateRemoteAppAndRun(app_name, app_passwd, user, 'complexWhich', [''])
221
+ authenticate_remote_app_and_run(app_name, app_passwd, user, 'complexWhich', [''])
192
222
  end
193
223
 
194
224
  alias complexWhich complex_which
@@ -201,32 +231,28 @@ module Mail
201
231
  raise Error, 'must login first' unless @cookie
202
232
 
203
233
  unless ['editor', 'owner'].include?(function)
204
- raise Error, 'invalid function name "#{editor}"'
234
+ raise Error, "invalid function name \"#{function}\""
205
235
  end
206
236
 
207
- @soap.authenticateAndRun(@email, @cookie, 'amI', [list_name, function, user])
237
+ authenticate_and_run('amI', [list_name, function, user])
208
238
  end
209
239
 
210
240
  alias amI am_i?
211
241
 
212
242
  # Adds the given +email+ to +list_name+ using +name+ (gecos). If +quiet+
213
243
  # is set to true (the default) then no email notification is sent.
214
- #--
215
- # TODO: Determine why this method does not return a boolean.
216
244
  #
217
245
  def add(email, list_name, name, quiet=true)
218
246
  raise Error, 'must login first' unless @cookie
219
- @soap.authenticateAndRun(@email, @cookie, 'add', [list_name, email, name, quiet])
247
+ authenticate_and_run('add', [list_name, email, name, quiet])
220
248
  end
221
249
 
222
250
  # Deletes the given +email+ from +list_name+. If +quiet+ is set to true
223
251
  # (the default) then no email notification is sent.
224
- #--
225
- # TODO: Determine why this method does not return a boolean.
226
252
  #
227
253
  def del(email, list_name, quiet=true)
228
254
  raise Error, 'must login first' unless @cookie
229
- @soap.authenticateAndRun(@email, @cookie, 'del', [list_name, email, quiet])
255
+ authenticate_and_run('del', [list_name, email, quiet])
230
256
  end
231
257
 
232
258
  # Subscribes the currently logged in user to +list_name+. By default the
@@ -234,14 +260,14 @@ module Mail
234
260
  #
235
261
  def subscribe(list_name, name = @email)
236
262
  raise Error, 'must login first' unless @cookie
237
- @soap.authenticateAndRun(@email, @cookie, 'subscribe', [list_name, name])
263
+ authenticate_and_run('subscribe', [list_name, name])
238
264
  end
239
265
 
240
266
  # Removes the currently logged in user from +list_name+.
241
267
  #
242
268
  def signoff(list_name)
243
269
  raise Error, 'must login first' unless @cookie
244
- @soap.authenticateAndRun(@email, @cookie, 'signoff', [list_name, @email])
270
+ authenticate_and_run('signoff', [list_name, @email])
245
271
  end
246
272
 
247
273
  alias delete del
@@ -249,10 +275,11 @@ module Mail
249
275
  alias url endpoint
250
276
 
251
277
  # Creates list +list_name+ with subject +subject+. Returns boolean.
278
+ # The server may require an explicit +topics+ value from its topics.conf.
252
279
  #
253
280
  def create_list(list_name, subject, template='discussion_list', description=' ', topics=' ')
254
281
  raise Error, 'must login first' unless @cookie
255
- @soap.authenticateAndRun(@email, @cookie, 'createList', [list_name, subject, template, description, topics])
282
+ authenticate_and_run('createList', [list_name, subject, template, description, topics])
256
283
  end
257
284
 
258
285
  alias createList create_list
@@ -261,14 +288,19 @@ module Mail
261
288
  #
262
289
  def close_list(list_name)
263
290
  raise Error, 'must login first' unless @cookie
264
- @soap.authenticateAndRun(@email, @cookie, 'closeList', [list_name])
291
+ authenticate_and_run('closeList', [list_name])
265
292
  end
266
293
 
267
294
  alias closeList close_list
268
295
 
269
296
  # Run command in trusted context.
270
297
  def authenticate_remote_app_and_run(app_name, app_password, variables, service, parameters)
271
- @soap.authenticateRemoteAppAndRun( app_name, app_password, variables, service, parameters )
298
+ soap_call(:authenticateRemoteAppAndRun, {
299
+ app_name: app_name,
300
+ app_password: app_password,
301
+ variables: variables,
302
+ service: service
303
+ }.merge(soap_parameters(parameters)))
272
304
  end
273
305
 
274
306
  alias authenticateRemoteAppAndRun authenticate_remote_app_and_run
data/mail-sympa.gemspec CHANGED
@@ -2,22 +2,33 @@ require 'rubygems'
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = 'mail-sympa'
5
- spec.version = '1.2.0'
5
+ spec.version = '1.3.0'
6
6
  spec.authors = ['Daniel J. Berger', 'David Salisbury', 'Mark Sallee']
7
- spec.license = 'Artistic 2.0'
7
+ spec.license = 'Artistic-2.0'
8
8
  spec.description = 'Ruby interface for the Sympa mailing list server'
9
9
  spec.email = 'djberg96@gmail.com'
10
10
  spec.files = Dir['**/*'].reject{ |f| f.include?('git') }
11
- spec.test_files = ['test/test_mail_sympa.rb']
11
+ spec.test_files = Dir['spec/*_spec.rb']
12
12
  spec.homepage = 'http://github.com/djberg96/mail-sympa'
13
+ spec.cert_chain = ['certs/djberg96_pub.pem']
13
14
 
14
- spec.required_ruby_version = '>= 1.9.3'
15
+ # Using Savon for SOAP client functionality (modern, Ruby 3.3+ compatible)
16
+ spec.add_dependency('savon', '~> 2.12')
15
17
 
16
- spec.extra_rdoc_files = ['README', 'CHANGES', 'MANIFEST']
18
+ # Additional dependency for XML handling
19
+ spec.add_dependency('nokogiri', '~> 1.13')
17
20
 
18
- spec.add_dependency('soap4r-ruby1.9', '>= 2.0.0')
19
- spec.add_development_dependency('test-unit', '>= 3.0.0')
20
- spec.add_development_dependency('dbi-dbrc')
21
+ spec.add_development_dependency('rake')
22
+ spec.add_development_dependency('rspec', '~> 3.0')
23
+
24
+ spec.metadata = {
25
+ 'homepage_uri' => 'https://github.com/djberg96/mail-sympa',
26
+ 'bug_tracker_uri' => 'https://github.com/djberg96/mail-sympa/issues',
27
+ 'changelog_uri' => 'https://github.com/djberg96/mail-sympa/blob/main/CHANGES.md',
28
+ 'documentation_uri' => 'https://github.com/djberg96/mail-sympa/wiki',
29
+ 'source_code_uri' => 'https://github.com/djberg96/mail-sympa',
30
+ 'wiki_uri' => 'https://github.com/djberg96/mail-sympa/wiki'
31
+ }
21
32
 
22
33
  spec.summary = <<-EOF
23
34
  The mail-sympa library provides a Ruby interface to the Sympa mailing