removed all references to sprintf from several files (not all files yet)
[citadel.git] / citadel / imap_misc.c
1 /*
2  * $Id$
3  *
4  *
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 <ctype.h>
31 #include <string.h>
32 #include <limits.h>
33 #include "citadel.h"
34 #include "server.h"
35 #include "sysdep_decls.h"
36 #include "citserver.h"
37 #include "support.h"
38 #include "config.h"
39 #include "dynloader.h"
40 #include "room_ops.h"
41 #include "user_ops.h"
42 #include "policy.h"
43 #include "database.h"
44 #include "msgbase.h"
45 #include "tools.h"
46 #include "internet_addressing.h"
47 #include "serv_imap.h"
48 #include "imap_tools.h"
49 #include "imap_fetch.h"
50 #include "imap_misc.h"
51 #include "genstamp.h"
52
53
54
55
56
57
58 /*
59  * imap_copy() calls imap_do_copy() to do its actual work, once it's
60  * validated and boiled down the request a bit.  (returns 0 on success)
61  */
62 int imap_do_copy(char *destination_folder) {
63         int i;
64         char roomname[ROOMNAMELEN];
65
66         i = imap_grabroom(roomname, destination_folder);
67         if (i != 0) return(i);
68
69         if (IMAP->num_msgs > 0) {
70                 for (i = 0; i < IMAP->num_msgs; ++i) {
71                         if (IMAP->flags[i] && IMAP_SELECTED) {
72                                 CtdlCopyMsgToRoom(
73                                         IMAP->msgids[i], roomname);
74                         }
75                 }
76         }
77
78         return(0);
79 }
80
81
82 /*
83  * This function is called by the main command loop.
84  */
85 void imap_copy(int num_parms, char *parms[]) {
86         int ret;
87
88         if (num_parms != 4) {
89                 cprintf("%s BAD invalid parameters\r\n", parms[0]);
90                 return;
91         }
92
93         if (imap_is_message_set(parms[2])) {
94                 imap_pick_range(parms[2], 0);
95         }
96         else {
97                 cprintf("%s BAD invalid parameters\r\n", parms[0]);
98                 return;
99         }
100
101         ret = imap_do_copy(parms[3]);
102         if (!ret) {
103                 cprintf("%s OK COPY completed\r\n", parms[0]);
104         }
105         else {
106                 cprintf("%s NO COPY failed (error %d)\r\n", parms[0], ret);
107         }
108 }
109
110 /*
111  * This function is called by the main command loop.
112  */
113 void imap_uidcopy(int num_parms, char *parms[]) {
114
115         if (num_parms != 5) {
116                 cprintf("%s BAD invalid parameters\r\n", parms[0]);
117                 return;
118         }
119
120         if (imap_is_message_set(parms[3])) {
121                 imap_pick_range(parms[3], 1);
122         }
123         else {
124                 cprintf("%s BAD invalid parameters\r\n", parms[0]);
125                 return;
126         }
127
128         if (imap_do_copy(parms[4]) == 0) {
129                 cprintf("%s OK UID COPY completed\r\n", parms[0]);
130         }
131         else {
132                 cprintf("%s NO UID COPY failed\r\n", parms[0]);
133         }
134 }
135
136
137 /*
138  * Poll for express messages (yeah, we can do this in IMAP ... I think)
139  */
140 void imap_print_express_messages(void) {
141         struct ExpressMessage *ptr, *holdptr;
142         char *dumpomatic = NULL;
143         int i;
144         size_t size, size2;
145
146         if (CC->FirstExpressMessage == NULL) {
147                 return;
148         }
149         begin_critical_section(S_SESSION_TABLE);
150         ptr = CC->FirstExpressMessage;
151         CC->FirstExpressMessage = NULL;
152         end_critical_section(S_SESSION_TABLE);
153
154         while (ptr != NULL) {
155                 size = strlen(ptr->text) + SIZ;
156                 dumpomatic = mallok(size);
157                 strcpy(dumpomatic, "");
158                 if (ptr->flags && EM_BROADCAST)
159                         strcat(dumpomatic, "Broadcast message ");
160                 else if (ptr->flags && EM_CHAT)
161                         strcat(dumpomatic, "Chat request ");
162                 else if (ptr->flags && EM_GO_AWAY)
163                         strcat(dumpomatic, "Please logoff now, as requested ");
164                 else
165                         strcat(dumpomatic, "Message ");
166
167                 size2 = strlen(dumpomatic);
168                 snprintf(&dumpomatic[size2], size - size2,
169                         "from %s:\n", ptr->sender);
170                 if (ptr->text != NULL)
171                         strcat(dumpomatic, ptr->text);
172
173                 holdptr = ptr->next;
174                 if (ptr->text != NULL) phree(ptr->text);
175                 phree(ptr);
176                 ptr = holdptr;
177
178                 for (i=0; i<strlen(dumpomatic); ++i) {
179                         if (!isprint(dumpomatic[i])) dumpomatic[i] = ' ';
180                         if (dumpomatic[i]=='\\') dumpomatic[i]='/';
181                         if (dumpomatic[i]=='\"') dumpomatic[i]='\'';
182                 }
183
184                 cprintf("* OK [ALERT] %s\r\n", dumpomatic);
185                 phree(dumpomatic);
186         }
187         cprintf("000\n");
188 }
189
190
191
192 /*
193  * This function is called by the main command loop.
194  */
195 void imap_append(int num_parms, char *parms[]) {
196         size_t literal_length;
197         struct CtdlMessage *msg;
198         int ret;
199         char roomname[ROOMNAMELEN];
200         char buf[SIZ];
201         char savedroom[ROOMNAMELEN];
202         int msgs, new;
203
204
205         if (num_parms < 4) {
206                 cprintf("%s BAD usage error\r\n", parms[0]);
207                 return;
208         }
209
210         if ( (parms[num_parms-1][0] != '{')
211            || (parms[num_parms-1][strlen(parms[num_parms-1])-1] != '}') )  {
212                 cprintf("%s BAD no message literal supplied\r\n", parms[0]);
213                 return;
214         }
215
216         literal_length = (size_t) atol(&parms[num_parms-1][1]);
217         if (literal_length < 1) {
218                 cprintf("%s BAD Message length must be at least 1.\r\n",
219                         parms[0]);
220                 return;
221         }
222
223         imap_free_transmitted_message();        /* just in case. */
224         IMAP->transmitted_message = mallok(literal_length + 1);
225         if (IMAP->transmitted_message == NULL) {
226                 cprintf("%s NO Cannot allocate memory.\r\n", parms[0]);
227                 return;
228         }
229         IMAP->transmitted_length = literal_length;
230
231         cprintf("+ Transmit message now.\r\n");
232         ret = client_read(IMAP->transmitted_message, literal_length);
233         IMAP->transmitted_message[literal_length] = 0;
234         if (ret != 1) {
235                 cprintf("%s NO Read failed.\r\n", parms[0]);
236                 return;
237         }
238
239         lprintf(9, "Converting message...\n");
240         msg = convert_internet_message(IMAP->transmitted_message);
241         IMAP->transmitted_message = NULL;
242         IMAP->transmitted_length = 0;
243
244         /* If the user is locally authenticated, FORCE the From: header to
245          * show up as the real sender.  FIXME do we really want to do this?
246          * Probably should make it site-definable or even room-definable.
247          */
248         if (CC->logged_in) {
249                 if (msg->cm_fields['A'] != NULL) phree(msg->cm_fields['A']);
250                 if (msg->cm_fields['N'] != NULL) phree(msg->cm_fields['N']);
251                 if (msg->cm_fields['H'] != NULL) phree(msg->cm_fields['H']);
252                 msg->cm_fields['A'] = strdoop(CC->usersupp.fullname);
253                 msg->cm_fields['N'] = strdoop(config.c_nodename);
254                 msg->cm_fields['H'] = strdoop(config.c_humannode);
255         }
256
257         ret = imap_grabroom(roomname, parms[2]);
258         if (ret != 0) {
259                 cprintf("%s NO Invalid mailbox name or location, or access denied\r\n",
260                         parms[0]);
261                 return;
262         }
263
264         /*
265          * usergoto() formally takes us to the desired room.  (If another
266          * folder is selected, save its name so we can return there!!!!!)
267          */
268         if (IMAP->selected) {
269                 strcpy(savedroom, CC->quickroom.QRname);
270         }
271         usergoto(roomname, 0, &msgs, &new);
272
273         /* 
274          * Can we post here?
275          */
276         ret = CtdlDoIHavePermissionToPostInThisRoom(buf);
277
278         if (ret) {
279                 /* Nope ... print an error message */
280                 cprintf("%s NO %s\r\n", parms[0], buf);
281         }
282
283         else {
284                 /* Yes ... go ahead and post! */
285                 if (msg != NULL) {
286                         CtdlSubmitMsg(msg, NULL, "");
287                 }
288                 cprintf("%s OK APPEND completed\r\n", parms[0]);
289         }
290
291         /*
292          * IMAP protocol response to client has already been sent by now.
293          *
294          * If another folder is selected, go back to that room so we can resume
295          * our happy day without violent explosions.
296          */
297         if (IMAP->selected) {
298                 usergoto(savedroom, 0, &msgs, &new);
299         }
300
301         /* We don't need this buffer anymore */
302         CtdlFreeMessage(msg);
303 }