16 #include <sys/types.h>
18 #if TIME_WITH_SYS_TIME
19 # include <sys/time.h>
23 # include <sys/time.h>
35 #include "sysdep_decls.h"
36 #include "citserver.h"
39 #include "serv_extensions.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"
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)
62 int imap_do_copy(char *destination_folder) {
64 char roomname[ROOMNAMELEN];
66 i = imap_grabroom(roomname, destination_folder);
67 if (i != 0) return(i);
69 if (IMAP->num_msgs > 0) {
70 for (i = 0; i < IMAP->num_msgs; ++i) {
71 if (IMAP->flags[i] & IMAP_SELECTED) {
73 IMAP->msgids[i], roomname);
83 * This function is called by the main command loop.
85 void imap_copy(int num_parms, char *parms[]) {
89 cprintf("%s BAD invalid parameters\r\n", parms[0]);
93 if (imap_is_message_set(parms[2])) {
94 imap_pick_range(parms[2], 0);
97 cprintf("%s BAD invalid parameters\r\n", parms[0]);
101 ret = imap_do_copy(parms[3]);
103 cprintf("%s OK COPY completed\r\n", parms[0]);
106 cprintf("%s NO COPY failed (error %d)\r\n", parms[0], ret);
111 * This function is called by the main command loop.
113 void imap_uidcopy(int num_parms, char *parms[]) {
115 if (num_parms != 5) {
116 cprintf("%s BAD invalid parameters\r\n", parms[0]);
120 if (imap_is_message_set(parms[3])) {
121 imap_pick_range(parms[3], 1);
124 cprintf("%s BAD invalid parameters\r\n", parms[0]);
128 if (imap_do_copy(parms[4]) == 0) {
129 cprintf("%s OK UID COPY completed\r\n", parms[0]);
132 cprintf("%s NO UID COPY failed\r\n", parms[0]);
138 * Poll for instant messages (yeah, we can do this in IMAP ... I think)
140 void imap_print_instant_messages(void) {
141 struct ExpressMessage *ptr, *holdptr;
142 char *dumpomatic = NULL;
148 if (CC->FirstExpressMessage == NULL) {
151 begin_critical_section(S_SESSION_TABLE);
152 ptr = CC->FirstExpressMessage;
153 CC->FirstExpressMessage = NULL;
154 end_critical_section(S_SESSION_TABLE);
156 while (ptr != NULL) {
157 stamp = localtime(&(ptr->timestamp));
158 size = strlen(ptr->text) + SIZ;
159 dumpomatic = malloc(size);
160 strcpy(dumpomatic, "");
161 if (ptr->flags && EM_BROADCAST)
162 strcat(dumpomatic, "Broadcast message ");
163 else if (ptr->flags && EM_CHAT)
164 strcat(dumpomatic, "Chat request ");
165 else if (ptr->flags && EM_GO_AWAY)
166 strcat(dumpomatic, "Please logoff now, as requested ");
168 strcat(dumpomatic, "Message ");
170 /* Timestamp. Can this be improved? */
171 if (stamp->tm_hour == 0 || stamp->tm_hour == 12)
172 sprintf(tmp, "at 12:%02d%cm",
174 stamp->tm_hour ? 'p' : 'a');
175 else if (stamp->tm_hour > 12) /* pm */
176 sprintf(tmp, "at %d:%02dpm",
180 sprintf(tmp, "at %d:%02dam",
181 stamp->tm_hour, stamp->tm_min);
182 strcat(dumpomatic, tmp);
184 size2 = strlen(dumpomatic);
185 snprintf(&dumpomatic[size2], size - size2,
186 " from %s:\n", ptr->sender);
187 if (ptr->text != NULL)
188 strcat(dumpomatic, ptr->text);
191 if (ptr->text != NULL) free(ptr->text);
195 for (i=0; i<strlen(dumpomatic); ++i) {
196 if (!isprint(dumpomatic[i])) dumpomatic[i] = ' ';
197 if (dumpomatic[i]=='\\') dumpomatic[i]='/';
198 if (dumpomatic[i]=='\"') dumpomatic[i]='\'';
201 cprintf("* OK [ALERT] %s\r\n", dumpomatic);
210 * This function is called by the main command loop.
212 void imap_append(int num_parms, char *parms[]) {
213 size_t literal_length;
214 size_t bytes_transferred;
215 size_t stripped_length = 0;
216 struct CtdlMessage *msg;
219 char roomname[ROOMNAMELEN];
221 char savedroom[ROOMNAMELEN];
227 cprintf("%s BAD usage error\r\n", parms[0]);
231 if ( (parms[num_parms-1][0] != '{')
232 || (parms[num_parms-1][strlen(parms[num_parms-1])-1] != '}') ) {
233 cprintf("%s BAD no message literal supplied\r\n", parms[0]);
237 literal_length = (size_t) atol(&parms[num_parms-1][1]);
238 if (literal_length < 1) {
239 cprintf("%s BAD Message length must be at least 1.\r\n",
244 imap_free_transmitted_message(); /* just in case. */
245 IMAP->transmitted_message = malloc(literal_length + 1);
246 if (IMAP->transmitted_message == NULL) {
247 cprintf("%s NO Cannot allocate memory.\r\n", parms[0]);
250 IMAP->transmitted_length = literal_length;
252 cprintf("+ Transmit message now.\r\n");
253 lprintf(CTDL_DEBUG, "imap_append() expecting %d bytes\n",
256 bytes_transferred = 0;
259 blksize = literal_length - bytes_transferred;
260 if (blksize > SIZ) blksize = SIZ;
263 ret = client_read(&IMAP->transmitted_message[bytes_transferred], blksize);
265 bytes_transferred = literal_length; /* bail out */
268 bytes_transferred += blksize; /* keep going */
270 lprintf(CTDL_DEBUG, "Received %d of %d bytes (%d%%)\n",
273 ((bytes_transferred * 100) / literal_length)
275 } while (bytes_transferred < literal_length);
277 IMAP->transmitted_message[literal_length] = 0;
279 cprintf("%s NO Read failed.\r\n", parms[0]);
282 lprintf(CTDL_DEBUG, "imap_append() finished reading message\n");
284 /* I think there's supposed to be a trailing CRLF after the
285 * literal (the message text) is received. This call to
286 * client_gets() absorbs it.
290 lprintf(CTDL_DEBUG, "Trailing CRLF: %s\n", buf);
292 /* Convert RFC822 newlines (CRLF) to Unix newlines (LF) */
293 lprintf(CTDL_DEBUG, "Converting newline format\n");
295 for (i=0; i<literal_length; ++i) {
296 if (strncmp(&IMAP->transmitted_message[i], "\r\n", 2)) {
297 IMAP->transmitted_message[stripped_length++] = IMAP->transmitted_message[i];
300 literal_length = stripped_length;
301 IMAP->transmitted_message[literal_length] = 0; /* reterminate it */
303 lprintf(CTDL_DEBUG, "Converting message format\n");
304 msg = convert_internet_message(IMAP->transmitted_message);
305 IMAP->transmitted_message = NULL;
306 IMAP->transmitted_length = 0;
308 ret = imap_grabroom(roomname, parms[2]);
310 cprintf("%s NO Invalid mailbox name or location, or access denied\r\n",
316 * usergoto() formally takes us to the desired room. (If another
317 * folder is selected, save its name so we can return there!!!!!)
319 if (IMAP->selected) {
320 strcpy(savedroom, CC->room.QRname);
322 usergoto(roomname, 0, 0, &msgs, &new);
324 /* If the user is locally authenticated, FORCE the From: header to
325 * show up as the real sender. FIXME do we really want to do this?
326 * Probably should make it site-definable or even room-definable.
328 * For now, we allow "forgeries" if the room is one of the user's
332 if ( (CC->room.QRflags & QR_MAILBOX) == 0) {
333 if (msg->cm_fields['A'] != NULL) free(msg->cm_fields['A']);
334 if (msg->cm_fields['N'] != NULL) free(msg->cm_fields['N']);
335 if (msg->cm_fields['H'] != NULL) free(msg->cm_fields['H']);
336 msg->cm_fields['A'] = strdup(CC->user.fullname);
337 msg->cm_fields['N'] = strdup(config.c_nodename);
338 msg->cm_fields['H'] = strdup(config.c_humannode);
345 ret = CtdlDoIHavePermissionToPostInThisRoom(buf, sizeof buf);
348 /* Nope ... print an error message */
349 cprintf("%s NO %s\r\n", parms[0], buf);
353 /* Yes ... go ahead and post! */
355 CtdlSubmitMsg(msg, NULL, "");
357 cprintf("%s OK APPEND completed\r\n", parms[0]);
361 * IMAP protocol response to client has already been sent by now.
363 * If another folder is selected, go back to that room so we can resume
364 * our happy day without violent explosions.
366 if (IMAP->selected) {
367 usergoto(savedroom, 0, 0, &msgs, &new);
370 /* We don't need this buffer anymore */
371 CtdlFreeMessage(msg);