5d9ceb8a7b1ac6baed11707263887cdb748eb1df
[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
21 int Conditional_LISTSUB_EXECUTE_SUBSCRIBE(StrBuf *Target, WCTemplputParams *TP)
22 {
23         int rc;
24         StrBuf *Line;
25         const char *ImpMsg;
26         const StrBuf *Room, *Email, *SubType;
27         
28         Room = sbstr("room");
29         if (Room == NULL)
30         {
31                 ImpMsg = _("You need to specify the mailinglist to subscribe to.");
32                 AppendImportantMessage(ImpMsg, -1);
33                 return 0;
34         }
35         Email = sbstr("email");
36         if (Email == NULL)
37         {
38                 ImpMsg = _("You need to specify the email address you'd like to subscribe with.");
39                 AppendImportantMessage(ImpMsg, -1);
40                 return 0;
41         }
42         SubType = sbstr("subtype");
43
44         Line = NewStrBuf();
45         serv_printf("SUBS subscribe|%s|%s|%s|%s/listsub",
46                     ChrPtr(Room),
47                     ChrPtr(Email),
48                     ChrPtr(SubType),
49                     ChrPtr(site_prefix)
50                 );
51         StrBuf_ServGetln(Line);
52         rc = GetServerStatusMsg(Line, NULL, 1, 2);
53         FreeStrBuf(&Line);
54         if (rc == 2)
55                 putbstr("__FAIL", NewStrBufPlain(HKEY("1")));
56         return rc == 2;
57 }
58
59 int Conditional_LISTSUB_EXECUTE_UNSUBSCRIBE(StrBuf *Target, WCTemplputParams *TP)
60 {
61         int rc;
62         StrBuf *Line;
63         const char *ImpMsg;
64         const StrBuf *Room, *Email;
65         
66         Room = sbstr("room");
67         if (Room == NULL)
68         {
69                 ImpMsg = _("You need to specify the mailinglist to subscribe to.");
70                 AppendImportantMessage(ImpMsg, -1);
71                 return 0;
72         }
73         Email = sbstr("email");
74         if (Email == NULL)
75         {
76                 ImpMsg = _("You need to specify the email address you'd like to subscribe with.");
77                 AppendImportantMessage(ImpMsg, -1);
78                 return 0;
79         }
80
81         serv_printf("SUBS unsubscribe|%s|%s|%s/listsub",
82                     ChrPtr(Room),
83                     ChrPtr(Email),
84                     ChrPtr(site_prefix)
85                 );
86         Line = NewStrBuf();
87         StrBuf_ServGetln(Line);
88         rc = GetServerStatusMsg(Line, NULL, 1, 2);
89         FreeStrBuf(&Line);
90         if (rc == 2)
91                 putbstr("__FAIL", NewStrBufPlain(HKEY("1")));
92         return rc == 2;
93 }
94
95 int Conditional_LISTSUB_EXECUTE_CONFIRM_SUBSCRIBE(StrBuf *Target, WCTemplputParams *TP)
96 {
97         int rc;
98         StrBuf *Line;
99         const char *ImpMsg;
100         const StrBuf *Room, *Token;
101         
102         Room = sbstr("room");
103         if (Room == NULL)
104         {
105                 ImpMsg = _("You need to specify the mailinglist to subscribe to.");
106                 AppendImportantMessage(ImpMsg, -1);
107                 return 0;
108         }
109         Token = sbstr("token");
110         if (Room == NULL)
111         {
112                 ImpMsg = _("You need to specify the mailinglist to subscribe to.");
113                 AppendImportantMessage(ImpMsg, -1);
114                 return 0;
115         }
116
117         Line = NewStrBuf();
118         serv_printf("SUBS confirm|%s|%s",
119                     ChrPtr(Room),
120                     ChrPtr(Token)
121                 );
122         StrBuf_ServGetln(Line);
123         rc = GetServerStatusMsg(Line, NULL, 1, 2);
124         FreeStrBuf(&Line);
125         if (rc == 2)
126                 putbstr("__FAIL", NewStrBufPlain(HKEY("1")));
127         return rc == 2;
128 }
129
130 void do_listsub(void)
131 {
132         if (!havebstr("cmd"))
133         {
134                 putbstr("cmd", NewStrBufPlain(HKEY("choose")));
135         }
136         output_headers(1, 0, 0, 0, 1, 0);
137         do_template("listsub_display");
138         end_burst();
139 }
140
141 void 
142 InitModule_LISTSUB
143 (void)
144 {
145         RegisterConditional("COND:LISTSUB:EXECUTE:SUBSCRIBE", 0, Conditional_LISTSUB_EXECUTE_SUBSCRIBE,  CTX_NONE);
146         RegisterConditional("COND:LISTSUB:EXECUTE:UNSUBSCRIBE", 0, Conditional_LISTSUB_EXECUTE_UNSUBSCRIBE,  CTX_NONE);
147         RegisterConditional("COND:LISTSUB:EXECUTE:CONFIRM:SUBSCRIBE", 0, Conditional_LISTSUB_EXECUTE_CONFIRM_SUBSCRIBE,  CTX_NONE);
148
149         WebcitAddUrlHandler(HKEY("listsub"), "", 0, do_listsub, ANONYMOUS|COOKIEUNNEEDED|FORCE_SESSIONCLOSE);
150
151
152 }