4 * Server-side module for Wiki rooms. This will handle things like version control.
6 * Copyright (c) 2009 by the citadel.org team
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 #include <sys/types.h>
34 #if TIME_WITH_SYS_TIME
35 # include <sys/time.h>
39 # include <sys/time.h>
48 #include <libcitadel.h>
51 #include "citserver.h"
59 #include "euidindex.h"
60 #include "ctdl_module.h"
63 * Before allowing a wiki page save to execute, we have to perform version control.
64 * This involves fetching the old version of the page if it exists.
66 int wiki_upload_beforesave(struct CtdlMessage *msg) {
67 struct CitContext *CCC = CC;
68 long old_msgnum = (-1L);
69 struct CtdlMessage *old_msg = NULL;
70 long history_msgnum = (-1L);
71 struct CtdlMessage *history_msg = NULL;
72 char diff_old_filename[PATH_MAX];
73 char diff_new_filename[PATH_MAX];
74 char diff_cmd[PATH_MAX];
77 char history_page[1024];
79 char prefixed_boundary[258];
83 size_t diffbuf_len = 0;
86 if (!CCC->logged_in) return(0); /* Only do this if logged in. */
88 /* Is this a room with a Wiki in it, don't run this hook. */
89 if (CCC->room.QRdefaultview != VIEW_WIKI) {
93 /* If this isn't a MIME message, don't bother. */
94 if (msg->cm_format_type != 4) return(0);
96 /* If there's no EUID we can't do this. */
97 if (msg->cm_fields['E'] == NULL) return(0);
98 snprintf(history_page, sizeof history_page, "%s_HISTORY_", msg->cm_fields['E']);
100 /* Make sure we're saving a real wiki page rather than a wiki history page.
101 * This is important in order to avoid recursing infinitely into this hook.
103 if ( (strlen(msg->cm_fields['E']) >= 9)
104 && (!strcasecmp(&msg->cm_fields['E'][strlen(msg->cm_fields['E'])-9], "_HISTORY_"))
106 CtdlLogPrintf(CTDL_DEBUG, "History page not being historied\n");
110 /* If there's no message text, obviously this is all b0rken and shouldn't happen at all */
111 if (msg->cm_fields['M'] == NULL) return(0);
113 /* See if we can retrieve the previous version. */
114 old_msgnum = locate_message_by_euid(msg->cm_fields['E'], &CCC->room);
115 if (old_msgnum > 0L) {
116 old_msg = CtdlFetchMessage(old_msgnum, 1);
122 if ((old_msg != NULL) && (old_msg->cm_fields['M'] == NULL)) { /* old version is corrupt? */
123 CtdlFreeMessage(old_msg);
127 /* If no changes were made, don't bother saving it again */
128 if ((old_msg != NULL) && (!strcmp(msg->cm_fields['M'], old_msg->cm_fields['M']))) {
129 CtdlFreeMessage(old_msg);
136 CtdlMakeTempFileName(diff_old_filename, sizeof diff_old_filename);
137 CtdlMakeTempFileName(diff_new_filename, sizeof diff_new_filename);
139 if (old_msg != NULL) {
140 fp = fopen(diff_old_filename, "w");
141 rv = fwrite(old_msg->cm_fields['M'], strlen(old_msg->cm_fields['M']), 1, fp);
143 CtdlFreeMessage(old_msg);
146 fp = fopen(diff_new_filename, "w");
147 rv = fwrite(msg->cm_fields['M'], strlen(msg->cm_fields['M']), 1, fp);
152 snprintf(diff_cmd, sizeof diff_cmd,
155 ((old_msg != NULL) ? diff_old_filename : "/dev/null")
157 fp = popen(diff_cmd, "r");
160 diffbuf = realloc(diffbuf, diffbuf_len + 1025);
161 nbytes = fread(&diffbuf[diffbuf_len], 1, 1024, fp);
162 diffbuf_len += nbytes;
163 } while (nbytes == 1024);
164 diffbuf[diffbuf_len] = 0;
167 CtdlLogPrintf(CTDL_DEBUG, "diff length is %d bytes\n", diffbuf_len);
169 unlink(diff_old_filename);
170 unlink(diff_new_filename);
172 /* Determine whether this was a bogus (empty) edit */
173 if ((diffbuf_len = 0) && (diffbuf != NULL)) {
177 if (diffbuf == NULL) {
178 return(1); /* No changes at all? Abandon the post entirely! */
181 /* Now look for the existing edit history */
183 history_msgnum = locate_message_by_euid(history_page, &CCC->room);
185 if (history_msgnum > 0L) {
186 history_msg = CtdlFetchMessage(history_msgnum, 1);
189 /* Create a new history message if necessary */
190 if (history_msg == NULL) {
191 history_msg = malloc(sizeof(struct CtdlMessage));
192 memset(history_msg, 0, sizeof(struct CtdlMessage));
193 history_msg->cm_magic = CTDLMESSAGE_MAGIC;
194 history_msg->cm_anon_type = MES_NORMAL;
195 history_msg->cm_format_type = FMT_RFC822;
196 history_msg->cm_fields['A'] = strdup("Citadel");
197 history_msg->cm_fields['R'] = strdup(CCC->room.QRname);
198 history_msg->cm_fields['E'] = strdup(history_page);
199 history_msg->cm_fields['U'] = strdup(history_page);
200 snprintf(boundary, sizeof boundary, "Citadel--Multipart--%04x--%08lx", getpid(), time(NULL));
201 history_msg->cm_fields['M'] = malloc(1024);
202 snprintf(history_msg->cm_fields['M'], 1024,
203 "Content-type: multipart/mixed; boundary=\"%s\"\n\n"
204 "This is a Citadel wiki history encoded as multipart MIME.\n"
205 "Each part is comprised of a diff script representing one change set.\n"
213 /* Update the history message (regardless of whether it's new or existing) */
215 /* First, figure out the boundary string. We do this even when we generated the
216 * boundary string in the above code, just to be safe and consistent.
218 strcpy(boundary, "");
220 ptr = history_msg->cm_fields['M'];
222 ptr = memreadline(ptr, buf, sizeof buf);
225 if (!IsEmptyStr(buf) && (!strncasecmp(buf, "Content-type:", 13))) {
227 (bmstrcasestr(buf, "multipart") != NULL)
228 && (bmstrcasestr(buf, "boundary=") != NULL)
230 safestrncpy(boundary, bmstrcasestr(buf, "\""), sizeof boundary);
232 qu = strchr(boundary, '\"');
234 strcpy(boundary, ++qu);
236 qu = strchr(boundary, '\"');
243 } while ( (IsEmptyStr(boundary)) && (*ptr != 0) );
245 /* Now look for the first boundary. That is where we need to insert our fun.
247 if (!IsEmptyStr(boundary)) {
248 snprintf(prefixed_boundary, sizeof prefixed_boundary, "--%s", boundary);
249 history_msg->cm_fields['M'] = realloc(history_msg->cm_fields['M'],
250 strlen(history_msg->cm_fields['M']) + strlen(diffbuf) + 1024
252 ptr = bmstrcasestr(history_msg->cm_fields['M'], prefixed_boundary);
254 char *the_rest_of_it = strdup(ptr);
257 char encoded_memo[768];
259 snprintf(memo, sizeof memo, "%s|%ld|%s|%s",
264 /* no longer logging CCC->cs_inet_email */
266 CtdlEncodeBase64(encoded_memo, memo, strlen(memo), 0);
267 sprintf(ptr, "--%s\n"
268 "Content-type: text/plain\n"
269 "Content-Disposition: inline; filename=\"%s\"\n"
270 "Content-Transfer-Encoding: 8bit\n"
280 free(the_rest_of_it);
283 history_msg->cm_fields['T'] = realloc(history_msg->cm_fields['T'], 32);
284 snprintf(history_msg->cm_fields['T'], 32, "%ld", time(NULL));
286 CtdlSubmitMsg(history_msg, NULL, "", 0);
289 CtdlLogPrintf(CTDL_ALERT, "Empty boundary string in history message. No history!\n");
299 * MIME Parser callback for wiki_history()
301 * The "filename" field will contain a memo field. All we have to do is decode
302 * the base64 and output it. The data is already in a delimited format suitable
303 * for our client protocol.
305 void wiki_history_callback(char *name, char *filename, char *partnum, char *disp,
306 void *content, char *cbtype, char *cbcharset, size_t length,
307 char *encoding, char *cbid, void *cbuserdata)
311 CtdlDecodeBase64(memo, filename, strlen(filename));
312 cprintf("%s\n", memo);
317 * Fetch a list of revisions for a particular wiki page
319 void wiki_history(char *pagename) {
321 char history_page_name[270];
323 struct CtdlMessage *msg;
325 r = CtdlDoIHavePermissionToReadMessagesInThisRoom();
327 if (r == om_not_logged_in) {
328 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
331 cprintf("%d An unknown error has occurred.\n", ERROR);
336 snprintf(history_page_name, sizeof history_page_name, "%s_HISTORY_", pagename);
337 msgnum = locate_message_by_euid(history_page_name, &CC->room);
339 msg = CtdlFetchMessage(msgnum, 1);
345 if ((msg != NULL) && (msg->cm_fields['M'] == NULL)) {
346 CtdlFreeMessage(msg);
351 cprintf("%d Revision history for '%s' was not found.\n", ERROR+MESSAGE_NOT_FOUND, pagename);
356 cprintf("%d Revision history for '%s'\n", LISTING_FOLLOWS, pagename);
357 mime_parser(msg->cm_fields['M'], NULL, *wiki_history_callback, NULL, NULL, NULL, 0);
360 CtdlFreeMessage(msg);
366 * commands related to wiki management
368 void cmd_wiki(char *argbuf) {
372 extract_token(subcmd, argbuf, 0, '|', sizeof subcmd);
374 if (!strcasecmp(subcmd, "history")) {
375 extract_token(pagename, argbuf, 1, '|', sizeof subcmd);
376 wiki_history(pagename);
380 cprintf("%d Invalid subcommand\n", ERROR + CMD_NOT_SUPPORTED);
386 * Module initialization
388 CTDL_MODULE_INIT(wiki)
392 CtdlRegisterMessageHook(wiki_upload_beforesave, EVT_BEFORESAVE);
393 CtdlRegisterProtoHook(cmd_wiki, "WIKI", "Commands related to Wiki management");
396 /* return our Subversion id for the Log */