1 #if !defined(lint) && !defined(DOS)
2 static char rcsid
[] = "$Id: stream.c 1012 2008-03-26 00:44:22Z hubert@u.washington.edu $";
6 * ========================================================================
7 * Copyright 2006-2008 University of Washington
8 * Copyright 2013-2015 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 /*======================================================================
21 Implements the Pine mail stream management routines
22 and c-client wrapper functions
26 #include "../pith/headers.h"
27 #include "../pith/stream.h"
28 #include "../pith/state.h"
29 #include "../pith/conf.h"
30 #include "../pith/flag.h"
31 #include "../pith/msgno.h"
32 #include "../pith/adrbklib.h"
33 #include "../pith/status.h"
34 #include "../pith/newmail.h"
35 #include "../pith/detach.h"
36 #include "../pith/folder.h"
37 #include "../pith/mailcmd.h"
38 #include "../pith/util.h"
39 #include "../pith/news.h"
40 #include "../pith/sequence.h"
41 #include "../pith/options.h"
42 #include "../pith/mimedesc.h"
45 void (*pith_opt_closing_stream
)(MAILSTREAM
*);
51 void reset_stream_view_state(MAILSTREAM
*);
52 void carefully_reset_sp_flags(MAILSTREAM
*, unsigned long);
53 char *partial_text_gets(readfn_t
, void *, unsigned long, GETS_DATA
*);
54 void mail_list_internal(MAILSTREAM
*, char *, char *);
55 int recent_activity(MAILSTREAM
*);
56 int hibit_in_searchpgm(SEARCHPGM
*);
57 int hibit_in_strlist(STRINGLIST
*);
58 int hibit_in_header(SEARCHHEADER
*);
59 int hibit_in_sizedtext(SIZEDTEXT
*);
60 int sp_nusepool_notperm(void);
61 int sp_add(MAILSTREAM
*, int);
62 void sp_delete(MAILSTREAM
*);
63 void sp_free(PER_STREAM_S
**);
66 static FETCH_READC_S
*g_pft_desc
;
69 MAILSTATUS
*pine_cached_status
; /* implement status for #move folder */
74 * Pine wrapper around mail_open. If we have the PREFER_ALT_AUTH flag turned
75 * on, we need to set the TRYALT flag before trying the open.
76 * This routine manages the stream pool, too. It tries to re-use existing
77 * streams instead of opening new ones, or maybe it will leave one open and
78 * use a new one if that seems to make more sense. Pine_mail_close leaves
79 * streams open so that they may be re-used. Each pine_mail_open should have
80 * a matching pine_mail_close (or possible pine_mail_actually_close) somewhere
84 * stream -- A possible stream for recycling. This isn't usually the
85 * way recycling happens. Usually it is automatic.
86 * mailbox -- The mailbox to be opened.
87 * openflags -- Flags passed here to modify the behavior.
88 * retflags -- Flags returned from here. SP_MATCH will be lit if that is
89 * what happened. If SP_MATCH is lit then SP_LOCKED may also
90 * be lit if the matched stream was already locked when
94 pine_mail_open(MAILSTREAM
*stream
, char *mailbox
, long int openflags
, long int *retflags
)
96 MAILSTREAM
*retstream
= NULL
;
98 int permlocked
= 0, is_inbox
= 0, usepool
= 0, tempuse
= 0, uf
= 0;
100 char **lock_these
, *target
= NULL
;
101 static unsigned long streamcounter
= 0;
104 "pine_mail_open: opening \"%s\"%s openflags=0x%x %s%s%s%s%s%s%s%s%s (%s)\n",
105 mailbox
? mailbox
: "(NULL)",
106 stream
? "" : " (stream was NULL)",
108 openflags
& OP_HALFOPEN
? " OP_HALFOPEN" : "",
109 openflags
& OP_READONLY
? " OP_READONLY" : "",
110 openflags
& OP_SILENT
? " OP_SILENT" : "",
111 openflags
& OP_DEBUG
? " OP_DEBUG" : "",
112 openflags
& SP_PERMLOCKED
? " SP_PERMLOCKED" : "",
113 openflags
& SP_INBOX
? " SP_INBOX" : "",
114 openflags
& SP_USERFLDR
? " SP_USERFLDR" : "",
115 openflags
& SP_USEPOOL
? " SP_USEPOOL" : "",
116 openflags
& SP_TEMPUSE
? " SP_TEMPUSE" : "",
117 debug_time(1, ps_global
->debug_timestamp
)));
122 if(ps_global
->user_says_cancel
){
123 dprint((7, "pine_mail_open: cancelled by user\n"));
127 is_inbox
= openflags
& SP_INBOX
;
128 uf
= openflags
& SP_USERFLDR
;
130 /* inbox is still special, assume that we want to permlock it */
131 permlocked
= (is_inbox
|| openflags
& SP_PERMLOCKED
) ? 1 : 0;
133 /* check to see if user wants this folder permlocked */
134 for(lock_these
= ps_global
->VAR_PERMLOCKED
;
135 uf
&& !permlocked
&& lock_these
&& *lock_these
; lock_these
++){
136 char *p
= NULL
, *dummy
= NULL
, *lt
, *lname
, *mname
;
137 char tmp1
[MAILTMPLEN
], tmp2
[MAILTMPLEN
];
139 /* there isn't really a pair, it just dequotes for us */
140 get_pair(*lock_these
, &dummy
, &p
, 0, 0);
143 * Check to see if this is an incoming nickname and replace it
144 * with the full name.
146 if(!(p
&& ps_global
->context_list
147 && ps_global
->context_list
->use
& CNTXT_INCMNG
148 && (lt
=folder_is_nick(p
, FOLDERS(ps_global
->context_list
), 0))))
152 fs_give((void **) &dummy
);
155 && (same_remote_mailboxes(mailbox
, lt
)
158 && (lname
=mailboxfile(tmp1
, lt
))
159 && (mname
=mailboxfile(tmp2
, mailbox
))
160 && !strcmp(lname
, mname
))))
164 fs_give((void **) &p
);
168 * Only cache if remote, not nntp, not pop, and caller asked us to.
169 * It might make sense to do some caching for nntp and pop, as well, but
170 * we aren't doing it right now. For example, an nntp stream open to
171 * one group could be reused for another group. An open pop stream could
172 * be used for mail_copy_full.
174 * An implication of doing only imap here is that sp_stream_get will only
175 * be concerned with imap streams.
177 if((d
= mail_valid (NIL
, mailbox
, (char *) NIL
)) && !strcmp(d
->name
, "imap")){
178 usepool
= openflags
& SP_USEPOOL
;
179 tempuse
= openflags
& SP_TEMPUSE
;
182 if(IS_REMOTE(mailbox
)){
183 dprint((9, "pine_mail_open: not cacheable: %s\n", !d
? "no driver?" : d
->name
? d
->name
: "?" ));
186 if(permlocked
|| (openflags
& OP_READONLY
)){
188 * This is a strange case. We want to allow stay-open local
189 * folders, but they don't fit into the rest of the framework
190 * well. So we'll look for it being already open in this case
191 * and special-case it (the already_open_stream() case
195 "pine_mail_open: not cacheable: not remote, but check for local stream\n"));
199 "pine_mail_open: not cacheable: not remote\n"));
204 /* If driver doesn't support halfopen, just open it. */
205 if(d
&& (openflags
& OP_HALFOPEN
) && !(d
->flags
& DR_HALFOPEN
)){
206 openflags
&= ~OP_HALFOPEN
;
208 "pine_mail_open: turning off OP_HALFOPEN flag\n"));
212 * Some of the flags are pine's, the rest are meant for mail_open.
213 * We've noted the pine flags, now remove them before we call mail_open.
215 openflags
&= ~(SP_USEPOOL
| SP_TEMPUSE
| SP_INBOX
216 | SP_PERMLOCKED
| SP_USERFLDR
);
219 if(ps_global
->debug_imap
> 3 || ps_global
->debugmem
)
220 openflags
|= OP_DEBUG
;
223 if(F_ON(F_PREFER_ALT_AUTH
, ps_global
)){
224 if((d
= mail_valid (NIL
, mailbox
, (char *) NIL
))
225 && !strcmp(d
->name
, "imap"))
226 openflags
|= OP_TRYALT
;
229 if(F_ON(F_ENABLE_MULNEWSRCS
, ps_global
)){
230 char source
[MAILTMPLEN
];
231 if(check_for_move_mbox(mailbox
, source
, sizeof(source
), &target
)){
233 if((d
= mail_valid(NIL
, source
, (char *) NIL
))
234 && (!strcmp(d
->name
, "news")
235 || !strcmp(d
->name
, "nntp")))
236 openflags
|= OP_MULNEWSRC
;
238 else if((d
= mail_valid(NIL
, mailbox
, (char *) NIL
))
239 && !strcmp(d
->name
, "nntp"))
240 openflags
|= OP_MULNEWSRC
;
244 * One of the problems is that the new-style stream caching (the
245 * sp_stream_get stuff) may conflict with some of the old-style caching
246 * (the passed in argument stream) that is still in the code. We should
247 * probably eliminate the old-style caching, but some of it is still useful,
248 * especially if it deals with something other than IMAP. We want to prevent
249 * mistakes caused by conflicts between the two styles. In particular, we
250 * don't want to have a new-style cached stream re-opened because of the
251 * old-style caching code. This can happen if a stream is passed in that
252 * is not useable, and then a new stream is opened because the passed in
253 * stream causes us to bypass the new caching code. Play it safe. If it
254 * is an IMAP stream, just close it. This should leave it in the new-style
255 * cache anyway, causing no loss. Maybe not if the cache wasn't large
256 * enough to have it in there in the first place, in which case we get
257 * a possibly unnecessary close and open. If it isn't IMAP we still have
258 * to worry about it because it will cause us to bypass the caching code.
259 * So if the stream isn't IMAP but the mailbox we're opening is, close it.
260 * The immediate alternative would be to try to emulate the code in
261 * mail_open that checks whether it is re-usable or not, but that is
262 * dangerous if that code changes on us.
265 if(is_imap_stream(stream
)
266 || ((d
= mail_valid (NIL
, mailbox
, (char *) NIL
))
267 && !strcmp(d
->name
, "imap"))){
268 if(is_imap_stream(stream
)){
270 "pine_mail_open: closing passed in IMAP stream %s\n",
271 stream
->mailbox
? stream
->mailbox
: "?"));
275 "pine_mail_open: closing passed in non-IMAP stream %s\n",
276 stream
->mailbox
? stream
->mailbox
: "?"));
279 pine_mail_close(stream
);
285 * Maildrops are special. The mailbox name will be a #move name. If the
286 * target of the maildrop is an IMAP folder we want to be sure it isn't
287 * already open in another cached stream, to avoid double opens. This
288 * could have happened if the user opened it manually as the target
289 * instead of as a maildrop. It could also be a side-effect of marking
290 * an answered flag after a reply.
293 if(check_for_move_mbox(mailbox
, NULL
, 0, &target
)){
294 MAILSTREAM
*targetstream
= NULL
;
296 if((d
= mail_valid (NIL
, target
, (char *) NIL
)) && !strcmp(d
->name
, "imap")){
297 targetstream
= sp_stream_get(target
, SP_MATCH
| SP_RO_OK
);
299 dprint((9, "pine_mail_open: close previously opened target of maildrop\n"));
300 pine_mail_actually_close(targetstream
);
305 if((usepool
&& !stream
&& permlocked
)
306 || (!usepool
&& (permlocked
|| (openflags
& OP_READONLY
))
307 && (retstream
= already_open_stream(mailbox
, AOS_NONE
)))){
311 stream
= sp_stream_get(mailbox
,
312 SP_MATCH
| ((openflags
& OP_READONLY
) ? SP_RO_OK
: 0));
315 | (usepool
? SP_USEPOOL
: 0)
316 | (permlocked
? SP_PERMLOCKED
: 0)
317 | (is_inbox
? SP_INBOX
: 0)
318 | (uf
? SP_USERFLDR
: 0)
319 | (tempuse
? SP_TEMPUSE
: 0);
322 * If the stream wasn't already locked, then we reset it so it
323 * looks like we are reopening it. We have to worry about recent
324 * messages since they will still be recent, if that affects us.
326 if(!(sp_flags(stream
) & SP_LOCKED
))
327 reset_stream_view_state(stream
);
330 *retflags
|= SP_MATCH
;
331 if(sp_flags(stream
) & SP_LOCKED
)
332 *retflags
|= SP_LOCKED
;
335 if(sp_flags(stream
) & SP_LOCKED
336 && sp_flags(stream
) & SP_USERFLDR
337 && !(flags
& SP_USERFLDR
)){
338 sp_set_ref_cnt(stream
, sp_ref_cnt(stream
)+1);
340 "pine_mail_open: permlocked: ref cnt up to %d\n",
341 sp_ref_cnt(stream
)));
343 else if(sp_ref_cnt(stream
) <= 0){
344 sp_set_ref_cnt(stream
, 1);
346 "pine_mail_open: permexact: ref cnt set to %d\n",
347 sp_ref_cnt(stream
)));
350 carefully_reset_sp_flags(stream
, flags
);
352 if(stream
->silent
&& !(openflags
& OP_SILENT
))
353 stream
->silent
= NIL
;
355 dprint((9, "pine_mail_open: stream was already open\n"));
356 if(stream
&& stream
->dtb
&& stream
->dtb
->name
357 && !strcmp(stream
->dtb
->name
, "imap")){
358 dprint((7, "pine_mail_open: next TAG %08lx\n",
366 if(usepool
&& !stream
){
368 * First, we look for an exact match, a stream which is already
369 * open to the mailbox we are trying to re-open, and we use that.
370 * Skip permlocked only because we did it above already.
373 stream
= sp_stream_get(mailbox
,
374 SP_MATCH
| ((openflags
& OP_READONLY
) ? SP_RO_OK
: 0));
378 | (usepool
? SP_USEPOOL
: 0)
379 | (permlocked
? SP_PERMLOCKED
: 0)
380 | (is_inbox
? SP_INBOX
: 0)
381 | (uf
? SP_USERFLDR
: 0)
382 | (tempuse
? SP_TEMPUSE
: 0);
385 * If the stream wasn't already locked, then we reset it so it
386 * looks like we are reopening it. We have to worry about recent
387 * messages since they will still be recent, if that affects us.
389 if(!(sp_flags(stream
) & SP_LOCKED
))
390 reset_stream_view_state(stream
);
393 *retflags
|= SP_MATCH
;
394 if(sp_flags(stream
) & SP_LOCKED
)
395 *retflags
|= SP_LOCKED
;
398 if(sp_flags(stream
) & SP_LOCKED
399 && sp_flags(stream
) & SP_USERFLDR
400 && !(flags
& SP_USERFLDR
)){
401 sp_set_ref_cnt(stream
, sp_ref_cnt(stream
)+1);
403 "pine_mail_open: matched: ref cnt up to %d\n",
404 sp_ref_cnt(stream
)));
406 else if(sp_ref_cnt(stream
) <= 0){
407 sp_set_ref_cnt(stream
, 1);
409 "pine_mail_open: exact: ref cnt set to %d\n",
410 sp_ref_cnt(stream
)));
413 carefully_reset_sp_flags(stream
, flags
);
416 * We may be re-using a stream that was previously open
417 * with OP_SILENT and now we don't want OP_SILENT.
418 * We don't turn !silent into silent because the !silentness
419 * could be important in the original context (for example,
420 * silent suppresses mm_expunged calls).
422 * WARNING: we're messing with c-client internals.
424 if(stream
->silent
&& !(openflags
& OP_SILENT
))
425 stream
->silent
= NIL
;
427 dprint((9, "pine_mail_open: stream already open\n"));
428 if(stream
&& stream
->dtb
&& stream
->dtb
->name
429 && !strcmp(stream
->dtb
->name
, "imap")){
430 dprint((7, "pine_mail_open: next TAG %08lx\n",
438 * No exact match, look for a stream which is open to the same
439 * server and marked for TEMPUSE.
441 stream
= sp_stream_get(mailbox
, SP_SAME
| SP_TEMPUSE
);
444 "pine_mail_open: attempting to re-use TEMP stream\n"));
447 * No SAME/TEMPUSE stream so we look to see if there is an
448 * open slot available (we're not yet at max_remstream). If there
449 * is an open slot, we'll just open a new stream and put it there.
450 * If not, we'll go inside this conditional.
453 && sp_nusepool_notperm() >= ps_global
->s_pool
.max_remstream
){
455 "pine_mail_open: no empty slots\n"));
457 * No empty remote slots available. See if there is a
458 * TEMPUSE stream that is open but to the wrong server.
460 stream
= sp_stream_get(mailbox
, SP_TEMPUSE
);
463 * We will close this stream and use the empty slot
467 "pine_mail_open: close a TEMPUSE stream and re-use that slot\n"));
468 pine_mail_actually_close(stream
);
474 * Still no luck. Look for a stream open to the same
475 * server that is just not locked. This would be a
476 * stream that might be reusable in the future, but we
477 * need it now instead.
479 stream
= sp_stream_get(mailbox
, SP_SAME
| SP_UNLOCKED
);
482 "pine_mail_open: attempting to re-use stream\n"));
486 * We'll take any UNLOCKED stream and re-use it.
488 stream
= sp_stream_get(mailbox
, 0);
491 * We will close this stream and use the empty slot
495 "pine_mail_open: close an UNLOCKED stream and re-use the slot\n"));
496 pine_mail_actually_close(stream
);
500 if(ps_global
->s_pool
.max_remstream
){
501 dprint((9, "pine_mail_open: all USEPOOL slots full of LOCKED streams, nothing to use\n"));
504 dprint((9, "pine_mail_open: no caching, max_remstream == 0\n"));
512 "pine_mail_open5: Can't mark folder Stay-Open: at max-remote limit\n"));
513 q_status_message1(SM_ORDER
, 3, 5,
514 "Can't mark folder Stay-Open: reached max-remote limit (%s)",
515 comatose((long) ps_global
->s_pool
.max_remstream
));
523 "pine_mail_open: there is an empty slot to use\n"));
527 * We'll make an assumption here. If we were asked to halfopen a
528 * stream then we'll assume that the caller doesn't really care if
529 * the stream is halfopen or if it happens to be open to some mailbox
530 * already. They are just saying halfopen because they don't need to
531 * SELECT a mailbox. That's the assumption, anyway.
533 if(openflags
& OP_HALFOPEN
&& stream
){
535 "pine_mail_open: asked for HALFOPEN so returning stream as is\n"));
536 sp_set_ref_cnt(stream
, sp_ref_cnt(stream
)+1);
538 "pine_mail_open: halfopen: ref cnt up to %d\n",
539 sp_ref_cnt(stream
)));
540 if(stream
&& stream
->dtb
&& stream
->dtb
->name
541 && !strcmp(stream
->dtb
->name
, "imap")){
542 dprint((7, "pine_mail_open: next TAG %08lx\n",
546 if(stream
->silent
&& !(openflags
& OP_SILENT
))
547 stream
->silent
= NIL
;
553 * We're going to SELECT another folder with this open stream.
557 * We will have just pinged the stream to make sure it is
558 * still alive. That ping may have discovered some new mail.
559 * Before unselecting the folder, we should process the filters
562 if(!sp_flagged(stream
, SP_LOCKED
)
563 && !sp_flagged(stream
, SP_USERFLDR
)
564 && sp_new_mail_count(stream
))
565 process_filter_patterns(stream
, sp_msgmap(stream
),
566 sp_new_mail_count(stream
));
568 if(stream
&& stream
->dtb
&& stream
->dtb
->name
569 && !strcmp(stream
->dtb
->name
, "imap")){
571 "pine_mail_open: cancel idle timer: TAG %08lx (%s)\n",
572 stream
->gensym
, debug_time(1, ps_global
->debug_timestamp
)));
576 * We need to reset the counters and everything.
577 * The easiest way to do that is just to delete all of the
578 * sp_s data and let the open fill it in correctly for
581 sp_free((PER_STREAM_S
**) &stream
->sparep
);
586 * When we pass a stream to mail_open, it will either re-use it or
589 retstream
= mail_open(stream
, mailbox
, openflags
);
593 dprint((7, "pine_mail_open: mail_open returns stream:\n original_mailbox=%s\n mailbox=%s\n driver=%s rdonly=%d halfopen=%d secure=%d nmsgs=%ld recent=%ld\n", retstream
->original_mailbox
? retstream
->original_mailbox
: "?", retstream
->mailbox
? retstream
->mailbox
: "?", (retstream
->dtb
&& retstream
->dtb
->name
) ? retstream
->dtb
->name
: "?", retstream
->rdonly
, retstream
->halfopen
, retstream
->secure
, retstream
->nmsgs
, retstream
->recent
));
596 * So it is easier to figure out which command goes with which
597 * stream when debugging, change the tag associated with each stream.
598 * Of course, this will come after the SELECT, so the startup IMAP
599 * commands will have confusing tags.
601 if(retstream
!= stream
&& retstream
->dtb
&& retstream
->dtb
->name
602 && !strcmp(retstream
->dtb
->name
, "imap")){
603 retstream
->gensym
+= (streamcounter
* 0x1000000);
604 streamcounter
= (streamcounter
+ 1) % (8 * 16);
607 if(retstream
&& retstream
->dtb
&& retstream
->dtb
->name
608 && !strcmp(retstream
->dtb
->name
, "imap")){
609 dprint((7, "pine_mail_open: next TAG %08lx\n",
614 * Catch the case where our test up above (where usepool was set)
615 * did not notice that this was news, but that we can tell once
616 * we've opened the stream. (One such case would be an imap proxy
617 * to an nntp server.) Remove it from being cached here. There was
618 * a possible penalty for not noticing sooner. If all the usepool
619 * slots were full, we will have closed one of the UNLOCKED streams
620 * above, preventing us from future re-use of that stream.
621 * We could figure out how to do the test better with just the
622 * name. We could open the stream and then close the other one
623 * after the fact. Or we could just not worry about it since it is
626 if(IS_NEWS(retstream
)){
631 /* make sure the message map has been instantiated */
632 (void) sp_msgmap(retstream
);
635 * If a referral during the mail_open above causes a stream
636 * re-use which involves a BYE on an IMAP stream, then that
637 * BYE will have caused an mm_notify which will have
638 * instantiated the sp_data and set dead_stream! Trust that
639 * it is alive up to here and reset it to zero.
641 sp_set_dead_stream(retstream
, 0);
644 | (usepool
? SP_USEPOOL
: 0)
645 | (permlocked
? SP_PERMLOCKED
: 0)
646 | (is_inbox
? SP_INBOX
: 0)
647 | (uf
? SP_USERFLDR
: 0)
648 | (tempuse
? SP_TEMPUSE
: 0);
650 sp_flag(retstream
, flags
);
651 sp_set_recent_since_visited(retstream
, retstream
->recent
);
653 /* initialize the reference count */
654 sp_set_ref_cnt(retstream
, 1);
655 dprint((7, "pine_mail_open: reset: ref cnt set to %d\n",
656 sp_ref_cnt(retstream
)));
658 if(sp_add(retstream
, usepool
) != 0 && usepool
){
662 | (usepool
? SP_USEPOOL
: 0)
663 | (permlocked
? SP_PERMLOCKED
: 0)
664 | (is_inbox
? SP_INBOX
: 0)
665 | (uf
? SP_USERFLDR
: 0)
666 | (tempuse
? SP_TEMPUSE
: 0);
668 sp_flag(retstream
, flags
);
669 (void) sp_add(retstream
, usepool
);
674 * When opening a newsgroup, c-client marks the messages up to the
675 * last Deleted as Unseen. If the feature news-approximates-new-status
676 * is on, we'd rather they be treated as Seen. That way, selecting New
677 * messages will give us the ones past the last Deleted. So we're going
678 * to change them to Seen. Since Seen is a session flag for news, making
679 * this change won't have any permanent effect. C-client also marks the
680 * messages after the last deleted Recent, which is the bit of
681 * information we'll use to find the messages we want to change.
683 if(F_ON(F_FAKE_NEW_IN_NEWS
, ps_global
) &&
684 retstream
->nmsgs
> 0 && IS_NEWS(retstream
)){
686 long i
, mflags
= ST_SET
;
690 * Search for !recent messages to set the searched bit for
691 * those messages we want to change. Then we'll flip the bits.
693 (void)count_flagged(retstream
, F_UNRECENT
);
695 for(i
= 1L; i
<= retstream
->nmsgs
; i
++)
696 if((mc
= mail_elt(retstream
,i
)) && mc
->searched
)
701 if(!is_imap_stream(retstream
))
703 if((seq
= build_sequence(retstream
, NULL
, NULL
)) != NULL
){
704 mail_flag(retstream
, seq
, "\\SEEN", mflags
);
705 fs_give((void **)&seq
);
715 reset_stream_view_state(MAILSTREAM
*stream
)
722 mm
= sp_msgmap(stream
);
727 sp_set_viewing_a_thread(stream
, 0);
728 sp_set_need_to_rethread(stream
, 0);
730 mn_reset_cur(mm
, stream
->nmsgs
> 0L ? stream
->nmsgs
: 0L); /* default */
732 mm
->visible_threads
= -1L;
735 mm
->top_after_thrd
= 0L;
737 mn_set_mansort(mm
, 0);
740 * Get rid of zooming and selections, but leave filtering flags. All the
741 * flag counts and everything should still be correct because set_lflag
742 * preserves them correctly.
744 if(any_lflagged(mm
, MN_SLCT
| MN_HIDE
)){
747 for(i
= 1L; i
<= mn_get_total(mm
); i
++)
748 set_lflag(stream
, mm
, i
, MN_SLCT
| MN_HIDE
, 0);
752 * We could try to set up a default sort order, but the caller is going
753 * to re-sort anyway if they are interested in sorting. So we won't do
760 * We have to be careful when we change the flags of an already
761 * open stream, because there may be more than one section of
762 * code actively using the stream.
763 * We allow turning on (but not off) SP_LOCKED
767 * We allow turning off (but not on) SP_TEMPUSE
770 carefully_reset_sp_flags(MAILSTREAM
*stream
, long unsigned int flags
)
772 if(sp_flags(stream
) != flags
){
773 /* allow turning on but not off */
774 if(sp_flags(stream
) & SP_LOCKED
&& !(flags
& SP_LOCKED
))
777 if(sp_flags(stream
) & SP_PERMLOCKED
&& !(flags
& SP_PERMLOCKED
))
778 flags
|= SP_PERMLOCKED
;
780 if(sp_flags(stream
) & SP_USERFLDR
&& !(flags
& SP_USERFLDR
))
781 flags
|= SP_USERFLDR
;
783 if(sp_flags(stream
) & SP_INBOX
&& !(flags
& SP_INBOX
))
787 /* allow turning off but not on */
788 if(!(sp_flags(stream
) & SP_TEMPUSE
) && flags
& SP_TEMPUSE
)
789 flags
&= ~SP_TEMPUSE
;
792 /* leave the way they already are */
793 if((sp_flags(stream
) & SP_FILTERED
) != (flags
& SP_FILTERED
))
794 flags
= (flags
& ~SP_FILTERED
) | (sp_flags(stream
) & SP_FILTERED
);
797 if(sp_flags(stream
) != flags
)
798 sp_flag(stream
, flags
);
804 * Pine wrapper around mail_create. If we have the PREFER_ALT_AUTH flag turned
805 * on we don't want to pass a NULL stream to c-client because it will open
806 * a non-ssl connection when we want it to be ssl.
809 pine_mail_create(MAILSTREAM
*stream
, char *mailbox
)
811 MAILSTREAM
*ourstream
= NULL
;
813 long openflags
= (OP_HALFOPEN
| OP_SILENT
| SP_USEPOOL
| SP_TEMPUSE
);
814 char source
[MAILTMPLEN
], *target
= NULL
;
817 dprint((7, "pine_mail_create: creating \"%s\"%s\n",
818 mailbox
? mailbox
: "(NULL)",
819 stream
? "" : " (stream was NULL)"));
821 if(check_for_move_mbox(mailbox
, source
, sizeof(source
), &target
)){
824 "pine_mail_create: #move special case, creating \"%s\"\n",
825 mailbox
? mailbox
: "(NULL)"));
829 * We don't really need this anymore, since we are now using IMAPTRYALT.
830 * We'll leave it since it works.
832 if((F_ON(F_PREFER_ALT_AUTH
, ps_global
)
833 || (ps_global
->debug_imap
> 3 || ps_global
->debugmem
))){
835 if((d
= mail_valid (NIL
, mailbox
, (char *) NIL
))
836 && !strcmp(d
->name
, "imap")){
838 if(F_ON(F_PREFER_ALT_AUTH
, ps_global
))
839 openflags
|= OP_TRYALT
;
844 stream
= sp_stream_get(mailbox
, SP_MATCH
);
846 stream
= sp_stream_get(mailbox
, SP_SAME
);
850 * It is only useful to open a stream in the imap case.
852 if((d
= mail_valid (NIL
, mailbox
, (char *) NIL
))
853 && !strcmp(d
->name
, "imap")){
855 stream
= pine_mail_open(NULL
, mailbox
, openflags
, NULL
);
860 return_val
= mail_create(stream
, mailbox
);
863 pine_mail_close(ourstream
);
870 * Pine wrapper around mail_delete.
873 pine_mail_delete(MAILSTREAM
*stream
, char *mailbox
)
875 MAILSTREAM
*ourstream
= NULL
;
877 long openflags
= (OP_HALFOPEN
| OP_SILENT
| SP_USEPOOL
| SP_TEMPUSE
);
878 char source
[MAILTMPLEN
], *target
= NULL
;
881 dprint((7, "pine_mail_delete: deleting \"%s\"%s\n",
882 mailbox
? mailbox
: "(NULL)",
883 stream
? "" : " (stream was NULL)"));
885 if(check_for_move_mbox(mailbox
, source
, sizeof(source
), &target
)){
888 "pine_mail_delete: #move special case, deleting \"%s\"\n",
889 mailbox
? mailbox
: "(NULL)"));
893 * We don't really need this anymore, since we are now using IMAPTRYALT.
895 if((F_ON(F_PREFER_ALT_AUTH
, ps_global
)
896 || (ps_global
->debug_imap
> 3 || ps_global
->debugmem
))){
898 if((d
= mail_valid (NIL
, mailbox
, (char *) NIL
))
899 && !strcmp(d
->name
, "imap")){
901 if(F_ON(F_PREFER_ALT_AUTH
, ps_global
))
902 openflags
|= OP_TRYALT
;
906 /* oops, we seem to be deleting a selected stream */
907 if(!stream
&& (stream
= sp_stream_get(mailbox
, SP_MATCH
))){
908 pine_mail_actually_close(stream
);
913 stream
= sp_stream_get(mailbox
, SP_SAME
);
917 * It is only useful to open a stream in the imap case.
919 if((d
= mail_valid (NIL
, mailbox
, (char *) NIL
))
920 && !strcmp(d
->name
, "imap")){
922 stream
= pine_mail_open(NULL
, mailbox
, openflags
, NULL
);
927 return_val
= mail_delete(stream
, mailbox
);
930 pine_mail_close(ourstream
);
937 * Pine wrapper around mail_append.
940 pine_mail_append_full(MAILSTREAM
*stream
, char *mailbox
, char *flags
, char *date
, STRING
*message
)
942 MAILSTREAM
*ourstream
= NULL
;
944 long openflags
= (OP_HALFOPEN
| OP_SILENT
| SP_USEPOOL
| SP_TEMPUSE
);
945 char source
[MAILTMPLEN
], *target
= NULL
;
946 char mailbox_nodelim
[MAILTMPLEN
];
950 dprint((7, "pine_mail_append_full: appending to \"%s\"%s\n",
951 mailbox
? mailbox
: "(NULL)",
952 stream
? "" : " (stream was NULL)"));
954 /* strip delimiter, it has no meaning in an APPEND and could cause trouble */
955 if(mailbox
&& (delim
= get_folder_delimiter(mailbox
)) != '\0'){
958 len
= strlen(mailbox
);
959 if(mailbox
[len
-1] == delim
){
960 strncpy(mailbox_nodelim
, mailbox
, MIN(len
-1,sizeof(mailbox_nodelim
)-1));
961 mailbox_nodelim
[MIN(len
-1,sizeof(mailbox_nodelim
)-1)] = '\0';
962 mailbox
= mailbox_nodelim
;
966 if(check_for_move_mbox(mailbox
, source
, sizeof(source
), &target
)){
969 "pine_mail_append_full: #move special case, appending to \"%s\"\n",
970 mailbox
? mailbox
: "(NULL)"));
974 * We don't really need this anymore, since we are now using IMAPTRYALT.
976 if((F_ON(F_PREFER_ALT_AUTH
, ps_global
)
977 || (ps_global
->debug_imap
> 3 || ps_global
->debugmem
))){
979 if((d
= mail_valid (NIL
, mailbox
, (char *) NIL
))
980 && !strcmp(d
->name
, "imap")){
982 if(F_ON(F_PREFER_ALT_AUTH
, ps_global
))
983 openflags
|= OP_TRYALT
;
988 stream
= sp_stream_get(mailbox
, SP_MATCH
);
990 stream
= sp_stream_get(mailbox
, SP_SAME
);
994 * It is only useful to open a stream in the imap case.
996 if((d
= mail_valid (NIL
, mailbox
, (char *) NIL
))
997 && !strcmp(d
->name
, "imap")){
999 stream
= pine_mail_open(NULL
, mailbox
, openflags
, NULL
);
1004 return_val
= mail_append_full(stream
, mailbox
, flags
, date
, message
);
1007 pine_mail_close(ourstream
);
1014 * Pine wrapper around mail_append.
1017 pine_mail_append_multiple(MAILSTREAM
*stream
, char *mailbox
, append_t af
,
1018 APPENDPACKAGE
*data
, MAILSTREAM
*not_this_stream
)
1020 MAILSTREAM
*ourstream
= NULL
;
1022 long openflags
= (OP_HALFOPEN
| OP_SILENT
| SP_USEPOOL
| SP_TEMPUSE
);
1023 char source
[MAILTMPLEN
], *target
= NULL
;
1025 int we_blocked_reuse
= 0;
1027 dprint((7, "pine_mail_append_multiple: appending to \"%s\"%s\n",
1028 mailbox
? mailbox
: "(NULL)",
1029 stream
? "" : " (stream was NULL)"));
1031 if(check_for_move_mbox(mailbox
, source
, sizeof(source
), &target
)){
1034 "pine_mail_append_multiple: #move special case, appending to \"%s\"\n",
1035 mailbox
? mailbox
: "(NULL)"));
1038 if((F_ON(F_PREFER_ALT_AUTH
, ps_global
)
1039 || (ps_global
->debug_imap
> 3 || ps_global
->debugmem
))){
1041 if((d
= mail_valid (NIL
, mailbox
, (char *) NIL
))
1042 && !strcmp(d
->name
, "imap")){
1044 if(F_ON(F_PREFER_ALT_AUTH
, ps_global
))
1045 openflags
|= OP_TRYALT
;
1050 * We have to be careful re-using streams for multiappend, because of
1051 * the way it works. We call into c-client below but part of the call
1052 * is data containing a callback function to us to supply the data to
1053 * be appended. That function may need to get the data from the server.
1054 * If that uses the same stream as we're trying to append on, we're
1055 * in trouble. We can't call back into c-client from c-client on the same
1056 * stream. (Just think about it, we're in the middle of an APPEND command.
1057 * We can't issue a FETCH before the APPEND completes in order to complete
1058 * the APPEND.) We can re-use a stream if it is a different stream from
1059 * the one we are reading from, so that's what the not_this_stream
1060 * stuff is for. If we mark it !SP_USEPOOL, it won't get reused.
1062 if(sp_flagged(not_this_stream
, SP_USEPOOL
)){
1064 sp_unflag(not_this_stream
, SP_USEPOOL
);
1068 stream
= sp_stream_get(mailbox
, SP_MATCH
);
1070 stream
= sp_stream_get(mailbox
, SP_SAME
);
1074 * It is only useful to open a stream in the imap case.
1076 if((d
= mail_valid (NIL
, mailbox
, (char *) NIL
))
1077 && !strcmp(d
->name
, "imap")){
1079 stream
= pine_mail_open(NULL
, mailbox
, openflags
, NULL
);
1084 if(we_blocked_reuse
)
1085 sp_set_flags(not_this_stream
, sp_flags(not_this_stream
) | SP_USEPOOL
);
1087 return_val
= mail_append_multiple(stream
, mailbox
, af
, (void *) data
);
1090 pine_mail_close(ourstream
);
1097 * Pine wrapper around mail_copy.
1100 pine_mail_copy_full(MAILSTREAM
*stream
, char *sequence
, char *mailbox
, long int options
)
1102 MAILSTREAM
*ourstream
= NULL
;
1104 long openflags
= (OP_HALFOPEN
| OP_SILENT
| SP_USEPOOL
| SP_TEMPUSE
);
1105 char source
[MAILTMPLEN
], *target
= NULL
;
1106 char mailbox_nodelim
[MAILTMPLEN
];
1110 dprint((7, "pine_mail_copy_full: copying to \"%s\"%s\n",
1111 mailbox
? mailbox
: "(NULL)",
1112 stream
? "" : " (stream was NULL)"));
1114 /* strip delimiter, it has no meaning in a COPY and could cause trouble */
1115 if(mailbox
&& (delim
= get_folder_delimiter(mailbox
)) != '\0'){
1118 len
= strlen(mailbox
);
1119 if(mailbox
[len
-1] == delim
){
1120 strncpy(mailbox_nodelim
, mailbox
, MIN(len
-1,sizeof(mailbox_nodelim
)-1));
1121 mailbox_nodelim
[MIN(len
-1,sizeof(mailbox_nodelim
)-1)] = '\0';
1122 mailbox
= mailbox_nodelim
;
1126 if(check_for_move_mbox(mailbox
, source
, sizeof(source
), &target
)){
1129 "pine_mail_copy_full: #move special case, copying to \"%s\"\n",
1130 mailbox
? mailbox
: "(NULL)"));
1134 * We don't really need this anymore, since we are now using IMAPTRYALT.
1136 if((F_ON(F_PREFER_ALT_AUTH
, ps_global
)
1137 || (ps_global
->debug_imap
> 3 || ps_global
->debugmem
))){
1139 if((d
= mail_valid (NIL
, mailbox
, (char *) NIL
))
1140 && !strcmp(d
->name
, "imap")){
1142 if(F_ON(F_PREFER_ALT_AUTH
, ps_global
))
1143 openflags
|= OP_TRYALT
;
1148 stream
= sp_stream_get(mailbox
, SP_MATCH
);
1150 stream
= sp_stream_get(mailbox
, SP_SAME
);
1154 * It is only useful to open a stream in the imap case.
1155 * Actually, mail_copy_full is the case where it might also be
1156 * useful to provide a stream in the nntp and pop3 cases. If we
1157 * cache such streams, then we will probably want to open one
1158 * here so that it gets cached.
1160 if((d
= mail_valid (NIL
, mailbox
, (char *) NIL
))
1161 && !strcmp(d
->name
, "imap")){
1163 stream
= pine_mail_open(NULL
, mailbox
, openflags
, NULL
);
1168 return_val
= mail_copy_full(stream
, sequence
, mailbox
, options
);
1171 pine_mail_close(ourstream
);
1178 * Pine wrapper around mail_rename.
1181 pine_mail_rename(MAILSTREAM
*stream
, char *old
, char *new)
1183 MAILSTREAM
*ourstream
= NULL
;
1185 long openflags
= (OP_HALFOPEN
| OP_SILENT
| SP_USEPOOL
| SP_TEMPUSE
);
1188 dprint((7, "pine_mail_rename(%s,%s)\n", old
? old
: "",
1192 * We don't really need this anymore, since we are now using IMAPTRYALT.
1194 if((F_ON(F_PREFER_ALT_AUTH
, ps_global
)
1195 || (ps_global
->debug_imap
> 3 || ps_global
->debugmem
))){
1197 if((d
= mail_valid (NIL
, old
, (char *) NIL
))
1198 && !strcmp(d
->name
, "imap")){
1200 if(F_ON(F_PREFER_ALT_AUTH
, ps_global
))
1201 openflags
|= OP_TRYALT
;
1205 /* oops, we seem to be renaming a selected stream */
1206 if(!stream
&& (stream
= sp_stream_get(old
, SP_MATCH
))){
1207 pine_mail_actually_close(stream
);
1212 stream
= sp_stream_get(old
, SP_SAME
);
1216 * It is only useful to open a stream in the imap case.
1218 if((d
= mail_valid (NIL
, old
, (char *) NIL
))
1219 && !strcmp(d
->name
, "imap")){
1221 stream
= pine_mail_open(NULL
, old
, openflags
, NULL
);
1226 return_val
= mail_rename(stream
, old
, new);
1229 pine_mail_close(ourstream
);
1235 /*----------------------------------------------------------------------
1236 Our mail_close wrapper to clean up anything on the mailstream we may have
1237 added to it. mostly in the unused bits of the elt's.
1240 pine_mail_close(MAILSTREAM
*stream
)
1242 unsigned long uid_last
, last_uid
;
1248 dprint((7, "pine_mail_close: %s (%s)\n",
1249 stream
&& stream
->mailbox
? stream
->mailbox
: "(NULL)",
1250 debug_time(1, ps_global
->debug_timestamp
)));
1252 if(sp_flagged(stream
, SP_USEPOOL
) && !sp_dead_stream(stream
)){
1254 refcnt
= sp_ref_cnt(stream
);
1255 dprint((7, "pine_mail_close: ref cnt is %d\n", refcnt
));
1258 * Instead of checkpointing here, which takes time that the user
1259 * definitely notices, we checkpoint in new_mail at the next
1260 * opportune time, hopefully when the user is idle.
1263 if(sp_flagged(stream
, SP_LOCKED
) && sp_flagged(stream
, SP_USERFLDR
)
1264 && !stream
->halfopen
&& refcnt
<= 1){
1265 if(changes_to_checkpoint(stream
))
1266 pine_mail_check(stream
);
1269 "pine_mail_close: dont think we need to checkpoint\n"));
1275 * Uid_last is valid when we first open a stream, but not always
1276 * valid after that. So if we know the last uid should be higher
1277 * than uid_last (!#%) use that instead.
1279 uid_last
= stream
->uid_last
;
1280 if(stream
->nmsgs
> 0L
1281 && (last_uid
=mail_uid(stream
,stream
->nmsgs
)) > uid_last
)
1282 uid_last
= last_uid
;
1284 sp_set_saved_uid_validity(stream
, stream
->uid_validity
);
1285 sp_set_saved_uid_last(stream
, uid_last
);
1288 * If the reference count is down to 0, unlock it.
1289 * In any case, don't actually do any real closing.
1292 sp_set_ref_cnt(stream
, refcnt
-1);
1294 refcnt
= sp_ref_cnt(stream
);
1295 dprint((7, "pine_mail_close: ref cnt is %d\n", refcnt
));
1298 "pine_mail_close: unlocking: start idle timer: TAG %08lx (%s)\n",
1299 stream
->gensym
, debug_time(1, ps_global
->debug_timestamp
)));
1300 sp_set_last_use_time(stream
, time(0));
1303 * Logically, we ought to be unflagging SP_INBOX, too. However,
1304 * the filtering code uses SP_INBOX when deciding if it should
1305 * filter some things, and we keep filtering after the mailbox
1306 * is closed. So leave SP_INBOX alone. This (the closing of INBOX)
1307 * usually only happens in goodnight_gracey when we're
1308 * shutting everything down.
1310 sp_unflag(stream
, SP_LOCKED
| SP_PERMLOCKED
| SP_USERFLDR
);
1313 dprint((7, "pine_mail_close: ref cnt is now %d\n",
1318 dprint((7, "pine_mail_close: %s\n",
1319 sp_flagged(stream
, SP_USEPOOL
) ? "dead stream" : "no pool"));
1321 refcnt
= sp_ref_cnt(stream
);
1322 dprint((7, "pine_mail_close: ref cnt is %d\n", refcnt
));
1325 * If the reference count is down to 0, unlock it.
1326 * In any case, don't actually do any real closing.
1329 sp_set_ref_cnt(stream
, refcnt
-1);
1331 refcnt
= sp_ref_cnt(stream
);
1333 pine_mail_actually_close(stream
);
1336 dprint((7, "pine_mail_close: ref cnt is now %d\n",
1345 pine_mail_actually_close(MAILSTREAM
*stream
)
1350 if(!sp_closing(stream
)){
1351 dprint((7, "pine_mail_actually_close: %s (%s)\n",
1352 stream
&& stream
->mailbox
? stream
->mailbox
: "(NULL)",
1353 debug_time(1, ps_global
->debug_timestamp
)));
1355 sp_set_closing(stream
, 1);
1357 if(!sp_flagged(stream
, SP_LOCKED
)
1358 && !sp_flagged(stream
, SP_USERFLDR
)
1359 && !sp_dead_stream(stream
)
1360 && sp_new_mail_count(stream
))
1361 process_filter_patterns(stream
, sp_msgmap(stream
),
1362 sp_new_mail_count(stream
));
1366 * let sp_free_callback() free the sp_s stuff and the callbacks to
1367 * free_pine_elt free the per-elt pine stuff.
1375 * If we haven't used a stream for a while, we may want to logout.
1378 maybe_kill_old_stream(MAILSTREAM
*stream
)
1380 #define KILL_IF_IDLE_TIME (25 * 60)
1382 && !sp_flagged(stream
, SP_LOCKED
)
1383 && !sp_flagged(stream
, SP_USERFLDR
)
1384 && time(0) - sp_last_use_time(stream
) > KILL_IF_IDLE_TIME
){
1387 "killing idle stream: %s (%s): idle timer = %ld secs\n",
1388 stream
&& stream
->mailbox
? stream
->mailbox
: "(NULL)",
1389 debug_time(1, ps_global
->debug_timestamp
),
1390 (long) (time(0)-sp_last_use_time(stream
))));
1393 * Another thing we could do here instead is to unselect the
1394 * mailbox, leaving the stream open to the server.
1396 pine_mail_actually_close(stream
);
1402 * Catch searches that don't need to go to the server.
1403 * (Not anymore, now c-client does this for us.)
1406 pine_mail_search_full(MAILSTREAM
*stream
, char *charset
,
1407 SEARCHPGM
*pgm
, long int flags
)
1410 * The charset should either be UTF-8 or NULL for alpine.
1411 * If it is UTF-8 we may be able to change it to NULL if
1412 * everything in the search is actually ascii. We try to
1413 * do this because not all servers understand the CHARSET
1416 if(charset
&& !strucmp(charset
, "utf-8")
1417 && is_imap_stream(stream
) && !hibit_in_searchpgm(pgm
))
1420 if(F_ON(F_NNTP_SEARCH_USES_OVERVIEW
, ps_global
))
1421 flags
|= SO_OVERVIEW
;
1423 return(stream
? mail_search_full(stream
, charset
, pgm
, flags
) : NIL
);
1428 hibit_in_searchpgm(SEARCHPGM
*pgm
)
1436 if((pgm
->subject
&& hibit_in_strlist(pgm
->subject
))
1437 || (pgm
->text
&& hibit_in_strlist(pgm
->text
))
1438 || (pgm
->body
&& hibit_in_strlist(pgm
->body
))
1439 || (pgm
->cc
&& hibit_in_strlist(pgm
->cc
))
1440 || (pgm
->from
&& hibit_in_strlist(pgm
->from
))
1441 || (pgm
->to
&& hibit_in_strlist(pgm
->to
))
1442 || (pgm
->bcc
&& hibit_in_strlist(pgm
->bcc
))
1443 || (pgm
->keyword
&& hibit_in_strlist(pgm
->keyword
))
1444 || (pgm
->unkeyword
&& hibit_in_strlist(pgm
->return_path
))
1445 || (pgm
->sender
&& hibit_in_strlist(pgm
->sender
))
1446 || (pgm
->reply_to
&& hibit_in_strlist(pgm
->reply_to
))
1447 || (pgm
->in_reply_to
&& hibit_in_strlist(pgm
->in_reply_to
))
1448 || (pgm
->message_id
&& hibit_in_strlist(pgm
->message_id
))
1449 || (pgm
->newsgroups
&& hibit_in_strlist(pgm
->newsgroups
))
1450 || (pgm
->followup_to
&& hibit_in_strlist(pgm
->followup_to
))
1451 || (pgm
->references
&& hibit_in_strlist(pgm
->references
))
1452 || (pgm
->header
&& hibit_in_header(pgm
->header
)))
1455 for(or = pgm
->or; or; or = or->next
)
1456 if(hibit_in_searchpgm(or->first
) || hibit_in_searchpgm(or->second
))
1459 for(not = pgm
->not; not; not = not->next
)
1460 if(hibit_in_searchpgm(not->pgm
))
1468 hibit_in_strlist(STRINGLIST
*sl
)
1470 for(; sl
; sl
= sl
->next
)
1471 if(hibit_in_sizedtext(&sl
->text
))
1479 hibit_in_header(SEARCHHEADER
*header
)
1483 for(hdr
= header
; hdr
; hdr
= hdr
->next
)
1484 if(hibit_in_sizedtext(&hdr
->line
) || hibit_in_sizedtext(&hdr
->text
))
1492 hibit_in_sizedtext(SIZEDTEXT
*st
)
1494 unsigned char *p
, *end
;
1496 p
= st
? st
->data
: NULL
;
1498 for(end
= p
+ st
->size
; p
< end
; p
++)
1507 pine_mail_fetch_flags(MAILSTREAM
*stream
, char *sequence
, long int flags
)
1509 ps_global
->dont_count_flagchanges
= 1;
1510 mail_fetch_flags(stream
, sequence
, flags
);
1511 ps_global
->dont_count_flagchanges
= 0;
1516 pine_mail_fetchenvelope(MAILSTREAM
*stream
, long unsigned int msgno
)
1518 ENVELOPE
*env
= NULL
;
1520 ps_global
->dont_count_flagchanges
= 1;
1521 if(stream
&& msgno
> 0L && msgno
<= stream
->nmsgs
)
1522 env
= mail_fetchenvelope(stream
, msgno
);
1524 ps_global
->dont_count_flagchanges
= 0;
1530 pine_mail_fetch_structure(MAILSTREAM
*stream
, long unsigned int msgno
,
1531 struct mail_bodystruct
**body
, long int flags
)
1533 ENVELOPE
*env
= NULL
;
1535 ps_global
->dont_count_flagchanges
= 1;
1536 if(stream
&& (flags
& FT_UID
|| (msgno
> 0L && msgno
<= stream
->nmsgs
)))
1537 env
= mail_fetch_structure(stream
, msgno
, body
, flags
);
1539 ps_global
->dont_count_flagchanges
= 0;
1545 pine_mail_fetchstructure(MAILSTREAM
*stream
, long unsigned int msgno
, struct mail_bodystruct
**body
)
1547 ENVELOPE
*env
= NULL
;
1549 ps_global
->dont_count_flagchanges
= 1;
1550 if(stream
&& msgno
> 0L && msgno
<= stream
->nmsgs
)
1551 env
= mail_fetchstructure(stream
, msgno
, body
);
1553 ps_global
->dont_count_flagchanges
= 0;
1559 * Wrapper around mail_fetch_body.
1560 * Currently only used to turn on partial fetching if trying
1561 * to work around the microsoft ssl bug.
1564 pine_mail_fetch_body(MAILSTREAM
*stream
, long unsigned int msgno
, char *section
,
1565 long unsigned int *len
, long int flags
)
1568 if(F_ON(F_QUELL_SSL_LARGEBLOCKS
, ps_global
))
1569 return(pine_mail_partial_fetch_wrapper(stream
, msgno
,
1570 section
, len
, flags
, 0, NULL
, 0));
1573 return(mail_fetch_body(stream
, msgno
, section
, len
, flags
));
1577 * Wrapper around mail_fetch_text.
1578 * Currently the only purpose this wrapper serves is to turn
1579 * on partial fetching for quell-ssl-largeblocks.
1582 pine_mail_fetch_text(MAILSTREAM
*stream
, long unsigned int msgno
, char *section
,
1583 long unsigned int *len
, long int flags
)
1586 if(F_ON(F_QUELL_SSL_LARGEBLOCKS
, ps_global
))
1587 return(pine_mail_partial_fetch_wrapper(stream
, msgno
,
1588 section
, len
, flags
,
1592 return(mail_fetch_text(stream
, msgno
, section
, len
, flags
));
1597 * Determine whether to do partial-fetching or not, and do it
1598 * args - same as c-client functions being wrapped around
1599 * get_n_bytes - try to partial fetch for the first n bytes.
1600 * makes no guarantees, might wind up fetching
1601 * the entire text anyway.
1602 * str_to_free - pointer to string to free if we only get
1603 * (non-cachable) partial text (required for
1606 * set, tells us to do mail_fetch_text and mail_partial_text
1607 * unset, tells us to do mail_fetch_body and mail_partial_body
1610 pine_mail_partial_fetch_wrapper(MAILSTREAM
*stream
, long unsigned int msgno
,
1611 char *section
, long unsigned int *len
,
1612 long int flags
, long unsigned int get_n_bytes
,
1613 char **str_to_free
, int is_text_fetch
)
1616 unsigned long size
, firstbyte
, lastbyte
;
1618 FETCH_READC_S
*pftc
;
1619 char imap_cache_section
[MAILTMPLEN
];
1622 char *(*fetch_full
)(MAILSTREAM
*, unsigned long, char *,
1623 unsigned long *, long);
1624 long (*fetch_partial
)(MAILSTREAM
*, unsigned long, char *,
1625 unsigned long, unsigned long, long);
1627 fetch_full
= is_text_fetch
? mail_fetch_text
: mail_fetch_body
;
1628 fetch_partial
= is_text_fetch
? mail_partial_text
: mail_partial_body
;
1630 *str_to_free
= NULL
;
1632 if(F_ON(F_QUELL_SSL_LARGEBLOCKS
, ps_global
) || get_n_bytes
){
1635 #endif /* _WINDOWS */
1636 if(section
&& *section
)
1637 body
= mail_body(stream
, msgno
, (unsigned char *) section
);
1639 pine_mail_fetch_structure(stream
, msgno
, &body
, flags
);
1642 if(body
->type
!= TYPEMULTIPART
)
1643 size
= body
->size
.bytes
;
1644 else if((!section
|| !*section
) && msgno
> 0L
1645 && stream
&& msgno
<= stream
->nmsgs
1646 && (mc
= mail_elt(stream
, msgno
)))
1647 size
= mc
->rfc822_size
; /* upper bound */
1648 else /* just a guess, can't get actual size */
1649 size
= fcc_size_guess(body
) + 2048;
1652 * imap_cache, originally intended for c-client internal use,
1653 * takes a section argument that is different from one we
1654 * would pass to mail_body. Typically in this function
1655 * section is NULL, which translates to "TEXT", but in other
1656 * cases we would want to append ".TEXT" to the section
1659 snprintf(imap_cache_section
, sizeof(imap_cache_section
), "%.*s%sTEXT", MAILTMPLEN
- 10,
1660 section
&& *section
? section
: "",
1661 section
&& *section
? "." : "");
1663 snprintf(imap_cache_section
, sizeof(imap_cache_section
), "%.*s", MAILTMPLEN
- 10,
1664 section
&& *section
? section
: "");
1666 if(modern_imap_stream(stream
)
1668 && ((size
> AVOID_MICROSOFT_SSL_CHUNKING_BUG
)
1669 || (get_n_bytes
&& size
> get_n_bytes
))
1671 && (get_n_bytes
&& size
> get_n_bytes
)
1672 #endif /* _WINDOWS */
1673 && !imap_cache(stream
, msgno
, imap_cache_section
,
1675 if(get_n_bytes
== 0){
1677 "fetch_wrapper: doing partial fetching to work around microsoft bug\n"));
1681 "fetch_wrapper: partial fetching due to %lu get_n_bytes\n", get_n_bytes
));
1683 pftc
= (FETCH_READC_S
*)fs_get(sizeof(FETCH_READC_S
));
1684 memset(g_pft_desc
= pftc
, 0, sizeof(FETCH_READC_S
));
1686 if(F_ON(F_QUELL_SSL_LARGEBLOCKS
, ps_global
)){
1688 pftc
->chunksize
= MIN(get_n_bytes
,
1689 AVOID_MICROSOFT_SSL_CHUNKING_BUG
);
1691 pftc
->chunksize
= AVOID_MICROSOFT_SSL_CHUNKING_BUG
;
1694 #endif /* _WINDOWS */
1695 pftc
->chunksize
= get_n_bytes
;
1697 pftc
->chunk
= (char *) fs_get((pftc
->chunksize
+1)
1699 pftc
->cache
= so_get(CharStar
, NULL
, EDIT_ACCESS
);
1701 so_truncate(pftc
->cache
, size
+ 1);
1702 old_gets
= mail_parameters(stream
, GET_GETS
, (void *)NULL
);
1703 mail_parameters(stream
, SET_GETS
, (void *) partial_text_gets
);
1704 /* start fetching */
1706 firstbyte
= pftc
->read
;
1707 lastbyte
= firstbyte
+ pftc
->chunksize
;
1708 if(get_n_bytes
> firstbyte
&& get_n_bytes
< lastbyte
){
1709 pftc
->chunksize
= get_n_bytes
- firstbyte
;
1710 lastbyte
= get_n_bytes
;
1712 (*fetch_partial
)(stream
, msgno
, section
, firstbyte
,
1713 pftc
->chunksize
, flags
);
1715 if(pftc
->read
!= lastbyte
)
1717 } while((pftc
->read
== lastbyte
)
1718 && (!get_n_bytes
|| (pftc
->read
< get_n_bytes
)));
1720 "fetch_wrapper: anticipated size=%lu read=%lu\n",
1722 mail_parameters(stream
, SET_GETS
, old_gets
);
1723 new_text
.size
= pftc
->read
;
1724 new_text
.data
= (unsigned char *)so_text(pftc
->cache
);
1726 if(get_n_bytes
&& pftc
->read
== get_n_bytes
){
1728 * don't write to cache if we're only dealing with
1732 alpine_panic("Programmer botch: partial fetch attempt w/o string pointer");
1734 *str_to_free
= (char *) new_text
.data
;
1737 /* ugh, rewrite string in case it got stomped on last call */
1739 snprintf(imap_cache_section
, sizeof(imap_cache_section
), "%.*s%sTEXT", MAILTMPLEN
- 10,
1740 section
&& *section
? section
: "",
1741 section
&& *section
? "." : "");
1743 snprintf(imap_cache_section
, sizeof(imap_cache_section
), "%.*s", MAILTMPLEN
- 10,
1744 section
&& *section
? section
: "");
1746 imap_cache(stream
, msgno
, imap_cache_section
, NULL
, &new_text
);
1749 pftc
->cache
->txt
= (void *)NULL
;
1750 so_give(&pftc
->cache
);
1751 fs_give((void **)&pftc
->chunk
);
1752 fs_give((void **)&pftc
);
1755 *len
= new_text
.size
;
1756 return ((char *)new_text
.data
);
1759 return((*fetch_full
)(stream
, msgno
, section
, len
, flags
));
1762 return((*fetch_full
)(stream
, msgno
, section
, len
, flags
));
1766 * c-client callback that handles getting the text
1769 partial_text_gets(readfn_t f
, void *stream
, long unsigned int size
, GETS_DATA
*md
)
1773 n
= MIN(g_pft_desc
->chunksize
, size
);
1774 g_pft_desc
->read
+=n
;
1776 (*f
) (stream
, n
, g_pft_desc
->chunk
);
1778 if(g_pft_desc
->cache
)
1779 so_nputs(g_pft_desc
->cache
, g_pft_desc
->chunk
, (long) n
);
1787 * Pings the stream. Returns 0 if the stream is dead, non-zero otherwise.
1790 pine_mail_ping(MAILSTREAM
*stream
)
1795 if(!sp_dead_stream(stream
)){
1796 ret
= mail_ping(stream
);
1797 if(ret
&& sp_dead_stream(stream
))
1803 sp_set_last_ping(stream
, now
);
1804 sp_set_last_expunged_reaper(stream
, now
);
1812 pine_mail_check(MAILSTREAM
*stream
)
1814 reset_check_point(stream
);
1820 * Unlike mail_list, this version returns a value. The returned value is
1821 * TRUE if the stream is opened ok, and FALSE if we failed opening the
1822 * stream. This allows us to differentiate between a LIST which returns
1823 * no matches and a failure opening the stream. We do this by pre-opening
1824 * the stream ourselves.
1827 pine_mail_list(MAILSTREAM
*stream
, char *ref
, char *pat
, unsigned int *options
)
1830 char *halfopen_target
;
1831 char source
[MAILTMPLEN
], *target
= NULL
;
1832 MAILSTREAM
*ourstream
= NULL
;
1834 dprint((7, "pine_mail_list: ref=%s pat=%s%s\n",
1835 ref
? ref
: "(NULL)",
1836 pat
? pat
: "(NULL)",
1837 stream
? "" : " (stream was NULL)"));
1839 if((!ref
&& check_for_move_mbox(pat
, source
, sizeof(source
), &target
))
1841 check_for_move_mbox(ref
, source
, sizeof(source
), &target
)){
1845 *options
|= PML_IS_MOVE_MBOX
;
1848 "pine_mail_list: #move special case, listing \"%s\"%s\n",
1849 pat
? pat
: "(NULL)",
1850 stream
? "" : " (stream was NULL)"));
1853 if(!stream
&& ((pat
&& *pat
== '{') || (ref
&& *ref
== '{'))){
1855 if(pat
&& *pat
== '{'){
1857 halfopen_target
= pat
;
1860 halfopen_target
= ref
;
1864 long flags
= (OP_HALFOPEN
| OP_SILENT
| SP_USEPOOL
| SP_TEMPUSE
);
1866 stream
= sp_stream_get(halfopen_target
, SP_MATCH
);
1868 stream
= sp_stream_get(halfopen_target
, SP_SAME
);
1874 * POP is a special case. We don't need to have a stream
1875 * to call mail_list for a POP name. The else part is the
1878 if((d
= mail_valid(NIL
, halfopen_target
, (char *) NIL
))
1879 && (d
->flags
& DR_HALFOPEN
)){
1880 stream
= pine_mail_open(NIL
, halfopen_target
, flags
, NULL
);
1889 mail_list_internal(stream
, ref
, pat
);
1892 mail_list_internal(stream
, ref
, pat
);
1895 pine_mail_close(ourstream
);
1902 * mail_list_internal -- A monument to software religion and those who
1903 * would force it upon us.
1906 mail_list_internal(MAILSTREAM
*s
, char *r
, char *p
)
1908 if(F_ON(F_FIX_BROKEN_LIST
, ps_global
)
1909 && ((s
&& s
->mailbox
&& *s
->mailbox
== '{')
1910 || (!s
&& ((r
&& *r
== '{') || (p
&& *p
== '{'))))){
1911 char tmp
[2*MAILTMPLEN
];
1913 snprintf(tmp
, sizeof(tmp
), "%.*s%.*s", sizeof(tmp
)/2-1, r
? r
: "",
1914 sizeof(tmp
)/2-1, p
);
1915 mail_list(s
, "", tmp
);
1923 pine_mail_status(MAILSTREAM
*stream
, char *mailbox
, long int flags
)
1925 return(pine_mail_status_full(stream
, mailbox
, flags
, NULL
, NULL
));
1930 pine_mail_status_full(MAILSTREAM
*stream
, char *mailbox
, long int flags
,
1931 imapuid_t
*uidvalidity
, imapuid_t
*uidnext
)
1933 char source
[MAILTMPLEN
], *target
= NULL
;
1935 MAILSTATUS cache
, status
;
1936 MAILSTREAM
*ourstream
= NULL
;
1938 if(check_for_move_mbox(mailbox
, source
, sizeof(source
), &target
)){
1939 memset(&status
, 0, sizeof(status
));
1940 memset(&cache
, 0, sizeof(cache
));
1942 /* tell mm_status() to write partial return here */
1943 pine_cached_status
= &status
;
1945 flags
|= (SA_UIDVALIDITY
| SA_UIDNEXT
| SA_MESSAGES
);
1947 /* do status of destination */
1949 stream
= sp_stream_get(target
, SP_SAME
);
1951 /* should never be news, don't worry about mulnewrsc flag*/
1952 if((ret
= pine_mail_status_full(stream
, target
, flags
, uidvalidity
,
1956 /* do status of source */
1957 pine_cached_status
= &cache
;
1958 stream
= sp_stream_get(source
, SP_SAME
);
1963 if((d
= mail_valid (NIL
, source
, (char *) NIL
))
1964 && !strcmp(d
->name
, "imap")){
1965 long openflags
=OP_HALFOPEN
|OP_SILENT
|SP_USEPOOL
|SP_TEMPUSE
;
1967 if(F_ON(F_PREFER_ALT_AUTH
, ps_global
))
1968 openflags
|= OP_TRYALT
;
1970 stream
= pine_mail_open(NULL
, source
, openflags
, NULL
);
1973 else if(F_ON(F_ENABLE_MULNEWSRCS
, ps_global
)
1974 && d
&& (!strucmp(d
->name
, "news")
1975 || !strucmp(d
->name
, "nntp")))
1976 flags
|= SA_MULNEWSRC
;
1980 if(!ps_global
->user_says_cancel
&& mail_status(stream
, source
, flags
)){
1985 * If the target has recent messages, then we don't come
1986 * through here. We just use the answer from the target.
1988 * If not, then we leave the target results in "status" and
1989 * run a mail_status on the source that puts its results
1990 * in "cache". Combine the results from cache with the
1991 * results that were already in status.
1993 * We count all messages in the source mailbox as recent and
1994 * unseen, even if they are not recent or unseen in the source,
1995 * because they will be recent and unseen in the target
1996 * when we open it. (Not quite true. It is possible that some
1997 * messages from a POP server will end up seen instead
1999 * It is also possible that it is correct. If we add unseen, or
2000 * if we add messages, we could get it wrong. As far as I
2001 * can tell, Pine doesn't ever even use status.unseen, so it
2002 * is currently academic anyway.) Hubert 2003-03-05
2003 * (Does now 2004-06-02 in next_folder.)
2005 * However, we don't want to count all messages as recent if
2006 * the source mailbox is NNTP or NEWS, because we won't be
2007 * deleting those messages from the source.
2008 * We only count recent.
2010 * There are other cases that are trouble. One in particular
2011 * is an IMAP-to-NNTP proxy, where the messages can't be removed
2012 * from the mailbox but they can be deleted. If we count
2013 * messages in the source as being recent and it turns out they
2014 * were all deleted already, then we incorrectly say the folder
2015 * has recent messages when it doesn't. We can recover from that
2016 * case at some cost by actually opening the folder the first
2017 * time if there are not recents, and then checking to see if
2018 * everything is deleted. Subsequently, we store the uid values
2019 * (which are returned by status) so that we can know if the
2020 * mailbox changed or not. The problem being solved is that
2021 * the TAB command indicates new messages in a folder when there
2022 * really aren't any. An alternative would be to use the is_news
2023 * half of the if-else in all cases. A problem with that is
2024 * that there could be non-recent messages sitting in the
2025 * source mailbox that we never discover. Hubert 2003-03-28
2028 if((d
= mail_valid (NIL
, source
, (char *) NIL
))
2029 && (!strcmp(d
->name
, "nntp") || !strcmp(d
->name
, "news")))
2032 if(is_news
&& cache
.flags
& SA_RECENT
){
2033 status
.messages
+= cache
.recent
;
2034 status
.recent
+= cache
.recent
;
2035 status
.unseen
+= cache
.recent
;
2036 status
.uidnext
+= cache
.recent
;
2039 if(uidvalidity
&& *uidvalidity
2040 && uidnext
&& *uidnext
2041 && cache
.flags
& SA_UIDVALIDITY
2042 && cache
.uidvalidity
== *uidvalidity
2043 && cache
.flags
& SA_UIDNEXT
2044 && cache
.uidnext
== *uidnext
){
2045 ; /* nothing changed in source mailbox */
2047 else if(cache
.flags
& SA_RECENT
&& cache
.recent
){
2048 status
.messages
+= cache
.recent
;
2049 status
.recent
+= cache
.recent
;
2050 status
.unseen
+= cache
.recent
;
2051 status
.uidnext
+= cache
.recent
;
2053 else if(!(cache
.flags
& SA_MESSAGES
) || cache
.messages
){
2054 long openflags
= OP_SILENT
| SP_USEPOOL
| SP_TEMPUSE
;
2055 long delete_count
, not_deleted
= 0L;
2057 /* Actually open it up and check */
2058 if(F_ON(F_PREFER_ALT_AUTH
, ps_global
))
2059 openflags
|= OP_TRYALT
;
2065 && !same_stream_and_mailbox(source
, ourstream
)){
2066 pine_mail_close(ourstream
);
2067 ourstream
= stream
= NULL
;
2071 stream
= pine_mail_open(stream
, source
, openflags
,
2077 delete_count
= count_flagged(stream
, F_DEL
);
2078 not_deleted
= stream
->nmsgs
- delete_count
;
2081 status
.messages
+= not_deleted
;
2082 status
.recent
+= not_deleted
;
2083 status
.unseen
+= not_deleted
;
2084 status
.uidnext
+= not_deleted
;
2087 if(uidvalidity
&& cache
.flags
& SA_UIDVALIDITY
)
2088 *uidvalidity
= cache
.uidvalidity
;
2090 if(uidnext
&& cache
.flags
& SA_UIDNEXT
)
2091 *uidnext
= cache
.uidnext
;
2097 * Do the regular mm_status callback which we've been intercepting
2098 * into different locations above.
2100 pine_cached_status
= NIL
;
2102 mm_status(NULL
, mailbox
, &status
);
2108 if((d
= mail_valid (NIL
, mailbox
, (char *) NIL
))
2109 && !strcmp(d
->name
, "imap")){
2110 long openflags
= OP_HALFOPEN
|OP_SILENT
|SP_USEPOOL
|SP_TEMPUSE
;
2112 if(F_ON(F_PREFER_ALT_AUTH
, ps_global
))
2113 openflags
|= OP_TRYALT
;
2116 * We just use this to find the answer.
2117 * We're asking for trouble if we do a STATUS on a
2118 * selected mailbox. I don't believe this happens in pine.
2119 * It does now (2004-06-02) in next_folder if the
2120 * F_TAB_USES_UNSEEN option is set and the folder was
2123 stream
= sp_stream_get(mailbox
, SP_MATCH
);
2125 memset(&status
, 0, sizeof(status
));
2126 if(flags
& SA_MESSAGES
){
2127 status
.flags
|= SA_MESSAGES
;
2128 status
.messages
= stream
->nmsgs
;
2131 if(flags
& SA_RECENT
){
2132 status
.flags
|= SA_RECENT
;
2133 status
.recent
= stream
->recent
;
2136 if(flags
& SA_UNSEEN
){
2141 srchpgm
= mail_newsearchpgm();
2142 srchpgm
->unseen
= 1;
2144 pine_mail_search_full(stream
, NULL
, srchpgm
,
2145 SE_NOPREFETCH
| SE_FREE
);
2146 status
.flags
|= SA_UNSEEN
;
2148 for(i
= 1L; i
<= stream
->nmsgs
; i
++)
2149 if((mc
= mail_elt(stream
, i
)) && mc
->searched
)
2153 if(flags
& SA_UIDVALIDITY
){
2154 status
.flags
|= SA_UIDVALIDITY
;
2155 status
.uidvalidity
= stream
->uid_validity
;
2158 if(flags
& SA_UIDNEXT
){
2159 status
.flags
|= SA_UIDNEXT
;
2160 status
.uidnext
= stream
->uid_last
+ 1L;
2163 mm_status(NULL
, mailbox
, &status
);
2164 return T
; /* that's what c-client returns when success */
2168 stream
= sp_stream_get(mailbox
, SP_SAME
);
2171 stream
= pine_mail_open(NULL
, mailbox
, openflags
, NULL
);
2175 else if(F_ON(F_ENABLE_MULNEWSRCS
, ps_global
)
2176 && d
&& (!strucmp(d
->name
, "news")
2177 || !strucmp(d
->name
, "nntp")))
2178 flags
|= SA_MULNEWSRC
;
2181 if(!ps_global
->user_says_cancel
)
2182 ret
= mail_status(stream
, mailbox
, flags
); /* non #move case */
2186 pine_mail_close(ourstream
);
2193 * Check for a mailbox name that is a legitimate #move mailbox.
2195 * Args mbox -- The mailbox name to check
2196 * sourcebuf -- Copy the source mailbox name into this buffer
2197 * sbuflen -- Length of sourcebuf
2198 * targetptr -- Set the pointer this points to to point to the
2199 * target mailbox name in the original string
2201 * Returns 1 - is a #move mailbox
2205 check_for_move_mbox(char *mbox
, char *sourcebuf
, size_t sbuflen
, char **targetptr
)
2210 if(mbox
&& (mbox
[0] == '#')
2211 && ((mbox
[1] == 'M') || (mbox
[1] == 'm'))
2212 && ((mbox
[2] == 'O') || (mbox
[2] == 'o'))
2213 && ((mbox
[3] == 'V') || (mbox
[3] == 'v'))
2214 && ((mbox
[4] == 'E') || (mbox
[4] == 'e'))
2215 && (delim
= mbox
[5])
2216 && (s
= strchr(mbox
+6, delim
))
2217 && (i
= s
++ - (mbox
+ 6))
2218 && (!sourcebuf
|| i
< sbuflen
)){
2221 strncpy(sourcebuf
, mbox
+6, i
); /* copy source mailbox name */
2222 sourcebuf
[i
] = '\0';
2236 * Checks through stream cache for a stream pointer already open to
2237 * this mailbox, read/write. Very similar to sp_stream_get, but we want
2238 * to look at all streams, not just imap streams.
2239 * Right now it is very specialized. If we want to use it more generally,
2240 * generalize it or combine it with sp_stream_get somehow.
2243 already_open_stream(char *mailbox
, int flags
)
2251 if(*mailbox
== '{'){
2252 for(i
= 0; i
< ps_global
->s_pool
.nstream
; i
++){
2253 m
= ps_global
->s_pool
.streams
[i
];
2254 if(m
&& !(flags
& AOS_RW_ONLY
&& m
->rdonly
)
2255 && (*m
->mailbox
== '{') && !sp_dead_stream(m
)
2256 && same_stream_and_mailbox(mailbox
, m
))
2261 char *cn
, tmp
[MAILTMPLEN
];
2263 cn
= mailboxfile(tmp
, mailbox
);
2264 for(i
= 0; i
< ps_global
->s_pool
.nstream
; i
++){
2265 m
= ps_global
->s_pool
.streams
[i
];
2266 if(m
&& !(flags
& AOS_RW_ONLY
&& m
->rdonly
)
2267 && (*m
->mailbox
!= '{') && !sp_dead_stream(m
)
2268 && ((cn
&& *cn
&& !strcmp(cn
, m
->mailbox
))
2269 || !strcmp(mailbox
, m
->original_mailbox
)
2270 || !strcmp(mailbox
, m
->mailbox
)))
2280 pine_imap_cmd_happened(MAILSTREAM
*stream
, char *cmd
, long int flags
)
2282 dprint((9, "imap_cmd(%s, %s, 0x%lx)\n",
2283 STREAMNAME(stream
), cmd
? cmd
: "?", flags
));
2285 if(cmd
&& !strucmp(cmd
, "CHECK"))
2286 reset_check_point(stream
);
2288 if(is_imap_stream(stream
)){
2292 sp_set_last_ping(stream
, now
);
2293 sp_set_last_activity(stream
, now
);
2294 if(!(flags
& SC_EXPUNGEDEFERRED
))
2295 sp_set_last_expunged_reaper(stream
, now
);
2301 * Tells us whether we ought to check for a dead stream or not.
2302 * We assume that we ought to check if it is not IMAP and if it is IMAP we
2303 * don't need to check if the last activity was within the last 5 minutes.
2306 recent_activity(MAILSTREAM
*stream
)
2308 if(is_imap_stream(stream
) && !sp_dead_stream(stream
)
2309 && (time(0) - sp_last_activity(stream
) < 5L * 60L))
2316 sp_cleanup_dead_streams(void)
2321 (void) streams_died(); /* tell user in case they don't know yet */
2323 for(i
= 0; i
< ps_global
->s_pool
.nstream
; i
++){
2324 m
= ps_global
->s_pool
.streams
[i
];
2325 if(m
&& sp_dead_stream(m
))
2332 * Returns 0 if stream flags not set, non-zero if they are.
2335 sp_flagged(MAILSTREAM
*stream
, long unsigned int flags
)
2337 return(sp_flags(stream
) & flags
);
2342 sp_set_fldr(MAILSTREAM
*stream
, char *folder
)
2346 pss
= sp_data(stream
);
2349 fs_give((void **) &(*pss
)->fldr
);
2352 (*pss
)->fldr
= cpystr(folder
);
2358 sp_set_saved_cur_msg_id(MAILSTREAM
*stream
, char *id
)
2362 pss
= sp_data(stream
);
2364 if((*pss
)->saved_cur_msg_id
)
2365 fs_give((void **) &(*pss
)->saved_cur_msg_id
);
2368 (*pss
)->saved_cur_msg_id
= cpystr(id
);
2374 * Sets flags absolutely, erasing old flags.
2377 sp_flag(MAILSTREAM
*stream
, long unsigned int flags
)
2382 dprint((9, "sp_flag(%s, 0x%x): %s%s%s%s%s%s%s%s\n",
2383 (stream
&& stream
->mailbox
) ? stream
->mailbox
: "?",
2385 flags
? "set" : "clear",
2386 (flags
& SP_LOCKED
) ? " SP_LOCKED" : "",
2387 (flags
& SP_PERMLOCKED
) ? " SP_PERMLOCKED" : "",
2388 (flags
& SP_INBOX
) ? " SP_INBOX" : "",
2389 (flags
& SP_USERFLDR
) ? " SP_USERFLDR" : "",
2390 (flags
& SP_USEPOOL
) ? " SP_USEPOOL" : "",
2391 (flags
& SP_TEMPUSE
) ? " SP_TEMPUSE" : "",
2392 !flags
? " ALL" : ""));
2394 sp_set_flags(stream
, flags
);
2399 * Clear individual stream flags.
2402 sp_unflag(MAILSTREAM
*stream
, long unsigned int flags
)
2404 if(!stream
|| !flags
)
2407 dprint((9, "sp_unflag(%s, 0x%x): unset%s%s%s%s%s%s\n",
2408 (stream
&& stream
->mailbox
) ? stream
->mailbox
: "?",
2410 (flags
& SP_LOCKED
) ? " SP_LOCKED" : "",
2411 (flags
& SP_PERMLOCKED
) ? " SP_PERMLOCKED" : "",
2412 (flags
& SP_INBOX
) ? " SP_INBOX" : "",
2413 (flags
& SP_USERFLDR
) ? " SP_USERFLDR" : "",
2414 (flags
& SP_USEPOOL
) ? " SP_USEPOOL" : "",
2415 (flags
& SP_TEMPUSE
) ? " SP_TEMPUSE" : ""));
2417 sp_set_flags(stream
, sp_flags(stream
) & ~flags
);
2419 flags
= sp_flags(stream
);
2420 dprint((9, "sp_unflag(%s, 0x%x): result:%s%s%s%s%s%s\n",
2421 (stream
&& stream
->mailbox
) ? stream
->mailbox
: "?",
2423 (flags
& SP_LOCKED
) ? " SP_LOCKED" : "",
2424 (flags
& SP_PERMLOCKED
) ? " SP_PERMLOCKED" : "",
2425 (flags
& SP_INBOX
) ? " SP_INBOX" : "",
2426 (flags
& SP_USERFLDR
) ? " SP_USERFLDR" : "",
2427 (flags
& SP_USEPOOL
) ? " SP_USEPOOL" : "",
2428 (flags
& SP_TEMPUSE
) ? " SP_TEMPUSE" : ""));
2433 * Set dead stream indicator and close if not locked.
2436 sp_mark_stream_dead(MAILSTREAM
*stream
)
2441 dprint((9, "sp_mark_stream_dead(%s)\n",
2442 (stream
&& stream
->mailbox
) ? stream
->mailbox
: "?"));
2445 * If the stream isn't locked, it is no longer useful. Get rid of it.
2447 if(!sp_flagged(stream
, SP_LOCKED
))
2448 pine_mail_actually_close(stream
);
2451 * If it is locked, then we have to worry about references to it
2452 * that still exist. For example, it might be a permlocked stream
2453 * or it might be the current stream. We need to let it be discovered
2454 * by those referencers instead of just eliminating it, so that they
2455 * can clean up the mess they need to clean up.
2457 sp_set_dead_stream(stream
, 1);
2463 * Returns the number of streams in the stream pool which are
2464 * SP_USEPOOL but not SP_PERMLOCKED.
2467 sp_nusepool_notperm(void)
2472 for(i
= 0; i
< ps_global
->s_pool
.nstream
; i
++){
2473 m
= ps_global
->s_pool
.streams
[i
];
2474 if(sp_flagged(m
, SP_USEPOOL
) && !sp_flagged(m
, SP_PERMLOCKED
))
2483 * Returns the number of folders that the user has marked to be PERMLOCKED
2484 * folders (plus INBOX) that are remote IMAP folders.
2486 * This routine depends on the fact that VAR_INBOX_PATH, VAR_PERMLOCKED,
2487 * and the ps_global->context_list are correctly set.
2490 sp_nremote_permlocked(void)
2493 char **lock_these
, *p
= NULL
, *dummy
= NULL
, *lt
;
2496 /* first check if INBOX is remote */
2497 lt
= ps_global
->VAR_INBOX_PATH
;
2498 if(lt
&& (d
=mail_valid(NIL
, lt
, (char *) NIL
))
2499 && !strcmp(d
->name
, "imap"))
2502 /* then count the user-specified permlocked folders */
2503 for(lock_these
= ps_global
->VAR_PERMLOCKED
; lock_these
&& *lock_these
;
2507 * Skip inbox, already done above. Should do this better so that we
2508 * catch the case where the user puts the technical spec of the inbox
2509 * in the list, or where the user lists one folder twice.
2511 if(*lock_these
&& !strucmp(*lock_these
, ps_global
->inbox_name
))
2514 /* there isn't really a pair, it just dequotes for us */
2515 get_pair(*lock_these
, &dummy
, &p
, 0, 0);
2518 * Check to see if this is an incoming nickname and replace it
2519 * with the full name.
2521 if(!(p
&& ps_global
->context_list
2522 && ps_global
->context_list
->use
& CNTXT_INCMNG
2523 && (lt
=folder_is_nick(p
, FOLDERS(ps_global
->context_list
),
2528 fs_give((void **) &dummy
);
2530 if(lt
&& (d
=mail_valid(NIL
, lt
, (char *) NIL
))
2531 && !strcmp(d
->name
, "imap"))
2535 fs_give((void **) &p
);
2543 * Look for an already open stream that can be used for a new purpose.
2544 * (Note that we only look through streams flagged SP_USEPOOL.)
2549 * Flags is a set of values or'd together which tells us what the request
2552 * Returns: a live stream from the stream pool or NULL.
2555 sp_stream_get(char *mailbox
, long unsigned int flags
)
2560 dprint((7, "sp_stream_get(%s):%s%s%s%s%s\n",
2561 mailbox
? mailbox
: "?",
2562 (flags
& SP_MATCH
) ? " SP_MATCH" : "",
2563 (flags
& SP_RO_OK
) ? " SP_RO_OK" : "",
2564 (flags
& SP_SAME
) ? " SP_SAME" : "",
2565 (flags
& SP_UNLOCKED
) ? " SP_UNLOCKED" : "",
2566 (flags
& SP_TEMPUSE
) ? " SP_TEMPUSE" : ""));
2568 /* look for stream already open to this mailbox */
2569 if(flags
& SP_MATCH
){
2570 for(i
= 0; i
< ps_global
->s_pool
.nstream
; i
++){
2571 m
= ps_global
->s_pool
.streams
[i
];
2572 if(m
&& sp_flagged(m
, SP_USEPOOL
)
2573 && (!m
->rdonly
|| (flags
& SP_RO_OK
)) && !sp_dead_stream(m
)
2574 && same_stream_and_mailbox(mailbox
, m
)){
2575 if((sp_flagged(m
, SP_LOCKED
) && recent_activity(m
))
2576 || pine_mail_ping(m
)){
2578 "sp_stream_get: found exact match, slot %d\n", i
));
2579 if(!sp_flagged(m
, SP_LOCKED
)){
2581 "reset idle timer1: next TAG %08lx (%s)\n",
2583 debug_time(1, ps_global
->debug_timestamp
)));
2584 sp_set_last_use_time(m
, time(0));
2590 sp_mark_stream_dead(m
);
2596 * SP_SAME will not match if an SP_MATCH match would have worked.
2597 * If the caller is interested in SP_MATCH streams as well as SP_SAME
2598 * streams then the caller should make two separate calls to this
2601 if(flags
& SP_SAME
){
2603 * If the flags arg does not have either SP_TEMPUSE or SP_UNLOCKED
2604 * set, then we'll accept any stream, even if locked.
2605 * We want to prefer the LOCKED case so that we don't have to ping.
2607 if(!(flags
& SP_UNLOCKED
) && !(flags
& SP_TEMPUSE
)){
2608 for(i
= 0; i
< ps_global
->s_pool
.nstream
; i
++){
2609 m
= ps_global
->s_pool
.streams
[i
];
2610 if(m
&& sp_flagged(m
, SP_USEPOOL
)
2611 && sp_flagged(m
, SP_LOCKED
) && !sp_dead_stream(m
)
2612 && same_stream(mailbox
, m
)
2613 && !same_stream_and_mailbox(mailbox
, m
)){
2614 if(recent_activity(m
) || pine_mail_ping(m
)){
2616 "sp_stream_get: found SAME match, slot %d\n", i
));
2620 sp_mark_stream_dead(m
);
2624 /* consider the unlocked streams */
2625 for(i
= 0; i
< ps_global
->s_pool
.nstream
; i
++){
2626 m
= ps_global
->s_pool
.streams
[i
];
2627 if(m
&& sp_flagged(m
, SP_USEPOOL
)
2628 && !sp_flagged(m
, SP_LOCKED
) && !sp_dead_stream(m
)
2629 && same_stream(mailbox
, m
)
2630 && !same_stream_and_mailbox(mailbox
, m
)){
2631 /* always ping unlocked streams */
2632 if(pine_mail_ping(m
)){
2634 "sp_stream_get: found SAME match, slot %d\n", i
));
2636 "reset idle timer4: next TAG %08lx (%s)\n",
2638 debug_time(1, ps_global
->debug_timestamp
)));
2639 sp_set_last_use_time(m
, time(0));
2644 sp_mark_stream_dead(m
);
2650 * Prefer streams marked SP_TEMPUSE and not LOCKED.
2651 * If SP_TEMPUSE is set in the flags arg then this is the
2654 for(i
= 0; i
< ps_global
->s_pool
.nstream
; i
++){
2655 m
= ps_global
->s_pool
.streams
[i
];
2656 if(m
&& sp_flagged(m
, SP_USEPOOL
) && sp_flagged(m
, SP_TEMPUSE
)
2657 && !sp_flagged(m
, SP_LOCKED
) && !sp_dead_stream(m
)
2658 && same_stream(mailbox
, m
)
2659 && !same_stream_and_mailbox(mailbox
, m
)){
2660 if(pine_mail_ping(m
)){
2662 "sp_stream_get: found SAME/TEMPUSE match, slot %d\n", i
));
2664 "reset idle timer2: next TAG %08lx (%s)\n",
2666 debug_time(1, ps_global
->debug_timestamp
)));
2667 sp_set_last_use_time(m
, time(0));
2671 sp_mark_stream_dead(m
);
2676 * If SP_TEMPUSE is not set in the flags arg but SP_UNLOCKED is,
2677 * then we will consider
2678 * streams which are not marked SP_TEMPUSE (but are still not
2679 * locked). We go through these in reverse order so that we'll get
2680 * the last one added instead of the first one. It's not clear if
2681 * that is a good idea or if a more complex search would somehow
2682 * be better. Maybe we should use a round-robin sort of search
2683 * here so that we don't leave behind unused streams. Or maybe
2684 * we should keep track of when we used it and look for the LRU stream.
2686 if(!(flags
& SP_TEMPUSE
)){
2687 for(i
= ps_global
->s_pool
.nstream
- 1; i
>= 0; i
--){
2688 m
= ps_global
->s_pool
.streams
[i
];
2689 if(m
&& sp_flagged(m
, SP_USEPOOL
)
2690 && !sp_flagged(m
, SP_LOCKED
) && !sp_dead_stream(m
)
2691 && same_stream(mailbox
, m
)
2692 && !same_stream_and_mailbox(mailbox
, m
)){
2693 if(pine_mail_ping(m
)){
2695 "sp_stream_get: found SAME/UNLOCKED match, slot %d\n", i
));
2697 "reset idle timer3: next TAG %08lx (%s)\n",
2699 debug_time(1, ps_global
->debug_timestamp
)));
2700 sp_set_last_use_time(m
, time(0));
2704 sp_mark_stream_dead(m
);
2711 * If we can't find a useful stream to use in pine_mail_open, we may
2712 * want to re-use one that is not actively being used even though it
2713 * is not on the same server. We'll have to close it and then re-use
2716 if(!(flags
& (SP_SAME
| SP_MATCH
))){
2718 * Prefer streams marked SP_TEMPUSE and not LOCKED.
2719 * If SP_TEMPUSE is set in the flags arg then this is the
2722 for(i
= 0; i
< ps_global
->s_pool
.nstream
; i
++){
2723 m
= ps_global
->s_pool
.streams
[i
];
2724 if(m
&& sp_flagged(m
, SP_USEPOOL
) && sp_flagged(m
, SP_TEMPUSE
)
2725 && !sp_flagged(m
, SP_LOCKED
)){
2727 "sp_stream_get: found Not-SAME/TEMPUSE match, slot %d\n", i
));
2729 * We ping it in case there is new mail that we should
2730 * pass through our filters. Pine_mail_actually_close will
2733 (void) pine_mail_ping(m
);
2739 * If SP_TEMPUSE is not set in the flags arg, then we will consider
2740 * streams which are not marked SP_TEMPUSE (but are still not
2741 * locked). Maybe we should use a round-robin sort of search
2742 * here so that we don't leave behind unused streams. Or maybe
2743 * we should keep track of when we used it and look for the LRU stream.
2745 if(!(flags
& SP_TEMPUSE
)){
2746 for(i
= ps_global
->s_pool
.nstream
- 1; i
>= 0; i
--){
2747 m
= ps_global
->s_pool
.streams
[i
];
2748 if(m
&& sp_flagged(m
, SP_USEPOOL
) && !sp_flagged(m
, SP_LOCKED
)){
2750 "sp_stream_get: found Not-SAME/UNLOCKED match, slot %d\n", i
));
2752 * We ping it in case there is new mail that we should
2753 * pass through our filters. Pine_mail_actually_close will
2756 (void) pine_mail_ping(m
);
2763 dprint((7, "sp_stream_get: no match found\n"));
2775 dprint((7, "sp_end\n"));
2777 for(i
= 0; i
< ps_global
->s_pool
.nstream
; i
++){
2778 m
= ps_global
->s_pool
.streams
[i
];
2780 pine_mail_actually_close(m
);
2783 if(ps_global
->s_pool
.streams
)
2784 fs_give((void **) &ps_global
->s_pool
.streams
);
2786 ps_global
->s_pool
.nstream
= 0;
2791 * Find a vacant slot to put this new stream in.
2792 * We are willing to close and kick out another stream as long as it isn't
2793 * LOCKED. However, we may find that there is no place to put this one
2794 * because all the slots are used and locked. For now, we'll return -1
2795 * in that case and leave the new stream out of the pool.
2798 sp_add(MAILSTREAM
*stream
, int usepool
)
2803 dprint((7, "sp_add(%s, %d)\n",
2804 (stream
&& stream
->mailbox
) ? stream
->mailbox
: "?", usepool
));
2807 dprint((7, "sp_add: NULL stream\n"));
2811 /* If this stream is already there, don't add it again */
2812 for(i
= 0; i
< ps_global
->s_pool
.nstream
; i
++){
2813 m
= ps_global
->s_pool
.streams
[i
];
2817 "sp_add: stream was already in slot %d\n", slot
));
2822 if(usepool
&& !sp_flagged(stream
, SP_PERMLOCKED
)
2823 && sp_nusepool_notperm() >= ps_global
->s_pool
.max_remstream
){
2825 "sp_add: reached max implicit SP_USEPOOL of %d\n",
2826 ps_global
->s_pool
.max_remstream
));
2830 /* Look for an unused slot */
2831 for(i
= 0; i
< ps_global
->s_pool
.nstream
; i
++){
2832 m
= ps_global
->s_pool
.streams
[i
];
2836 "sp_add: using empty slot %d\n", slot
));
2841 /* else, allocate more space */
2843 ps_global
->s_pool
.nstream
++;
2844 slot
= ps_global
->s_pool
.nstream
- 1;
2845 if(ps_global
->s_pool
.streams
){
2846 fs_resize((void **) &ps_global
->s_pool
.streams
,
2847 ps_global
->s_pool
.nstream
*
2848 sizeof(*ps_global
->s_pool
.streams
));
2849 ps_global
->s_pool
.streams
[slot
] = NULL
;
2852 ps_global
->s_pool
.streams
=
2853 (MAILSTREAM
**) fs_get(ps_global
->s_pool
.nstream
*
2854 sizeof(*ps_global
->s_pool
.streams
));
2855 memset(ps_global
->s_pool
.streams
, 0,
2856 ps_global
->s_pool
.nstream
*
2857 sizeof(*ps_global
->s_pool
.streams
));
2861 "sp_add: allocate more space, using new slot %d\n", slot
));
2864 if(slot
>= 0 && slot
< ps_global
->s_pool
.nstream
){
2865 ps_global
->s_pool
.streams
[slot
] = stream
;
2869 dprint((7, "sp_add: failed to find a slot!\n"));
2876 * Simply remove this stream from the stream pool.
2879 sp_delete(MAILSTREAM
*stream
)
2887 dprint((7, "sp_delete(%s)\n",
2888 (stream
&& stream
->mailbox
) ? stream
->mailbox
: "?"));
2891 * There are some global stream pointers that we have to worry
2892 * about before deleting the stream.
2895 /* first, mail_stream is the global currently open folder */
2896 if(ps_global
->mail_stream
== stream
)
2897 ps_global
->mail_stream
= NULL
;
2899 /* remote address books may have open stream pointers */
2900 note_closed_adrbk_stream(stream
);
2902 if(pith_opt_closing_stream
)
2903 (*pith_opt_closing_stream
)(stream
);
2905 for(i
= 0; i
< ps_global
->s_pool
.nstream
; i
++){
2906 m
= ps_global
->s_pool
.streams
[i
];
2908 ps_global
->s_pool
.streams
[i
] = NULL
;
2910 "sp_delete: stream removed from slot %d\n", i
));
2918 * Returns 1 if any locked userfldr is dead, 0 if all alive.
2921 sp_a_locked_stream_is_dead(void)
2926 for(i
= 0; !ret
&& i
< ps_global
->s_pool
.nstream
; i
++){
2927 m
= ps_global
->s_pool
.streams
[i
];
2928 if(m
&& sp_flagged(m
, SP_LOCKED
) && sp_flagged(m
, SP_USERFLDR
)
2929 && sp_dead_stream(m
))
2938 * Returns 1 if any locked stream is changed, 0 otherwise
2941 sp_a_locked_stream_changed(void)
2946 for(i
= 0; !ret
&& i
< ps_global
->s_pool
.nstream
; i
++){
2947 m
= ps_global
->s_pool
.streams
[i
];
2948 if(m
&& sp_flagged(m
, SP_LOCKED
) && sp_flagged(m
, SP_USERFLDR
)
2949 && sp_mail_box_changed(m
))
2958 * Returns the inbox stream or NULL.
2961 sp_inbox_stream(void)
2964 MAILSTREAM
*m
, *ret
= NULL
;
2966 for(i
= 0; !ret
&& i
< ps_global
->s_pool
.nstream
; i
++){
2967 m
= ps_global
->s_pool
.streams
[i
];
2968 if(m
&& sp_flagged(m
, SP_INBOX
))
2977 * Make sure that the sp_data per-stream data storage area exists.
2979 * Returns a handle to the sp_data data unless stream is NULL,
2980 * in which case NULL is returned
2983 sp_data(MAILSTREAM
*stream
)
2985 PER_STREAM_S
**pss
= NULL
;
2988 if(*(pss
= (PER_STREAM_S
**) &stream
->sparep
) == NULL
){
2989 *pss
= (PER_STREAM_S
*) fs_get(sizeof(PER_STREAM_S
));
2990 memset(*pss
, 0, sizeof(PER_STREAM_S
));
2991 reset_check_point(stream
);
3000 * Returns a pointer to the msgmap associated with the argument stream.
3002 * If the PER_STREAM_S data or the msgmap does not already exist, it will be
3006 sp_msgmap(MAILSTREAM
*stream
)
3008 MSGNO_S
**msgmap
= NULL
;
3009 PER_STREAM_S
**pss
= NULL
;
3011 pss
= sp_data(stream
);
3014 && (*(msgmap
= (MSGNO_S
**) &(*pss
)->msgmap
) == NULL
))
3015 mn_init(msgmap
, stream
->nmsgs
);
3017 return(msgmap
? *msgmap
: NULL
);
3022 sp_free_callback(void **sparep
)
3025 MAILSTREAM
*stream
= NULL
, *m
;
3028 pss
= (PER_STREAM_S
**) sparep
;
3032 * It is possible that this has been called from c-client when
3033 * we weren't expecting it. We need to clean up the stream pool
3034 * entries if the stream that goes with this pointer is in the
3035 * stream pool somewhere.
3037 for(i
= 0; !stream
&& i
< ps_global
->s_pool
.nstream
; i
++){
3038 m
= ps_global
->s_pool
.streams
[i
];
3039 if(sparep
&& *sparep
&& m
&& m
->sparep
== *sparep
)
3044 if(ps_global
->mail_stream
== stream
)
3045 ps_global
->mail_stream
= NULL
;
3056 * Free the data but don't mess with the stream pool.
3059 sp_free(PER_STREAM_S
**pss
)
3063 if(ps_global
->msgmap
== (*pss
)->msgmap
)
3064 ps_global
->msgmap
= NULL
;
3066 mn_give(&(*pss
)->msgmap
);
3070 fs_give((void **) &(*pss
)->fldr
);
3072 fs_give((void **) pss
);
3078 /*----------------------------------------------------------------------
3079 See if stream can be used for a mailbox name
3081 Accepts: mailbox name
3083 Returns: stream if it can be used, else NIL
3085 This is called to weed out unnecessary use of c-client streams. In other
3086 words, to help facilitate re-use of streams.
3088 This code is very similar to the same_remote_mailboxes code below, which
3089 is used in pine_mail_open. That code compares two mailbox names. One is
3090 usually from the config file and the other is either from the config file
3091 or is typed in. Here and in same_stream_and_mailbox below, we're comparing
3092 an open stream to a name instead of two names. We could conceivably use
3093 same_remote_mailboxes to compare stream->mailbox to name, but it isn't
3094 exactly the same and the differences may be important. Some stuff that
3095 happens here seems wrong, but it isn't easy to fix.
3096 Having !mb_n.port count as a match to any mb_s.port isn't right. It should
3097 only match if mb_s.port is equal to the default, but the default isn't
3098 something that is available to us. The same thing is done in c-client in
3099 the mail_usable_network_stream() routine, and it isn't right there, either.
3100 The semantics of a missing user are also suspect, because just like with
3101 port, a default is used.
3104 same_stream(char *name
, MAILSTREAM
*stream
)
3106 NETMBX mb_s
, mb_n
, mb_o
;
3108 if(stream
&& stream
->mailbox
&& *stream
->mailbox
&& name
&& *name
3109 && !(sp_dead_stream(stream
))
3110 && mail_valid_net_parse(stream
->mailbox
, &mb_s
)
3111 && mail_valid_net_parse(stream
->original_mailbox
, &mb_o
)
3112 && mail_valid_net_parse(name
, &mb_n
)
3113 && !strucmp(mb_n
.service
, mb_s
.service
)
3114 && (!strucmp(mb_n
.host
, mb_o
.host
) /* s is already canonical */
3115 || !strucmp(canonical_name(mb_n
.host
), mb_s
.host
))
3116 && (!mb_n
.port
|| mb_n
.port
== mb_s
.port
)
3117 && mb_n
.anoflag
== stream
->anonymous
3118 && ((mb_n
.user
&& *mb_n
.user
&&
3119 mb_s
.user
&& !strcmp(mb_n
.user
, mb_s
.user
))
3121 ((!mb_n
.user
|| !*mb_n
.user
)
3123 && ((ps_global
->VAR_USER_ID
3124 && !strcmp(ps_global
->VAR_USER_ID
, mb_s
.user
))
3126 (!ps_global
->VAR_USER_ID
3127 && ps_global
->ui
.login
[0]
3128 && !strcmp(ps_global
->ui
.login
, mb_s
.user
))))
3130 (!((mb_n
.user
&& *mb_n
.user
) || (mb_s
.user
&& *mb_s
.user
))
3131 && stream
->anonymous
))
3132 && (struncmp(mb_n
.service
, "imap", 4) ? 1 : strcmp(imap_host(stream
), ".NO-IMAP-CONNECTION."))){
3133 dprint((7, "same_stream: name->%s == stream->%s: yes\n",
3135 (stream
&& stream
->mailbox
) ? stream
->mailbox
: "NULL"));
3139 dprint((7, "same_stream: name->%s == stream->%s: no dice\n",
3141 (stream
&& stream
->mailbox
) ? stream
->mailbox
: "NULL"));
3147 /*----------------------------------------------------------------------
3148 See if this stream has the named mailbox selected.
3150 Accepts: mailbox name
3152 Returns: stream if it can be used, else NIL
3155 same_stream_and_mailbox(char *name
, MAILSTREAM
*stream
)
3159 if(same_stream(name
, stream
)
3160 && mail_valid_net_parse(stream
->mailbox
, &mb_s
)
3161 && mail_valid_net_parse(name
, &mb_n
)
3162 && (mb_n
.mailbox
&& mb_s
.mailbox
3163 && (!strcmp(mb_n
.mailbox
,mb_s
.mailbox
) /* case depend except INBOX */
3164 || (!strucmp(mb_n
.mailbox
,"INBOX")
3165 && !strucmp(mb_s
.mailbox
,"INBOX"))))){
3167 "same_stream_and_mailbox: name->%s == stream->%s: yes\n",
3169 (stream
&& stream
->mailbox
) ? stream
->mailbox
: "NULL"));
3174 "same_stream_and_mailbox: name->%s == stream->%s: no dice\n",
3176 (stream
&& stream
->mailbox
) ? stream
->mailbox
: "NULL"));
3181 * Args -- name1 and name2 are remote mailbox names.
3183 * Returns -- True if names refer to same mailbox accessed in same way
3186 * This has some very similar code to same_stream_and_mailbox but we're not
3187 * quite ready to discard the differences.
3188 * The treatment of the port and the user is not quite the same.
3191 same_remote_mailboxes(char *name1
, char *name2
)
3197 * Probably we should allow !port equal to default port, but we don't
3198 * know how to get the default port. To match what c-client does we
3199 * allow !port to be equal to anything.
3201 return(name1
&& IS_REMOTE(name1
)
3202 && name2
&& IS_REMOTE(name2
)
3203 && mail_valid_net_parse(name1
, &mb1
)
3204 && mail_valid_net_parse(name2
, &mb2
)
3205 && !strucmp(mb1
.service
, mb2
.service
)
3206 && (!strucmp(mb1
.host
, mb2
.host
) /* just to save DNS lookups */
3207 || !strucmp(cn1
=canonical_name(mb1
.host
), mb2
.host
)
3208 || !strucmp(cn1
, canonical_name(mb2
.host
)))
3209 && (!mb1
.port
|| !mb2
.port
|| mb1
.port
== mb2
.port
)
3210 && mb1
.anoflag
== mb2
.anoflag
3211 && mb1
.mailbox
&& mb2
.mailbox
3212 && (!strcmp(mb1
.mailbox
, mb2
.mailbox
)
3213 || (!strucmp(mb1
.mailbox
,"INBOX")
3214 && !strucmp(mb2
.mailbox
,"INBOX")))
3215 && ((mb1
.user
&& *mb1
.user
&& mb2
.user
&& *mb2
.user
3216 && !strcmp(mb1
.user
, mb2
.user
))
3218 (!(mb1
.user
&& *mb1
.user
) && !(mb2
.user
&& *mb2
.user
))
3220 (!(mb1
.user
&& *mb1
.user
)
3221 && ((ps_global
->VAR_USER_ID
3222 && !strcmp(ps_global
->VAR_USER_ID
, mb2
.user
))
3224 (!ps_global
->VAR_USER_ID
3225 && ps_global
->ui
.login
[0]
3226 && !strcmp(ps_global
->ui
.login
, mb2
.user
))))
3228 (!(mb2
.user
&& *mb2
.user
)
3229 && ((ps_global
->VAR_USER_ID
3230 && !strcmp(ps_global
->VAR_USER_ID
, mb1
.user
))
3232 (!ps_global
->VAR_USER_ID
3233 && ps_global
->ui
.login
[0]
3234 && !strcmp(ps_global
->ui
.login
, mb1
.user
))))));
3239 is_imap_stream(MAILSTREAM
*stream
)
3241 return(stream
&& stream
->dtb
&& stream
->dtb
->name
3242 && !strcmp(stream
->dtb
->name
, "imap"));
3247 modern_imap_stream(MAILSTREAM
*stream
)
3249 return(is_imap_stream(stream
) && LEVELIMAP4rev1(stream
));
3253 /*----------------------------------------------------------------------
3254 Check and see if all the stream are alive
3256 Returns: 0 if there was no change
3257 >0 if streams have died since last call
3259 Also outputs a message that the streams have died
3267 unsigned char *folder
;
3269 for(i
= 0; i
< ps_global
->s_pool
.nstream
; i
++){
3270 m
= ps_global
->s_pool
.streams
[i
];
3271 if(m
&& sp_dead_stream(m
)){
3272 if(sp_flagged(m
, SP_LOCKED
) && sp_flagged(m
, SP_USERFLDR
)){
3273 if(!sp_noticed_dead_stream(m
)){
3275 sp_set_noticed_dead_stream(m
, 1);
3276 folder
= folder_name_decoded((unsigned char *)STREAMNAME(m
));
3277 q_status_message1(SM_ORDER
| SM_DING
, 3, 3,
3278 _("MAIL FOLDER \"%s\" CLOSED DUE TO ACCESS ERROR"),
3279 short_str(pretty_fn(folder
) ? pretty_fn(folder
) : "?",
3280 tmp_20k_buf
+1000, SIZEOF_20KBUF
-1000, 35, FrontDots
));
3281 dprint((6, "streams_died: locked: \"%s\"\n",
3284 snprintf(tmp_20k_buf
, SIZEOF_20KBUF
, _("Folder \"%s\" is Closed"),
3285 short_str(pretty_fn(folder
) ? pretty_fn(folder
) : "?",
3286 tmp_20k_buf
+1000, SIZEOF_20KBUF
-1000, 35, FrontDots
));
3287 if(pith_opt_icon_text
)
3288 (*pith_opt_icon_text
)(tmp_20k_buf
, IT_MCLOSED
);
3290 if(folder
) fs_give((void **)&folder
);
3294 if(!sp_noticed_dead_stream(m
)){
3295 sp_set_noticed_dead_stream(m
, 1);
3296 folder
= (unsigned char *) STREAMNAME(m
);
3298 * If a cached stream died and then we tried to use it
3299 * it could cause problems. We could warn about it here
3300 * but it may be confusing because it might be
3301 * unrelated to what the user is doing and not cause
3302 * any problem at all.
3305 if(sp_flagged(m
, SP_USEPOOL
))
3306 q_status_message(SM_ORDER
, 3, 3,
3307 "Warning: Possible problem accessing remote data, connection died.");
3310 dprint((6, "streams_died: not locked: \"%s\"\n",
3314 pine_mail_actually_close(m
);
3323 /* Some stream is locked checks to see if there is any stream for which we
3324 * are in a callback from c-client
3328 some_stream_is_locked(void)
3333 for(i
= 0; rv
== 0 && i
< ps_global
->s_pool
.nstream
; i
++){
3334 m
= ps_global
->s_pool
.streams
[i
];
3343 * Very simple version of appenduid_cb until we need something
3347 static imapuid_t last_append_uid
;
3350 appenduid_cb(char *mailbox
,unsigned long uidvalidity
, SEARCHSET
*set
)
3352 last_append_uid
= set
? set
->first
: 0L;
3357 get_last_append_uid(void)
3359 return last_append_uid
;
3364 * mail_cmd_stream - return a stream suitable for mail_lsub,
3365 * mail_subscribe, and mail_unsubscribe
3369 mail_cmd_stream(CONTEXT_S
*context
, int *closeit
)
3371 char tmp
[MAILTMPLEN
];
3374 (void) context_apply(tmp
, context
, "x", sizeof(tmp
));
3376 return(pine_mail_open(NULL
, tmp
,
3377 OP_HALFOPEN
| OP_SILENT
| SP_USEPOOL
| SP_TEMPUSE
,
3383 * This is so we can replace the old rfc822_ routines like rfc822_header_line
3384 * with the new version that checks bounds, like rfc822_output_header_line.
3385 * This routine is called when would be a bounds overflow, which we simply log
3386 * and go on with the truncated data.
3389 dummy_soutr(void *stream
, char *string
)
3391 dprint((2, "dummy_soutr unexpected call, caught overflow\n"));