]> code.citadel.org Git - citadel.git/blob - citadel/serv_listsub.c
* WebCit list subscription integration
[citadel.git] / citadel / serv_listsub.c
1 /*
2  * $Id$
3  *
4  * This module handles self-service subscription/unsubscription to mail lists.
5  *
6  * Copyright (C) 2002 by Art Cancro and others.
7  * This code is released under the terms of the GNU General Public License.
8  *
9  */
10
11 #include "sysdep.h"
12 #include <stdlib.h>
13 #include <unistd.h>
14 #include <stdio.h>
15 #include <fcntl.h>
16 #include <ctype.h>
17 #include <signal.h>
18 #include <pwd.h>
19 #include <errno.h>
20 #include <sys/types.h>
21 #include <dirent.h>
22 #if TIME_WITH_SYS_TIME
23 # include <sys/time.h>
24 # include <time.h>
25 #else
26 # if HAVE_SYS_TIME_H
27 #  include <sys/time.h>
28 # else
29 #  include <time.h>
30 # endif
31 #endif
32
33 #include <sys/wait.h>
34 #include <string.h>
35 #include <limits.h>
36 #include "citadel.h"
37 #include "server.h"
38 #include "sysdep_decls.h"
39 #include "citserver.h"
40 #include "support.h"
41 #include "config.h"
42 #include "dynloader.h"
43 #include "room_ops.h"
44 #include "user_ops.h"
45 #include "policy.h"
46 #include "database.h"
47 #include "msgbase.h"
48 #include "tools.h"
49 #include "internet_addressing.h"
50 #include "serv_network.h"
51 #include "clientsocket.h"
52 #include "file_ops.h"
53
54 #ifndef HAVE_SNPRINTF
55 #include "snprintf.h"
56 #endif
57
58
59 /*
60  * Generate a randomizationalisticized token to use for authentication of
61  * a subscribe or unsubscribe request.
62  */
63 void listsub_generate_token(char *buf) {
64         char sourcebuf[SIZ];
65         static int seq = 0;
66
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
69          * and unique.
70          */
71         sprintf(sourcebuf, "%d%d%ld",
72                 ++seq,
73                 getpid(),
74                 time(NULL)
75         );
76
77         /* Convert it to base64 so it looks cool */     
78         encode_base64(buf, sourcebuf);
79 }
80
81
82 /*
83  * Enter a subscription request
84  */
85 void do_subscribe(char *room, char *email, char *subtype, char *webpage) {
86         struct quickroom qrbuf;
87         FILE *ncfp;
88         char filename[SIZ];
89         char token[SIZ];
90         char confirmation_request[SIZ];
91
92         if (getroom(&qrbuf, room) != 0) {
93                 cprintf("%d There is no list called '%s'\n", ERROR, room);
94                 return;
95         }
96
97         listsub_generate_token(token);
98
99         begin_critical_section(S_NETCONFIGS);
100         assoc_file_name(filename, sizeof filename, &qrbuf, "netconfigs");
101         ncfp = fopen(filename, "a");
102         if (ncfp != NULL) {
103                 fprintf(ncfp, "subpending|%s|%s|%s|%ld|%s\n",
104                         email,
105                         subtype,
106                         token,
107                         time(NULL),
108                         webpage
109                 );
110                 fclose(ncfp);
111         }
112         end_critical_section(S_NETCONFIGS);
113
114         /* Generate and send the confirmation request */
115
116         snprintf(confirmation_request, sizeof confirmation_request,
117                 "Someone (probably you) has submitted a request to subscribe\n"
118                 "<%s> to the '%s' mailing list.\n\n"
119                 "In order to confirm this subscription request, please\n"
120                 "point your web browser at the following location:\n\n"
121                 "http://%s?room=%s&token=%s\n\n"
122                 "If this request has been submitted in error and you do not\n"
123                 "wish to receive the '%s' mailing list, simply do nothing,\n"
124                 "and you will not receive any further mailings.\n",
125
126                 email, qrbuf.QRname, webpage, qrbuf.QRname, token, qrbuf.QRname
127         );
128
129         quickie_message(
130                 "Citadel",
131                 email,
132                 qrbuf.QRname,
133                 confirmation_request
134         );
135
136         cprintf("%d Subscription entered; confirmation request sent\n", CIT_OK);
137
138 }
139
140
141 /*
142  * Confirm a subscribe/unsubscribe request.
143  */
144 void do_confirm(char *room, char *token) {
145         struct quickroom qrbuf;
146         FILE *ncfp;
147         char filename[SIZ];
148         char line_token[SIZ];
149         long line_offset;
150         int line_length;
151         char buf[SIZ];
152         char cmd[SIZ];
153         char email[SIZ];
154         char subtype[SIZ];
155         int success = 0;
156
157         if (getroom(&qrbuf, room) != 0) {
158                 cprintf("%d There is no list called '%s'\n", ERROR, room);
159                 return;
160         }
161
162         begin_critical_section(S_NETCONFIGS);
163         assoc_file_name(filename, sizeof filename, &qrbuf, "netconfigs");
164         ncfp = fopen(filename, "r+");
165         if (ncfp != NULL) {
166                 while (line_offset = ftell(ncfp),
167                       (fgets(buf, sizeof buf, ncfp) != NULL) ) {
168                         buf[strlen(buf)-1] = 0;
169                         line_length = strlen(buf);
170                         extract(cmd, buf, 0);
171                         if (!strcasecmp(cmd, "subpending")) {
172                                 extract(email, buf, 1);
173                                 extract(subtype, buf, 2);
174                                 extract(line_token, buf, 3);
175                                 if (!strcasecmp(token, line_token)) {
176                                         if (!strcasecmp(subtype, "digest")) {
177                                                 strcpy(buf, "digestrecp|");
178                                         }
179                                         else {
180                                                 strcpy(buf, "listrecp|");
181                                         }
182                                         strcat(buf, email);
183                                         strcat(buf, "|");
184                                         /* SLEAZY HACK: pad the line out so
185                                          * it's the same length as the line
186                                          * we're replacing.
187                                          */
188                                         while (strlen(buf) < line_length) {
189                                                 strcat(buf, " ");
190                                         }
191                                         fseek(ncfp, line_offset, SEEK_SET);
192                                         fprintf(ncfp, "%s\n", buf);
193                                         ++success;
194                                 }
195                         }
196                 }
197                 fclose(ncfp);
198         }
199         end_critical_section(S_NETCONFIGS);
200
201         if (success) {
202                 cprintf("%d %d operation(s) confirmed.\n", CIT_OK, success);
203         }
204         else {
205                 cprintf("%d Invalid token.\n", ERROR);
206         }
207
208 }
209
210
211
212 /* 
213  * process subscribe/unsubscribe requests and confirmations
214  */
215 void cmd_subs(char *cmdbuf) {
216
217         char opr[SIZ];
218         char room[SIZ];
219         char email[SIZ];
220         char subtype[SIZ];
221         char token[SIZ];
222         char webpage[SIZ];
223
224         extract(opr, cmdbuf, 0);
225         if (!strcasecmp(opr, "subscribe")) {
226                 extract(subtype, cmdbuf, 3);
227                 if ( (strcasecmp(subtype, "list"))
228                    && (strcasecmp(subtype, "digest")) ) {
229                         cprintf("%d Invalid subscription type.\n", ERROR);
230                 }
231                 else {
232                         extract(room, cmdbuf, 1);
233                         extract(email, cmdbuf, 2);
234                         extract(webpage, cmdbuf, 3);
235                         do_subscribe(room, email, subtype, webpage);
236                 }
237         }
238         else if (!strcasecmp(opr, "unsubscribe")) {
239                 cprintf("%d not yet implemented\n", ERROR);
240         }
241         else if (!strcasecmp(opr, "confirm")) {
242                 extract(room, cmdbuf, 1);
243                 extract(token, cmdbuf, 2);
244                 do_confirm(room, token);
245         }
246         else {
247                 cprintf("%d Invalid command\n", ERROR);
248         }
249 }
250
251
252 /*
253  * Module entry point
254  */
255 char *Dynamic_Module_Init(void)
256 {
257         CtdlRegisterProtoHook(cmd_subs, "SUBS", "List subscribe/unsubscribe");
258         return "$Id$";
259 }