799a3f322dfcf69d89057ecf7ec833f35d4d604f
[citadel] / 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 "database.h"
49 #include "msgbase.h"
50 #include "euidindex.h"
51 #include "ctdl_module.h"
52
53 /*
54  * Data passed back and forth between wiki_rev() and its MIME parser callback
55  */
56 struct HistoryEraserCallBackData {
57         char *tempfilename;             /* name of temp file being patched */
58         char *stop_when;                /* stop when we hit this uuid */
59         int done;                       /* set to nonzero when we're done patching */
60 };
61
62 /*
63  * Name of the temporary room we create to store old revisions when someone requests them.
64  * We put it in an invalid namespace so the DAP cleans up after us later.
65  */
66 char *wwm = "9999999999.WikiWaybackMachine";
67
68 /*
69  * Before allowing a wiki page save to execute, we have to perform version control.
70  * This involves fetching the old version of the page if it exists.
71  */
72 int wiki_upload_beforesave(struct CtdlMessage *msg) {
73         struct CitContext *CCC = CC;
74         long old_msgnum = (-1L);
75         struct CtdlMessage *old_msg = NULL;
76         long history_msgnum = (-1L);
77         struct CtdlMessage *history_msg = NULL;
78         char diff_old_filename[PATH_MAX];
79         char diff_new_filename[PATH_MAX];
80         char diff_out_filename[PATH_MAX];
81         char diff_cmd[PATH_MAX];
82         FILE *fp;
83         int rv;
84         char history_page[1024];
85         char boundary[256];
86         char prefixed_boundary[258];
87         char buf[1024];
88         char *diffbuf = NULL;
89         size_t diffbuf_len = 0;
90         char *ptr = NULL;
91
92         if (!CCC->logged_in) return(0); /* Only do this if logged in. */
93
94         /* Is this a room with a Wiki in it, don't run this hook. */
95         if (CCC->room.QRdefaultview != VIEW_WIKI) {
96                 return(0);
97         }
98
99         /* If this isn't a MIME message, don't bother. */
100         if (msg->cm_format_type != 4) return(0);
101
102         /* If there's no EUID we can't do this.  Reject the post. */
103         if (msg->cm_fields[eExclusiveID] == NULL) return(1);
104
105         snprintf(history_page, sizeof history_page, "%s_HISTORY_", msg->cm_fields[eExclusiveID]);
106
107         /* Make sure we're saving a real wiki page rather than a wiki history page.
108          * This is important in order to avoid recursing infinitely into this hook.
109          */
110         if (    (strlen(msg->cm_fields[eExclusiveID]) >= 9)
111                 && (!strcasecmp(&msg->cm_fields[eExclusiveID][strlen(msg->cm_fields[eExclusiveID])-9], "_HISTORY_"))
112         ) {
113                 syslog(LOG_DEBUG, "History page not being historied\n");
114                 return(0);
115         }
116
117         /* If there's no message text, obviously this is all b0rken and shouldn't happen at all */
118         if (msg->cm_fields[eMesageText] == NULL) return(0);
119
120         /* Set the message subject identical to the page name */
121         if (msg->cm_fields[eMsgSubject] != NULL) {
122                 free(msg->cm_fields[eMsgSubject]);
123         }
124         msg->cm_fields[eMsgSubject] = strdup(msg->cm_fields[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) && (old_msg->cm_fields[eMesageText] == NULL)) {   /* old version is corrupt? */
136                 CtdlFreeMessage(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                 CtdlFreeMessage(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], strlen(old_msg->cm_fields[eMesageText]), 1, fp);
156                 fclose(fp);
157                 CtdlFreeMessage(old_msg);
158         }
159
160         fp = fopen(diff_new_filename, "w");
161         rv = fwrite(msg->cm_fields[eMesageText], strlen(msg->cm_fields[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                 history_msg = malloc(sizeof(struct CtdlMessage));
216                 memset(history_msg, 0, sizeof(struct CtdlMessage));
217                 history_msg->cm_magic = CTDLMESSAGE_MAGIC;
218                 history_msg->cm_anon_type = MES_NORMAL;
219                 history_msg->cm_format_type = FMT_RFC822;
220                 history_msg->cm_fields[eAuthor] = strdup("Citadel");
221                 history_msg->cm_fields[eRecipient] = strdup(CCC->room.QRname);
222                 history_msg->cm_fields[eExclusiveID] = strdup(history_page);
223                 history_msg->cm_fields[eMsgSubject] = strdup(history_page);
224                 history_msg->cm_fields[eSuppressIdx] = strdup("1");             /* suppress full text indexing */
225                 snprintf(boundary, sizeof boundary, "Citadel--Multipart--%04x--%08lx", getpid(), time(NULL));
226                 history_msg->cm_fields[eMesageText] = malloc(1024);
227                 snprintf(history_msg->cm_fields[eMesageText], 1024,
228                         "Content-type: multipart/mixed; boundary=\"%s\"\n\n"
229                         "This is a Citadel wiki history encoded as multipart MIME.\n"
230                         "Each part is comprised of a diff script representing one change set.\n"
231                         "\n"
232                         "--%s--\n"
233                         ,
234                         boundary, boundary
235                 );
236         }
237
238         /* Update the history message (regardless of whether it's new or existing) */
239
240         /* Remove the Message-ID from the old version of the history message.  This will cause a brand
241          * new one to be generated, avoiding an uninitentional hit of the loop zapper when we replicate.
242          */
243         if (history_msg->cm_fields[emessageId] != NULL) {
244                 free(history_msg->cm_fields[emessageId]);
245                 history_msg->cm_fields[emessageId] = NULL;
246         }
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         strcpy(boundary, "");
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                 snprintf(prefixed_boundary, sizeof prefixed_boundary, "--%s", boundary);
283                 history_msg->cm_fields[eMesageText] = realloc(history_msg->cm_fields[eMesageText],
284                         strlen(history_msg->cm_fields[eMesageText]) + strlen(diffbuf) + 1024
285                 );
286                 ptr = bmstrcasestr(history_msg->cm_fields[eMesageText], prefixed_boundary);
287                 if (ptr != NULL) {
288                         char *the_rest_of_it = strdup(ptr);
289                         char uuid[64];
290                         char memo[512];
291                         char encoded_memo[1024];
292                         generate_uuid(uuid);
293                         snprintf(memo, sizeof memo, "%s|%ld|%s|%s", 
294                                 uuid,
295                                 time(NULL),
296                                 CCC->user.fullname,
297                                 config.c_nodename
298                         );
299                         CtdlEncodeBase64(encoded_memo, memo, strlen(memo), 0);
300                         sprintf(ptr, "--%s\n"
301                                         "Content-type: text/plain\n"
302                                         "Content-Disposition: inline; filename=\"%s\"\n"
303                                         "Content-Transfer-Encoding: 8bit\n"
304                                         "\n"
305                                         "%s\n"
306                                         "%s"
307                                         ,
308                                 boundary,
309                                 encoded_memo,
310                                 diffbuf,
311                                 the_rest_of_it
312                         );
313                         free(the_rest_of_it);
314                 }
315
316                 history_msg->cm_fields[eTimestamp] = realloc(history_msg->cm_fields[eTimestamp], 32);
317                 if (history_msg->cm_fields[eTimestamp] != NULL) {
318                         snprintf(history_msg->cm_fields[eTimestamp], 32, "%ld", time(NULL));
319                 }
320         
321                 CtdlSubmitMsg(history_msg, NULL, "", 0);
322         }
323         else {
324                 syslog(LOG_ALERT, "Empty boundary string in history message.  No history!\n");
325         }
326
327         free(diffbuf);
328         free(history_msg);
329         return(0);
330 }
331
332
333 /*
334  * MIME Parser callback for wiki_history()
335  *
336  * The "filename" field will contain a memo field.  All we have to do is decode
337  * the base64 and output it.  The data is already in a delimited format suitable
338  * for our client protocol.
339  */
340 void wiki_history_callback(char *name, char *filename, char *partnum, char *disp,
341                    void *content, char *cbtype, char *cbcharset, size_t length,
342                    char *encoding, char *cbid, void *cbuserdata)
343 {
344         char memo[1024];
345
346         CtdlDecodeBase64(memo, filename, strlen(filename));
347         cprintf("%s\n", memo);
348 }
349
350
351 /*
352  * Fetch a list of revisions for a particular wiki page
353  */
354 void wiki_history(char *pagename) {
355         int r;
356         char history_page_name[270];
357         long msgnum;
358         struct CtdlMessage *msg;
359
360         r = CtdlDoIHavePermissionToReadMessagesInThisRoom();
361         if (r != om_ok) {
362                 if (r == om_not_logged_in) {
363                         cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
364                 }
365                 else {
366                         cprintf("%d An unknown error has occurred.\n", ERROR);
367                 }
368                 return;
369         }
370
371         snprintf(history_page_name, sizeof history_page_name, "%s_HISTORY_", pagename);
372         msgnum = CtdlLocateMessageByEuid(history_page_name, &CC->room);
373         if (msgnum > 0L) {
374                 msg = CtdlFetchMessage(msgnum, 1);
375         }
376         else {
377                 msg = NULL;
378         }
379
380         if ((msg != NULL) && (msg->cm_fields[eMesageText] == NULL)) {
381                 CtdlFreeMessage(msg);
382                 msg = NULL;
383         }
384
385         if (msg == NULL) {
386                 cprintf("%d Revision history for '%s' was not found.\n", ERROR+MESSAGE_NOT_FOUND, pagename);
387                 return;
388         }
389
390         
391         cprintf("%d Revision history for '%s'\n", LISTING_FOLLOWS, pagename);
392         mime_parser(msg->cm_fields[eMesageText], NULL, *wiki_history_callback, NULL, NULL, NULL, 0);
393         cprintf("000\n");
394
395         CtdlFreeMessage(msg);
396         return;
397 }
398
399 /*
400  * MIME Parser callback for wiki_rev()
401  *
402  * The "filename" field will contain a memo field, which includes (among other things)
403  * the uuid of this revision.  After we hit the desired revision, we stop processing.
404  *
405  * The "content" filed will contain "diff" output suitable for applying via "patch"
406  * to our temporary file.
407  */
408 void wiki_rev_callback(char *name, char *filename, char *partnum, char *disp,
409                    void *content, char *cbtype, char *cbcharset, size_t length,
410                    char *encoding, char *cbid, void *cbuserdata)
411 {
412         struct HistoryEraserCallBackData *hecbd = (struct HistoryEraserCallBackData *)cbuserdata;
413         char memo[1024];
414         char this_rev[256];
415         FILE *fp;
416         char *ptr = NULL;
417         char buf[1024];
418
419         /* Did a previous callback already indicate that we've reached our target uuid?
420          * If so, don't process anything else.
421          */
422         if (hecbd->done) {
423                 return;
424         }
425
426         CtdlDecodeBase64(memo, filename, strlen(filename));
427         extract_token(this_rev, memo, 0, '|', sizeof this_rev);
428         striplt(this_rev);
429
430         /* Perform the patch */
431         fp = popen(PATCH " -f -s -p0 -r /dev/null >/dev/null 2>/dev/null", "w");
432         if (fp) {
433                 /* Replace the filenames in the patch with the tempfilename we're actually tweaking */
434                 fprintf(fp, "--- %s\n", hecbd->tempfilename);
435                 fprintf(fp, "+++ %s\n", hecbd->tempfilename);
436
437                 ptr = (char *)content;
438                 int linenum = 0;
439                 do {
440                         ++linenum;
441                         ptr = memreadline(ptr, buf, sizeof buf);
442                         if (*ptr != 0) {
443                                 if (linenum <= 2) {
444                                         /* skip the first two lines; they contain bogus filenames */
445                                 }
446                                 else {
447                                         fprintf(fp, "%s\n", buf);
448                                 }
449                         }
450                 } while ((*ptr != 0) && (ptr < ((char*)content + length)));
451                 if (pclose(fp) != 0) {
452                         syslog(LOG_ERR, "pclose() returned an error - patch failed\n");
453                 }
454         }
455
456         if (!strcasecmp(this_rev, hecbd->stop_when)) {
457                 /* Found our target rev.  Tell any subsequent callbacks to suppress processing. */
458                 syslog(LOG_DEBUG, "Target revision has been reached -- stop patching.\n");
459                 hecbd->done = 1;
460         }
461 }
462
463
464 /*
465  * Fetch a specific revision of a wiki page.  The "operation" string may be set to "fetch" in order
466  * to simply fetch the desired revision and store it in a temporary location for viewing, or "revert"
467  * to revert the currently active page to that revision.
468  */
469 void wiki_rev(char *pagename, char *rev, char *operation)
470 {
471         int r;
472         char history_page_name[270];
473         long msgnum;
474         char temp[PATH_MAX];
475         char timestamp[64];
476         struct CtdlMessage *msg;
477         FILE *fp;
478         struct HistoryEraserCallBackData hecbd;
479         long len = 0L;
480         int rv;
481
482         r = CtdlDoIHavePermissionToReadMessagesInThisRoom();
483         if (r != om_ok) {
484                 if (r == om_not_logged_in) {
485                         cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
486                 }
487                 else {
488                         cprintf("%d An unknown error has occurred.\n", ERROR);
489                 }
490                 return;
491         }
492
493         if (!strcasecmp(operation, "revert")) {
494                 r = CtdlDoIHavePermissionToPostInThisRoom(temp, sizeof temp, NULL, POST_LOGGED_IN, 0);
495                 if (r != 0) {
496                         cprintf("%d %s\n", r, temp);
497                         return;
498                 }
499         }
500
501         /* Begin by fetching the current version of the page.  We're going to patch
502          * backwards through the diffs until we get the one we want.
503          */
504         msgnum = CtdlLocateMessageByEuid(pagename, &CC->room);
505         if (msgnum > 0L) {
506                 msg = CtdlFetchMessage(msgnum, 1);
507         }
508         else {
509                 msg = NULL;
510         }
511
512         if ((msg != NULL) && (msg->cm_fields[eMesageText] == NULL)) {
513                 CtdlFreeMessage(msg);
514                 msg = NULL;
515         }
516
517         if (msg == NULL) {
518                 cprintf("%d Page '%s' was not found.\n", ERROR+MESSAGE_NOT_FOUND, pagename);
519                 return;
520         }
521
522         /* Output it to a temporary file */
523
524         CtdlMakeTempFileName(temp, sizeof temp);
525         fp = fopen(temp, "w");
526         if (fp != NULL) {
527                 r = fwrite(msg->cm_fields[eMesageText], strlen(msg->cm_fields[eMesageText]), 1, fp);
528                 fclose(fp);
529         }
530         else {
531                 syslog(LOG_ALERT, "Cannot open %s: %s\n", temp, strerror(errno));
532         }
533         CtdlFreeMessage(msg);
534
535         /* Get the revision history */
536
537         snprintf(history_page_name, sizeof history_page_name, "%s_HISTORY_", pagename);
538         msgnum = CtdlLocateMessageByEuid(history_page_name, &CC->room);
539         if (msgnum > 0L) {
540                 msg = CtdlFetchMessage(msgnum, 1);
541         }
542         else {
543                 msg = NULL;
544         }
545
546         if ((msg != NULL) && (msg->cm_fields[eMesageText] == NULL)) {
547                 CtdlFreeMessage(msg);
548                 msg = NULL;
549         }
550
551         if (msg == NULL) {
552                 cprintf("%d Revision history for '%s' was not found.\n", ERROR+MESSAGE_NOT_FOUND, pagename);
553                 return;
554         }
555
556         /* Start patching backwards (newest to oldest) through the revision history until we
557          * hit the revision uuid requested by the user.  (The callback will perform each one.)
558          */
559
560         memset(&hecbd, 0, sizeof(struct HistoryEraserCallBackData));
561         hecbd.tempfilename = temp;
562         hecbd.stop_when = rev;
563         striplt(hecbd.stop_when);
564
565         mime_parser(msg->cm_fields[eMesageText], NULL, *wiki_rev_callback, NULL, NULL, (void *)&hecbd, 0);
566         CtdlFreeMessage(msg);
567
568         /* Were we successful? */
569         if (hecbd.done == 0) {
570                 cprintf("%d Revision '%s' of page '%s' was not found.\n",
571                         ERROR + MESSAGE_NOT_FOUND, rev, pagename
572                 );
573         }
574
575         /* We have the desired revision on disk.  Now do something with it. */
576
577         else if ( (!strcasecmp(operation, "fetch")) || (!strcasecmp(operation, "revert")) ) {
578                 msg = malloc(sizeof(struct CtdlMessage));
579                 memset(msg, 0, sizeof(struct CtdlMessage));
580                 msg->cm_magic = CTDLMESSAGE_MAGIC;
581                 msg->cm_anon_type = MES_NORMAL;
582                 msg->cm_format_type = FMT_RFC822;
583                 fp = fopen(temp, "r");
584                 if (fp) {
585                         fseek(fp, 0L, SEEK_END);
586                         len = ftell(fp);
587                         fseek(fp, 0L, SEEK_SET);
588                         msg->cm_fields[eMesageText] = malloc(len + 1);
589                         rv = fread(msg->cm_fields[eMesageText], len, 1, fp);
590                         syslog(LOG_DEBUG, "did %d blocks of %ld bytes\n", rv, len);
591                         msg->cm_fields[eMesageText][len] = 0;
592                         fclose(fp);
593                 }
594                 if (len <= 0) {
595                         msgnum = (-1L);
596                 }
597                 else if (!strcasecmp(operation, "fetch")) {
598                         msg->cm_fields[eAuthor] = strdup("Citadel");
599                         CtdlCreateRoom(wwm, 5, "", 0, 1, 1, VIEW_BBS);  /* Not an error if already exists */
600                         msgnum = CtdlSubmitMsg(msg, NULL, wwm, 0);      /* Store the revision here */
601
602                         /*
603                          * WARNING: VILE SLEAZY HACK
604                          * This will avoid the 'message xxx is not in this room' security error,
605                          * but only if the client fetches the message we just generated immediately
606                          * without first trying to perform other fetch operations.
607                          */
608                         if (CC->cached_msglist != NULL) {
609                                 free(CC->cached_msglist);
610                                 CC->cached_msglist = NULL;
611                                 CC->cached_num_msgs = 0;
612                         }
613                         CC->cached_msglist = malloc(sizeof(long));
614                         if (CC->cached_msglist != NULL) {
615                                 CC->cached_num_msgs = 1;
616                                 CC->cached_msglist[0] = msgnum;
617                         }
618
619                 }
620                 else if (!strcasecmp(operation, "revert")) {
621                         snprintf(timestamp, sizeof timestamp, "%ld", time(NULL));
622                         msg->cm_fields[eTimestamp] = strdup(timestamp);
623                         msg->cm_fields[eAuthor] = strdup(CC->user.fullname);
624                         msg->cm_fields[erFc822Addr] = strdup(CC->cs_inet_email);
625                         msg->cm_fields[eOriginalRoom] = strdup(CC->room.QRname);
626                         msg->cm_fields[eNodeName] = strdup(NODENAME);
627                         msg->cm_fields[eExclusiveID] = strdup(pagename);
628                         msgnum = CtdlSubmitMsg(msg, NULL, "", 0);       /* Replace the current revision */
629                 }
630                 else {
631                         /* Theoretically it is impossible to get here, but throw an error anyway */
632                         msgnum = (-1L);
633                 }
634                 CtdlFreeMessage(msg);
635                 if (msgnum >= 0L) {
636                         cprintf("%d %ld\n", CIT_OK, msgnum);            /* Give the client a msgnum */
637                 }
638                 else {
639                         cprintf("%d Error %ld has occurred.\n", ERROR+INTERNAL_ERROR, msgnum);
640                 }
641         }
642
643         /* We did all this work for nothing.  Express anguish to the caller. */
644         else {
645                 cprintf("%d An unknown operation was requested.\n", ERROR+CMD_NOT_SUPPORTED);
646         }
647
648         unlink(temp);
649         return;
650 }
651
652
653
654 /*
655  * commands related to wiki management
656  */
657 void cmd_wiki(char *argbuf) {
658         char subcmd[32];
659         char pagename[256];
660         char rev[128];
661         char operation[16];
662
663         extract_token(subcmd, argbuf, 0, '|', sizeof subcmd);
664
665         if (!strcasecmp(subcmd, "history")) {
666                 extract_token(pagename, argbuf, 1, '|', sizeof pagename);
667                 wiki_history(pagename);
668                 return;
669         }
670
671         if (!strcasecmp(subcmd, "rev")) {
672                 extract_token(pagename, argbuf, 1, '|', sizeof pagename);
673                 extract_token(rev, argbuf, 2, '|', sizeof rev);
674                 extract_token(operation, argbuf, 3, '|', sizeof operation);
675                 wiki_rev(pagename, rev, operation);
676                 return;
677         }
678
679         cprintf("%d Invalid subcommand\n", ERROR + CMD_NOT_SUPPORTED);
680 }
681
682
683
684 /*
685  * Module initialization
686  */
687 CTDL_MODULE_INIT(wiki)
688 {
689         if (!threading)
690         {
691                 CtdlRegisterMessageHook(wiki_upload_beforesave, EVT_BEFORESAVE);
692                 CtdlRegisterProtoHook(cmd_wiki, "WIKI", "Commands related to Wiki management");
693         }
694
695         /* return our module name for the log */
696         return "wiki";
697 }