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