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 char diff_old_filename[PATH_MAX];
58 char diff_new_filename[PATH_MAX];
59 char diff_cmd[PATH_MAX];
65 if (!CCC->logged_in) return(0); /* Only do this if logged in. */
67 /* Is this a room with a Wiki in it, don't run this hook. */
68 if (CCC->room.QRdefaultview != VIEW_WIKI) {
72 /* If this isn't a MIME message, don't bother. */
73 if (msg->cm_format_type != 4) return(0);
75 /* If there's no EUID we can't do this. */
76 if (msg->cm_fields['E'] == NULL) return(0);
78 /* If there's no message text, obviously this is all b0rken and shouldn't happen at all */
79 if (msg->cm_fields['M'] == NULL) return(0);
81 /* See if we can retrieve the previous version. */
82 old_msgnum = locate_message_by_euid(msg->cm_fields['E'], &CCC->room);
83 if (old_msgnum <= 0L) return(0);
85 old_msg = CtdlFetchMessage(old_msgnum, 1);
86 if (old_msg == NULL) return(0);
88 if (old_msg->cm_fields['M'] == NULL) { /* old version is corrupt? */
89 CtdlFreeMessage(old_msg);
93 /* If no changes were made, don't bother saving it again */
94 if (!strcmp(msg->cm_fields['M'], old_msg->cm_fields['M'])) {
95 CtdlFreeMessage(old_msg);
102 CtdlMakeTempFileName(diff_old_filename, sizeof diff_old_filename);
103 CtdlMakeTempFileName(diff_new_filename, sizeof diff_new_filename);
105 fp = fopen(diff_new_filename, "w");
106 rv = fwrite(msg->cm_fields['M'], strlen(msg->cm_fields['M']), 1, fp);
109 fp = fopen(diff_old_filename, "w");
110 rv = fwrite(old_msg->cm_fields['M'], strlen(old_msg->cm_fields['M']), 1, fp);
113 CtdlFreeMessage(old_msg);
115 snprintf(diff_cmd, sizeof diff_cmd, "diff -u %s %s", diff_new_filename, diff_old_filename);
116 fp = popen(diff_cmd, "r");
118 while (s = fgets(buf, sizeof buf, fp), (s != NULL)) {
119 CtdlLogPrintf(CTDL_DEBUG, "\033[32m%s\033[0m", s);
124 unlink(diff_old_filename);
125 unlink(diff_new_filename);
131 * Module initialization
133 CTDL_MODULE_INIT(wiki)
137 CtdlRegisterMessageHook(wiki_upload_beforesave, EVT_BEFORESAVE);
140 /* return our Subversion id for the Log */