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>
38 #include "sysdep_decls.h"
39 #include "citserver.h"
42 #include "serv_extensions.h"
49 #include "internet_addressing.h"
50 #include "serv_network.h"
51 #include "clientsocket.h"
60 * Generate a randomizationalisticized token to use for authentication of
61 * a subscribe or unsubscribe request.
63 void listsub_generate_token(char *buf) {
67 /* Theo, please sit down and shut up. This key doesn't have to be
68 * tinfoil-hat secure, it just needs to be reasonably unguessable
71 sprintf(sourcebuf, "%lx",
72 (long) (++seq + getpid() + time(NULL))
75 /* Convert it to base64 so it looks cool */
76 CtdlEncodeBase64(buf, sourcebuf, strlen(sourcebuf));
81 * Enter a subscription request
83 void do_subscribe(char *room, char *email, char *subtype, char *webpage) {
84 struct ctdlroom qrbuf;
88 char confirmation_request[2048];
90 char urlroom[ROOMNAMELEN];
95 if (getroom(&qrbuf, room) != 0) {
96 cprintf("%d There is no list called '%s'\n", ERROR + ROOM_NOT_FOUND, room);
100 if ((qrbuf.QRflags2 & QR2_SELFLIST) == 0) {
102 "does not accept subscribe/unsubscribe requests.\n",
103 ERROR + HIGHER_ACCESS_REQUIRED, qrbuf.QRname);
107 listsub_generate_token(token);
109 assoc_file_name(filename, sizeof filename, &qrbuf, ctdl_netcfg_dir);
112 * Make sure the requested address isn't already subscribed
114 begin_critical_section(S_NETCONFIGS);
115 ncfp = fopen(filename, "r");
117 while (fgets(buf, sizeof buf, ncfp) != NULL) {
118 buf[strlen(buf)-1] = 0;
119 extract_token(scancmd, buf, 0, '|', sizeof scancmd);
120 extract_token(scanemail, buf, 1, '|', sizeof scanemail);
121 if ((!strcasecmp(scancmd, "listrecp"))
122 || (!strcasecmp(scancmd, "digestrecp"))) {
123 if (!strcasecmp(scanemail, email)) {
130 end_critical_section(S_NETCONFIGS);
132 if (found_sub != 0) {
133 cprintf("%d '%s' is already subscribed to '%s'.\n",
134 ERROR + ALREADY_EXISTS,
135 email, qrbuf.QRname);
140 * Now add it to the file
142 begin_critical_section(S_NETCONFIGS);
143 ncfp = fopen(filename, "a");
145 fprintf(ncfp, "subpending|%s|%s|%s|%ld|%s\n",
154 end_critical_section(S_NETCONFIGS);
156 /* Generate and send the confirmation request */
158 urlesc(urlroom, qrbuf.QRname);
160 snprintf(confirmation_request, sizeof confirmation_request,
162 "MIME-Version: 1.0\n"
163 "Content-Type: multipart/alternative; boundary=\"__ctdlmultipart__\"\n"
165 "This is a multipart message in MIME format.\n"
167 "--__ctdlmultipart__\n"
168 "Content-type: text/plain\n"
170 "Someone (probably you) has submitted a request to subscribe\n"
171 "<%s> to the '%s' mailing list.\n"
173 "Please go here to confirm this request:\n"
174 " %s?room=%s&token=%s&cmd=confirm \n"
176 "If this request has been submitted in error and you do not\n"
177 "wish to receive the '%s' mailing list, simply do nothing,\n"
178 "and you will not receive any further mailings.\n"
180 "--__ctdlmultipart__\n"
181 "Content-type: text/html\n"
183 "Someone (probably you) has submitted a request to subscribe\n"
184 "<%s> to the <B>%s</B> mailing list.<BR><BR>\n"
185 "Please click here to confirm this request:<BR>\n"
186 "<A HREF=\"%s?room=%s&token=%s&cmd=confirm\">"
187 "%s?room=%s&token=%s&cmd=confirm</A><BR><BR>\n"
188 "If this request has been submitted in error and you do not\n"
189 "wish to receive the '%s' mailing list, simply do nothing,\n"
190 "and you will not receive any further mailings.\n"
193 "--__ctdlmultipart__--\n",
196 webpage, urlroom, token,
200 webpage, urlroom, token,
201 webpage, urlroom, token,
205 quickie_message( /* This delivers the message */
209 confirmation_request,
211 "Please confirm your list subscription"
214 cprintf("%d Subscription entered; confirmation request sent\n", CIT_OK);
219 * Enter an unsubscription request
221 void do_unsubscribe(char *room, char *email, char *webpage) {
222 struct ctdlroom qrbuf;
227 char confirmation_request[2048];
228 char urlroom[ROOMNAMELEN];
233 if (getroom(&qrbuf, room) != 0) {
234 cprintf("%d There is no list called '%s'\n",
235 ERROR + ROOM_NOT_FOUND, room);
239 if ((qrbuf.QRflags2 & QR2_SELFLIST) == 0) {
241 "does not accept subscribe/unsubscribe requests.\n",
242 ERROR + HIGHER_ACCESS_REQUIRED, qrbuf.QRname);
246 listsub_generate_token(token);
248 assoc_file_name(filename, sizeof filename, &qrbuf, ctdl_netcfg_dir);
251 * Make sure there's actually a subscription there to remove
253 begin_critical_section(S_NETCONFIGS);
254 ncfp = fopen(filename, "r");
256 while (fgets(buf, sizeof buf, ncfp) != NULL) {
257 buf[strlen(buf)-1] = 0;
258 extract_token(scancmd, buf, 0, '|', sizeof scancmd);
259 extract_token(scanemail, buf, 1, '|', sizeof scanemail);
260 if ((!strcasecmp(scancmd, "listrecp"))
261 || (!strcasecmp(scancmd, "digestrecp"))) {
262 if (!strcasecmp(scanemail, email)) {
269 end_critical_section(S_NETCONFIGS);
271 if (found_sub == 0) {
272 cprintf("%d '%s' is not subscribed to '%s'.\n",
273 ERROR + NO_SUCH_USER,
274 email, qrbuf.QRname);
279 * Ok, now enter the unsubscribe-pending entry.
281 begin_critical_section(S_NETCONFIGS);
282 ncfp = fopen(filename, "a");
284 fprintf(ncfp, "unsubpending|%s|%s|%ld|%s\n",
292 end_critical_section(S_NETCONFIGS);
294 /* Generate and send the confirmation request */
296 urlesc(urlroom, qrbuf.QRname);
298 snprintf(confirmation_request, sizeof confirmation_request,
300 "MIME-Version: 1.0\n"
301 "Content-Type: multipart/alternative; boundary=\"__ctdlmultipart__\"\n"
303 "This is a multipart message in MIME format.\n"
305 "--__ctdlmultipart__\n"
306 "Content-type: text/plain\n"
308 "Someone (probably you) has submitted a request to unsubscribe\n"
309 "<%s> from the '%s' mailing list.\n"
311 "Please go here to confirm this request:\n"
312 " %s?room=%s&token=%s&cmd=confirm \n"
314 "If this request has been submitted in error and you do not\n"
315 "wish to unsubscribe from the '%s' mailing list, simply do nothing,\n"
316 "and the request will not be processed.\n"
318 "--__ctdlmultipart__\n"
319 "Content-type: text/html\n"
321 "Someone (probably you) has submitted a request to unsubscribe\n"
322 "<%s> from the <B>%s</B> mailing list.<BR><BR>\n"
323 "Please click here to confirm this request:<BR>\n"
324 "<A HREF=\"%s?room=%s&token=%s&cmd=confirm\">"
325 "%s?room=%s&token=%s&cmd=confirm</A><BR><BR>\n"
326 "If this request has been submitted in error and you do not\n"
327 "wish to unsubscribe from the '%s' mailing list, simply do nothing,\n"
328 "and the request will not be processed.\n"
331 "--__ctdlmultipart__--\n",
334 webpage, urlroom, token,
338 webpage, urlroom, token,
339 webpage, urlroom, token,
343 quickie_message( /* This delivers the message */
347 confirmation_request,
349 "Please confirm your unsubscribe request"
352 cprintf("%d Unubscription noted; confirmation request sent\n", CIT_OK);
357 * Confirm a subscribe/unsubscribe request.
359 void do_confirm(char *room, char *token) {
360 struct ctdlroom qrbuf;
363 char line_token[256];
371 char address_to_unsubscribe[256];
374 char *holdbuf = NULL;
378 strcpy(address_to_unsubscribe, "");
380 if (getroom(&qrbuf, room) != 0) {
381 cprintf("%d There is no list called '%s'\n",
382 ERROR + ROOM_NOT_FOUND, room);
386 if ((qrbuf.QRflags2 & QR2_SELFLIST) == 0) {
388 "does not accept subscribe/unsubscribe requests.\n",
389 ERROR + HIGHER_ACCESS_REQUIRED, qrbuf.QRname);
394 * Now start scanning this room's netconfig file for the
397 assoc_file_name(filename, sizeof filename, &qrbuf, ctdl_netcfg_dir);
398 begin_critical_section(S_NETCONFIGS);
399 ncfp = fopen(filename, "r+");
401 while (line_offset = ftell(ncfp),
402 (fgets(buf, sizeof buf, ncfp) != NULL) ) {
403 buf[strlen(buf)-1] = 0;
404 line_length = strlen(buf);
405 extract_token(cmd, buf, 0, '|', sizeof cmd);
406 if (!strcasecmp(cmd, "subpending")) {
407 extract_token(email, buf, 1, '|', sizeof email);
408 extract_token(subtype, buf, 2, '|', sizeof subtype);
409 extract_token(line_token, buf, 3, '|', sizeof line_token);
410 if (!strcasecmp(token, line_token)) {
411 if (!strcasecmp(subtype, "digest")) {
412 safestrncpy(buf, "digestrecp|", sizeof buf);
415 safestrncpy(buf, "listrecp|", sizeof buf);
419 /* SLEAZY HACK: pad the line out so
420 * it's the same length as the line
423 while (strlen(buf) < line_length) {
426 fseek(ncfp, line_offset, SEEK_SET);
427 fprintf(ncfp, "%s\n", buf);
431 if (!strcasecmp(cmd, "unsubpending")) {
432 extract_token(line_token, buf, 2, '|', sizeof line_token);
433 if (!strcasecmp(token, line_token)) {
434 extract_token(address_to_unsubscribe, buf, 1, '|',
435 sizeof address_to_unsubscribe);
441 end_critical_section(S_NETCONFIGS);
444 * If "address_to_unsubscribe" contains something, then we have to
445 * make another pass at the file, stripping out lines referring to
448 if (strlen(address_to_unsubscribe) > 0) {
449 holdbuf = malloc(SIZ);
450 begin_critical_section(S_NETCONFIGS);
451 ncfp = fopen(filename, "r+");
453 while (line_offset = ftell(ncfp),
454 (fgets(buf, sizeof buf, ncfp) != NULL) ) {
455 buf[strlen(buf)-1]=0;
456 extract_token(scancmd, buf, 0, '|', sizeof scancmd);
457 extract_token(scanemail, buf, 1, '|', sizeof scanemail);
458 if ( (!strcasecmp(scancmd, "listrecp"))
459 && (!strcasecmp(scanemail,
460 address_to_unsubscribe)) ) {
463 else if ( (!strcasecmp(scancmd, "digestrecp"))
464 && (!strcasecmp(scanemail,
465 address_to_unsubscribe)) ) {
468 else if ( (!strcasecmp(scancmd, "subpending"))
469 && (!strcasecmp(scanemail,
470 address_to_unsubscribe)) ) {
473 else if ( (!strcasecmp(scancmd, "unsubpending"))
474 && (!strcasecmp(scanemail,
475 address_to_unsubscribe)) ) {
478 else { /* Not relevant, so *keep* it! */
479 linelen = strlen(buf);
480 holdbuf = realloc(holdbuf,
481 (buflen + linelen + 2) );
482 strcpy(&holdbuf[buflen], buf);
484 strcpy(&holdbuf[buflen], "\n");
490 ncfp = fopen(filename, "w");
492 fwrite(holdbuf, buflen+1, 1, ncfp);
495 end_critical_section(S_NETCONFIGS);
500 * Did we do anything useful today?
503 cprintf("%d %d operation(s) confirmed.\n", CIT_OK, success);
504 lprintf(CTDL_NOTICE, "Mailing list: %s %ssubscribed to %s with token %s\n", email, (strlen(address_to_unsubscribe) > 0) ? "un" : "", room, token);
507 cprintf("%d Invalid token.\n", ERROR + ILLEGAL_VALUE);
515 * process subscribe/unsubscribe requests and confirmations
517 void cmd_subs(char *cmdbuf) {
520 char room[ROOMNAMELEN];
526 extract_token(opr, cmdbuf, 0, '|', sizeof opr);
527 if (!strcasecmp(opr, "subscribe")) {
528 extract_token(subtype, cmdbuf, 3, '|', sizeof subtype);
529 if ( (strcasecmp(subtype, "list"))
530 && (strcasecmp(subtype, "digest")) ) {
531 cprintf("%d Invalid subscription type '%s'\n",
532 ERROR + ILLEGAL_VALUE, subtype);
535 extract_token(room, cmdbuf, 1, '|', sizeof room);
536 extract_token(email, cmdbuf, 2, '|', sizeof email);
537 extract_token(webpage, cmdbuf, 4, '|', sizeof webpage);
538 do_subscribe(room, email, subtype, webpage);
541 else if (!strcasecmp(opr, "unsubscribe")) {
542 extract_token(room, cmdbuf, 1, '|', sizeof room);
543 extract_token(email, cmdbuf, 2, '|', sizeof email);
544 extract_token(webpage, cmdbuf, 3, '|', sizeof webpage);
545 do_unsubscribe(room, email, webpage);
547 else if (!strcasecmp(opr, "confirm")) {
548 extract_token(room, cmdbuf, 1, '|', sizeof room);
549 extract_token(token, cmdbuf, 2, '|', sizeof token);
550 do_confirm(room, token);
553 cprintf("%d Invalid command\n", ERROR + ILLEGAL_VALUE);
561 char *serv_listsub_init(void)
563 CtdlRegisterProtoHook(cmd_subs, "SUBS", "List subscribe/unsubscribe");