4 * Server-side module for Wiki rooms. This will handle things like version control.
6 * Copyright (c) 2009 / released under the GNU General Public License v3
18 #include <sys/types.h>
20 #if TIME_WITH_SYS_TIME
21 # include <sys/time.h>
25 # include <sys/time.h>
34 #include <libcitadel.h>
37 #include "citserver.h"
46 #include "euidindex.h"
47 #include "ctdl_module.h"
50 * Before allowing a wiki page save to execute, we have to perform version control.
51 * This involves fetching the old version of the page if it exists... FIXME finish this
53 int wiki_upload_beforesave(struct CtdlMessage *msg) {
54 struct CitContext *CCC = CC;
55 long old_msgnum = (-1L);
56 struct CtdlMessage *old_msg = NULL;
57 long history_msgnum = (-1L);
58 struct CtdlMessage *history_msg = NULL;
59 char diff_old_filename[PATH_MAX];
60 char diff_new_filename[PATH_MAX];
61 char diff_cmd[PATH_MAX];
64 char history_page[1024];
68 size_t diffbuf_len = 0;
70 if (!CCC->logged_in) return(0); /* Only do this if logged in. */
72 /* Is this a room with a Wiki in it, don't run this hook. */
73 if (CCC->room.QRdefaultview != VIEW_WIKI) {
77 /* If this isn't a MIME message, don't bother. */
78 if (msg->cm_format_type != 4) return(0);
80 /* If there's no EUID we can't do this. */
81 if (msg->cm_fields['E'] == NULL) return(0);
83 /* If there's no message text, obviously this is all b0rken and shouldn't happen at all */
84 if (msg->cm_fields['M'] == NULL) return(0);
86 /* See if we can retrieve the previous version. */
87 old_msgnum = locate_message_by_euid(msg->cm_fields['E'], &CCC->room);
88 if (old_msgnum <= 0L) return(0);
89 snprintf(history_page, sizeof history_page, "%s_HISTORY_", msg->cm_fields['E']);
91 old_msg = CtdlFetchMessage(old_msgnum, 1);
92 if (old_msg == NULL) return(0);
94 if (old_msg->cm_fields['M'] == NULL) { /* old version is corrupt? */
95 CtdlFreeMessage(old_msg);
99 /* If no changes were made, don't bother saving it again */
100 if (!strcmp(msg->cm_fields['M'], old_msg->cm_fields['M'])) {
101 CtdlFreeMessage(old_msg);
108 CtdlMakeTempFileName(diff_old_filename, sizeof diff_old_filename);
109 CtdlMakeTempFileName(diff_new_filename, sizeof diff_new_filename);
111 fp = fopen(diff_old_filename, "w");
112 rv = fwrite(old_msg->cm_fields['M'], strlen(old_msg->cm_fields['M']), 1, fp);
114 CtdlFreeMessage(old_msg);
116 fp = fopen(diff_new_filename, "w");
117 rv = fwrite(msg->cm_fields['M'], strlen(msg->cm_fields['M']), 1, fp);
122 snprintf(diff_cmd, sizeof diff_cmd, "diff -u %s %s", diff_old_filename, diff_new_filename);
123 fp = popen(diff_cmd, "r");
126 diffbuf = realloc(diffbuf, diffbuf_len + 1025);
127 nbytes = fread(&diffbuf[diffbuf_len], 1, 1024, fp);
128 diffbuf_len += nbytes;
129 } while (nbytes == 1024);
130 diffbuf[diffbuf_len] = 0;
133 CtdlLogPrintf(CTDL_DEBUG, "diff length is %d bytes\n", diffbuf_len);
135 unlink(diff_old_filename);
136 unlink(diff_new_filename);
138 /* Determine whether this was a bogus (empty) edit */
139 if ((diffbuf_len = 0) && (diffbuf != NULL)) {
143 if (diffbuf == NULL) {
144 return(1); /* No changes at all? Abandon the post entirely! */
147 free(diffbuf); /* FIXME do something with it */
149 /* Now look for the existing edit history */
151 history_msgnum = locate_message_by_euid(history_page, &CCC->room);
153 if (history_msgnum > 0L) {
154 history_msg = CtdlFetchMessage(old_msgnum, 1);
157 /* Create a new history message if necessary */
158 if (history_msg == NULL) {
159 history_msg = malloc(sizeof(struct CtdlMessage));
160 memset(history_msg, 0, sizeof(struct CtdlMessage));
161 history_msg->cm_magic = CTDLMESSAGE_MAGIC;
162 history_msg->cm_anon_type = MES_NORMAL;
163 history_msg->cm_format_type = FMT_RFC822;
164 history_msg->cm_fields['A'] = strdup("Citadel");
165 history_msg->cm_fields['R'] = strdup(CCC->room.QRname);
166 snprintf(boundary, sizeof boundary, "Citadel--Multipart--%04x--%08lx", getpid(), time(NULL));
167 history_msg->cm_fields['M'] = malloc(1024);
168 snprintf(history_msg->cm_fields['M'], 1024,
169 "Content-type: multipart/mixed; boundary=\"%s\"\n\n"
170 "This is a Citadel wiki history encoded as multipart MIME.\n"
177 /* Update the history message (regardless of whether it's new or existing) */
178 /* FIXME now do something with it */
179 CtdlLogPrintf(CTDL_DEBUG, "\033[31m%s\033[0m", history_msg->cm_fields['M']);
189 * Module initialization
191 CTDL_MODULE_INIT(wiki)
195 CtdlRegisterMessageHook(wiki_upload_beforesave, EVT_BEFORESAVE);
198 /* return our Subversion id for the Log */