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"
51 #include "euidindex.h"
52 #include "ctdl_module.h"
55 * Data passed back and forth between wiki_rev() and its MIME parser callback
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 */
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.
67 char *wwm = "9999999999.WikiWaybackMachine";
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.
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];
85 char history_page[1024];
86 long history_page_len;
88 char prefixed_boundary[258];
91 size_t diffbuf_len = 0;
94 if (!CCC->logged_in) return(0); /* Only do this if logged in. */
96 /* Is this a room with a Wiki in it, don't run this hook. */
97 if ((CCC->room.QRdefaultview != VIEW_WIKI) &&
98 (CCC->room.QRdefaultview != VIEW_WIKIMD)) {
102 /* If this isn't a MIME message, don't bother. */
103 if (msg->cm_format_type != 4) return(0);
105 /* If there's no EUID we can't do this. Reject the post. */
106 if (CM_IsEmpty(msg, eExclusiveID)) return(1);
108 history_page_len = snprintf(history_page, sizeof history_page,
109 "%s_HISTORY_", msg->cm_fields[eExclusiveID]);
111 /* Make sure we're saving a real wiki page rather than a wiki history page.
112 * This is important in order to avoid recursing infinitely into this hook.
114 if ( (msg->cm_lengths[eExclusiveID] >= 9)
115 && (!strcasecmp(&msg->cm_fields[eExclusiveID][msg->cm_lengths[eExclusiveID]-9], "_HISTORY_"))
117 syslog(LOG_DEBUG, "History page not being historied\n");
121 /* If there's no message text, obviously this is all b0rken and shouldn't happen at all */
122 if (CM_IsEmpty(msg, eMesageText)) return(0);
124 /* Set the message subject identical to the page name */
125 CM_CopyField(msg, eMsgSubject, eExclusiveID);
127 /* See if we can retrieve the previous version. */
128 old_msgnum = CtdlLocateMessageByEuid(msg->cm_fields[eExclusiveID], &CCC->room);
129 if (old_msgnum > 0L) {
130 old_msg = CtdlFetchMessage(old_msgnum, 1);
136 if ((old_msg != NULL) && (CM_IsEmpty(old_msg, eMesageText))) { /* old version is corrupt? */
141 /* If no changes were made, don't bother saving it again */
142 if ((old_msg != NULL) && (!strcmp(msg->cm_fields[eMesageText], old_msg->cm_fields[eMesageText]))) {
150 CtdlMakeTempFileName(diff_old_filename, sizeof diff_old_filename);
151 CtdlMakeTempFileName(diff_new_filename, sizeof diff_new_filename);
152 CtdlMakeTempFileName(diff_out_filename, sizeof diff_out_filename);
154 if (old_msg != NULL) {
155 fp = fopen(diff_old_filename, "w");
156 rv = fwrite(old_msg->cm_fields[eMesageText], old_msg->cm_lengths[eMesageText], 1, fp);
161 fp = fopen(diff_new_filename, "w");
162 rv = fwrite(msg->cm_fields[eMesageText], msg->cm_lengths[eMesageText], 1, fp);
165 snprintf(diff_cmd, sizeof diff_cmd,
166 DIFF " -u %s %s >%s",
168 ((old_msg != NULL) ? diff_old_filename : "/dev/null"),
171 syslog(LOG_DEBUG, "diff cmd: %s", diff_cmd);
172 rv = system(diff_cmd);
173 syslog(LOG_DEBUG, "diff cmd returned %d", rv);
177 fp = fopen(diff_out_filename, "r");
179 fp = fopen("/dev/null", "r");
182 fseek(fp, 0L, SEEK_END);
183 diffbuf_len = ftell(fp);
184 fseek(fp, 0L, SEEK_SET);
185 diffbuf = malloc(diffbuf_len + 1);
186 fread(diffbuf, diffbuf_len, 1, fp);
187 diffbuf[diffbuf_len] = '\0';
191 syslog(LOG_DEBUG, "diff length is "SIZE_T_FMT" bytes", diffbuf_len);
193 unlink(diff_old_filename);
194 unlink(diff_new_filename);
195 unlink(diff_out_filename);
197 /* Determine whether this was a bogus (empty) edit */
198 if ((diffbuf_len = 0) && (diffbuf != NULL)) {
202 if (diffbuf == NULL) {
203 return(1); /* No changes at all? Abandon the post entirely! */
206 /* Now look for the existing edit history */
208 history_msgnum = CtdlLocateMessageByEuid(history_page, &CCC->room);
210 if (history_msgnum > 0L) {
211 history_msg = CtdlFetchMessage(history_msgnum, 1);
214 /* Create a new history message if necessary */
215 if (history_msg == NULL) {
219 history_msg = malloc(sizeof(struct CtdlMessage));
220 memset(history_msg, 0, sizeof(struct CtdlMessage));
221 history_msg->cm_magic = CTDLMESSAGE_MAGIC;
222 history_msg->cm_anon_type = MES_NORMAL;
223 history_msg->cm_format_type = FMT_RFC822;
224 CM_SetField(history_msg, eAuthor, HKEY("Citadel"));
225 CM_SetField(history_msg, eRecipient, CCC->room.QRname, strlen(CCC->room.QRname));
226 CM_SetField(history_msg, eExclusiveID, history_page, history_page_len);
227 CM_SetField(history_msg, eMsgSubject, history_page, history_page_len);
228 CM_SetField(history_msg, eSuppressIdx, HKEY("1")); /* suppress full text indexing */
229 snprintf(boundary, sizeof boundary, "Citadel--Multipart--%04x--%08lx", getpid(), time(NULL));
230 buf = (char*) malloc(1024);
231 len = snprintf(buf, 1024,
232 "Content-type: multipart/mixed; boundary=\"%s\"\n\n"
233 "This is a Citadel wiki history encoded as multipart MIME.\n"
234 "Each part is comprised of a diff script representing one change set.\n"
239 CM_SetAsField(history_msg, eMesageText, &buf, len);
242 /* Update the history message (regardless of whether it's new or existing) */
244 /* Remove the Message-ID from the old version of the history message. This will cause a brand
245 * new one to be generated, avoiding an uninitentional hit of the loop zapper when we replicate.
247 CM_FlushField(history_msg, emessageId);
249 /* Figure out the boundary string. We do this even when we generated the
250 * boundary string in the above code, just to be safe and consistent.
254 ptr = history_msg->cm_fields[eMesageText];
256 ptr = memreadline(ptr, buf, sizeof buf);
259 if (!IsEmptyStr(buf) && (!strncasecmp(buf, "Content-type:", 13))) {
261 (bmstrcasestr(buf, "multipart") != NULL)
262 && (bmstrcasestr(buf, "boundary=") != NULL)
264 safestrncpy(boundary, bmstrcasestr(buf, "\""), sizeof boundary);
266 qu = strchr(boundary, '\"');
268 strcpy(boundary, ++qu);
270 qu = strchr(boundary, '\"');
277 } while ( (IsEmptyStr(boundary)) && (*ptr != 0) );
280 * Now look for the first boundary. That is where we need to insert our fun.
282 if (!IsEmptyStr(boundary)) {
285 time_t Now = time(NULL);
287 snprintf(prefixed_boundary, sizeof(prefixed_boundary), "--%s", boundary);
289 CM_GetAsField(history_msg, eMesageText, &MsgText, &MsgTextLen);
291 ptr = bmstrcasestr(MsgText, prefixed_boundary);
297 char encoded_memo[1024];
299 NewMsgText = NewStrBufPlain(NULL, MsgTextLen + diffbuf_len + 1024);
302 memolen = snprintf(memo, sizeof(memo), "%s|%ld|%s|%s",
308 memolen = CtdlEncodeBase64(encoded_memo, memo, memolen, 0);
310 StrBufAppendBufPlain(NewMsgText, HKEY("--"), 0);
311 StrBufAppendBufPlain(NewMsgText, boundary, -1, 0);
312 StrBufAppendBufPlain(
315 "Content-type: text/plain\n"
316 "Content-Disposition: inline; filename=\""), 0);
318 StrBufAppendBufPlain(NewMsgText, encoded_memo, memolen, 0);
320 StrBufAppendBufPlain(
323 "Content-Transfer-Encoding: 8bit\n"
326 StrBufAppendBufPlain(NewMsgText, diffbuf, diffbuf_len, 0);
327 StrBufAppendBufPlain(NewMsgText, HKEY("\n"), 0);
329 StrBufAppendBufPlain(NewMsgText, ptr, MsgTextLen - (ptr - MsgText), 0);
331 CM_SetAsFieldSB(history_msg, eMesageText, &NewMsgText);
335 CM_SetAsField(history_msg, eMesageText, &MsgText, MsgTextLen);
338 CM_SetFieldLONG(history_msg, eTimestamp, Now);
340 CtdlSubmitMsg(history_msg, NULL, "", 0);
343 syslog(LOG_ALERT, "Empty boundary string in history message. No history!\n");
347 CM_Free(history_msg);
353 * MIME Parser callback for wiki_history()
355 * The "filename" field will contain a memo field. All we have to do is decode
356 * the base64 and output it. The data is already in a delimited format suitable
357 * for our client protocol.
359 void wiki_history_callback(char *name, char *filename, char *partnum, char *disp,
360 void *content, char *cbtype, char *cbcharset, size_t length,
361 char *encoding, char *cbid, void *cbuserdata)
365 CtdlDecodeBase64(memo, filename, strlen(filename));
366 cprintf("%s\n", memo);
371 * Fetch a list of revisions for a particular wiki page
373 void wiki_history(char *pagename) {
375 char history_page_name[270];
377 struct CtdlMessage *msg;
379 r = CtdlDoIHavePermissionToReadMessagesInThisRoom();
381 if (r == om_not_logged_in) {
382 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
385 cprintf("%d An unknown error has occurred.\n", ERROR);
390 snprintf(history_page_name, sizeof history_page_name, "%s_HISTORY_", pagename);
391 msgnum = CtdlLocateMessageByEuid(history_page_name, &CC->room);
393 msg = CtdlFetchMessage(msgnum, 1);
399 if ((msg != NULL) && CM_IsEmpty(msg, eMesageText)) {
405 cprintf("%d Revision history for '%s' was not found.\n", ERROR+MESSAGE_NOT_FOUND, pagename);
410 cprintf("%d Revision history for '%s'\n", LISTING_FOLLOWS, pagename);
411 mime_parser(CM_RANGE(msg, eMesageText), *wiki_history_callback, NULL, NULL, NULL, 0);
419 * MIME Parser callback for wiki_rev()
421 * The "filename" field will contain a memo field, which includes (among other things)
422 * the uuid of this revision. After we hit the desired revision, we stop processing.
424 * The "content" filed will contain "diff" output suitable for applying via "patch"
425 * to our temporary file.
427 void wiki_rev_callback(char *name, char *filename, char *partnum, char *disp,
428 void *content, char *cbtype, char *cbcharset, size_t length,
429 char *encoding, char *cbid, void *cbuserdata)
431 struct HistoryEraserCallBackData *hecbd = (struct HistoryEraserCallBackData *)cbuserdata;
438 /* Did a previous callback already indicate that we've reached our target uuid?
439 * If so, don't process anything else.
445 CtdlDecodeBase64(memo, filename, strlen(filename));
446 extract_token(this_rev, memo, 0, '|', sizeof this_rev);
449 /* Perform the patch */
450 fp = popen(PATCH " -f -s -p0 -r /dev/null >/dev/null 2>/dev/null", "w");
452 /* Replace the filenames in the patch with the tempfilename we're actually tweaking */
453 fprintf(fp, "--- %s\n", hecbd->tempfilename);
454 fprintf(fp, "+++ %s\n", hecbd->tempfilename);
456 ptr = (char *)content;
460 ptr = memreadline(ptr, buf, sizeof buf);
463 /* skip the first two lines; they contain bogus filenames */
466 fprintf(fp, "%s\n", buf);
469 } while ((*ptr != 0) && (ptr < ((char*)content + length)));
470 if (pclose(fp) != 0) {
471 syslog(LOG_ERR, "pclose() returned an error - patch failed\n");
475 if (!strcasecmp(this_rev, hecbd->stop_when)) {
476 /* Found our target rev. Tell any subsequent callbacks to suppress processing. */
477 syslog(LOG_DEBUG, "Target revision has been reached -- stop patching.\n");
484 * Fetch a specific revision of a wiki page. The "operation" string may be set to "fetch" in order
485 * to simply fetch the desired revision and store it in a temporary location for viewing, or "revert"
486 * to revert the currently active page to that revision.
488 void wiki_rev(char *pagename, char *rev, char *operation)
490 struct CitContext *CCC = CC;
492 char history_page_name[270];
495 struct CtdlMessage *msg;
497 struct HistoryEraserCallBackData hecbd;
501 r = CtdlDoIHavePermissionToReadMessagesInThisRoom();
503 if (r == om_not_logged_in) {
504 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
507 cprintf("%d An unknown error has occurred.\n", ERROR);
512 if (!strcasecmp(operation, "revert")) {
513 r = CtdlDoIHavePermissionToPostInThisRoom(temp, sizeof temp, NULL, POST_LOGGED_IN, 0);
515 cprintf("%d %s\n", r, temp);
520 /* Begin by fetching the current version of the page. We're going to patch
521 * backwards through the diffs until we get the one we want.
523 msgnum = CtdlLocateMessageByEuid(pagename, &CCC->room);
525 msg = CtdlFetchMessage(msgnum, 1);
531 if ((msg != NULL) && CM_IsEmpty(msg, eMesageText)) {
537 cprintf("%d Page '%s' was not found.\n", ERROR+MESSAGE_NOT_FOUND, pagename);
541 /* Output it to a temporary file */
543 CtdlMakeTempFileName(temp, sizeof temp);
544 fp = fopen(temp, "w");
546 r = fwrite(msg->cm_fields[eMesageText], msg->cm_lengths[eMesageText], 1, fp);
550 syslog(LOG_ALERT, "Cannot open %s: %s\n", temp, strerror(errno));
554 /* Get the revision history */
556 snprintf(history_page_name, sizeof history_page_name, "%s_HISTORY_", pagename);
557 msgnum = CtdlLocateMessageByEuid(history_page_name, &CCC->room);
559 msg = CtdlFetchMessage(msgnum, 1);
565 if ((msg != NULL) && CM_IsEmpty(msg, eMesageText)) {
571 cprintf("%d Revision history for '%s' was not found.\n", ERROR+MESSAGE_NOT_FOUND, pagename);
575 /* Start patching backwards (newest to oldest) through the revision history until we
576 * hit the revision uuid requested by the user. (The callback will perform each one.)
579 memset(&hecbd, 0, sizeof(struct HistoryEraserCallBackData));
580 hecbd.tempfilename = temp;
581 hecbd.stop_when = rev;
582 striplt(hecbd.stop_when);
584 mime_parser(CM_RANGE(msg, eMesageText), *wiki_rev_callback, NULL, NULL, (void *)&hecbd, 0);
587 /* Were we successful? */
588 if (hecbd.done == 0) {
589 cprintf("%d Revision '%s' of page '%s' was not found.\n",
590 ERROR + MESSAGE_NOT_FOUND, rev, pagename
594 /* We have the desired revision on disk. Now do something with it. */
596 else if ( (!strcasecmp(operation, "fetch")) || (!strcasecmp(operation, "revert")) ) {
597 msg = malloc(sizeof(struct CtdlMessage));
598 memset(msg, 0, sizeof(struct CtdlMessage));
599 msg->cm_magic = CTDLMESSAGE_MAGIC;
600 msg->cm_anon_type = MES_NORMAL;
601 msg->cm_format_type = FMT_RFC822;
602 fp = fopen(temp, "r");
605 fseek(fp, 0L, SEEK_END);
607 fseek(fp, 0L, SEEK_SET);
608 msgbuf = malloc(len + 1);
609 rv = fread(msgbuf, len, 1, fp);
610 syslog(LOG_DEBUG, "did %d blocks of %ld bytes\n", rv, len);
612 CM_SetAsField(msg, eMesageText, &msgbuf, len);
618 else if (!strcasecmp(operation, "fetch")) {
619 CM_SetField(msg, eAuthor, HKEY("Citadel"));
620 CtdlCreateRoom(wwm, 5, "", 0, 1, 1, VIEW_BBS); /* Not an error if already exists */
621 msgnum = CtdlSubmitMsg(msg, NULL, wwm, 0); /* Store the revision here */
624 * WARNING: VILE SLEAZY HACK
625 * This will avoid the 'message xxx is not in this room' security error,
626 * but only if the client fetches the message we just generated immediately
627 * without first trying to perform other fetch operations.
629 if (CCC->cached_msglist != NULL) {
630 free(CCC->cached_msglist);
631 CCC->cached_msglist = NULL;
632 CCC->cached_num_msgs = 0;
634 CCC->cached_msglist = malloc(sizeof(long));
635 if (CCC->cached_msglist != NULL) {
636 CCC->cached_num_msgs = 1;
637 CCC->cached_msglist[0] = msgnum;
641 else if (!strcasecmp(operation, "revert")) {
642 CM_SetFieldLONG(msg, eTimestamp, time(NULL));
643 CM_SetField(msg, eAuthor, CCC->user.fullname, strlen(CCC->user.fullname));
644 CM_SetField(msg, erFc822Addr, CCC->cs_inet_email, strlen(CCC->cs_inet_email));
645 CM_SetField(msg, eOriginalRoom, CCC->room.QRname, strlen(CCC->room.QRname));
646 CM_SetField(msg, eNodeName, CFG_KEY(c_nodename));
647 CM_SetField(msg, eExclusiveID, pagename, strlen(pagename));
648 msgnum = CtdlSubmitMsg(msg, NULL, "", 0); /* Replace the current revision */
651 /* Theoretically it is impossible to get here, but throw an error anyway */
656 cprintf("%d %ld\n", CIT_OK, msgnum); /* Give the client a msgnum */
659 cprintf("%d Error %ld has occurred.\n", ERROR+INTERNAL_ERROR, msgnum);
663 /* We did all this work for nothing. Express anguish to the caller. */
665 cprintf("%d An unknown operation was requested.\n", ERROR+CMD_NOT_SUPPORTED);
675 * commands related to wiki management
677 void cmd_wiki(char *argbuf) {
683 extract_token(subcmd, argbuf, 0, '|', sizeof subcmd);
685 if (!strcasecmp(subcmd, "history")) {
686 extract_token(pagename, argbuf, 1, '|', sizeof pagename);
687 wiki_history(pagename);
691 if (!strcasecmp(subcmd, "rev")) {
692 extract_token(pagename, argbuf, 1, '|', sizeof pagename);
693 extract_token(rev, argbuf, 2, '|', sizeof rev);
694 extract_token(operation, argbuf, 3, '|', sizeof operation);
695 wiki_rev(pagename, rev, operation);
699 cprintf("%d Invalid subcommand\n", ERROR + CMD_NOT_SUPPORTED);
705 * Module initialization
707 CTDL_MODULE_INIT(wiki)
711 CtdlRegisterMessageHook(wiki_upload_beforesave, EVT_BEFORESAVE);
712 CtdlRegisterProtoHook(cmd_wiki, "WIKI", "Commands related to Wiki management");
715 /* return our module name for the log */