citadel
About: Citadel is an advanced messaging and collaboration system for groupware and BBS applications (preferred OS: Linux).
  Fossies Dox: citadel.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

imap_fetch.c
Go to the documentation of this file.
1/*
2 * Implements the FETCH command in IMAP.
3 * This is a good example of the protocol's gratuitous complexity.
4 *
5 * Copyright (c) 2001-2020 by the citadel.org team
6 *
7 * This program is open source software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 */
17
18
19#include "sysdep.h"
20#include <stdlib.h>
21#include <unistd.h>
22#include <stdio.h>
23#include <fcntl.h>
24#include <signal.h>
25#include <pwd.h>
26#include <errno.h>
27#include <sys/types.h>
28#include <time.h>
29#include <sys/wait.h>
30#include <ctype.h>
31#include <string.h>
32#include <limits.h>
33#include <libcitadel.h>
34#include "citadel.h"
35#include "server.h"
36#include "sysdep_decls.h"
37#include "citserver.h"
38#include "support.h"
39#include "config.h"
40#include "user_ops.h"
41#include "database.h"
42#include "msgbase.h"
43#include "internet_addressing.h"
44#include "serv_imap.h"
45#include "imap_tools.h"
46#include "imap_fetch.h"
47#include "genstamp.h"
48#include "ctdl_module.h"
49
50
51
52/*
53 * Individual field functions for imap_do_fetch_msg() ...
54 */
55
56void imap_fetch_uid(int seq) {
57 IAPrintf("UID %ld", IMAP->msgids[seq-1]);
58}
59
60void imap_fetch_flags(int seq)
61{
62 citimap *Imap = IMAP;
63 int num_flags_printed = 0;
64 IAPuts("FLAGS (");
65 if (Imap->flags[seq] & IMAP_DELETED) {
66 if (num_flags_printed > 0)
67 IAPuts(" ");
68 IAPuts("\\Deleted");
69 ++num_flags_printed;
70 }
71 if (Imap->flags[seq] & IMAP_SEEN) {
72 if (num_flags_printed > 0)
73 IAPuts(" ");
74 IAPuts("\\Seen");
75 ++num_flags_printed;
76 }
77 if (Imap->flags[seq] & IMAP_ANSWERED) {
78 if (num_flags_printed > 0)
79 IAPuts(" ");
80 IAPuts("\\Answered");
81 ++num_flags_printed;
82 }
83 if (Imap->flags[seq] & IMAP_RECENT) {
84 if (num_flags_printed > 0)
85 IAPuts(" ");
86 IAPuts("\\Recent");
87 ++num_flags_printed;
88 }
89 IAPuts(")");
90}
91
92
94 char datebuf[64];
95 time_t msgdate;
96
97 if (!msg) return;
98 if (!CM_IsEmpty(msg, eTimestamp)) {
99 msgdate = atol(msg->cm_fields[eTimestamp]);
100 }
101 else {
102 msgdate = time(NULL);
103 }
104
105 datestring(datebuf, sizeof datebuf, msgdate, DATESTRING_IMAP);
106 IAPrintf( "INTERNALDATE \"%s\"", datebuf);
107}
108
109
110/*
111 * Fetch RFC822-formatted messages.
112 *
113 * 'whichfmt' should be set to one of:
114 * "RFC822" entire message
115 * "RFC822.HEADER" headers only (with trailing blank line)
116 * "RFC822.SIZE" size of translated message
117 * "RFC822.TEXT" body only (without leading blank line)
118 */
119void imap_fetch_rfc822(long msgnum, const char *whichfmt) {
120 CitContext *CCC = CC;
121 citimap *Imap = CCCIMAP;
122 const char *ptr = NULL;
123 size_t headers_size, text_size, total_size;
124 size_t bytes_to_send = 0;
125 struct MetaData smi;
126 int need_to_rewrite_metadata = 0;
127 int need_body = 0;
128
129 /* Determine whether this particular fetch operation requires
130 * us to fetch the message body from disk. If not, we can save
131 * on some disk operations...
132 */
133 if ( (!strcasecmp(whichfmt, "RFC822")) || (!strcasecmp(whichfmt, "RFC822.TEXT")) ) {
134 need_body = 1;
135 }
136
137 /* If this is an RFC822.SIZE fetch, first look in the message's
138 * metadata record to see if we've saved that information.
139 */
140 if (!strcasecmp(whichfmt, "RFC822.SIZE")) {
141 GetMetaData(&smi, msgnum);
142 if (smi.meta_rfc822_length > 0L) {
143 IAPrintf("RFC822.SIZE %ld", smi.meta_rfc822_length);
144 return;
145 }
146 need_to_rewrite_metadata = 1;
147 need_body = 1;
148 }
149
150 /* Cache the most recent RFC822 FETCH because some clients like to
151 * fetch in pieces, and we don't want to have to go back to the
152 * message store for each piece. We also burn the cache if the
153 * client requests something that involves reading the message
154 * body, but we haven't fetched the body yet.
155 */
156 if ((Imap->cached_rfc822 != NULL)
157 && (Imap->cached_rfc822_msgnum == msgnum)
158 && (Imap->cached_rfc822_withbody || (!need_body)) ) {
159 /* Good to go! */
160 }
161 else if (Imap->cached_rfc822 != NULL) {
162 /* Some other message is cached -- free it */
163 FreeStrBuf(&Imap->cached_rfc822);
164 Imap->cached_rfc822_msgnum = (-1);
165 }
166
167 /* At this point, we now can fetch and convert the message iff it's not
168 * the one we had cached.
169 */
170 if (Imap->cached_rfc822 == NULL) {
171 /*
172 * Load the message into memory for translation & measurement
173 */
174 CCC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
175 CtdlOutputMsg(msgnum, MT_RFC822,
176 (need_body ? HEADERS_ALL : HEADERS_FAST),
177 0, 1, NULL, SUPPRESS_ENV_TO, NULL, NULL, NULL
178 );
179 if (!need_body) {
180 client_write(HKEY("\r\n")); // extra trailing newline -- to the redirect_buffer, *not* to the client
181 }
182 Imap->cached_rfc822 = CCC->redirect_buffer;
183 CCC->redirect_buffer = NULL;
184 Imap->cached_rfc822_msgnum = msgnum;
185 Imap->cached_rfc822_withbody = need_body;
186 if ( (need_to_rewrite_metadata) &&
187 (StrLength(Imap->cached_rfc822) > 0) ) {
188 smi.meta_rfc822_length = StrLength(Imap->cached_rfc822);
190 }
191 }
192
193 /*
194 * Now figure out where the headers/text break is. IMAP considers the
195 * intervening blank line to be part of the headers, not the text.
196 */
197 headers_size = 0;
198
199 if (need_body) {
200 StrBuf *Line = NewStrBuf();
201 ptr = NULL;
202 do {
203 StrBufSipLine(Line, Imap->cached_rfc822, &ptr);
204
205 if ((StrLength(Line) != 0) && (ptr != StrBufNOTNULL))
206 {
207 StrBufTrim(Line);
208 if ((StrLength(Line) != 0) &&
209 (ptr != StrBufNOTNULL) )
210 {
211 headers_size = ptr - ChrPtr(Imap->cached_rfc822);
212 }
213 }
214 } while ( (headers_size == 0) &&
215 (ptr != StrBufNOTNULL) );
216
217 total_size = StrLength(Imap->cached_rfc822);
218 text_size = total_size - headers_size;
219 FreeStrBuf(&Line);
220 }
221 else {
222 headers_size = total_size = StrLength(Imap->cached_rfc822);
223 text_size = 0;
224 }
225
226 syslog(LOG_DEBUG, "imap: RFC822 headers=" SIZE_T_FMT ", text=" SIZE_T_FMT ", total=" SIZE_T_FMT, headers_size, text_size, total_size);
227
228 if (!strcasecmp(whichfmt, "RFC822.SIZE")) {
229 IAPrintf("RFC822.SIZE " SIZE_T_FMT, total_size);
230 return;
231 }
232
233 else if (!strcasecmp(whichfmt, "RFC822")) {
234 ptr = ChrPtr(Imap->cached_rfc822);
235 bytes_to_send = total_size;
236 }
237
238 else if (!strcasecmp(whichfmt, "RFC822.HEADER")) {
239 ptr = ChrPtr(Imap->cached_rfc822);
240 bytes_to_send = headers_size;
241 }
242
243 else if (!strcasecmp(whichfmt, "RFC822.TEXT")) {
244 ptr = &ChrPtr(Imap->cached_rfc822)[headers_size];
245 bytes_to_send = text_size;
246 }
247
248 IAPrintf("%s {" SIZE_T_FMT "}\r\n", whichfmt, bytes_to_send);
249 iaputs(ptr, bytes_to_send);
250}
251
252
253/*
254 * Load a specific part of a message into the temp file to be output to a
255 * client. FIXME we can handle parts like "2" and "2.1" and even "2.MIME"
256 * but we still can't handle "2.HEADER" (which might not be a problem).
257 *
258 * Note: mime_parser() was called with dont_decode set to 1, so we have the
259 * luxury of simply spewing without having to re-encode.
260 */
261void imap_load_part(char *name, char *filename, char *partnum, char *disp,
262 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
263 char *cbid, void *cbuserdata)
264{
265 char mimebuf2[SIZ];
266 StrBuf *desired_section;
267
268 desired_section = (StrBuf *)cbuserdata;
269 syslog(LOG_DEBUG, "imap: imap_load_part() looking for %s, found %s", ChrPtr(desired_section), partnum);
270
271 if (!strcasecmp(partnum, ChrPtr(desired_section))) {
272 client_write(content, length);
273 }
274
275 snprintf(mimebuf2, sizeof mimebuf2, "%s.MIME", partnum);
276
277 if (!strcasecmp(ChrPtr(desired_section), mimebuf2)) {
278 client_write(HKEY("Content-type: "));
279 client_write(cbtype, strlen(cbtype));
280 if (!IsEmptyStr(cbcharset)) {
281 client_write(HKEY("; charset=\""));
282 client_write(cbcharset, strlen(cbcharset));
283 client_write(HKEY("\""));
284 }
285 if (!IsEmptyStr(name)) {
286 client_write(HKEY("; name=\""));
287 client_write(name, strlen(name));
288 client_write(HKEY("\""));
289 }
290 client_write(HKEY("\r\n"));
291 if (!IsEmptyStr(encoding)) {
292 client_write(HKEY("Content-Transfer-Encoding: "));
293 client_write(encoding, strlen(encoding));
294 client_write(HKEY("\r\n"));
295 }
296 if (!IsEmptyStr(encoding)) {
297 client_write(HKEY("Content-Disposition: "));
298 client_write(disp, strlen(disp));
299
300 if (!IsEmptyStr(filename)) {
301 client_write(HKEY("; filename=\""));
302 client_write(filename, strlen(filename));
303 client_write(HKEY("\""));
304 }
305 client_write(HKEY("\r\n"));
306 }
307 cprintf("Content-Length: %ld\r\n\r\n", (long)length);
308 }
309}
310
311
312/*
313 * Called by imap_fetch_envelope() to output the "From" field.
314 * This is in its own function because its logic is kind of complex. We
315 * really need to make this suck less.
316 */
318 char user[SIZ], node[SIZ], name[SIZ];
319
320 if (!msg) return;
321
322 /* For anonymous messages, it's so easy! */
323 if (!is_room_aide() && (msg->cm_anon_type == MES_ANONONLY)) {
324 IAPuts("((\"----\" NIL \"x\" \"x.org\")) ");
325 return;
326 }
327 if (!is_room_aide() && (msg->cm_anon_type == MES_ANONOPT)) {
328 IAPuts("((\"anonymous\" NIL \"x\" \"x.org\")) ");
329 return;
330 }
331
332 /* For everything else, we do stuff. */
333 IAPuts("(("); // open double-parens
334 IPutMsgField(eAuthor); // display name
335 IAPuts(" NIL "); // source route (not used)
336
337 if (!CM_IsEmpty(msg, erFc822Addr)) {
338 process_rfc822_addr(msg->cm_fields[erFc822Addr], user, node, name);
339 IPutStr(user, strlen(user)); /* mailbox name (user id) */
340 IAPuts(" ");
341 IPutStr(node, strlen(node)); /* host name */
342 }
343 else {
344 IPutMsgField(eAuthor); /* Make up a synthetic address */
345 IAPuts(" ");
346 IPutStr(CtdlGetConfigStr("c_fqdn"), strlen(CtdlGetConfigStr("c_fqdn")));
347 }
348
349 IAPuts(")) "); /* close double-parens */
350}
351
352
353/*
354 * Output an envelope address (or set of addresses) in the official,
355 * convoluted, braindead format. (Note that we can't use this for
356 * the "From" address because its data may come from a number of different
357 * fields. But we can use it for "To" and possibly others.
358 */
360 char individual_addr[256];
361 int num_addrs;
362 int i;
363 char user[256];
364 char node[256];
365 char name[256];
366
367 if (addr == NULL) {
368 IAPuts("NIL ");
369 return;
370 }
371
372 if (IsEmptyStr(addr)) {
373 IAPuts("NIL ");
374 return;
375 }
376
377 IAPuts("(");
378
379 /* How many addresses are listed here? */
380 num_addrs = num_tokens(addr, ',');
381
382 /* Output them one by one. */
383 for (i=0; i<num_addrs; ++i) {
384 extract_token(individual_addr, addr, i, ',', sizeof individual_addr);
385 striplt(individual_addr);
386 process_rfc822_addr(individual_addr, user, node, name);
387 IAPuts("(");
388 IPutStr(name, strlen(name));
389 IAPuts(" NIL ");
390 IPutStr(user, strlen(user));
391 IAPuts(" ");
392 IPutStr(node, strlen(node));
393 IAPuts(")");
394 if (i < (num_addrs-1))
395 IAPuts(" ");
396 }
397
398 IAPuts(") ");
399}
400
401
402/*
403 * Implements the ENVELOPE fetch item
404 *
405 * Note that the imap_strout() function can cleverly output NULL fields as NIL,
406 * so we don't have to check for that condition like we do elsewhere.
407 */
409 char datestringbuf[SIZ];
410 time_t msgdate;
411 char *fieldptr = NULL;
412 long len;
413
414 if (!msg) return;
415
416 /* Parse the message date into an IMAP-format date string */
417 if (!CM_IsEmpty(msg, eTimestamp)) {
418 msgdate = atol(msg->cm_fields[eTimestamp]);
419 }
420 else {
421 msgdate = time(NULL);
422 }
423 len = datestring(datestringbuf, sizeof datestringbuf, msgdate, DATESTRING_IMAP);
424
425 /* Now start spewing data fields. The order is important, as it is
426 * defined by the protocol specification. Nonexistent fields must
427 * be output as NIL, existent fields must be quoted or literalled.
428 * The imap_strout() function conveniently does all this for us.
429 */
430 IAPuts("ENVELOPE (");
431
432 /* Date */
433 IPutStr(datestringbuf, len);
434 IAPuts(" ");
435
436 /* Subject */
438 IAPuts(" ");
439
440 /* From */
442
443 /* Sender (default to same as 'From' if not present) */
444 fieldptr = rfc822_fetch_field(msg->cm_fields[eMesageText], "Sender");
445 if (fieldptr != NULL) {
447 free(fieldptr);
448 }
449 else {
451 }
452
453 /* Reply-to */
454 fieldptr = rfc822_fetch_field(msg->cm_fields[eMesageText], "Reply-to");
455 if (fieldptr != NULL) {
457 free(fieldptr);
458 }
459 else {
461 }
462
463 /* To */
465
466 /* Cc (we do it this way because there might be a legacy non-Citadel Cc: field present) */
467 fieldptr = msg->cm_fields[eCarbonCopY];
468 if (fieldptr != NULL) {
470 }
471 else {
472 fieldptr = rfc822_fetch_field(msg->cm_fields[eMesageText], "Cc");
474 if (fieldptr != NULL) free(fieldptr);
475 }
476
477 /* Bcc */
478 fieldptr = rfc822_fetch_field(msg->cm_fields[eMesageText], "Bcc");
480 if (fieldptr != NULL) free(fieldptr);
481
482 /* In-reply-to */
483 fieldptr = rfc822_fetch_field(msg->cm_fields[eMesageText], "In-reply-to");
484 IPutStr(fieldptr, (fieldptr)?strlen(fieldptr):0);
485 IAPuts(" ");
486 if (fieldptr != NULL) free(fieldptr);
487
488 /* message ID */
489 len = msg->cm_lengths[emessageId];
490
491 if ((len == 0) || (
492 (msg->cm_fields[emessageId][0] == '<') &&
493 (msg->cm_fields[emessageId][len - 1] == '>'))
494 ) {
496 }
497 else
498 {
499 char *Buf = malloc(len + 3);
500 long pos = 0;
501
502 if (msg->cm_fields[emessageId][0] != '<')
503 {
504 Buf[pos] = '<';
505 pos ++;
506 }
507 memcpy(&Buf[pos], msg->cm_fields[emessageId], len);
508 pos += len;
509 if (msg->cm_fields[emessageId][len] != '>')
510 {
511 Buf[pos] = '>';
512 pos++;
513 }
514 Buf[pos] = '\0';
515 IPutStr(Buf, pos);
516 free(Buf);
517 }
518 IAPuts(")");
519}
520
521
522/*
523 * This function is called only when CC->redirect_buffer contains a set of
524 * RFC822 headers with no body attached. Its job is to strip that set of
525 * headers down to *only* the ones we're interested in.
526 */
527void imap_strip_headers(StrBuf *section) {
528 citimap_command Cmd;
529 StrBuf *which_fields = NULL;
530 int doing_headers = 0;
531 int headers_not = 0;
532 int num_parms = 0;
533 int i;
534 StrBuf *boiled_headers = NULL;
535 StrBuf *Line;
536 int ok = 0;
537 int done_headers = 0;
538 const char *Ptr = NULL;
539 CitContext *CCC = CC;
540
541 if (CCC->redirect_buffer == NULL) return;
542
543 which_fields = NewStrBufDup(section);
544
545 if (!strncasecmp(ChrPtr(which_fields), "HEADER.FIELDS", 13))
546 doing_headers = 1;
547 if (doing_headers &&
548 !strncasecmp(ChrPtr(which_fields), "HEADER.FIELDS.NOT", 17))
549 headers_not = 1;
550
551 for (i=0; i < StrLength(which_fields); ++i) {
552 if (ChrPtr(which_fields)[i]=='(')
553 StrBufReplaceToken(which_fields, i, 1, HKEY(""));
554 }
555 for (i=0; i < StrLength(which_fields); ++i) {
556 if (ChrPtr(which_fields)[i]==')') {
557 StrBufCutAt(which_fields, i, NULL);
558 break;
559 }
560 }
561 memset(&Cmd, 0, sizeof(citimap_command));
562 Cmd.CmdBuf = which_fields;
563 num_parms = imap_parameterize(&Cmd);
564
565 boiled_headers = NewStrBufPlain(NULL, StrLength(CCC->redirect_buffer));
566 Line = NewStrBufPlain(NULL, SIZ);
567 Ptr = NULL;
568 ok = 0;
569 do {
570 StrBufSipLine(Line, CCC->redirect_buffer, &Ptr);
571
572 if (!isspace(ChrPtr(Line)[0])) {
573
574 if (doing_headers == 0) ok = 1;
575 else {
576 /* we're supposed to print all headers that are not matching the filter list */
577 if (headers_not) for (i=0, ok = 1; (i < num_parms) && (ok == 1); ++i) {
578 if ( (!strncasecmp(ChrPtr(Line),
579 Cmd.Params[i].Key,
580 Cmd.Params[i].len)) &&
581 (ChrPtr(Line)[Cmd.Params[i].len]==':') ) {
582 ok = 0;
583 }
584 }
585 /* we're supposed to print all headers matching the filterlist */
586 else for (i=0, ok = 0; ((i < num_parms) && (ok == 0)); ++i) {
587 if ( (!strncasecmp(ChrPtr(Line),
588 Cmd.Params[i].Key,
589 Cmd.Params[i].len)) &&
590 (ChrPtr(Line)[Cmd.Params[i].len]==':') ) {
591 ok = 1;
592 }
593 }
594 }
595 }
596
597 if (ok) {
598 StrBufAppendBuf(boiled_headers, Line, 0);
599 StrBufAppendBufPlain(boiled_headers, HKEY("\r\n"), 0);
600 }
601
602 if ((Ptr == StrBufNOTNULL) ||
603 (StrLength(Line) == 0) ||
604 (ChrPtr(Line)[0]=='\r') ||
605 (ChrPtr(Line)[0]=='\n') ) done_headers = 1;
606 } while (!done_headers);
607
608 StrBufAppendBufPlain(boiled_headers, HKEY("\r\n"), 0);
609
610 /* Now save it back (it'll always be smaller) */
611 FreeStrBuf(&CCC->redirect_buffer);
612 CCC->redirect_buffer = boiled_headers;
613
614 free(Cmd.Params);
615 FreeStrBuf(&which_fields);
616 FreeStrBuf(&Line);
617}
618
619
620/*
621 * Implements the BODY and BODY.PEEK fetch items
622 */
623void imap_fetch_body(long msgnum, ConstStr item, int is_peek) {
624 struct CtdlMessage *msg = NULL;
625 StrBuf *section;
626 StrBuf *partial;
627 int is_partial = 0;
628 size_t pstart, pbytes;
629 int loading_body_now = 0;
630 int need_body = 1;
631 int burn_the_cache = 0;
632 CitContext *CCC = CC;
633 citimap *Imap = CCCIMAP;
634
635 /* extract section */
636 section = NewStrBufPlain(CKEY(item));
637
638 if (strchr(ChrPtr(section), '[') != NULL) {
639 StrBufStripAllBut(section, '[', ']');
640 }
641 syslog(LOG_DEBUG, "imap: selected section is [%s]", (StrLength(section) == 0) ? "(empty)" : ChrPtr(section));
642
643 /* Burn the cache if we don't have the same section of the
644 * same message again.
645 */
646 if (Imap->cached_body != NULL) {
647 if (Imap->cached_bodymsgnum != msgnum) {
648 burn_the_cache = 1;
649 }
650 else if ( (!Imap->cached_body_withbody) && (need_body) ) {
651 burn_the_cache = 1;
652 }
653 else if (strcasecmp(Imap->cached_bodypart, ChrPtr(section))) {
654 burn_the_cache = 1;
655 }
656 if (burn_the_cache) {
657 /* Yup, go ahead and burn the cache. */
658 free(Imap->cached_body);
659 Imap->cached_body_len = 0;
660 Imap->cached_body = NULL;
661 Imap->cached_bodymsgnum = (-1);
662 strcpy(Imap->cached_bodypart, "");
663 }
664 }
665
666 /* extract partial */
667 partial = NewStrBufPlain(CKEY(item));
668 if (strchr(ChrPtr(partial), '<') != NULL) {
669 StrBufStripAllBut(partial, '<', '>');
670 is_partial = 1;
671 }
672 if ( (is_partial == 1) && (StrLength(partial) > 0) ) {
673 syslog(LOG_DEBUG, "imap: selected partial is <%s>", ChrPtr(partial));
674 }
675
676 if (Imap->cached_body == NULL) {
677 CCC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
678 loading_body_now = 1;
679 msg = CtdlFetchMessage(msgnum, (need_body ? 1 : 0));
680 }
681
682 /* Now figure out what the client wants, and get it */
683
684 if (!loading_body_now) {
685 /* What we want is already in memory */
686 }
687
688 else if ( (!strcmp(ChrPtr(section), "1")) && (msg->cm_format_type != 4) ) {
690 }
691
692 else if (StrLength(section) == 0) {
694 }
695
696 /*
697 * If the client asked for just headers, or just particular header
698 * fields, strip it down.
699 */
700 else if (!strncasecmp(ChrPtr(section), "HEADER", 6)) {
701 /* This used to work with HEADERS_FAST, but then Apple got stupid with their
702 * IMAP library and this broke Mail.App and iPhone Mail, so we had to change it
703 * to HEADERS_ONLY so the trendy hipsters with their iPhones can read mail.
704 */
706 imap_strip_headers(section);
707 }
708
709 /*
710 * Strip it down if the client asked for everything _except_ headers.
711 */
712 else if (!strncasecmp(ChrPtr(section), "TEXT", 4)) {
714 }
715
716 /*
717 * Anything else must be a part specifier.
718 * (Note value of 1 passed as 'dont_decode' so client gets it encoded)
719 */
720 else {
721 mime_parser(CM_RANGE(msg, eMesageText),
722 *imap_load_part, NULL, NULL,
723 section,
724 1
725 );
726 }
727
728 if (loading_body_now) {
729 Imap->cached_body_len = StrLength(CCC->redirect_buffer);
730 Imap->cached_body = SmashStrBuf(&CCC->redirect_buffer);
731 Imap->cached_bodymsgnum = msgnum;
732 Imap->cached_body_withbody = need_body;
733 strcpy(Imap->cached_bodypart, ChrPtr(section));
734 }
735
736 if (is_partial == 0) {
737 IAPuts("BODY[");
738 iaputs(SKEY(section));
739 IAPrintf("] {" SIZE_T_FMT "}\r\n", Imap->cached_body_len);
740 pstart = 0;
741 pbytes = Imap->cached_body_len;
742 }
743 else {
744 sscanf(ChrPtr(partial), SIZE_T_FMT "." SIZE_T_FMT, &pstart, &pbytes);
745 if (pbytes > (Imap->cached_body_len - pstart)) {
746 pbytes = Imap->cached_body_len - pstart;
747 }
748 IAPuts("BODY[");
749 iaputs(SKEY(section));
750 IAPrintf("]<" SIZE_T_FMT "> {" SIZE_T_FMT "}\r\n", pstart, pbytes);
751 }
752
753 FreeStrBuf(&partial);
754
755 /* Here we go -- output it */
756 iaputs(&Imap->cached_body[pstart], pbytes);
757
758 if (msg != NULL) {
759 CM_Free(msg);
760 }
761
762 /* Mark this message as "seen" *unless* this is a "peek" operation */
763 if (is_peek == 0) {
764 CtdlSetSeen(&msgnum, 1, 1, ctdlsetseen_seen, NULL, NULL);
765 }
766 FreeStrBuf(&section);
767}
768
769/*
770 * Called immediately before outputting a multipart bodystructure
771 */
773 char *name, char *filename, char *partnum, char *disp,
774 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
775 char *cbid, void *cbuserdata
776 ) {
777
778 IAPuts("(");
779}
780
781
782
783/*
784 * Called immediately after outputting a multipart bodystructure
785 */
787 char *name, char *filename, char *partnum, char *disp,
788 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
789 char *cbid, void *cbuserdata
790 ) {
791 long len;
792 char subtype[128];
793
794 IAPuts(" ");
795
796 /* disposition */
797 len = extract_token(subtype, cbtype, 1, '/', sizeof subtype);
798 IPutStr(subtype, len);
799
800 /* body language */
801 /* IAPuts(" NIL"); We thought we needed this at one point, but maybe we don't... */
802
803 IAPuts(")");
804}
805
806
807
808/*
809 * Output the info for a MIME part in the format required by BODYSTRUCTURE.
810 *
811 */
813 char *name, char *filename, char *partnum, char *disp,
814 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
815 char *cbid, void *cbuserdata
816 ) {
817
818 int have_cbtype = 0;
819 int have_encoding = 0;
820 int lines = 0;
821 size_t i;
822 char cbmaintype[128];
823 char cbsubtype[128];
824 long cbmaintype_len;
825 long cbsubtype_len;
826
827 if (cbtype != NULL) if (!IsEmptyStr(cbtype)) have_cbtype = 1;
828 if (have_cbtype) {
829 cbmaintype_len = extract_token(cbmaintype, cbtype, 0, '/', sizeof cbmaintype);
830 cbsubtype_len = extract_token(cbsubtype, cbtype, 1, '/', sizeof cbsubtype);
831 }
832 else {
833 strcpy(cbmaintype, "TEXT");
834 cbmaintype_len = 4;
835 strcpy(cbsubtype, "PLAIN");
836 cbsubtype_len = 5;
837 }
838
839 IAPuts("(");
840 IPutStr(cbmaintype, cbmaintype_len); /* body type */
841 IAPuts(" ");
842 IPutStr(cbsubtype, cbsubtype_len); /* body subtype */
843 IAPuts(" ");
844
845 IAPuts("("); /* begin body parameter list */
846
847 /* "NAME" must appear as the first parameter. This is not required by IMAP,
848 * but the Asterisk voicemail application blindly assumes that NAME will be in
849 * the first position. If it isn't, it rejects the message.
850 */
851 if ((name != NULL) && (!IsEmptyStr(name))) {
852 IAPuts("\"NAME\" ");
853 IPutStr(name, strlen(name));
854 IAPuts(" ");
855 }
856
857 IAPuts("\"CHARSET\" ");
858 if ((cbcharset == NULL) || (cbcharset[0] == 0)){
859 IPutStr(HKEY("US-ASCII"));
860 }
861 else {
862 IPutStr(cbcharset, strlen(cbcharset));
863 }
864 IAPuts(") "); /* end body parameter list */
865
866 IAPuts("NIL "); /* Body ID */
867 IAPuts("NIL "); /* Body description */
868
869 if ((encoding != NULL) && (encoding[0] != 0)) have_encoding = 1;
870 if (have_encoding) {
871 IPutStr(encoding, strlen(encoding));
872 }
873 else {
874 IPutStr(HKEY("7BIT"));
875 }
876 IAPuts(" ");
877
878 /* The next field is the size of the part in bytes. */
879 IAPrintf("%ld ", (long)length); /* bytes */
880
881 /* The next field is the number of lines in the part, if and only
882 * if the part is TEXT. More gratuitous complexity.
883 */
884 if (!strcasecmp(cbmaintype, "TEXT")) {
885 if (length) for (i=0; i<length; ++i) {
886 if (((char *)content)[i] == '\n') ++lines;
887 }
888 IAPrintf("%d ", lines);
889 }
890
891 /* More gratuitous complexity */
892 if ((!strcasecmp(cbmaintype, "MESSAGE"))
893 && (!strcasecmp(cbsubtype, "RFC822"))) {
894 /* FIXME: message/rfc822 also needs to output the envelope structure,
895 * body structure, and line count of the encapsulated message. Fortunately
896 * there are not yet any clients depending on this, so we can get away
897 * with not implementing it for now.
898 */
899 }
900
901 /* MD5 value of body part; we can get away with NIL'ing this */
902 IAPuts("NIL ");
903
904 /* Disposition */
905 if ((disp == NULL) || IsEmptyStr(disp)) {
906 IAPuts("NIL");
907 }
908 else {
909 IAPuts("(");
910 IPutStr(disp, strlen(disp));
911 if ((filename != NULL) && (!IsEmptyStr(filename))) {
912 IAPuts(" (\"FILENAME\" ");
913 IPutStr(filename, strlen(filename));
914 IAPuts(")");
915 }
916 IAPuts(")");
917 }
918
919 /* Body language (not defined yet) */
920 IAPuts(" NIL)");
921}
922
923
924
925/*
926 * Spew the BODYSTRUCTURE data for a message.
927 *
928 */
929void imap_fetch_bodystructure (long msgnum, const char *item,
930 struct CtdlMessage *msg) {
931 const char *rfc822 = NULL;
932 const char *rfc822_body = NULL;
933 size_t rfc822_len;
934 size_t rfc822_headers_len;
935 size_t rfc822_body_len;
936 const char *ptr = NULL;
937 char *pch;
938 char buf[SIZ];
939 int lines = 0;
940
941 /* Handle NULL message gracefully */
942 if (msg == NULL) {
943 IAPuts("BODYSTRUCTURE (\"TEXT\" \"PLAIN\" "
944 "(\"CHARSET\" \"US-ASCII\") NIL NIL "
945 "\"7BIT\" 0 0)");
946 return;
947 }
948
949 /* For non-RFC822 (ordinary Citadel) messages, this is short and
950 * sweet...
951 */
952 if (msg->cm_format_type != FMT_RFC822) {
953
954 /* *sigh* We have to RFC822-format the message just to be able
955 * to measure it. FIXME use smi cached fields if possible
956 */
957
958 CC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
960 rfc822_len = StrLength(CC->redirect_buffer);
961 rfc822 = pch = SmashStrBuf(&CC->redirect_buffer);
962
963 ptr = rfc822;
964 do {
965 ptr = cmemreadline(ptr, buf, sizeof buf);
966 ++lines;
967 if ((IsEmptyStr(buf)) && (rfc822_body == NULL)) {
968 rfc822_body = ptr;
969 }
970 } while (*ptr != 0);
971
972 rfc822_headers_len = rfc822_body - rfc822;
973 rfc822_body_len = rfc822_len - rfc822_headers_len;
974 free(pch);
975
976 IAPuts("BODYSTRUCTURE (\"TEXT\" \"PLAIN\" "
977 "(\"CHARSET\" \"US-ASCII\") NIL NIL "
978 "\"7BIT\" ");
979 IAPrintf(SIZE_T_FMT " %d)", rfc822_body_len, lines);
980
981 return;
982 }
983
984 /* For messages already stored in RFC822 format, we have to parse. */
985 IAPuts("BODYSTRUCTURE ");
986 mime_parser(CM_RANGE(msg, eMesageText),
988 *imap_fetch_bodystructure_pre, /* pre-multi */
989 *imap_fetch_bodystructure_post, /* post-multi */
990 NULL,
991 1); /* don't decode -- we want it as-is */
992}
993
994
995/*
996 * imap_do_fetch() calls imap_do_fetch_msg() to output the data of an
997 * individual message, once it has been selected for output.
998 */
1000 int i;
1001 citimap *Imap = IMAP;
1002 struct CtdlMessage *msg = NULL;
1003 int body_loaded = 0;
1004
1005 /* Don't attempt to fetch bogus messages or UID's */
1006 if (seq < 1) return;
1007 if (Imap->msgids[seq-1] < 1L) return;
1008
1009 buffer_output();
1010 IAPrintf("* %d FETCH (", seq);
1011
1012 for (i=0; i<Cmd->num_parms; ++i) {
1013
1014 /* Fetchable without going to the message store at all */
1015 if (!strcasecmp(Cmd->Params[i].Key, "UID")) {
1016 imap_fetch_uid(seq);
1017 }
1018 else if (!strcasecmp(Cmd->Params[i].Key, "FLAGS")) {
1019 imap_fetch_flags(seq-1);
1020 }
1021
1022 /* Potentially fetchable from cache, if the client requests
1023 * stuff from the same message several times in a row.
1024 */
1025 else if (!strcasecmp(Cmd->Params[i].Key, "RFC822")) {
1026 imap_fetch_rfc822(Imap->msgids[seq-1], Cmd->Params[i].Key);
1027 }
1028 else if (!strcasecmp(Cmd->Params[i].Key, "RFC822.HEADER")) {
1029 imap_fetch_rfc822(Imap->msgids[seq-1], Cmd->Params[i].Key);
1030 }
1031 else if (!strcasecmp(Cmd->Params[i].Key, "RFC822.SIZE")) {
1032 imap_fetch_rfc822(Imap->msgids[seq-1], Cmd->Params[i].Key);
1033 }
1034 else if (!strcasecmp(Cmd->Params[i].Key, "RFC822.TEXT")) {
1035 imap_fetch_rfc822(Imap->msgids[seq-1], Cmd->Params[i].Key);
1036 }
1037
1038 /* BODY fetches do their own fetching and caching too. */
1039 else if (!strncasecmp(Cmd->Params[i].Key, "BODY[", 5)) {
1040 imap_fetch_body(Imap->msgids[seq-1], Cmd->Params[i], 0);
1041 }
1042 else if (!strncasecmp(Cmd->Params[i].Key, "BODY.PEEK[", 10)) {
1043 imap_fetch_body(Imap->msgids[seq-1], Cmd->Params[i], 1);
1044 }
1045
1046 /* Otherwise, load the message into memory.
1047 */
1048 else if (!strcasecmp(Cmd->Params[i].Key, "BODYSTRUCTURE")) {
1049 if ((msg != NULL) && (!body_loaded)) {
1050 CM_Free(msg); /* need the whole thing */
1051 msg = NULL;
1052 }
1053 if (msg == NULL) {
1054 msg = CtdlFetchMessage(Imap->msgids[seq-1], 1);
1055 body_loaded = 1;
1056 }
1057 imap_fetch_bodystructure(Imap->msgids[seq-1],
1058 Cmd->Params[i].Key, msg);
1059 }
1060 else if (!strcasecmp(Cmd->Params[i].Key, "ENVELOPE")) {
1061 if (msg == NULL) {
1062 msg = CtdlFetchMessage(Imap->msgids[seq-1], 0);
1063 body_loaded = 0;
1064 }
1066 }
1067 else if (!strcasecmp(Cmd->Params[i].Key, "INTERNALDATE")) {
1068 if (msg == NULL) {
1069 msg = CtdlFetchMessage(Imap->msgids[seq-1], 0);
1070 body_loaded = 0;
1071 }
1073 }
1074
1075 if (i != Cmd->num_parms-1) IAPuts(" ");
1076 }
1077
1078 IAPuts(")\r\n");
1080 if (msg != NULL) {
1081 CM_Free(msg);
1082 }
1083}
1084
1085
1086
1087/*
1088 * imap_fetch() calls imap_do_fetch() to do its actual work, once it's
1089 * validated and boiled down the request a bit.
1090 */
1092 citimap *Imap = IMAP;
1093 int i;
1094#if 0
1095/* debug output the parsed vector */
1096 {
1097 int i;
1098 syslog(LOG_DEBUG, "imap: ----- %ld params", Cmd->num_parms);
1099
1100 for (i=0; i < Cmd->num_parms; i++) {
1101 if (Cmd->Params[i].len != strlen(Cmd->Params[i].Key))
1102 syslog(LOG_DEBUG, "imap: *********** %ld != %ld : %s",
1103 Cmd->Params[i].len,
1104 strlen(Cmd->Params[i].Key),
1105 Cmd->Params[i].Key);
1106 else
1107 syslog(LOG_DEBUG, "imap: %ld : %s",
1108 Cmd->Params[i].len,
1109 Cmd->Params[i].Key);
1110 }}
1111
1112#endif
1113
1114 if (Imap->num_msgs > 0) {
1115 for (i = 0; i < Imap->num_msgs; ++i) {
1116
1117 /* Abort the fetch loop if the session breaks.
1118 * This is important for users who keep mailboxes
1119 * that are too big *and* are too impatient to
1120 * let them finish loading. :)
1121 */
1122 if (CC->kill_me) return;
1123
1124 /* Get any message marked for fetch. */
1125 if (Imap->flags[i] & IMAP_SELECTED) {
1126 imap_do_fetch_msg(i+1, Cmd);
1127 }
1128 }
1129 }
1130}
1131
1132
1133
1134/*
1135 * Back end for imap_handle_macros()
1136 * Note that this function *only* looks at the beginning of the string. It
1137 * is not a generic search-and-replace function.
1138 */
1139void imap_macro_replace(StrBuf *Buf, long where,
1140 StrBuf *TmpBuf,
1141 char *find, long findlen,
1142 char *replace, long replacelen)
1143{
1144
1145 if (StrLength(Buf) - where > findlen)
1146 return;
1147
1148 if (!strncasecmp(ChrPtr(Buf) + where, find, findlen)) {
1149 if (ChrPtr(Buf)[where + findlen] == ' ') {
1150 StrBufPlain(TmpBuf, replace, replacelen);
1151 StrBufAppendBufPlain(TmpBuf, HKEY(" "), 0);
1152 StrBufReplaceToken(Buf, where, findlen,
1153 SKEY(TmpBuf));
1154 }
1155 if (where + findlen == StrLength(Buf)) {
1156 StrBufReplaceToken(Buf, where, findlen,
1157 replace, replacelen);
1158 }
1159 }
1160}
1161
1162
1163
1164/*
1165 * Handle macros embedded in FETCH data items.
1166 * (What the heck are macros doing in a wire protocol? Are we trying to save
1167 * the computer at the other end the trouble of typing a lot of characters?)
1168 */
1170 long i;
1171 int nest = 0;
1172 StrBuf *Tmp = NewStrBuf();
1173
1174 for (i=0; i < StrLength(Cmd->CmdBuf); ++i) {
1175 char ch = ChrPtr(Cmd->CmdBuf)[i];
1176 if ((ch=='(') ||
1177 (ch=='[') ||
1178 (ch=='<') ||
1179 (ch=='{')) ++nest;
1180 else if ((ch==')') ||
1181 (ch==']') ||
1182 (ch=='>') ||
1183 (ch=='}')) --nest;
1184
1185 if (nest <= 0) {
1186 imap_macro_replace(Cmd->CmdBuf, i,
1187 Tmp,
1188 HKEY("ALL"),
1189 HKEY("FLAGS INTERNALDATE RFC822.SIZE ENVELOPE")
1190 );
1191 imap_macro_replace(Cmd->CmdBuf, i,
1192 Tmp,
1193 HKEY("BODY"),
1194 HKEY("BODYSTRUCTURE")
1195 );
1196 imap_macro_replace(Cmd->CmdBuf, i,
1197 Tmp,
1198 HKEY("FAST"),
1199 HKEY("FLAGS INTERNALDATE RFC822.SIZE")
1200 );
1201 imap_macro_replace(Cmd->CmdBuf, i,
1202 Tmp,
1203 HKEY("FULL"),
1204 HKEY("FLAGS INTERNALDATE RFC822.SIZE ENVELOPE BODY")
1205 );
1206 }
1207 }
1208 FreeStrBuf(&Tmp);
1209}
1210
1211
1212/*
1213 * Break out the data items requested, possibly a parenthesized list.
1214 * Returns the number of data items, or -1 if the list is invalid.
1215 * NOTE: this function alters the string it is fed, and uses it as a buffer
1216 * to hold the data for the pointers it returns.
1217 */
1219{
1220 int nArgs;
1221 int nest = 0;
1222 const char *pch, *end;
1223
1224 /* Convert all whitespace to ordinary space characters. */
1225 pch = ChrPtr(Cmd->CmdBuf);
1226 end = pch + StrLength(Cmd->CmdBuf);
1227
1228 while (pch < end)
1229 {
1230 if (isspace(*pch))
1231 StrBufPeek(Cmd->CmdBuf, pch, 0, ' ');
1232 pch++;
1233 }
1234
1235 /* Strip leading and trailing whitespace, then strip leading and
1236 * trailing parentheses if it's a list
1237 */
1238 StrBufTrim(Cmd->CmdBuf);
1239 pch = ChrPtr(Cmd->CmdBuf);
1240 if ( (pch[0]=='(') &&
1241 (pch[StrLength(Cmd->CmdBuf)-1]==')') )
1242 {
1243 StrBufCutRight(Cmd->CmdBuf, 1);
1244 StrBufCutLeft(Cmd->CmdBuf, 1);
1245 StrBufTrim(Cmd->CmdBuf);
1246 }
1247
1248 /* Parse any macro data items */
1249 imap_handle_macros(Cmd);
1250
1251 /*
1252 * Now break out the data items. We throw in one trailing space in
1253 * order to avoid having to break out the last one manually.
1254 */
1255 nArgs = StrLength(Cmd->CmdBuf) / 10 + 10;
1256 nArgs = CmdAdjust(Cmd, nArgs, 0);
1257 Cmd->num_parms = 0;
1258 Cmd->Params[Cmd->num_parms].Key = pch = ChrPtr(Cmd->CmdBuf);
1259 end = Cmd->Params[Cmd->num_parms].Key + StrLength(Cmd->CmdBuf);
1260
1261 while (pch < end)
1262 {
1263 if ((*pch=='(') ||
1264 (*pch=='[') ||
1265 (*pch=='<') ||
1266 (*pch=='{'))
1267 ++nest;
1268
1269 else if ((*pch==')') ||
1270 (*pch==']') ||
1271 (*pch=='>') ||
1272 (*pch=='}'))
1273 --nest;
1274
1275 if ((nest <= 0) && (*pch==' ')) {
1276 StrBufPeek(Cmd->CmdBuf, pch, 0, '\0');
1277 Cmd->Params[Cmd->num_parms].len =
1278 pch - Cmd->Params[Cmd->num_parms].Key;
1279
1280 if (Cmd->num_parms + 1 >= Cmd->avail_parms) {
1281 nArgs = CmdAdjust(Cmd, nArgs * 2, 1);
1282 }
1283 Cmd->num_parms++;
1284 Cmd->Params[Cmd->num_parms].Key = ++pch;
1285 }
1286 else if (pch + 1 == end) {
1287 Cmd->Params[Cmd->num_parms].len =
1288 pch - Cmd->Params[Cmd->num_parms].Key + 1;
1289
1290 Cmd->num_parms++;
1291 }
1292 pch ++;
1293 }
1294 return Cmd->num_parms;
1295
1296}
1297
1298
1299/*
1300 * One particularly hideous aspect of IMAP is that we have to allow the client
1301 * to specify arbitrary ranges and/or sets of messages to fetch. Citadel IMAP
1302 * handles this by setting the IMAP_SELECTED flag for each message specified in
1303 * the ranges/sets, then looping through the message array, outputting messages
1304 * with the flag set. We don't bother returning an error if an out-of-range
1305 * number is specified (we just return quietly) because any client braindead
1306 * enough to request a bogus message number isn't going to notice the
1307 * difference anyway.
1308 *
1309 * This function clears out the IMAP_SELECTED bits, then sets that bit for each
1310 * message included in the specified range.
1311 *
1312 * Set is_uid to 1 to fetch by UID instead of sequence number.
1313 */
1314void imap_pick_range(const char *supplied_range, int is_uid) {
1315 citimap *Imap = IMAP;
1316 int i;
1317 int num_sets;
1318 int s;
1319 char setstr[SIZ], lostr[SIZ], histr[SIZ];
1320 long lo, hi;
1321 char actual_range[SIZ];
1322
1323 /*
1324 * Handle the "ALL" macro
1325 */
1326 if (!strcasecmp(supplied_range, "ALL")) {
1327 safestrncpy(actual_range, "1:*", sizeof actual_range);
1328 }
1329 else {
1330 safestrncpy(actual_range, supplied_range, sizeof actual_range);
1331 }
1332
1333 /*
1334 * Clear out the IMAP_SELECTED flags for all messages.
1335 */
1336 for (i = 0; i < Imap->num_msgs; ++i) {
1337 Imap->flags[i] = Imap->flags[i] & ~IMAP_SELECTED;
1338 }
1339
1340 /*
1341 * Now set it for all specified messages.
1342 */
1343 num_sets = num_tokens(actual_range, ',');
1344 for (s=0; s<num_sets; ++s) {
1345 extract_token(setstr, actual_range, s, ',', sizeof setstr);
1346
1347 extract_token(lostr, setstr, 0, ':', sizeof lostr);
1348 if (num_tokens(setstr, ':') >= 2) {
1349 extract_token(histr, setstr, 1, ':', sizeof histr);
1350 if (!strcmp(histr, "*")) snprintf(histr, sizeof histr, "%ld", LONG_MAX);
1351 }
1352 else {
1353 safestrncpy(histr, lostr, sizeof histr);
1354 }
1355 lo = atol(lostr);
1356 hi = atol(histr);
1357
1358 /* Loop through the array, flipping bits where appropriate */
1359 for (i = 1; i <= Imap->num_msgs; ++i) {
1360 if (is_uid) { /* fetch by sequence number */
1361 if ( (Imap->msgids[i-1]>=lo)
1362 && (Imap->msgids[i-1]<=hi)) {
1363 Imap->flags[i-1] |= IMAP_SELECTED;
1364 }
1365 }
1366 else { /* fetch by uid */
1367 if ( (i>=lo) && (i<=hi)) {
1368 Imap->flags[i-1] |= IMAP_SELECTED;
1369 }
1370 }
1371 }
1372 }
1373}
1374
1375
1376
1377/*
1378 * This function is called by the main command loop.
1379 */
1380void imap_fetch(int num_parms, ConstStr *Params) {
1381 citimap_command Cmd;
1382 int num_items;
1383
1384 if (num_parms < 4) {
1385 IReply("BAD invalid parameters");
1386 return;
1387 }
1388
1389 imap_pick_range(Params[2].Key, 0);
1390
1391 memset(&Cmd, 0, sizeof(citimap_command));
1392 Cmd.CmdBuf = NewStrBufPlain(NULL, StrLength(IMAP->Cmd.CmdBuf));
1393 MakeStringOf(Cmd.CmdBuf, 3);
1394
1395 num_items = imap_extract_data_items(&Cmd);
1396 if (num_items < 1) {
1397 IReply("BAD invalid data item list");
1398 FreeStrBuf(&Cmd.CmdBuf);
1399 free(Cmd.Params);
1400 return;
1401 }
1402
1403 imap_do_fetch(&Cmd);
1404 IReply("OK FETCH completed");
1405 FreeStrBuf(&Cmd.CmdBuf);
1406 free(Cmd.Params);
1407}
1408
1409/*
1410 * This function is called by the main command loop.
1411 */
1412void imap_uidfetch(int num_parms, ConstStr *Params) {
1413 citimap_command Cmd;
1414 int num_items;
1415 int i;
1416 int have_uid_item = 0;
1417
1418 if (num_parms < 5) {
1419 IReply("BAD invalid parameters");
1420 return;
1421 }
1422
1423 imap_pick_range(Params[3].Key, 1);
1424
1425 memset(&Cmd, 0, sizeof(citimap_command));
1426 Cmd.CmdBuf = NewStrBufPlain(NULL, StrLength(IMAP->Cmd.CmdBuf));
1427
1428 MakeStringOf(Cmd.CmdBuf, 4);
1429#if 0
1430 syslog(LOG_DEBUG, "imap: -------%s--------", ChrPtr(Cmd.CmdBuf));
1431#endif
1432 num_items = imap_extract_data_items(&Cmd);
1433 if (num_items < 1) {
1434 IReply("BAD invalid data item list");
1435 FreeStrBuf(&Cmd.CmdBuf);
1436 free(Cmd.Params);
1437 return;
1438 }
1439
1440 /* If the "UID" item was not included, we include it implicitly
1441 * (at the beginning) because this is a UID FETCH command
1442 */
1443 for (i=0; i<num_items; ++i) {
1444 if (!strcasecmp(Cmd.Params[i].Key, "UID")) ++have_uid_item;
1445 }
1446 if (have_uid_item == 0) {
1447 if (Cmd.num_parms + 1 >= Cmd.avail_parms)
1448 CmdAdjust(&Cmd, Cmd.avail_parms + 1, 1);
1449 memmove(&Cmd.Params[1],
1450 &Cmd.Params[0],
1451 sizeof(ConstStr) * Cmd.num_parms);
1452
1453 Cmd.num_parms++;
1454 Cmd.Params[0] = (ConstStr){HKEY("UID")};
1455 }
1456
1457 imap_do_fetch(&Cmd);
1458 IReply("OK UID FETCH completed");
1459 FreeStrBuf(&Cmd.CmdBuf);
1460 free(Cmd.Params);
1461}
1462
1463
#define MES_ANONONLY
Definition: citadel.h:136
#define LONG_MAX
Definition: citadel.h:167
#define MES_ANONOPT
Definition: citadel.h:137
char * CtdlGetConfigStr(char *key)
Definition: config.c:393
#define CC
Definition: context.h:140
long datestring(char *buf, size_t n, time_t xtime, int which_format)
Definition: genstamp.c:26
@ DATESTRING_IMAP
Definition: genstamp.h:6
void imap_fetch_bodystructure(long msgnum, const char *item, struct CtdlMessage *msg)
Definition: imap_fetch.c:929
void imap_fetch_internaldate(struct CtdlMessage *msg)
Definition: imap_fetch.c:93
void imap_fetch_rfc822(long msgnum, const char *whichfmt)
Definition: imap_fetch.c:119
void imap_strip_headers(StrBuf *section)
Definition: imap_fetch.c:527
void imap_fetch_bodystructure_post(char *name, char *filename, char *partnum, char *disp, void *content, char *cbtype, char *cbcharset, size_t length, char *encoding, char *cbid, void *cbuserdata)
Definition: imap_fetch.c:786
void imap_do_fetch_msg(int seq, citimap_command *Cmd)
Definition: imap_fetch.c:999
void imap_pick_range(const char *supplied_range, int is_uid)
Definition: imap_fetch.c:1314
void imap_output_envelope_from(struct CtdlMessage *msg)
Definition: imap_fetch.c:317
void imap_fetch_uid(int seq)
Definition: imap_fetch.c:56
void imap_do_fetch(citimap_command *Cmd)
Definition: imap_fetch.c:1091
void imap_load_part(char *name, char *filename, char *partnum, char *disp, void *content, char *cbtype, char *cbcharset, size_t length, char *encoding, char *cbid, void *cbuserdata)
Definition: imap_fetch.c:261
int imap_extract_data_items(citimap_command *Cmd)
Definition: imap_fetch.c:1218
void imap_fetch_bodystructure_part(char *name, char *filename, char *partnum, char *disp, void *content, char *cbtype, char *cbcharset, size_t length, char *encoding, char *cbid, void *cbuserdata)
Definition: imap_fetch.c:812
void imap_uidfetch(int num_parms, ConstStr *Params)
Definition: imap_fetch.c:1412
void imap_macro_replace(StrBuf *Buf, long where, StrBuf *TmpBuf, char *find, long findlen, char *replace, long replacelen)
Definition: imap_fetch.c:1139
void imap_fetch(int num_parms, ConstStr *Params)
Definition: imap_fetch.c:1380
void imap_output_envelope_addr(char *addr)
Definition: imap_fetch.c:359
void imap_fetch_body(long msgnum, ConstStr item, int is_peek)
Definition: imap_fetch.c:623
void imap_handle_macros(citimap_command *Cmd)
Definition: imap_fetch.c:1169
void imap_fetch_flags(int seq)
Definition: imap_fetch.c:60
void imap_fetch_envelope(struct CtdlMessage *msg)
Definition: imap_fetch.c:408
void imap_fetch_bodystructure_pre(char *name, char *filename, char *partnum, char *disp, void *content, char *cbtype, char *cbcharset, size_t length, char *encoding, char *cbid, void *cbuserdata)
Definition: imap_fetch.c:772
void iaputs(const char *Str, long Len)
Definition: imap_tools.c:908
void MakeStringOf(StrBuf *Buf, int skip)
Definition: imap_tools.c:383
int imap_parameterize(citimap_command *Cmd)
Definition: imap_tools.c:468
void IAPrintf(const char *Format,...)
Definition: imap_tools.c:898
int CmdAdjust(citimap_command *Cmd, int nArgs, int Realloc)
Definition: imap_tools.c:428
void IPutStr(const char *Msg, long Len)
Definition: imap_tools.c:955
#define IPutMsgField(Which)
Definition: imap_tools.h:54
#define IAPuts(Msg)
Definition: imap_tools.h:39
#define IReply(msg)
Definition: imap_tools.h:45
void process_rfc822_addr(const char *rfc822, char *user, char *node, char *name)
char * rfc822_fetch_field(const char *rfc822, const char *fieldname)
void CM_Free(struct CtdlMessage *msg)
Definition: msgbase.c:310
int CtdlOutputMsg(long msg_num, int mode, int headers_only, int do_proto, int crlf, char *section, int flags, char **Author, char **Address, char **MessageID)
Definition: msgbase.c:1481
void GetMetaData(struct MetaData *smibuf, long msgnum)
Definition: msgbase.c:3383
int CtdlOutputPreLoadedMsg(struct CtdlMessage *TheMessage, int mode, int headers_only, int do_proto, int crlf, int flags)
Definition: msgbase.c:1969
struct CtdlMessage * CtdlFetchMessage(long msgnum, int with_body)
Definition: msgbase.c:1135
int CM_IsEmpty(struct CtdlMessage *Msg, eMsgField which)
Definition: msgbase.c:137
void CtdlSetSeen(long *target_msgnums, int num_target_msgnums, int target_setting, int which_set, struct ctdluser *which_user, struct ctdlroom *which_room)
Definition: msgbase.c:409
void PutMetaData(struct MetaData *smibuf)
Definition: msgbase.c:3411
#define HEADERS_NONE
Definition: msgbase.h:41
#define SUPPRESS_ENV_TO
Definition: msgbase.h:163
@ ctdlsetseen_seen
Definition: msgbase.h:176
#define CM_RANGE(Message, Which)
Definition: msgbase.h:127
#define HEADERS_ALL
Definition: msgbase.h:39
#define HEADERS_FAST
Definition: msgbase.h:42
#define HEADERS_ONLY
Definition: msgbase.h:40
void * malloc(size_t)
void free(void *)
#define IMAP_SELECTED
Definition: serv_imap.h:90
#define IMAP_ANSWERED
Definition: serv_imap.h:81
#define IMAP_DELETED
Definition: serv_imap.h:83
#define IMAP_RECENT
Definition: serv_imap.h:91
#define IMAP
Definition: serv_imap.h:104
#define CCCIMAP
Definition: serv_imap.h:105
#define IMAP_SEEN
Definition: serv_imap.h:85
struct MetaData smi
Definition: serv_migrate.c:504
@ emessageId
Definition: server.h:311
@ eMesageText
Definition: server.h:315
@ erFc822Addr
Definition: server.h:310
@ eAuthor
Definition: server.h:307
@ eCarbonCopY
Definition: server.h:323
@ eTimestamp
Definition: server.h:319
@ eMsgSubject
Definition: server.h:320
@ eRecipient
Definition: server.h:318
#define FMT_RFC822
Definition: server.h:178
@ MT_RFC822
Definition: server.h:167
int snprintf(char *buf, size_t max, const char *fmt,...)
Definition: snprintf.c:69
StrBuf * redirect_buffer
Definition: context.h:62
long cm_lengths[256]
Definition: server.h:38
char cm_anon_type
Definition: server.h:35
char * cm_fields[256]
Definition: server.h:37
char cm_format_type
Definition: server.h:36
long meta_rfc822_length
Definition: server.h:271
ConstStr * Params
Definition: serv_imap.h:30
StrBuf * CmdBuf
Definition: serv_imap.h:29
char cached_bodypart[SIZ]
Definition: serv_imap.h:61
StrBuf * cached_rfc822
Definition: serv_imap.h:54
unsigned int * flags
Definition: serv_imap.h:47
int num_msgs
Definition: serv_imap.h:43
long cached_bodymsgnum
Definition: serv_imap.h:62
char * cached_body
Definition: serv_imap.h:59
char cached_body_withbody
Definition: serv_imap.h:63
size_t cached_body_len
Definition: serv_imap.h:60
long * msgids
Definition: serv_imap.h:46
long cached_rfc822_msgnum
Definition: serv_imap.h:55
char cached_rfc822_withbody
Definition: serv_imap.h:56
#define SIZ
Definition: sysconfig.h:33
void cprintf(const char *format,...)
Definition: sysdep.c:381
int client_write(const char *buf, int nbytes)
Definition: sysdep.c:307
void buffer_output(void)
Definition: sysdep.c:267
void unbuffer_output(void)
Definition: sysdep.c:276
#define SIZE_T_FMT
Definition: sysdep_decls.h:22
int is_room_aide(void)
Definition: user_ops.c:441