* History template
[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                         wc_printf(_("There is no room called '%s'."), ChrPtr(roomname));
52                         return;
53                 }
54
55         }
56
57         if (WC->wc_view != VIEW_WIKI) {
58                 wc_printf(_("'%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         wc_printf("<br /><br />"
74                 "<div align=\"center\">"
75                 "<table border=\"0\" bgcolor=\"#ffffff\" cellpadding=\"10\">"
76                 "<tr><td align=\"center\">"
77         );
78         wc_printf("<br><b>");
79         wc_printf(_("There is no page called '%s' here."), pagename);
80         wc_printf("</b><br><br>");
81         wc_printf(_("Select the 'Edit this page' link in the room banner "
82                 "if you would like to create this page."));
83         wc_printf("<br><br>");
84         wc_printf("</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 /*
105  * Display the revision history for a wiki page (template callback)
106  */
107 void tmplput_display_wiki_history(StrBuf *Target, WCTemplputParams *TP)
108 {
109         const StrBuf *roomname;
110         char pagename[128];
111         StrBuf *Buf;
112
113         roomname = sbstr("room");
114         safestrncpy(pagename, bstr("page"), sizeof pagename);
115         str_wiki_index(pagename);
116
117         if (StrLength(roomname) > 0) {
118
119                 /* If we're not in the correct room, try going there. */
120                 if (strcasecmp(ChrPtr(roomname), ChrPtr(WC->wc_roomname))) {
121                         gotoroom(roomname);
122                 }
123         
124                 /* If we're still not in the correct room, it doesn't exist. */
125                 if (strcasecmp(ChrPtr(roomname), ChrPtr(WC->wc_roomname))) {
126                         wc_printf(_("There is no room called '%s'."), ChrPtr(roomname));
127                         return;
128                 }
129
130         }
131
132         serv_printf("WIKI history|%s", pagename);
133         Buf = NewStrBuf();
134         StrBuf_ServGetln(Buf);
135         if (GetServerStatus(Buf, NULL) == 1) {
136                 while(StrBuf_ServGetln(Buf), strcmp(ChrPtr(Buf), "000")) {
137                         wc_printf("<tt>%s</tt><br>\n", ChrPtr(Buf));
138                 }
139         }
140         else {
141                 wc_printf("%s", ChrPtr(Buf));
142         }
143
144         FreeStrBuf(&Buf);
145 }
146
147
148 /*
149  * Display the revision history for a wiki page
150  */
151 void display_wiki_history(void)
152 {
153         output_headers(1, 1, 1, 0, 0, 0);
154         do_template("wiki_history", NULL);
155         wDumpContent(1);
156 }
157
158
159 int wiki_Cleanup(void **ViewSpecific)
160 {
161         char pagename[5];
162         safestrncpy(pagename, "home", sizeof pagename);
163         display_wiki_page_backend(WC->wc_roomname, pagename);
164         wDumpContent(1);
165         return 0;
166 }
167
168 void 
169 InitModule_WIKI
170 (void)
171 {
172         RegisterReadLoopHandlerset(
173                 VIEW_WIKI,
174                 NULL,
175                 NULL,
176                 NULL,
177                 NULL,
178                 wiki_Cleanup
179         );
180
181         WebcitAddUrlHandler(HKEY("wiki"), "", 0, display_wiki_page, 0);
182         WebcitAddUrlHandler(HKEY("wiki_history"), "", 0, display_wiki_history, 0);
183         RegisterNamespace("WIKI:DISPLAYHISTORY", 0, 0, tmplput_display_wiki_history, NULL, CTX_NONE);
184 }