c41fb12d073cfb993c005939ea883dd329847e3c
[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 by the citadel.org team
7  *
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.
12  *
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.
17  *
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
21  */
22
23 #include "sysdep.h"
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <stdio.h>
27 #include <fcntl.h>
28 #include <signal.h>
29 #include <pwd.h>
30 #include <errno.h>
31 #include <ctype.h>
32 #include <sys/types.h>
33
34 #if TIME_WITH_SYS_TIME
35 # include <sys/time.h>
36 # include <time.h>
37 #else
38 # if HAVE_SYS_TIME_H
39 #  include <sys/time.h>
40 # else
41 #  include <time.h>
42 # endif
43 #endif
44
45 #include <sys/wait.h>
46 #include <string.h>
47 #include <limits.h>
48 #include <libcitadel.h>
49 #include "citadel.h"
50 #include "server.h"
51 #include "citserver.h"
52 #include "support.h"
53 #include "config.h"
54 #include "control.h"
55 #include "user_ops.h"
56 #include "policy.h"
57 #include "database.h"
58 #include "msgbase.h"
59 #include "euidindex.h"
60 #include "ctdl_module.h"
61
62 /*
63  * Before allowing a wiki page save to execute, we have to perform version control.
64  * This involves fetching the old version of the page if it exists.
65  */
66 int wiki_upload_beforesave(struct CtdlMessage *msg) {
67         struct CitContext *CCC = CC;
68         long old_msgnum = (-1L);
69         struct CtdlMessage *old_msg = NULL;
70         long history_msgnum = (-1L);
71         struct CtdlMessage *history_msg = NULL;
72         char diff_old_filename[PATH_MAX];
73         char diff_new_filename[PATH_MAX];
74         char diff_cmd[PATH_MAX];
75         FILE *fp;
76         int rv;
77         char history_page[1024];
78         char boundary[256];
79         char prefixed_boundary[258];
80         char buf[1024];
81         int nbytes = 0;
82         char *diffbuf = NULL;
83         size_t diffbuf_len = 0;
84         char *ptr = NULL;
85
86         if (!CCC->logged_in) return(0); /* Only do this if logged in. */
87
88         /* Is this a room with a Wiki in it, don't run this hook. */
89         if (CCC->room.QRdefaultview != VIEW_WIKI) {
90                 return(0);
91         }
92
93         /* If this isn't a MIME message, don't bother. */
94         if (msg->cm_format_type != 4) return(0);
95
96         /* If there's no EUID we can't do this. */
97         if (msg->cm_fields['E'] == NULL) return(0);
98         snprintf(history_page, sizeof history_page, "%s_HISTORY_", msg->cm_fields['E']);
99
100         /* Make sure we're saving a real wiki page rather than a wiki history page.
101          * This is important in order to avoid recursing infinitely into this hook.
102          */
103         if (    (strlen(msg->cm_fields['E']) >= 9)
104                 && (!strcasecmp(&msg->cm_fields['E'][strlen(msg->cm_fields['E'])-9], "_HISTORY_"))
105         ) {
106                 CtdlLogPrintf(CTDL_DEBUG, "History page not being historied\n");
107                 return(0);
108         }
109
110         /* If there's no message text, obviously this is all b0rken and shouldn't happen at all */
111         if (msg->cm_fields['M'] == NULL) return(0);
112
113         /* See if we can retrieve the previous version. */
114         old_msgnum = locate_message_by_euid(msg->cm_fields['E'], &CCC->room);
115         if (old_msgnum > 0L) {
116                 old_msg = CtdlFetchMessage(old_msgnum, 1);
117         }
118         else {
119                 old_msg = NULL;
120         }
121
122         if ((old_msg != NULL) && (old_msg->cm_fields['M'] == NULL)) {   /* old version is corrupt? */
123                 CtdlFreeMessage(old_msg);
124                 old_msg = NULL;
125         }
126         
127         /* If no changes were made, don't bother saving it again */
128         if ((old_msg != NULL) && (!strcmp(msg->cm_fields['M'], old_msg->cm_fields['M']))) {
129                 CtdlFreeMessage(old_msg);
130                 return(1);
131         }
132
133         /*
134          * Generate diffs
135          */
136         CtdlMakeTempFileName(diff_old_filename, sizeof diff_old_filename);
137         CtdlMakeTempFileName(diff_new_filename, sizeof diff_new_filename);
138
139         if (old_msg != NULL) {
140                 fp = fopen(diff_old_filename, "w");
141                 rv = fwrite(old_msg->cm_fields['M'], strlen(old_msg->cm_fields['M']), 1, fp);
142                 fclose(fp);
143                 CtdlFreeMessage(old_msg);
144         }
145
146         fp = fopen(diff_new_filename, "w");
147         rv = fwrite(msg->cm_fields['M'], strlen(msg->cm_fields['M']), 1, fp);
148         fclose(fp);
149
150         diffbuf_len = 0;
151         diffbuf = NULL;
152         snprintf(diff_cmd, sizeof diff_cmd,
153                 "diff -u %s %s",
154                 diff_new_filename,
155                 ((old_msg != NULL) ? diff_old_filename : "/dev/null")
156         );
157         fp = popen(diff_cmd, "r");
158         if (fp != NULL) {
159                 do {
160                         diffbuf = realloc(diffbuf, diffbuf_len + 1025);
161                         nbytes = fread(&diffbuf[diffbuf_len], 1, 1024, fp);
162                         diffbuf_len += nbytes;
163                 } while (nbytes == 1024);
164                 diffbuf[diffbuf_len] = 0;
165                 pclose(fp);
166         }
167         CtdlLogPrintf(CTDL_DEBUG, "diff length is %d bytes\n", diffbuf_len);
168
169         unlink(diff_old_filename);
170         unlink(diff_new_filename);
171
172         /* Determine whether this was a bogus (empty) edit */
173         if ((diffbuf_len = 0) && (diffbuf != NULL)) {
174                 free(diffbuf);
175                 diffbuf = NULL;
176         }
177         if (diffbuf == NULL) {
178                 return(1);              /* No changes at all?  Abandon the post entirely! */
179         }
180
181         /* Now look for the existing edit history */
182
183         history_msgnum = locate_message_by_euid(history_page, &CCC->room);
184         history_msg = NULL;
185         if (history_msgnum > 0L) {
186                 history_msg = CtdlFetchMessage(history_msgnum, 1);
187         }
188
189         /* Create a new history message if necessary */
190         if (history_msg == NULL) {
191                 history_msg = malloc(sizeof(struct CtdlMessage));
192                 memset(history_msg, 0, sizeof(struct CtdlMessage));
193                 history_msg->cm_magic = CTDLMESSAGE_MAGIC;
194                 history_msg->cm_anon_type = MES_NORMAL;
195                 history_msg->cm_format_type = FMT_RFC822;
196                 history_msg->cm_fields['A'] = strdup("Citadel");
197                 history_msg->cm_fields['R'] = strdup(CCC->room.QRname);
198                 history_msg->cm_fields['E'] = strdup(history_page);
199                 history_msg->cm_fields['U'] = strdup(history_page);
200                 snprintf(boundary, sizeof boundary, "Citadel--Multipart--%04x--%08lx", getpid(), time(NULL));
201                 history_msg->cm_fields['M'] = malloc(1024);
202                 snprintf(history_msg->cm_fields['M'], 1024,
203                         "Content-type: multipart/mixed; boundary=\"%s\"\n\n"
204                         "This is a Citadel wiki history encoded as multipart MIME.\n"
205                         "Each part is comprised of a diff script representing one change set.\n"
206                         "\n"
207                         "--%s--\n"
208                         ,
209                         boundary, boundary
210                 );
211         }
212
213         /* Update the history message (regardless of whether it's new or existing) */
214
215         /* First, figure out the boundary string.  We do this even when we generated the
216          * boundary string in the above code, just to be safe and consistent.
217          */
218         strcpy(boundary, "");
219
220         ptr = history_msg->cm_fields['M'];
221         do {
222                 ptr = memreadline(ptr, buf, sizeof buf);
223                 if (*ptr != 0) {
224                         striplt(buf);
225                         if (!IsEmptyStr(buf) && (!strncasecmp(buf, "Content-type:", 13))) {
226                                 if (
227                                         (bmstrcasestr(buf, "multipart") != NULL)
228                                         && (bmstrcasestr(buf, "boundary=") != NULL)
229                                 ) {
230                                         safestrncpy(boundary, bmstrcasestr(buf, "\""), sizeof boundary);
231                                         char *qu;
232                                         qu = strchr(boundary, '\"');
233                                         if (qu) {
234                                                 strcpy(boundary, ++qu);
235                                         }
236                                         qu = strchr(boundary, '\"');
237                                         if (qu) {
238                                                 *qu = 0;
239                                         }
240                                 }
241                         }
242                 }
243         } while ( (IsEmptyStr(boundary)) && (*ptr != 0) );
244
245         /* Now look for the first boundary.  That is where we need to insert our fun.
246          */
247         if (!IsEmptyStr(boundary)) {
248                 snprintf(prefixed_boundary, sizeof prefixed_boundary, "--%s", boundary);
249                 history_msg->cm_fields['M'] = realloc(history_msg->cm_fields['M'],
250                         strlen(history_msg->cm_fields['M']) + strlen(diffbuf) + 512
251                 );
252                 ptr = bmstrcasestr(history_msg->cm_fields['M'], prefixed_boundary);
253                 if (ptr != NULL) {
254                         char *the_rest_of_it = strdup(ptr);
255                         sprintf(ptr, "--%s\n"
256                                         "Content-type: text/plain\n"
257                                         "From: %s <%s>\n"
258                                         "\n"
259                                         "%s\n"
260                                         "%s"
261                                         ,
262                                 boundary,
263                                 CCC->user.fullname,
264                                 CCC->cs_inet_email,
265                                 diffbuf,
266                                 the_rest_of_it
267                         );
268                         free(the_rest_of_it);
269                 }
270
271                 history_msg->cm_fields['T'] = realloc(history_msg->cm_fields['T'], 32);
272                 snprintf(history_msg->cm_fields['T'], 32, "%ld", time(NULL));
273         
274                 CtdlSubmitMsg(history_msg, NULL, "", 0);
275         }
276         else {
277                 CtdlLogPrintf(CTDL_ALERT, "Empty boundary string in history message.  No history!\n");
278         }
279
280         free(diffbuf);
281         free(history_msg);
282         return(0);
283 }
284
285
286 /*
287  * Module initialization
288  */
289 CTDL_MODULE_INIT(wiki)
290 {
291         if (!threading)
292         {
293                 CtdlRegisterMessageHook(wiki_upload_beforesave, EVT_BEFORESAVE);
294         }
295
296         /* return our Subversion id for the Log */
297         return "$Id$";
298 }