871650691a698e6f8e207fa8c20bda1b45ccd1db
[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         char *s;
64         char buf[1024];
65         int rv;
66         char history_page[1024];
67         char boundary[256];
68
69         if (!CCC->logged_in) return(0); /* Only do this if logged in. */
70
71         /* Is this a room with a Wiki in it, don't run this hook. */
72         if (CCC->room.QRdefaultview != VIEW_WIKI) {
73                 return(0);
74         }
75
76         /* If this isn't a MIME message, don't bother. */
77         if (msg->cm_format_type != 4) return(0);
78
79         /* If there's no EUID we can't do this. */
80         if (msg->cm_fields['E'] == NULL) return(0);
81
82         /* If there's no message text, obviously this is all b0rken and shouldn't happen at all */
83         if (msg->cm_fields['M'] == NULL) return(0);
84
85         /* See if we can retrieve the previous version. */
86         old_msgnum = locate_message_by_euid(msg->cm_fields['E'], &CCC->room);
87         if (old_msgnum <= 0L) return(0);
88         snprintf(history_page, sizeof history_page, "%s_HISTORY_", msg->cm_fields['E']);
89
90         old_msg = CtdlFetchMessage(old_msgnum, 1);
91         if (old_msg == NULL) return(0);
92
93         if (old_msg->cm_fields['M'] == NULL) {          /* old version is corrupt? */
94                 CtdlFreeMessage(old_msg);
95                 return(0);
96         }
97
98         /* If no changes were made, don't bother saving it again */
99         if (!strcmp(msg->cm_fields['M'], old_msg->cm_fields['M'])) {
100                 CtdlFreeMessage(old_msg);
101                 return(1);
102         }
103
104         /*
105          * Generate diffs
106          */
107         CtdlMakeTempFileName(diff_old_filename, sizeof diff_old_filename);
108         CtdlMakeTempFileName(diff_new_filename, sizeof diff_new_filename);
109
110         fp = fopen(diff_old_filename, "w");
111         rv = fwrite(old_msg->cm_fields['M'], strlen(old_msg->cm_fields['M']), 1, fp);
112         fclose(fp);
113         CtdlFreeMessage(old_msg);
114
115         fp = fopen(diff_new_filename, "w");
116         rv = fwrite(msg->cm_fields['M'], strlen(msg->cm_fields['M']), 1, fp);
117         fclose(fp);
118
119         snprintf(diff_cmd, sizeof diff_cmd, "diff -u %s %s", diff_old_filename, diff_new_filename);
120         fp = popen(diff_cmd, "r");
121         if (fp != NULL) {
122                 while (s = fgets(buf, sizeof buf, fp), (s != NULL)) {
123                         /* FIXME now do something with it */
124                         CtdlLogPrintf(CTDL_DEBUG, "\033[32m%s\033[0m", s);
125                 }
126                 pclose(fp);
127         }
128
129         unlink(diff_old_filename);
130         unlink(diff_new_filename);
131
132         /* Now look for the existing edit history */
133
134         history_msgnum = locate_message_by_euid(history_page, &CCC->room);
135         history_msg = NULL;
136         if (history_msgnum > 0L) {
137                 history_msg = CtdlFetchMessage(old_msgnum, 1);
138         }
139
140         /* Create a new history message if necessary */
141         if (history_msg == NULL) {
142                 history_msg = malloc(sizeof(struct CtdlMessage));
143                 memset(history_msg, 0, sizeof(struct CtdlMessage));
144                 history_msg->cm_magic = CTDLMESSAGE_MAGIC;
145                 history_msg->cm_anon_type = MES_NORMAL;
146                 history_msg->cm_format_type = FMT_RFC822;
147                 history_msg->cm_fields['A'] = strdup("Citadel");
148                 history_msg->cm_fields['R'] = strdup(CCC->room.QRname);
149                 snprintf(boundary, sizeof boundary, "Citadel--Multipart--%04x--%08lx", getpid(), time(NULL));
150                 history_msg->cm_fields['M'] = malloc(1024);
151                 snprintf(history_msg->cm_fields['M'], 1024,
152                         "Content-type: multipart/mixed; boundary=\"%s\"\n"
153                         "This is a Citadel wiki history encoded as multipart MIME.\n"
154                         "--%s--\n"
155                         ,
156                         boundary, boundary
157                 );
158         }
159
160         /* Update the history message (regardless of whether it's new or existing) */
161
162         /* FIXME */
163
164         free(history_msg);
165         return(0);
166 }
167
168
169 /*
170  * Module initialization
171  */
172 CTDL_MODULE_INIT(wiki)
173 {
174         if (!threading)
175         {
176                 CtdlRegisterMessageHook(wiki_upload_beforesave, EVT_BEFORESAVE);
177         }
178
179         /* return our Subversion id for the Log */
180         return "$Id$";
181 }