7cb5c60fad9b68dbd173484d3499425d04cf58fb
[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         int no_changes_were_made = 0;
58
59         if (!CCC->logged_in) return(0); /* Only do this if logged in. */
60
61         /* Is this a room with a Wiki in it, don't run this hook. */
62         if (CCC->room.QRdefaultview != VIEW_WIKI) {
63                 return(0);
64         }
65
66         /* If this isn't a MIME message, don't bother. */
67         if (msg->cm_format_type != 4) return(0);
68
69         /* If there's no EUID we can't do this. */
70         if (msg->cm_fields['E'] == NULL) return(0);
71
72         /* See if we can retrieve the previous version. */
73         old_msgnum = locate_message_by_euid(msg->cm_fields['E'], &CCC->room);
74         if (old_msgnum <= 0L) return(0);
75
76         old_msg = CtdlFetchMessage(old_msgnum, 1);
77         if (old_msg == NULL) return(0);
78
79         if ((msg->cm_fields['M'] != NULL) && (old_msg->cm_fields['M'] != NULL)) {
80
81                 /* If no changes were made, don't bother saving it again */
82                 if (!strcmp(msg->cm_fields['M'], old_msg->cm_fields['M'])) {
83                         no_changes_were_made = 1;
84                 }
85
86                 /* FIXME here's where diffs should be generated
87                  *
88                 FILE *fp;
89                 fp = fopen("/tmp/new.txt", "w");
90                 fwrite(msg->cm_fields['M'], strlen(msg->cm_fields['M']), 1, fp);
91                 fclose(fp);
92                 fp = fopen("/tmp/old.txt", "w");
93                 fwrite(old_msg->cm_fields['M'], strlen(old_msg->cm_fields['M']), 1, fp);
94                 fclose(fp);
95                  *
96                  */
97         }
98
99         CtdlFreeMessage(old_msg);
100         return(no_changes_were_made);
101 }
102
103
104 /*
105  * Module initialization
106  */
107 CTDL_MODULE_INIT(wiki)
108 {
109         if (!threading)
110         {
111                 CtdlRegisterMessageHook(wiki_upload_beforesave, EVT_BEFORESAVE);
112         }
113
114         /* return our Subversion id for the Log */
115         return "$Id$";
116 }