listsub.c: style cleanup
[citadel.git] / webcit / listsub.c
1 // Web forms for handling mailing list subscribe/unsubscribe requests.
2 //
3 // Copyright (c) 1996-2022 by the citadel.org team
4 //
5 // This program is open source software.  You can redistribute it and/or
6 // modify it under the terms of the GNU General Public License, version 3.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 // GNU General Public License for more details.
12
13 #include "webcit.h"
14
15 // List subscription handling
16 int Conditional_LISTSUB_EXECUTE_SUBSCRIBE(StrBuf *Target, WCTemplputParams *TP) {
17         int rc;
18         StrBuf *Line;
19         const char *ImpMsg;
20         const StrBuf *Room, *Email;
21
22         if (strcmp(bstr("cmd"), "subscribe")) {
23                 return 0;
24         }
25
26         Room = sbstr("room");
27         if (Room == NULL) {
28                 ImpMsg = _("You need to specify the mailinglist to subscribe to.");
29                 AppendImportantMessage(ImpMsg, -1);
30                 return 0;
31         }
32         Email = sbstr("email");
33         if (Email == NULL) {
34                 ImpMsg = _("You need to specify the email address you'd like to subscribe with.");
35                 AppendImportantMessage(ImpMsg, -1);
36                 return 0;
37         }
38
39         Line = NewStrBuf();
40         serv_printf("LSUB subscribe|%s|%s|%s/listsub", ChrPtr(Room), ChrPtr(Email), ChrPtr(site_prefix));
41         StrBuf_ServGetln(Line);
42         rc = GetServerStatusMsg(Line, NULL, 1, 2);
43         FreeStrBuf(&Line);
44         if (rc == 2) {
45                 putbstr("__FAIL", NewStrBufPlain(HKEY("1")));
46         }
47         return rc == 2;
48 }
49
50
51 int Conditional_LISTSUB_EXECUTE_UNSUBSCRIBE(StrBuf *Target, WCTemplputParams *TP) {
52         int rc;
53         StrBuf *Line;
54         const char *ImpMsg;
55         const StrBuf *Room, *Email;
56
57         if (strcmp(bstr("cmd"), "unsubscribe")) {
58                 return 0;
59         }
60
61         Room = sbstr("room");
62         if (Room == NULL) {
63                 ImpMsg = _("You need to specify the mailinglist to subscribe to.");
64                 AppendImportantMessage(ImpMsg, -1);
65                 return 0;
66         }
67         Email = sbstr("email");
68         if (Email == NULL) {
69                 ImpMsg = _("You need to specify the email address you'd like to subscribe with.");
70                 AppendImportantMessage(ImpMsg, -1);
71                 return 0;
72         }
73
74         serv_printf("LSUB unsubscribe|%s|%s|%s/listsub", ChrPtr(Room), ChrPtr(Email), ChrPtr(site_prefix));
75         Line = NewStrBuf();
76         StrBuf_ServGetln(Line);
77         rc = GetServerStatusMsg(Line, NULL, 1, 2);
78         FreeStrBuf(&Line);
79         if (rc == 2) {
80                 putbstr("__FAIL", NewStrBufPlain(HKEY("1")));
81         }
82         return rc == 2;
83 }
84
85
86 int Conditional_LISTSUB_EXECUTE_CONFIRM_SUBSCRIBE(StrBuf *Target, WCTemplputParams *TP) {
87         int rc;
88         StrBuf *Line;
89         const char *ImpMsg;
90         const StrBuf *Room, *Token;
91
92         if (strcmp(bstr("cmd"), "confirm")) {
93                 return 0;
94         }
95
96         Room = sbstr("room");
97         if (Room == NULL) {
98                 ImpMsg = _("You need to specify the mailinglist to subscribe to.");
99                 AppendImportantMessage(ImpMsg, -1);
100                 return 0;
101         }
102         Token = sbstr("token");
103         if (Room == NULL) {
104                 ImpMsg = _("You need to specify the mailinglist to subscribe to.");
105                 AppendImportantMessage(ImpMsg, -1);
106                 return 0;
107         }
108
109         Line = NewStrBuf();
110         serv_printf("LSUB confirm|%s|%s", ChrPtr(Room), ChrPtr(Token));
111         StrBuf_ServGetln(Line);
112         rc = GetServerStatusMsg(Line, NULL, 1, 2);
113         FreeStrBuf(&Line);
114         if (rc == 2) {
115                 putbstr("__FAIL", NewStrBufPlain(HKEY("1")));
116         }
117         return rc == 2;
118 }
119
120
121 void do_listsub(void) {
122         if (!havebstr("cmd")) {
123                 putbstr("cmd", NewStrBufPlain(HKEY("choose")));
124         }
125         output_headers(1, 0, 0, 0, 1, 0);
126         do_template("listsub_display");
127         end_burst();
128 }
129
130
131 void 
132 InitModule_LISTSUB
133 (void)
134 {
135         RegisterConditional("COND:LISTSUB:EXECUTE:SUBSCRIBE", 0, Conditional_LISTSUB_EXECUTE_SUBSCRIBE,  CTX_NONE);
136         RegisterConditional("COND:LISTSUB:EXECUTE:UNSUBSCRIBE", 0, Conditional_LISTSUB_EXECUTE_UNSUBSCRIBE,  CTX_NONE);
137         RegisterConditional("COND:LISTSUB:EXECUTE:CONFIRM:SUBSCRIBE", 0, Conditional_LISTSUB_EXECUTE_CONFIRM_SUBSCRIBE,  CTX_NONE);
138         WebcitAddUrlHandler(HKEY("listsub"), "", 0, do_listsub, ANONYMOUS|COOKIEUNNEEDED|FORCE_SESSIONCLOSE);
139 }