mail-sympa 1.1.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,5 +1,5 @@
1
- gem 'soap4r-ruby1.9' if RUBY_VERSION > '1.9'
2
- require 'soap/rpc/driver'
1
+ require 'savon'
2
+
3
3
 
4
4
  # The Mail module serves as a namespace only.
5
5
  module Mail
@@ -9,7 +9,7 @@ module Mail
9
9
  class Error < StandardError; end
10
10
 
11
11
  # The version of the mail-sympa library.
12
- VERSION = '1.1.0'
12
+ VERSION = '1.2.0'
13
13
 
14
14
  # The session cookie returned by the login method.
15
15
  attr_reader :cookie
@@ -31,30 +31,20 @@ module Mail
31
31
  @endpoint = endpoint.to_s # Allow for URI objects
32
32
  @namespace = namespace
33
33
 
34
- @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
+ )
35
45
 
36
46
  @email = nil
37
- @password = nil
38
47
  @cookie = nil
39
-
40
- @soap.add_method('login', 'email', 'password')
41
-
42
- @soap.add_method(
43
- 'authenticateAndRun',
44
- 'email',
45
- 'cookie',
46
- 'service',
47
- 'parameters'
48
- )
49
-
50
- @soap.add_method(
51
- 'authenticateRemoteAppAndRun',
52
- 'appname',
53
- 'apppassword',
54
- 'vars',
55
- 'service',
56
- 'parameters'
57
- )
58
48
  end
59
49
 
60
50
  # Authenticate with the Sympa server. This method must be called before
@@ -66,9 +56,13 @@ module Mail
66
56
  # sympa.login(email, password)
67
57
  #
68
58
  def login(email, password)
69
- @email = email
70
- @password = password
71
- @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
72
66
  end
73
67
 
74
68
  # Returns an array of available mailing lists based on +topic+ and
@@ -87,11 +81,63 @@ module Mail
87
81
  #
88
82
  def lists(topic='', sub_topic='')
89
83
  raise Error, 'must login first' unless @cookie
90
- @soap.authenticateAndRun(@email, @cookie, 'lists', [topic, sub_topic])
84
+ authenticate_and_run('lists', [topic, sub_topic])
85
+ end
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}"
91
124
  end
92
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
+
93
139
  # Returns an array of available mailing lists in complex object format,
94
- # 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.
95
141
  #
96
142
  # Example:
97
143
  #
@@ -99,20 +145,20 @@ module Mail
99
145
  # sympa.login(email, password)
100
146
  #
101
147
  # sympa.complex_lists.each{ |list|
102
- # puts list.subject
103
- # puts list.homepage
148
+ # puts list[:subject] || list['subject']
149
+ # puts list[:homepage] || list['homepage']
104
150
  # }
105
151
  #
106
152
  def complex_lists(topic='', sub_topic='')
107
153
  raise Error, 'must login first' unless @cookie
108
154
  args = [topic, sub_topic]
109
- @soap.authenticateAndRun(@email, @cookie, 'complexLists', args)
155
+ authenticate_and_run('complexLists', args)
110
156
  end
111
157
 
112
158
  alias complexLists complex_lists
113
159
 
114
160
  # Returns a description about the given +list_name+. This is a
115
- # SOAP::Mapping object.
161
+ # hash object.
116
162
  #
117
163
  # Example:
118
164
  #
@@ -121,13 +167,14 @@ module Mail
121
167
  #
122
168
  # info = sympa.info(list)
123
169
  #
124
- # puts info.subject
125
- # puts info.homepage
126
- # puts info.isOwner
170
+ # puts info[:subject] || info['subject']
171
+ # puts info[:homepage] || info['homepage']
172
+ # puts info[:isOwner] || info['isOwner']
127
173
  #
128
174
  def info(list_name)
129
175
  raise Error, 'must login first' unless @cookie
130
- @soap.authenticateAndRun(@email, @cookie, 'info', [list_name])
176
+ result = authenticate_and_run('info', [list_name])
177
+ result.is_a?(Array) ? result.first : result
131
178
  end
132
179
 
133
180
  # Returns an array of members that belong to the given +list_name+.
@@ -141,7 +188,7 @@ module Mail
141
188
  #
142
189
  def review(list_name)
143
190
  raise Error, 'must login first' unless @cookie
144
- @soap.authenticateAndRun(@email, @cookie, 'review', [list_name])
191
+ authenticate_and_run('review', [list_name])
145
192
  end
146
193
 
147
194
  # Returns an array of lists that the +user+ is subscribed to. The +user+
@@ -163,15 +210,15 @@ module Mail
163
210
  #
164
211
  def which(user, app_name, app_passwd)
165
212
  raise Error, 'must login first' unless @cookie
166
- @soap.authenticateRemoteAppAndRun(app_name, app_passwd, user, 'which', [''])
213
+ authenticate_remote_app_and_run(app_name, app_passwd, user, 'which', [''])
167
214
  end
168
215
 
169
- # Same as the Sympa#which method, but returns an array of SOAP::Mapping
170
- # 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.
171
218
  #
172
219
  def complex_which(user, app_name, app_passwd)
173
220
  raise Error, 'must login first' unless @cookie
174
- @soap.authenticateRemoteAppAndRun(app_name, app_passwd, user, 'complexWhich', [''])
221
+ authenticate_remote_app_and_run(app_name, app_passwd, user, 'complexWhich', [''])
175
222
  end
176
223
 
177
224
  alias complexWhich complex_which
@@ -184,32 +231,28 @@ module Mail
184
231
  raise Error, 'must login first' unless @cookie
185
232
 
186
233
  unless ['editor', 'owner'].include?(function)
187
- raise Error, 'invalid function name "#{editor}"'
234
+ raise Error, "invalid function name \"#{function}\""
188
235
  end
189
236
 
190
- @soap.authenticateAndRun(@email, @cookie, 'amI', [list_name, function, user])
237
+ authenticate_and_run('amI', [list_name, function, user])
191
238
  end
192
239
 
193
240
  alias amI am_i?
194
241
 
195
242
  # Adds the given +email+ to +list_name+ using +name+ (gecos). If +quiet+
196
243
  # is set to true (the default) then no email notification is sent.
197
- #--
198
- # TODO: Determine why this method does not return a boolean.
199
244
  #
200
245
  def add(email, list_name, name, quiet=true)
201
246
  raise Error, 'must login first' unless @cookie
202
- @soap.authenticateAndRun(@email, @cookie, 'add', [list_name, email, name, quiet])
247
+ authenticate_and_run('add', [list_name, email, name, quiet])
203
248
  end
204
249
 
205
250
  # Deletes the given +email+ from +list_name+. If +quiet+ is set to true
206
251
  # (the default) then no email notification is sent.
207
- #--
208
- # TODO: Determine why this method does not return a boolean.
209
252
  #
210
253
  def del(email, list_name, quiet=true)
211
254
  raise Error, 'must login first' unless @cookie
212
- @soap.authenticateAndRun(@email, @cookie, 'del', [list_name, email, quiet])
255
+ authenticate_and_run('del', [list_name, email, quiet])
213
256
  end
214
257
 
215
258
  # Subscribes the currently logged in user to +list_name+. By default the
@@ -217,14 +260,14 @@ module Mail
217
260
  #
218
261
  def subscribe(list_name, name = @email)
219
262
  raise Error, 'must login first' unless @cookie
220
- @soap.authenticateAndRun(@email, @cookie, 'subscribe', [list_name, name])
263
+ authenticate_and_run('subscribe', [list_name, name])
221
264
  end
222
265
 
223
266
  # Removes the currently logged in user from +list_name+.
224
267
  #
225
268
  def signoff(list_name)
226
269
  raise Error, 'must login first' unless @cookie
227
- @soap.authenticateAndRun(@email, @cookie, 'signoff', [list_name, @email])
270
+ authenticate_and_run('signoff', [list_name, @email])
228
271
  end
229
272
 
230
273
  alias delete del
@@ -232,10 +275,11 @@ module Mail
232
275
  alias url endpoint
233
276
 
234
277
  # Creates list +list_name+ with subject +subject+. Returns boolean.
278
+ # The server may require an explicit +topics+ value from its topics.conf.
235
279
  #
236
280
  def create_list(list_name, subject, template='discussion_list', description=' ', topics=' ')
237
281
  raise Error, 'must login first' unless @cookie
238
- @soap.authenticateAndRun(@email, @cookie, 'createList', [list_name, subject, template, description, topics])
282
+ authenticate_and_run('createList', [list_name, subject, template, description, topics])
239
283
  end
240
284
 
241
285
  alias createList create_list
@@ -244,14 +288,19 @@ module Mail
244
288
  #
245
289
  def close_list(list_name)
246
290
  raise Error, 'must login first' unless @cookie
247
- @soap.authenticateAndRun(@email, @cookie, 'closeList', [list_name])
291
+ authenticate_and_run('closeList', [list_name])
248
292
  end
249
293
 
250
294
  alias closeList close_list
251
295
 
252
296
  # Run command in trusted context.
253
297
  def authenticate_remote_app_and_run(app_name, app_password, variables, service, parameters)
254
- @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)))
255
304
  end
256
305
 
257
306
  alias authenticateRemoteAppAndRun authenticate_remote_app_and_run
data/lib/mail-sympa.rb ADDED
@@ -0,0 +1 @@
1
+ require_relative 'mail/sympa'
data/mail-sympa.gemspec CHANGED
@@ -1,21 +1,36 @@
1
1
  require 'rubygems'
2
2
 
3
- Gem::Specification.new do |gem|
4
- gem.name = 'mail-sympa'
5
- gem.version = '1.1.0'
6
- gem.authors = ['Daniel J. Berger', 'David Salisbury', 'Mark Sallee']
7
- gem.license = 'Artistic 2.0'
8
- gem.description = 'Ruby interface for the Sympa mailing list server'
9
- gem.email = 'djberg96@gmail.com'
10
- gem.files = Dir['**/*'].reject{ |f| f.include?('git') }
11
- gem.test_files = ['test/test_mail_sympa.rb']
12
- gem.homepage = 'http://github.com/djberg96/mail-sympa'
3
+ Gem::Specification.new do |spec|
4
+ spec.name = 'mail-sympa'
5
+ spec.version = '1.3.0'
6
+ spec.authors = ['Daniel J. Berger', 'David Salisbury', 'Mark Sallee']
7
+ spec.license = 'Artistic-2.0'
8
+ spec.description = 'Ruby interface for the Sympa mailing list server'
9
+ spec.email = 'djberg96@gmail.com'
10
+ spec.files = Dir['**/*'].reject{ |f| f.include?('git') }
11
+ spec.test_files = Dir['spec/*_spec.rb']
12
+ spec.homepage = 'http://github.com/djberg96/mail-sympa'
13
+ spec.cert_chain = ['certs/djberg96_pub.pem']
13
14
 
14
- gem.extra_rdoc_files = ['README', 'CHANGES', 'MANIFEST']
15
+ # Using Savon for SOAP client functionality (modern, Ruby 3.3+ compatible)
16
+ spec.add_dependency('savon', '~> 2.12')
15
17
 
16
- gem.add_dependency('soap4r', '>= 1.5.8')
18
+ # Additional dependency for XML handling
19
+ spec.add_dependency('nokogiri', '~> 1.13')
17
20
 
18
- gem.summary = <<-EOF
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
+ }
32
+
33
+ spec.summary = <<-EOF
19
34
  The mail-sympa library provides a Ruby interface to the Sympa mailing
20
35
  list server software. This is a convenient and pretty wrapper for the
21
36
  various SOAP functions that Sympa server publishes.