Class: MailserviceController
- Inherits:
-
ApplicationController
- Object
- ActionController::Base
- ApplicationController
- MailserviceController
- 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 "[email protected]"
If you log in as an “admin” user, then all of the admin_* messages are also available to you.
Instance Method Summary collapse
-
#admin_get_confirmation_code(mailinglist_name, address) ⇒ Object
Returns the confirmation code for a particular combination of mailing list and address.
-
#admin_mailinglists_all ⇒ Object
Returns all mailing lists in this domain.
-
#admin_user_delete(login, login_confirmation) ⇒ Object
Deletes a user.
-
#admin_user_list ⇒ Object
Returns all users in this domain.
-
#admin_user_reset_password(login, password, password_confirmation) ⇒ Object
Resets a user’s password.
-
#admin_user_signup(login, password, password_confirmation) ⇒ Object
Creates a new user.
-
#domain_logged_in ⇒ Object
Verify whether the domain is logged in or not.
-
#domain_login(domainname, password) ⇒ Object
Log into the domain.
-
#domain_logout ⇒ Object
Log out of the domain.
-
#domain_name ⇒ Object
Returns the domain name.
-
#is_admin ⇒ Object
Returns true if the user is an administrator and false otherwise.
-
#mailinglist_address(id) ⇒ Object
Returns the email address of the mailing list with id
id
. -
#mailinglist_class_get_attributes(mlclass) ⇒ Object
Returns the attributes of the mailing list class
mlclass
. -
#mailinglist_classes ⇒ Object
Returns a list of the names of all the mailing list classes.
-
#mailinglist_confirm(mailinglist_name, address, code) ⇒ Object
Confirms a subscription to a mailing list.
-
#mailinglist_create(mailinglist_name, mailinglist_class) ⇒ Object
Creates a mailing list of type
mailinglist_class
. -
#mailinglist_delete(mailinglist_id) ⇒ Object
Deletes a mailing list if you’re allowed to do that.
-
#mailinglist_find_by_name(name) ⇒ Object
Returns the id of the mailing list with name
name
. -
#mailinglist_messages(mailinglist_id) ⇒ Object
Returns the IDs of all the messages messages sent to this mailing list.
-
#mailinglist_name(id) ⇒ Object
Returns the name of the mailing list with id
id
. -
#mailinglist_pending(mailinglist_id) ⇒ Object
Returns a list of non-confirmed addresses on a mailing list.
-
#mailinglist_subscribe(mailinglist_id, address) ⇒ Object
Adds an address to a mailing list.
-
#mailinglist_subscribers(mailinglist_id) ⇒ Object
Returns a list of addresses on a mailing list.
-
#mailinglist_unsubscribe(mailinglist_id, address) ⇒ Object
Removes an address from a mailing list.
-
#message(message_id) ⇒ Object
Returns the message with the id
id
. -
#message_parent(message_id) ⇒ Object
Return the message that this one’s a response to.
-
#message_responses(message_id) ⇒ Object
Returns all responses to this message.
-
#ping ⇒ Object
Use this to ensure that the SOAP connection is still alive and responding.
-
#user_change_password(old_password, password, password_confirmation) ⇒ Object
Lets the user change his password.
-
#user_change_real_name(password, new_name) ⇒ Object
Allows the user to change his real name.
-
#user_email_address ⇒ Object
Returns the user’s email address.
-
#user_email_address_confirm(address, code) ⇒ Object
Allows the user to confirm his email address on the web instead of by email.
-
#user_email_addresses ⇒ Object
Returns all addresses belonging to this user.
-
#user_email_addresses_add(address) ⇒ Object
Adds a new email address to this user.
-
#user_email_addresses_confirmed ⇒ Object
Returns all confirmed email addresses belonging to this user.
-
#user_email_addresses_remove(address) ⇒ Object
Removes an email address from the user’s email address list.
-
#user_email_addresses_unconfirmed ⇒ Object
Returns all yet-to-be-confirmed email addresses belong to this user.
-
#user_logged_in ⇒ Object
Returns whether the user is logged in or not.
-
#user_login(username, password) ⇒ Object
Logs the user in.
-
#user_logout ⇒ Object
Logs the user out.
-
#user_mailinglists ⇒ Object
Returns all the mailing lists that belong to this user.
-
#user_name ⇒ Object
Returns the user’s username.
-
#user_real_name ⇒ Object
Returns the user’s real name.
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.
200 201 202 203 204 205 206 207 208 209 |
# File 'app/controllers/mailservice_controller.rb', line 200 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_all ⇒ Object
Returns all mailing lists in this domain.
212 213 214 215 216 |
# File 'app/controllers/mailservice_controller.rb', line 212 def admin_mailinglists_all user_admin? Mailinglist.find_by_domain(@session[:domain]).map { |ml| ml.id } end |
#admin_user_delete(login, login_confirmation) ⇒ Object
Deletes a user.
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'app/controllers/mailservice_controller.rb', line 180 def admin_user_delete login, login_confirmation user_admin? if login_confirmation != login then raise "Login and login confirmation not the same" end u=User.find_by_login login if u then u.destroy return true else return false end end |
#admin_user_list ⇒ Object
Returns all users in this domain.
154 155 156 157 158 159 |
# File 'app/controllers/mailservice_controller.rb', line 154 def admin_user_list user_admin? all_users=User.find_all_by_domain_id @session[:domain].id return all_users.map { |u| [ u.login, u.description ] } end |
#admin_user_reset_password(login, password, password_confirmation) ⇒ Object
Resets a user’s password.
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'app/controllers/mailservice_controller.rb', line 162 def admin_user_reset_password login, password, password_confirmation user_admin? user=User.find_by_login login 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, password, password_confirmation) ⇒ Object
Creates a new user. NOTE: This method requires that you’re already signed in as an administrator.
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'app/controllers/mailservice_controller.rb', line 133 def admin_user_signup login, password, password_confirmation user_admin? user=User.new if user then user.login=login user.domain=@session[:domain] 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 return false end end |
#domain_logged_in ⇒ Object
Verify whether the domain is logged in or not.
115 116 117 |
# File 'app/controllers/mailservice_controller.rb', line 115 def domain_logged_in not @session[:domain].nil? end |
#domain_login(domainname, password) ⇒ Object
Log into the domain.
99 100 101 102 |
# File 'app/controllers/mailservice_controller.rb', line 99 def domain_login domainname, password @session[:domain]=Domain.authenticate domainname, password domain_logged_in end |
#domain_logout ⇒ Object
Log out of the domain.
105 106 107 108 109 110 111 112 |
# File 'app/controllers/mailservice_controller.rb', line 105 def domain_logout if @session[:domain] then @session[:domain] = nil true else false end end |
#domain_name ⇒ Object
Returns the domain name.
120 121 122 |
# File 'app/controllers/mailservice_controller.rb', line 120 def domain_name @session[:domain].name end |
#is_admin ⇒ Object
Returns true if the user is an administrator and false otherwise
127 128 129 |
# File 'app/controllers/mailservice_controller.rb', line 127 def is_admin user_admin? rescue false end |
#mailinglist_address(id) ⇒ Object
Returns the email address of the mailing list with id id
392 393 394 |
# File 'app/controllers/mailservice_controller.rb', line 392 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.
382 383 384 |
# File 'app/controllers/mailservice_controller.rb', line 382 def mailinglist_class_get_attributes(mlclass) MailinglistClass.find_by_name mlclass end |
#mailinglist_classes ⇒ Object
Returns a list of the names of all the mailing list classes.
373 374 375 376 377 |
# File 'app/controllers/mailservice_controller.rb', line 373 def mailinglist_classes # the first mailing list class is special (it's reserved for # forwarding addresses). MailinglistClass.find_all("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.
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 |
# File 'app/controllers/mailservice_controller.rb', line 445 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.
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 |
# File 'app/controllers/mailservice_controller.rb', line 350 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, :user => @session[:user]) if m then if m.save then return m.address else nil end else nil end end |
#mailinglist_delete(mailinglist_id) ⇒ Object
Deletes a mailing list if you’re allowed to do that.
406 407 408 409 410 411 412 413 414 |
# File 'app/controllers/mailservice_controller.rb', line 406 def mailinglist_delete mailinglist_id m=my_mailing_list(mailinglist_id) if m.destroy true else false end end |
#mailinglist_find_by_name(name) ⇒ Object
Returns the id of the mailing list with name name
. The converse of mailinglist_name
398 399 400 401 402 403 |
# File 'app/controllers/mailservice_controller.rb', line 398 def mailinglist_find_by_name name user_logged_in? m=Mailinglist.find_by_address("#{name}@#{@session[:domain].name}") if m then m[0].id end end |
#mailinglist_messages(mailinglist_id) ⇒ Object
Returns the IDs of all the messages messages sent to this mailing list.
This should probably do something more useful like return all new messages since some datestamp or something.
475 476 477 |
# File 'app/controllers/mailservice_controller.rb', line 475 def mailinglist_id my_mailing_list(mailinglist_id). end |
#mailinglist_name(id) ⇒ Object
Returns the name of the mailing list with id id
387 388 389 |
# File 'app/controllers/mailservice_controller.rb', line 387 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.
425 426 427 428 429 430 |
# File 'app/controllers/mailservice_controller.rb', line 425 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
.
435 436 437 438 439 440 441 |
# File 'app/controllers/mailservice_controller.rb', line 435 def mailinglist_subscribe mailinglist_id, address if my_mailing_list(mailinglist_id).subscribe(address) true else false end end |
#mailinglist_subscribers(mailinglist_id) ⇒ Object
Returns a list of addresses on a mailing list.
417 418 419 420 421 422 |
# File 'app/controllers/mailservice_controller.rb', line 417 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.
462 463 464 465 466 467 468 |
# File 'app/controllers/mailservice_controller.rb', line 462 def mailinglist_unsubscribe mailinglist_id, address if my_mailing_list(mailinglist_id).unsubscribe(address) true else false end end |
#message(message_id) ⇒ Object
Returns the message with the id id
.
484 485 486 |
# File 'app/controllers/mailservice_controller.rb', line 484 def (id) end |
#message_parent(message_id) ⇒ Object
Return the message that this one’s a response to.
489 490 491 |
# File 'app/controllers/mailservice_controller.rb', line 489 def (id).parent end |
#message_responses(message_id) ⇒ Object
Returns all responses to this message.
494 495 496 |
# File 'app/controllers/mailservice_controller.rb', line 494 def (id).children.map { |m| m.id } end |
#ping ⇒ Object
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.
90 91 92 |
# File 'app/controllers/mailservice_controller.rb', line 90 def ping true end |
#user_change_password(old_password, password, password_confirmation) ⇒ Object
Lets the user change his password.
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
# File 'app/controllers/mailservice_controller.rb', line 285 def user_change_password(old_password, password, password_confirmation) user_logged_in? user=@session[:user] if User.authenticate(user.login, 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.
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 |
# File 'app/controllers/mailservice_controller.rb', line 313 def user_change_real_name password, new_name user_logged_in? user=@session[:user] if User.authenticate(user.login, password) then user.description=new_name if user.save return new_name else errstr = user.errors.sort.map do |fac, err| "#{fac}: #{err}" end.join("\n") raise RuntimeError, errstr end else raise "auth: password incorrect" end end |
#user_email_address ⇒ Object
Returns the user’s email address.
247 248 249 250 |
# File 'app/controllers/mailservice_controller.rb', line 247 def user_email_address user_logged_in? @session[:user].address end |
#user_email_address_confirm(address, code) ⇒ Object
Allows the user to confirm his email address on the web instead of by email.
273 274 275 276 277 278 279 280 281 282 |
# File 'app/controllers/mailservice_controller.rb', line 273 def user_email_address_confirm(address,code) user_logged_in? address_obj=Address.find_by_address(address) ml=@session[:user].mailinglist if ml.confirm(address_obj,code) then ml.save else false end end |
#user_email_addresses ⇒ Object
Returns all addresses belonging to this user.
253 254 255 256 |
# File 'app/controllers/mailservice_controller.rb', line 253 def user_email_addresses user_logged_in? @session[:user].addresses.map { |a| a.address } end |
#user_email_addresses_add(address) ⇒ Object
Adds a new email address to this user.
334 335 336 337 338 339 340 |
# File 'app/controllers/mailservice_controller.rb', line 334 def user_email_addresses_add(address) user_logged_in? if @session[:user].mailinglist.subscribe address then @session[:user].mailinglist.save end @session[:user].mailinglist.addresses end |
#user_email_addresses_confirmed ⇒ Object
Returns all confirmed email addresses belonging to this user
259 260 261 262 |
# File 'app/controllers/mailservice_controller.rb', line 259 def user_email_addresses_confirmed user_logged_in? @session[:user].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.
343 344 345 346 |
# File 'app/controllers/mailservice_controller.rb', line 343 def user_email_addresses_remove(address) user_logged_in? @session[:user].mailinglist.unsubscribe address end |
#user_email_addresses_unconfirmed ⇒ Object
Returns all yet-to-be-confirmed email addresses belong to this user.
266 267 268 269 |
# File 'app/controllers/mailservice_controller.rb', line 266 def user_email_addresses_unconfirmed user_logged_in? @session[:user].mailinglist.pending_addresses.map { |a| a.address } end |
#user_logged_in ⇒ Object
Returns whether the user is logged in or not.
234 |
# File 'app/controllers/mailservice_controller.rb', line 234 def user_logged_in; not @session[:user].nil?; end |
#user_login(username, password) ⇒ Object
Logs the user in. If the user logs in successfully, returns true, otherwise false.
224 225 226 227 228 |
# File 'app/controllers/mailservice_controller.rb', line 224 def user_login username, password domain_logged_in? @session[:user]=User.authenticate username, password user_logged_in end |
#user_logout ⇒ Object
Logs the user out.
231 |
# File 'app/controllers/mailservice_controller.rb', line 231 def user_logout; @session[:user] = nil; end |
#user_mailinglists ⇒ Object
Returns all the mailing lists that belong to this user.
240 241 242 243 244 |
# File 'app/controllers/mailservice_controller.rb', line 240 def user_mailinglists user_logged_in? Mailinglist.find_all_by_user_id(@session[:user].id).map { |m| m.id } - [ @session[:user].mailinglist_id ] end |
#user_name ⇒ Object
Returns the user’s username.
237 |
# File 'app/controllers/mailservice_controller.rb', line 237 def user_name; @session[:user].login; end |
#user_real_name ⇒ Object
Returns the user’s real name.
306 307 308 309 310 |
# File 'app/controllers/mailservice_controller.rb', line 306 def user_real_name user_logged_in? @session[:user].description end |