4 * This module handles self-service subscription/unsubscription to mail lists.
6 * Copyright (c) 2002-2009 by the citadel.org team
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33 #include <sys/types.h>
35 #if TIME_WITH_SYS_TIME
36 # include <sys/time.h>
40 # include <sys/time.h>
49 #include <libcitadel.h>
52 #include "citserver.h"
59 #include "internet_addressing.h"
60 #include "clientsocket.h"
69 #include "ctdl_module.h"
73 * Generate a randomizationalisticized token to use for authentication of
74 * a subscribe or unsubscribe request.
76 void listsub_generate_token(char *buf) {
80 /* Theo, please sit down and shut up. This key doesn't have to be
81 * tinfoil-hat secure, it just needs to be reasonably unguessable
84 sprintf(sourcebuf, "%lx",
85 (long) (++seq + getpid() + time(NULL))
88 /* Convert it to base64 so it looks cool */
89 CtdlEncodeBase64(buf, sourcebuf, strlen(sourcebuf), 0);
94 * Enter a subscription request
96 void do_subscribe(char *room, char *email, char *subtype, char *webpage) {
97 struct ctdlroom qrbuf;
101 char confirmation_request[2048];
103 char urlroom[ROOMNAMELEN];
108 if (CtdlGetRoom(&qrbuf, room) != 0) {
109 cprintf("%d There is no list called '%s'\n", ERROR + ROOM_NOT_FOUND, room);
113 if ((qrbuf.QRflags2 & QR2_SELFLIST) == 0) {
115 "does not accept subscribe/unsubscribe requests.\n",
116 ERROR + HIGHER_ACCESS_REQUIRED, qrbuf.QRname);
120 listsub_generate_token(token);
122 assoc_file_name(filename, sizeof filename, &qrbuf, ctdl_netcfg_dir);
125 * Make sure the requested address isn't already subscribed
127 begin_critical_section(S_NETCONFIGS);
128 ncfp = fopen(filename, "r");
130 while (fgets(buf, sizeof buf, ncfp) != NULL) {
131 buf[strlen(buf)-1] = 0;
132 extract_token(scancmd, buf, 0, '|', sizeof scancmd);
133 extract_token(scanemail, buf, 1, '|', sizeof scanemail);
134 if ((!strcasecmp(scancmd, "listrecp"))
135 || (!strcasecmp(scancmd, "digestrecp"))) {
136 if (!strcasecmp(scanemail, email)) {
143 end_critical_section(S_NETCONFIGS);
145 if (found_sub != 0) {
146 cprintf("%d '%s' is already subscribed to '%s'.\n",
147 ERROR + ALREADY_EXISTS,
148 email, qrbuf.QRname);
153 * Now add it to the file
155 begin_critical_section(S_NETCONFIGS);
156 ncfp = fopen(filename, "a");
158 fprintf(ncfp, "subpending|%s|%s|%s|%ld|%s\n",
167 end_critical_section(S_NETCONFIGS);
169 /* Generate and send the confirmation request */
171 urlesc(urlroom, ROOMNAMELEN, qrbuf.QRname);
173 snprintf(confirmation_request, sizeof confirmation_request,
175 "MIME-Version: 1.0\n"
176 "Content-Type: multipart/alternative; boundary=\"__ctdlmultipart__\"\n"
178 "This is a multipart message in MIME format.\n"
180 "--__ctdlmultipart__\n"
181 "Content-type: text/plain\n"
183 "Someone (probably you) has submitted a request to subscribe\n"
184 "<%s> to the '%s' mailing list.\n"
186 "Please go here to confirm this request:\n"
187 " %s?room=%s&token=%s&cmd=confirm \n"
189 "If this request has been submitted in error and you do not\n"
190 "wish to receive the '%s' mailing list, simply do nothing,\n"
191 "and you will not receive any further mailings.\n"
193 "--__ctdlmultipart__\n"
194 "Content-type: text/html\n"
197 "Someone (probably you) has submitted a request to subscribe\n"
198 "<%s> to the <B>%s</B> mailing list.<BR><BR>\n"
199 "Please click here to confirm this request:<BR>\n"
200 "<A HREF=\"%s?room=%s&token=%s&cmd=confirm\">"
201 "%s?room=%s&token=%s&cmd=confirm</A><BR><BR>\n"
202 "If this request has been submitted in error and you do not\n"
203 "wish to receive the '%s' mailing list, simply do nothing,\n"
204 "and you will not receive any further mailings.\n"
207 "--__ctdlmultipart__--\n",
210 webpage, urlroom, token,
214 webpage, urlroom, token,
215 webpage, urlroom, token,
219 quickie_message( /* This delivers the message */
224 confirmation_request,
226 "Please confirm your list subscription"
229 cprintf("%d Subscription entered; confirmation request sent\n", CIT_OK);
234 * Enter an unsubscription request
236 void do_unsubscribe(char *room, char *email, char *webpage) {
237 struct ctdlroom qrbuf;
242 char confirmation_request[2048];
243 char urlroom[ROOMNAMELEN];
248 if (CtdlGetRoom(&qrbuf, room) != 0) {
249 cprintf("%d There is no list called '%s'\n",
250 ERROR + ROOM_NOT_FOUND, room);
254 if ((qrbuf.QRflags2 & QR2_SELFLIST) == 0) {
256 "does not accept subscribe/unsubscribe requests.\n",
257 ERROR + HIGHER_ACCESS_REQUIRED, qrbuf.QRname);
261 listsub_generate_token(token);
263 assoc_file_name(filename, sizeof filename, &qrbuf, ctdl_netcfg_dir);
266 * Make sure there's actually a subscription there to remove
268 begin_critical_section(S_NETCONFIGS);
269 ncfp = fopen(filename, "r");
271 while (fgets(buf, sizeof buf, ncfp) != NULL) {
272 buf[strlen(buf)-1] = 0;
273 extract_token(scancmd, buf, 0, '|', sizeof scancmd);
274 extract_token(scanemail, buf, 1, '|', sizeof scanemail);
275 if ((!strcasecmp(scancmd, "listrecp"))
276 || (!strcasecmp(scancmd, "digestrecp"))) {
277 if (!strcasecmp(scanemail, email)) {
284 end_critical_section(S_NETCONFIGS);
286 if (found_sub == 0) {
287 cprintf("%d '%s' is not subscribed to '%s'.\n",
288 ERROR + NO_SUCH_USER,
289 email, qrbuf.QRname);
294 * Ok, now enter the unsubscribe-pending entry.
296 begin_critical_section(S_NETCONFIGS);
297 ncfp = fopen(filename, "a");
299 fprintf(ncfp, "unsubpending|%s|%s|%ld|%s\n",
307 end_critical_section(S_NETCONFIGS);
309 /* Generate and send the confirmation request */
311 urlesc(urlroom, ROOMNAMELEN, qrbuf.QRname);
313 snprintf(confirmation_request, sizeof confirmation_request,
315 "MIME-Version: 1.0\n"
316 "Content-Type: multipart/alternative; boundary=\"__ctdlmultipart__\"\n"
318 "This is a multipart message in MIME format.\n"
320 "--__ctdlmultipart__\n"
321 "Content-type: text/plain\n"
323 "Someone (probably you) has submitted a request to unsubscribe\n"
324 "<%s> from the '%s' mailing list.\n"
326 "Please go here to confirm this request:\n"
327 " %s?room=%s&token=%s&cmd=confirm \n"
329 "If this request has been submitted in error and you do not\n"
330 "wish to unsubscribe from the '%s' mailing list, simply do nothing,\n"
331 "and the request will not be processed.\n"
333 "--__ctdlmultipart__\n"
334 "Content-type: text/html\n"
337 "Someone (probably you) has submitted a request to unsubscribe\n"
338 "<%s> from the <B>%s</B> mailing list.<BR><BR>\n"
339 "Please click here to confirm this request:<BR>\n"
340 "<A HREF=\"%s?room=%s&token=%s&cmd=confirm\">"
341 "%s?room=%s&token=%s&cmd=confirm</A><BR><BR>\n"
342 "If this request has been submitted in error and you do not\n"
343 "wish to unsubscribe from the '%s' mailing list, simply do nothing,\n"
344 "and the request will not be processed.\n"
347 "--__ctdlmultipart__--\n",
350 webpage, urlroom, token,
354 webpage, urlroom, token,
355 webpage, urlroom, token,
359 quickie_message( /* This delivers the message */
364 confirmation_request,
366 "Please confirm your unsubscribe request"
369 cprintf("%d Unubscription noted; confirmation request sent\n", CIT_OK);
374 * Confirm a subscribe/unsubscribe request.
376 void do_confirm(char *room, char *token) {
377 struct ctdlroom qrbuf;
380 char line_token[256];
388 char address_to_unsubscribe[256];
391 char *holdbuf = NULL;
395 strcpy(address_to_unsubscribe, "");
397 if (CtdlGetRoom(&qrbuf, room) != 0) {
398 cprintf("%d There is no list called '%s'\n",
399 ERROR + ROOM_NOT_FOUND, room);
403 if ((qrbuf.QRflags2 & QR2_SELFLIST) == 0) {
405 "does not accept subscribe/unsubscribe requests.\n",
406 ERROR + HIGHER_ACCESS_REQUIRED, qrbuf.QRname);
411 * Now start scanning this room's netconfig file for the
414 assoc_file_name(filename, sizeof filename, &qrbuf, ctdl_netcfg_dir);
415 begin_critical_section(S_NETCONFIGS);
416 ncfp = fopen(filename, "r+");
418 while (line_offset = ftell(ncfp),
419 (fgets(buf, sizeof buf, ncfp) != NULL) ) {
420 buf[strlen(buf)-1] = 0;
421 line_length = strlen(buf);
422 extract_token(cmd, buf, 0, '|', sizeof cmd);
423 if (!strcasecmp(cmd, "subpending")) {
424 extract_token(email, buf, 1, '|', sizeof email);
425 extract_token(subtype, buf, 2, '|', sizeof subtype);
426 extract_token(line_token, buf, 3, '|', sizeof line_token);
427 if (!strcasecmp(token, line_token)) {
428 if (!strcasecmp(subtype, "digest")) {
429 safestrncpy(buf, "digestrecp|", sizeof buf);
432 safestrncpy(buf, "listrecp|", sizeof buf);
436 /* SLEAZY HACK: pad the line out so
437 * it's the same length as the line
440 while (strlen(buf) < line_length) {
443 fseek(ncfp, line_offset, SEEK_SET);
444 fprintf(ncfp, "%s\n", buf);
448 if (!strcasecmp(cmd, "unsubpending")) {
449 extract_token(line_token, buf, 2, '|', sizeof line_token);
450 if (!strcasecmp(token, line_token)) {
451 extract_token(address_to_unsubscribe, buf, 1, '|',
452 sizeof address_to_unsubscribe);
458 end_critical_section(S_NETCONFIGS);
461 * If "address_to_unsubscribe" contains something, then we have to
462 * make another pass at the file, stripping out lines referring to
465 if (!IsEmptyStr(address_to_unsubscribe)) {
466 holdbuf = malloc(SIZ);
467 begin_critical_section(S_NETCONFIGS);
468 ncfp = fopen(filename, "r+");
470 while (line_offset = ftell(ncfp),
471 (fgets(buf, sizeof buf, ncfp) != NULL) ) {
472 buf[strlen(buf)-1]=0;
473 extract_token(scancmd, buf, 0, '|', sizeof scancmd);
474 extract_token(scanemail, buf, 1, '|', sizeof scanemail);
475 if ( (!strcasecmp(scancmd, "listrecp"))
476 && (!strcasecmp(scanemail,
477 address_to_unsubscribe)) ) {
480 else if ( (!strcasecmp(scancmd, "digestrecp"))
481 && (!strcasecmp(scanemail,
482 address_to_unsubscribe)) ) {
485 else if ( (!strcasecmp(scancmd, "subpending"))
486 && (!strcasecmp(scanemail,
487 address_to_unsubscribe)) ) {
490 else if ( (!strcasecmp(scancmd, "unsubpending"))
491 && (!strcasecmp(scanemail,
492 address_to_unsubscribe)) ) {
495 else { /* Not relevant, so *keep* it! */
496 linelen = strlen(buf);
497 holdbuf = realloc(holdbuf,
498 (buflen + linelen + 2) );
499 strcpy(&holdbuf[buflen], buf);
501 strcpy(&holdbuf[buflen], "\n");
507 ncfp = fopen(filename, "w");
509 fwrite(holdbuf, buflen+1, 1, ncfp);
512 end_critical_section(S_NETCONFIGS);
517 * Did we do anything useful today?
520 cprintf("%d %d operation(s) confirmed.\n", CIT_OK, success);
521 CtdlLogPrintf(CTDL_NOTICE,
522 "Mailing list: %s %ssubscribed to %s with token %s\n",
524 (!IsEmptyStr(address_to_unsubscribe)) ? "un" : "",
529 cprintf("%d Invalid token.\n", ERROR + ILLEGAL_VALUE);
537 * process subscribe/unsubscribe requests and confirmations
539 void cmd_subs(char *cmdbuf) {
542 char room[ROOMNAMELEN];
548 extract_token(opr, cmdbuf, 0, '|', sizeof opr);
549 if (!strcasecmp(opr, "subscribe")) {
550 extract_token(subtype, cmdbuf, 3, '|', sizeof subtype);
551 if ( (strcasecmp(subtype, "list"))
552 && (strcasecmp(subtype, "digest")) ) {
553 cprintf("%d Invalid subscription type '%s'\n",
554 ERROR + ILLEGAL_VALUE, subtype);
557 extract_token(room, cmdbuf, 1, '|', sizeof room);
558 extract_token(email, cmdbuf, 2, '|', sizeof email);
559 extract_token(webpage, cmdbuf, 4, '|', sizeof webpage);
560 do_subscribe(room, email, subtype, webpage);
563 else if (!strcasecmp(opr, "unsubscribe")) {
564 extract_token(room, cmdbuf, 1, '|', sizeof room);
565 extract_token(email, cmdbuf, 2, '|', sizeof email);
566 extract_token(webpage, cmdbuf, 3, '|', sizeof webpage);
567 do_unsubscribe(room, email, webpage);
569 else if (!strcasecmp(opr, "confirm")) {
570 extract_token(room, cmdbuf, 1, '|', sizeof room);
571 extract_token(token, cmdbuf, 2, '|', sizeof token);
572 do_confirm(room, token);
575 cprintf("%d Invalid command\n", ERROR + ILLEGAL_VALUE);
583 CTDL_MODULE_INIT(listsub)
587 CtdlRegisterProtoHook(cmd_subs, "SUBS", "List subscribe/unsubscribe");
590 /* return our Subversion id for the Log */