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 "dynloader.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 encode_base64(buf, sourcebuf);
81 * Enter a subscription request
83 void do_subscribe(char *room, char *email, char *subtype, char *webpage) {
84 struct quickroom qrbuf;
88 char confirmation_request[SIZ];
91 if (getroom(&qrbuf, room) != 0) {
92 cprintf("%d There is no list called '%s'\n", ERROR, room);
96 if ((qrbuf.QRflags2 & QR2_SELFLIST) == 0) {
98 "does not accept subscribe/unsubscribe requests.\n",
99 ERROR+HIGHER_ACCESS_REQUIRED, qrbuf.QRname);
103 listsub_generate_token(token);
105 begin_critical_section(S_NETCONFIGS);
106 assoc_file_name(filename, sizeof filename, &qrbuf, "netconfigs");
107 ncfp = fopen(filename, "a");
109 fprintf(ncfp, "subpending|%s|%s|%s|%ld|%s\n",
118 end_critical_section(S_NETCONFIGS);
120 /* Generate and send the confirmation request */
122 urlesc(urlroom, qrbuf.QRname);
124 snprintf(confirmation_request, sizeof confirmation_request,
125 "Content-type: text/html\n\n"
127 "Someone (probably you) has submitted a request to subscribe\n"
128 "<%s> to the <B>%s</B> mailing list.<BR><BR>\n"
129 "<A HREF=\"http://%s?room=%s&token=%s&cmd=confirm\">"
130 "Please click here to confirm this request.</A><BR><BR>\n"
131 "If this request has been submitted in error and you do not\n"
132 "wish to receive the '%s' mailing list, simply do nothing,\n"
133 "and you will not receive any further mailings.\n"
136 email, qrbuf.QRname, webpage, urlroom, token, qrbuf.QRname
139 quickie_message( /* This delivers the message */
143 confirmation_request,
147 cprintf("%d Subscription entered; confirmation request sent\n", CIT_OK);
152 * Enter an unsubscription request
154 void do_unsubscribe(char *room, char *email, char *webpage) {
155 struct quickroom qrbuf;
160 char confirmation_request[SIZ];
166 if (getroom(&qrbuf, room) != 0) {
167 cprintf("%d There is no list called '%s'\n",
168 ERROR+ROOM_NOT_FOUND, room);
172 if ((qrbuf.QRflags2 & QR2_SELFLIST) == 0) {
174 "does not accept subscribe/unsubscribe requests.\n",
175 ERROR+HIGHER_ACCESS_REQUIRED, qrbuf.QRname);
179 listsub_generate_token(token);
181 assoc_file_name(filename, sizeof filename, &qrbuf, "netconfigs");
184 * Make sure there's actually a subscription there to remove
186 begin_critical_section(S_NETCONFIGS);
187 ncfp = fopen(filename, "r");
189 while (fgets(buf, sizeof buf, ncfp) != NULL) {
190 buf[strlen(buf)-1] = 0;
191 extract(scancmd, buf, 0);
192 extract(scanemail, buf, 1);
193 if ((!strcasecmp(scancmd, "listrecp"))
194 || (!strcasecmp(scancmd, "digestrecp"))) {
195 if (!strcasecmp(scanemail, email)) {
202 end_critical_section(S_NETCONFIGS);
204 if (found_sub == 0) {
205 cprintf("%d '%s' is not subscribed to '%s'.\n",
207 email, qrbuf.QRname);
212 * Ok, now enter the unsubscribe-pending entry.
214 begin_critical_section(S_NETCONFIGS);
215 ncfp = fopen(filename, "a");
217 fprintf(ncfp, "unsubpending|%s|%s|%ld|%s\n",
225 end_critical_section(S_NETCONFIGS);
227 /* Generate and send the confirmation request */
229 urlesc(urlroom, qrbuf.QRname);
231 snprintf(confirmation_request, sizeof confirmation_request,
232 "Content-type: text/html\n\n"
234 "Someone (probably you) has submitted a request "
236 "<%s> from the <B>%s</B> mailing list.<BR><BR>\n"
237 "<A HREF=\"http://%s?room=%s&token=%s&cmd=confirm\">"
238 "Please click here to confirm this request.</A><BR><BR>\n"
239 "If this request has been submitted in error and you do not\n"
240 "wish to unsubscribe from the "
241 "'%s' mailing list, simply do nothing,\n"
242 "and you will remain subscribed to the list.\n"
245 email, qrbuf.QRname, webpage, urlroom, token, qrbuf.QRname
248 quickie_message( /* This delivers the message */
252 confirmation_request,
256 cprintf("%d Unubscription noted; confirmation request sent\n", CIT_OK);
261 * Confirm a subscribe/unsubscribe request.
263 void do_confirm(char *room, char *token) {
264 struct quickroom qrbuf;
267 char line_token[SIZ];
276 if (getroom(&qrbuf, room) != 0) {
277 cprintf("%d There is no list called '%s'\n",
278 ERROR+ROOM_NOT_FOUND, room);
282 if ((qrbuf.QRflags2 & QR2_SELFLIST) == 0) {
284 "does not accept subscribe/unsubscribe requests.\n",
285 ERROR+HIGHER_ACCESS_REQUIRED, qrbuf.QRname);
289 begin_critical_section(S_NETCONFIGS);
290 assoc_file_name(filename, sizeof filename, &qrbuf, "netconfigs");
291 ncfp = fopen(filename, "r+");
293 while (line_offset = ftell(ncfp),
294 (fgets(buf, sizeof buf, ncfp) != NULL) ) {
295 buf[strlen(buf)-1] = 0;
296 line_length = strlen(buf);
297 extract(cmd, buf, 0);
298 if (!strcasecmp(cmd, "subpending")) {
299 extract(email, buf, 1);
300 extract(subtype, buf, 2);
301 extract(line_token, buf, 3);
302 if (!strcasecmp(token, line_token)) {
303 if (!strcasecmp(subtype, "digest")) {
304 strcpy(buf, "digestrecp|");
307 strcpy(buf, "listrecp|");
311 /* SLEAZY HACK: pad the line out so
312 * it's the same length as the line
315 while (strlen(buf) < line_length) {
318 fseek(ncfp, line_offset, SEEK_SET);
319 fprintf(ncfp, "%s\n", buf);
326 end_critical_section(S_NETCONFIGS);
329 cprintf("%d %d operation(s) confirmed.\n", CIT_OK, success);
332 cprintf("%d Invalid token.\n", ERROR);
340 * process subscribe/unsubscribe requests and confirmations
342 void cmd_subs(char *cmdbuf) {
351 extract(opr, cmdbuf, 0);
352 if (!strcasecmp(opr, "subscribe")) {
353 extract(subtype, cmdbuf, 3);
354 if ( (strcasecmp(subtype, "list"))
355 && (strcasecmp(subtype, "digest")) ) {
356 cprintf("%d Invalid subscription type.\n", ERROR);
359 extract(room, cmdbuf, 1);
360 extract(email, cmdbuf, 2);
361 extract(webpage, cmdbuf, 4);
362 do_subscribe(room, email, subtype, webpage);
365 else if (!strcasecmp(opr, "unsubscribe")) {
366 extract(room, cmdbuf, 1);
367 extract(email, cmdbuf, 2);
368 extract(webpage, cmdbuf, 4);
369 do_unsubscribe(room, email, webpage);
371 else if (!strcasecmp(opr, "confirm")) {
372 extract(room, cmdbuf, 1);
373 extract(token, cmdbuf, 2);
374 do_confirm(room, token);
377 cprintf("%d Invalid command\n", ERROR);
385 char *Dynamic_Module_Init(void)
387 CtdlRegisterProtoHook(cmd_subs, "SUBS", "List subscribe/unsubscribe");