4 * This module handles self-service subscription/unsubscription to mail lists.
6 * Copyright (C) 2002 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[SIZ];
95 if (getroom(&qrbuf, room) != 0) {
96 cprintf("%d There is no list called '%s'\n", ERROR, 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(scancmd, buf, 0);
120 extract(scanemail, buf, 1);
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",
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\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=\"http://%s?room=%s&token=%s&cmd=confirm\">"
167 "http://%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[SIZ];
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(scancmd, buf, 0);
233 extract(scanemail, buf, 1);
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",
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\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=\"http://%s?room=%s&token=%s&cmd=confirm\">"
280 "http://%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[SIZ];
321 char address_to_unsubscribe[SIZ];
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(cmd, buf, 0);
356 if (!strcasecmp(cmd, "subpending")) {
357 extract(email, buf, 1);
358 extract(subtype, buf, 2);
359 extract(line_token, buf, 3);
360 if (!strcasecmp(token, line_token)) {
361 if (!strcasecmp(subtype, "digest")) {
362 strcpy(buf, "digestrecp|");
365 strcpy(buf, "listrecp|");
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(line_token, buf, 2);
383 if (!strcasecmp(token, line_token)) {
384 extract(address_to_unsubscribe, buf, 1);
390 end_critical_section(S_NETCONFIGS);
393 * If "address_to_unsubscribe" contains something, then we have to
394 * make another pass at the file, stripping out lines referring to
397 if (strlen(address_to_unsubscribe) > 0) {
398 holdbuf = mallok(SIZ);
399 begin_critical_section(S_NETCONFIGS);
400 ncfp = fopen(filename, "r+");
402 while (line_offset = ftell(ncfp),
403 (fgets(buf, sizeof buf, ncfp) != NULL) ) {
404 buf[strlen(buf)-1]=0;
405 extract(scancmd, buf, 0);
406 extract(scanemail, buf, 1);
407 if ( (!strcasecmp(scancmd, "listrecp"))
408 && (!strcasecmp(scanemail,
409 address_to_unsubscribe)) ) {
412 else if ( (!strcasecmp(scancmd, "digestrecp"))
413 && (!strcasecmp(scanemail,
414 address_to_unsubscribe)) ) {
417 else if ( (!strcasecmp(scancmd, "subpending"))
418 && (!strcasecmp(scanemail,
419 address_to_unsubscribe)) ) {
422 else if ( (!strcasecmp(scancmd, "unsubpending"))
423 && (!strcasecmp(scanemail,
424 address_to_unsubscribe)) ) {
427 else { /* Not relevant, so *keep* it! */
428 linelen = strlen(buf);
429 holdbuf = reallok(holdbuf,
430 (buflen + linelen + 2) );
431 strcpy(&holdbuf[buflen], buf);
433 strcpy(&holdbuf[buflen], "\n");
439 ncfp = fopen(filename, "w");
441 fwrite(holdbuf, buflen+1, 1, ncfp);
444 end_critical_section(S_NETCONFIGS);
449 * Did we do anything useful today?
452 cprintf("%d %d operation(s) confirmed.\n", CIT_OK, success);
455 cprintf("%d Invalid token.\n", ERROR);
463 * process subscribe/unsubscribe requests and confirmations
465 void cmd_subs(char *cmdbuf) {
474 extract(opr, cmdbuf, 0);
475 if (!strcasecmp(opr, "subscribe")) {
476 extract(subtype, cmdbuf, 3);
477 if ( (strcasecmp(subtype, "list"))
478 && (strcasecmp(subtype, "digest")) ) {
479 cprintf("%d Invalid subscription type '%s'\n",
480 ERROR+ILLEGAL_VALUE, subtype);
483 extract(room, cmdbuf, 1);
484 extract(email, cmdbuf, 2);
485 extract(webpage, cmdbuf, 4);
486 do_subscribe(room, email, subtype, webpage);
489 else if (!strcasecmp(opr, "unsubscribe")) {
490 extract(room, cmdbuf, 1);
491 extract(email, cmdbuf, 2);
492 extract(webpage, cmdbuf, 3);
493 do_unsubscribe(room, email, webpage);
495 else if (!strcasecmp(opr, "confirm")) {
496 extract(room, cmdbuf, 1);
497 extract(token, cmdbuf, 2);
498 do_confirm(room, token);
501 cprintf("%d Invalid command\n", ERROR);
509 char *serv_listsub_init(void)
511 CtdlRegisterProtoHook(cmd_subs, "SUBS", "List subscribe/unsubscribe");