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 */
210 confirmation_request,
212 "Please confirm your list subscription"
215 cprintf("%d Subscription entered; confirmation request sent\n", CIT_OK);
220 * Enter an unsubscription request
222 void do_unsubscribe(char *room, char *email, char *webpage) {
223 struct ctdlroom qrbuf;
228 char confirmation_request[2048];
229 char urlroom[ROOMNAMELEN];
234 if (getroom(&qrbuf, room) != 0) {
235 cprintf("%d There is no list called '%s'\n",
236 ERROR + ROOM_NOT_FOUND, room);
240 if ((qrbuf.QRflags2 & QR2_SELFLIST) == 0) {
242 "does not accept subscribe/unsubscribe requests.\n",
243 ERROR + HIGHER_ACCESS_REQUIRED, qrbuf.QRname);
247 listsub_generate_token(token);
249 assoc_file_name(filename, sizeof filename, &qrbuf, ctdl_netcfg_dir);
252 * Make sure there's actually a subscription there to remove
254 begin_critical_section(S_NETCONFIGS);
255 ncfp = fopen(filename, "r");
257 while (fgets(buf, sizeof buf, ncfp) != NULL) {
258 buf[strlen(buf)-1] = 0;
259 extract_token(scancmd, buf, 0, '|', sizeof scancmd);
260 extract_token(scanemail, buf, 1, '|', sizeof scanemail);
261 if ((!strcasecmp(scancmd, "listrecp"))
262 || (!strcasecmp(scancmd, "digestrecp"))) {
263 if (!strcasecmp(scanemail, email)) {
270 end_critical_section(S_NETCONFIGS);
272 if (found_sub == 0) {
273 cprintf("%d '%s' is not subscribed to '%s'.\n",
274 ERROR + NO_SUCH_USER,
275 email, qrbuf.QRname);
280 * Ok, now enter the unsubscribe-pending entry.
282 begin_critical_section(S_NETCONFIGS);
283 ncfp = fopen(filename, "a");
285 fprintf(ncfp, "unsubpending|%s|%s|%ld|%s\n",
293 end_critical_section(S_NETCONFIGS);
295 /* Generate and send the confirmation request */
297 urlesc(urlroom, qrbuf.QRname);
299 snprintf(confirmation_request, sizeof confirmation_request,
301 "MIME-Version: 1.0\n"
302 "Content-Type: multipart/alternative; boundary=\"__ctdlmultipart__\"\n"
304 "This is a multipart message in MIME format.\n"
306 "--__ctdlmultipart__\n"
307 "Content-type: text/plain\n"
309 "Someone (probably you) has submitted a request to unsubscribe\n"
310 "<%s> from the '%s' mailing list.\n"
312 "Please go here to confirm this request:\n"
313 " %s?room=%s&token=%s&cmd=confirm \n"
315 "If this request has been submitted in error and you do not\n"
316 "wish to unsubscribe from the '%s' mailing list, simply do nothing,\n"
317 "and the request will not be processed.\n"
319 "--__ctdlmultipart__\n"
320 "Content-type: text/html\n"
322 "Someone (probably you) has submitted a request to unsubscribe\n"
323 "<%s> from the <B>%s</B> mailing list.<BR><BR>\n"
324 "Please click here to confirm this request:<BR>\n"
325 "<A HREF=\"%s?room=%s&token=%s&cmd=confirm\">"
326 "%s?room=%s&token=%s&cmd=confirm</A><BR><BR>\n"
327 "If this request has been submitted in error and you do not\n"
328 "wish to unsubscribe from the '%s' mailing list, simply do nothing,\n"
329 "and the request will not be processed.\n"
332 "--__ctdlmultipart__--\n",
335 webpage, urlroom, token,
339 webpage, urlroom, token,
340 webpage, urlroom, token,
344 quickie_message( /* This delivers the message */
349 confirmation_request,
351 "Please confirm your unsubscribe request"
354 cprintf("%d Unubscription noted; confirmation request sent\n", CIT_OK);
359 * Confirm a subscribe/unsubscribe request.
361 void do_confirm(char *room, char *token) {
362 struct ctdlroom qrbuf;
365 char line_token[256];
373 char address_to_unsubscribe[256];
376 char *holdbuf = NULL;
380 strcpy(address_to_unsubscribe, "");
382 if (getroom(&qrbuf, room) != 0) {
383 cprintf("%d There is no list called '%s'\n",
384 ERROR + ROOM_NOT_FOUND, room);
388 if ((qrbuf.QRflags2 & QR2_SELFLIST) == 0) {
390 "does not accept subscribe/unsubscribe requests.\n",
391 ERROR + HIGHER_ACCESS_REQUIRED, qrbuf.QRname);
396 * Now start scanning this room's netconfig file for the
399 assoc_file_name(filename, sizeof filename, &qrbuf, ctdl_netcfg_dir);
400 begin_critical_section(S_NETCONFIGS);
401 ncfp = fopen(filename, "r+");
403 while (line_offset = ftell(ncfp),
404 (fgets(buf, sizeof buf, ncfp) != NULL) ) {
405 buf[strlen(buf)-1] = 0;
406 line_length = strlen(buf);
407 extract_token(cmd, buf, 0, '|', sizeof cmd);
408 if (!strcasecmp(cmd, "subpending")) {
409 extract_token(email, buf, 1, '|', sizeof email);
410 extract_token(subtype, buf, 2, '|', sizeof subtype);
411 extract_token(line_token, buf, 3, '|', sizeof line_token);
412 if (!strcasecmp(token, line_token)) {
413 if (!strcasecmp(subtype, "digest")) {
414 safestrncpy(buf, "digestrecp|", sizeof buf);
417 safestrncpy(buf, "listrecp|", sizeof buf);
421 /* SLEAZY HACK: pad the line out so
422 * it's the same length as the line
425 while (strlen(buf) < line_length) {
428 fseek(ncfp, line_offset, SEEK_SET);
429 fprintf(ncfp, "%s\n", buf);
433 if (!strcasecmp(cmd, "unsubpending")) {
434 extract_token(line_token, buf, 2, '|', sizeof line_token);
435 if (!strcasecmp(token, line_token)) {
436 extract_token(address_to_unsubscribe, buf, 1, '|',
437 sizeof address_to_unsubscribe);
443 end_critical_section(S_NETCONFIGS);
446 * If "address_to_unsubscribe" contains something, then we have to
447 * make another pass at the file, stripping out lines referring to
450 if (strlen(address_to_unsubscribe) > 0) {
451 holdbuf = malloc(SIZ);
452 begin_critical_section(S_NETCONFIGS);
453 ncfp = fopen(filename, "r+");
455 while (line_offset = ftell(ncfp),
456 (fgets(buf, sizeof buf, ncfp) != NULL) ) {
457 buf[strlen(buf)-1]=0;
458 extract_token(scancmd, buf, 0, '|', sizeof scancmd);
459 extract_token(scanemail, buf, 1, '|', sizeof scanemail);
460 if ( (!strcasecmp(scancmd, "listrecp"))
461 && (!strcasecmp(scanemail,
462 address_to_unsubscribe)) ) {
465 else if ( (!strcasecmp(scancmd, "digestrecp"))
466 && (!strcasecmp(scanemail,
467 address_to_unsubscribe)) ) {
470 else if ( (!strcasecmp(scancmd, "subpending"))
471 && (!strcasecmp(scanemail,
472 address_to_unsubscribe)) ) {
475 else if ( (!strcasecmp(scancmd, "unsubpending"))
476 && (!strcasecmp(scanemail,
477 address_to_unsubscribe)) ) {
480 else { /* Not relevant, so *keep* it! */
481 linelen = strlen(buf);
482 holdbuf = realloc(holdbuf,
483 (buflen + linelen + 2) );
484 strcpy(&holdbuf[buflen], buf);
486 strcpy(&holdbuf[buflen], "\n");
492 ncfp = fopen(filename, "w");
494 fwrite(holdbuf, buflen+1, 1, ncfp);
497 end_critical_section(S_NETCONFIGS);
502 * Did we do anything useful today?
505 cprintf("%d %d operation(s) confirmed.\n", CIT_OK, success);
506 lprintf(CTDL_NOTICE, "Mailing list: %s %ssubscribed to %s with token %s\n", email, (strlen(address_to_unsubscribe) > 0) ? "un" : "", room, token);
509 cprintf("%d Invalid token.\n", ERROR + ILLEGAL_VALUE);
517 * process subscribe/unsubscribe requests and confirmations
519 void cmd_subs(char *cmdbuf) {
522 char room[ROOMNAMELEN];
528 extract_token(opr, cmdbuf, 0, '|', sizeof opr);
529 if (!strcasecmp(opr, "subscribe")) {
530 extract_token(subtype, cmdbuf, 3, '|', sizeof subtype);
531 if ( (strcasecmp(subtype, "list"))
532 && (strcasecmp(subtype, "digest")) ) {
533 cprintf("%d Invalid subscription type '%s'\n",
534 ERROR + ILLEGAL_VALUE, subtype);
537 extract_token(room, cmdbuf, 1, '|', sizeof room);
538 extract_token(email, cmdbuf, 2, '|', sizeof email);
539 extract_token(webpage, cmdbuf, 4, '|', sizeof webpage);
540 do_subscribe(room, email, subtype, webpage);
543 else if (!strcasecmp(opr, "unsubscribe")) {
544 extract_token(room, cmdbuf, 1, '|', sizeof room);
545 extract_token(email, cmdbuf, 2, '|', sizeof email);
546 extract_token(webpage, cmdbuf, 3, '|', sizeof webpage);
547 do_unsubscribe(room, email, webpage);
549 else if (!strcasecmp(opr, "confirm")) {
550 extract_token(room, cmdbuf, 1, '|', sizeof room);
551 extract_token(token, cmdbuf, 2, '|', sizeof token);
552 do_confirm(room, token);
555 cprintf("%d Invalid command\n", ERROR + ILLEGAL_VALUE);
563 char *serv_listsub_init(void)
565 CtdlRegisterProtoHook(cmd_subs, "SUBS", "List subscribe/unsubscribe");