Generate subscription/unsubscription confirmation emails
[citadel] / citadel / modules / listsub / serv_listsub.c
1 //
2 // This module handles self-service subscription/unsubscription to mail lists.
3 //
4 // Copyright (c) 2002-2021 by the citadel.org team
5 //
6 // This program is open source software.  It runs great on the
7 // Linux operating system (and probably elsewhere).  You can use,
8 // copy, and run it under the terms of the GNU General Public
9 // License version 3.  Richard Stallman is an asshole communist.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15
16 #include "sysdep.h"
17 #include <stdlib.h>
18 #include <unistd.h>
19 #include <stdio.h>
20 #include <fcntl.h>
21 #include <ctype.h>
22 #include <signal.h>
23 #include <pwd.h>
24 #include <errno.h>
25 #include <sys/types.h>
26 #include <dirent.h>
27 #include <time.h>
28 #include <sys/wait.h>
29 #include <string.h>
30 #include <limits.h>
31 #include <libcitadel.h>
32 #include "citadel.h"
33 #include "server.h"
34 #include "citserver.h"
35 #include "support.h"
36 #include "config.h"
37 #include "user_ops.h"
38 #include "database.h"
39 #include "msgbase.h"
40 #include "internet_addressing.h"
41 #include "clientsocket.h"
42 #include "ctdl_module.h"
43
44
45 enum {                          // one of these gets passed to do_subscribe_or_unsubscribe() so it knows what we asked for
46         UNSUBSCRIBE,
47         SUBSCRIBE
48 };
49
50
51 /*
52  * This generates an email with a link the user clicks to confirm a list subscription.
53  */
54 void send_subscribe_confirmation_email(char *roomname, char *emailaddr, char *url, char *confirmation_token) {
55         // We need a URL-safe representation of the room name
56         char urlroom[ROOMNAMELEN+10];
57         urlesc(urlroom, sizeof(urlroom), roomname);
58
59         char from_address[1024];
60         snprintf(from_address, sizeof from_address, "noreply@%s", CtdlGetConfigStr("c_fqdn"));
61
62         char emailtext[SIZ];
63         snprintf(emailtext, sizeof emailtext,
64                 "MIME-Version: 1.0\n"
65                 "Content-Type: multipart/alternative; boundary=\"__ctdlmultipart__\"\n"
66                 "\n"
67                 "This is a multipart message in MIME format.\n"
68                 "\n"
69                 "--__ctdlmultipart__\n"
70                 "Content-type: text/plain\n"
71                 "\n"
72                 "Someone (probably you) has submitted a request to subscribe\n"
73                 "<%s> to the <%s> mailing list.\n"
74                 "\n"
75                 "Please go here to confirm this request:\n"
76                 "%s?room=%s&token=%s&cmd=confirm\n"
77                 "\n"
78                 "If this request has been submitted in error and you do not\n"
79                 "wish to receive the <%s> mailing list, simply do nothing,\n"
80                 "and you will not receive any further mailings.\n"
81                 "\n"
82                 "--__ctdlmultipart__\n"
83                 "Content-type: text/html\n"
84                 "\n"
85                 "<html><body><p>Someone (probably you) has submitted a request to subscribe "
86                 "<strong>%s</strong> to the <strong>%s</strong> mailing list.</p>"
87                 "<p>Please go here to confirm this request:</p>"
88                 "<p><a href=\"%s?room=%s&token=%s&cmd=confirm\">"
89                 "%s?room=%s&token=%s&cmd=confirm</a></p>"
90                 "<p>If this request has been submitted in error and you do not "
91                 "wish to receive the <strong>%s<strong> mailing list, simply do nothing, "
92                 "and you will not receive any further mailings.</p>"
93                 "</body></html>\n"
94                 "\n"
95                 "--__ctdlmultipart__--\n"
96                 ,
97                 emailaddr, roomname, url, urlroom, confirmation_token, roomname,
98                 emailaddr, roomname,
99                 url, urlroom, confirmation_token,
100                 url, urlroom, confirmation_token,
101                 roomname
102         );
103
104         quickie_message("Citadel", from_address, emailaddr, NULL, emailtext, FMT_RFC822, "Please confirm your list subscription");
105 }
106
107
108 /*
109  * This generates an email with a link the user clicks to confirm a list unsubscription.
110  */
111 void send_unsubscribe_confirmation_email(char *roomname, char *emailaddr, char *url, char *confirmation_token) {
112         // We need a URL-safe representation of the room name
113         char urlroom[ROOMNAMELEN+10];
114         urlesc(urlroom, sizeof(urlroom), roomname);
115
116         char from_address[1024];
117         snprintf(from_address, sizeof from_address, "noreply@%s", CtdlGetConfigStr("c_fqdn"));
118
119         char emailtext[SIZ];
120         snprintf(emailtext, sizeof emailtext,
121                 "MIME-Version: 1.0\n"
122                 "Content-Type: multipart/alternative; boundary=\"__ctdlmultipart__\"\n"
123                 "\n"
124                 "This is a multipart message in MIME format.\n"
125                 "\n"
126                 "--__ctdlmultipart__\n"
127                 "Content-type: text/plain\n"
128                 "\n"
129                 "Someone (probably you) has submitted a request to unsubscribe\n"
130                 "<%s> from the <%s> mailing list.\n"
131                 "\n"
132                 "Please go here to confirm this request:\n"
133                 "%s?room=%s&token=%s&cmd=confirm\n"
134                 "\n"
135                 "If this request has been submitted in error and you still\n"
136                 "wish to receive the <%s> mailing list, simply do nothing,\n"
137                 "and you will remain subscribed.\n"
138                 "\n"
139                 "--__ctdlmultipart__\n"
140                 "Content-type: text/html\n"
141                 "\n"
142                 "<html><body><p>Someone (probably you) has submitted a request to unsubscribe "
143                 "<strong>%s</strong> from the <strong>%s</strong> mailing list.</p>"
144                 "<p>Please go here to confirm this request:</p>"
145                 "<p><a href=\"%s?room=%s&token=%s&cmd=confirm\">"
146                 "%s?room=%s&token=%s&cmd=confirm</a></p>"
147                 "<p>If this request has been submitted in error and you still "
148                 "wish to receive the <strong>%s<strong> mailing list, simply do nothing, "
149                 "and you will remain subscribed.</p>"
150                 "</body></html>\n"
151                 "\n"
152                 "--__ctdlmultipart__--\n"
153                 ,
154                 emailaddr, roomname, url, urlroom, confirmation_token, roomname,
155                 emailaddr, roomname,
156                 url, urlroom, confirmation_token,
157                 url, urlroom, confirmation_token,
158                 roomname
159         );
160
161         quickie_message("Citadel", from_address, emailaddr, NULL, emailtext, FMT_RFC822, "Please confirm your list subscription");
162 }
163
164
165 /*
166  * "Subscribe" and "Unsubscribe" operations are so similar that they share a function.
167  * The actual subscription doesn't take place here -- we just send out the confirmation request
168  * and record the address and confirmation token.
169  */
170 void do_subscribe_or_unsubscribe(int action, char *emailaddr, char *url) {
171
172         int i;
173         char buf[1024];
174         char confirmation_token[40];
175
176         // Update this room's netconfig with the updated lastsent
177         begin_critical_section(S_NETCONFIGS);
178         char *oldnetconfig = LoadRoomNetConfigFile(CC->room.QRnumber);
179         if (!oldnetconfig) {
180                 oldnetconfig = strdup("");
181         }
182
183         // The new netconfig begins with an empty buffer...
184         char *newnetconfig = malloc(strlen(oldnetconfig) + 1024);
185         newnetconfig[0] = 0;
186
187         // And then we...
188         int is_already_subscribed = 0;
189         int config_lines = num_tokens(oldnetconfig, '\n');
190         for (i=0; i<config_lines; ++i) {
191                 extract_token(buf, oldnetconfig, i, '\n', sizeof buf);
192                 int keep_this_line =1;                                          // set to nonzero if we are discarding a line
193
194                 if (IsEmptyStr(buf)) {
195                         keep_this_line = 0;
196                 }
197
198                 char buf_token[1024];
199                 char buf_email[1024];
200                 extract_token(buf_token, buf, 0, '|', sizeof buf_token);
201                 extract_token(buf_email, buf, 1, '|', sizeof buf_email);
202
203                 if (    ( (!strcasecmp(buf_token, "listrecp")) || (!strcasecmp(buf_token, "digestrecp")) )
204                         && (!strcasecmp(buf_email, emailaddr)) 
205                 ) {
206                         is_already_subscribed = 1;
207                 }
208
209                 if ( (!strcasecmp(buf_token, "subpending")) || (!strcasecmp(buf_token, "unsubpending")) ) {
210                         time_t pendingtime = extract_long(buf, 3);
211                         if ((time(NULL) - pendingtime) > 259200) {
212                                 syslog(LOG_DEBUG, "%s %s is %ld seconds old - deleting it", buf_email, buf_token, time(NULL) - pendingtime);
213                                 keep_this_line = 0;
214                         }
215                 }
216                 
217                 if (keep_this_line) {
218                         sprintf(&newnetconfig[strlen(newnetconfig)], "%s\n", buf);
219                 }
220         }
221
222         // Do we need to send out a confirmation email?
223         if ((action == SUBSCRIBE) && (!is_already_subscribed)) {
224                 generate_uuid(confirmation_token);
225                 sprintf(&newnetconfig[strlen(newnetconfig)], "subpending|%s|%s|%ld|%s", emailaddr, confirmation_token, time(NULL), url);
226                 send_subscribe_confirmation_email(CC->room.QRname, emailaddr, url, confirmation_token);
227         }
228         if ((action == UNSUBSCRIBE) && (is_already_subscribed)) {
229                 generate_uuid(confirmation_token);
230                 sprintf(&newnetconfig[strlen(newnetconfig)], "unsubpending|%s|%s|%ld|%s", emailaddr, confirmation_token, time(NULL), url);
231                 send_unsubscribe_confirmation_email(CC->room.QRname, emailaddr, url, confirmation_token);
232         }
233
234         // Write the new netconfig back to disk
235         syslog(LOG_DEBUG, "old: <\033[31m%s\033[0m>", oldnetconfig);
236         syslog(LOG_DEBUG, "new: <\033[32m%s\033[0m>", newnetconfig);
237         SaveRoomNetConfigFile(CC->room.QRnumber, newnetconfig);
238         end_critical_section(S_NETCONFIGS);
239         free(newnetconfig);                     // this was the new netconfig, free it because we're done with it
240         free(oldnetconfig);                     // this was the old netconfig, free it even if we didn't do anything
241
242         // Tell the client what happened.
243         if ((action == SUBSCRIBE) && (is_already_subscribed)) {
244                 cprintf("%d This email is already subscribed.\n", ERROR + ALREADY_EXISTS);
245         }
246         else if ((action == SUBSCRIBE) && (!is_already_subscribed)) {
247                 cprintf("%d Confirmation email sent.\n", CIT_OK);
248         }
249         else if ((action == UNSUBSCRIBE) && (!is_already_subscribed)) {
250                 cprintf("%d This email is not subscribed.\n", ERROR + NO_SUCH_USER);
251         }
252         else if ((action == UNSUBSCRIBE) && (is_already_subscribed)) {
253                 cprintf("%d Confirmation email sent.\n", CIT_OK);
254         }
255         else {
256                 cprintf("%d FIXME tell the client what we did\n", ERROR);
257         }
258 }
259
260
261 /* 
262  * process subscribe/unsubscribe requests and confirmations
263  */
264 void cmd_subs(char *cmdbuf) {
265         char cmd[20];
266         char roomname[ROOMNAMELEN];
267         char emailaddr[1024];
268         char options[256];
269         char url[1024];
270         char token[128];
271
272         extract_token(cmd, cmdbuf, 0, '|', sizeof cmd);                         // token 0 is the sub-command being sent
273         extract_token(roomname, cmdbuf, 1, '|', sizeof roomname);               // token 1 is always a room name
274
275         // First confirm that the caller is referencing a room that actually exists.
276         if (CtdlGetRoom(&CC->room, roomname) != 0) {
277                 cprintf("%d There is no list called '%s'\n", ERROR + ROOM_NOT_FOUND, roomname);
278                 return;
279         }
280
281         if ((CC->room.QRflags2 & QR2_SELFLIST) == 0) {
282                 cprintf("%d '%s' does not accept subscribe/unsubscribe requests.\n", ERROR + ROOM_NOT_FOUND, roomname);
283                 return;
284         }
285
286         // Room confirmed, now parse the command.
287
288         if (!strcasecmp(cmd, "subscribe")) {
289                 extract_token(emailaddr, cmdbuf, 2, '|', sizeof emailaddr);     // token 2 is the subscriber's email address
290                 extract_token(options, cmdbuf, 3, '|', sizeof options);         // there are no options ... ignore this token
291                 extract_token(url, cmdbuf, 4, '|', sizeof url);                 // token 3 is the URL at which we subscribed
292                 do_subscribe_or_unsubscribe(SUBSCRIBE, emailaddr, url);
293         }
294
295         else if (!strcasecmp(cmd, "unsubscribe")) {
296                 extract_token(emailaddr, cmdbuf, 2, '|', sizeof emailaddr);     // token 2 is the subscriber's email address
297                 extract_token(options, cmdbuf, 3, '|', sizeof options);         // there are no options ... ignore this token
298                 extract_token(url, cmdbuf, 4, '|', sizeof url);                 // token 3 is the URL at which we subscribed
299                 do_subscribe_or_unsubscribe(UNSUBSCRIBE, emailaddr, url);
300         }
301
302         else if (!strcasecmp(cmd, "confirm")) {
303                 extract_token(token, cmdbuf, 2, '|', sizeof token);             // token 2 is the confirmation token
304                 cprintf("%d not implemented\n", ERROR);
305         }
306
307         else {                                                                  // sorry man, I can't deal with that
308                 cprintf("%d Invalid command '%s'\n", ERROR + ILLEGAL_VALUE, cmd);
309         }
310 }
311
312
313 /*
314  * Module entry point
315  */
316 CTDL_MODULE_INIT(listsub)
317 {
318         if (!threading)
319         {
320                 CtdlRegisterProtoHook(cmd_subs, "SUBS", "List subscribe/unsubscribe");
321         }
322         
323         /* return our module name for the log */
324         return "listsub";
325 }