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