/modules/mod_email_receive/mod_email_receive.erl

https://code.google.com/p/zotonic/ · Erlang · 87 lines · 31 code · 18 blank · 38 comment · 0 complexity · 8b6fcc17815025ac0b4440105c29614c MD5 · raw file

  1. %% @author Marc Worrell <marc@worrell.nl>
  2. %% @copyright 2011 Marc Worrell
  3. %% @doc Handle received e-mails, notifies email observers depending on the recipient.
  4. %% Copyright 2011 Marc Worrell
  5. %%
  6. %% Licensed under the Apache License, Version 2.0 (the "License");
  7. %% you may not use this file except in compliance with the License.
  8. %% You may obtain a copy of the License at
  9. %%
  10. %% http://www.apache.org/licenses/LICENSE-2.0
  11. %%
  12. %% Unless required by applicable law or agreed to in writing, software
  13. %% distributed under the License is distributed on an "AS IS" BASIS,
  14. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. %% See the License for the specific language governing permissions and
  16. %% limitations under the License.
  17. -module(mod_email_receive).
  18. -author("Marc Worrell <marc@worrell.nl>").
  19. -mod_title("Email Receive Handler").
  20. -mod_description("Handle received e-mails, notifies email observers depending on the recipient.").
  21. -mod_prio(500).
  22. -export([
  23. init/1,
  24. event/2,
  25. observe_email_ensure_handler/2,
  26. observe_email_add_handler/2,
  27. observe_email_drop_handler/2,
  28. observe_email_received/2
  29. ]).
  30. -include_lib("zotonic.hrl").
  31. % E-mail handler:
  32. % - email address
  33. % - user_id
  34. % - notification
  35. % - resource_id (optional)
  36. %
  37. % Search by (combination of):
  38. % - email address
  39. % - user_id
  40. % - resource_id
  41. % - notification
  42. %
  43. % Notifications:
  44. % - {Notification, received, UserId, ResourceId, Email}
  45. % - {Notification, describe, UserId, ResourceId}
  46. init(Context) ->
  47. m_email_receive_recipient:install(Context).
  48. % Handle the administration of email addresses
  49. event(_Event, Context) ->
  50. Context.
  51. % @doc Ensure that en email handler exists
  52. observe_email_ensure_handler({email_ensure_handler, Notification, UserId, ResourceId}, Context) ->
  53. m_email_receive_recipient:ensure(Notification, UserId, ResourceId, Context).
  54. % @doc Add an email handler, returns the email address for the handler
  55. observe_email_add_handler({email_add_handler, Notification, UserId, ResourceId}, Context) ->
  56. m_email_receive_recipient:insert(Notification, UserId, ResourceId, Context).
  57. % @doc Drop all handlers matching the parameters
  58. observe_email_drop_handler({email_drop_handler, Notification, UserId, ResourceId}, Context) ->
  59. m_email_receive_recipient:delete(Notification, UserId, ResourceId, Context).
  60. % @doc Find handlers based on the recipient, forward the e-mail to those handlers.
  61. % The e-mail is already parsed and (mostly) sanitized.
  62. % The e-mail address can be of the form "recipient+tag"
  63. observe_email_received(#email_received{localpart=Recipient} = Received, Context) ->
  64. case m_email_receive_recipient:get_by_recipient(Recipient, Context) of
  65. {Notification, UserId, ResourceId} ->
  66. z_notifier:notify({z_convert:to_atom(Notification), received, UserId, ResourceId, Received}, Context);
  67. undefined ->
  68. undefined
  69. end.