2 // This module handles self-service subscription/unsubscription to mail lists.
4 // Copyright (c) 2002-2021 by the citadel.org team
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.
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.
25 #include <sys/types.h>
31 #include <libcitadel.h>
34 #include "citserver.h"
40 #include "internet_addressing.h"
41 #include "clientsocket.h"
42 #include "ctdl_module.h"
45 enum { // one of these gets passed to do_subscribe_or_unsubscribe() so it knows what we asked for
52 * This generates an email with a link the user clicks to confirm a list subscription.
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);
59 char from_address[1024];
60 snprintf(from_address, sizeof from_address, "noreply@%s", CtdlGetConfigStr("c_fqdn"));
63 snprintf(emailtext, sizeof emailtext,
65 "Content-Type: multipart/alternative; boundary=\"__ctdlmultipart__\"\n"
67 "This is a multipart message in MIME format.\n"
69 "--__ctdlmultipart__\n"
70 "Content-type: text/plain\n"
72 "Someone (probably you) has submitted a request to subscribe\n"
73 "<%s> to the <%s> mailing list.\n"
75 "Please go here to confirm this request:\n"
76 "%s?room=%s&token=%s&cmd=confirm\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"
82 "--__ctdlmultipart__\n"
83 "Content-type: text/html\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>"
95 "--__ctdlmultipart__--\n"
97 emailaddr, roomname, url, urlroom, confirmation_token, roomname,
99 url, urlroom, confirmation_token,
100 url, urlroom, confirmation_token,
104 quickie_message("Citadel", from_address, emailaddr, NULL, emailtext, FMT_RFC822, "Please confirm your list subscription");
109 * This generates an email with a link the user clicks to confirm a list unsubscription.
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);
116 char from_address[1024];
117 snprintf(from_address, sizeof from_address, "noreply@%s", CtdlGetConfigStr("c_fqdn"));
120 snprintf(emailtext, sizeof emailtext,
121 "MIME-Version: 1.0\n"
122 "Content-Type: multipart/alternative; boundary=\"__ctdlmultipart__\"\n"
124 "This is a multipart message in MIME format.\n"
126 "--__ctdlmultipart__\n"
127 "Content-type: text/plain\n"
129 "Someone (probably you) has submitted a request to unsubscribe\n"
130 "<%s> from the <%s> mailing list.\n"
132 "Please go here to confirm this request:\n"
133 "%s?room=%s&token=%s&cmd=confirm\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"
139 "--__ctdlmultipart__\n"
140 "Content-type: text/html\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>"
152 "--__ctdlmultipart__--\n"
154 emailaddr, roomname, url, urlroom, confirmation_token, roomname,
156 url, urlroom, confirmation_token,
157 url, urlroom, confirmation_token,
161 quickie_message("Citadel", from_address, emailaddr, NULL, emailtext, FMT_RFC822, "Please confirm your list subscription");
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.
170 void do_subscribe_or_unsubscribe(int action, char *emailaddr, char *url) {
174 char confirmation_token[40];
176 // Update this room's netconfig with the updated lastsent
177 begin_critical_section(S_NETCONFIGS);
178 char *oldnetconfig = LoadRoomNetConfigFile(CC->room.QRnumber);
180 oldnetconfig = strdup("");
183 // The new netconfig begins with an empty buffer...
184 char *newnetconfig = malloc(strlen(oldnetconfig) + 1024);
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
194 if (IsEmptyStr(buf)) {
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);
203 if ( ( (!strcasecmp(buf_token, "listrecp")) || (!strcasecmp(buf_token, "digestrecp")) )
204 && (!strcasecmp(buf_email, emailaddr))
206 is_already_subscribed = 1;
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);
217 if (keep_this_line) {
218 sprintf(&newnetconfig[strlen(newnetconfig)], "%s\n", buf);
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);
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);
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
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);
246 else if ((action == SUBSCRIBE) && (!is_already_subscribed)) {
247 cprintf("%d Confirmation email sent.\n", CIT_OK);
249 else if ((action == UNSUBSCRIBE) && (!is_already_subscribed)) {
250 cprintf("%d This email is not subscribed.\n", ERROR + NO_SUCH_USER);
252 else if ((action == UNSUBSCRIBE) && (is_already_subscribed)) {
253 cprintf("%d Confirmation email sent.\n", CIT_OK);
256 cprintf("%d FIXME tell the client what we did\n", ERROR);
262 * process subscribe/unsubscribe requests and confirmations
264 void cmd_subs(char *cmdbuf) {
266 char roomname[ROOMNAMELEN];
267 char emailaddr[1024];
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
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);
281 if ((CC->room.QRflags2 & QR2_SELFLIST) == 0) {
282 cprintf("%d '%s' does not accept subscribe/unsubscribe requests.\n", ERROR + ROOM_NOT_FOUND, roomname);
286 // Room confirmed, now parse the command.
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);
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);
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);
307 else { // sorry man, I can't deal with that
308 cprintf("%d Invalid command '%s'\n", ERROR + ILLEGAL_VALUE, cmd);
316 CTDL_MODULE_INIT(listsub)
320 CtdlRegisterProtoHook(cmd_subs, "SUBS", "List subscribe/unsubscribe");
323 /* return our module name for the log */