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"
60 #include "euidindex.h"
61 #include "ctdl_module.h"
64 * Before allowing a wiki page save to execute, we have to perform version control.
65 * This involves fetching the old version of the page if it exists.
67 int wiki_upload_beforesave(struct CtdlMessage *msg) {
68 struct CitContext *CCC = CC;
69 long old_msgnum = (-1L);
70 struct CtdlMessage *old_msg = NULL;
71 long history_msgnum = (-1L);
72 struct CtdlMessage *history_msg = NULL;
73 char diff_old_filename[PATH_MAX];
74 char diff_new_filename[PATH_MAX];
75 char diff_cmd[PATH_MAX];
78 char history_page[1024];
84 size_t diffbuf_len = 0;
87 if (!CCC->logged_in) return(0); /* Only do this if logged in. */
89 /* Is this a room with a Wiki in it, don't run this hook. */
90 if (CCC->room.QRdefaultview != VIEW_WIKI) {
94 /* If this isn't a MIME message, don't bother. */
95 if (msg->cm_format_type != 4) return(0);
97 /* If there's no EUID we can't do this. */
98 if (msg->cm_fields['E'] == NULL) return(0);
99 snprintf(history_page, sizeof history_page, "%s_HISTORY_", msg->cm_fields['E']);
101 /* Make sure we're saving a real wiki page rather than a wiki history page.
102 * This is important in order to avoid recursing infinitely into this hook.
104 if ( (strlen(msg->cm_fields['E']) >= 9)
105 && (!strcasecmp(&msg->cm_fields['E'][strlen(msg->cm_fields['E'])-9], "_HISTORY_"))
107 CtdlLogPrintf(CTDL_DEBUG, "History page not being historied\n");
111 /* If there's no message text, obviously this is all b0rken and shouldn't happen at all */
112 if (msg->cm_fields['M'] == NULL) return(0);
114 /* See if we can retrieve the previous version. */
115 old_msgnum = locate_message_by_euid(msg->cm_fields['E'], &CCC->room);
116 if (old_msgnum > 0L) {
117 old_msg = CtdlFetchMessage(old_msgnum, 1);
123 if ((old_msg != NULL) && (old_msg->cm_fields['M'] == NULL)) { /* old version is corrupt? */
124 CtdlFreeMessage(old_msg);
128 /* If no changes were made, don't bother saving it again */
129 if ((old_msg != NULL) && (!strcmp(msg->cm_fields['M'], old_msg->cm_fields['M']))) {
130 CtdlFreeMessage(old_msg);
137 CtdlMakeTempFileName(diff_old_filename, sizeof diff_old_filename);
138 CtdlMakeTempFileName(diff_new_filename, sizeof diff_new_filename);
140 if (old_msg != NULL) {
141 fp = fopen(diff_old_filename, "w");
142 rv = fwrite(old_msg->cm_fields['M'], strlen(old_msg->cm_fields['M']), 1, fp);
144 CtdlFreeMessage(old_msg);
147 fp = fopen(diff_new_filename, "w");
148 rv = fwrite(msg->cm_fields['M'], strlen(msg->cm_fields['M']), 1, fp);
153 snprintf(diff_cmd, sizeof diff_cmd,
155 ((old_msg != NULL) ? diff_old_filename : "/dev/null"),
158 fp = popen(diff_cmd, "r");
161 diffbuf = realloc(diffbuf, diffbuf_len + 1025);
162 nbytes = fread(&diffbuf[diffbuf_len], 1, 1024, fp);
163 diffbuf_len += nbytes;
164 } while (nbytes == 1024);
165 diffbuf[diffbuf_len] = 0;
168 CtdlLogPrintf(CTDL_DEBUG, "diff length is %d bytes\n", diffbuf_len);
170 unlink(diff_old_filename);
171 unlink(diff_new_filename);
173 /* Determine whether this was a bogus (empty) edit */
174 if ((diffbuf_len = 0) && (diffbuf != NULL)) {
178 if (diffbuf == NULL) {
179 return(1); /* No changes at all? Abandon the post entirely! */
182 /* Now look for the existing edit history */
184 history_msgnum = locate_message_by_euid(history_page, &CCC->room);
186 if (history_msgnum > 0L) {
187 history_msg = CtdlFetchMessage(history_msgnum, 1);
190 /* Create a new history message if necessary */
191 if (history_msg == NULL) {
192 history_msg = malloc(sizeof(struct CtdlMessage));
193 memset(history_msg, 0, sizeof(struct CtdlMessage));
194 history_msg->cm_magic = CTDLMESSAGE_MAGIC;
195 history_msg->cm_anon_type = MES_NORMAL;
196 history_msg->cm_format_type = FMT_RFC822;
197 history_msg->cm_fields['A'] = strdup("Citadel");
198 history_msg->cm_fields['R'] = strdup(CCC->room.QRname);
199 history_msg->cm_fields['E'] = strdup(history_page);
200 history_msg->cm_fields['U'] = strdup(history_page);
201 snprintf(boundary, sizeof boundary, "Citadel--Multipart--%04x--%08lx", getpid(), time(NULL));
202 history_msg->cm_fields['M'] = malloc(1024);
203 snprintf(history_msg->cm_fields['M'], 1024,
204 "Content-type: multipart/mixed; boundary=\"%s\"\n\n"
205 "This is a Citadel wiki history encoded as multipart MIME.\n"
206 "Each part is comprised of a diff script representing one change set.\n"
214 /* Update the history message (regardless of whether it's new or existing) */
216 /* First, figure out the boundary string. We do this even when we generated the
217 * boundary string in the above code, just to be safe and consistent.
219 strcpy(boundary, "");
221 ptr = history_msg->cm_fields['M'];
223 ptr = memreadline(ptr, buf, sizeof buf);
226 if (!IsEmptyStr(buf) && (!strncasecmp(buf, "Content-type:", 13))) {
228 (bmstrcasestr(buf, "multipart") != NULL)
229 && (bmstrcasestr(buf, "boundary=") != NULL)
231 safestrncpy(boundary, bmstrcasestr(buf, "\""), sizeof boundary);
233 qu = strchr(boundary, '\"');
235 strcpy(boundary, ++qu);
237 qu = strchr(boundary, '\"');
244 } while ( (IsEmptyStr(boundary)) && (*ptr != 0) );
246 if (!IsEmptyStr(boundary)) {
247 snprintf(endary, sizeof endary, "--%s--", boundary);
248 history_msg->cm_fields['M'] = realloc(history_msg->cm_fields['M'],
249 strlen(history_msg->cm_fields['M']) + strlen(diffbuf) + 512
251 ptr = bmstrcasestr(history_msg->cm_fields['M'], endary);
253 sprintf(ptr, "--%s\n"
254 "Content-type: text/plain\n"
268 history_msg->cm_fields['T'] = realloc(history_msg->cm_fields['T'], 32);
269 snprintf(history_msg->cm_fields['T'], 32, "%ld", time(NULL));
271 CtdlSubmitMsg(history_msg, NULL, "", 0);
274 CtdlLogPrintf(CTDL_ALERT, "Empty boundary string in history message. No history!\n");
284 * Module initialization
286 CTDL_MODULE_INIT(wiki)
290 CtdlRegisterMessageHook(wiki_upload_beforesave, EVT_BEFORESAVE);
293 /* return our Subversion id for the Log */