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, "netconfigs");
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,
161 "Content-type: text/html\nMIME-Version: 1.0\n\n"
163 "Someone (probably you) has submitted a request to subscribe\n"
164 "<%s> to the <B>%s</B> mailing list.<BR><BR>\n"
165 "Please click here to confirm this request:<BR>\n"
166 "<A HREF=\"%s?room=%s&token=%s&cmd=confirm\">"
167 "%s?room=%s&token=%s&cmd=confirm</A><BR><BR>\n"
168 "If this request has been submitted in error and you do not\n"
169 "wish to receive the '%s' mailing list, simply do nothing,\n"
170 "and you will not receive any further mailings.\n"
174 webpage, urlroom, token,
175 webpage, urlroom, token,
179 quickie_message( /* This delivers the message */
183 confirmation_request,
185 "Please confirm your list subscription"
188 cprintf("%d Subscription entered; confirmation request sent\n", CIT_OK);
193 * Enter an unsubscription request
195 void do_unsubscribe(char *room, char *email, char *webpage) {
196 struct ctdlroom qrbuf;
201 char confirmation_request[2048];
202 char urlroom[ROOMNAMELEN];
207 if (getroom(&qrbuf, room) != 0) {
208 cprintf("%d There is no list called '%s'\n",
209 ERROR + ROOM_NOT_FOUND, room);
213 if ((qrbuf.QRflags2 & QR2_SELFLIST) == 0) {
215 "does not accept subscribe/unsubscribe requests.\n",
216 ERROR + HIGHER_ACCESS_REQUIRED, qrbuf.QRname);
220 listsub_generate_token(token);
222 assoc_file_name(filename, sizeof filename, &qrbuf, "netconfigs");
225 * Make sure there's actually a subscription there to remove
227 begin_critical_section(S_NETCONFIGS);
228 ncfp = fopen(filename, "r");
230 while (fgets(buf, sizeof buf, ncfp) != NULL) {
231 buf[strlen(buf)-1] = 0;
232 extract_token(scancmd, buf, 0, '|', sizeof scancmd);
233 extract_token(scanemail, buf, 1, '|', sizeof scanemail);
234 if ((!strcasecmp(scancmd, "listrecp"))
235 || (!strcasecmp(scancmd, "digestrecp"))) {
236 if (!strcasecmp(scanemail, email)) {
243 end_critical_section(S_NETCONFIGS);
245 if (found_sub == 0) {
246 cprintf("%d '%s' is not subscribed to '%s'.\n",
247 ERROR + NO_SUCH_USER,
248 email, qrbuf.QRname);
253 * Ok, now enter the unsubscribe-pending entry.
255 begin_critical_section(S_NETCONFIGS);
256 ncfp = fopen(filename, "a");
258 fprintf(ncfp, "unsubpending|%s|%s|%ld|%s\n",
266 end_critical_section(S_NETCONFIGS);
268 /* Generate and send the confirmation request */
270 urlesc(urlroom, qrbuf.QRname);
272 snprintf(confirmation_request, sizeof confirmation_request,
273 "Content-type: text/html\nMIME-Version: 1.0\n\n"
275 "Someone (probably you) has submitted a request "
277 "<%s> from the <B>%s</B> mailing list.<BR><BR>\n"
278 "Please click here to confirm this request:<BR>\n"
279 "<A HREF=\"%s?room=%s&token=%s&cmd=confirm\">"
280 "%s?room=%s&token=%s&cmd=confirm</A><BR><BR>\n"
281 "If this request has been submitted in error and you do\n"
282 "<i>not</i> wish to unsubscribe from the "
283 "'%s' mailing list, simply do nothing,\n"
284 "and you will remain subscribed to the list.\n"
288 webpage, urlroom, token,
289 webpage, urlroom, token,
293 quickie_message( /* This delivers the message */
297 confirmation_request,
299 "Please confirm your unsubscribe request"
302 cprintf("%d Unubscription noted; confirmation request sent\n", CIT_OK);
307 * Confirm a subscribe/unsubscribe request.
309 void do_confirm(char *room, char *token) {
310 struct ctdlroom qrbuf;
313 char line_token[256];
321 char address_to_unsubscribe[256];
324 char *holdbuf = NULL;
328 strcpy(address_to_unsubscribe, "");
330 if (getroom(&qrbuf, room) != 0) {
331 cprintf("%d There is no list called '%s'\n",
332 ERROR + ROOM_NOT_FOUND, room);
336 if ((qrbuf.QRflags2 & QR2_SELFLIST) == 0) {
338 "does not accept subscribe/unsubscribe requests.\n",
339 ERROR + HIGHER_ACCESS_REQUIRED, qrbuf.QRname);
344 * Now start scanning this room's netconfig file for the
347 assoc_file_name(filename, sizeof filename, &qrbuf, "netconfigs");
348 begin_critical_section(S_NETCONFIGS);
349 ncfp = fopen(filename, "r+");
351 while (line_offset = ftell(ncfp),
352 (fgets(buf, sizeof buf, ncfp) != NULL) ) {
353 buf[strlen(buf)-1] = 0;
354 line_length = strlen(buf);
355 extract_token(cmd, buf, 0, '|', sizeof cmd);
356 if (!strcasecmp(cmd, "subpending")) {
357 extract_token(email, buf, 1, '|', sizeof email);
358 extract_token(subtype, buf, 2, '|', sizeof subtype);
359 extract_token(line_token, buf, 3, '|', sizeof line_token);
360 if (!strcasecmp(token, line_token)) {
361 if (!strcasecmp(subtype, "digest")) {
362 safestrncpy(buf, "digestrecp|", sizeof buf);
365 safestrncpy(buf, "listrecp|", sizeof buf);
369 /* SLEAZY HACK: pad the line out so
370 * it's the same length as the line
373 while (strlen(buf) < line_length) {
376 fseek(ncfp, line_offset, SEEK_SET);
377 fprintf(ncfp, "%s\n", buf);
381 if (!strcasecmp(cmd, "unsubpending")) {
382 extract_token(line_token, buf, 2, '|', sizeof line_token);
383 if (!strcasecmp(token, line_token)) {
384 extract_token(address_to_unsubscribe, buf, 1, '|',
385 sizeof address_to_unsubscribe);
391 end_critical_section(S_NETCONFIGS);
394 * If "address_to_unsubscribe" contains something, then we have to
395 * make another pass at the file, stripping out lines referring to
398 if (strlen(address_to_unsubscribe) > 0) {
399 holdbuf = malloc(SIZ);
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 extract_token(scancmd, buf, 0, '|', sizeof scancmd);
407 extract_token(scanemail, buf, 1, '|', sizeof scanemail);
408 if ( (!strcasecmp(scancmd, "listrecp"))
409 && (!strcasecmp(scanemail,
410 address_to_unsubscribe)) ) {
413 else if ( (!strcasecmp(scancmd, "digestrecp"))
414 && (!strcasecmp(scanemail,
415 address_to_unsubscribe)) ) {
418 else if ( (!strcasecmp(scancmd, "subpending"))
419 && (!strcasecmp(scanemail,
420 address_to_unsubscribe)) ) {
423 else if ( (!strcasecmp(scancmd, "unsubpending"))
424 && (!strcasecmp(scanemail,
425 address_to_unsubscribe)) ) {
428 else { /* Not relevant, so *keep* it! */
429 linelen = strlen(buf);
430 holdbuf = realloc(holdbuf,
431 (buflen + linelen + 2) );
432 strcpy(&holdbuf[buflen], buf);
434 strcpy(&holdbuf[buflen], "\n");
440 ncfp = fopen(filename, "w");
442 fwrite(holdbuf, buflen+1, 1, ncfp);
445 end_critical_section(S_NETCONFIGS);
450 * Did we do anything useful today?
453 cprintf("%d %d operation(s) confirmed.\n", CIT_OK, success);
456 cprintf("%d Invalid token.\n", ERROR + ILLEGAL_VALUE);
464 * process subscribe/unsubscribe requests and confirmations
466 void cmd_subs(char *cmdbuf) {
469 char room[ROOMNAMELEN];
475 extract_token(opr, cmdbuf, 0, '|', sizeof opr);
476 if (!strcasecmp(opr, "subscribe")) {
477 extract_token(subtype, cmdbuf, 3, '|', sizeof subtype);
478 if ( (strcasecmp(subtype, "list"))
479 && (strcasecmp(subtype, "digest")) ) {
480 cprintf("%d Invalid subscription type '%s'\n",
481 ERROR + ILLEGAL_VALUE, subtype);
484 extract_token(room, cmdbuf, 1, '|', sizeof room);
485 extract_token(email, cmdbuf, 2, '|', sizeof email);
486 extract_token(webpage, cmdbuf, 4, '|', sizeof webpage);
487 do_subscribe(room, email, subtype, webpage);
490 else if (!strcasecmp(opr, "unsubscribe")) {
491 extract_token(room, cmdbuf, 1, '|', sizeof room);
492 extract_token(email, cmdbuf, 2, '|', sizeof email);
493 extract_token(webpage, cmdbuf, 3, '|', sizeof webpage);
494 do_unsubscribe(room, email, webpage);
496 else if (!strcasecmp(opr, "confirm")) {
497 extract_token(room, cmdbuf, 1, '|', sizeof room);
498 extract_token(token, cmdbuf, 2, '|', sizeof token);
499 do_confirm(room, token);
502 cprintf("%d Invalid command\n", ERROR + ILLEGAL_VALUE);
510 char *serv_listsub_init(void)
512 CtdlRegisterProtoHook(cmd_subs, "SUBS", "List subscribe/unsubscribe");