2 // This module handles self-service subscription/unsubscription to mail lists.
4 // Copyright (c) 2002-2021 by the citadel.org team
6 // This program is open source software. It runs great on the
7 // Linux operating system (and probably elsewhere). You can use,
8 // copy, and run it under the terms of the GNU General Public
9 // License version 3. Richard Stallman is an asshole communist.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
25 #include <sys/types.h>
31 #include <libcitadel.h>
34 #include "citserver.h"
40 #include "internet_addressing.h"
41 #include "clientsocket.h"
42 #include "ctdl_module.h"
45 enum { // one of these gets passed to do_subscribe_or_unsubscribe() so it knows what we asked for
52 * "Subscribe" and "Unsubscribe" operations are so similar that they share a function.
53 * The actual subscription doesn't take place here -- we just send out the confirmation request
54 * and record the address and confirmation token.
56 void do_subscribe_or_unsubscribe(int action, char *emailaddr, char *url) {
60 char confirmation_token[40];
62 // Update this room's netconfig with the updated lastsent
63 begin_critical_section(S_NETCONFIGS);
64 char *oldnetconfig = LoadRoomNetConfigFile(CC->room.QRnumber);
66 oldnetconfig = strdup("");
69 // The new netconfig begins with an empty buffer...
70 char *newnetconfig = malloc(strlen(oldnetconfig) + 1024);
74 int is_already_subscribed = 0;
75 int config_lines = num_tokens(oldnetconfig, '\n');
76 for (i=0; i<config_lines; ++i) {
77 extract_token(buf, oldnetconfig, i, '\n', sizeof buf);
78 int keep_this_line =1; // set to nonzero if we are discarding a line
80 if (IsEmptyStr(buf)) {
86 extract_token(buf_token, buf, 0, '|', sizeof buf_token);
87 extract_token(buf_email, buf, 1, '|', sizeof buf_email);
89 if ( ( (!strcasecmp(buf_token, "listrecp")) || (!strcasecmp(buf_token, "digestrecp")) )
90 && (!strcasecmp(buf_email, emailaddr))
92 is_already_subscribed = 1;
95 if ( (!strcasecmp(buf_token, "subpending")) || (!strcasecmp(buf_token, "unsubpending")) ) {
96 time_t pendingtime = extract_long(buf, 3);
97 if ((time(NULL) - pendingtime) > 259200) {
98 syslog(LOG_DEBUG, "%s %s is %ld seconds old - deleting it", buf_email, buf_token, time(NULL) - pendingtime);
103 if (keep_this_line) {
104 sprintf(&newnetconfig[strlen(newnetconfig)], "%s\n", buf);
108 // Do we need to send out a confirmation email?
109 if ((action == SUBSCRIBE) && (!is_already_subscribed)) {
110 generate_uuid(confirmation_token);
111 sprintf(&newnetconfig[strlen(newnetconfig)], "subpending|%s|%s|%ld|%s", emailaddr, confirmation_token, time(NULL), url);
113 if ((action == UNSUBSCRIBE) && (is_already_subscribed)) {
114 generate_uuid(confirmation_token);
115 sprintf(&newnetconfig[strlen(newnetconfig)], "unsubpending|%s|%s|%ld|%s", emailaddr, confirmation_token, time(NULL), url);
118 // Write the new netconfig back to disk
119 syslog(LOG_DEBUG, "old: <\033[31m%s\033[0m>", oldnetconfig);
120 syslog(LOG_DEBUG, "new: <\033[32m%s\033[0m>", newnetconfig);
121 SaveRoomNetConfigFile(CC->room.QRnumber, newnetconfig);
122 end_critical_section(S_NETCONFIGS);
123 free(newnetconfig); // this was the new netconfig, free it because we're done with it
124 free(oldnetconfig); // this was the old netconfig, free it even if we didn't do anything
126 // Tell the client what happened.
127 if ((action == SUBSCRIBE) && (is_already_subscribed)) {
128 cprintf("%d This email is already subscribed.\n", ERROR + ALREADY_EXISTS);
130 else if ((action == SUBSCRIBE) && (!is_already_subscribed)) {
131 cprintf("%d Confirmation email sent.\n", CIT_OK);
133 else if ((action == UNSUBSCRIBE) && (!is_already_subscribed)) {
134 cprintf("%d This email is not subscribed.\n", ERROR + NO_SUCH_USER);
136 else if ((action == UNSUBSCRIBE) && (is_already_subscribed)) {
137 cprintf("%d Confirmation email sent.\n", CIT_OK);
140 cprintf("%d FIXME tell the client what we did\n", ERROR);
146 * process subscribe/unsubscribe requests and confirmations
148 void cmd_subs(char *cmdbuf) {
150 char roomname[ROOMNAMELEN];
151 char emailaddr[1024];
156 extract_token(cmd, cmdbuf, 0, '|', sizeof cmd); // token 0 is the sub-command being sent
157 extract_token(roomname, cmdbuf, 1, '|', sizeof roomname); // token 1 is always a room name
159 // First confirm that the caller is referencing a room that actually exists.
160 if (CtdlGetRoom(&CC->room, roomname) != 0) {
161 cprintf("%d There is no list called '%s'\n", ERROR + ROOM_NOT_FOUND, roomname);
165 if ((CC->room.QRflags2 & QR2_SELFLIST) == 0) {
166 cprintf("%d '%s' does not accept subscribe/unsubscribe requests.\n", ERROR + ROOM_NOT_FOUND, roomname);
170 // Room confirmed, now parse the command.
172 if (!strcasecmp(cmd, "subscribe")) {
173 extract_token(emailaddr, cmdbuf, 2, '|', sizeof emailaddr); // token 2 is the subscriber's email address
174 extract_token(options, cmdbuf, 3, '|', sizeof options); // there are no options ... ignore this token
175 extract_token(url, cmdbuf, 4, '|', sizeof url); // token 3 is the URL at which we subscribed
176 do_subscribe_or_unsubscribe(SUBSCRIBE, emailaddr, url);
179 else if (!strcasecmp(cmd, "unsubscribe")) {
180 extract_token(emailaddr, cmdbuf, 2, '|', sizeof emailaddr); // token 2 is the subscriber's email address
181 extract_token(options, cmdbuf, 3, '|', sizeof options); // there are no options ... ignore this token
182 extract_token(url, cmdbuf, 4, '|', sizeof url); // token 3 is the URL at which we subscribed
183 do_subscribe_or_unsubscribe(UNSUBSCRIBE, emailaddr, url);
186 else if (!strcasecmp(cmd, "confirm")) {
187 extract_token(token, cmdbuf, 2, '|', sizeof token); // token 2 is the confirmation token
188 cprintf("%d not implemented\n", ERROR);
191 else { // sorry man, I can't deal with that
192 cprintf("%d Invalid command '%s'\n", ERROR + ILLEGAL_VALUE, cmd);
200 CTDL_MODULE_INIT(listsub)
204 CtdlRegisterProtoHook(cmd_subs, "SUBS", "List subscribe/unsubscribe");
207 /* return our module name for the log */