b60d66d5b8edfaffd3a1bdc3542f8f786031cdc4
[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 void display_wiki_page_backend(const StrBuf *roomname, char *pagename)
36 {
37         const StrBuf *Mime;
38         long msgnum = (-1L);
39
40         str_wiki_index(pagename);
41
42         if (StrLength(roomname) > 0) {
43
44                 /* If we're not in the correct room, try going there. */
45                 if (strcasecmp(ChrPtr(roomname), ChrPtr(WC->wc_roomname))) {
46                         gotoroom(roomname);
47                 }
48         
49                 /* If we're still not in the correct room, it doesn't exist. */
50                 if (strcasecmp(ChrPtr(roomname), ChrPtr(WC->wc_roomname))) {
51                         wprintf(_("There is no room called '%s'."), ChrPtr(roomname));
52                         return;
53                 }
54
55         }
56
57         if (WC->wc_view != VIEW_WIKI) {
58                 wprintf(_("'%s' is not a Wiki room."), ChrPtr(roomname));
59                 return;
60         }
61
62         if (IsEmptyStr(pagename)) {
63                 strcpy(pagename, "home");
64         }
65
66         /* Found it!  Now read it. */
67         msgnum = locate_message_by_uid(pagename);
68         if (msgnum >= 0L) {
69                 read_message(WC->WBuf, HKEY("view_message"), msgnum, NULL, &Mime);
70                 return;
71         }
72
73         wprintf("<br /><br />"
74                 "<div align=\"center\">"
75                 "<table border=\"0\" bgcolor=\"#ffffff\" cellpadding=\"10\">"
76                 "<tr><td align=\"center\">"
77         );
78         wprintf("<br><b>");
79         wprintf(_("There is no page called '%s' here."), pagename);
80         wprintf("</b><br><br>");
81         wprintf(_("Select the 'Edit this page' link in the room banner "
82                 "if you would like to create this page."));
83         wprintf("<br><br>");
84         wprintf("</td></tr></table></div>\n");
85 }
86
87
88 /*
89  * Display a specific page from a wiki room
90  */
91 void display_wiki_page(void)
92 {
93         const StrBuf *roomname;
94         char pagename[128];
95
96         output_headers(1, 1, 1, 0, 0, 0);
97         roomname = sbstr("room");
98         safestrncpy(pagename, bstr("page"), sizeof pagename);
99         display_wiki_page_backend(roomname, pagename);
100         wDumpContent(1);
101 }
102
103
104 int wiki_Cleanup(void **ViewSpecific)
105 {
106         char pagename[5];
107         safestrncpy(pagename, "home", sizeof pagename);
108         display_wiki_page_backend(WC->wc_roomname, pagename);
109         wDumpContent(1);
110         return 0;
111 }
112
113 void 
114 InitModule_WIKI
115 (void)
116 {
117         RegisterReadLoopHandlerset(
118                 VIEW_WIKI,
119                 NULL,
120                 NULL,
121                 NULL,
122                 NULL,
123                 wiki_Cleanup
124         );
125
126         WebcitAddUrlHandler(HKEY("wiki"), display_wiki_page, 0);
127 }
128
129