bf18afd485573fd2a809e41047de076dbc2c577c
[citadel.git] / citadel / server / modules / wiki / serv_wiki.c
1 /*
2  * Server-side module for Wiki rooms.  This handles things like version control. 
3  * 
4  * Copyright (c) 2009-2023 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 #include <time.h>
26 #include <sys/wait.h>
27 #include <string.h>
28 #include <limits.h>
29 #include <libcitadel.h>
30 #include "../../citadel_defs.h"
31 #include "../../server.h"
32 #include "../../citserver.h"
33 #include "../../support.h"
34 #include "../../config.h"
35 #include "../../control.h"
36 #include "../../user_ops.h"
37 #include "../../room_ops.h"
38 #include "../../database.h"
39 #include "../../msgbase.h"
40 #include "../../euidindex.h"
41 #include "../../ctdl_module.h"
42
43 /*
44  * Data passed back and forth between wiki_rev() and its MIME parser callback
45  */
46 struct HistoryEraserCallBackData {
47         char *tempfilename;             /* name of temp file being patched */
48         char *stop_when;                /* stop when we hit this uuid */
49         int done;                       /* set to nonzero when we're done patching */
50 };
51
52 /*
53  * Name of the temporary room we create to store old revisions when someone requests them.
54  * We put it in an invalid namespace so the DAP cleans up after us later.
55  */
56 char *wwm = "9999999999.WikiWaybackMachine";
57
58 /*
59  * Before allowing a wiki page save to execute, we have to perform version control.
60  * This involves fetching the old version of the page if it exists.
61  */
62 int wiki_upload_beforesave(struct CtdlMessage *msg, struct recptypes *recp) {
63         long old_msgnum = (-1L);
64         struct CtdlMessage *old_msg = NULL;
65         long history_msgnum = (-1L);
66         struct CtdlMessage *history_msg = NULL;
67         char diff_old_filename[PATH_MAX];
68         char diff_new_filename[PATH_MAX];
69         char diff_out_filename[PATH_MAX];
70         char diff_cmd[PATH_MAX];
71         FILE *fp;
72         int rv;
73         char history_page[1024];
74         long history_page_len;
75         char boundary[256];
76         char prefixed_boundary[258];
77         char buf[1024];
78         char *diffbuf = NULL;
79         size_t diffbuf_len = 0;
80         char *ptr = NULL;
81         long newmsgid;
82         StrBuf *msgidbuf;
83
84         if (!CC->logged_in) return(0);  /* Only do this if logged in. */
85
86         /* Is this a room with a Wiki in it, don't run this hook. */
87         if (CC->room.QRdefaultview != VIEW_WIKI) {
88                 return(0);
89         }
90
91         /* If this isn't a MIME message, don't bother. */
92         if (msg->cm_format_type != 4) return(0);
93
94         /* If there's no EUID we can't do this.  Reject the post. */
95         if (CM_IsEmpty(msg, eExclusiveID)) return(1);
96
97         newmsgid = get_new_message_number();
98         msgidbuf = NewStrBuf();
99         StrBufPrintf(msgidbuf, "%08lX-%08lX@%s/%s",
100                      (long unsigned int) time(NULL),
101                      (long unsigned int) newmsgid,
102                      CtdlGetConfigStr("c_fqdn"),
103                      msg->cm_fields[eExclusiveID]
104                 );
105
106         CM_SetAsFieldSB(msg, emessageId, &msgidbuf);
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], &CC->room);
129         if (old_msgnum > 0L) {
130                 old_msg = CtdlFetchMessage(old_msgnum, 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, &CC->room);
209         history_msg = NULL;
210         if (history_msgnum > 0L) {
211                 history_msg = CtdlFetchMessage(history_msgnum, 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, "Citadel");
225                 if (!IsEmptyStr(CC->room.QRname)){
226                         CM_SetField(history_msg, eRecipient, CC->room.QRname);
227                 }
228                 CM_SetField(history_msg, eExclusiveID, history_page);
229                 CM_SetField(history_msg, eMsgSubject, history_page);
230                 CM_SetField(history_msg, eSuppressIdx, "1"); /* suppress full text indexing */
231                 snprintf(boundary, sizeof boundary, "Citadel--Multipart--%04x--%08lx", getpid(), time(NULL));
232                 buf = (char*) malloc(1024);
233                 len = snprintf(buf, 1024,
234                                "Content-type: multipart/mixed; boundary=\"%s\"\n\n"
235                                "This is a Citadel wiki history encoded as multipart MIME.\n"
236                                "Each part is comprised of a diff script representing one change set.\n"
237                                "\n"
238                                "--%s--\n",
239                                boundary, boundary
240                 );
241                 CM_SetAsField(history_msg, eMesageText, &buf, len);
242         }
243
244         /* Update the history message (regardless of whether it's new or existing) */
245
246         /* Remove the Message-ID from the old version of the history message.  This will cause a brand
247          * new one to be generated, avoiding an uninitentional hit of the loop zapper when we replicate.
248          */
249         CM_FlushField(history_msg, emessageId);
250
251         /* Figure out the boundary string.  We do this even when we generated the
252          * boundary string in the above code, just to be safe and consistent.
253          */
254         *boundary = '\0';
255
256         ptr = history_msg->cm_fields[eMesageText];
257         do {
258                 ptr = memreadline(ptr, buf, sizeof buf);
259                 if (*ptr != 0) {
260                         string_trim(buf);
261                         if (!IsEmptyStr(buf) && (!strncasecmp(buf, "Content-type:", 13))) {
262                                 if (
263                                         (bmstrcasestr(buf, "multipart") != NULL)
264                                         && (bmstrcasestr(buf, "boundary=") != NULL)
265                                 ) {
266                                         safestrncpy(boundary, bmstrcasestr(buf, "\""), sizeof boundary);
267                                         char *qu;
268                                         qu = strchr(boundary, '\"');
269                                         if (qu) {
270                                                 strcpy(boundary, ++qu);
271                                         }
272                                         qu = strchr(boundary, '\"');
273                                         if (qu) {
274                                                 *qu = 0;
275                                         }
276                                 }
277                         }
278                 }
279         } while ( (IsEmptyStr(boundary)) && (*ptr != 0) );
280
281         /*
282          * Now look for the first boundary.  That is where we need to insert our fun.
283          */
284         if (!IsEmptyStr(boundary)) {
285                 char *MsgText;
286                 long MsgTextLen;
287                 time_t Now = time(NULL);
288
289                 snprintf(prefixed_boundary, sizeof(prefixed_boundary), "--%s", boundary);
290                 
291                 CM_GetAsField(history_msg, eMesageText, &MsgText, &MsgTextLen);
292
293                 ptr = bmstrcasestr(MsgText, prefixed_boundary);
294                 if (ptr != NULL) {
295                         StrBuf *NewMsgText;
296                         char uuid[64];
297                         char memo[512];
298                         long memolen;
299                         char encoded_memo[1024];
300                         
301                         NewMsgText = NewStrBufPlain(NULL, MsgTextLen + diffbuf_len + 1024);
302
303                         generate_uuid(uuid);
304                         memolen = snprintf(memo, sizeof(memo), "%s|%ld|%s|%s", 
305                                            uuid,
306                                            Now,
307                                            CC->user.fullname,
308                                            CtdlGetConfigStr("c_nodename"));
309
310                         memolen = CtdlEncodeBase64(encoded_memo, memo, memolen, BASE64_YES_LINEBREAKS);
311
312                         StrBufAppendBufPlain(NewMsgText, HKEY("--"), 0);
313                         StrBufAppendBufPlain(NewMsgText, boundary, -1, 0);
314                         StrBufAppendBufPlain(
315                                 NewMsgText, 
316                                 HKEY("\n"
317                                      "Content-type: text/plain\n"
318                                      "Content-Disposition: inline; filename=\""), 0);
319
320                         StrBufAppendBufPlain(NewMsgText, encoded_memo, memolen, 0);
321
322                         StrBufAppendBufPlain(
323                                 NewMsgText, 
324                                 HKEY("\"\n"
325                                      "Content-Transfer-Encoding: 8bit\n"
326                                      "\n"), 0);
327
328                         StrBufAppendBufPlain(NewMsgText, diffbuf, diffbuf_len, 0);
329                         StrBufAppendBufPlain(NewMsgText, HKEY("\n"), 0);
330
331                         StrBufAppendBufPlain(NewMsgText, ptr, MsgTextLen - (ptr - MsgText), 0);
332                         free(MsgText);
333                         CM_SetAsFieldSB(history_msg, eMesageText, &NewMsgText); 
334                 }
335                 else {
336                         CM_SetAsField(history_msg, eMesageText, &MsgText, MsgTextLen); 
337                 }
338
339                 CM_SetFieldLONG(history_msg, eTimestamp, Now);
340         
341                 CtdlSubmitMsg(history_msg, NULL, "");
342         }
343         else {
344                 syslog(LOG_ALERT, "Empty boundary string in history message.  No history!\n");
345         }
346
347         free(diffbuf);
348         CM_Free(history_msg);
349         return(0);
350 }
351
352
353 /*
354  * MIME Parser callback for wiki_history()
355  *
356  * The "filename" field will contain a memo field.  All we have to do is decode
357  * the base64 and output it.  The data is already in a delimited format suitable
358  * for our client protocol.
359  */
360 void wiki_history_callback(char *name, char *filename, char *partnum, char *disp,
361                    void *content, char *cbtype, char *cbcharset, size_t length,
362                    char *encoding, char *cbid, void *cbuserdata)
363 {
364         char memo[1024];
365
366         CtdlDecodeBase64(memo, filename, strlen(filename));
367         cprintf("%s\n", memo);
368 }
369
370
371 /*
372  * Fetch a list of revisions for a particular wiki page
373  */
374 void wiki_history(char *pagename) {
375         int r;
376         char history_page_name[270];
377         long msgnum;
378         struct CtdlMessage *msg;
379
380         r = CtdlDoIHavePermissionToReadMessagesInThisRoom();
381         if (r != om_ok) {
382                 if (r == om_not_logged_in) {
383                         cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
384                 }
385                 else {
386                         cprintf("%d An unknown error has occurred.\n", ERROR);
387                 }
388                 return;
389         }
390
391         snprintf(history_page_name, sizeof history_page_name, "%s_HISTORY_", pagename);
392         msgnum = CtdlLocateMessageByEuid(history_page_name, &CC->room);
393         if (msgnum > 0L) {
394                 msg = CtdlFetchMessage(msgnum, 1);
395         }
396         else {
397                 msg = NULL;
398         }
399
400         if ((msg != NULL) && CM_IsEmpty(msg, eMesageText)) {
401                 CM_Free(msg);
402                 msg = NULL;
403         }
404
405         if (msg == NULL) {
406                 cprintf("%d Revision history for '%s' was not found.\n", ERROR+MESSAGE_NOT_FOUND, pagename);
407                 return;
408         }
409
410         
411         cprintf("%d Revision history for '%s'\n", LISTING_FOLLOWS, pagename);
412         mime_parser(CM_RANGE(msg, eMesageText), *wiki_history_callback, NULL, NULL, NULL, 0);
413         cprintf("000\n");
414
415         CM_Free(msg);
416         return;
417 }
418
419 /*
420  * MIME Parser callback for wiki_rev()
421  *
422  * The "filename" field will contain a memo field, which includes (among other things)
423  * the uuid of this revision.  After we hit the desired revision, we stop processing.
424  *
425  * The "content" filed will contain "diff" output suitable for applying via "patch"
426  * to our temporary file.
427  */
428 void wiki_rev_callback(char *name, char *filename, char *partnum, char *disp,
429                    void *content, char *cbtype, char *cbcharset, size_t length,
430                    char *encoding, char *cbid, void *cbuserdata)
431 {
432         struct HistoryEraserCallBackData *hecbd = (struct HistoryEraserCallBackData *)cbuserdata;
433         char memo[1024];
434         char this_rev[256];
435         FILE *fp;
436         char *ptr = NULL;
437         char buf[1024];
438
439         /* Did a previous callback already indicate that we've reached our target uuid?
440          * If so, don't process anything else.
441          */
442         if (hecbd->done) {
443                 return;
444         }
445
446         CtdlDecodeBase64(memo, filename, strlen(filename));
447         extract_token(this_rev, memo, 0, '|', sizeof this_rev);
448         string_trim(this_rev);
449
450         /* Perform the patch */
451         fp = popen("patch -f -s -p0 -r /dev/null >/dev/null 2>/dev/null", "w");
452         if (fp) {
453                 /* Replace the filenames in the patch with the tempfilename we're actually tweaking */
454                 fprintf(fp, "--- %s\n", hecbd->tempfilename);
455                 fprintf(fp, "+++ %s\n", hecbd->tempfilename);
456
457                 ptr = (char *)content;
458                 int linenum = 0;
459                 do {
460                         ++linenum;
461                         ptr = memreadline(ptr, buf, sizeof buf);
462                         if (*ptr != 0) {
463                                 if (linenum <= 2) {
464                                         /* skip the first two lines; they contain bogus filenames */
465                                 }
466                                 else {
467                                         fprintf(fp, "%s\n", buf);
468                                 }
469                         }
470                 } while ((*ptr != 0) && (ptr < ((char*)content + length)));
471                 if (pclose(fp) != 0) {
472                         syslog(LOG_ERR, "pclose() returned an error - patch failed\n");
473                 }
474         }
475
476         if (!strcasecmp(this_rev, hecbd->stop_when)) {
477                 /* Found our target rev.  Tell any subsequent callbacks to suppress processing. */
478                 syslog(LOG_DEBUG, "Target revision has been reached -- stop patching.\n");
479                 hecbd->done = 1;
480         }
481 }
482
483
484 /*
485  * Fetch a specific revision of a wiki page.  The "operation" string may be set to "fetch" in order
486  * to simply fetch the desired revision and store it in a temporary location for viewing, or "revert"
487  * to revert the currently active page to that revision.
488  */
489 void wiki_rev(char *pagename, char *rev, char *operation)
490 {
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, 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, &CC->room);
524         if (msgnum > 0L) {
525                 msg = CtdlFetchMessage(msgnum, 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_ERR, "%s: %m", temp);
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, &CC->room);
558         if (msgnum > 0L) {
559                 msg = CtdlFetchMessage(msgnum, 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         string_trim(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, "Citadel");
620                         CtdlCreateRoom(wwm, 5, "", 0, 1, 1, VIEW_BBS);  /* Not an error if already exists */
621                         msgnum = CtdlSubmitMsg(msg, NULL, wwm);         /* 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 (CC->cached_msglist != NULL) {
630                                 free(CC->cached_msglist);
631                                 CC->cached_msglist = NULL;
632                                 CC->cached_num_msgs = 0;
633                         }
634                         CC->cached_msglist = malloc(sizeof(long));
635                         if (CC->cached_msglist != NULL) {
636                                 CC->cached_num_msgs = 1;
637                                 CC->cached_msglist[0] = msgnum;
638                         }
639
640                 }
641                 else if (!strcasecmp(operation, "revert")) {
642                         CM_SetFieldLONG(msg, eTimestamp, time(NULL));
643                         if (!IsEmptyStr(CC->user.fullname)) {
644                                 CM_SetField(msg, eAuthor, CC->user.fullname);
645                         }
646
647                         if (!IsEmptyStr(CC->cs_inet_email)) {
648                                 CM_SetField(msg, erFc822Addr, CC->cs_inet_email);
649                         }
650
651                         if (!IsEmptyStr(CC->room.QRname)) {
652                                 CM_SetField(msg, eOriginalRoom, CC->room.QRname);
653                         }
654
655                         if (!IsEmptyStr(pagename)) {
656                                 CM_SetField(msg, eExclusiveID, pagename);
657                         }
658                         msgnum = CtdlSubmitMsg(msg, NULL, "");          /* Replace the current revision */
659                 }
660                 else {
661                         /* Theoretically it is impossible to get here, but throw an error anyway */
662                         msgnum = (-1L);
663                 }
664                 CM_Free(msg);
665                 if (msgnum >= 0L) {
666                         cprintf("%d %ld\n", CIT_OK, msgnum);            /* Give the client a msgnum */
667                 }
668                 else {
669                         cprintf("%d Error %ld has occurred.\n", ERROR+INTERNAL_ERROR, msgnum);
670                 }
671         }
672
673         /* We did all this work for nothing.  Express anguish to the caller. */
674         else {
675                 cprintf("%d An unknown operation was requested.\n", ERROR+CMD_NOT_SUPPORTED);
676         }
677
678         unlink(temp);
679         return;
680 }
681
682
683
684 /*
685  * commands related to wiki management
686  */
687 void cmd_wiki(char *argbuf) {
688         char subcmd[32];
689         char pagename[256];
690         char rev[128];
691         char operation[16];
692
693         extract_token(subcmd, argbuf, 0, '|', sizeof subcmd);
694
695         if (!strcasecmp(subcmd, "history")) {
696                 extract_token(pagename, argbuf, 1, '|', sizeof pagename);
697                 wiki_history(pagename);
698                 return;
699         }
700
701         if (!strcasecmp(subcmd, "rev")) {
702                 extract_token(pagename, argbuf, 1, '|', sizeof pagename);
703                 extract_token(rev, argbuf, 2, '|', sizeof rev);
704                 extract_token(operation, argbuf, 3, '|', sizeof operation);
705                 wiki_rev(pagename, rev, operation);
706                 return;
707         }
708
709         cprintf("%d Invalid subcommand\n", ERROR + CMD_NOT_SUPPORTED);
710 }
711
712
713
714 // Initialization function, called from modules_init.c
715 char *ctdl_module_init_wiki() {
716         if (!threading) {
717                 CtdlRegisterMessageHook(wiki_upload_beforesave, EVT_BEFORESAVE);
718                 CtdlRegisterProtoHook(cmd_wiki, "WIKI", "Commands related to Wiki management");
719         }
720
721         /* return our module name for the log */
722         return "wiki";
723 }