* Create help for explaining how encrypted password file support
[alpine.git] / pith / detach.c
blobaebadae6d921542eb1531bb78b199a99b89fe365
1 #if !defined(lint) && !defined(DOS)
2 static char rcsid[] = "$Id: detach.c 1074 2008-06-04 00:08:43Z hubert@u.washington.edu $";
3 #endif
5 /*
6 * ========================================================================
7 * Copyright 2006-2008 University of Washington
8 * Copyright 2013-2014 Eduardo Chappa
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * ========================================================================
19 #include "../pith/headers.h"
20 #include "../pith/detach.h"
21 #include "../pith/state.h"
22 #include "../pith/conf.h"
23 #include "../pith/store.h"
24 #include "../pith/filter.h"
25 #include "../pith/mailview.h"
26 #include "../pith/status.h"
27 #include "../pith/addrstring.h"
28 #include "../pith/bldaddr.h"
29 #include "../pith/mimedesc.h"
30 #include "../pith/adjtime.h"
31 #include "../pith/pipe.h"
32 #include "../pith/busy.h"
33 #include "../pith/signal.h"
34 #include "../pico/osdep/filesys.h"
38 * We need to define simple functions here for the piping and
39 * temporary storage object below. We can use the filter.c functions
40 * because they're already in use for the "putchar" function passed to
41 * detach.
43 static STORE_S *detach_so = NULL;
47 * The display filter is locally global because it's set in df_trigger_cmp
48 * which sniffs at lines of the unencoded segment...
50 typedef struct _trigger {
51 int (*cmp)(char *, char *);
52 char *text;
53 char *cmd;
54 struct _trigger *next;
55 } TRGR_S;
57 static char *display_filter;
58 static TRGR_S *df_trigger_list;
60 FETCH_READC_S *g_fr_desc;
62 #define INIT_FETCH_CHUNK ((unsigned long)(8 * 1024L))
63 #define MIN_FETCH_CHUNK ((unsigned long)(4 * 1024L))
64 #define MAX_FETCH_CHUNK ((unsigned long)(256 * 1024L))
65 #define TARGET_INTR_TIME ((unsigned long)2000000L) /* two seconds */
66 #define FETCH_READC g_fr_desc->readc
70 * Internal Prototypes
73 * This function is intentionally declared without an argument type so
74 * that warnings will go away in Windows. We're using gf_io_t for both
75 * input and output functions and the arguments aren't actually the
76 * same in the two cases. We should really have a read version and
77 * a write version of gf_io_t. That's why this is like this for now.
79 int detach_writec();
80 TRGR_S *build_trigger_list(void);
81 void blast_trigger_list(TRGR_S **);
82 int df_trigger_cmp(long, char *, LT_INS_S **, void *);
83 int df_trigger_cmp_text(char *, char *);
84 int df_trigger_cmp_lwsp(char *, char *);
85 int df_trigger_cmp_start(char *, char *);
86 int fetch_readc_cleanup(void);
87 char *fetch_gets(readfn_t, void *, unsigned long, GETS_DATA *);
88 int fetch_readc(unsigned char *);
92 /*----------------------------------------------------------------------
93 detach the given raw body part; don't do any decoding
95 Args: a bunch
97 Returns: NULL on success, error message otherwise
98 ----*/
99 char *
100 detach_raw(MAILSTREAM *stream, /* c-client stream to use */
101 long int msg_no, /* message number to deal with */
102 char *part_no, /* part number of message */
103 gf_io_t pc, /* where to put it */
104 int flags)
106 FETCH_READC_S *frd = (FETCH_READC_S *)fs_get(sizeof(FETCH_READC_S));
107 char *err = NULL;
108 int column = (flags & FM_DISPLAY) ? ps_global->ttyo->screen_cols : 80;
110 memset(g_fr_desc = frd, 0, sizeof(FETCH_READC_S));
111 frd->stream = stream;
112 frd->msgno = msg_no;
113 frd->section = part_no;
114 frd->size = 0; /* wouldn't be here otherwise */
115 frd->readc = fetch_readc;
116 frd->chunk = pine_mail_fetch_text(stream, msg_no, NULL, &frd->read, 0);
117 frd->endp = &frd->chunk[frd->read];
118 frd->chunkp = frd->chunk;
120 gf_filter_init();
121 if (!(flags & FM_NOWRAP))
122 gf_link_filter(gf_wrap, gf_wrap_filter_opt(column, column, NULL, 0,
123 (flags & FM_DISPLAY)
124 ? GFW_HANDLES : 0));
125 err = gf_pipe(FETCH_READC, pc);
127 return(err);
131 /*----------------------------------------------------------------------
132 detach the given body part using the given encoding
134 Args: a bunch
136 Returns: NULL on success, error message otherwise
137 ----*/
138 char *
139 detach(MAILSTREAM *stream, /* c-client stream to use */
140 long int msg_no, /* message number to deal with */
141 char *part_no, /* part number of message */
142 long int partial, /* if >0, limit read to this many bytes */
143 long int *len, /* returns bytes read in this arg */
144 gf_io_t pc, /* where to put it */
145 FILTLIST_S *aux_filters, /* null terminated array of filts */
146 long flags)
148 unsigned long rv;
149 unsigned long size;
150 long fetch_flags;
151 int we_cancel = 0, is_text;
152 char *status, trigger[MAILTMPLEN];
153 char *charset = NULL;
154 BODY *body;
155 static char err_string[100];
156 FETCH_READC_S fetch_part;
158 err_string[0] = '\0';
160 if(!ps_global->print && !pc_is_picotext(pc))
161 we_cancel = busy_cue(NULL, NULL, 1);
163 gf_filter_init(); /* prepare for filtering! */
165 if(!(body = mail_body(stream, msg_no, (unsigned char *) part_no)))
166 return(_("Can't find body for requested message"));
168 is_text = body->type == TYPETEXT;
170 size = body->size.bytes;
171 if(partial > 0L && partial < size)
172 size = partial;
174 fetch_flags = (flags & ~DT_NODFILTER);
175 fetch_readc_init(&fetch_part, stream, msg_no, part_no, body->size.bytes, partial,
176 fetch_flags);
177 rv = size ? size : 1;
179 switch(body->encoding) { /* handle decoding */
180 case ENC7BIT:
181 case ENC8BIT:
182 case ENCBINARY:
183 break;
185 case ENCBASE64:
186 gf_link_filter(gf_b64_binary, NULL);
187 break;
189 case ENCQUOTEDPRINTABLE:
190 gf_link_filter(gf_qp_8bit, NULL);
191 break;
193 case ENCOTHER:
194 default:
195 dprint((1, "detach: unknown CTE: \"%s\" (%d)\n",
196 (body->encoding <= ENCMAX
197 && body_encodings[body->encoding])
198 ? body_encodings[body->encoding]
199 : "BEYOND-KNOWN-TYPES",
200 body->encoding));
201 break;
204 /* convert all text to UTF-8 */
205 if(is_text){
206 charset = parameter_val(body->parameter, "charset");
209 * If the charset is unlabeled or unknown replace it
210 * with the user's configured unknown charset.
212 if(!charset || !strucmp(charset, UNKNOWN_CHARSET) || !strucmp(charset, "us-ascii")){
213 if(charset)
214 fs_give((void **) &charset);
216 if(ps_global->VAR_UNK_CHAR_SET)
217 charset = cpystr(ps_global->VAR_UNK_CHAR_SET);
220 /* convert to UTF-8 */
221 if(!(charset && !strucmp(charset, "utf-8")))
222 gf_link_filter(gf_utf8, gf_utf8_opt(charset));
224 if(charset)
225 fs_give((void **) &charset);
229 * If we're detaching a text segment and there are user-defined
230 * filters and there are text triggers to look for, install filter
231 * to let us look at each line...
233 display_filter = NULL;
234 if(is_text
235 && ps_global->tools.display_filter
236 && ps_global->tools.display_filter_trigger
237 && ps_global->VAR_DISPLAY_FILTERS
238 && !(flags & DT_NODFILTER)){
239 /* check for "static" triggers (i.e., none or CHARSET) */
240 if(!(display_filter = (*ps_global->tools.display_filter_trigger)(body, trigger, sizeof(trigger)))
241 && (df_trigger_list = build_trigger_list())){
242 /* else look for matching text trigger */
243 gf_link_filter(gf_line_test,
244 gf_line_test_opt(df_trigger_cmp, NULL));
247 else
248 /* add aux filters if we're not going to MIME decode into a temporary
249 * storage object, otherwise we pass the aux_filters on to gf_filter
250 * below so it can pass what comes out of the external filter command
251 * thru the rest of the filters...
253 for( ; aux_filters && aux_filters->filter; aux_filters++)
254 gf_link_filter(aux_filters->filter, aux_filters->data);
257 * Following canonical model, after decoding convert newlines from
258 * crlf to local convention. ALSO, convert newlines if we're fetching
259 * a multipart segment since an external handler's going to have to
260 * make sense of it...
262 if(is_text || body->type == TYPEMESSAGE || body->type == TYPEMULTIPART)
263 gf_link_filter(gf_nvtnl_local, NULL);
266 * If we're detaching a text segment and a user-defined filter may
267 * need to be invoked later (see below), decode the segment into
268 * a temporary storage object...
270 if(is_text
271 && ps_global->tools.display_filter
272 && ps_global->tools.display_filter_trigger
273 && ps_global->VAR_DISPLAY_FILTERS
274 && !(flags & DT_NODFILTER)
275 && !(detach_so = so_get(CharStar, NULL, EDIT_ACCESS))){
276 strncpy(err_string,
277 _("Formatting error: no space to make copy, no display filters used"), sizeof(err_string));
278 err_string[sizeof(err_string)-1] = '\0';
281 if((status = gf_pipe(FETCH_READC, detach_so ? detach_writec : pc)) != NULL) {
282 snprintf(err_string, sizeof(err_string), "Formatting error: %s", status);
283 rv = 0L;
287 * If we wrote to a temporary area, there MAY be a user-defined
288 * filter to invoke. Filter it it (or not if no trigger match)
289 * *AND* send the output thru any auxiliary filters, destroy the
290 * temporary object and be done with it...
292 if(detach_so){
293 if(!err_string[0] && display_filter && *display_filter){
294 FILTLIST_S *p, *aux = NULL;
295 size_t count;
297 if(aux_filters){
298 /* insert NL conversion filters around remaining aux_filters
299 * so they're not tripped up by local NL convention
301 for(p = aux_filters; p->filter; p++) /* count aux_filters */
304 count = (p - aux_filters) + 3;
305 p = aux = (FILTLIST_S *) fs_get(count * sizeof(FILTLIST_S));
306 memset(p, 0, count * sizeof(FILTLIST_S));
307 p->filter = gf_local_nvtnl;
308 p++;
309 for(; aux_filters->filter; p++, aux_filters++)
310 *p = *aux_filters;
312 p->filter = gf_nvtnl_local;
315 if((status = (*ps_global->tools.display_filter)(display_filter, detach_so, pc, aux)) != NULL){
316 snprintf(err_string, sizeof(err_string), "Formatting error: %s", status);
317 rv = 0L;
320 if(aux)
321 fs_give((void **)&aux);
323 else{ /* just copy it, then */
324 gf_io_t gc;
326 gf_set_so_readc(&gc, detach_so);
327 so_seek(detach_so, 0L, 0);
328 gf_filter_init();
329 if(aux_filters){
330 /* if other filters are involved, correct for
331 * newlines on either side of the pipe...
333 gf_link_filter(gf_local_nvtnl, NULL);
334 for( ; aux_filters->filter ; aux_filters++)
335 gf_link_filter(aux_filters->filter, aux_filters->data);
337 gf_link_filter(gf_nvtnl_local, NULL);
340 if((status = gf_pipe(gc, pc)) != NULL){ /* Second pass, sheesh */
341 snprintf(err_string, sizeof(err_string), "Formatting error: %s", status);
342 rv = 0L;
345 gf_clear_so_readc(detach_so);
348 so_give(&detach_so); /* blast temp copy */
351 if(!ps_global->print && we_cancel)
352 cancel_busy_cue(0);
354 if (len)
355 *len = rv;
357 if(df_trigger_list)
358 blast_trigger_list(&df_trigger_list);
360 return((err_string[0] == '\0') ? NULL : err_string);
365 detach_writec(int c)
367 return(so_writec(c, detach_so));
372 * build_trigger_list - return possible triggers in a list of triggers
373 * structs
375 TRGR_S *
376 build_trigger_list(void)
378 TRGR_S *tp = NULL, **trailp;
379 char **l, *test, *str, *ep, *cmd = NULL;
380 int i;
382 trailp = &tp;
383 for(l = ps_global->VAR_DISPLAY_FILTERS ; l && *l; l++){
384 get_pair(*l, &test, &cmd, 1, 1);
385 if(test && valid_filter_command(&cmd)){
386 *trailp = (TRGR_S *) fs_get(sizeof(TRGR_S));
387 (*trailp)->cmp = df_trigger_cmp_text;
388 str = test;
389 if(*test == '_' && (i = strlen(test)) > 10
390 && *(ep = &test[i-1]) == '_' && *--ep == ')'){
391 if(struncmp(test, "_CHARSET(", 9) == 0){
392 fs_give((void **)&test);
393 fs_give((void **)&cmd);
394 fs_give((void **)trailp);
395 continue;
398 if(strncmp(test+1, "LEADING(", 8) == 0){
399 (*trailp)->cmp = df_trigger_cmp_lwsp;
400 *ep = '\0';
401 str = cpystr(test+9);
402 fs_give((void **)&test);
404 else if(strncmp(test+1, "BEGINNING(", 10) == 0){
405 (*trailp)->cmp = df_trigger_cmp_start;
406 *ep = '\0';
407 str = cpystr(test+11);
408 fs_give((void **)&test);
412 (*trailp)->text = str;
413 (*trailp)->cmd = cmd;
414 *(trailp = &(*trailp)->next) = NULL;
416 else{
417 fs_give((void **)&test);
418 fs_give((void **)&cmd);
422 return(tp);
427 * blast_trigger_list - zot any list of triggers we've been using
429 void
430 blast_trigger_list(TRGR_S **tlist)
432 if((*tlist)->next)
433 blast_trigger_list(&(*tlist)->next);
435 fs_give((void **)&(*tlist)->text);
436 fs_give((void **)&(*tlist)->cmd);
437 fs_give((void **)tlist);
442 * df_trigger_cmp - compare the line passed us with the list of defined
443 * display filter triggers
446 df_trigger_cmp(long int n, char *s, LT_INS_S **e, void *l)
448 register TRGR_S *tp;
449 int result;
451 if(!display_filter) /* already found? */
452 for(tp = df_trigger_list; tp; tp = tp->next)
453 if(tp->cmp){
454 if((result = (*tp->cmp)(s, tp->text)) < 0)
455 tp->cmp = NULL;
456 else if(result > 0)
457 return(((display_filter = tp->cmd) != NULL) ? 1 : 0);
460 return(0);
465 * df_trigger_cmp_text - return 1 if s1 is in s2
468 df_trigger_cmp_text(char *s1, char *s2)
470 return(strstr(s1, s2) != NULL);
475 * df_trigger_cmp_lwsp - compare the line passed us with the list of defined
476 * display filter triggers. returns:
478 * 0 if we don't know yet
479 * 1 if we match
480 * -1 if we clearly don't match
483 df_trigger_cmp_lwsp(char *s1, char *s2)
485 while(*s1 && isspace((unsigned char)*s1))
486 s1++;
488 return((*s1) ? (!strncmp(s1, s2, strlen(s2)) ? 1 : -1) : 0);
493 * df_trigger_cmp_start - return 1 if first strlen(s2) chars start s1
496 df_trigger_cmp_start(char *s1, char *s2)
498 return(!strncmp(s1, s2, strlen(s2)));
503 * valid_filter_command - make sure argv[0] of command really exists.
504 * "cmd" is required to be an alloc'd string since
505 * it will get realloc'd if the command's path is
506 * expanded.
509 valid_filter_command(char **cmd)
511 int i;
512 char cpath[MAXPATH+1], *p;
514 if(!(cmd && *cmd))
515 return(FALSE);
518 * copy cmd to build expanded path if necessary.
520 for(i = 0; i < sizeof(cpath) && (cpath[i] = (*cmd)[i]); i++)
521 if(isspace((unsigned char)(*cmd)[i])){
522 cpath[i] = '\0'; /* tie off command's path*/
523 break;
526 #if defined(DOS) || defined(OS2)
527 if(is_absolute_path(cpath)){
528 size_t l;
530 fixpath(cpath, sizeof(cpath));
531 l = strlen(cpath) + strlen(&(*cmd)[i]);
532 p = (char *) fs_get((l+1) * sizeof(char));
533 strncpy(p, cpath, l); /* copy new path */
534 p[l] = '\0';
535 strncat(p, &(*cmd)[i], l+1-1-strlen(p)); /* and old args */
536 p[l] = '\0';
537 fs_give((void **) cmd); /* free it */
538 *cmd = p; /* and assign new buf */
540 #else
541 if(cpath[0] == '~'){
542 if(fnexpand(cpath, sizeof(cpath))){
543 size_t l;
545 l = strlen(cpath) + strlen(&(*cmd)[i]);
546 p = (char *) fs_get((l+1) * sizeof(char));
547 strncpy(p, cpath, l); /* copy new path */
548 p[l] = '\0';
549 strncat(p, &(*cmd)[i], l+1-1-strlen(p)); /* and old args */
550 p[l] = '\0';
551 fs_give((void **) cmd); /* free it */
552 *cmd = p; /* and assign new buf */
554 else
555 return(FALSE);
557 #endif
559 return(is_absolute_path(cpath) && can_access(cpath, EXECUTE_ACCESS) == 0);
563 void
564 fetch_readc_init(FETCH_READC_S *frd, MAILSTREAM *stream, long int msgno,
565 char *section, unsigned long size, long partial, long int flags)
567 int nointr = 0;
569 nointr = flags & DT_NOINTR;
570 flags &= ~DT_NOINTR;
572 memset(g_fr_desc = frd, 0, sizeof(FETCH_READC_S));
573 frd->stream = stream;
574 frd->msgno = msgno;
575 frd->section = section;
576 frd->flags = flags;
577 frd->size = size;
578 frd->readc = fetch_readc;
580 #ifdef SMIME
582 * The call to imap_cache below will return true in the case where
583 * we've already stashed fake data in the content of the part.
584 * This happens when an S/MIME message is decrypted.
586 #endif
588 if(modern_imap_stream(stream)
589 && !imap_cache(stream, msgno, section, NULL, NULL)
590 && (size > INIT_FETCH_CHUNK || (partial > 0L && partial < size))
591 && (F_OFF(F_QUELL_PARTIAL_FETCH, ps_global)
593 #ifdef _WINDOWS
594 F_ON(F_QUELL_SSL_LARGEBLOCKS, ps_global)
595 #else
597 #endif
600 if(partial > 0L && partial < size){
601 /* partial fetch is being asked for */
602 frd->size = partial;
605 frd->allocsize = MIN(INIT_FETCH_CHUNK,frd->size);
606 frd->chunk = (char *) fs_get ((frd->allocsize + 1) * sizeof(char));
607 frd->chunksize = frd->allocsize/2; /* this gets doubled 1st time */
608 frd->endp = frd->chunk;
609 frd->free_me = 1;
611 if(!nointr)
612 if(intr_handling_on())
613 frd->we_turned_on = 1;
615 if(!(partial > 0L && partial < size)){
616 frd->cache = so_get(CharStar, NULL, EDIT_ACCESS);
617 so_truncate(frd->cache, size); /* pre-allocate */
620 else{ /* fetch the whole bloody thing here */
621 frd->chunk = mail_fetch_body(stream, msgno, section, &frd->read, flags);
623 /* This only happens if the server gave us a bogus size */
624 if(partial > 0L && partial < size){
625 /* partial fetch is being asked for */
626 frd->size = partial;
627 frd->endp = &frd->chunk[frd->size];
629 else if(size != frd->read){
630 dprint((1,
631 "fetch_readc_init: size mismatch: size=%lu read=%lu, continue...\n",
632 frd->size, frd->read));
633 q_status_message(SM_ORDER | SM_DING, 0, 3,
634 _("Message size does not match expected size, continuing..."));
635 frd->size = MIN(size, frd->read);
636 frd->endp = &frd->chunk[frd->read];
638 else
639 frd->endp = &frd->chunk[frd->read];
642 frd->chunkp = frd->chunk;
647 fetch_readc_cleanup(void)
649 if(g_fr_desc){
650 if(g_fr_desc->we_turned_on)
651 intr_handling_off();
653 if(g_fr_desc->chunk && g_fr_desc->free_me)
654 fs_give((void **) &g_fr_desc->chunk);
656 if(g_fr_desc->cache){
657 SIZEDTEXT text;
659 text.size = g_fr_desc->size;
660 text.data = (unsigned char *) so_text(g_fr_desc->cache);
661 imap_cache(g_fr_desc->stream, g_fr_desc->msgno,
662 g_fr_desc->section, NULL, &text);
663 g_fr_desc->cache->txt = (void *) NULL;
664 so_give(&g_fr_desc->cache);
668 return(0);
672 char *
673 fetch_gets(readfn_t f, void *stream, long unsigned int size, GETS_DATA *md)
675 unsigned long n;
677 n = MIN(g_fr_desc->chunksize, size);
678 g_fr_desc->read += n;
679 g_fr_desc->endp = &g_fr_desc->chunk[n];
681 (*f) (stream, n, g_fr_desc->chunkp = g_fr_desc->chunk);
683 if(g_fr_desc->cache)
684 so_nputs(g_fr_desc->cache, g_fr_desc->chunk, (long) n);
686 /* BUG: need to read requested "size" in case it's larger than chunk? */
688 return(NULL);
693 fetch_readc(unsigned char *c)
695 extern void gf_error(char *);
697 if(ps_global->intr_pending){
698 (void) fetch_readc_cleanup();
699 /* TRANSLATORS: data transfer was interrupted by something */
700 gf_error(g_fr_desc->error ? g_fr_desc->error :_("Transfer interrupted!"));
701 /* no return */
703 else if(g_fr_desc->chunkp == g_fr_desc->endp){
705 /* Anything to read, do it */
706 if(g_fr_desc->read < g_fr_desc->size){
707 void *old_gets;
708 int rv;
709 TIMEVAL_S before, after;
710 long diff, wdiff;
711 unsigned long save_read;
713 old_gets = mail_parameters(g_fr_desc->stream, GET_GETS,
714 (void *)NULL);
715 mail_parameters(g_fr_desc->stream, SET_GETS, (void *) fetch_gets);
718 * Adjust chunksize with the goal that it will be about
719 * TARGET_INTR_TIME useconds +- 20%
720 * to finish the partial fetch. We want that time
721 * to be small so that interrupts will happen fast, but we want
722 * the chunksize large so that the whole fetch will happen
723 * fast. So it's a tradeoff between those two things.
725 * If the estimated fetchtime is getting too large, we
726 * half the chunksize. If it is small, we double
727 * the chunksize. If it is in between, we leave it. There is
728 * some risk of oscillating between two values, but who cares?
730 if(g_fr_desc->fetchtime <
731 TARGET_INTR_TIME - TARGET_INTR_TIME/5)
732 g_fr_desc->chunksize *= 2;
733 else if(g_fr_desc->fetchtime >
734 TARGET_INTR_TIME + TARGET_INTR_TIME/5)
735 g_fr_desc->chunksize /= 2;
737 g_fr_desc->chunksize = MIN(MAX_FETCH_CHUNK,
738 MAX(MIN_FETCH_CHUNK,
739 g_fr_desc->chunksize));
741 #ifdef _WINDOWS
743 * If this feature is set, limit the max size to less than
744 * 16K - 5, the magic number that avoids Microsoft's bug.
745 * Let's just go with 12K instead of 16K - 5.
747 if(F_ON(F_QUELL_SSL_LARGEBLOCKS, ps_global))
748 g_fr_desc->chunksize =
749 MIN(AVOID_MICROSOFT_SSL_CHUNKING_BUG, g_fr_desc->chunksize);
750 #endif
752 /* don't ask for more than there should be left to ask for */
753 g_fr_desc->chunksize =
754 MIN(g_fr_desc->size - g_fr_desc->read, g_fr_desc->chunksize);
757 * If chunksize grew, reallocate chunk.
759 if(g_fr_desc->chunksize > g_fr_desc->allocsize){
760 g_fr_desc->allocsize = g_fr_desc->chunksize;
761 fs_give((void **) &g_fr_desc->chunk);
762 g_fr_desc->chunk = (char *) fs_get ((g_fr_desc->allocsize + 1)
763 * sizeof(char));
764 g_fr_desc->endp = g_fr_desc->chunk;
765 g_fr_desc->chunkp = g_fr_desc->chunk;
768 save_read = g_fr_desc->read;
769 (void)get_time(&before);
771 rv = mail_partial_body(g_fr_desc->stream, g_fr_desc->msgno,
772 g_fr_desc->section, g_fr_desc->read,
773 g_fr_desc->chunksize, g_fr_desc->flags);
776 * If the amount we actually read is less than the amount we
777 * asked for we assume that is because the server gave us a
778 * bogus size when we originally asked for it.
780 if(g_fr_desc->chunksize > (g_fr_desc->read - save_read)){
781 dprint((1,
782 "partial_body returned less than asked for: asked=%lu got=%lu, continue...\n",
783 g_fr_desc->chunksize, g_fr_desc->read - save_read));
784 if(g_fr_desc->read - save_read > 0)
785 q_status_message(SM_ORDER | SM_DING, 0, 3,
786 _("Message size does not match expected size, continuing..."));
787 else{
788 rv = 0;
789 q_status_message(SM_ORDER | SM_DING, 3, 3,
790 _("Server returns zero bytes, Quell-Partial-Fetch feature may help"));
793 g_fr_desc->size = g_fr_desc->read;
796 if(get_time(&after) == 0){
797 diff = time_diff(&after, &before);
798 wdiff = MIN(TARGET_INTR_TIME + TARGET_INTR_TIME/2,
799 MAX(TARGET_INTR_TIME - TARGET_INTR_TIME/2, diff));
801 * Fetchtime is an exponentially weighted average of the number
802 * of usecs it takes to do a single fetch of whatever the
803 * current chunksize is. Since the fetch time probably isn't
804 * simply proportional to the chunksize, we don't try to
805 * calculate a chunksize by keeping track of the bytes per
806 * second. Instead, we just double or half the chunksize if
807 * we are too fast or too slow. That happens the next time
808 * through the loop a few lines up.
809 * Too settle it down a bit, Windsorize the mean.
811 g_fr_desc->fetchtime = (g_fr_desc->fetchtime == 0)
812 ? wdiff
813 : g_fr_desc->fetchtime/2 + wdiff/2;
814 dprint((8,
815 "fetch: diff=%ld wdiff=%ld fetchave=%ld prev chunksize=%ld\n",
816 diff, wdiff, g_fr_desc->fetchtime, g_fr_desc->chunksize));
818 else /* just set it so it won't affect anything */
819 g_fr_desc->fetchtime = TARGET_INTR_TIME;
821 /* UNinstall mailgets */
822 mail_parameters(g_fr_desc->stream, SET_GETS, old_gets);
824 if(!rv){
825 (void) fetch_readc_cleanup();
826 gf_error("Partial fetch failed!");
827 /* no return */
830 else /* clean up and return done. */
831 return(fetch_readc_cleanup());
834 *c = *g_fr_desc->chunkp++;
836 return(1);