PageRenderTime 28ms CodeModel.GetById 11ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

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