* wiki still needs some tuning
[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-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 "room_ops.h"
56 #include "user_ops.h"
57 #include "policy.h"
58 #include "database.h"
59 #include "msgbase.h"
60 #include "euidindex.h"
61 #include "ctdl_module.h"
62
63 /*
64  * Before allowing a wiki page save to execute, we have to perform version control.
65  * This involves fetching the old version of the page if it exists.
66  */
67 int wiki_upload_beforesave(struct CtdlMessage *msg) {
68         struct CitContext *CCC = CC;
69         long old_msgnum = (-1L);
70         struct CtdlMessage *old_msg = NULL;
71         long history_msgnum = (-1L);
72         struct CtdlMessage *history_msg = NULL;
73         char diff_old_filename[PATH_MAX];
74         char diff_new_filename[PATH_MAX];
75         char diff_cmd[PATH_MAX];
76         FILE *fp;
77         int rv;
78         char history_page[1024];
79         char boundary[256];
80         char endary[260];
81         char buf[1024];
82         int nbytes = 0;
83         char *diffbuf = NULL;
84         size_t diffbuf_len = 0;
85         char *ptr = NULL;
86
87         if (!CCC->logged_in) return(0); /* Only do this if logged in. */
88
89         /* Is this a room with a Wiki in it, don't run this hook. */
90         if (CCC->room.QRdefaultview != VIEW_WIKI) {
91                 return(0);
92         }
93
94         /* If this isn't a MIME message, don't bother. */
95         if (msg->cm_format_type != 4) return(0);
96
97         /* If there's no EUID we can't do this. */
98         if (msg->cm_fields['E'] == NULL) return(0);
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) return(0);
116         snprintf(history_page, sizeof history_page, "%s_HISTORY_", msg->cm_fields['E']);
117
118         old_msg = CtdlFetchMessage(old_msgnum, 1);
119
120         if ((old_msg != NULL) && (old_msg->cm_fields['M'] == NULL)) {   /* old version is corrupt? */
121                 CtdlFreeMessage(old_msg);
122                 old_msg = NULL;
123         }
124         
125         /* If no changes were made, don't bother saving it again */
126         if ((old_msg != NULL) && (!strcmp(msg->cm_fields['M'], old_msg->cm_fields['M']))) {
127                 CtdlFreeMessage(old_msg);
128                 return(1);
129         }
130
131         /*
132          * Generate diffs
133          */
134         CtdlMakeTempFileName(diff_old_filename, sizeof diff_old_filename);
135         CtdlMakeTempFileName(diff_new_filename, sizeof diff_new_filename);
136
137         if (old_msg != NULL) {
138                 fp = fopen(diff_old_filename, "w");
139                 rv = fwrite(old_msg->cm_fields['M'], strlen(old_msg->cm_fields['M']), 1, fp);
140                 fclose(fp);
141                 CtdlFreeMessage(old_msg);
142         }
143
144         fp = fopen(diff_new_filename, "w");
145         rv = fwrite(msg->cm_fields['M'], strlen(msg->cm_fields['M']), 1, fp);
146         fclose(fp);
147
148         diffbuf_len = 0;
149         diffbuf = NULL;
150         snprintf(diff_cmd, sizeof diff_cmd,
151                 "diff -u %s %s",
152                 ((old_msg != NULL) ? diff_old_filename : "/dev/null"),
153                 diff_new_filename
154         );
155         fp = popen(diff_cmd, "r");
156         if (fp != NULL) {
157                 do {
158                         diffbuf = realloc(diffbuf, diffbuf_len + 1025);
159                         nbytes = fread(&diffbuf[diffbuf_len], 1, 1024, fp);
160                         diffbuf_len += nbytes;
161                 } while (nbytes == 1024);
162                 diffbuf[diffbuf_len] = 0;
163                 pclose(fp);
164         }
165         CtdlLogPrintf(CTDL_DEBUG, "diff length is %d bytes\n", diffbuf_len);
166
167         unlink(diff_old_filename);
168         unlink(diff_new_filename);
169
170         /* Determine whether this was a bogus (empty) edit */
171         if ((diffbuf_len = 0) && (diffbuf != NULL)) {
172                 free(diffbuf);
173                 diffbuf = NULL;
174         }
175         if (diffbuf == NULL) {
176                 return(1);              /* No changes at all?  Abandon the post entirely! */
177         }
178
179         /* Now look for the existing edit history */
180
181         history_msgnum = locate_message_by_euid(history_page, &CCC->room);
182         history_msg = NULL;
183         if (history_msgnum > 0L) {
184                 history_msg = CtdlFetchMessage(history_msgnum, 1);
185         }
186
187         /* Create a new history message if necessary */
188         if (history_msg == NULL) {
189                 history_msg = malloc(sizeof(struct CtdlMessage));
190                 memset(history_msg, 0, sizeof(struct CtdlMessage));
191                 history_msg->cm_magic = CTDLMESSAGE_MAGIC;
192                 history_msg->cm_anon_type = MES_NORMAL;
193                 history_msg->cm_format_type = FMT_RFC822;
194                 history_msg->cm_fields['A'] = strdup("Citadel");
195                 history_msg->cm_fields['R'] = strdup(CCC->room.QRname);
196                 history_msg->cm_fields['E'] = strdup(history_page);
197                 snprintf(boundary, sizeof boundary, "Citadel--Multipart--%04x--%08lx", getpid(), time(NULL));
198                 history_msg->cm_fields['M'] = malloc(1024);
199                 snprintf(history_msg->cm_fields['M'], 1024,
200                         "Content-type: multipart/mixed; boundary=\"%s\"\n\n"
201                         "This is a Citadel wiki history encoded as multipart MIME.\n"
202                         "--%s--\n"
203                         ,
204                         boundary, boundary
205                 );
206         }
207
208         /* Update the history message (regardless of whether it's new or existing) */
209
210         /* First, figure out the boundary string.  We do this even when we generated the
211          * boundary string in the above code, just to be safe and consistent.
212          */
213         strcpy(boundary, "");
214
215         ptr = history_msg->cm_fields['M'];
216         do {
217                 ptr = memreadline(ptr, buf, sizeof buf);
218                 if (*ptr != 0) {
219                         striplt(buf);
220                         if (!IsEmptyStr(buf) && (!strncasecmp(buf, "Content-type:", 13))) {
221                                 if (
222                                         (bmstrcasestr(buf, "multipart") != NULL)
223                                         && (bmstrcasestr(buf, "boundary=") != NULL)
224                                 ) {
225                                         safestrncpy(boundary, bmstrcasestr(buf, "\""), sizeof boundary);
226                                         char *qu;
227                                         qu = strchr(boundary, '\"');
228                                         if (qu) {
229                                                 strcpy(boundary, ++qu);
230                                         }
231                                         qu = strchr(boundary, '\"');
232                                         if (qu) {
233                                                 *qu = 0;
234                                         }
235                                 }
236                         }
237                 }
238         } while ( (IsEmptyStr(boundary)) && (*ptr != 0) );
239
240         if (!IsEmptyStr(boundary)) {
241                 snprintf(endary, sizeof endary, "--%s--", boundary);
242                 history_msg->cm_fields['M'] = realloc(history_msg->cm_fields['M'],
243                         strlen(history_msg->cm_fields['M']) + strlen(diffbuf) + 512
244                 );
245                 ptr = bmstrcasestr(history_msg->cm_fields['M'], endary);
246                 if (ptr != NULL) {
247                         sprintf(ptr, "--%s\n"
248                                         "Content-type: text/plain\n"
249                                         "From: %s <%s>\n"
250                                         "\n"
251                                         "%s\n"
252                                         "--%s--\n"
253                                         ,
254                                 boundary,
255                                 CCC->user.fullname,
256                                 CCC->cs_inet_email,
257                                 diffbuf,
258                                 boundary
259                         );
260                 }
261
262                 history_msg->cm_fields['T'] = realloc(history_msg->cm_fields['T'], 32);
263                 snprintf(history_msg->cm_fields['T'], 32, "%ld", time(NULL));
264         
265                 CtdlSubmitMsg(history_msg, NULL, "", 0);
266         }
267         else {
268                 CtdlLogPrintf(CTDL_ALERT, "Empty boundary string in history message.  No history!\n");
269         }
270
271         free(diffbuf);
272         free(history_msg);
273         return(0);
274 }
275
276
277 /*
278  * Module initialization
279  */
280 CTDL_MODULE_INIT(wiki)
281 {
282         if (!threading)
283         {
284                 CtdlRegisterMessageHook(wiki_upload_beforesave, EVT_BEFORESAVE);
285         }
286
287         /* return our Subversion id for the Log */
288         return "$Id$";
289 }