* more work on the wiki revision control engine
[citadel.git] / citadel / modules / wiki / serv_wiki.c
1 /*
2  * $Id$
3  *
4  * Server-side module for Wiki rooms.  This will handle things like version control. 
5  * 
6  * Copyright (c) 2009 / released under the GNU General Public License v3
7  */
8
9 #include "sysdep.h"
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <stdio.h>
13 #include <fcntl.h>
14 #include <signal.h>
15 #include <pwd.h>
16 #include <errno.h>
17 #include <ctype.h>
18 #include <sys/types.h>
19
20 #if TIME_WITH_SYS_TIME
21 # include <sys/time.h>
22 # include <time.h>
23 #else
24 # if HAVE_SYS_TIME_H
25 #  include <sys/time.h>
26 # else
27 #  include <time.h>
28 # endif
29 #endif
30
31 #include <sys/wait.h>
32 #include <string.h>
33 #include <limits.h>
34 #include <libcitadel.h>
35 #include "citadel.h"
36 #include "server.h"
37 #include "citserver.h"
38 #include "support.h"
39 #include "config.h"
40 #include "control.h"
41 #include "room_ops.h"
42 #include "user_ops.h"
43 #include "policy.h"
44 #include "database.h"
45 #include "msgbase.h"
46 #include "euidindex.h"
47 #include "ctdl_module.h"
48
49 /*
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
52  */
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];
62         FILE *fp;
63         int rv;
64         char history_page[1024];
65         char boundary[256];
66         int nbytes = 0;
67         char *diffbuf = NULL;
68         size_t diffbuf_len = 0;
69
70         if (!CCC->logged_in) return(0); /* Only do this if logged in. */
71
72         /* Is this a room with a Wiki in it, don't run this hook. */
73         if (CCC->room.QRdefaultview != VIEW_WIKI) {
74                 return(0);
75         }
76
77         /* If this isn't a MIME message, don't bother. */
78         if (msg->cm_format_type != 4) return(0);
79
80         /* If there's no EUID we can't do this. */
81         if (msg->cm_fields['E'] == NULL) return(0);
82
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);
85
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']);
90
91         old_msg = CtdlFetchMessage(old_msgnum, 1);
92         if (old_msg == NULL) return(0);
93
94         if (old_msg->cm_fields['M'] == NULL) {          /* old version is corrupt? */
95                 CtdlFreeMessage(old_msg);
96                 return(0);
97         }
98
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);
102                 return(1);
103         }
104
105         /*
106          * Generate diffs
107          */
108         CtdlMakeTempFileName(diff_old_filename, sizeof diff_old_filename);
109         CtdlMakeTempFileName(diff_new_filename, sizeof diff_new_filename);
110
111         fp = fopen(diff_old_filename, "w");
112         rv = fwrite(old_msg->cm_fields['M'], strlen(old_msg->cm_fields['M']), 1, fp);
113         fclose(fp);
114         CtdlFreeMessage(old_msg);
115
116         fp = fopen(diff_new_filename, "w");
117         rv = fwrite(msg->cm_fields['M'], strlen(msg->cm_fields['M']), 1, fp);
118         fclose(fp);
119
120         diffbuf_len = 0;
121         diffbuf = NULL;
122         snprintf(diff_cmd, sizeof diff_cmd, "diff -u %s %s", diff_old_filename, diff_new_filename);
123         fp = popen(diff_cmd, "r");
124         if (fp != NULL) {
125                 do {
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;
131                 pclose(fp);
132         }
133         CtdlLogPrintf(CTDL_DEBUG, "diff length is %d bytes\n", diffbuf_len);
134
135         unlink(diff_old_filename);
136         unlink(diff_new_filename);
137
138         /* Determine whether this was a bogus (empty) edit */
139         if ((diffbuf_len = 0) && (diffbuf != NULL)) {
140                 free(diffbuf);
141                 diffbuf = NULL;
142         }
143         if (diffbuf == NULL) {
144                 return(1);              /* No changes at all?  Abandon the post entirely! */
145         }
146
147         free(diffbuf);  /* FIXME do something with it */
148
149         /* Now look for the existing edit history */
150
151         history_msgnum = locate_message_by_euid(history_page, &CCC->room);
152         history_msg = NULL;
153         if (history_msgnum > 0L) {
154                 history_msg = CtdlFetchMessage(old_msgnum, 1);
155         }
156
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"
171                         "--%s--\n"
172                         ,
173                         boundary, boundary
174                 );
175         }
176
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']);
180
181         /* FIXME */
182
183         free(history_msg);
184         return(0);
185 }
186
187
188 /*
189  * Module initialization
190  */
191 CTDL_MODULE_INIT(wiki)
192 {
193         if (!threading)
194         {
195                 CtdlRegisterMessageHook(wiki_upload_beforesave, EVT_BEFORESAVE);
196         }
197
198         /* return our Subversion id for the Log */
199         return "$Id$";
200 }