2 * Server-side module for Wiki rooms. This handles things like version control.
4 * Copyright (c) 2009-2012 by the citadel.org team
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.
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.
24 #include <sys/types.h>
26 #if TIME_WITH_SYS_TIME
27 # include <sys/time.h>
31 # include <sys/time.h>
40 #include <libcitadel.h>
43 #include "citserver.h"
50 #include "euidindex.h"
51 #include "ctdl_module.h"
54 * Data passed back and forth between wiki_rev() and its MIME parser callback
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 */
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.
66 char *wwm = "9999999999.WikiWaybackMachine";
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.
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];
84 char history_page[1024];
85 long history_page_len;
87 char prefixed_boundary[258];
90 size_t diffbuf_len = 0;
93 if (!CCC->logged_in) return(0); /* Only do this if logged in. */
95 /* Is this a room with a Wiki in it, don't run this hook. */
96 if (CCC->room.QRdefaultview != VIEW_WIKI) {
100 /* If this isn't a MIME message, don't bother. */
101 if (msg->cm_format_type != 4) return(0);
103 /* If there's no EUID we can't do this. Reject the post. */
104 if (CM_IsEmpty(msg, eExclusiveID)) return(1);
106 history_page_len = snprintf(history_page, sizeof history_page,
107 "%s_HISTORY_", msg->cm_fields[eExclusiveID]);
109 /* Make sure we're saving a real wiki page rather than a wiki history page.
110 * This is important in order to avoid recursing infinitely into this hook.
112 if ( (strlen(msg->cm_fields[eExclusiveID]) >= 9)
113 && (!strcasecmp(&msg->cm_fields[eExclusiveID][strlen(msg->cm_fields[eExclusiveID])-9], "_HISTORY_"))
115 syslog(LOG_DEBUG, "History page not being historied\n");
119 /* If there's no message text, obviously this is all b0rken and shouldn't happen at all */
120 if (CM_IsEmpty(msg, eMesageText)) return(0);
122 /* Set the message subject identical to the page name */
123 CM_CopyField(msg, eMsgSubject, eExclusiveID);
125 /* See if we can retrieve the previous version. */
126 old_msgnum = CtdlLocateMessageByEuid(msg->cm_fields[eExclusiveID], &CCC->room);
127 if (old_msgnum > 0L) {
128 old_msg = CtdlFetchMessage(old_msgnum, 1);
134 if ((old_msg != NULL) && (CM_IsEmpty(old_msg, eMesageText))) { /* old version is corrupt? */
139 /* If no changes were made, don't bother saving it again */
140 if ((old_msg != NULL) && (!strcmp(msg->cm_fields[eMesageText], old_msg->cm_fields[eMesageText]))) {
148 CtdlMakeTempFileName(diff_old_filename, sizeof diff_old_filename);
149 CtdlMakeTempFileName(diff_new_filename, sizeof diff_new_filename);
150 CtdlMakeTempFileName(diff_out_filename, sizeof diff_out_filename);
152 if (old_msg != NULL) {
153 fp = fopen(diff_old_filename, "w");
154 rv = fwrite(old_msg->cm_fields[eMesageText], strlen(old_msg->cm_fields[eMesageText]), 1, fp);
159 fp = fopen(diff_new_filename, "w");
160 rv = fwrite(msg->cm_fields[eMesageText], strlen(msg->cm_fields[eMesageText]), 1, fp);
163 snprintf(diff_cmd, sizeof diff_cmd,
164 DIFF " -u %s %s >%s",
166 ((old_msg != NULL) ? diff_old_filename : "/dev/null"),
169 syslog(LOG_DEBUG, "diff cmd: %s", diff_cmd);
170 rv = system(diff_cmd);
171 syslog(LOG_DEBUG, "diff cmd returned %d", rv);
175 fp = fopen(diff_out_filename, "r");
177 fp = fopen("/dev/null", "r");
180 fseek(fp, 0L, SEEK_END);
181 diffbuf_len = ftell(fp);
182 fseek(fp, 0L, SEEK_SET);
183 diffbuf = malloc(diffbuf_len + 1);
184 fread(diffbuf, diffbuf_len, 1, fp);
185 diffbuf[diffbuf_len] = '\0';
189 syslog(LOG_DEBUG, "diff length is "SIZE_T_FMT" bytes", diffbuf_len);
191 unlink(diff_old_filename);
192 unlink(diff_new_filename);
193 unlink(diff_out_filename);
195 /* Determine whether this was a bogus (empty) edit */
196 if ((diffbuf_len = 0) && (diffbuf != NULL)) {
200 if (diffbuf == NULL) {
201 return(1); /* No changes at all? Abandon the post entirely! */
204 /* Now look for the existing edit history */
206 history_msgnum = CtdlLocateMessageByEuid(history_page, &CCC->room);
208 if (history_msgnum > 0L) {
209 history_msg = CtdlFetchMessage(history_msgnum, 1);
212 /* Create a new history message if necessary */
213 if (history_msg == NULL) {
217 history_msg = malloc(sizeof(struct CtdlMessage));
218 memset(history_msg, 0, sizeof(struct CtdlMessage));
219 history_msg->cm_magic = CTDLMESSAGE_MAGIC;
220 history_msg->cm_anon_type = MES_NORMAL;
221 history_msg->cm_format_type = FMT_RFC822;
222 CM_SetField(history_msg, eAuthor, HKEY("Citadel"));
223 CM_SetField(history_msg, eRecipient, CCC->room.QRname, strlen(CCC->room.QRname));
224 CM_SetField(history_msg, eExclusiveID, history_page, history_page_len);
225 CM_SetField(history_msg, eMsgSubject, history_page, history_page_len);
226 CM_SetField(history_msg, eSuppressIdx, HKEY("1")); /* suppress full text indexing */
227 snprintf(boundary, sizeof boundary, "Citadel--Multipart--%04x--%08lx", getpid(), time(NULL));
228 buf = (char*) malloc(1024);
229 len = snprintf(buf, 1024,
230 "Content-type: multipart/mixed; boundary=\"%s\"\n\n"
231 "This is a Citadel wiki history encoded as multipart MIME.\n"
232 "Each part is comprised of a diff script representing one change set.\n"
237 CM_SetAsField(history_msg, eMesageText, &buf, len);
240 /* Update the history message (regardless of whether it's new or existing) */
242 /* Remove the Message-ID from the old version of the history message. This will cause a brand
243 * new one to be generated, avoiding an uninitentional hit of the loop zapper when we replicate.
245 CM_FlushField(history_msg, emessageId);
247 /* Figure out the boundary string. We do this even when we generated the
248 * boundary string in the above code, just to be safe and consistent.
252 ptr = history_msg->cm_fields[eMesageText];
254 ptr = memreadline(ptr, buf, sizeof buf);
257 if (!IsEmptyStr(buf) && (!strncasecmp(buf, "Content-type:", 13))) {
259 (bmstrcasestr(buf, "multipart") != NULL)
260 && (bmstrcasestr(buf, "boundary=") != NULL)
262 safestrncpy(boundary, bmstrcasestr(buf, "\""), sizeof boundary);
264 qu = strchr(boundary, '\"');
266 strcpy(boundary, ++qu);
268 qu = strchr(boundary, '\"');
275 } while ( (IsEmptyStr(boundary)) && (*ptr != 0) );
278 * Now look for the first boundary. That is where we need to insert our fun.
280 if (!IsEmptyStr(boundary)) {
283 time_t Now = time(NULL);
285 snprintf(prefixed_boundary, sizeof(prefixed_boundary), "--%s", boundary);
287 CM_GetAsField(history_msg, eMesageText, &MsgText, &MsgTextLen);
289 ptr = bmstrcasestr(MsgText, prefixed_boundary);
295 char encoded_memo[1024];
297 NewMsgText = NewStrBufPlain(NULL, MsgTextLen + diffbuf_len + 1024);
300 memolen = snprintf(memo, sizeof(memo), "%s|%ld|%s|%s",
306 memolen = CtdlEncodeBase64(encoded_memo, memo, memolen, 0);
308 StrBufAppendBufPlain(NewMsgText, HKEY("--"), 0);
309 StrBufAppendBufPlain(NewMsgText, boundary, -1, 0);
310 StrBufAppendBufPlain(
313 "Content-type: text/plain\n"
314 "Content-Disposition: inline; filename=\""), 0);
316 StrBufAppendBufPlain(NewMsgText, encoded_memo, memolen, 0);
318 StrBufAppendBufPlain(
321 "Content-Transfer-Encoding: 8bit\n"
324 StrBufAppendBufPlain(NewMsgText, diffbuf, diffbuf_len, 0);
325 StrBufAppendBufPlain(NewMsgText, HKEY("\n"), 0);
327 StrBufAppendBufPlain(NewMsgText, ptr, MsgTextLen - (ptr - MsgText), 0);
329 CM_SetAsFieldSB(history_msg, eMesageText, &NewMsgText);
333 CM_SetAsField(history_msg, eMesageText, &MsgText, MsgTextLen);
336 CM_SetFieldLONG(history_msg, eTimestamp, Now);
338 CtdlSubmitMsg(history_msg, NULL, "", 0);
341 syslog(LOG_ALERT, "Empty boundary string in history message. No history!\n");
345 CM_Free(history_msg);
351 * MIME Parser callback for wiki_history()
353 * The "filename" field will contain a memo field. All we have to do is decode
354 * the base64 and output it. The data is already in a delimited format suitable
355 * for our client protocol.
357 void wiki_history_callback(char *name, char *filename, char *partnum, char *disp,
358 void *content, char *cbtype, char *cbcharset, size_t length,
359 char *encoding, char *cbid, void *cbuserdata)
363 CtdlDecodeBase64(memo, filename, strlen(filename));
364 cprintf("%s\n", memo);
369 * Fetch a list of revisions for a particular wiki page
371 void wiki_history(char *pagename) {
373 char history_page_name[270];
375 struct CtdlMessage *msg;
377 r = CtdlDoIHavePermissionToReadMessagesInThisRoom();
379 if (r == om_not_logged_in) {
380 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
383 cprintf("%d An unknown error has occurred.\n", ERROR);
388 snprintf(history_page_name, sizeof history_page_name, "%s_HISTORY_", pagename);
389 msgnum = CtdlLocateMessageByEuid(history_page_name, &CC->room);
391 msg = CtdlFetchMessage(msgnum, 1);
397 if ((msg != NULL) && CM_IsEmpty(msg, eMesageText)) {
403 cprintf("%d Revision history for '%s' was not found.\n", ERROR+MESSAGE_NOT_FOUND, pagename);
408 cprintf("%d Revision history for '%s'\n", LISTING_FOLLOWS, pagename);
409 mime_parser(msg->cm_fields[eMesageText], NULL, *wiki_history_callback, NULL, NULL, NULL, 0);
417 * MIME Parser callback for wiki_rev()
419 * The "filename" field will contain a memo field, which includes (among other things)
420 * the uuid of this revision. After we hit the desired revision, we stop processing.
422 * The "content" filed will contain "diff" output suitable for applying via "patch"
423 * to our temporary file.
425 void wiki_rev_callback(char *name, char *filename, char *partnum, char *disp,
426 void *content, char *cbtype, char *cbcharset, size_t length,
427 char *encoding, char *cbid, void *cbuserdata)
429 struct HistoryEraserCallBackData *hecbd = (struct HistoryEraserCallBackData *)cbuserdata;
436 /* Did a previous callback already indicate that we've reached our target uuid?
437 * If so, don't process anything else.
443 CtdlDecodeBase64(memo, filename, strlen(filename));
444 extract_token(this_rev, memo, 0, '|', sizeof this_rev);
447 /* Perform the patch */
448 fp = popen(PATCH " -f -s -p0 -r /dev/null >/dev/null 2>/dev/null", "w");
450 /* Replace the filenames in the patch with the tempfilename we're actually tweaking */
451 fprintf(fp, "--- %s\n", hecbd->tempfilename);
452 fprintf(fp, "+++ %s\n", hecbd->tempfilename);
454 ptr = (char *)content;
458 ptr = memreadline(ptr, buf, sizeof buf);
461 /* skip the first two lines; they contain bogus filenames */
464 fprintf(fp, "%s\n", buf);
467 } while ((*ptr != 0) && (ptr < ((char*)content + length)));
468 if (pclose(fp) != 0) {
469 syslog(LOG_ERR, "pclose() returned an error - patch failed\n");
473 if (!strcasecmp(this_rev, hecbd->stop_when)) {
474 /* Found our target rev. Tell any subsequent callbacks to suppress processing. */
475 syslog(LOG_DEBUG, "Target revision has been reached -- stop patching.\n");
482 * Fetch a specific revision of a wiki page. The "operation" string may be set to "fetch" in order
483 * to simply fetch the desired revision and store it in a temporary location for viewing, or "revert"
484 * to revert the currently active page to that revision.
486 void wiki_rev(char *pagename, char *rev, char *operation)
488 struct CitContext *CCC = CC;
490 char history_page_name[270];
493 struct CtdlMessage *msg;
495 struct HistoryEraserCallBackData hecbd;
499 r = CtdlDoIHavePermissionToReadMessagesInThisRoom();
501 if (r == om_not_logged_in) {
502 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
505 cprintf("%d An unknown error has occurred.\n", ERROR);
510 if (!strcasecmp(operation, "revert")) {
511 r = CtdlDoIHavePermissionToPostInThisRoom(temp, sizeof temp, NULL, POST_LOGGED_IN, 0);
513 cprintf("%d %s\n", r, temp);
518 /* Begin by fetching the current version of the page. We're going to patch
519 * backwards through the diffs until we get the one we want.
521 msgnum = CtdlLocateMessageByEuid(pagename, &CCC->room);
523 msg = CtdlFetchMessage(msgnum, 1);
529 if ((msg != NULL) && CM_IsEmpty(msg, eMesageText)) {
535 cprintf("%d Page '%s' was not found.\n", ERROR+MESSAGE_NOT_FOUND, pagename);
539 /* Output it to a temporary file */
541 CtdlMakeTempFileName(temp, sizeof temp);
542 fp = fopen(temp, "w");
544 r = fwrite(msg->cm_fields[eMesageText], strlen(msg->cm_fields[eMesageText]), 1, fp);
548 syslog(LOG_ALERT, "Cannot open %s: %s\n", temp, strerror(errno));
552 /* Get the revision history */
554 snprintf(history_page_name, sizeof history_page_name, "%s_HISTORY_", pagename);
555 msgnum = CtdlLocateMessageByEuid(history_page_name, &CCC->room);
557 msg = CtdlFetchMessage(msgnum, 1);
563 if ((msg != NULL) && CM_IsEmpty(msg, eMesageText)) {
569 cprintf("%d Revision history for '%s' was not found.\n", ERROR+MESSAGE_NOT_FOUND, pagename);
573 /* Start patching backwards (newest to oldest) through the revision history until we
574 * hit the revision uuid requested by the user. (The callback will perform each one.)
577 memset(&hecbd, 0, sizeof(struct HistoryEraserCallBackData));
578 hecbd.tempfilename = temp;
579 hecbd.stop_when = rev;
580 striplt(hecbd.stop_when);
582 mime_parser(msg->cm_fields[eMesageText], NULL, *wiki_rev_callback, NULL, NULL, (void *)&hecbd, 0);
585 /* Were we successful? */
586 if (hecbd.done == 0) {
587 cprintf("%d Revision '%s' of page '%s' was not found.\n",
588 ERROR + MESSAGE_NOT_FOUND, rev, pagename
592 /* We have the desired revision on disk. Now do something with it. */
594 else if ( (!strcasecmp(operation, "fetch")) || (!strcasecmp(operation, "revert")) ) {
595 msg = malloc(sizeof(struct CtdlMessage));
596 memset(msg, 0, sizeof(struct CtdlMessage));
597 msg->cm_magic = CTDLMESSAGE_MAGIC;
598 msg->cm_anon_type = MES_NORMAL;
599 msg->cm_format_type = FMT_RFC822;
600 fp = fopen(temp, "r");
603 fseek(fp, 0L, SEEK_END);
605 fseek(fp, 0L, SEEK_SET);
606 msgbuf = malloc(len + 1);
607 rv = fread(msgbuf, len, 1, fp);
608 syslog(LOG_DEBUG, "did %d blocks of %ld bytes\n", rv, len);
610 CM_SetAsField(msg, eMesageText, &msgbuf, len);
616 else if (!strcasecmp(operation, "fetch")) {
617 CM_SetField(msg, eAuthor, HKEY("Citadel"));
618 CtdlCreateRoom(wwm, 5, "", 0, 1, 1, VIEW_BBS); /* Not an error if already exists */
619 msgnum = CtdlSubmitMsg(msg, NULL, wwm, 0); /* Store the revision here */
622 * WARNING: VILE SLEAZY HACK
623 * This will avoid the 'message xxx is not in this room' security error,
624 * but only if the client fetches the message we just generated immediately
625 * without first trying to perform other fetch operations.
627 if (CCC->cached_msglist != NULL) {
628 free(CCC->cached_msglist);
629 CCC->cached_msglist = NULL;
630 CCC->cached_num_msgs = 0;
632 CCC->cached_msglist = malloc(sizeof(long));
633 if (CCC->cached_msglist != NULL) {
634 CCC->cached_num_msgs = 1;
635 CCC->cached_msglist[0] = msgnum;
639 else if (!strcasecmp(operation, "revert")) {
640 CM_SetFieldLONG(msg, eTimestamp, time(NULL));
641 CM_SetField(msg, eAuthor, CCC->user.fullname, strlen(CCC->user.fullname));
642 CM_SetField(msg, erFc822Addr, CCC->cs_inet_email, strlen(CCC->cs_inet_email));
643 CM_SetField(msg, eOriginalRoom, CCC->room.QRname, strlen(CCC->room.QRname));
644 CM_SetField(msg, eNodeName, NODENAME, strlen(NODENAME));
645 CM_SetField(msg, eExclusiveID, pagename, strlen(pagename));
646 msgnum = CtdlSubmitMsg(msg, NULL, "", 0); /* Replace the current revision */
649 /* Theoretically it is impossible to get here, but throw an error anyway */
654 cprintf("%d %ld\n", CIT_OK, msgnum); /* Give the client a msgnum */
657 cprintf("%d Error %ld has occurred.\n", ERROR+INTERNAL_ERROR, msgnum);
661 /* We did all this work for nothing. Express anguish to the caller. */
663 cprintf("%d An unknown operation was requested.\n", ERROR+CMD_NOT_SUPPORTED);
673 * commands related to wiki management
675 void cmd_wiki(char *argbuf) {
681 extract_token(subcmd, argbuf, 0, '|', sizeof subcmd);
683 if (!strcasecmp(subcmd, "history")) {
684 extract_token(pagename, argbuf, 1, '|', sizeof pagename);
685 wiki_history(pagename);
689 if (!strcasecmp(subcmd, "rev")) {
690 extract_token(pagename, argbuf, 1, '|', sizeof pagename);
691 extract_token(rev, argbuf, 2, '|', sizeof rev);
692 extract_token(operation, argbuf, 3, '|', sizeof operation);
693 wiki_rev(pagename, rev, operation);
697 cprintf("%d Invalid subcommand\n", ERROR + CMD_NOT_SUPPORTED);
703 * Module initialization
705 CTDL_MODULE_INIT(wiki)
709 CtdlRegisterMessageHook(wiki_upload_beforesave, EVT_BEFORESAVE);
710 CtdlRegisterProtoHook(cmd_wiki, "WIKI", "Commands related to Wiki management");
713 /* return our module name for the log */