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];
65 char history_page[1024];
69 if (!CCC->logged_in) return(0); /* Only do this if logged in. */
71 /* Is this a room with a Wiki in it, don't run this hook. */
72 if (CCC->room.QRdefaultview != VIEW_WIKI) {
76 /* If this isn't a MIME message, don't bother. */
77 if (msg->cm_format_type != 4) return(0);
79 /* If there's no EUID we can't do this. */
80 if (msg->cm_fields['E'] == NULL) return(0);
82 /* If there's no message text, obviously this is all b0rken and shouldn't happen at all */
83 if (msg->cm_fields['M'] == NULL) return(0);
85 /* See if we can retrieve the previous version. */
86 old_msgnum = locate_message_by_euid(msg->cm_fields['E'], &CCC->room);
87 if (old_msgnum <= 0L) return(0);
88 snprintf(history_page, sizeof history_page, "%s_HISTORY_", msg->cm_fields['E']);
90 old_msg = CtdlFetchMessage(old_msgnum, 1);
91 if (old_msg == NULL) return(0);
93 if (old_msg->cm_fields['M'] == NULL) { /* old version is corrupt? */
94 CtdlFreeMessage(old_msg);
98 /* If no changes were made, don't bother saving it again */
99 if (!strcmp(msg->cm_fields['M'], old_msg->cm_fields['M'])) {
100 CtdlFreeMessage(old_msg);
107 CtdlMakeTempFileName(diff_old_filename, sizeof diff_old_filename);
108 CtdlMakeTempFileName(diff_new_filename, sizeof diff_new_filename);
110 fp = fopen(diff_old_filename, "w");
111 rv = fwrite(old_msg->cm_fields['M'], strlen(old_msg->cm_fields['M']), 1, fp);
113 CtdlFreeMessage(old_msg);
115 fp = fopen(diff_new_filename, "w");
116 rv = fwrite(msg->cm_fields['M'], strlen(msg->cm_fields['M']), 1, fp);
119 snprintf(diff_cmd, sizeof diff_cmd, "diff -u %s %s", diff_old_filename, diff_new_filename);
120 fp = popen(diff_cmd, "r");
123 nbytes = fread(buf, 1, sizeof buf, fp);
125 /* FIXME now do something with it */
126 CtdlLogPrintf(CTDL_DEBUG, "\033[32mREAD %d BYTES\033[0m\n", nbytes);
128 } while (nbytes == sizeof(buf));
132 unlink(diff_old_filename);
133 unlink(diff_new_filename);
135 /* Now look for the existing edit history */
137 history_msgnum = locate_message_by_euid(history_page, &CCC->room);
139 if (history_msgnum > 0L) {
140 history_msg = CtdlFetchMessage(old_msgnum, 1);
143 /* Create a new history message if necessary */
144 if (history_msg == NULL) {
145 history_msg = malloc(sizeof(struct CtdlMessage));
146 memset(history_msg, 0, sizeof(struct CtdlMessage));
147 history_msg->cm_magic = CTDLMESSAGE_MAGIC;
148 history_msg->cm_anon_type = MES_NORMAL;
149 history_msg->cm_format_type = FMT_RFC822;
150 history_msg->cm_fields['A'] = strdup("Citadel");
151 history_msg->cm_fields['R'] = strdup(CCC->room.QRname);
152 snprintf(boundary, sizeof boundary, "Citadel--Multipart--%04x--%08lx", getpid(), time(NULL));
153 history_msg->cm_fields['M'] = malloc(1024);
154 snprintf(history_msg->cm_fields['M'], 1024,
155 "Content-type: multipart/mixed; boundary=\"%s\"\n\n"
156 "This is a Citadel wiki history encoded as multipart MIME.\n"
163 /* Update the history message (regardless of whether it's new or existing) */
164 /* FIXME now do something with it */
165 CtdlLogPrintf(CTDL_DEBUG, "\033[31m%s\033[0m", history_msg->cm_fields['M']);
175 * Module initialization
177 CTDL_MODULE_INIT(wiki)
181 CtdlRegisterMessageHook(wiki_upload_beforesave, EVT_BEFORESAVE);
184 /* return our Subversion id for the Log */