* For mailing lists, Alpine adds a description of the type of link
[alpine.git] / pith / detach.c
blob112f96289ea356fe0d74e2656206098dc1da18e8
1 /*
2 * ========================================================================
3 * Copyright 2013-2022 Eduardo Chappa
4 * Copyright 2006-2008 University of Washington
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * ========================================================================
15 #include "../pith/headers.h"
16 #include "../pith/detach.h"
17 #include "../pith/state.h"
18 #include "../pith/conf.h"
19 #include "../pith/store.h"
20 #include "../pith/filter.h"
21 #include "../pith/mailview.h"
22 #include "../pith/status.h"
23 #include "../pith/addrstring.h"
24 #include "../pith/bldaddr.h"
25 #include "../pith/mimedesc.h"
26 #include "../pith/adjtime.h"
27 #include "../pith/pipe.h"
28 #include "../pith/busy.h"
29 #include "../pith/signal.h"
30 #include "../pico/osdep/filesys.h"
34 * We need to define simple functions here for the piping and
35 * temporary storage object below. We can use the filter.c functions
36 * because they're already in use for the "putchar" function passed to
37 * detach.
39 static STORE_S *detach_so = NULL;
43 * The display filter is locally global because it's set in df_trigger_cmp
44 * which sniffs at lines of the unencoded segment...
46 typedef struct _trigger {
47 int (*cmp)(char *, char *);
48 char *text;
49 char *cmd;
50 struct _trigger *next;
51 } TRGR_S;
53 static char *display_filter;
54 static TRGR_S *df_trigger_list;
56 FETCH_READC_S *g_fr_desc;
58 #define INIT_FETCH_CHUNK ((unsigned long)(8 * 1024L))
59 #define MIN_FETCH_CHUNK ((unsigned long)(4 * 1024L))
60 #define MAX_FETCH_CHUNK ((unsigned long)(256 * 1024L))
61 #define TARGET_INTR_TIME ((unsigned long)2000000L) /* two seconds */
62 #define FETCH_READC g_fr_desc->readc
66 * Internal Prototypes
69 * This function is intentionally declared without an argument type so
70 * that warnings will go away in Windows. We're using gf_io_t for both
71 * input and output functions and the arguments aren't actually the
72 * same in the two cases. We should really have a read version and
73 * a write version of gf_io_t. That's why this is like this for now.
75 int detach_writec();
76 TRGR_S *build_trigger_list(void);
77 void blast_trigger_list(TRGR_S **);
78 int df_trigger_cmp(long, char *, LT_INS_S **, void *);
79 int df_trigger_cmp_text(char *, char *);
80 int df_trigger_cmp_lwsp(char *, char *);
81 int df_trigger_cmp_start(char *, char *);
82 int fetch_readc_cleanup(int);
83 char *fetch_gets(readfn_t, void *, unsigned long, GETS_DATA *);
84 int fetch_readc(unsigned char *);
88 /*----------------------------------------------------------------------
89 detach the given raw body part; don't do any decoding
91 Args: a bunch
93 Returns: NULL on success, error message otherwise
94 ----*/
95 char *
96 detach_raw(MAILSTREAM *stream, /* c-client stream to use */
97 long int msg_no, /* message number to deal with */
98 char *part_no, /* part number of message */
99 gf_io_t pc, /* where to put it */
100 int flags)
102 FETCH_READC_S *frd = (FETCH_READC_S *)fs_get(sizeof(FETCH_READC_S));
103 char *err = NULL;
104 int column = (flags & FM_DISPLAY) ? ps_global->ttyo->screen_cols : 80;
106 memset(g_fr_desc = frd, 0, sizeof(FETCH_READC_S));
107 frd->stream = stream;
108 frd->msgno = msg_no;
109 frd->section = part_no;
110 frd->size = 0; /* wouldn't be here otherwise */
111 frd->readc = fetch_readc;
112 frd->chunk = pine_mail_fetch_text(stream, msg_no, NULL, &frd->read, 0);
113 frd->endp = &frd->chunk[frd->read];
114 frd->chunkp = frd->chunk;
116 gf_filter_init();
117 if (!(flags & FM_NOWRAP))
118 gf_link_filter(gf_wrap, gf_wrap_filter_opt(column, column, NULL, 0,
119 (flags & FM_DISPLAY)
120 ? GFW_HANDLES : 0));
121 err = gf_pipe(FETCH_READC, pc);
123 return(err);
127 /*----------------------------------------------------------------------
128 detach the given body part using the given encoding
130 Args: a bunch
132 Returns: NULL on success, error message otherwise
133 ----*/
134 char *
135 detach(MAILSTREAM *stream, /* c-client stream to use */
136 long int msg_no, /* message number to deal with */
137 char *part_no, /* part number of message */
138 long int partial, /* if >0, limit read to this many bytes */
139 long int *len, /* returns bytes read in this arg */
140 gf_io_t pc, /* where to put it */
141 FILTLIST_S *aux_filters, /* null terminated array of filts */
142 long flags)
144 unsigned long rv;
145 unsigned long size;
146 long fetch_flags;
147 int we_cancel = 0, is_text;
148 char *status, trigger[MAILTMPLEN];
149 char *charset = NULL;
150 BODY *body;
151 static char err_string[100];
152 FETCH_READC_S fetch_part;
154 err_string[0] = '\0';
156 if(!ps_global->print && !pc_is_picotext(pc))
157 we_cancel = busy_cue(NULL, NULL, 1);
159 gf_filter_init(); /* prepare for filtering! */
161 if(!(body = mail_body(stream, msg_no, (unsigned char *) part_no)))
162 return(_("Can't find body for requested message"));
164 is_text = body->type == TYPETEXT;
166 size = body->size.bytes;
167 if(partial > 0L && partial < size)
168 size = partial;
170 fetch_flags = (flags & ~DT_NODFILTER);
171 fetch_readc_init(&fetch_part, stream, msg_no, part_no, body->size.bytes, partial,
172 fetch_flags);
173 rv = size ? size : 1;
175 switch(body->encoding) { /* handle decoding */
176 case ENC7BIT:
177 case ENC8BIT:
178 case ENCBINARY:
179 break;
181 case ENCBASE64:
182 gf_link_filter(gf_b64_binary, NULL);
183 break;
185 case ENCQUOTEDPRINTABLE:
186 gf_link_filter(gf_qp_8bit, NULL);
187 break;
189 case ENCOTHER:
190 default:
191 dprint((1, "detach: unknown CTE: \"%s\" (%d)\n",
192 (body->encoding <= ENCMAX
193 && body_encodings[body->encoding])
194 ? body_encodings[body->encoding]
195 : "BEYOND-KNOWN-TYPES",
196 body->encoding));
197 break;
200 /* convert all text to UTF-8 */
201 if(is_text & !(flags & (DT_BINARY | DT_EXTERNAL))){
202 charset = parameter_val(body->parameter, "charset");
205 * If the charset is unlabeled or unknown replace it
206 * with the user's configured unknown charset.
208 if(!charset || !strucmp(charset, UNKNOWN_CHARSET)
209 || !strucmp(charset, "MISSING_PARAMETER_VALUE")
210 || !strucmp(charset, "us-ascii")){
211 if(charset)
212 fs_give((void **) &charset);
214 if(ps_global->VAR_UNK_CHAR_SET)
215 charset = cpystr(ps_global->VAR_UNK_CHAR_SET);
218 /* some messages are mislabeled as iso-8859-1 when in reality
219 * they are windows-1252, so we treat them as windows-1252
220 * from the beginning. If we did not make this change, we might
221 * see '?' characters in the message, without an apparent explanation
222 * of this fact.
224 if(charset && !strucmp(charset, "iso-8859-1")){
225 fs_give((void **) &charset);
226 charset = cpystr("windows-1252");
229 /* convert to UTF-8 */
230 if(!(charset && !strucmp(charset, "utf-8")))
231 gf_link_filter(gf_utf8, gf_utf8_opt(charset));
233 if(charset)
234 fs_give((void **) &charset);
238 * If we're detaching a text segment and there are user-defined
239 * filters and there are text triggers to look for, install filter
240 * to let us look at each line...
242 display_filter = NULL;
243 if(is_text
244 && ps_global->tools.display_filter
245 && ps_global->tools.display_filter_trigger
246 && ps_global->VAR_DISPLAY_FILTERS
247 && !(flags & DT_NODFILTER)){
248 /* check for "static" triggers (i.e., none or CHARSET) */
249 if(!(display_filter = (*ps_global->tools.display_filter_trigger)(body, trigger, sizeof(trigger)))
250 && (df_trigger_list = build_trigger_list())){
251 /* else look for matching text trigger */
252 gf_link_filter(gf_line_test,
253 gf_line_test_opt(df_trigger_cmp, NULL));
256 else
257 /* add aux filters if we're not going to MIME decode into a temporary
258 * storage object, otherwise we pass the aux_filters on to gf_filter
259 * below so it can pass what comes out of the external filter command
260 * thru the rest of the filters...
262 for( ; aux_filters && aux_filters->filter; aux_filters++)
263 gf_link_filter(aux_filters->filter, aux_filters->data);
266 * Following canonical model, after decoding convert newlines from
267 * crlf to local convention. ALSO, convert newlines if we're fetching
268 * a multipart segment since an external handler's going to have to
269 * make sense of it...
271 if((is_text & !(flags & DT_BINARY))
272 || body->type == TYPEMESSAGE
273 || body->type == TYPEMULTIPART)
274 gf_link_filter(gf_nvtnl_local, NULL);
276 if(flags & DT_EXTERNAL){
277 char i = flags & DT_ALLIMAGES ? 'i' : '\0';
278 gf_link_filter(gf_html_cid2file, (void *) &i);
282 * If we're detaching a text segment and a user-defined filter may
283 * need to be invoked later (see below), decode the segment into
284 * a temporary storage object...
286 if(is_text
287 && ps_global->tools.display_filter
288 && ps_global->tools.display_filter_trigger
289 && ps_global->VAR_DISPLAY_FILTERS
290 && !(flags & DT_NODFILTER)
291 && !(detach_so = so_get(CharStar, NULL, EDIT_ACCESS))){
292 strncpy(err_string,
293 _("Formatting error: no space to make copy, no display filters used"), sizeof(err_string));
294 err_string[sizeof(err_string)-1] = '\0';
297 if((status = gf_pipe(FETCH_READC, detach_so ? detach_writec : pc)) != NULL) {
298 snprintf(err_string, sizeof(err_string), "Formatting error: %s", status);
299 rv = 0L;
303 * If we wrote to a temporary area, there MAY be a user-defined
304 * filter to invoke. Filter it it (or not if no trigger match)
305 * *AND* send the output thru any auxiliary filters, destroy the
306 * temporary object and be done with it...
308 if(detach_so){
309 if(!err_string[0] && display_filter && *display_filter){
310 FILTLIST_S *p, *aux = NULL;
311 size_t count;
313 if(aux_filters && !(flags & DT_BINARY)){
314 /* insert NL conversion filters around remaining aux_filters
315 * so they're not tripped up by local NL convention
317 for(p = aux_filters; p->filter; p++) /* count aux_filters */
320 count = (p - aux_filters) + 3;
321 p = aux = (FILTLIST_S *) fs_get(count * sizeof(FILTLIST_S));
322 memset(p, 0, count * sizeof(FILTLIST_S));
323 p->filter = gf_local_nvtnl;
324 p++;
325 for(; aux_filters->filter; p++, aux_filters++)
326 *p = *aux_filters;
328 p->filter = gf_nvtnl_local;
331 if((status = (*ps_global->tools.display_filter)(display_filter, detach_so, pc, aux)) != NULL){
332 snprintf(err_string, sizeof(err_string), "Formatting error: %s", status);
333 rv = 0L;
336 if(aux)
337 fs_give((void **)&aux);
339 else{ /* just copy it, then */
340 gf_io_t gc;
342 gf_set_so_readc(&gc, detach_so);
343 so_seek(detach_so, 0L, 0);
344 gf_filter_init();
345 if(aux_filters){
346 /* if other filters are involved, correct for
347 * newlines on either side of the pipe...
349 gf_link_filter(gf_local_nvtnl, NULL);
350 for( ; aux_filters->filter ; aux_filters++)
351 gf_link_filter(aux_filters->filter, aux_filters->data);
353 if(!(flags & DT_BINARY))
354 gf_link_filter(gf_nvtnl_local, NULL);
357 if((status = gf_pipe(gc, pc)) != NULL){ /* Second pass, sheesh */
358 snprintf(err_string, sizeof(err_string), "Formatting error: %s", status);
359 rv = 0L;
362 gf_clear_so_readc(detach_so);
365 so_give(&detach_so); /* blast temp copy */
368 if(!ps_global->print && we_cancel)
369 cancel_busy_cue(0);
371 if (len)
372 *len = rv;
374 if(df_trigger_list)
375 blast_trigger_list(&df_trigger_list);
377 return((err_string[0] == '\0') ? NULL : err_string);
382 detach_writec(int c)
384 return(so_writec(c, detach_so));
389 * build_trigger_list - return possible triggers in a list of triggers
390 * structs
392 TRGR_S *
393 build_trigger_list(void)
395 TRGR_S *tp = NULL, **trailp;
396 char **l, *test, *str, *ep, *cmd = NULL;
397 int i;
399 trailp = &tp;
400 for(l = ps_global->VAR_DISPLAY_FILTERS ; l && *l; l++){
401 get_pair(*l, &test, &cmd, 1, 1);
402 if(test && valid_filter_command(&cmd)){
403 *trailp = (TRGR_S *) fs_get(sizeof(TRGR_S));
404 (*trailp)->cmp = df_trigger_cmp_text;
405 str = test;
406 if(*test == '_' && (i = strlen(test)) > 10
407 && *(ep = &test[i-1]) == '_' && *--ep == ')'){
408 if(struncmp(test, "_CHARSET(", 9) == 0){
409 fs_give((void **)&test);
410 fs_give((void **)&cmd);
411 fs_give((void **)trailp);
412 continue;
415 if(strncmp(test+1, "LEADING(", 8) == 0){
416 (*trailp)->cmp = df_trigger_cmp_lwsp;
417 *ep = '\0';
418 str = cpystr(test+9);
419 fs_give((void **)&test);
421 else if(strncmp(test+1, "BEGINNING(", 10) == 0){
422 (*trailp)->cmp = df_trigger_cmp_start;
423 *ep = '\0';
424 str = cpystr(test+11);
425 fs_give((void **)&test);
429 (*trailp)->text = str;
430 (*trailp)->cmd = cmd;
431 *(trailp = &(*trailp)->next) = NULL;
433 else{
434 fs_give((void **)&test);
435 fs_give((void **)&cmd);
439 return(tp);
444 * blast_trigger_list - zot any list of triggers we've been using
446 void
447 blast_trigger_list(TRGR_S **tlist)
449 if((*tlist)->next)
450 blast_trigger_list(&(*tlist)->next);
452 fs_give((void **)&(*tlist)->text);
453 fs_give((void **)&(*tlist)->cmd);
454 fs_give((void **)tlist);
459 * df_trigger_cmp - compare the line passed us with the list of defined
460 * display filter triggers
463 df_trigger_cmp(long int n, char *s, LT_INS_S **e, void *l)
465 register TRGR_S *tp;
466 int result;
468 if(!display_filter) /* already found? */
469 for(tp = df_trigger_list; tp; tp = tp->next)
470 if(tp->cmp){
471 if((result = (*tp->cmp)(s, tp->text)) < 0)
472 tp->cmp = NULL;
473 else if(result > 0)
474 return(((display_filter = tp->cmd) != NULL) ? 1 : 0);
477 return(0);
482 * df_trigger_cmp_text - return 1 if s1 is in s2
485 df_trigger_cmp_text(char *s1, char *s2)
487 return(strstr(s1, s2) != NULL);
492 * df_trigger_cmp_lwsp - compare the line passed us with the list of defined
493 * display filter triggers. returns:
495 * 0 if we don't know yet
496 * 1 if we match
497 * -1 if we clearly don't match
500 df_trigger_cmp_lwsp(char *s1, char *s2)
502 while(*s1 && isspace((unsigned char)*s1))
503 s1++;
505 return((*s1) ? (!strncmp(s1, s2, strlen(s2)) ? 1 : -1) : 0);
510 * df_trigger_cmp_start - return 1 if first strlen(s2) chars start s1
513 df_trigger_cmp_start(char *s1, char *s2)
515 return(!strncmp(s1, s2, strlen(s2)));
520 * valid_filter_command - make sure argv[0] of command really exists.
521 * "cmd" is required to be an alloc'd string since
522 * it will get realloc'd if the command's path is
523 * expanded.
526 valid_filter_command(char **cmd)
528 int i;
529 char cpath[MAXPATH+1], *p;
531 if(!(cmd && *cmd))
532 return(FALSE);
535 * copy cmd to build expanded path if necessary.
537 for(i = 0; i < sizeof(cpath) && (cpath[i] = (*cmd)[i]); i++)
538 if(isspace((unsigned char)(*cmd)[i])){
539 cpath[i] = '\0'; /* tie off command's path*/
540 break;
543 #if defined(DOS) || defined(OS2)
544 if(is_absolute_path(cpath)){
545 size_t l;
547 fixpath(cpath, sizeof(cpath));
548 l = strlen(cpath) + strlen(&(*cmd)[i]);
549 p = (char *) fs_get((l+1) * sizeof(char));
550 strncpy(p, cpath, l); /* copy new path */
551 p[l] = '\0';
552 strncat(p, &(*cmd)[i], l+1-1-strlen(p)); /* and old args */
553 p[l] = '\0';
554 fs_give((void **) cmd); /* free it */
555 *cmd = p; /* and assign new buf */
557 #else
558 if(cpath[0] == '~'){
559 if(fnexpand(cpath, sizeof(cpath))){
560 size_t l;
562 l = strlen(cpath) + strlen(&(*cmd)[i]);
563 p = (char *) fs_get((l+1) * sizeof(char));
564 strncpy(p, cpath, l); /* copy new path */
565 p[l] = '\0';
566 strncat(p, &(*cmd)[i], l+1-1-strlen(p)); /* and old args */
567 p[l] = '\0';
568 fs_give((void **) cmd); /* free it */
569 *cmd = p; /* and assign new buf */
571 else
572 return(FALSE);
574 #endif
576 return(is_absolute_path(cpath) && can_access(cpath, EXECUTE_ACCESS) == 0);
580 void
581 fetch_readc_init(FETCH_READC_S *frd, MAILSTREAM *stream, long int msgno,
582 char *section, unsigned long size, long partial, long int flags)
584 int nointr = 0;
586 nointr = flags & DT_NOINTR;
587 flags &= ~DT_NOINTR;
589 memset(g_fr_desc = frd, 0, sizeof(FETCH_READC_S));
590 frd->stream = stream;
591 frd->msgno = msgno;
592 frd->section = section;
593 frd->flags = flags;
594 frd->size = size;
595 frd->readc = fetch_readc;
597 #ifdef SMIME
599 * The call to imap_cache below will return true in the case where
600 * we've already stashed fake data in the content of the part.
601 * This happens when an S/MIME message is decrypted.
603 #endif
605 if(modern_imap_stream(stream)
606 && !imap_cache(stream, msgno, section, NULL, NULL)
607 && (size > INIT_FETCH_CHUNK || (partial > 0L && partial < size))
608 && (F_OFF(F_QUELL_PARTIAL_FETCH, ps_global)
610 #ifdef _WINDOWS
611 F_ON(F_QUELL_SSL_LARGEBLOCKS, ps_global)
612 #else
614 #endif
617 if(partial > 0L && partial < size){
618 /* partial fetch is being asked for */
619 frd->size = partial;
622 frd->allocsize = MIN(INIT_FETCH_CHUNK,frd->size);
623 frd->chunk = (char *) fs_get ((frd->allocsize + 1) * sizeof(char));
624 frd->chunksize = frd->allocsize/2; /* this gets doubled 1st time */
625 frd->endp = frd->chunk;
626 frd->free_me = 1;
628 if(!nointr)
629 if(intr_handling_on())
630 frd->we_turned_on = 1;
632 if(!(partial > 0L && partial < size)){
633 frd->cache = so_get(CharStar, NULL, EDIT_ACCESS);
634 so_truncate(frd->cache, size); /* pre-allocate */
637 else{ /* fetch the whole bloody thing here */
638 frd->chunk = mail_fetch_body(stream, msgno, section, &frd->read, flags);
640 /* This only happens if the server gave us a bogus size */
641 if(partial > 0L && partial < size){
642 /* partial fetch is being asked for */
643 frd->size = partial;
644 frd->endp = &frd->chunk[frd->size];
646 else if(size != frd->read){
647 dprint((1,
648 "fetch_readc_init: size mismatch: size=%lu read=%lu, continue...\n",
649 frd->size, frd->read));
650 q_status_message(SM_ORDER | SM_DING, 0, 3,
651 _("Message size does not match expected size, continuing..."));
652 frd->size = MIN(size, frd->read);
653 frd->endp = &frd->chunk[frd->read];
655 else
656 frd->endp = &frd->chunk[frd->read];
659 frd->chunkp = frd->chunk;
664 fetch_readc_cleanup(int store)
666 if(g_fr_desc){
667 if(g_fr_desc->we_turned_on)
668 intr_handling_off();
670 if(g_fr_desc->chunk && g_fr_desc->free_me)
671 fs_give((void **) &g_fr_desc->chunk);
673 if(g_fr_desc->cache && store){
674 SIZEDTEXT text;
676 text.size = g_fr_desc->size;
677 text.data = (unsigned char *) so_text(g_fr_desc->cache);
678 imap_cache(g_fr_desc->stream, g_fr_desc->msgno,
679 g_fr_desc->section, NULL, &text);
680 g_fr_desc->cache->txt = (void *) NULL;
681 so_give(&g_fr_desc->cache);
685 return(0);
689 char *
690 fetch_gets(readfn_t f, void *stream, long unsigned int size, GETS_DATA *md)
692 unsigned long n;
694 n = MIN(g_fr_desc->chunksize, size);
695 g_fr_desc->read += n;
696 g_fr_desc->endp = &g_fr_desc->chunk[n];
698 (*f) (stream, n, g_fr_desc->chunkp = g_fr_desc->chunk);
700 if(g_fr_desc->cache)
701 so_nputs(g_fr_desc->cache, g_fr_desc->chunk, (long) n);
703 /* BUG: need to read requested "size" in case it's larger than chunk? */
705 return(NULL);
710 fetch_readc(unsigned char *c)
712 extern void gf_error(char *);
714 if(ps_global->intr_pending){
715 (void) fetch_readc_cleanup(0);
716 /* TRANSLATORS: data transfer was interrupted by something */
717 gf_error(g_fr_desc->error ? g_fr_desc->error :_("Transfer interrupted!"));
718 /* no return */
720 else if(g_fr_desc->chunkp == g_fr_desc->endp){
722 /* Anything to read, do it */
723 if(g_fr_desc->read < g_fr_desc->size){
724 void *old_gets;
725 int rv;
726 TIMEVAL_S before, after;
727 long diff, wdiff;
728 unsigned long save_read;
730 old_gets = mail_parameters(g_fr_desc->stream, GET_GETS,
731 (void *)NULL);
732 mail_parameters(g_fr_desc->stream, SET_GETS, (void *) fetch_gets);
735 * Adjust chunksize with the goal that it will be about
736 * TARGET_INTR_TIME useconds +- 20%
737 * to finish the partial fetch. We want that time
738 * to be small so that interrupts will happen fast, but we want
739 * the chunksize large so that the whole fetch will happen
740 * fast. So it's a tradeoff between those two things.
742 * If the estimated fetchtime is getting too large, we
743 * half the chunksize. If it is small, we double
744 * the chunksize. If it is in between, we leave it. There is
745 * some risk of oscillating between two values, but who cares?
747 if(g_fr_desc->fetchtime <
748 TARGET_INTR_TIME - TARGET_INTR_TIME/5)
749 g_fr_desc->chunksize *= 2;
750 else if(g_fr_desc->fetchtime >
751 TARGET_INTR_TIME + TARGET_INTR_TIME/5)
752 g_fr_desc->chunksize /= 2;
754 g_fr_desc->chunksize = MIN(MAX_FETCH_CHUNK,
755 MAX(MIN_FETCH_CHUNK,
756 g_fr_desc->chunksize));
758 #ifdef _WINDOWS
760 * If this feature is set, limit the max size to less than
761 * 16K - 5, the magic number that avoids Microsoft's bug.
762 * Let's just go with 12K instead of 16K - 5.
764 if(F_ON(F_QUELL_SSL_LARGEBLOCKS, ps_global))
765 g_fr_desc->chunksize =
766 MIN(AVOID_MICROSOFT_SSL_CHUNKING_BUG, g_fr_desc->chunksize);
767 #endif
769 /* don't ask for more than there should be left to ask for */
770 g_fr_desc->chunksize =
771 MIN(g_fr_desc->size - g_fr_desc->read, g_fr_desc->chunksize);
774 * If chunksize grew, reallocate chunk.
776 if(g_fr_desc->chunksize > g_fr_desc->allocsize){
777 g_fr_desc->allocsize = g_fr_desc->chunksize;
778 fs_give((void **) &g_fr_desc->chunk);
779 g_fr_desc->chunk = (char *) fs_get ((g_fr_desc->allocsize + 1)
780 * sizeof(char));
781 g_fr_desc->endp = g_fr_desc->chunk;
782 g_fr_desc->chunkp = g_fr_desc->chunk;
785 save_read = g_fr_desc->read;
786 (void)get_time(&before);
788 rv = mail_partial_body(g_fr_desc->stream, g_fr_desc->msgno,
789 g_fr_desc->section, g_fr_desc->read,
790 g_fr_desc->chunksize, g_fr_desc->flags);
793 * If the amount we actually read is less than the amount we
794 * asked for we assume that is because the server gave us a
795 * bogus size when we originally asked for it.
797 if(g_fr_desc->chunksize > (g_fr_desc->read - save_read)){
798 dprint((1,
799 "partial_body returned less than asked for: asked=%lu got=%lu, continue...\n",
800 g_fr_desc->chunksize, g_fr_desc->read - save_read));
801 if(g_fr_desc->read - save_read > 0)
802 q_status_message(SM_ORDER | SM_DING, 0, 3,
803 _("Message size does not match expected size, continuing..."));
804 else{
805 rv = 0;
806 q_status_message(SM_ORDER | SM_DING, 3, 3,
807 _("Server returns zero bytes, Quell-Partial-Fetch feature may help"));
810 g_fr_desc->size = g_fr_desc->read;
813 if(get_time(&after) == 0){
814 diff = time_diff(&after, &before);
815 wdiff = MIN(TARGET_INTR_TIME + TARGET_INTR_TIME/2,
816 MAX(TARGET_INTR_TIME - TARGET_INTR_TIME/2, diff));
818 * Fetchtime is an exponentially weighted average of the number
819 * of usecs it takes to do a single fetch of whatever the
820 * current chunksize is. Since the fetch time probably isn't
821 * simply proportional to the chunksize, we don't try to
822 * calculate a chunksize by keeping track of the bytes per
823 * second. Instead, we just double or half the chunksize if
824 * we are too fast or too slow. That happens the next time
825 * through the loop a few lines up.
826 * Too settle it down a bit, Windsorize the mean.
828 g_fr_desc->fetchtime = (g_fr_desc->fetchtime == 0)
829 ? wdiff
830 : g_fr_desc->fetchtime/2 + wdiff/2;
831 dprint((8,
832 "fetch: diff=%ld wdiff=%ld fetchave=%ld prev chunksize=%ld\n",
833 diff, wdiff, g_fr_desc->fetchtime, g_fr_desc->chunksize));
835 else /* just set it so it won't affect anything */
836 g_fr_desc->fetchtime = TARGET_INTR_TIME;
838 /* UNinstall mailgets */
839 mail_parameters(g_fr_desc->stream, SET_GETS, old_gets);
841 if(!rv){
842 (void) fetch_readc_cleanup(0);
843 gf_error("Partial fetch failed!");
844 /* no return */
847 else /* clean up and return done. */
848 return(fetch_readc_cleanup(1));
851 *c = *g_fr_desc->chunkp++;
853 return(1);