* Began writing code to compare wiki page versions and feed them through 'diff' for...
[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         char diff_old_filename[PATH_MAX];
58         char diff_new_filename[PATH_MAX];
59         char diff_cmd[PATH_MAX];
60         FILE *fp;
61         char *s;
62         char buf[1024];
63         int rv;
64
65         if (!CCC->logged_in) return(0); /* Only do this if logged in. */
66
67         /* Is this a room with a Wiki in it, don't run this hook. */
68         if (CCC->room.QRdefaultview != VIEW_WIKI) {
69                 return(0);
70         }
71
72         /* If this isn't a MIME message, don't bother. */
73         if (msg->cm_format_type != 4) return(0);
74
75         /* If there's no EUID we can't do this. */
76         if (msg->cm_fields['E'] == NULL) return(0);
77
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);
80
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);
84
85         old_msg = CtdlFetchMessage(old_msgnum, 1);
86         if (old_msg == NULL) return(0);
87
88         if (old_msg->cm_fields['M'] == NULL) {          /* old version is corrupt? */
89                 CtdlFreeMessage(old_msg);
90                 return(0);
91         }
92
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);
96                 return(1);
97         }
98
99         /*
100          * Generate diffs
101          */
102         CtdlMakeTempFileName(diff_old_filename, sizeof diff_old_filename);
103         CtdlMakeTempFileName(diff_new_filename, sizeof diff_new_filename);
104
105         fp = fopen(diff_new_filename, "w");
106         rv = fwrite(msg->cm_fields['M'], strlen(msg->cm_fields['M']), 1, fp);
107         fclose(fp);
108
109         fp = fopen(diff_old_filename, "w");
110         rv = fwrite(old_msg->cm_fields['M'], strlen(old_msg->cm_fields['M']), 1, fp);
111         fclose(fp);
112
113         CtdlFreeMessage(old_msg);
114
115         snprintf(diff_cmd, sizeof diff_cmd, "diff -u %s %s", diff_new_filename, diff_old_filename);
116         fp = popen(diff_cmd, "r");
117         if (fp != NULL) {
118                 while (s = fgets(buf, sizeof buf, fp), (s != NULL)) {
119                         CtdlLogPrintf(CTDL_DEBUG, "\033[32m%s\033[0m", s);
120                 }
121                 pclose(fp);
122         }
123
124         unlink(diff_old_filename);
125         unlink(diff_new_filename);
126         return(0);
127 }
128
129
130 /*
131  * Module initialization
132  */
133 CTDL_MODULE_INIT(wiki)
134 {
135         if (!threading)
136         {
137                 CtdlRegisterMessageHook(wiki_upload_beforesave, EVT_BEFORESAVE);
138         }
139
140         /* return our Subversion id for the Log */
141         return "$Id$";
142 }