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