silenced a silly little compiler warning
[citadel.git] / webcit / wiki.c
1 /*
2  * Functions pertaining to rooms with a wiki view
3  *
4  * Copyright (c) 2009-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 #include "dav.h"
17
18 /* 
19  * Convert a string to something suitable as a wiki index
20  */
21 void str_wiki_index(StrBuf *s)
22 {
23         StrBufSanitizeAscii(s, '_');
24         StrBufLowerCase(s);
25 }
26
27 /*
28  * Display a specific page from a wiki room
29  *
30  * "rev" may be set to an empty string to display the current version.
31  * "do_revert" may be set to nonzero to perform a reversion to the specified version.
32  */
33 void display_wiki_page_backend(StrBuf *pagename, char *rev, int do_revert)
34 {
35         wcsession *WCC = WC;
36         const StrBuf *Mime;
37         long msgnum = (-1L);
38         char buf[256];
39
40         if ((WCC->CurRoom.view != VIEW_WIKI) &&
41             (WCC->CurRoom.view != VIEW_WIKIMD)) {
42                 wc_printf(_("'%s' is not a Wiki room."), ChrPtr(WCC->CurRoom.name) );
43                 return;
44         }
45
46         if (StrLength(pagename) == 0) {
47                 StrBufPlain(pagename, HKEY("home"));
48         }
49
50         str_wiki_index(pagename);       /* convert index name to lowercase and numeric only */
51
52         if ((rev != NULL) && (strlen(rev) > 0)) {
53                 /* read an older revision */
54                 serv_printf("WIKI rev|%s|%s|%s", ChrPtr(pagename), rev, (do_revert ? "revert" : "fetch") );
55                 serv_getln(buf, sizeof buf);
56                 if (buf[0] == '2') {
57                         msgnum = extract_long(&buf[4], 0);
58                 }
59         }
60         else {
61                 /* read the current revision */
62                 msgnum = locate_message_by_uid(ChrPtr(pagename));
63         }
64
65         if (msgnum >= 0L) {
66                 read_message(WCC->WBuf, HKEY("view_message"), msgnum, NULL, &Mime, NULL);
67                 return;
68         }
69         putbstr("pagename", pagename);
70         do_template("wiki_empty");
71 }
72
73
74 /*
75  * Display a specific page from a wiki room
76  */
77 void display_wiki_page(void)
78 {
79         StrBuf *pagename;
80         char rev[128];
81         int do_revert = 0;
82
83         output_headers(1, 1, 1, 0, 0, 0);
84         pagename = NewStrBufDup(sbstr("page"));
85         str_wiki_index(pagename);
86         safestrncpy(rev, bstr("rev"), sizeof rev);
87         do_revert = atoi(bstr("revert"));
88         display_wiki_page_backend(pagename, rev, do_revert);
89         wDumpContent(1);
90 }
91
92
93 /*
94  * Display the revision history for a wiki page (template callback)
95  */
96 void tmplput_display_wiki_history(StrBuf *Target, WCTemplputParams *TP)
97 {
98         StrBuf *pagename;
99         StrBuf *Buf;
100         int row = 0;
101
102         pagename = NewStrBufDup(sbstr("page"));
103         str_wiki_index(pagename);
104
105         serv_printf("WIKI history|%s", ChrPtr(pagename));
106         Buf = NewStrBuf();
107         StrBuf_ServGetln(Buf);
108         if (GetServerStatus(Buf, NULL) == 1) {
109
110                 time_t rev_date;
111                 char rev_date_displayed[64];
112                 StrBuf *rev_uuid = NewStrBuf();
113                 StrBuf *author = NewStrBuf();
114                 StrBuf *node = NewStrBuf();
115
116                 wc_printf("<table class=\"wiki_history_background\">");
117
118                 wc_printf("<th>%s</th>", _("Date"));
119                 wc_printf("<th>%s</th>", _("Author"));
120
121                 while((StrBuf_ServGetln(Buf) >= 0) &&  strcmp(ChrPtr(Buf), "000")) {
122
123                         rev_date = extract_long(ChrPtr(Buf), 1);
124                         webcit_fmt_date(rev_date_displayed, sizeof rev_date_displayed, rev_date, DATEFMT_FULL);
125                         StrBufExtract_token(author, Buf, 2, '|');
126
127                         wc_printf("<tr bgcolor=\"%s\">", ((row%2) ? "#FFFFFF" : "#DDDDDD"));
128                         wc_printf("<td>%s</td><td>", rev_date_displayed);
129                         if (!strcasecmp(ChrPtr(node), (char *)WC->serv_info->serv_nodename)) {
130                                 escputs(ChrPtr(author));
131                                 wc_printf(" @ ");
132                                 escputs(ChrPtr(node));
133                         }
134                         else {
135                                 wc_printf("<a href=\"showuser?who=");
136                                 urlescputs(ChrPtr(author));
137                                 wc_printf("\">");
138                                 escputs(ChrPtr(author));
139                                 wc_printf("</a>");
140                         }
141                         wc_printf("</td>");
142
143                         if (row == 0) {
144                                 wc_printf("<td><a href=\"wiki?page=%s", bstr("page"));
145                                 wc_printf("?go="); urlescputs(ChrPtr(WC->CurRoom.name));
146                                 wc_printf("\">%s</a></td>", _("(show)"));
147                                 wc_printf("<td>(%s)</td>", _("Current version"));
148                         }
149
150                         else {
151                                 wc_printf("<td><a href=\"wiki?page=%s?rev=%s",
152                                         bstr("page"),
153                                         ChrPtr(rev_uuid)
154                                 );
155                                 wc_printf("?go="); urlescputs(ChrPtr(WC->CurRoom.name));
156                                 wc_printf("\">%s</a></td>", _("(show)"));
157                                 wc_printf("<td><a href=\"javascript:GetLoggedInFirst(encodeURIComponent('wiki?page=%s?rev=%s?revert=1'))\">%s</a></td>",
158                                         bstr("page"),
159                                         ChrPtr(rev_uuid),
160                                         _("(revert)")
161                                 );
162                         }
163                         wc_printf("</tr>\n");
164
165                         /* Extract all fields except the author and date after displaying the row.  This
166                          * is deliberate, because the timestamp reflects when the diff was written, not
167                          * when the version which it reflects was written.  Similarly, the name associated
168                          * with each diff is the author who created the newer version of the page that
169                          * made the diff happen.
170                          */
171                         StrBufExtract_token(rev_uuid, Buf, 0, '|');
172                         StrBufExtract_token(node, Buf, 3, '|');
173                         ++row;
174                 }
175
176                 wc_printf("</table>\n");
177                 FreeStrBuf(&author);
178                 FreeStrBuf(&node);
179                 FreeStrBuf(&rev_uuid);
180         }
181         else {
182                 wc_printf("%s", ChrPtr(Buf));
183         }
184
185         FreeStrBuf(&Buf);
186 }
187
188
189
190 /*
191  * Display the revision history for a wiki page
192  */
193 void display_wiki_history(void)
194 {
195         output_headers(1, 1, 1, 0, 0, 0);
196         do_template("wiki_history");
197         wDumpContent(1);
198 }
199
200
201 /*
202  * Display a list of all pages in a Wiki room (template callback)
203  */
204 void tmplput_display_wiki_pagelist(StrBuf *Target, WCTemplputParams *TP)
205 {
206         StrBuf *Buf;
207         int row = 0;
208
209         if (!IsEmptyStr(bstr("query"))) {
210                 serv_printf("MSGS SEARCH|%s||4", bstr("query"));        /* search-reduced list */
211         }
212         else {
213                 serv_printf("MSGS ALL|||4");                            /* full list */
214         }
215
216         Buf = NewStrBuf();
217         StrBuf_ServGetln(Buf);
218         if (GetServerStatus(Buf, NULL) == 1) {
219                 StrBuf *pagetitle = NewStrBuf();
220
221                 wc_printf("<table class=\"wiki_pagelist_background\">");
222                 wc_printf("<th>%s</th>", _("Page title"));
223
224                 while((StrBuf_ServGetln(Buf) >= 0) && strcmp(ChrPtr(Buf), "000")) {
225                         StrBufExtract_token(pagetitle, Buf, 1, '|');
226
227                         if (!bmstrcasestr((char *)ChrPtr(pagetitle), "_HISTORY_")) {    /* no history pages */
228                                 wc_printf("<tr bgcolor=\"%s\">", ((row%2) ? "#FFFFFF" : "#DDDDDD"));
229                                 wc_printf("<td><a href=\"wiki?page=");
230                                 urlescputs(ChrPtr(pagetitle));
231                                 wc_printf("\">");
232                                 escputs(ChrPtr(pagetitle));
233                                 wc_printf("</a></td>");
234                                 wc_printf("</tr>\n");
235                                 ++row;
236                         }
237                 }
238                 wc_printf("</table>\n");
239                 FreeStrBuf(&pagetitle);
240         }
241
242         FreeStrBuf(&Buf);
243 }
244
245
246 /*
247  * Display a list of all pages in a Wiki room.  Search requests in a Wiki room also go here.
248  */
249 void display_wiki_pagelist(void)
250 {
251         output_headers(1, 1, 1, 0, 0, 0);
252         do_template("wiki_pagelist");
253         wDumpContent(1);
254 }
255
256
257 int wiki_Cleanup(void **ViewSpecific)
258 {
259         StrBuf *pagename;
260         pagename = NewStrBufDup(sbstr("page"));
261         display_wiki_page_backend(pagename, "", 0);
262         wDumpContent(1);
263         return 0;
264 }
265
266
267 int ConditionalHaveWikiPage(StrBuf *Target, WCTemplputParams *TP)
268 {
269         const char *page;
270         const char *pch;
271         long len;
272
273         page = BSTR("page");
274         GetTemplateTokenString(Target, TP, 2, &pch, &len);
275         return strcasecmp(page, pch) == 0;
276 }
277
278
279 int ConditionalHavewikiType(StrBuf *Target, WCTemplputParams *TP)
280 {
281         wcsession *WCC = WC;
282         const char *pch;
283         long len;
284
285         GetTemplateTokenString(Target, TP, 2, &pch, &len);
286         return bmstrcasestr((char *)ChrPtr(WCC->Hdr->HR.ReqLine), pch) != NULL;
287 }
288
289
290 int wiki_PrintHeaderPage(SharedMessageStatus *Stat, void **ViewSpecific)
291 {
292         /* this function was intentionaly left empty. */
293         return 0;
294 }
295
296 int wiki_GetParamsGetServerCall(SharedMessageStatus *Stat, 
297                                 void **ViewSpecific, 
298                                 long oper, 
299                                 char *cmd, 
300                                 long len,
301                                 char *filter,
302                                 long flen)
303 {
304         if (oper == do_search)
305                 display_wiki_pagelist();
306         else 
307                 http_redirect("wiki?page=home");
308
309         return 300;
310 }
311
312
313 void 
314 InitModule_WIKI
315 (void)
316 {
317         RegisterReadLoopHandlerset(
318                 VIEW_WIKI,
319                 wiki_GetParamsGetServerCall,
320                 wiki_PrintHeaderPage,
321                 NULL,
322                 NULL,
323                 NULL,
324                 NULL,
325                 wiki_Cleanup,
326                 NULL
327         );
328
329         RegisterReadLoopHandlerset(
330                 VIEW_WIKIMD,
331                 wiki_GetParamsGetServerCall,
332                 wiki_PrintHeaderPage,
333                 NULL,
334                 NULL,
335                 NULL,
336                 NULL,
337                 wiki_Cleanup,
338                 NULL
339         );
340
341         WebcitAddUrlHandler(HKEY("wiki"), "", 0, display_wiki_page, 0);
342         WebcitAddUrlHandler(HKEY("wiki_history"), "", 0, display_wiki_history, 0);
343         WebcitAddUrlHandler(HKEY("wiki_pagelist"), "", 0, display_wiki_pagelist, 0);
344         RegisterNamespace("WIKI:DISPLAYHISTORY", 0, 0, tmplput_display_wiki_history, NULL, CTX_NONE);
345         RegisterNamespace("WIKI:DISPLAYPAGELIST", 0, 0, tmplput_display_wiki_pagelist, NULL, CTX_NONE);
346         RegisterConditional("COND:WIKI:PAGE", 1, ConditionalHaveWikiPage, CTX_NONE);
347         RegisterConditional("COND:WIKI:TYPE", 1, ConditionalHavewikiType, CTX_NONE);
348 }