4 * This module handles self-service subscription/unsubscription to mail lists.
6 * Copyright (C) 2002-2005 by Art Cancro and others.
7 * This code is released under the terms of the GNU General Public License.
20 #include <sys/types.h>
22 #if TIME_WITH_SYS_TIME
23 # include <sys/time.h>
27 # include <sys/time.h>
36 #include <libcitadel.h>
39 #include "citserver.h"
47 #include "internet_addressing.h"
48 #include "clientsocket.h"
57 #include "ctdl_module.h"
61 * Generate a randomizationalisticized token to use for authentication of
62 * a subscribe or unsubscribe request.
64 void listsub_generate_token(char *buf
) {
68 /* Theo, please sit down and shut up. This key doesn't have to be
69 * tinfoil-hat secure, it just needs to be reasonably unguessable
72 sprintf(sourcebuf
, "%lx",
73 (long) (++seq
+ getpid() + time(NULL
))
76 /* Convert it to base64 so it looks cool */
77 CtdlEncodeBase64(buf
, sourcebuf
, strlen(sourcebuf
), 0);
82 * Enter a subscription request
84 void do_subscribe(char *room
, char *email
, char *subtype
, char *webpage
) {
85 struct ctdlroom qrbuf
;
89 char confirmation_request
[2048];
91 char urlroom
[ROOMNAMELEN
];
96 if (getroom(&qrbuf
, room
) != 0) {
97 cprintf("%d There is no list called '%s'\n", ERROR
+ ROOM_NOT_FOUND
, room
);
101 if ((qrbuf
.QRflags2
& QR2_SELFLIST
) == 0) {
103 "does not accept subscribe/unsubscribe requests.\n",
104 ERROR
+ HIGHER_ACCESS_REQUIRED
, qrbuf
.QRname
);
108 listsub_generate_token(token
);
110 assoc_file_name(filename
, sizeof filename
, &qrbuf
, ctdl_netcfg_dir
);
113 * Make sure the requested address isn't already subscribed
115 begin_critical_section(S_NETCONFIGS
);
116 ncfp
= fopen(filename
, "r");
118 while (fgets(buf
, sizeof buf
, ncfp
) != NULL
) {
119 buf
[strlen(buf
)-1] = 0;
120 extract_token(scancmd
, buf
, 0, '|', sizeof scancmd
);
121 extract_token(scanemail
, buf
, 1, '|', sizeof scanemail
);
122 if ((!strcasecmp(scancmd
, "listrecp"))
123 || (!strcasecmp(scancmd
, "digestrecp"))) {
124 if (!strcasecmp(scanemail
, email
)) {
131 end_critical_section(S_NETCONFIGS
);
133 if (found_sub
!= 0) {
134 cprintf("%d '%s' is already subscribed to '%s'.\n",
135 ERROR
+ ALREADY_EXISTS
,
136 email
, qrbuf
.QRname
);
141 * Now add it to the file
143 begin_critical_section(S_NETCONFIGS
);
144 ncfp
= fopen(filename
, "a");
146 fprintf(ncfp
, "subpending|%s|%s|%s|%ld|%s\n",
155 end_critical_section(S_NETCONFIGS
);
157 /* Generate and send the confirmation request */
159 urlesc(urlroom
, ROOMNAMELEN
, qrbuf
.QRname
);
161 snprintf(confirmation_request
, sizeof confirmation_request
,
163 "MIME-Version: 1.0\n"
164 "Content-Type: multipart/alternative; boundary=\"__ctdlmultipart__\"\n"
166 "This is a multipart message in MIME format.\n"
168 "--__ctdlmultipart__\n"
169 "Content-type: text/plain\n"
171 "Someone (probably you) has submitted a request to subscribe\n"
172 "<%s> to the '%s' mailing list.\n"
174 "Please go here to confirm this request:\n"
175 " %s?room=%s&token=%s&cmd=confirm \n"
177 "If this request has been submitted in error and you do not\n"
178 "wish to receive the '%s' mailing list, simply do nothing,\n"
179 "and you will not receive any further mailings.\n"
181 "--__ctdlmultipart__\n"
182 "Content-type: text/html\n"
185 "Someone (probably you) has submitted a request to subscribe\n"
186 "<%s> to the <B>%s</B> mailing list.<BR><BR>\n"
187 "Please click here to confirm this request:<BR>\n"
188 "<A HREF=\"%s?room=%s&token=%s&cmd=confirm\">"
189 "%s?room=%s&token=%s&cmd=confirm</A><BR><BR>\n"
190 "If this request has been submitted in error and you do not\n"
191 "wish to receive the '%s' mailing list, simply do nothing,\n"
192 "and you will not receive any further mailings.\n"
195 "--__ctdlmultipart__--\n",
198 webpage
, urlroom
, token
,
202 webpage
, urlroom
, token
,
203 webpage
, urlroom
, token
,
207 quickie_message( /* This delivers the message */
212 confirmation_request
,
214 "Please confirm your list subscription"
217 cprintf("%d Subscription entered; confirmation request sent\n", CIT_OK
);
222 * Enter an unsubscription request
224 void do_unsubscribe(char *room
, char *email
, char *webpage
) {
225 struct ctdlroom qrbuf
;
230 char confirmation_request
[2048];
231 char urlroom
[ROOMNAMELEN
];
236 if (getroom(&qrbuf
, room
) != 0) {
237 cprintf("%d There is no list called '%s'\n",
238 ERROR
+ ROOM_NOT_FOUND
, room
);
242 if ((qrbuf
.QRflags2
& QR2_SELFLIST
) == 0) {
244 "does not accept subscribe/unsubscribe requests.\n",
245 ERROR
+ HIGHER_ACCESS_REQUIRED
, qrbuf
.QRname
);
249 listsub_generate_token(token
);
251 assoc_file_name(filename
, sizeof filename
, &qrbuf
, ctdl_netcfg_dir
);
254 * Make sure there's actually a subscription there to remove
256 begin_critical_section(S_NETCONFIGS
);
257 ncfp
= fopen(filename
, "r");
259 while (fgets(buf
, sizeof buf
, ncfp
) != NULL
) {
260 buf
[strlen(buf
)-1] = 0;
261 extract_token(scancmd
, buf
, 0, '|', sizeof scancmd
);
262 extract_token(scanemail
, buf
, 1, '|', sizeof scanemail
);
263 if ((!strcasecmp(scancmd
, "listrecp"))
264 || (!strcasecmp(scancmd
, "digestrecp"))) {
265 if (!strcasecmp(scanemail
, email
)) {
272 end_critical_section(S_NETCONFIGS
);
274 if (found_sub
== 0) {
275 cprintf("%d '%s' is not subscribed to '%s'.\n",
276 ERROR
+ NO_SUCH_USER
,
277 email
, qrbuf
.QRname
);
282 * Ok, now enter the unsubscribe-pending entry.
284 begin_critical_section(S_NETCONFIGS
);
285 ncfp
= fopen(filename
, "a");
287 fprintf(ncfp
, "unsubpending|%s|%s|%ld|%s\n",
295 end_critical_section(S_NETCONFIGS
);
297 /* Generate and send the confirmation request */
299 urlesc(urlroom
, ROOMNAMELEN
, qrbuf
.QRname
);
301 snprintf(confirmation_request
, sizeof confirmation_request
,
303 "MIME-Version: 1.0\n"
304 "Content-Type: multipart/alternative; boundary=\"__ctdlmultipart__\"\n"
306 "This is a multipart message in MIME format.\n"
308 "--__ctdlmultipart__\n"
309 "Content-type: text/plain\n"
311 "Someone (probably you) has submitted a request to unsubscribe\n"
312 "<%s> from the '%s' mailing list.\n"
314 "Please go here to confirm this request:\n"
315 " %s?room=%s&token=%s&cmd=confirm \n"
317 "If this request has been submitted in error and you do not\n"
318 "wish to unsubscribe from the '%s' mailing list, simply do nothing,\n"
319 "and the request will not be processed.\n"
321 "--__ctdlmultipart__\n"
322 "Content-type: text/html\n"
325 "Someone (probably you) has submitted a request to unsubscribe\n"
326 "<%s> from the <B>%s</B> mailing list.<BR><BR>\n"
327 "Please click here to confirm this request:<BR>\n"
328 "<A HREF=\"%s?room=%s&token=%s&cmd=confirm\">"
329 "%s?room=%s&token=%s&cmd=confirm</A><BR><BR>\n"
330 "If this request has been submitted in error and you do not\n"
331 "wish to unsubscribe from the '%s' mailing list, simply do nothing,\n"
332 "and the request will not be processed.\n"
335 "--__ctdlmultipart__--\n",
338 webpage
, urlroom
, token
,
342 webpage
, urlroom
, token
,
343 webpage
, urlroom
, token
,
347 quickie_message( /* This delivers the message */
352 confirmation_request
,
354 "Please confirm your unsubscribe request"
357 cprintf("%d Unubscription noted; confirmation request sent\n", CIT_OK
);
362 * Confirm a subscribe/unsubscribe request.
364 void do_confirm(char *room
, char *token
) {
365 struct ctdlroom qrbuf
;
368 char line_token
[256];
376 char address_to_unsubscribe
[256];
379 char *holdbuf
= NULL
;
383 strcpy(address_to_unsubscribe
, "");
385 if (getroom(&qrbuf
, room
) != 0) {
386 cprintf("%d There is no list called '%s'\n",
387 ERROR
+ ROOM_NOT_FOUND
, room
);
391 if ((qrbuf
.QRflags2
& QR2_SELFLIST
) == 0) {
393 "does not accept subscribe/unsubscribe requests.\n",
394 ERROR
+ HIGHER_ACCESS_REQUIRED
, qrbuf
.QRname
);
399 * Now start scanning this room's netconfig file for the
402 assoc_file_name(filename
, sizeof filename
, &qrbuf
, ctdl_netcfg_dir
);
403 begin_critical_section(S_NETCONFIGS
);
404 ncfp
= fopen(filename
, "r+");
406 while (line_offset
= ftell(ncfp
),
407 (fgets(buf
, sizeof buf
, ncfp
) != NULL
) ) {
408 buf
[strlen(buf
)-1] = 0;
409 line_length
= strlen(buf
);
410 extract_token(cmd
, buf
, 0, '|', sizeof cmd
);
411 if (!strcasecmp(cmd
, "subpending")) {
412 extract_token(email
, buf
, 1, '|', sizeof email
);
413 extract_token(subtype
, buf
, 2, '|', sizeof subtype
);
414 extract_token(line_token
, buf
, 3, '|', sizeof line_token
);
415 if (!strcasecmp(token
, line_token
)) {
416 if (!strcasecmp(subtype
, "digest")) {
417 safestrncpy(buf
, "digestrecp|", sizeof buf
);
420 safestrncpy(buf
, "listrecp|", sizeof buf
);
424 /* SLEAZY HACK: pad the line out so
425 * it's the same length as the line
428 while (strlen(buf
) < line_length
) {
431 fseek(ncfp
, line_offset
, SEEK_SET
);
432 fprintf(ncfp
, "%s\n", buf
);
436 if (!strcasecmp(cmd
, "unsubpending")) {
437 extract_token(line_token
, buf
, 2, '|', sizeof line_token
);
438 if (!strcasecmp(token
, line_token
)) {
439 extract_token(address_to_unsubscribe
, buf
, 1, '|',
440 sizeof address_to_unsubscribe
);
446 end_critical_section(S_NETCONFIGS
);
449 * If "address_to_unsubscribe" contains something, then we have to
450 * make another pass at the file, stripping out lines referring to
453 if (!IsEmptyStr(address_to_unsubscribe
)) {
454 holdbuf
= malloc(SIZ
);
455 begin_critical_section(S_NETCONFIGS
);
456 ncfp
= fopen(filename
, "r+");
458 while (line_offset
= ftell(ncfp
),
459 (fgets(buf
, sizeof buf
, ncfp
) != NULL
) ) {
460 buf
[strlen(buf
)-1]=0;
461 extract_token(scancmd
, buf
, 0, '|', sizeof scancmd
);
462 extract_token(scanemail
, buf
, 1, '|', sizeof scanemail
);
463 if ( (!strcasecmp(scancmd
, "listrecp"))
464 && (!strcasecmp(scanemail
,
465 address_to_unsubscribe
)) ) {
468 else if ( (!strcasecmp(scancmd
, "digestrecp"))
469 && (!strcasecmp(scanemail
,
470 address_to_unsubscribe
)) ) {
473 else if ( (!strcasecmp(scancmd
, "subpending"))
474 && (!strcasecmp(scanemail
,
475 address_to_unsubscribe
)) ) {
478 else if ( (!strcasecmp(scancmd
, "unsubpending"))
479 && (!strcasecmp(scanemail
,
480 address_to_unsubscribe
)) ) {
483 else { /* Not relevant, so *keep* it! */
484 linelen
= strlen(buf
);
485 holdbuf
= realloc(holdbuf
,
486 (buflen
+ linelen
+ 2) );
487 strcpy(&holdbuf
[buflen
], buf
);
489 strcpy(&holdbuf
[buflen
], "\n");
495 ncfp
= fopen(filename
, "w");
497 fwrite(holdbuf
, buflen
+1, 1, ncfp
);
500 end_critical_section(S_NETCONFIGS
);
505 * Did we do anything useful today?
508 cprintf("%d %d operation(s) confirmed.\n", CIT_OK
, success
);
509 CtdlLogPrintf(CTDL_NOTICE
,
510 "Mailing list: %s %ssubscribed to %s with token %s\n",
512 (!IsEmptyStr(address_to_unsubscribe
)) ? "un" : "",
517 cprintf("%d Invalid token.\n", ERROR
+ ILLEGAL_VALUE
);
525 * process subscribe/unsubscribe requests and confirmations
527 void cmd_subs(char *cmdbuf
) {
530 char room
[ROOMNAMELEN
];
536 extract_token(opr
, cmdbuf
, 0, '|', sizeof opr
);
537 if (!strcasecmp(opr
, "subscribe")) {
538 extract_token(subtype
, cmdbuf
, 3, '|', sizeof subtype
);
539 if ( (strcasecmp(subtype
, "list"))
540 && (strcasecmp(subtype
, "digest")) ) {
541 cprintf("%d Invalid subscription type '%s'\n",
542 ERROR
+ ILLEGAL_VALUE
, subtype
);
545 extract_token(room
, cmdbuf
, 1, '|', sizeof room
);
546 extract_token(email
, cmdbuf
, 2, '|', sizeof email
);
547 extract_token(webpage
, cmdbuf
, 4, '|', sizeof webpage
);
548 do_subscribe(room
, email
, subtype
, webpage
);
551 else if (!strcasecmp(opr
, "unsubscribe")) {
552 extract_token(room
, cmdbuf
, 1, '|', sizeof room
);
553 extract_token(email
, cmdbuf
, 2, '|', sizeof email
);
554 extract_token(webpage
, cmdbuf
, 3, '|', sizeof webpage
);
555 do_unsubscribe(room
, email
, webpage
);
557 else if (!strcasecmp(opr
, "confirm")) {
558 extract_token(room
, cmdbuf
, 1, '|', sizeof room
);
559 extract_token(token
, cmdbuf
, 2, '|', sizeof token
);
560 do_confirm(room
, token
);
563 cprintf("%d Invalid command\n", ERROR
+ ILLEGAL_VALUE
);
571 CTDL_MODULE_INIT(listsub
)
575 CtdlRegisterProtoHook(cmd_subs
, "SUBS", "List subscribe/unsubscribe");
578 /* return our Subversion id for the Log */