Began fixing http://uncensored.citadel.org/wiki?page=stefan2012mar07?go=exterminate
[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(char *s)
22 {
23         int i;
24
25         if (s == NULL) return;
26
27         /* First remove all non-alphanumeric characters */
28         for (i=0; i<strlen(s); ++i) {
29                 if (!isalnum(s[i])) {
30                         strcpy(&s[i], &s[i+1]);
31                 }
32         }
33
34         /* Then make everything lower case */
35         for (i=0; i<strlen(s); ++i) {
36                 s[i] = tolower(s[i]);
37         }
38 }
39
40 /*
41  * Display a specific page from a wiki room
42  *
43  * "rev" may be set to an empty string to display the current version.
44  * "do_revert" may be set to nonzero to perform a reversion to the specified version.
45  */
46 void display_wiki_page_backend(const StrBuf *roomname, char *pagename, char *rev, int do_revert)
47 {
48         const StrBuf *Mime;
49         long msgnum = (-1L);
50         char buf[256];
51
52         str_wiki_index(pagename);
53
54         if (StrLength(roomname) > 0) {
55
56                 /* If we're not in the correct room, try going there. */
57                 if (strcasecmp(ChrPtr(roomname), ChrPtr(WC->CurRoom.name))) {
58                         gotoroom(roomname);
59                 }
60         
61                 /* If we're still not in the correct room, it doesn't exist. */
62                 if (strcasecmp(ChrPtr(roomname), ChrPtr(WC->CurRoom.name))) {
63                         wc_printf(_("There is no room called '%s'."), ChrPtr(roomname));
64                         return;
65                 }
66
67         }
68
69         if (WC->CurRoom.view != VIEW_WIKI) {
70                 wc_printf(_("'%s' is not a Wiki room."), ChrPtr(roomname));
71                 return;
72         }
73
74         if (IsEmptyStr(pagename)) {
75                 strcpy(pagename, "home");
76         }
77
78         /* Found it!  Now read it. */
79
80         if ((rev != NULL) && (strlen(rev) > 0)) {
81                 /* read an older revision */
82                 serv_printf("WIKI rev|%s|%s|%s", pagename, rev, (do_revert ? "revert" : "fetch") );
83                 serv_getln(buf, sizeof buf);
84                 if (buf[0] == '2') {
85                         msgnum = extract_long(&buf[4], 0);
86                 }
87         }
88         else {
89                 /* read the current revision? */
90                 msgnum = locate_message_by_uid(pagename);
91         }
92
93         if (msgnum >= 0L) {
94                 read_message(WC->WBuf, HKEY("view_message"), msgnum, NULL, &Mime);
95                 return;
96         }
97
98         wc_printf("<br><br>"
99                 "<div align=\"center\">"
100                 "<table border=\"0\" bgcolor=\"#ffffff\" cellpadding=\"10\">"
101                 "<tr><td align=\"center\">"
102         );
103         wc_printf("<br><b>");
104         wc_printf(_("There is no page called '%s' here."), pagename);
105         wc_printf("</b><br><br>");
106         wc_printf(_("Select the 'Edit this page' link in the room banner "
107                 "if you would like to create this page."));
108         wc_printf("<br><br>");
109         wc_printf("</td></tr></table></div>\n");
110 }
111
112
113 /*
114  * Display a specific page from a wiki room
115  */
116 void display_wiki_page(void)
117 {
118         const StrBuf *roomname;
119         char pagename[128];
120         char rev[128];
121         int do_revert = 0;
122
123         output_headers(1, 1, 1, 0, 0, 0);
124         roomname = sbstr("room");
125         safestrncpy(pagename, bstr("page"), sizeof pagename);
126         str_wiki_index(pagename);
127         safestrncpy(rev, bstr("rev"), sizeof rev);
128         do_revert = atoi(bstr("revert"));
129         display_wiki_page_backend(roomname, pagename, rev, do_revert);
130         wDumpContent(1);
131 }
132
133
134 /*
135  * Display the revision history for a wiki page (template callback)
136  */
137 void tmplput_display_wiki_history(StrBuf *Target, WCTemplputParams *TP)
138 {
139         const StrBuf *roomname;
140         char pagename[128];
141         StrBuf *Buf;
142         int row = 0;
143
144         roomname = sbstr("room");
145         safestrncpy(pagename, bstr("page"), sizeof pagename);
146         str_wiki_index(pagename);
147
148         if (StrLength(roomname) > 0) {
149
150                 /* If we're not in the correct room, try going there. */
151                 if (strcasecmp(ChrPtr(roomname), ChrPtr(WC->CurRoom.name))) {
152                         gotoroom(roomname);
153                 }
154         
155                 /* If we're still not in the correct room, it doesn't exist. */
156                 if (strcasecmp(ChrPtr(roomname), ChrPtr(WC->CurRoom.name))) {
157                         wc_printf(_("There is no room called '%s'."), ChrPtr(roomname));
158                         return;
159                 }
160
161         }
162
163         serv_printf("WIKI history|%s", pagename);
164         Buf = NewStrBuf();
165         StrBuf_ServGetln(Buf);
166         if (GetServerStatus(Buf, NULL) == 1) {
167
168                 time_t rev_date;
169                 char rev_date_displayed[64];
170                 StrBuf *rev_uuid = NewStrBuf();
171                 StrBuf *author = NewStrBuf();
172                 StrBuf *node = NewStrBuf();
173
174                 wc_printf("<table class=\"wiki_history_background\">");
175
176                 wc_printf("<th>%s</th>", _("Date"));
177                 wc_printf("<th>%s</th>", _("Author"));
178
179                 while((StrBuf_ServGetln(Buf) >= 0) &&  strcmp(ChrPtr(Buf), "000")) {
180
181                         rev_date = extract_long(ChrPtr(Buf), 1);
182                         webcit_fmt_date(rev_date_displayed, sizeof rev_date_displayed, rev_date, DATEFMT_FULL);
183                         StrBufExtract_token(author, Buf, 2, '|');
184
185                         wc_printf("<tr bgcolor=\"%s\">", ((row%2) ? "#FFFFFF" : "#DDDDDD"));
186                         wc_printf("<td>%s</td><td>", rev_date_displayed);
187                         if (!strcasecmp(ChrPtr(node), (char *)WC->serv_info->serv_nodename)) {
188                                 escputs(ChrPtr(author));
189                                 wc_printf(" @ ");
190                                 escputs(ChrPtr(node));
191                         }
192                         else {
193                                 wc_printf("<a href=\"showuser?who=");
194                                 urlescputs(ChrPtr(author));
195                                 wc_printf("\">");
196                                 escputs(ChrPtr(author));
197                                 wc_printf("</a>");
198                         }
199                         wc_printf("</td>");
200
201                         if (row == 0) {
202                                 wc_printf("<td><a href=\"wiki?page=%s\">%s</a></td>",
203                                         bstr("page"),
204                                         _("(show)")
205                                 );
206                                 wc_printf("<td>(%s)</td>", _("Current version"));
207                         }
208
209                         else {
210                                 wc_printf("<td><a href=\"wiki?page=%s?rev=%s\">%s</a></td>",
211                                         bstr("page"),
212                                         ChrPtr(rev_uuid),
213                                         _("(show)")
214                                 );
215                                 wc_printf("<td><a href=\"javascript:GetLoggedInFirst(encodeURIComponent('wiki?page=%s?rev=%s?revert=1'))\">%s</a></td>",
216                                         bstr("page"),
217                                         ChrPtr(rev_uuid),
218                                         _("(revert)")
219                                 );
220                         }
221                         wc_printf("</tr>\n");
222
223                         /* Extract all fields except the author and date after displaying the row.  This
224                          * is deliberate, because the timestamp reflects when the diff was written, not
225                          * when the version which it reflects was written.  Similarly, the name associated
226                          * with each diff is the author who created the newer version of the page that
227                          * made the diff happen.
228                          */
229                         StrBufExtract_token(rev_uuid, Buf, 0, '|');
230                         StrBufExtract_token(node, Buf, 3, '|');
231                         ++row;
232                 }
233
234                 wc_printf("</table>\n");
235                 FreeStrBuf(&author);
236                 FreeStrBuf(&node);
237                 FreeStrBuf(&rev_uuid);
238         }
239         else {
240                 wc_printf("%s", ChrPtr(Buf));
241         }
242
243         FreeStrBuf(&Buf);
244 }
245
246
247
248 /*
249  * Display the revision history for a wiki page
250  */
251 void display_wiki_history(void)
252 {
253         output_headers(1, 1, 1, 0, 0, 0);
254         do_template("wiki_history");
255         wDumpContent(1);
256 }
257
258
259 /*
260  * Display a list of all pages in a Wiki room (template callback)
261  */
262 void tmplput_display_wiki_pagelist(StrBuf *Target, WCTemplputParams *TP)
263 {
264         const StrBuf *roomname;
265         StrBuf *Buf;
266         int row = 0;
267
268         roomname = sbstr("room");
269         if (StrLength(roomname) > 0) {
270                 /* If we're not in the correct room, try going there. */
271                 if (strcasecmp(ChrPtr(roomname), ChrPtr(WC->CurRoom.name))) {
272                         gotoroom(roomname);
273                 }
274         
275                 /* If we're still not in the correct room, it doesn't exist. */
276                 if (strcasecmp(ChrPtr(roomname), ChrPtr(WC->CurRoom.name))) {
277                         wc_printf(_("There is no room called '%s'."), ChrPtr(roomname));
278                         return;
279                 }
280         }
281
282         if (!IsEmptyStr(bstr("query"))) {
283                 serv_printf("MSGS SEARCH|%s||4", bstr("query"));        /* search-reduced list */
284         }
285         else {
286                 serv_printf("MSGS ALL|||4");                            /* full list */
287         }
288
289         Buf = NewStrBuf();
290         StrBuf_ServGetln(Buf);
291         if (GetServerStatus(Buf, NULL) == 1) {
292                 StrBuf *pagetitle = NewStrBuf();
293
294                 wc_printf("<table class=\"wiki_pagelist_background\">");
295                 wc_printf("<th>%s</th>", _("Page title"));
296
297                 while((StrBuf_ServGetln(Buf) >= 0) && strcmp(ChrPtr(Buf), "000")) {
298                         StrBufExtract_token(pagetitle, Buf, 1, '|');
299
300                         if (!bmstrcasestr((char *)ChrPtr(pagetitle), "_HISTORY_")) {    /* no history pages */
301                                 wc_printf("<tr bgcolor=\"%s\">", ((row%2) ? "#FFFFFF" : "#DDDDDD"));
302                                 wc_printf("<td><a href=\"wiki?page=");
303                                 urlescputs(ChrPtr(pagetitle));
304                                 wc_printf("\">");
305                                 escputs(ChrPtr(pagetitle));
306                                 wc_printf("</a></td>");
307                                 wc_printf("</tr>\n");
308                                 ++row;
309                         }
310                 }
311                 wc_printf("</table>\n");
312                 FreeStrBuf(&pagetitle);
313         }
314
315         FreeStrBuf(&Buf);
316 }
317
318
319 /*
320  * Display a list of all pages in a Wiki room.  Search requests in a Wiki room also go here.
321  */
322 void display_wiki_pagelist(void)
323 {
324         output_headers(1, 1, 1, 0, 0, 0);
325         do_template("wiki_pagelist");
326         wDumpContent(1);
327 }
328
329
330 int wiki_Cleanup(void **ViewSpecific)
331 {
332         char pagename[5];
333         safestrncpy(pagename, "home", sizeof pagename);
334         display_wiki_page_backend(WC->CurRoom.name, pagename, "", 0);
335         wDumpContent(1);
336         return 0;
337 }
338
339
340 int ConditionalHaveWikiPage(StrBuf *Target, WCTemplputParams *TP)
341 {
342         const char *page;
343         const char *pch;
344         long len;
345
346         page = BSTR("page");
347         GetTemplateTokenString(Target, TP, 2, &pch, &len);
348         return strcasecmp(page, pch) == 0;
349 }
350
351
352 int ConditionalHavewikiType(StrBuf *Target, WCTemplputParams *TP)
353 {
354         wcsession *WCC = WC;
355         const char *pch;
356         long len;
357
358         GetTemplateTokenString(Target, TP, 2, &pch, &len);
359         return bmstrcasestr((char *)ChrPtr(WCC->Hdr->HR.ReqLine), pch) != NULL;
360 }
361
362
363 int wiki_PrintHeaderPage(SharedMessageStatus *Stat, void **ViewSpecific)
364 {
365         /* this function was intentionaly left empty. */
366         return 0;
367 }
368
369 int wiki_GetParamsGetServerCall(SharedMessageStatus *Stat, 
370                                 void **ViewSpecific, 
371                                 long oper, 
372                                 char *cmd, 
373                                 long len,
374                                 char *filter,
375                                 long flen)
376 {
377         if (oper == do_search)
378                 display_wiki_pagelist();
379         else 
380                 http_redirect("wiki?page=home");
381
382         return 300;
383 }
384
385
386 void 
387 InitModule_WIKI
388 (void)
389 {
390         RegisterReadLoopHandlerset(
391                 VIEW_WIKI,
392                 wiki_GetParamsGetServerCall,
393                 wiki_PrintHeaderPage,
394                 NULL,
395                 NULL,
396                 NULL,
397                 NULL,
398                 wiki_Cleanup
399         );
400
401         WebcitAddUrlHandler(HKEY("wiki"), "", 0, display_wiki_page, 0);
402         WebcitAddUrlHandler(HKEY("wiki_history"), "", 0, display_wiki_history, 0);
403         WebcitAddUrlHandler(HKEY("wiki_pagelist"), "", 0, display_wiki_pagelist, 0);
404         RegisterNamespace("WIKI:DISPLAYHISTORY", 0, 0, tmplput_display_wiki_history, NULL, CTX_NONE);
405         RegisterNamespace("WIKI:DISPLAYPAGELIST", 0, 0, tmplput_display_wiki_pagelist, NULL, CTX_NONE);
406         RegisterConditional(HKEY("COND:WIKI:PAGE"), 1, ConditionalHaveWikiPage, CTX_NONE);
407         RegisterConditional(HKEY("COND:WIKI:TYPE"), 1, ConditionalHavewikiType, CTX_NONE);
408 }