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