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