Class: MailserviceController

Inherits:
ApplicationController show all
Defined in:
app/controllers/mailservice_controller.rb

Overview

Using sugoi-mail in your own web applications

Sugoi-Mail provides a SOAP (and XMLRPC) API to let you embed it into your own web appplication without using its own interface. (This is handy if, for example, you want to make a Xoops or Drupal module for it.)

To use the web service, first you need to have a Domain account on the server. Let’s say that your domain is “example.com” and the password for example.com is “example”.

You’d start a session with Sugoi-Mail by logging into the domain. I’m going to use SOAP as my example API, with the assumption that you’ve already had wsdl2ruby build a client library for you with.

*MAKE SURE THAT COOKIES ARE ENABLED IN YOUR SOAP CLIENT!*

wsdl2ruby --wsdl http://sugoi-mail-server/wsdl --type client

This being done, you’d connect like this:

client = MailserviceMailservicePort.new
client.domainLogin "example.com", "example"

This is how the web application logs in. Once the application is logged in, then the user (let’s keep the “example” theme going with a username of “example” and a password of “password”) can log in:

client.userLogin "example", "password"

Once the user’s logged in, then your application can call the user_* functions–for example, to retrieve the user’s email address, use the UserEmailAddress call:

emailaddress = client.userEmailAddress  # returns "example@example.com"

If you log in as an “admin” user, then all of the admin_* messages are also available to you.

Instance Method Summary collapse

Instance Method Details

#admin_get_confirmation_code(mailinglist_name, address) ⇒ Object

Returns the confirmation code for a particular combination of mailing list and address.

This one should probably resend the confirmation message.

[View source]

221
222
223
224
225
226
227
228
229
230
# File 'app/controllers/mailservice_controller.rb', line 221

def admin_get_confirmation_code mailinglist_name, address
    user_admin?

    m=Mailinglist.find_by_name(mailinglist_name)
    a=Address.find_by_address(address)
    c=Confirmationcode.find(:first,:conditions => [ 
            'mailinglist_id = ?  and address_id = ?', m.id, a.id 
        ])
    return c.code
end

#admin_mailinglists_allObject

Returns all mailing lists in this domain.

[View source]

233
234
235
236
237
# File 'app/controllers/mailservice_controller.rb', line 233

def admin_mailinglists_all
    user_admin?

    Mailinglist.find_all_by_domain_id session[:domain_id]
end

#admin_user_collectionObject

Returns all users in this domain, as a hash

[View source]

173
174
175
176
177
# File 'app/controllers/mailservice_controller.rb', line 173

def admin_user_collection
    user_admin?
    all_users=User.find_all_by_domain_id session[:domain_id]
    return all_users
end

#admin_user_delete(login, login_confirmation) ⇒ Object

Deletes a user.

[View source]

198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'app/controllers/mailservice_controller.rb', line 198

def admin_user_delete , 
    user_admin?

    if  !=  then
        raise "Login and login confirmation not the same"
    end
    if User.find(session[:user_id]). ==  then
        raise "Trying to delete yourself? Very funny"
    end
      
    u=User. 
    if u then
        User.delete u.id
        return true
    else
        return false
    end
end

#admin_user_listObject

Returns all users in this domain.

[View source]

165
166
167
168
169
170
# File 'app/controllers/mailservice_controller.rb', line 165

def admin_user_list
    user_admin?

    all_users=User.find_all_by_domain_id session[:domain_id]
    return all_users.map { |u| [ u., u.description ] }
end

#admin_user_reset_password(login, password, password_confirmation) ⇒ Object

Resets a user’s password.

[View source]

180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'app/controllers/mailservice_controller.rb', line 180

def admin_user_reset_password , password, password_confirmation
    user_admin?

    user=User. 
    if user then
        user.password=password
        user.password_confirmation=password_confirmation
        if user.save then
            true
        else
            raise user.errors.sort.map { |e| "#{e[0]}: #{e[1]}" }.join("\n")
        end
    else
        raise "user: user not found"
    end
end

#admin_user_signup(login, description, password, password_confirmation, mailinglist_admin, domain_admin) ⇒ Object

Creates a new user. NOTE: This method requires that you’re already signed in as an administrator.

[View source]

139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'app/controllers/mailservice_controller.rb', line 139

def  , description, password, password_confirmation, 
                      mailinglist_admin, domain_admin
    user_admin?

    user=User.new

    user.=
    user.domain_id=session[:domain_id]
    user.password=password
    user.password_confirmation=password_confirmation
    user.mailinglistadmin = mailinglist_admin
    user.domainadmin = domain_admin
    if user.save then
        #this is a disgusting hack
        user.password = password
        user.password_confirmation = password_confirmation
        user.save
        user.mailinglist.description = description
        user.mailinglist.save
        return true
    else
        raise user.errors.sort.map { |e| "#{e[0]}: #{e[1]}" }.join("\n")
    end
end

#domain_logged_inObject

Verify whether the domain is logged in or not.

[View source]

119
120
121
# File 'app/controllers/mailservice_controller.rb', line 119

def domain_logged_in
    not session[:domain_id].nil?
end

#domain_login(domainname, password) ⇒ Object

Log into the domain.

[View source]

100
101
102
103
104
105
106
# File 'app/controllers/mailservice_controller.rb', line 100

def  domainname, password
    domain = Domain.authenticate(domainname, password)
    if domain
        session[:domain_id]=Domain.authenticate(domainname, password).id
    end
    domain_logged_in
end

#domain_logoutObject

Log out of the domain.

[View source]

109
110
111
112
113
114
115
116
# File 'app/controllers/mailservice_controller.rb', line 109

def domain_logout
    if session[:domain_id] then
       session[:domain_id] = nil
       true
    else
        false
    end
end

#domain_nameObject

Returns the domain name.

[View source]

124
125
126
# File 'app/controllers/mailservice_controller.rb', line 124

def domain_name
    Domain.find(session[:domain_id]).name
end

#is_adminObject

Returns true if the user is an administrator and false otherwise

[View source]

133
134
135
# File 'app/controllers/mailservice_controller.rb', line 133

def is_admin
    user_admin? rescue false
end

#mailinglist_address(id) ⇒ Object

Returns the email address of the mailing list with id id

[View source]

417
418
419
# File 'app/controllers/mailservice_controller.rb', line 417

def mailinglist_address id
    my_mailing_list(id).address
end

#mailinglist_class_get_attributes(mlclass) ⇒ Object

Returns the attributes of the mailing list class mlclass. Use mailinglist_classes to retrieve a list of mailing list class names.

[View source]

407
408
409
# File 'app/controllers/mailservice_controller.rb', line 407

def mailinglist_class_get_attributes(mlclass)
    MailinglistClass.find_by_name mlclass
end

#mailinglist_classesObject

Returns a list of the names of all the mailing list classes.

[View source]

398
399
400
401
402
# File 'app/controllers/mailservice_controller.rb', line 398

def mailinglist_classes
    # the first mailing list class is special (it's reserved for
    # forwarding addresses).
    MailinglistClass.find(:all, :conditions => "id > 1").map { |mlc| mlc.name }
end

#mailinglist_confirm(mailinglist_name, address, code) ⇒ Object

Confirms a subscription to a mailing list. Returns false if the confirmation code was incorrect.

[View source]

467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
# File 'app/controllers/mailservice_controller.rb', line 467

def mailinglist_confirm mailinglist_name, address, code
    addr = Address.find_by_address address
    mailinglist = Mailinglist.find_by_name mailinglist_name

    return false if addr == nil or mailinglist == nil

    # This redundant-looking if is to ensure that it returns only
    # "true" or "false", and not a not-true-but-still-evaluated-as-
    # truth value like a mailing list, or a not-false such as nil.
    if mailinglist.confirm(addr, code)
        true
    else
        false
    end
end

#mailinglist_create(mailinglist_name, mailinglist_class) ⇒ Object

Creates a mailing list of type mailinglist_class. Retrieve the list of valid mailing list classes with mailinglist_classes.

[View source]

383
384
385
386
387
388
389
390
391
392
393
394
395
# File 'app/controllers/mailservice_controller.rb', line 383

def mailinglist_create mailinglist_name, mailinglist_class
    user_logged_in?

    mailinglist_class_id = MailinglistClass.find_by_name mailinglist_class
    unless mailinglist_class_id
        raise "Class does not exist (check MailinglistClasses for list)"
    end

    m=Mailinglist.new(:name => mailinglist_name,
                      :mailinglist_class_id => mailinglist_class_id.id,
                      :user_id => session[:user_id])
    m.save
end

#mailinglist_delete(mailinglist_id) ⇒ Object

Deletes a mailing list if you’re allowed to do that.

[View source]

432
433
434
435
436
437
438
# File 'app/controllers/mailservice_controller.rb', line 432

def mailinglist_delete mailinglist_id
    m=my_mailing_list(mailinglist_id) 

    if m.destroy
        true
    end
end

#mailinglist_find_by_name(name) ⇒ Object

Returns the id of the mailing list with name name. The converse of mailinglist_name

[View source]

423
424
425
426
427
428
429
# File 'app/controllers/mailservice_controller.rb', line 423

def mailinglist_find_by_name name
    user_logged_in?

    m=Mailinglist.find_by_address \
        "#{name}@#{Domain.find(session[:domain_id]).name}"
    if m then m[0].id end
end

#mailinglist_name(id) ⇒ Object

Returns the name of the mailing list with id id

[View source]

412
413
414
# File 'app/controllers/mailservice_controller.rb', line 412

def mailinglist_name id
    my_mailing_list(id).name
end

#mailinglist_pending(mailinglist_id) ⇒ Object

Returns a list of non-confirmed addresses on a mailing list.

[View source]

449
450
451
452
453
454
# File 'app/controllers/mailservice_controller.rb', line 449

def mailinglist_pending mailinglist_id
    mailinglist=my_mailing_list(mailinglist_id)
    mailinglist.pending_addresses.map do |addr|
        addr.address
    end
end

#mailinglist_subscribe(mailinglist_id, address) ⇒ Object

Adds an address to a mailing list. If the mailing list requires confirmation, then the confirmation code will be emailed to address.

[View source]

459
460
461
462
463
# File 'app/controllers/mailservice_controller.rb', line 459

def mailinglist_subscribe mailinglist_id, address
    if my_mailing_list(mailinglist_id).subscribe(address)
        true
    end
end

#mailinglist_subscribers(mailinglist_id) ⇒ Object

Returns a list of addresses on a mailing list.

[View source]

441
442
443
444
445
446
# File 'app/controllers/mailservice_controller.rb', line 441

def mailinglist_subscribers mailinglist_id
    mailinglist=my_mailing_list(mailinglist_id)
    mailinglist.confirmed_addresses.each do |addr|
        addr.address
    end
end

#mailinglist_unsubscribe(mailinglist_id, address) ⇒ Object

Removes an address from a mailing list.

[View source]

484
485
486
487
488
# File 'app/controllers/mailservice_controller.rb', line 484

def mailinglist_unsubscribe mailinglist_id, address
    if my_mailing_list(mailinglist_id).unsubscribe(address)
        true
    end
end

#mailinglist_unsubscribe_quiet(mailinglist_id, address) ⇒ Object

Removes an address from a mailing list without sending the farewell message

[View source]

492
493
494
495
496
# File 'app/controllers/mailservice_controller.rb', line 492

def mailinglist_unsubscribe_quiet mailinglist_id, address
    if my_mailing_list(mailinglist_id).unsubscribe(address, false)
        true
    end
end

#pingObject

Use this to ensure that the SOAP connection is still alive and responding. This and domain_logged_in are the only API methods that don’t require any kind of authentication.

[View source]

91
92
93
# File 'app/controllers/mailservice_controller.rb', line 91

def ping
    true
end

#user_change_password(old_password, password, password_confirmation) ⇒ Object

Lets the user change his password.

[View source]

316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'app/controllers/mailservice_controller.rb', line 316

def user_change_password(old_password, password, password_confirmation)
    user_logged_in?
    user=User.find(session[:user_id])
    if User.authenticate(user., old_password) then
        user.password=password
        user.password_confirmation=password_confirmation
        if(user.save)
            return true
        else
            errstr = user.errors.sort.map do |fac,err| 
                "#{fac}: #{err}" 
            end.join("\n")

            raise RuntimeError, errstr
        end
    else
        raise "auth: original password incorrect"
    end
end

#user_change_real_name(password, new_name) ⇒ Object

Allows the user to change his real name.

[View source]

344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# File 'app/controllers/mailservice_controller.rb', line 344

def user_change_real_name password, new_name
    user_logged_in?
    user=User.find(session[:user_id])

    if User.authenticate(user., password) then
        ml = user.mailinglist
        ml.description=new_name
        if ml.save
            return new_name
        else
            errstr = ml.errors.sort.map do |fac, err|
                "#{fac}: #{err}"
            end.join("\n")

            raise RuntimeError, errstr
        end
    else
        raise "auth: password incorrect"
    end
end

#user_email_addressObject

Returns the user’s email address.

[View source]

276
277
278
279
# File 'app/controllers/mailservice_controller.rb', line 276

def user_email_address
    user_logged_in?
    User.find(session[:user_id]).address
end

#user_email_address_confirm(address, code) ⇒ Object

Allows the user to confirm his email address on the web instead of by email.

[View source]

304
305
306
307
308
309
310
311
312
313
# File 'app/controllers/mailservice_controller.rb', line 304

def user_email_address_confirm(address,code)
    user_logged_in?
    address_obj=Address.find_by_address(address)
    ml=User.find(session[:user_id]).mailinglist
    if ml.confirm(address_obj,code) then 
        ml.save
    else
        false
    end
end

#user_email_addressesObject

Returns all addresses belonging to this user.

[View source]

282
283
284
285
# File 'app/controllers/mailservice_controller.rb', line 282

def user_email_addresses
    user_logged_in?
    User.find(session[:user_id]).addresses.map { |a| a.address }
end

#user_email_addresses_add(address) ⇒ Object

Adds a new email address to this user.

[View source]

366
367
368
369
370
371
372
373
# File 'app/controllers/mailservice_controller.rb', line 366

def user_email_addresses_add(address)
    user_logged_in?
    user = User.find(session[:user_id])
    if user.mailinglist.subscribe address then
        user.mailinglist.save
    end
    user.mailinglist.addresses
end

#user_email_addresses_confirmedObject

Returns all confirmed email addresses belonging to this user

[View source]

288
289
290
291
292
# File 'app/controllers/mailservice_controller.rb', line 288

def user_email_addresses_confirmed
    user_logged_in?
    User.find(session[:user_id]).mailinglist .
        confirmed_addresses.map { |a| a.address }
end

#user_email_addresses_remove(address) ⇒ Object

Removes an email address from the user’s email address list.

[View source]

376
377
378
379
# File 'app/controllers/mailservice_controller.rb', line 376

def user_email_addresses_remove(address)
    user_logged_in?
    User.find(session[:user_id]).mailinglist.unsubscribe address
end

#user_email_addresses_unconfirmedObject

Returns all yet-to-be-confirmed email addresses belong to this user.

[View source]

296
297
298
299
300
# File 'app/controllers/mailservice_controller.rb', line 296

def user_email_addresses_unconfirmed
    user_logged_in?
    User.find(session[:user_id]).mailinglist .
        pending_addresses.map { |a| a.address }
end

#user_logged_inObject

Returns whether the user is logged in or not.

[View source]

263
# File 'app/controllers/mailservice_controller.rb', line 263

def user_logged_in; not session[:user_id].nil?; end

#user_login(username, password) ⇒ Object

Logs the user in. If the user logs in successfully, returns true, otherwise false.

[View source]

245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'app/controllers/mailservice_controller.rb', line 245

def  username, password
    domain_logged_in?

    session[:user_id] = nil

    user = User.authenticate username, password

    if user
        session[:user_id]=user.id
    end

    user_logged_in
end

#user_logoutObject

Logs the user out.

[View source]

260
# File 'app/controllers/mailservice_controller.rb', line 260

def user_logout; session[:user_id] = nil; end

#user_mailinglistsObject

Returns all the mailing lists that belong to this user.

[View source]

269
270
271
272
273
# File 'app/controllers/mailservice_controller.rb', line 269

def user_mailinglists
    user_logged_in?
    Mailinglist.find_all_by_user_id(session[:user_id]) -
        [ User.find(session[:user_id]).mailinglist ]
end

#user_nameObject

Returns the user’s username.

[View source]

266
# File 'app/controllers/mailservice_controller.rb', line 266

def user_name; User.find(session[:user_id]).; end

#user_real_nameObject

Returns the user’s real name.

[View source]

337
338
339
340
341
# File 'app/controllers/mailservice_controller.rb', line 337

def user_real_name
    user_logged_in?

    User.find(session[:user_id]).description
end