]> code.citadel.org Git - citadel.git/blob - citadel/serv_listsub.c
* More work on the self-service subscribe/unsubscribe infrastructure
[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) {
86         struct quickroom qrbuf;
87         FILE *ncfp;
88         char filename[SIZ];
89         char token[SIZ];
90
91         if (getroom(&qrbuf, room) != 0) {
92                 cprintf("%d There is no list called '%s'\n", ERROR, room);
93                 return;
94         }
95
96         listsub_generate_token(token);
97
98         begin_critical_section(S_NETCONFIGS);
99         assoc_file_name(filename, sizeof filename, &qrbuf, "netconfigs");
100         ncfp = fopen(filename, "a");
101         if (ncfp != NULL) {
102                 fprintf(ncfp, "subpending|%s|%s|%s|%ld\n",
103                         email,
104                         subtype,
105                         token,
106                         time(NULL)
107                 );
108                 fclose(ncfp);
109         }
110         end_critical_section(S_NETCONFIGS);
111
112         /* FIXME  --  generate and send the confirmation request */
113
114         cprintf("%d Subscription entered; confirmation request sent\n", CIT_OK);
115
116 }
117
118
119 /*
120  * Confirm a subscribe/unsubscribe request.
121  */
122 void do_confirm(char *room, char *token) {
123         struct quickroom qrbuf;
124         FILE *ncfp;
125         char filename[SIZ];
126         char line_token[SIZ];
127         long line_offset;
128         int line_length;
129         char buf[SIZ];
130         char cmd[SIZ];
131         char email[SIZ];
132         char subtype[SIZ];
133         int success = 0;
134
135         if (getroom(&qrbuf, room) != 0) {
136                 cprintf("%d There is no list called '%s'\n", ERROR, room);
137                 return;
138         }
139
140         begin_critical_section(S_NETCONFIGS);
141         assoc_file_name(filename, sizeof filename, &qrbuf, "netconfigs");
142         ncfp = fopen(filename, "r+");
143         if (ncfp != NULL) {
144                 while (line_offset = ftell(ncfp),
145                       (fgets(buf, sizeof buf, ncfp) != NULL) ) {
146                         buf[strlen(buf)-1] = 0;
147                         line_length = strlen(buf);
148                         extract(cmd, buf, 0);
149                         if (!strcasecmp(cmd, "subpending")) {
150                                 extract(email, buf, 1);
151                                 extract(subtype, buf, 2);
152                                 extract(line_token, buf, 3);
153                                 if (!strcasecmp(token, line_token)) {
154                                         if (!strcasecmp(subtype, "digest")) {
155                                                 strcpy(buf, "digestrecp|");
156                                         }
157                                         else {
158                                                 strcpy(buf, "listrecp|");
159                                         }
160                                         strcat(buf, email);
161                                         strcat(buf, "|");
162                                         /* SLEAZY HACK: pad the line out so
163                                          * it's the same length as the line
164                                          * we're replacing.
165                                          */
166                                         while (strlen(buf) < line_length) {
167                                                 strcat(buf, " ");
168                                         }
169                                         fseek(ncfp, line_offset, SEEK_SET);
170                                         fprintf(ncfp, "%s\n", buf);
171                                         ++success;
172                                 }
173                         }
174                 }
175                 fclose(ncfp);
176         }
177         end_critical_section(S_NETCONFIGS);
178
179         if (success) {
180                 cprintf("%d %d operation(s) confirmed.\n", CIT_OK, success);
181         }
182         else {
183                 cprintf("%d Invalid token.\n", ERROR);
184         }
185
186 }
187
188
189
190 /* 
191  * process subscribe/unsubscribe requests and confirmations
192  */
193 void cmd_subs(char *cmdbuf) {
194
195         char opr[SIZ];
196         char room[SIZ];
197         char email[SIZ];
198         char subtype[SIZ];
199         char token[SIZ];
200
201         extract(opr, cmdbuf, 0);
202         if (!strcasecmp(opr, "subscribe")) {
203                 extract(subtype, cmdbuf, 3);
204                 if ( (strcasecmp(subtype, "list"))
205                    && (strcasecmp(subtype, "digest")) ) {
206                         cprintf("%d Invalid subscription type.\n", ERROR);
207                 }
208                 else {
209                         extract(room, cmdbuf, 1);
210                         extract(email, cmdbuf, 2);
211                         do_subscribe(room, email, subtype);
212                 }
213         }
214         else if (!strcasecmp(opr, "unsubscribe")) {
215                 cprintf("%d not yet implemented\n", ERROR);
216         }
217         else if (!strcasecmp(opr, "confirm")) {
218                 extract(room, cmdbuf, 1);
219                 extract(token, cmdbuf, 2);
220                 do_confirm(room, token);
221         }
222         else {
223                 cprintf("%d Invalid command\n", ERROR);
224         }
225 }
226
227
228 /*
229  * Module entry point
230  */
231 char *Dynamic_Module_Init(void)
232 {
233         CtdlRegisterProtoHook(cmd_subs, "SUBS", "List subscribe/unsubscribe");
234         return "$Id$";
235 }