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