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