Did away with lprintf all together now its called CtdlLogPrintf()
[citadel.git] / citadel / modules / notes / serv_notes.c
1 /*
2  * $Id$
3  *
4  * Handles functions related to yellow sticky notes.
5  *
6  */
7
8 #include "sysdep.h"
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <stdio.h>
12 #include <fcntl.h>
13 #include <signal.h>
14 #include <pwd.h>
15 #include <errno.h>
16 #include <sys/types.h>
17
18 #if TIME_WITH_SYS_TIME
19 # include <sys/time.h>
20 # include <time.h>
21 #else
22 # if HAVE_SYS_TIME_H
23 #  include <sys/time.h>
24 # else
25 #  include <time.h>
26 # endif
27 #endif
28
29 #include <sys/wait.h>
30 #include <string.h>
31 #include <limits.h>
32 #include <libcitadel.h>
33 #include "citadel.h"
34 #include "server.h"
35 #include "citserver.h"
36 #include "support.h"
37 #include "config.h"
38 #include "room_ops.h"
39 #include "user_ops.h"
40 #include "policy.h"
41 #include "database.h"
42 #include "msgbase.h"
43
44 #include "ctdl_module.h"
45
46
47
48 /*
49  * If we are in a "notes" view room, and the client has sent an RFC822
50  * message containing an X-KOrg-Note-Id: field (Aethera does this, as
51  * do some Kolab clients) then set both the Subject and the Exclusive ID
52  * of the message to that.  It's going to be a UUID so we want to replace
53  * any existing message containing that UUID.
54  */
55 int serv_notes_beforesave(struct CtdlMessage *msg)
56 {
57         char *p;
58         int a, i;
59         char uuid[SIZ];
60
61         /* First determine if this room has the "notes" view set */
62
63         if (CC->room.QRdefaultview != VIEW_NOTES) {
64                 return(0);                      /* not notes; do nothing */
65         }
66
67         /* It must be an RFC822 message! */
68         if (msg->cm_format_type != 4) {
69                 return(0);      /* You tried to save a non-RFC822 message! */
70         }
71         
72         /* Find the X-KOrg-Note-Id: header */
73         strcpy(uuid, "");
74         p = msg->cm_fields['M'];
75         a = strlen(p);
76         while (--a > 0) {
77                 if (!strncasecmp(p, "X-KOrg-Note-Id: ", 16)) {  /* Found it */
78                         safestrncpy(uuid, p + 16, sizeof(uuid));
79                         for (i = 0; uuid[i]; ++i) {
80                                 if ( (uuid[i] == '\r') || (uuid[i] == '\n') ) {
81                                         uuid[i] = 0;
82                                         break;
83                                 }
84                         }
85
86                         CtdlLogPrintf(9, "UUID of note is: %s\n", uuid);
87                         if (!IsEmptyStr(uuid)) {
88
89                                 if (msg->cm_fields['E'] != NULL) {
90                                         free(msg->cm_fields['E']);
91                                 }
92                                 msg->cm_fields['E'] = strdup(uuid);
93
94                                 if (msg->cm_fields['U'] != NULL) {
95                                         free(msg->cm_fields['U']);
96                                 }
97                                 msg->cm_fields['U'] = strdup(uuid);
98                         }
99                 }
100                 p++;
101         }
102         
103         return(0);
104 }
105
106
107 CTDL_MODULE_INIT(notes)
108 {
109         if (!threading)
110         {
111                 CtdlRegisterMessageHook(serv_notes_beforesave, EVT_BEFORESAVE);
112         }
113         
114         /* return our Subversion id for the Log */
115         return "$Id$";
116 }