9896b5811865595e37db36f43e421239ffd7d47f
[citadel.git] / webcit / listsub.c
1 /*
2  * Web forms for handling mailing list subscribe/unsubscribe requests.
3  *
4  * Copyright (c) 1996-2012 by the citadel.org team
5  *
6  * This program is open source software.  You can redistribute it and/or
7  * modify it under the terms of the GNU General Public License, version 3.
8  * 
9  * 
10  *
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.
15  *
16  * 
17  * 
18  * 
19  */
20
21 #include "webcit.h"
22
23 /*
24  * List subscription handling
25  */
26 #ifndef EXPERIMENTAL_LISTSUB
27 void do_listsub(void)
28 {
29         char cmd[256];
30         char room[256];
31         char token[256];
32         char email[256];
33         char subtype[256];
34         char escaped_email[256];
35         char escaped_room[256];
36
37         char buf[SIZ];
38         int self;
39         char sroom[SIZ];
40
41         FlushStrBuf(WC->wc_fullname);
42         FlushStrBuf(WC->wc_username);
43         FlushStrBuf(WC->wc_password);
44         FlushStrBuf(WC->CurRoom.name);
45
46         output_headers(1, 0, 0, 1, 1, 0);
47         begin_burst();
48
49         wc_printf("<HTML><HEAD>\n"
50                 "<meta name=\"MSSmartTagsPreventParsing\" content=\"TRUE\" />\n"
51                 "<link href=\"static/styles/webcit.css\" rel=\"stylesheet\" type=\"text/css\">\n"
52                 "<TITLE>\n"
53         );
54         wc_printf(_("List subscription"));
55         wc_printf("</TITLE></HEAD><BODY>\n");
56
57         strcpy(cmd, bstr("cmd"));
58         strcpy(room, bstr("room"));
59         strcpy(token, bstr("token"));
60         strcpy(email, bstr("email"));
61         strcpy(subtype, bstr("subtype"));
62
63         wc_printf("<div align=center>");
64         wc_printf("<table border=0 width=75%%><tr><td>");
65
66         do_template("box_begin_1");
67         StrBufAppendBufPlain(WC->WBuf, _("List subscribe/unsubscribe"), -1, 0);
68         do_template("box_begin_2");
69         wc_printf("<div align=center><br>");
70
71         /*
72          * Subscribe command
73          */
74         if (!strcasecmp(cmd, "subscribe")) {
75                 serv_printf("SUBS subscribe|%s|%s|%s|%s/listsub",
76                         room,
77                         email,
78                         subtype,
79                         ChrPtr(site_prefix)
80                 );
81                 serv_getln(buf, sizeof buf);
82                 if (buf[0] == '2') {
83                         stresc(escaped_email, 256, email, 0, 0);
84                         stresc(escaped_room, 256, room, 0, 0);
85
86                         wc_printf("<CENTER><H1>");
87                         wc_printf(_("Confirmation request sent"));
88                         wc_printf("</H1>");
89                         wc_printf(_("You are subscribing <TT>%s"
90                                 "</TT> to the <b>%s</b> mailing list.  "
91                                 "The listserver has "
92                                 "sent you an e-mail with one additional "
93                                 "Web link for you to click on to confirm "
94                                 "your subscription.  This extra step is for "
95                                 "your protection, as it prevents others from "
96                                 "being able to subscribe you to lists "
97                                 "without your consent.<br><br>"
98                                 "Please click on the link which is being "
99                                 "e-mailed to you and your subscription will "
100                                 "be confirmed.<br>\n"),
101                                 escaped_email, escaped_room);
102                         wc_printf("<a href=\"listsub\">%s</A></CENTER>\n", _("Go back..."));
103                 }
104                 else {
105                         wc_printf("<FONT SIZE=+1><B>ERROR: %s</B>"
106                                 "</FONT><br><br>\n",
107                                 &buf[4]);
108                         goto FORM;
109                 }
110         }
111
112         /*
113          * Unsubscribe command
114          */
115         else if (!strcasecmp(cmd, "unsubscribe")) {
116                 serv_printf("SUBS unsubscribe|%s|%s|%s/listsub",
117                         room,
118                         email,
119                         ChrPtr(site_prefix)
120                 );
121                 serv_getln(buf, sizeof buf);
122                 if (buf[0] == '2') {
123                         wc_printf("<CENTER><H1>Confirmation request sent</H1>"
124                                 "You are unsubscribing <TT>");
125                         escputs(email);
126                         wc_printf("</TT> from the &quot;");
127                         escputs(room);
128                         wc_printf("&quot; mailing list.  The listserver has "
129                                 "sent you an e-mail with one additional "
130                                 "Web link for you to click on to confirm "
131                                 "your unsubscription.  This extra step is for "
132                                 "your protection, as it prevents others from "
133                                 "being able to unsubscribe you from "
134                                 "lists without your consent.<br><br>"
135                                 "Please click on the link which is being "
136                                 "e-mailed to you and your unsubscription will "
137                                 "be confirmed.<br>\n"
138                                 "<a href=\"listsub\">Back...</A></CENTER>\n"
139                         );
140                 }
141                 else {
142                         wc_printf("<FONT SIZE=+1><B>ERROR: %s</B>"
143                                 "</FONT><br><br>\n",
144                                 &buf[4]);
145                         goto FORM;
146                 }
147         }
148
149         /*
150          * Confirm command
151          */
152         else if (!strcasecmp(cmd, "confirm")) {
153                 serv_printf("SUBS confirm|%s|%s",
154                         room,
155                         token
156                 );
157                 serv_getln(buf, sizeof buf);
158                 if (buf[0] == '2') {
159                         wc_printf("<CENTER><H1>Confirmation successful!</H1>");
160                 }
161                 else {
162                         wc_printf("<CENTER><H1>Confirmation failed.</H1>"
163                                 "This could mean one of two things:<UL>\n"
164                                 "<LI>You waited too long to confirm your "
165                                 "subscribe/unsubscribe request (the "
166                                 "confirmation link is only valid for three "
167                                 "days)\n<LI>You have <i>already</i> "
168                                 "successfully confirmed your "
169                                 "subscribe/unsubscribe request and are "
170                                 "attempting to do it again.</UL>\n"
171                                 "The error returned by the server was: "
172                         );
173                 }
174                 wc_printf("%s</CENTER><br>\n", &buf[4]);
175         }
176
177         /*
178          * Any other (invalid) command causes the form to be displayed
179          */
180         else {
181 FORM:           wc_printf("<form method=\"POST\" action=\"listsub\">\n");
182
183                 wc_printf("Name of list: "
184                         "<select name=\"room\" size=1>\n");
185
186                 serv_puts("LPRM");
187                 serv_getln(buf, sizeof buf);
188                 if (buf[0] == '1') {
189                         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
190                                 extract_token(sroom, buf, 0, '|', sizeof sroom);
191                                 self = extract_int(buf, 4) & QR2_SELFLIST ;
192                                 if (self) {
193                                         wc_printf("<option value=\"");
194                                         escputs(sroom);
195                                         wc_printf("\">");
196                                         escputs(sroom);
197                                         wc_printf("</option>\n");
198                                 }
199                         }
200                 }
201                 wc_printf("</select><br><br>\n");
202
203                 wc_printf("Your e-mail address: "
204                         "<INPUT TYPE=\"text\" NAME=\"email\" "
205                         "VALUE=\""
206                 );
207                 escputs(email);
208                 wc_printf("\" maxlength=128 size=60><br><br>\n");
209
210                 wc_printf("(If subscribing) preferred format: "
211                         "<INPUT TYPE=\"radio\" NAME=\"subtype\" "
212                         "VALUE=\"list\" CHECKED>One message at a time&nbsp; "
213                         "<INPUT TYPE=\"radio\" NAME=\"subtype\" "
214                         "VALUE=\"digest\">Digest format&nbsp; "
215                         "<br><br>\n"
216                         "<INPUT TYPE=\"submit\" NAME=\"cmd\""
217                         " VALUE=\"subscribe\">\n"
218                         "<INPUT TYPE=\"submit\" NAME=\"cmd\""
219                         " VALUE=\"unsubscribe\"><br><br>\n"
220                         "</FORM>\n"
221                 );
222
223                 wc_printf("<hr>When you attempt to subscribe or unsubscribe to "
224                         "a mailing list, you will receive an e-mail containing"
225                         " one additional web link to click on for final "
226                         "confirmation.  This extra step is for your "
227                         "protection, as it prevents others from being able to "
228                         "subscribe or unsubscribe you to lists.<br>\n"
229                 );
230
231         }
232
233         wc_printf("</div>");
234         do_template("box_end");
235         wc_printf("</td></tr></table></div>");
236
237         wc_printf("</BODY></HTML>\n");
238         wDumpContent(0);
239         end_webcit_session();
240 }
241 #endif
242
243 int Conditional_LISTSUB_EXECUTE_SUBSCRIBE(StrBuf *Target, WCTemplputParams *TP)
244 {
245         int rc;
246         StrBuf *Line;
247         const char *ImpMsg;
248         const StrBuf *Room, *Email, *SubType;
249         
250         Room = sbstr("room");
251         if (Room == NULL)
252         {
253                 ImpMsg = _("You need to specify the mailinglist to subscribe to.");
254                 AppendImportantMessage(ImpMsg, -1);
255                 return 0;
256         }
257         Email = sbstr("email");
258         if (Email == NULL)
259         {
260                 ImpMsg = _("You need to specify the email address you'd like to subscribe with.");
261                 AppendImportantMessage(ImpMsg, -1);
262                 return 0;
263         }
264         SubType = sbstr("subtype");
265
266         Line = NewStrBuf();
267         serv_printf("SUBS subscribe|%s|%s|%s|%s/listsub",
268                     ChrPtr(Room),
269                     ChrPtr(Email),
270                     ChrPtr(SubType),
271                     ChrPtr(site_prefix)
272                 );
273         StrBuf_ServGetln(Line);
274         rc = GetServerStatusMsg(Line, NULL, 1, 2);
275         FreeStrBuf(&Line);
276         if (rc == 2)
277                 putbstr("__FAIL", NewStrBufPlain(HKEY("1")));
278         return rc == 2;
279 }
280
281 int Conditional_LISTSUB_EXECUTE_UNSUBSCRIBE(StrBuf *Target, WCTemplputParams *TP)
282 {
283         int rc;
284         StrBuf *Line;
285         const char *ImpMsg;
286         const StrBuf *Room, *Email;
287         
288         Room = sbstr("room");
289         if (Room == NULL)
290         {
291                 ImpMsg = _("You need to specify the mailinglist to subscribe to.");
292                 AppendImportantMessage(ImpMsg, -1);
293                 return 0;
294         }
295         Email = sbstr("email");
296         if (Email == NULL)
297         {
298                 ImpMsg = _("You need to specify the email address you'd like to subscribe with.");
299                 AppendImportantMessage(ImpMsg, -1);
300                 return 0;
301         }
302
303         serv_printf("SUBS unsubscribe|%s|%s|%s/listsub",
304                     ChrPtr(Room),
305                     ChrPtr(Email),
306                     ChrPtr(site_prefix)
307                 );
308         Line = NewStrBuf();
309         StrBuf_ServGetln(Line);
310         rc = GetServerStatusMsg(Line, NULL, 1, 2);
311         FreeStrBuf(&Line);
312         if (rc == 2)
313                 putbstr("__FAIL", NewStrBufPlain(HKEY("1")));
314         return rc == 2;
315 }
316
317 int Conditional_LISTSUB_EXECUTE_CONFIRM_SUBSCRIBE(StrBuf *Target, WCTemplputParams *TP)
318 {
319         int rc;
320         StrBuf *Line;
321         const char *ImpMsg;
322         const StrBuf *Room, *Token;
323         
324         Room = sbstr("room");
325         if (Room == NULL)
326         {
327                 ImpMsg = _("You need to specify the mailinglist to subscribe to.");
328                 AppendImportantMessage(ImpMsg, -1);
329                 return 0;
330         }
331         Token = sbstr("token");
332         if (Room == NULL)
333         {
334                 ImpMsg = _("You need to specify the mailinglist to subscribe to.");
335                 AppendImportantMessage(ImpMsg, -1);
336                 return 0;
337         }
338
339         Line = NewStrBuf();
340         serv_printf("SUBS confirm|%s|%s",
341                     ChrPtr(Room),
342                     ChrPtr(Token)
343                 );
344         StrBuf_ServGetln(Line);
345         rc = GetServerStatusMsg(Line, NULL, 1, 2);
346         FreeStrBuf(&Line);
347         if (rc == 2)
348                 putbstr("__FAIL", NewStrBufPlain(HKEY("1")));
349         return rc == 2;
350 }
351
352 #ifdef EXPERIMENTAL_LISTSUB
353 void do_listsub(void)
354 {
355         if (!havebstr("cmd"))
356         {
357                 putbstr("cmd", NewStrBufPlain(HKEY("")));
358         }
359         output_headers(1, 0, 0, 0, 1, 0);
360         do_template("listsub_display");
361         end_burst();
362 }
363 #endif
364
365 void 
366 InitModule_LISTSUB
367 (void)
368 {
369         RegisterConditional(HKEY("COND:LISTSUB:EXECUTE:SUBSCRIBE"), 0, Conditional_LISTSUB_EXECUTE_SUBSCRIBE,  CTX_NONE);
370         RegisterConditional(HKEY("COND:LISTSUB:EXECUTE:UNSUBSCRIBE"), 0, Conditional_LISTSUB_EXECUTE_UNSUBSCRIBE,  CTX_NONE);
371         RegisterConditional(HKEY("COND:LISTSUB:EXECUTE:CONFIRM:SUBSCRIBE"), 0, Conditional_LISTSUB_EXECUTE_CONFIRM_SUBSCRIBE,  CTX_NONE);
372
373         WebcitAddUrlHandler(HKEY("listsub"), "", 0, do_listsub, ANONYMOUS|COOKIEUNNEEDED|FORCE_SESSIONCLOSE);
374
375
376 }