* Modified the text client so that the <E>nter message command does not work in room...
[citadel.git] / citadel / modules / wiki / serv_wiki.c
1 /*
2  * $Id$
3  *
4  * Server-side module for Wiki rooms.  This handles things like version control. 
5  * 
6  * Copyright (c) 2009 by the citadel.org team
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 3 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #include "sysdep.h"
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <stdio.h>
27 #include <fcntl.h>
28 #include <signal.h>
29 #include <pwd.h>
30 #include <errno.h>
31 #include <ctype.h>
32 #include <sys/types.h>
33
34 #if TIME_WITH_SYS_TIME
35 # include <sys/time.h>
36 # include <time.h>
37 #else
38 # if HAVE_SYS_TIME_H
39 #  include <sys/time.h>
40 # else
41 #  include <time.h>
42 # endif
43 #endif
44
45 #include <sys/wait.h>
46 #include <string.h>
47 #include <limits.h>
48 #include <libcitadel.h>
49 #include "citadel.h"
50 #include "server.h"
51 #include "citserver.h"
52 #include "support.h"
53 #include "config.h"
54 #include "control.h"
55 #include "user_ops.h"
56 #include "policy.h"
57 #include "database.h"
58 #include "msgbase.h"
59 #include "euidindex.h"
60 #include "ctdl_module.h"
61
62 /*
63  * Data passed back and forth between wiki_rev() and its MIME parser callback
64  */
65 struct HistoryEraserCallBackData {
66         char *tempfilename;             /* name of temp file being patched */
67         char *stop_when;                /* stop when we hit this uuid */
68         int done;                       /* set to nonzero when we're done patching */
69 };
70
71 /*
72  * Name of the temporary room we create to store old revisions when someone requests them.
73  * We put it in an invalid namespace so the DAP cleans up after us later.
74  */
75 char *wwm = "9999999999.WikiWaybackMachine";
76
77 /*
78  * Before allowing a wiki page save to execute, we have to perform version control.
79  * This involves fetching the old version of the page if it exists.
80  */
81 int wiki_upload_beforesave(struct CtdlMessage *msg) {
82         struct CitContext *CCC = CC;
83         long old_msgnum = (-1L);
84         struct CtdlMessage *old_msg = NULL;
85         long history_msgnum = (-1L);
86         struct CtdlMessage *history_msg = NULL;
87         char diff_old_filename[PATH_MAX];
88         char diff_new_filename[PATH_MAX];
89         char diff_cmd[PATH_MAX];
90         FILE *fp;
91         int rv;
92         char history_page[1024];
93         char boundary[256];
94         char prefixed_boundary[258];
95         char buf[1024];
96         int nbytes = 0;
97         char *diffbuf = NULL;
98         size_t diffbuf_len = 0;
99         char *ptr = NULL;
100
101         if (!CCC->logged_in) return(0); /* Only do this if logged in. */
102
103         /* Is this a room with a Wiki in it, don't run this hook. */
104         if (CCC->room.QRdefaultview != VIEW_WIKI) {
105                 return(0);
106         }
107
108         /* If this isn't a MIME message, don't bother. */
109         if (msg->cm_format_type != 4) return(0);
110
111         /* If there's no EUID we can't do this.  Reject the post. */
112         if (msg->cm_fields['E'] == NULL) return(1);
113
114         snprintf(history_page, sizeof history_page, "%s_HISTORY_", msg->cm_fields['E']);
115
116         /* Make sure we're saving a real wiki page rather than a wiki history page.
117          * This is important in order to avoid recursing infinitely into this hook.
118          */
119         if (    (strlen(msg->cm_fields['E']) >= 9)
120                 && (!strcasecmp(&msg->cm_fields['E'][strlen(msg->cm_fields['E'])-9], "_HISTORY_"))
121         ) {
122                 CtdlLogPrintf(CTDL_DEBUG, "History page not being historied\n");
123                 return(0);
124         }
125
126         /* If there's no message text, obviously this is all b0rken and shouldn't happen at all */
127         if (msg->cm_fields['M'] == NULL) return(0);
128
129         /* Set the message subject identical to the page name */
130         if (msg->cm_fields['U'] != NULL) {
131                 free(msg->cm_fields['U']);
132         }
133         msg->cm_fields['U'] = strdup(msg->cm_fields['E']);
134
135         /* See if we can retrieve the previous version. */
136         old_msgnum = CtdlLocateMessageByEuid(msg->cm_fields['E'], &CCC->room);
137         if (old_msgnum > 0L) {
138                 old_msg = CtdlFetchMessage(old_msgnum, 1);
139         }
140         else {
141                 old_msg = NULL;
142         }
143
144         if ((old_msg != NULL) && (old_msg->cm_fields['M'] == NULL)) {   /* old version is corrupt? */
145                 CtdlFreeMessage(old_msg);
146                 old_msg = NULL;
147         }
148         
149         /* If no changes were made, don't bother saving it again */
150         if ((old_msg != NULL) && (!strcmp(msg->cm_fields['M'], old_msg->cm_fields['M']))) {
151                 CtdlFreeMessage(old_msg);
152                 return(1);
153         }
154
155         /*
156          * Generate diffs
157          */
158         CtdlMakeTempFileName(diff_old_filename, sizeof diff_old_filename);
159         CtdlMakeTempFileName(diff_new_filename, sizeof diff_new_filename);
160
161         if (old_msg != NULL) {
162                 fp = fopen(diff_old_filename, "w");
163                 rv = fwrite(old_msg->cm_fields['M'], strlen(old_msg->cm_fields['M']), 1, fp);
164                 fclose(fp);
165                 CtdlFreeMessage(old_msg);
166         }
167
168         fp = fopen(diff_new_filename, "w");
169         rv = fwrite(msg->cm_fields['M'], strlen(msg->cm_fields['M']), 1, fp);
170         fclose(fp);
171
172         diffbuf_len = 0;
173         diffbuf = NULL;
174         snprintf(diff_cmd, sizeof diff_cmd,
175                 "diff -u %s %s",
176                 diff_new_filename,
177                 ((old_msg != NULL) ? diff_old_filename : "/dev/null")
178         );
179         fp = popen(diff_cmd, "r");
180         if (fp != NULL) {
181                 do {
182                         diffbuf = realloc(diffbuf, diffbuf_len + 1025);
183                         nbytes = fread(&diffbuf[diffbuf_len], 1, 1024, fp);
184                         diffbuf_len += nbytes;
185                 } while (nbytes == 1024);
186                 diffbuf[diffbuf_len] = 0;
187                 if (pclose(fp) != 0) {
188                         CtdlLogPrintf(CTDL_ERR, "pclose() returned an error - diff failed\n");
189                 }
190         }
191         CtdlLogPrintf(CTDL_DEBUG, "diff length is %d bytes\n", diffbuf_len);
192
193         unlink(diff_old_filename);
194         unlink(diff_new_filename);
195
196         /* Determine whether this was a bogus (empty) edit */
197         if ((diffbuf_len = 0) && (diffbuf != NULL)) {
198                 free(diffbuf);
199                 diffbuf = NULL;
200         }
201         if (diffbuf == NULL) {
202                 return(1);              /* No changes at all?  Abandon the post entirely! */
203         }
204
205         /* Now look for the existing edit history */
206
207         history_msgnum = CtdlLocateMessageByEuid(history_page, &CCC->room);
208         history_msg = NULL;
209         if (history_msgnum > 0L) {
210                 history_msg = CtdlFetchMessage(history_msgnum, 1);
211         }
212
213         /* Create a new history message if necessary */
214         if (history_msg == NULL) {
215                 history_msg = malloc(sizeof(struct CtdlMessage));
216                 memset(history_msg, 0, sizeof(struct CtdlMessage));
217                 history_msg->cm_magic = CTDLMESSAGE_MAGIC;
218                 history_msg->cm_anon_type = MES_NORMAL;
219                 history_msg->cm_format_type = FMT_RFC822;
220                 history_msg->cm_fields['A'] = strdup("Citadel");
221                 history_msg->cm_fields['R'] = strdup(CCC->room.QRname);
222                 history_msg->cm_fields['E'] = strdup(history_page);
223                 history_msg->cm_fields['U'] = strdup(history_page);
224                 history_msg->cm_fields['1'] = strdup("1");              /* suppress full text indexing */
225                 snprintf(boundary, sizeof boundary, "Citadel--Multipart--%04x--%08lx", getpid(), time(NULL));
226                 history_msg->cm_fields['M'] = malloc(1024);
227                 snprintf(history_msg->cm_fields['M'], 1024,
228                         "Content-type: multipart/mixed; boundary=\"%s\"\n\n"
229                         "This is a Citadel wiki history encoded as multipart MIME.\n"
230                         "Each part is comprised of a diff script representing one change set.\n"
231                         "\n"
232                         "--%s--\n"
233                         ,
234                         boundary, boundary
235                 );
236         }
237
238         /* Update the history message (regardless of whether it's new or existing) */
239
240         /* Remove the Message-ID from the old version of the history message.  This will cause a brand
241          * new one to be generated, avoiding an uninitentional hit of the loop zapper when we replicate.
242          */
243         if (history_msg->cm_fields['I'] != NULL) {
244                 free(history_msg->cm_fields['I']);
245         }
246
247         /* Figure out the boundary string.  We do this even when we generated the
248          * boundary string in the above code, just to be safe and consistent.
249          */
250         strcpy(boundary, "");
251
252         ptr = history_msg->cm_fields['M'];
253         do {
254                 ptr = memreadline(ptr, buf, sizeof buf);
255                 if (*ptr != 0) {
256                         striplt(buf);
257                         if (!IsEmptyStr(buf) && (!strncasecmp(buf, "Content-type:", 13))) {
258                                 if (
259                                         (bmstrcasestr(buf, "multipart") != NULL)
260                                         && (bmstrcasestr(buf, "boundary=") != NULL)
261                                 ) {
262                                         safestrncpy(boundary, bmstrcasestr(buf, "\""), sizeof boundary);
263                                         char *qu;
264                                         qu = strchr(boundary, '\"');
265                                         if (qu) {
266                                                 strcpy(boundary, ++qu);
267                                         }
268                                         qu = strchr(boundary, '\"');
269                                         if (qu) {
270                                                 *qu = 0;
271                                         }
272                                 }
273                         }
274                 }
275         } while ( (IsEmptyStr(boundary)) && (*ptr != 0) );
276
277         /* Now look for the first boundary.  That is where we need to insert our fun.
278          */
279         if (!IsEmptyStr(boundary)) {
280                 snprintf(prefixed_boundary, sizeof prefixed_boundary, "--%s", boundary);
281                 history_msg->cm_fields['M'] = realloc(history_msg->cm_fields['M'],
282                         strlen(history_msg->cm_fields['M']) + strlen(diffbuf) + 1024
283                 );
284                 ptr = bmstrcasestr(history_msg->cm_fields['M'], prefixed_boundary);
285                 if (ptr != NULL) {
286                         char *the_rest_of_it = strdup(ptr);
287                         char uuid[32];
288                         char memo[512];
289                         char encoded_memo[768];
290                         generate_uuid(uuid);
291                         snprintf(memo, sizeof memo, "%s|%ld|%s|%s", 
292                                 uuid,
293                                 time(NULL),
294                                 CCC->user.fullname,
295                                 config.c_nodename
296                                 /* no longer logging CCC->cs_inet_email */
297                         );
298                         CtdlEncodeBase64(encoded_memo, memo, strlen(memo), 0);
299                         sprintf(ptr, "--%s\n"
300                                         "Content-type: text/plain\n"
301                                         "Content-Disposition: inline; filename=\"%s\"\n"
302                                         "Content-Transfer-Encoding: 8bit\n"
303                                         "\n"
304                                         "%s\n"
305                                         "%s"
306                                         ,
307                                 boundary,
308                                 encoded_memo,
309                                 diffbuf,
310                                 the_rest_of_it
311                         );
312                         free(the_rest_of_it);
313                 }
314
315                 history_msg->cm_fields['T'] = realloc(history_msg->cm_fields['T'], 32);
316                 snprintf(history_msg->cm_fields['T'], 32, "%ld", time(NULL));
317         
318                 CtdlSubmitMsg(history_msg, NULL, "", 0);
319         }
320         else {
321                 CtdlLogPrintf(CTDL_ALERT, "Empty boundary string in history message.  No history!\n");
322         }
323
324         free(diffbuf);
325         free(history_msg);
326         return(0);
327 }
328
329
330 /*
331  * MIME Parser callback for wiki_history()
332  *
333  * The "filename" field will contain a memo field.  All we have to do is decode
334  * the base64 and output it.  The data is already in a delimited format suitable
335  * for our client protocol.
336  */
337 void wiki_history_callback(char *name, char *filename, char *partnum, char *disp,
338                    void *content, char *cbtype, char *cbcharset, size_t length,
339                    char *encoding, char *cbid, void *cbuserdata)
340 {
341         char memo[1024];
342
343         CtdlDecodeBase64(memo, filename, strlen(filename));
344         cprintf("%s\n", memo);
345 }
346
347
348 /*
349  * Fetch a list of revisions for a particular wiki page
350  */
351 void wiki_history(char *pagename) {
352         int r;
353         char history_page_name[270];
354         long msgnum;
355         struct CtdlMessage *msg;
356
357         r = CtdlDoIHavePermissionToReadMessagesInThisRoom();
358         if (r != om_ok) {
359                 if (r == om_not_logged_in) {
360                         cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
361                 }
362                 else {
363                         cprintf("%d An unknown error has occurred.\n", ERROR);
364                 }
365                 return;
366         }
367
368         snprintf(history_page_name, sizeof history_page_name, "%s_HISTORY_", pagename);
369         msgnum = CtdlLocateMessageByEuid(history_page_name, &CC->room);
370         if (msgnum > 0L) {
371                 msg = CtdlFetchMessage(msgnum, 1);
372         }
373         else {
374                 msg = NULL;
375         }
376
377         if ((msg != NULL) && (msg->cm_fields['M'] == NULL)) {
378                 CtdlFreeMessage(msg);
379                 msg = NULL;
380         }
381
382         if (msg == NULL) {
383                 cprintf("%d Revision history for '%s' was not found.\n", ERROR+MESSAGE_NOT_FOUND, pagename);
384                 return;
385         }
386
387         
388         cprintf("%d Revision history for '%s'\n", LISTING_FOLLOWS, pagename);
389         mime_parser(msg->cm_fields['M'], NULL, *wiki_history_callback, NULL, NULL, NULL, 0);
390         cprintf("000\n");
391
392         CtdlFreeMessage(msg);
393         return;
394 }
395
396 /*
397  * MIME Parser callback for wiki_rev()
398  *
399  * The "filename" field will contain a memo field, which includes (among other things)
400  * the uuid of this revision.  After we hit the desired revision, we stop processing.
401  *
402  * The "content" filed will contain "diff" output suitable for applying via "patch"
403  * to our temporary file.
404  */
405 void wiki_rev_callback(char *name, char *filename, char *partnum, char *disp,
406                    void *content, char *cbtype, char *cbcharset, size_t length,
407                    char *encoding, char *cbid, void *cbuserdata)
408 {
409         struct HistoryEraserCallBackData *hecbd = (struct HistoryEraserCallBackData *)cbuserdata;
410         char memo[1024];
411         char this_rev[256];
412         FILE *fp;
413         char *ptr = NULL;
414         char buf[1024];
415
416         /* Did a previous callback already indicate that we've reached our target uuid?
417          * If so, don't process anything else.
418          */
419         if (hecbd->done) {
420                 return;
421         }
422
423         CtdlDecodeBase64(memo, filename, strlen(filename));
424         extract_token(this_rev, memo, 0, '|', sizeof this_rev);
425         CtdlLogPrintf(CTDL_DEBUG, "callback found rev: %s\n", this_rev);
426
427         /* Perform the patch */
428         fp = popen("patch -f -s -p0 --global-reject-file=/dev/null >/dev/null 2>/dev/null", "w");
429         if (fp) {
430                 /* Replace the filenames in the patch with the tempfilename we're actually tweaking */
431                 fprintf(fp, "--- %s\n", hecbd->tempfilename);
432                 fprintf(fp, "+++ %s\n", hecbd->tempfilename);
433
434                 ptr = (char *)content;
435                 int linenum = 0;
436                 do {
437                         ++linenum;
438                         ptr = memreadline(ptr, buf, sizeof buf);
439                         if (*ptr != 0) {
440                                 if (linenum <= 2) {
441                                         /* skip the first two lines; they contain bogus filenames */
442                                 }
443                                 else {
444                                         fprintf(fp, "%s\n", buf);
445                                 }
446                         }
447                 } while ((*ptr != 0) && ((int)ptr < ((int)content + length)));
448                 if (pclose(fp) != 0) {
449                         CtdlLogPrintf(CTDL_ERR, "pclose() returned an error - patch failed\n");
450                 }
451         }
452
453         if (!strcasecmp(this_rev, hecbd->stop_when)) {
454                 /* Found our target rev.  Tell any subsequent callbacks to suppress processing. */
455                 CtdlLogPrintf(CTDL_DEBUG, "Target revision has been reached -- stop patching.\n");
456                 hecbd->done = 1;
457         }
458 }
459
460
461 /*
462  * Fetch a specific revision of a wiki page.  The "operation" string may be set to "fetch" in order
463  * to simply fetch the desired revision and store it in a temporary location for viewing, or "revert"
464  * to revert the currently active page to that revision.
465  */
466 void wiki_rev(char *pagename, char *rev, char *operation)
467 {
468         int r;
469         char history_page_name[270];
470         long msgnum;
471         char temp[PATH_MAX];
472         char timestamp[64];
473         struct CtdlMessage *msg;
474         FILE *fp;
475         struct HistoryEraserCallBackData hecbd;
476         long len = 0L;
477         int rv;
478
479         r = CtdlDoIHavePermissionToReadMessagesInThisRoom();
480         if (r != om_ok) {
481                 if (r == om_not_logged_in) {
482                         cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
483                 }
484                 else {
485                         cprintf("%d An unknown error has occurred.\n", ERROR);
486                 }
487                 return;
488         }
489
490         /* Begin by fetching the current version of the page.  We're going to patch
491          * backwards through the diffs until we get the one we want.
492          */
493         msgnum = CtdlLocateMessageByEuid(pagename, &CC->room);
494         if (msgnum > 0L) {
495                 msg = CtdlFetchMessage(msgnum, 1);
496         }
497         else {
498                 msg = NULL;
499         }
500
501         if ((msg != NULL) && (msg->cm_fields['M'] == NULL)) {
502                 CtdlFreeMessage(msg);
503                 msg = NULL;
504         }
505
506         if (msg == NULL) {
507                 cprintf("%d Page '%s' was not found.\n", ERROR+MESSAGE_NOT_FOUND, pagename);
508                 return;
509         }
510
511         /* Output it to a temporary file */
512
513         CtdlMakeTempFileName(temp, sizeof temp);
514         fp = fopen(temp, "w");
515         if (fp != NULL) {
516                 r = fwrite(msg->cm_fields['M'], strlen(msg->cm_fields['M']), 1, fp);
517                 fclose(fp);
518         }
519         else {
520                 CtdlLogPrintf(CTDL_ALERT, "Cannot open %s: %s\n", temp, strerror(errno));
521         }
522         CtdlFreeMessage(msg);
523
524         /* Get the revision history */
525
526         snprintf(history_page_name, sizeof history_page_name, "%s_HISTORY_", pagename);
527         msgnum = CtdlLocateMessageByEuid(history_page_name, &CC->room);
528         if (msgnum > 0L) {
529                 msg = CtdlFetchMessage(msgnum, 1);
530         }
531         else {
532                 msg = NULL;
533         }
534
535         if ((msg != NULL) && (msg->cm_fields['M'] == NULL)) {
536                 CtdlFreeMessage(msg);
537                 msg = NULL;
538         }
539
540         if (msg == NULL) {
541                 cprintf("%d Revision history for '%s' was not found.\n", ERROR+MESSAGE_NOT_FOUND, pagename);
542                 return;
543         }
544
545         /* Start patching backwards (newest to oldest) through the revision history until we
546          * hit the revision uuid requested by the user.  (The callback will perform each one.)
547          */
548
549         memset(&hecbd, 0, sizeof(struct HistoryEraserCallBackData));
550         hecbd.tempfilename = temp;
551         hecbd.stop_when = rev;
552
553         mime_parser(msg->cm_fields['M'], NULL, *wiki_rev_callback, NULL, NULL, (void *)&hecbd, 0);
554         CtdlFreeMessage(msg);
555
556         /* Were we successful? */
557         if (hecbd.done == 0) {
558                 cprintf("%d Revision '%s' of page '%s' was not found.\n",
559                         ERROR + MESSAGE_NOT_FOUND, rev, pagename
560                 );
561         }
562
563         /* We have the desired revision on disk.  Now do something with it. */
564
565         else if ( (!strcasecmp(operation, "fetch")) || (!strcasecmp(operation, "revert")) ) {
566                 msg = malloc(sizeof(struct CtdlMessage));
567                 memset(msg, 0, sizeof(struct CtdlMessage));
568                 msg->cm_magic = CTDLMESSAGE_MAGIC;
569                 msg->cm_anon_type = MES_NORMAL;
570                 msg->cm_format_type = FMT_RFC822;
571                 fp = fopen(temp, "r");
572                 if (fp) {
573                         fseek(fp, 0L, SEEK_END);
574                         len = ftell(fp);
575                         fseek(fp, 0L, SEEK_SET);
576                         msg->cm_fields['M'] = malloc(len + 1);
577                         rv = fread(msg->cm_fields['M'], len, 1, fp);
578                         CtdlLogPrintf(CTDL_DEBUG, "\033[32mdid %d blocks of %d bytes\033[0m\n", rv, len);
579                         msg->cm_fields['M'][len] = 0;
580                         fclose(fp);
581                 }
582                 if (len <= 0) {
583                         msgnum = (-1L);
584                 }
585                 else if (!strcasecmp(operation, "fetch")) {
586                         msg->cm_fields['A'] = strdup("Citadel");
587                         CtdlCreateRoom(wwm, 5, "", 0, 1, 1, VIEW_BBS);  /* Not an error if already exists */
588                         msgnum = CtdlSubmitMsg(msg, NULL, wwm, 0);      /* Store the revision here */
589                 }
590                 else if (!strcasecmp(operation, "revert")) {
591                         snprintf(timestamp, sizeof timestamp, "%ld", time(NULL));
592                         msg->cm_fields['T'] = strdup(timestamp);
593                         msg->cm_fields['A'] = strdup(CC->user.fullname);
594                         msg->cm_fields['F'] = strdup(CC->cs_inet_email);
595                         msg->cm_fields['O'] = strdup(CC->room.QRname);
596                         msg->cm_fields['N'] = strdup(NODENAME);
597                         msg->cm_fields['E'] = strdup(pagename);
598                         msgnum = CtdlSubmitMsg(msg, NULL, "", 0);       /* Replace the current revision */
599                 }
600                 else {
601                         /* Theoretically it is impossible to get here, but throw an error anyway */
602                         msgnum = (-1L);
603                 }
604                 CtdlFreeMessage(msg);
605                 if (msgnum >= 0L) {
606                         cprintf("%d %ld\n", CIT_OK, msgnum);            /* Give the client a msgnum */
607                 }
608                 else {
609                         cprintf("%d Error %ld has occurred.\n", ERROR+INTERNAL_ERROR, msgnum);
610                 }
611         }
612
613         /* We did all this work for nothing.  Express anguish to the caller. */
614         else {
615                 cprintf("%d An unknown operation was requested.\n", ERROR+CMD_NOT_SUPPORTED);
616         }
617
618         unlink(temp);
619         return;
620 }
621
622
623
624 /*
625  * commands related to wiki management
626  */
627 void cmd_wiki(char *argbuf) {
628         char subcmd[32];
629         char pagename[256];
630         char rev[128];
631         char operation[16];
632
633         extract_token(subcmd, argbuf, 0, '|', sizeof subcmd);
634
635         if (!strcasecmp(subcmd, "history")) {
636                 extract_token(pagename, argbuf, 1, '|', sizeof pagename);
637                 wiki_history(pagename);
638                 return;
639         }
640
641         if (!strcasecmp(subcmd, "rev")) {
642                 extract_token(pagename, argbuf, 1, '|', sizeof pagename);
643                 extract_token(rev, argbuf, 2, '|', sizeof rev);
644                 extract_token(operation, argbuf, 3, '|', sizeof operation);
645                 wiki_rev(pagename, rev, operation);
646                 return;
647         }
648
649         cprintf("%d Invalid subcommand\n", ERROR + CMD_NOT_SUPPORTED);
650 }
651
652
653
654 /*
655  * Module initialization
656  */
657 CTDL_MODULE_INIT(wiki)
658 {
659         if (!threading)
660         {
661                 CtdlRegisterMessageHook(wiki_upload_beforesave, EVT_BEFORESAVE);
662                 CtdlRegisterProtoHook(cmd_wiki, "WIKI", "Commands related to Wiki management");
663         }
664
665         /* return our Subversion id for the Log */
666         return "$Id$";
667 }