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