* Add binaries pico.exe, mailutil.exe, rpload.exe andd rpdump.exe
[alpine.git] / pith / detach.c
blob046a2de052b7b41a747e1898ea138d3d61f1a08b
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 2013-2017 Eduardo Chappa
8 * Copyright 2006-2008 University of Washington
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(int);
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 & !(flags & DT_BINARY)){
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)
213 || !strucmp(charset, "MISSING_PARAMETER_VALUE")
214 || !strucmp(charset, "us-ascii")){
215 if(charset)
216 fs_give((void **) &charset);
218 if(ps_global->VAR_UNK_CHAR_SET)
219 charset = cpystr(ps_global->VAR_UNK_CHAR_SET);
222 /* convert to UTF-8 */
223 if(!(charset && !strucmp(charset, "utf-8")))
224 gf_link_filter(gf_utf8, gf_utf8_opt(charset));
226 if(charset)
227 fs_give((void **) &charset);
231 * If we're detaching a text segment and there are user-defined
232 * filters and there are text triggers to look for, install filter
233 * to let us look at each line...
235 display_filter = NULL;
236 if(is_text
237 && ps_global->tools.display_filter
238 && ps_global->tools.display_filter_trigger
239 && ps_global->VAR_DISPLAY_FILTERS
240 && !(flags & DT_NODFILTER)){
241 /* check for "static" triggers (i.e., none or CHARSET) */
242 if(!(display_filter = (*ps_global->tools.display_filter_trigger)(body, trigger, sizeof(trigger)))
243 && (df_trigger_list = build_trigger_list())){
244 /* else look for matching text trigger */
245 gf_link_filter(gf_line_test,
246 gf_line_test_opt(df_trigger_cmp, NULL));
249 else
250 /* add aux filters if we're not going to MIME decode into a temporary
251 * storage object, otherwise we pass the aux_filters on to gf_filter
252 * below so it can pass what comes out of the external filter command
253 * thru the rest of the filters...
255 for( ; aux_filters && aux_filters->filter; aux_filters++)
256 gf_link_filter(aux_filters->filter, aux_filters->data);
259 * Following canonical model, after decoding convert newlines from
260 * crlf to local convention. ALSO, convert newlines if we're fetching
261 * a multipart segment since an external handler's going to have to
262 * make sense of it...
264 if((is_text & !(flags & DT_BINARY))
265 || body->type == TYPEMESSAGE
266 || body->type == TYPEMULTIPART)
267 gf_link_filter(gf_nvtnl_local, NULL);
270 * If we're detaching a text segment and a user-defined filter may
271 * need to be invoked later (see below), decode the segment into
272 * a temporary storage object...
274 if(is_text
275 && ps_global->tools.display_filter
276 && ps_global->tools.display_filter_trigger
277 && ps_global->VAR_DISPLAY_FILTERS
278 && !(flags & DT_NODFILTER)
279 && !(detach_so = so_get(CharStar, NULL, EDIT_ACCESS))){
280 strncpy(err_string,
281 _("Formatting error: no space to make copy, no display filters used"), sizeof(err_string));
282 err_string[sizeof(err_string)-1] = '\0';
285 if((status = gf_pipe(FETCH_READC, detach_so ? detach_writec : pc)) != NULL) {
286 snprintf(err_string, sizeof(err_string), "Formatting error: %s", status);
287 rv = 0L;
291 * If we wrote to a temporary area, there MAY be a user-defined
292 * filter to invoke. Filter it it (or not if no trigger match)
293 * *AND* send the output thru any auxiliary filters, destroy the
294 * temporary object and be done with it...
296 if(detach_so){
297 if(!err_string[0] && display_filter && *display_filter){
298 FILTLIST_S *p, *aux = NULL;
299 size_t count;
301 if(aux_filters && !(flags & DT_BINARY)){
302 /* insert NL conversion filters around remaining aux_filters
303 * so they're not tripped up by local NL convention
305 for(p = aux_filters; p->filter; p++) /* count aux_filters */
308 count = (p - aux_filters) + 3;
309 p = aux = (FILTLIST_S *) fs_get(count * sizeof(FILTLIST_S));
310 memset(p, 0, count * sizeof(FILTLIST_S));
311 p->filter = gf_local_nvtnl;
312 p++;
313 for(; aux_filters->filter; p++, aux_filters++)
314 *p = *aux_filters;
316 p->filter = gf_nvtnl_local;
319 if((status = (*ps_global->tools.display_filter)(display_filter, detach_so, pc, aux)) != NULL){
320 snprintf(err_string, sizeof(err_string), "Formatting error: %s", status);
321 rv = 0L;
324 if(aux)
325 fs_give((void **)&aux);
327 else{ /* just copy it, then */
328 gf_io_t gc;
330 gf_set_so_readc(&gc, detach_so);
331 so_seek(detach_so, 0L, 0);
332 gf_filter_init();
333 if(aux_filters){
334 /* if other filters are involved, correct for
335 * newlines on either side of the pipe...
337 gf_link_filter(gf_local_nvtnl, NULL);
338 for( ; aux_filters->filter ; aux_filters++)
339 gf_link_filter(aux_filters->filter, aux_filters->data);
341 if(!(flags & DT_BINARY))
342 gf_link_filter(gf_nvtnl_local, NULL);
345 if((status = gf_pipe(gc, pc)) != NULL){ /* Second pass, sheesh */
346 snprintf(err_string, sizeof(err_string), "Formatting error: %s", status);
347 rv = 0L;
350 gf_clear_so_readc(detach_so);
353 so_give(&detach_so); /* blast temp copy */
356 if(!ps_global->print && we_cancel)
357 cancel_busy_cue(0);
359 if (len)
360 *len = rv;
362 if(df_trigger_list)
363 blast_trigger_list(&df_trigger_list);
365 return((err_string[0] == '\0') ? NULL : err_string);
370 detach_writec(int c)
372 return(so_writec(c, detach_so));
377 * build_trigger_list - return possible triggers in a list of triggers
378 * structs
380 TRGR_S *
381 build_trigger_list(void)
383 TRGR_S *tp = NULL, **trailp;
384 char **l, *test, *str, *ep, *cmd = NULL;
385 int i;
387 trailp = &tp;
388 for(l = ps_global->VAR_DISPLAY_FILTERS ; l && *l; l++){
389 get_pair(*l, &test, &cmd, 1, 1);
390 if(test && valid_filter_command(&cmd)){
391 *trailp = (TRGR_S *) fs_get(sizeof(TRGR_S));
392 (*trailp)->cmp = df_trigger_cmp_text;
393 str = test;
394 if(*test == '_' && (i = strlen(test)) > 10
395 && *(ep = &test[i-1]) == '_' && *--ep == ')'){
396 if(struncmp(test, "_CHARSET(", 9) == 0){
397 fs_give((void **)&test);
398 fs_give((void **)&cmd);
399 fs_give((void **)trailp);
400 continue;
403 if(strncmp(test+1, "LEADING(", 8) == 0){
404 (*trailp)->cmp = df_trigger_cmp_lwsp;
405 *ep = '\0';
406 str = cpystr(test+9);
407 fs_give((void **)&test);
409 else if(strncmp(test+1, "BEGINNING(", 10) == 0){
410 (*trailp)->cmp = df_trigger_cmp_start;
411 *ep = '\0';
412 str = cpystr(test+11);
413 fs_give((void **)&test);
417 (*trailp)->text = str;
418 (*trailp)->cmd = cmd;
419 *(trailp = &(*trailp)->next) = NULL;
421 else{
422 fs_give((void **)&test);
423 fs_give((void **)&cmd);
427 return(tp);
432 * blast_trigger_list - zot any list of triggers we've been using
434 void
435 blast_trigger_list(TRGR_S **tlist)
437 if((*tlist)->next)
438 blast_trigger_list(&(*tlist)->next);
440 fs_give((void **)&(*tlist)->text);
441 fs_give((void **)&(*tlist)->cmd);
442 fs_give((void **)tlist);
447 * df_trigger_cmp - compare the line passed us with the list of defined
448 * display filter triggers
451 df_trigger_cmp(long int n, char *s, LT_INS_S **e, void *l)
453 register TRGR_S *tp;
454 int result;
456 if(!display_filter) /* already found? */
457 for(tp = df_trigger_list; tp; tp = tp->next)
458 if(tp->cmp){
459 if((result = (*tp->cmp)(s, tp->text)) < 0)
460 tp->cmp = NULL;
461 else if(result > 0)
462 return(((display_filter = tp->cmd) != NULL) ? 1 : 0);
465 return(0);
470 * df_trigger_cmp_text - return 1 if s1 is in s2
473 df_trigger_cmp_text(char *s1, char *s2)
475 return(strstr(s1, s2) != NULL);
480 * df_trigger_cmp_lwsp - compare the line passed us with the list of defined
481 * display filter triggers. returns:
483 * 0 if we don't know yet
484 * 1 if we match
485 * -1 if we clearly don't match
488 df_trigger_cmp_lwsp(char *s1, char *s2)
490 while(*s1 && isspace((unsigned char)*s1))
491 s1++;
493 return((*s1) ? (!strncmp(s1, s2, strlen(s2)) ? 1 : -1) : 0);
498 * df_trigger_cmp_start - return 1 if first strlen(s2) chars start s1
501 df_trigger_cmp_start(char *s1, char *s2)
503 return(!strncmp(s1, s2, strlen(s2)));
508 * valid_filter_command - make sure argv[0] of command really exists.
509 * "cmd" is required to be an alloc'd string since
510 * it will get realloc'd if the command's path is
511 * expanded.
514 valid_filter_command(char **cmd)
516 int i;
517 char cpath[MAXPATH+1], *p;
519 if(!(cmd && *cmd))
520 return(FALSE);
523 * copy cmd to build expanded path if necessary.
525 for(i = 0; i < sizeof(cpath) && (cpath[i] = (*cmd)[i]); i++)
526 if(isspace((unsigned char)(*cmd)[i])){
527 cpath[i] = '\0'; /* tie off command's path*/
528 break;
531 #if defined(DOS) || defined(OS2)
532 if(is_absolute_path(cpath)){
533 size_t l;
535 fixpath(cpath, sizeof(cpath));
536 l = strlen(cpath) + strlen(&(*cmd)[i]);
537 p = (char *) fs_get((l+1) * sizeof(char));
538 strncpy(p, cpath, l); /* copy new path */
539 p[l] = '\0';
540 strncat(p, &(*cmd)[i], l+1-1-strlen(p)); /* and old args */
541 p[l] = '\0';
542 fs_give((void **) cmd); /* free it */
543 *cmd = p; /* and assign new buf */
545 #else
546 if(cpath[0] == '~'){
547 if(fnexpand(cpath, sizeof(cpath))){
548 size_t l;
550 l = strlen(cpath) + strlen(&(*cmd)[i]);
551 p = (char *) fs_get((l+1) * sizeof(char));
552 strncpy(p, cpath, l); /* copy new path */
553 p[l] = '\0';
554 strncat(p, &(*cmd)[i], l+1-1-strlen(p)); /* and old args */
555 p[l] = '\0';
556 fs_give((void **) cmd); /* free it */
557 *cmd = p; /* and assign new buf */
559 else
560 return(FALSE);
562 #endif
564 return(is_absolute_path(cpath) && can_access(cpath, EXECUTE_ACCESS) == 0);
568 void
569 fetch_readc_init(FETCH_READC_S *frd, MAILSTREAM *stream, long int msgno,
570 char *section, unsigned long size, long partial, long int flags)
572 int nointr = 0;
574 nointr = flags & DT_NOINTR;
575 flags &= ~DT_NOINTR;
577 memset(g_fr_desc = frd, 0, sizeof(FETCH_READC_S));
578 frd->stream = stream;
579 frd->msgno = msgno;
580 frd->section = section;
581 frd->flags = flags;
582 frd->size = size;
583 frd->readc = fetch_readc;
585 #ifdef SMIME
587 * The call to imap_cache below will return true in the case where
588 * we've already stashed fake data in the content of the part.
589 * This happens when an S/MIME message is decrypted.
591 #endif
593 if(modern_imap_stream(stream)
594 && !imap_cache(stream, msgno, section, NULL, NULL)
595 && (size > INIT_FETCH_CHUNK || (partial > 0L && partial < size))
596 && (F_OFF(F_QUELL_PARTIAL_FETCH, ps_global)
598 #ifdef _WINDOWS
599 F_ON(F_QUELL_SSL_LARGEBLOCKS, ps_global)
600 #else
602 #endif
605 if(partial > 0L && partial < size){
606 /* partial fetch is being asked for */
607 frd->size = partial;
610 frd->allocsize = MIN(INIT_FETCH_CHUNK,frd->size);
611 frd->chunk = (char *) fs_get ((frd->allocsize + 1) * sizeof(char));
612 frd->chunksize = frd->allocsize/2; /* this gets doubled 1st time */
613 frd->endp = frd->chunk;
614 frd->free_me = 1;
616 if(!nointr)
617 if(intr_handling_on())
618 frd->we_turned_on = 1;
620 if(!(partial > 0L && partial < size)){
621 frd->cache = so_get(CharStar, NULL, EDIT_ACCESS);
622 so_truncate(frd->cache, size); /* pre-allocate */
625 else{ /* fetch the whole bloody thing here */
626 frd->chunk = mail_fetch_body(stream, msgno, section, &frd->read, flags);
628 /* This only happens if the server gave us a bogus size */
629 if(partial > 0L && partial < size){
630 /* partial fetch is being asked for */
631 frd->size = partial;
632 frd->endp = &frd->chunk[frd->size];
634 else if(size != frd->read){
635 dprint((1,
636 "fetch_readc_init: size mismatch: size=%lu read=%lu, continue...\n",
637 frd->size, frd->read));
638 q_status_message(SM_ORDER | SM_DING, 0, 3,
639 _("Message size does not match expected size, continuing..."));
640 frd->size = MIN(size, frd->read);
641 frd->endp = &frd->chunk[frd->read];
643 else
644 frd->endp = &frd->chunk[frd->read];
647 frd->chunkp = frd->chunk;
652 fetch_readc_cleanup(int store)
654 if(g_fr_desc){
655 if(g_fr_desc->we_turned_on)
656 intr_handling_off();
658 if(g_fr_desc->chunk && g_fr_desc->free_me)
659 fs_give((void **) &g_fr_desc->chunk);
661 if(g_fr_desc->cache && store){
662 SIZEDTEXT text;
664 text.size = g_fr_desc->size;
665 text.data = (unsigned char *) so_text(g_fr_desc->cache);
666 imap_cache(g_fr_desc->stream, g_fr_desc->msgno,
667 g_fr_desc->section, NULL, &text);
668 g_fr_desc->cache->txt = (void *) NULL;
669 so_give(&g_fr_desc->cache);
673 return(0);
677 char *
678 fetch_gets(readfn_t f, void *stream, long unsigned int size, GETS_DATA *md)
680 unsigned long n;
682 n = MIN(g_fr_desc->chunksize, size);
683 g_fr_desc->read += n;
684 g_fr_desc->endp = &g_fr_desc->chunk[n];
686 (*f) (stream, n, g_fr_desc->chunkp = g_fr_desc->chunk);
688 if(g_fr_desc->cache)
689 so_nputs(g_fr_desc->cache, g_fr_desc->chunk, (long) n);
691 /* BUG: need to read requested "size" in case it's larger than chunk? */
693 return(NULL);
698 fetch_readc(unsigned char *c)
700 extern void gf_error(char *);
702 if(ps_global->intr_pending){
703 (void) fetch_readc_cleanup(0);
704 /* TRANSLATORS: data transfer was interrupted by something */
705 gf_error(g_fr_desc->error ? g_fr_desc->error :_("Transfer interrupted!"));
706 /* no return */
708 else if(g_fr_desc->chunkp == g_fr_desc->endp){
710 /* Anything to read, do it */
711 if(g_fr_desc->read < g_fr_desc->size){
712 void *old_gets;
713 int rv;
714 TIMEVAL_S before, after;
715 long diff, wdiff;
716 unsigned long save_read;
718 old_gets = mail_parameters(g_fr_desc->stream, GET_GETS,
719 (void *)NULL);
720 mail_parameters(g_fr_desc->stream, SET_GETS, (void *) fetch_gets);
723 * Adjust chunksize with the goal that it will be about
724 * TARGET_INTR_TIME useconds +- 20%
725 * to finish the partial fetch. We want that time
726 * to be small so that interrupts will happen fast, but we want
727 * the chunksize large so that the whole fetch will happen
728 * fast. So it's a tradeoff between those two things.
730 * If the estimated fetchtime is getting too large, we
731 * half the chunksize. If it is small, we double
732 * the chunksize. If it is in between, we leave it. There is
733 * some risk of oscillating between two values, but who cares?
735 if(g_fr_desc->fetchtime <
736 TARGET_INTR_TIME - TARGET_INTR_TIME/5)
737 g_fr_desc->chunksize *= 2;
738 else if(g_fr_desc->fetchtime >
739 TARGET_INTR_TIME + TARGET_INTR_TIME/5)
740 g_fr_desc->chunksize /= 2;
742 g_fr_desc->chunksize = MIN(MAX_FETCH_CHUNK,
743 MAX(MIN_FETCH_CHUNK,
744 g_fr_desc->chunksize));
746 #ifdef _WINDOWS
748 * If this feature is set, limit the max size to less than
749 * 16K - 5, the magic number that avoids Microsoft's bug.
750 * Let's just go with 12K instead of 16K - 5.
752 if(F_ON(F_QUELL_SSL_LARGEBLOCKS, ps_global))
753 g_fr_desc->chunksize =
754 MIN(AVOID_MICROSOFT_SSL_CHUNKING_BUG, g_fr_desc->chunksize);
755 #endif
757 /* don't ask for more than there should be left to ask for */
758 g_fr_desc->chunksize =
759 MIN(g_fr_desc->size - g_fr_desc->read, g_fr_desc->chunksize);
762 * If chunksize grew, reallocate chunk.
764 if(g_fr_desc->chunksize > g_fr_desc->allocsize){
765 g_fr_desc->allocsize = g_fr_desc->chunksize;
766 fs_give((void **) &g_fr_desc->chunk);
767 g_fr_desc->chunk = (char *) fs_get ((g_fr_desc->allocsize + 1)
768 * sizeof(char));
769 g_fr_desc->endp = g_fr_desc->chunk;
770 g_fr_desc->chunkp = g_fr_desc->chunk;
773 save_read = g_fr_desc->read;
774 (void)get_time(&before);
776 rv = mail_partial_body(g_fr_desc->stream, g_fr_desc->msgno,
777 g_fr_desc->section, g_fr_desc->read,
778 g_fr_desc->chunksize, g_fr_desc->flags);
781 * If the amount we actually read is less than the amount we
782 * asked for we assume that is because the server gave us a
783 * bogus size when we originally asked for it.
785 if(g_fr_desc->chunksize > (g_fr_desc->read - save_read)){
786 dprint((1,
787 "partial_body returned less than asked for: asked=%lu got=%lu, continue...\n",
788 g_fr_desc->chunksize, g_fr_desc->read - save_read));
789 if(g_fr_desc->read - save_read > 0)
790 q_status_message(SM_ORDER | SM_DING, 0, 3,
791 _("Message size does not match expected size, continuing..."));
792 else{
793 rv = 0;
794 q_status_message(SM_ORDER | SM_DING, 3, 3,
795 _("Server returns zero bytes, Quell-Partial-Fetch feature may help"));
798 g_fr_desc->size = g_fr_desc->read;
801 if(get_time(&after) == 0){
802 diff = time_diff(&after, &before);
803 wdiff = MIN(TARGET_INTR_TIME + TARGET_INTR_TIME/2,
804 MAX(TARGET_INTR_TIME - TARGET_INTR_TIME/2, diff));
806 * Fetchtime is an exponentially weighted average of the number
807 * of usecs it takes to do a single fetch of whatever the
808 * current chunksize is. Since the fetch time probably isn't
809 * simply proportional to the chunksize, we don't try to
810 * calculate a chunksize by keeping track of the bytes per
811 * second. Instead, we just double or half the chunksize if
812 * we are too fast or too slow. That happens the next time
813 * through the loop a few lines up.
814 * Too settle it down a bit, Windsorize the mean.
816 g_fr_desc->fetchtime = (g_fr_desc->fetchtime == 0)
817 ? wdiff
818 : g_fr_desc->fetchtime/2 + wdiff/2;
819 dprint((8,
820 "fetch: diff=%ld wdiff=%ld fetchave=%ld prev chunksize=%ld\n",
821 diff, wdiff, g_fr_desc->fetchtime, g_fr_desc->chunksize));
823 else /* just set it so it won't affect anything */
824 g_fr_desc->fetchtime = TARGET_INTR_TIME;
826 /* UNinstall mailgets */
827 mail_parameters(g_fr_desc->stream, SET_GETS, old_gets);
829 if(!rv){
830 (void) fetch_readc_cleanup(0);
831 gf_error("Partial fetch failed!");
832 /* no return */
835 else /* clean up and return done. */
836 return(fetch_readc_cleanup(1));
839 *c = *g_fr_desc->chunkp++;
841 return(1);