* Performing a 'fetch' operation on an old revision of a wiki page now stuffs the...
[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) + 1024
251                 );
252                 ptr = bmstrcasestr(history_msg->cm_fields['M'], prefixed_boundary);
253                 if (ptr != NULL) {
254                         char *the_rest_of_it = strdup(ptr);
255                         char uuid[32];
256                         char memo[512];
257                         char encoded_memo[768];
258                         generate_uuid(uuid);
259                         snprintf(memo, sizeof memo, "%s|%ld|%s|%s", 
260                                 uuid,
261                                 time(NULL),
262                                 CCC->user.fullname,
263                                 config.c_nodename
264                                 /* no longer logging CCC->cs_inet_email */
265                         );
266                         CtdlEncodeBase64(encoded_memo, memo, strlen(memo), 0);
267                         sprintf(ptr, "--%s\n"
268                                         "Content-type: text/plain\n"
269                                         "Content-Disposition: inline; filename=\"%s\"\n"
270                                         "Content-Transfer-Encoding: 8bit\n"
271                                         "\n"
272                                         "%s\n"
273                                         "%s"
274                                         ,
275                                 boundary,
276                                 encoded_memo,
277                                 diffbuf,
278                                 the_rest_of_it
279                         );
280                         free(the_rest_of_it);
281                 }
282
283                 history_msg->cm_fields['T'] = realloc(history_msg->cm_fields['T'], 32);
284                 snprintf(history_msg->cm_fields['T'], 32, "%ld", time(NULL));
285         
286                 CtdlSubmitMsg(history_msg, NULL, "", 0);
287         }
288         else {
289                 CtdlLogPrintf(CTDL_ALERT, "Empty boundary string in history message.  No history!\n");
290         }
291
292         free(diffbuf);
293         free(history_msg);
294         return(0);
295 }
296
297
298 /*
299  * MIME Parser callback for wiki_history()
300  *
301  * The "filename" field will contain a memo field.  All we have to do is decode
302  * the base64 and output it.  The data is already in a delimited format suitable
303  * for our client protocol.
304  */
305 void wiki_history_callback(char *name, char *filename, char *partnum, char *disp,
306                    void *content, char *cbtype, char *cbcharset, size_t length,
307                    char *encoding, char *cbid, void *cbuserdata)
308 {
309         char memo[1024];
310
311         CtdlDecodeBase64(memo, filename, strlen(filename));
312         cprintf("%s\n", memo);
313 }
314
315
316 /*
317  * Fetch a list of revisions for a particular wiki page
318  */
319 void wiki_history(char *pagename) {
320         int r;
321         char history_page_name[270];
322         long msgnum;
323         struct CtdlMessage *msg;
324
325         r = CtdlDoIHavePermissionToReadMessagesInThisRoom();
326         if (r != om_ok) {
327                 if (r == om_not_logged_in) {
328                         cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
329                 }
330                 else {
331                         cprintf("%d An unknown error has occurred.\n", ERROR);
332                 }
333                 return;
334         }
335
336         snprintf(history_page_name, sizeof history_page_name, "%s_HISTORY_", pagename);
337         msgnum = locate_message_by_euid(history_page_name, &CC->room);
338         if (msgnum > 0L) {
339                 msg = CtdlFetchMessage(msgnum, 1);
340         }
341         else {
342                 msg = NULL;
343         }
344
345         if ((msg != NULL) && (msg->cm_fields['M'] == NULL)) {
346                 CtdlFreeMessage(msg);
347                 msg = NULL;
348         }
349
350         if (msg == NULL) {
351                 cprintf("%d Revision history for '%s' was not found.\n", ERROR+MESSAGE_NOT_FOUND, pagename);
352                 return;
353         }
354
355         
356         cprintf("%d Revision history for '%s'\n", LISTING_FOLLOWS, pagename);
357         mime_parser(msg->cm_fields['M'], NULL, *wiki_history_callback, NULL, NULL, NULL, 0);
358         cprintf("000\n");
359
360         CtdlFreeMessage(msg);
361         return;
362 }
363
364
365 struct HistoryEraserCallBackData {
366         char *tempfilename;             /* name of temp file being patched */
367         char *stop_when;                /* stop when we hit this uuid */
368         int done;                       /* set to nonzero when we're done patching */
369 };
370
371
372
373 /*
374  * MIME Parser callback for wiki_rev()
375  *
376  * The "filename" field will contain a memo field.  All we have to do is decode
377  * the base64 and output it.  The data is already in a delimited format suitable
378  * for our client protocol.
379  */
380 void wiki_rev_callback(char *name, char *filename, char *partnum, char *disp,
381                    void *content, char *cbtype, char *cbcharset, size_t length,
382                    char *encoding, char *cbid, void *cbuserdata)
383 {
384         struct HistoryEraserCallBackData *hecbd = (struct HistoryEraserCallBackData *)cbuserdata;
385         char memo[1024];
386         char this_rev[256];
387         FILE *fp;
388         char *ptr = NULL;
389         char buf[1024];
390
391         /* Did a previous callback already indicate that we've reached our target uuid?
392          * If so, don't process anything else.
393          */
394         if (hecbd->done) {
395                 return;
396         }
397
398         CtdlDecodeBase64(memo, filename, strlen(filename));
399         extract_token(this_rev, memo, 0, '|', sizeof this_rev);
400         CtdlLogPrintf(CTDL_DEBUG, "callback found rev: %s\n", this_rev);
401
402         /* Perform the patch */
403         fp = popen("patch -f -s -p0 >/dev/null 2>/dev/null", "w");
404         if (fp) {
405                 /* Replace the filenames in the patch with the tempfilename we're actually tweaking */
406                 fprintf(fp, "--- %s\n", hecbd->tempfilename);
407                 fprintf(fp, "+++ %s\n", hecbd->tempfilename);
408
409                 ptr = (char *)content;
410                 int linenum = 0;
411                 do {
412                         ++linenum;
413                         ptr = memreadline(ptr, buf, sizeof buf);
414                         if (*ptr != 0) {
415                                 if (linenum <= 2) {
416                                         /* skip the first two lines; they contain bogus filenames */
417                                 }
418                                 else {
419                                         fprintf(fp, "%s\n", buf);
420                                 }
421                         }
422                 } while ((*ptr != 0) && ((int)ptr < ((int)content + length)));
423                 pclose(fp);
424         }
425
426         if (!strcasecmp(this_rev, hecbd->stop_when)) {
427                 CtdlLogPrintf(CTDL_DEBUG, "Found our target rev.  Stopping!\n");
428                 hecbd->done = 1;
429         }
430 }
431
432
433 /*
434  * Fetch a specific revision of a wiki page
435  */
436 void wiki_rev(char *pagename, char *rev, char *operation)
437 {
438         int r;
439         char history_page_name[270];
440         long msgnum;
441         char temp[PATH_MAX];
442         struct CtdlMessage *msg;
443         FILE *fp;
444         struct HistoryEraserCallBackData hecbd;
445         int rv;
446
447         r = CtdlDoIHavePermissionToReadMessagesInThisRoom();
448         if (r != om_ok) {
449                 if (r == om_not_logged_in) {
450                         cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
451                 }
452                 else {
453                         cprintf("%d An unknown error has occurred.\n", ERROR);
454                 }
455                 return;
456         }
457
458         /* Begin by fetching the current version of the page.  We're going to patch
459          * backwards through the diffs until we get the one we want.
460          */
461         msgnum = locate_message_by_euid(pagename, &CC->room);
462         if (msgnum > 0L) {
463                 msg = CtdlFetchMessage(msgnum, 1);
464         }
465         else {
466                 msg = NULL;
467         }
468
469         if ((msg != NULL) && (msg->cm_fields['M'] == NULL)) {
470                 CtdlFreeMessage(msg);
471                 msg = NULL;
472         }
473
474         if (msg == NULL) {
475                 cprintf("%d Page '%s' was not found.\n", ERROR+MESSAGE_NOT_FOUND, pagename);
476                 return;
477         }
478
479         /* Output it to a file... */
480
481         CtdlMakeTempFileName(temp, sizeof temp);
482         fp = fopen(temp, "w");
483         if (fp != NULL) {
484                 r = fwrite(msg->cm_fields['M'], strlen(msg->cm_fields['M']), 1, fp);
485                 fclose(fp);
486         }
487         else {
488                 CtdlLogPrintf(CTDL_ALERT, "Cannot open %s: %s\n", temp, strerror(errno));
489         }
490         CtdlFreeMessage(msg);
491
492         /* Now go get the revision history and patch backwards through the diffs until
493          * we get to the revision we want.
494          */
495
496         snprintf(history_page_name, sizeof history_page_name, "%s_HISTORY_", pagename);
497         msgnum = locate_message_by_euid(history_page_name, &CC->room);
498         if (msgnum > 0L) {
499                 msg = CtdlFetchMessage(msgnum, 1);
500         }
501         else {
502                 msg = NULL;
503         }
504
505         if ((msg != NULL) && (msg->cm_fields['M'] == NULL)) {
506                 CtdlFreeMessage(msg);
507                 msg = NULL;
508         }
509
510         if (msg == NULL) {
511                 cprintf("%d Revision history for '%s' was not found.\n", ERROR+MESSAGE_NOT_FOUND, pagename);
512                 return;
513         }
514
515         memset(&hecbd, 0, sizeof(struct HistoryEraserCallBackData));
516         hecbd.tempfilename = temp;
517         hecbd.stop_when = rev;
518
519         mime_parser(msg->cm_fields['M'], NULL, *wiki_rev_callback, NULL, NULL, (void *)&hecbd, 0);
520         CtdlFreeMessage(msg);
521
522         if (hecbd.done == 0) {
523                 cprintf("%d Revision '%s' of page '%s' was not found.\n",
524                         ERROR + MESSAGE_NOT_FOUND, rev, pagename
525                 );
526         }
527         else if (!strcasecmp(operation, "fetch")) {
528                 msg = malloc(sizeof(struct CtdlMessage));
529                 memset(msg, 0, sizeof(struct CtdlMessage));
530                 msg->cm_magic = CTDLMESSAGE_MAGIC;
531                 msg->cm_anon_type = MES_NORMAL;
532                 msg->cm_format_type = FMT_RFC822;
533                 msg->cm_fields['A'] = strdup("Citadel");
534                 fp = fopen(temp, "r");
535                 if (fp) {
536                         long len;
537                         fseek(fp, 0L, SEEK_END);
538                         len = ftell(fp);
539                         fseek(fp, 0L, SEEK_SET);
540                         msg->cm_fields['M'] = malloc(len + 1);
541                         rv = fread(msg->cm_fields['M'], len, 1, fp);
542                         msg->cm_fields['M'][len] = 0;
543                         fclose(fp);
544                 }
545                 char *wwm = "9999999999.WikiWaybackMachine";
546                 CtdlCreateRoom(wwm, 5, "", 0, 1, 1, VIEW_BBS);
547                 msgnum = CtdlSubmitMsg(msg, NULL, wwm, 0);      /* FIXME put somewhere else */
548                 CtdlFreeMessage(msg);
549                 cprintf("%d %ld\n", CIT_OK, msgnum);
550         }
551         else if (!strcasecmp(operation, "revert")) {
552                 cprintf("%d FIXME not finished yet, check the log to find out wtf\n", ERROR);
553         }
554         else {
555                 cprintf("%d An unknown operation was requested.\n", ERROR+CMD_NOT_SUPPORTED);
556         }
557
558         unlink(temp);
559         return;
560 }
561
562
563
564 /*
565  * commands related to wiki management
566  */
567 void cmd_wiki(char *argbuf) {
568         char subcmd[32];
569         char pagename[256];
570         char rev[128];
571         char operation[16];
572
573         extract_token(subcmd, argbuf, 0, '|', sizeof subcmd);
574
575         if (!strcasecmp(subcmd, "history")) {
576                 extract_token(pagename, argbuf, 1, '|', sizeof pagename);
577                 wiki_history(pagename);
578                 return;
579         }
580
581         if (!strcasecmp(subcmd, "rev")) {
582                 extract_token(pagename, argbuf, 1, '|', sizeof pagename);
583                 extract_token(rev, argbuf, 2, '|', sizeof rev);
584                 extract_token(operation, argbuf, 3, '|', sizeof operation);
585                 wiki_rev(pagename, rev, operation);
586                 return;
587         }
588
589         cprintf("%d Invalid subcommand\n", ERROR + CMD_NOT_SUPPORTED);
590 }
591
592
593
594 /*
595  * Module initialization
596  */
597 CTDL_MODULE_INIT(wiki)
598 {
599         if (!threading)
600         {
601                 CtdlRegisterMessageHook(wiki_upload_beforesave, EVT_BEFORESAVE);
602                 CtdlRegisterProtoHook(cmd_wiki, "WIKI", "Commands related to Wiki management");
603         }
604
605         /* return our Subversion id for the Log */
606         return "$Id$";
607 }