* new version 2.20.9.
[alpine.git] / pith / stream.c
blobcfdcc9a5cbe076111a88235c4b6063d8a3b6511b
1 #if !defined(lint) && !defined(DOS)
2 static char rcsid[] = "$Id: stream.c 1012 2008-03-26 00:44:22Z hubert@u.washington.edu $";
3 #endif
5 /*
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 /*======================================================================
20 stream.c
21 Implements the Pine mail stream management routines
22 and c-client wrapper functions
23 ====*/
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 *);
49 * Internal prototypes
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
81 * that goes with it.
83 * Args:
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
91 * we got here.
93 MAILSTREAM *
94 pine_mail_open(MAILSTREAM *stream, char *mailbox, long int openflags, long int *retflags)
96 MAILSTREAM *retstream = NULL;
97 DRIVER *d;
98 int permlocked = 0, is_inbox = 0, usepool = 0, tempuse = 0, uf = 0;
99 unsigned long flags;
100 char **lock_these, *target = NULL;
101 static unsigned long streamcounter = 0;
103 dprint((7,
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)",
107 openflags,
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)));
119 if(retflags)
120 *retflags = 0L;
122 if(ps_global->user_says_cancel){
123 dprint((7, "pine_mail_open: cancelled by user\n"));
124 return(retstream);
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))))
149 lt = p;
151 if(dummy)
152 fs_give((void **) &dummy);
154 if(lt && mailbox
155 && (same_remote_mailboxes(mailbox, lt)
157 (!IS_REMOTE(mailbox)
158 && (lname=mailboxfile(tmp1, lt))
159 && (mname=mailboxfile(tmp2, mailbox))
160 && !strcmp(lname, mname))))
161 permlocked++;
163 if(p)
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;
181 else{
182 if(IS_REMOTE(mailbox)){
183 dprint((9, "pine_mail_open: not cacheable: %s\n", !d ? "no driver?" : d->name ? d->name : "?" ));
185 else{
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
192 * below).
194 dprint((9,
195 "pine_mail_open: not cacheable: not remote, but check for local stream\n"));
197 else{
198 dprint((9,
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;
207 dprint((9,
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);
218 #ifdef DEBUG
219 if(ps_global->debug_imap > 3 || ps_global->debugmem)
220 openflags |= OP_DEBUG;
221 #endif
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)){
232 DRIVER *d;
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.
264 if(stream){
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)){
269 dprint((7,
270 "pine_mail_open: closing passed in IMAP stream %s\n",
271 stream->mailbox ? stream->mailbox : "?"));
273 else{
274 dprint((7,
275 "pine_mail_open: closing passed in non-IMAP stream %s\n",
276 stream->mailbox ? stream->mailbox : "?"));
279 pine_mail_close(stream);
280 stream = NULL;
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.
292 target = NULL;
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);
298 if(targetstream){
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)))){
308 if(retstream)
309 stream = retstream;
310 else
311 stream = sp_stream_get(mailbox,
312 SP_MATCH | ((openflags & OP_READONLY) ? SP_RO_OK : 0));
313 if(stream){
314 flags = SP_LOCKED
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);
329 if(retflags){
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);
339 dprint((7,
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);
345 dprint((7,
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",
359 stream->gensym));
362 return(stream);
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.
372 if(!permlocked)
373 stream = sp_stream_get(mailbox,
374 SP_MATCH | ((openflags & OP_READONLY) ? SP_RO_OK : 0));
376 if(stream){
377 flags = SP_LOCKED
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);
392 if(retflags){
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);
402 dprint((7,
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);
408 dprint((7,
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",
431 stream->gensym));
434 return(stream);
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);
442 if(stream){
443 dprint((9,
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.
452 else if(!permlocked
453 && sp_nusepool_notperm() >= ps_global->s_pool.max_remstream){
454 dprint((9,
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);
461 if(stream){
463 * We will close this stream and use the empty slot
464 * that that creates.
466 dprint((9,
467 "pine_mail_open: close a TEMPUSE stream and re-use that slot\n"));
468 pine_mail_actually_close(stream);
469 stream = NULL;
471 else{
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);
480 if(stream){
481 dprint((9,
482 "pine_mail_open: attempting to re-use stream\n"));
484 else{
486 * We'll take any UNLOCKED stream and re-use it.
488 stream = sp_stream_get(mailbox, 0);
489 if(stream){
491 * We will close this stream and use the empty slot
492 * that that creates.
494 dprint((9,
495 "pine_mail_open: close an UNLOCKED stream and re-use the slot\n"));
496 pine_mail_actually_close(stream);
497 stream = NULL;
499 else{
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"));
503 else{
504 dprint((9, "pine_mail_open: no caching, max_remstream == 0\n"));
507 usepool = 0;
508 tempuse = 0;
509 if(permlocked){
510 permlocked = 0;
511 dprint((2,
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));
521 else{
522 dprint((9,
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){
534 dprint((9,
535 "pine_mail_open: asked for HALFOPEN so returning stream as is\n"));
536 sp_set_ref_cnt(stream, sp_ref_cnt(stream)+1);
537 dprint((7,
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",
543 stream->gensym));
546 if(stream->silent && !(openflags & OP_SILENT))
547 stream->silent = NIL;
549 return(stream);
553 * We're going to SELECT another folder with this open stream.
555 if(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
560 * for that new mail.
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")){
570 dprint((7,
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
579 * the new folder.
581 sp_free((PER_STREAM_S **) &stream->sparep);
586 * When we pass a stream to mail_open, it will either re-use it or
587 * close it.
589 retstream = mail_open(stream, mailbox, openflags);
591 if(retstream){
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",
610 retstream->gensym));
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
624 * rare.
626 if(IS_NEWS(retstream)){
627 usepool = 0;
628 tempuse = 0;
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);
643 flags = SP_LOCKED
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){
659 usepool = 0;
660 tempuse = 0;
661 flags = SP_LOCKED
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)){
685 char *seq;
686 long i, mflags = ST_SET;
687 MESSAGECACHE *mc;
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)
697 mc->sequence = 1;
698 else
699 mc->sequence = 0;
701 if(!is_imap_stream(retstream))
702 mflags |= ST_SILENT;
703 if((seq = build_sequence(retstream, NULL, NULL)) != NULL){
704 mail_flag(retstream, seq, "\\SEEN", mflags);
705 fs_give((void **)&seq);
710 return(retstream);
714 void
715 reset_stream_view_state(MAILSTREAM *stream)
717 MSGNO_S *mm;
719 if(!stream)
720 return;
722 mm = sp_msgmap(stream);
724 if(!mm)
725 return;
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;
733 mm->top = 0L;
734 mm->max_thrdno = 0L;
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)){
745 long i;
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
754 * it here.
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
764 * SP_PERMLOCKED
765 * SP_USERFLDR
766 * SP_INBOX
767 * We allow turning off (but not on) SP_TEMPUSE
769 void
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))
775 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))
784 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.
808 long
809 pine_mail_create(MAILSTREAM *stream, char *mailbox)
811 MAILSTREAM *ourstream = NULL;
812 long return_val;
813 long openflags = (OP_HALFOPEN | OP_SILENT | SP_USEPOOL | SP_TEMPUSE);
814 char source[MAILTMPLEN], *target = NULL;
815 DRIVER *d;
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)){
822 mailbox = target;
823 dprint((7,
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;
843 if(!stream)
844 stream = sp_stream_get(mailbox, SP_MATCH);
845 if(!stream)
846 stream = sp_stream_get(mailbox, SP_SAME);
848 if(!stream){
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);
856 ourstream = stream;
860 return_val = mail_create(stream, mailbox);
862 if(ourstream)
863 pine_mail_close(ourstream);
865 return(return_val);
870 * Pine wrapper around mail_delete.
872 long
873 pine_mail_delete(MAILSTREAM *stream, char *mailbox)
875 MAILSTREAM *ourstream = NULL;
876 long return_val;
877 long openflags = (OP_HALFOPEN | OP_SILENT | SP_USEPOOL | SP_TEMPUSE);
878 char source[MAILTMPLEN], *target = NULL;
879 DRIVER *d;
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)){
886 mailbox = target;
887 dprint((7,
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);
909 stream = NULL;
912 if(!stream)
913 stream = sp_stream_get(mailbox, SP_SAME);
915 if(!stream){
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);
923 ourstream = stream;
927 return_val = mail_delete(stream, mailbox);
929 if(ourstream)
930 pine_mail_close(ourstream);
932 return(return_val);
937 * Pine wrapper around mail_append.
939 long
940 pine_mail_append_full(MAILSTREAM *stream, char *mailbox, char *flags, char *date, STRING *message)
942 MAILSTREAM *ourstream = NULL;
943 long return_val;
944 long openflags = (OP_HALFOPEN | OP_SILENT | SP_USEPOOL | SP_TEMPUSE);
945 char source[MAILTMPLEN], *target = NULL;
946 char mailbox_nodelim[MAILTMPLEN];
947 int delim;
948 DRIVER *d;
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'){
956 size_t len;
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)){
967 mailbox = target;
968 dprint((7,
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;
987 if(!stream)
988 stream = sp_stream_get(mailbox, SP_MATCH);
989 if(!stream)
990 stream = sp_stream_get(mailbox, SP_SAME);
992 if(!stream){
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);
1000 ourstream = stream;
1004 return_val = mail_append_full(stream, mailbox, flags, date, message);
1006 if(ourstream)
1007 pine_mail_close(ourstream);
1009 return(return_val);
1014 * Pine wrapper around mail_append.
1016 long
1017 pine_mail_append_multiple(MAILSTREAM *stream, char *mailbox, append_t af,
1018 APPENDPACKAGE *data, MAILSTREAM *not_this_stream)
1020 MAILSTREAM *ourstream = NULL;
1021 long return_val;
1022 long openflags = (OP_HALFOPEN | OP_SILENT | SP_USEPOOL | SP_TEMPUSE);
1023 char source[MAILTMPLEN], *target = NULL;
1024 DRIVER *d;
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)){
1032 mailbox = target;
1033 dprint((7,
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)){
1063 we_blocked_reuse++;
1064 sp_unflag(not_this_stream, SP_USEPOOL);
1067 if(!stream)
1068 stream = sp_stream_get(mailbox, SP_MATCH);
1069 if(!stream)
1070 stream = sp_stream_get(mailbox, SP_SAME);
1072 if(!stream){
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);
1080 ourstream = stream;
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);
1089 if(ourstream)
1090 pine_mail_close(ourstream);
1092 return(return_val);
1097 * Pine wrapper around mail_copy.
1099 long
1100 pine_mail_copy_full(MAILSTREAM *stream, char *sequence, char *mailbox, long int options)
1102 MAILSTREAM *ourstream = NULL;
1103 long return_val;
1104 long openflags = (OP_HALFOPEN | OP_SILENT | SP_USEPOOL | SP_TEMPUSE);
1105 char source[MAILTMPLEN], *target = NULL;
1106 char mailbox_nodelim[MAILTMPLEN];
1107 int delim;
1108 DRIVER *d;
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'){
1116 size_t len;
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)){
1127 mailbox = target;
1128 dprint((7,
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;
1147 if(!stream)
1148 stream = sp_stream_get(mailbox, SP_MATCH);
1149 if(!stream)
1150 stream = sp_stream_get(mailbox, SP_SAME);
1152 if(!stream){
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);
1164 ourstream = stream;
1168 return_val = mail_copy_full(stream, sequence, mailbox, options);
1170 if(ourstream)
1171 pine_mail_close(ourstream);
1173 return(return_val);
1178 * Pine wrapper around mail_rename.
1180 long
1181 pine_mail_rename(MAILSTREAM *stream, char *old, char *new)
1183 MAILSTREAM *ourstream = NULL;
1184 long return_val;
1185 long openflags = (OP_HALFOPEN | OP_SILENT | SP_USEPOOL | SP_TEMPUSE);
1186 DRIVER *d;
1188 dprint((7, "pine_mail_rename(%s,%s)\n", old ? old : "",
1189 new ? new : ""));
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);
1208 stream = NULL;
1211 if(!stream)
1212 stream = sp_stream_get(old, SP_SAME);
1214 if(!stream){
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);
1222 ourstream = stream;
1226 return_val = mail_rename(stream, old, new);
1228 if(ourstream)
1229 pine_mail_close(ourstream);
1231 return(return_val);
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.
1238 ----*/
1239 void
1240 pine_mail_close(MAILSTREAM *stream)
1242 unsigned long uid_last, last_uid;
1243 int refcnt;
1245 if(!stream)
1246 return;
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.
1262 #if 0
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);
1267 else{
1268 dprint((7,
1269 "pine_mail_close: dont think we need to checkpoint\n"));
1272 #endif
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.
1291 if(refcnt > 0)
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));
1296 if(refcnt <= 0){
1297 dprint((7,
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);
1312 else{
1313 dprint((7, "pine_mail_close: ref cnt is now %d\n",
1314 refcnt));
1317 else{
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.
1328 if(refcnt > 0)
1329 sp_set_ref_cnt(stream, refcnt-1);
1331 refcnt = sp_ref_cnt(stream);
1332 if(refcnt <= 0){
1333 pine_mail_actually_close(stream);
1335 else{
1336 dprint((7, "pine_mail_close: ref cnt is now %d\n",
1337 refcnt));
1344 void
1345 pine_mail_actually_close(MAILSTREAM *stream)
1347 if(!stream)
1348 return;
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));
1363 sp_delete(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.
1369 mail_close(stream);
1375 * If we haven't used a stream for a while, we may want to logout.
1377 void
1378 maybe_kill_old_stream(MAILSTREAM *stream)
1380 #define KILL_IF_IDLE_TIME (25 * 60)
1381 if(stream
1382 && !sp_flagged(stream, SP_LOCKED)
1383 && !sp_flagged(stream, SP_USERFLDR)
1384 && time(0) - sp_last_use_time(stream) > KILL_IF_IDLE_TIME){
1386 dprint((7,
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.)
1405 long
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
1414 * parameter.
1416 if(charset && !strucmp(charset, "utf-8")
1417 && is_imap_stream(stream) && !hibit_in_searchpgm(pgm))
1418 charset = NULL;
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)
1430 SEARCHOR *or;
1431 SEARCHPGMLIST *not;
1433 if(!pgm)
1434 return 0;
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)))
1453 return 1;
1455 for(or = pgm->or; or; or = or->next)
1456 if(hibit_in_searchpgm(or->first) || hibit_in_searchpgm(or->second))
1457 return 1;
1459 for(not = pgm->not; not; not = not->next)
1460 if(hibit_in_searchpgm(not->pgm))
1461 return 1;
1463 return 0;
1468 hibit_in_strlist(STRINGLIST *sl)
1470 for(; sl; sl = sl->next)
1471 if(hibit_in_sizedtext(&sl->text))
1472 return 1;
1474 return 0;
1479 hibit_in_header(SEARCHHEADER *header)
1481 SEARCHHEADER *hdr;
1483 for(hdr = header; hdr; hdr = hdr->next)
1484 if(hibit_in_sizedtext(&hdr->line) || hibit_in_sizedtext(&hdr->text))
1485 return 1;
1487 return 0;
1492 hibit_in_sizedtext(SIZEDTEXT *st)
1494 unsigned char *p, *end;
1496 p = st ? st->data : NULL;
1497 if(p)
1498 for(end = p + st->size; p < end; p++)
1499 if(*p & 0x80)
1500 return 1;
1502 return 0;
1506 void
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;
1515 ENVELOPE *
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;
1525 return(env);
1529 ENVELOPE *
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;
1540 return(env);
1544 ENVELOPE *
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;
1554 return(env);
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.
1563 char *
1564 pine_mail_fetch_body(MAILSTREAM *stream, long unsigned int msgno, char *section,
1565 long unsigned int *len, long int flags)
1567 #ifdef _WINDOWS
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));
1571 else
1572 #endif
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.
1581 char *
1582 pine_mail_fetch_text(MAILSTREAM *stream, long unsigned int msgno, char *section,
1583 long unsigned int *len, long int flags)
1585 #ifdef _WINDOWS
1586 if(F_ON(F_QUELL_SSL_LARGEBLOCKS, ps_global))
1587 return(pine_mail_partial_fetch_wrapper(stream, msgno,
1588 section, len, flags,
1589 0, NULL, 1));
1590 else
1591 #endif
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
1604 * get_n_bytes).
1605 * is_text_fetch -
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
1609 char *
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)
1615 BODY *body;
1616 unsigned long size, firstbyte, lastbyte;
1617 void *old_gets;
1618 FETCH_READC_S *pftc;
1619 char imap_cache_section[MAILTMPLEN];
1620 SIZEDTEXT new_text;
1621 MESSAGECACHE *mc;
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;
1629 if(str_to_free)
1630 *str_to_free = NULL;
1631 #ifdef _WINDOWS
1632 if(F_ON(F_QUELL_SSL_LARGEBLOCKS, ps_global) || get_n_bytes){
1633 #else
1634 if(get_n_bytes){
1635 #endif /* _WINDOWS */
1636 if(section && *section)
1637 body = mail_body(stream, msgno, (unsigned char *) section);
1638 else
1639 pine_mail_fetch_structure(stream, msgno, &body, flags);
1640 if(!body)
1641 return NULL;
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
1658 if(is_text_fetch)
1659 snprintf(imap_cache_section, sizeof(imap_cache_section), "%.*s%sTEXT", MAILTMPLEN - 10,
1660 section && *section ? section : "",
1661 section && *section ? "." : "");
1662 else
1663 snprintf(imap_cache_section, sizeof(imap_cache_section), "%.*s", MAILTMPLEN - 10,
1664 section && *section ? section : "");
1666 if(modern_imap_stream(stream)
1667 #ifdef _WINDOWS
1668 && ((size > AVOID_MICROSOFT_SSL_CHUNKING_BUG)
1669 || (get_n_bytes && size > get_n_bytes))
1670 #else
1671 && (get_n_bytes && size > get_n_bytes)
1672 #endif /* _WINDOWS */
1673 && !imap_cache(stream, msgno, imap_cache_section,
1674 NULL, NULL)){
1675 if(get_n_bytes == 0){
1676 dprint((8,
1677 "fetch_wrapper: doing partial fetching to work around microsoft bug\n"));
1679 else{
1680 dprint((8,
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));
1685 #ifdef _WINDOWS
1686 if(F_ON(F_QUELL_SSL_LARGEBLOCKS, ps_global)){
1687 if(get_n_bytes)
1688 pftc->chunksize = MIN(get_n_bytes,
1689 AVOID_MICROSOFT_SSL_CHUNKING_BUG);
1690 else
1691 pftc->chunksize = AVOID_MICROSOFT_SSL_CHUNKING_BUG;
1693 else
1694 #endif /* _WINDOWS */
1695 pftc->chunksize = get_n_bytes;
1697 pftc->chunk = (char *) fs_get((pftc->chunksize+1)
1698 * sizeof(char));
1699 pftc->cache = so_get(CharStar, NULL, EDIT_ACCESS);
1700 pftc->read = 0L;
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)
1716 break;
1717 } while((pftc->read == lastbyte)
1718 && (!get_n_bytes || (pftc->read < get_n_bytes)));
1719 dprint((8,
1720 "fetch_wrapper: anticipated size=%lu read=%lu\n",
1721 size, pftc->read));
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
1729 * partial text.
1731 if(!str_to_free)
1732 alpine_panic("Programmer botch: partial fetch attempt w/o string pointer");
1733 else
1734 *str_to_free = (char *) new_text.data;
1736 else {
1737 /* ugh, rewrite string in case it got stomped on last call */
1738 if(is_text_fetch)
1739 snprintf(imap_cache_section, sizeof(imap_cache_section), "%.*s%sTEXT", MAILTMPLEN - 10,
1740 section && *section ? section : "",
1741 section && *section ? "." : "");
1742 else
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);
1753 g_pft_desc = NULL;
1754 if(len)
1755 *len = new_text.size;
1756 return ((char *)new_text.data);
1758 else
1759 return((*fetch_full)(stream, msgno, section, len, flags));
1761 else
1762 return((*fetch_full)(stream, msgno, section, len, flags));
1766 * c-client callback that handles getting the text
1768 char *
1769 partial_text_gets(readfn_t f, void *stream, long unsigned int size, GETS_DATA *md)
1771 unsigned long n;
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);
1782 return(NULL);
1787 * Pings the stream. Returns 0 if the stream is dead, non-zero otherwise.
1789 long
1790 pine_mail_ping(MAILSTREAM *stream)
1792 time_t now;
1793 long ret = 0L;
1795 if(!sp_dead_stream(stream)){
1796 ret = mail_ping(stream);
1797 if(ret && sp_dead_stream(stream))
1798 ret = 0L;
1801 if(ret){
1802 now = time(0);
1803 sp_set_last_ping(stream, now);
1804 sp_set_last_expunged_reaper(stream, now);
1807 return(ret);
1811 void
1812 pine_mail_check(MAILSTREAM *stream)
1814 reset_check_point(stream);
1815 mail_check(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)
1829 int we_open = 0;
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)){
1842 ref = NIL;
1843 pat = target;
1844 if(options)
1845 *options |= PML_IS_MOVE_MBOX;
1847 dprint((7,
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 == '{'))){
1854 we_open++;
1855 if(pat && *pat == '{'){
1856 ref = NIL;
1857 halfopen_target = pat;
1859 else
1860 halfopen_target = ref;
1863 if(we_open){
1864 long flags = (OP_HALFOPEN | OP_SILENT | SP_USEPOOL | SP_TEMPUSE);
1866 stream = sp_stream_get(halfopen_target, SP_MATCH);
1867 if(!stream)
1868 stream = sp_stream_get(halfopen_target, SP_SAME);
1870 if(!stream){
1871 DRIVER *d;
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
1876 * POP part.
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);
1881 ourstream = stream;
1882 if(!stream)
1883 return(FALSE);
1885 else
1886 stream = NULL;
1889 mail_list_internal(stream, ref, pat);
1891 else
1892 mail_list_internal(stream, ref, pat);
1894 if(ourstream)
1895 pine_mail_close(ourstream);
1897 return(TRUE);
1902 * mail_list_internal -- A monument to software religion and those who
1903 * would force it upon us.
1905 void
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);
1917 else
1918 mail_list(s, r, p);
1922 long
1923 pine_mail_status(MAILSTREAM *stream, char *mailbox, long int flags)
1925 return(pine_mail_status_full(stream, mailbox, flags, NULL, NULL));
1929 long
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;
1934 long ret = NIL;
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,
1953 uidnext))
1954 && !status.recent){
1956 /* do status of source */
1957 pine_cached_status = &cache;
1958 stream = sp_stream_get(source, SP_SAME);
1960 if(!stream){
1961 DRIVER *d;
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);
1971 ourstream = stream;
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)){
1981 DRIVER *d;
1982 int is_news = 0;
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
1998 * of unseen.
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")))
2030 is_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;
2038 else{
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;
2061 if(!ourstream)
2062 stream = NULL;
2064 if(ourstream
2065 && !same_stream_and_mailbox(source, ourstream)){
2066 pine_mail_close(ourstream);
2067 ourstream = stream = NULL;
2070 if(!stream){
2071 stream = pine_mail_open(stream, source, openflags,
2072 NULL);
2073 ourstream = stream;
2076 if(stream){
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;
2101 if(ret)
2102 mm_status(NULL, mailbox, &status);
2104 else{
2105 if(!stream){
2106 DRIVER *d;
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
2121 * already opened.
2123 stream = sp_stream_get(mailbox, SP_MATCH);
2124 if(stream){
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){
2137 long i;
2138 SEARCHPGM *srchpgm;
2139 MESSAGECACHE *mc;
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;
2147 status.unseen = 0L;
2148 for(i = 1L; i <= stream->nmsgs; i++)
2149 if((mc = mail_elt(stream, i)) && mc->searched)
2150 status.unseen++;
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 */
2167 if(!stream)
2168 stream = sp_stream_get(mailbox, SP_SAME);
2170 if(!stream){
2171 stream = pine_mail_open(NULL, mailbox, openflags, NULL);
2172 ourstream = stream;
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 */
2185 if(ourstream)
2186 pine_mail_close(ourstream);
2188 return ret;
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
2202 * 0 - not
2205 check_for_move_mbox(char *mbox, char *sourcebuf, size_t sbuflen, char **targetptr)
2207 char delim, *s;
2208 int i;
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)){
2220 if(sourcebuf){
2221 strncpy(sourcebuf, mbox+6, i); /* copy source mailbox name */
2222 sourcebuf[i] = '\0';
2225 if(targetptr)
2226 *targetptr = s;
2228 return 1;
2231 return 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.
2242 MAILSTREAM *
2243 already_open_stream(char *mailbox, int flags)
2245 int i;
2246 MAILSTREAM *m;
2248 if(!mailbox)
2249 return(NULL);
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))
2257 return(m);
2260 else{
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)))
2271 return(m);
2275 return(NULL);
2279 void
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)){
2289 time_t now;
2291 now = time(0);
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))
2310 return 1;
2311 else
2312 return 0;
2315 void
2316 sp_cleanup_dead_streams(void)
2318 int i;
2319 MAILSTREAM *m;
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))
2326 pine_mail_close(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);
2341 void
2342 sp_set_fldr(MAILSTREAM *stream, char *folder)
2344 PER_STREAM_S **pss;
2346 pss = sp_data(stream);
2347 if(pss && *pss){
2348 if((*pss)->fldr)
2349 fs_give((void **) &(*pss)->fldr);
2351 if(folder)
2352 (*pss)->fldr = cpystr(folder);
2357 void
2358 sp_set_saved_cur_msg_id(MAILSTREAM *stream, char *id)
2360 PER_STREAM_S **pss;
2362 pss = sp_data(stream);
2363 if(pss && *pss){
2364 if((*pss)->saved_cur_msg_id)
2365 fs_give((void **) &(*pss)->saved_cur_msg_id);
2367 if(id)
2368 (*pss)->saved_cur_msg_id = cpystr(id);
2374 * Sets flags absolutely, erasing old flags.
2376 void
2377 sp_flag(MAILSTREAM *stream, long unsigned int flags)
2379 if(!stream)
2380 return;
2382 dprint((9, "sp_flag(%s, 0x%x): %s%s%s%s%s%s%s%s\n",
2383 (stream && stream->mailbox) ? stream->mailbox : "?",
2384 flags,
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.
2401 void
2402 sp_unflag(MAILSTREAM *stream, long unsigned int flags)
2404 if(!stream || !flags)
2405 return;
2407 dprint((9, "sp_unflag(%s, 0x%x): unset%s%s%s%s%s%s\n",
2408 (stream && stream->mailbox) ? stream->mailbox : "?",
2409 flags,
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 : "?",
2422 flags,
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.
2435 void
2436 sp_mark_stream_dead(MAILSTREAM *stream)
2438 if(!stream)
2439 return;
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);
2449 else{
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)
2469 int i, cnt = 0;
2470 MAILSTREAM *m;
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))
2475 cnt++;
2478 return(cnt);
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)
2492 int cnt = 0;
2493 char **lock_these, *p = NULL, *dummy = NULL, *lt;
2494 DRIVER *d;
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"))
2500 cnt++;
2502 /* then count the user-specified permlocked folders */
2503 for(lock_these = ps_global->VAR_PERMLOCKED; lock_these && *lock_these;
2504 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))
2512 continue;
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),
2524 FN_WHOLE_NAME))))
2525 lt = p;
2527 if(dummy)
2528 fs_give((void **) &dummy);
2530 if(lt && (d=mail_valid(NIL, lt, (char *) NIL))
2531 && !strcmp(d->name, "imap"))
2532 cnt++;
2534 if(p)
2535 fs_give((void **) &p);
2538 return(cnt);
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.)
2546 * Args: mailbox
2547 * flags
2549 * Flags is a set of values or'd together which tells us what the request
2550 * is looking for.
2552 * Returns: a live stream from the stream pool or NULL.
2554 MAILSTREAM *
2555 sp_stream_get(char *mailbox, long unsigned int flags)
2557 int i;
2558 MAILSTREAM *m;
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)){
2577 dprint((7,
2578 "sp_stream_get: found exact match, slot %d\n", i));
2579 if(!sp_flagged(m, SP_LOCKED)){
2580 dprint((7,
2581 "reset idle timer1: next TAG %08lx (%s)\n",
2582 m->gensym,
2583 debug_time(1, ps_global->debug_timestamp)));
2584 sp_set_last_use_time(m, time(0));
2587 return(m);
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
2599 * routine.
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)){
2615 dprint((7,
2616 "sp_stream_get: found SAME match, slot %d\n", i));
2617 return(m);
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)){
2633 dprint((7,
2634 "sp_stream_get: found SAME match, slot %d\n", i));
2635 dprint((7,
2636 "reset idle timer4: next TAG %08lx (%s)\n",
2637 m->gensym,
2638 debug_time(1, ps_global->debug_timestamp)));
2639 sp_set_last_use_time(m, time(0));
2641 return(m);
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
2652 * only loop we try.
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)){
2661 dprint((7,
2662 "sp_stream_get: found SAME/TEMPUSE match, slot %d\n", i));
2663 dprint((7,
2664 "reset idle timer2: next TAG %08lx (%s)\n",
2665 m->gensym,
2666 debug_time(1, ps_global->debug_timestamp)));
2667 sp_set_last_use_time(m, time(0));
2668 return(m);
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)){
2694 dprint((7,
2695 "sp_stream_get: found SAME/UNLOCKED match, slot %d\n", i));
2696 dprint((7,
2697 "reset idle timer3: next TAG %08lx (%s)\n",
2698 m->gensym,
2699 debug_time(1, ps_global->debug_timestamp)));
2700 sp_set_last_use_time(m, time(0));
2701 return(m);
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
2714 * the slot.
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
2720 * only loop we try.
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)){
2726 dprint((7,
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
2731 * do that.
2733 (void) pine_mail_ping(m);
2734 return(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)){
2749 dprint((7,
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
2754 * do that.
2756 (void) pine_mail_ping(m);
2757 return(m);
2763 dprint((7, "sp_stream_get: no match found\n"));
2765 return(NULL);
2769 void
2770 sp_end(void)
2772 int i;
2773 MAILSTREAM *m;
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];
2779 if(m)
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)
2800 int i, slot = -1;
2801 MAILSTREAM *m;
2803 dprint((7, "sp_add(%s, %d)\n",
2804 (stream && stream->mailbox) ? stream->mailbox : "?", usepool));
2806 if(!stream){
2807 dprint((7, "sp_add: NULL stream\n"));
2808 return -1;
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];
2814 if(m == stream){
2815 slot = i;
2816 dprint((7,
2817 "sp_add: stream was already in slot %d\n", slot));
2818 return 0;
2822 if(usepool && !sp_flagged(stream, SP_PERMLOCKED)
2823 && sp_nusepool_notperm() >= ps_global->s_pool.max_remstream){
2824 dprint((7,
2825 "sp_add: reached max implicit SP_USEPOOL of %d\n",
2826 ps_global->s_pool.max_remstream));
2827 return -1;
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];
2833 if(!m){
2834 slot = i;
2835 dprint((7,
2836 "sp_add: using empty slot %d\n", slot));
2837 break;
2841 /* else, allocate more space */
2842 if(slot < 0){
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;
2851 else{
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));
2860 dprint((7,
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;
2866 return 0;
2868 else{
2869 dprint((7, "sp_add: failed to find a slot!\n"));
2870 return -1;
2876 * Simply remove this stream from the stream pool.
2878 void
2879 sp_delete(MAILSTREAM *stream)
2881 int i;
2882 MAILSTREAM *m;
2884 if(!stream)
2885 return;
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];
2907 if(m == stream){
2908 ps_global->s_pool.streams[i] = NULL;
2909 dprint((7,
2910 "sp_delete: stream removed from slot %d\n", i));
2911 return;
2918 * Returns 1 if any locked userfldr is dead, 0 if all alive.
2921 sp_a_locked_stream_is_dead(void)
2923 int i, ret = 0;
2924 MAILSTREAM *m;
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))
2930 ret++;
2933 return(ret);
2938 * Returns 1 if any locked stream is changed, 0 otherwise
2941 sp_a_locked_stream_changed(void)
2943 int i, ret = 0;
2944 MAILSTREAM *m;
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))
2950 ret++;
2953 return(ret);
2958 * Returns the inbox stream or NULL.
2960 MAILSTREAM *
2961 sp_inbox_stream(void)
2963 int i;
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))
2969 ret = m;
2972 return(ret);
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
2982 PER_STREAM_S **
2983 sp_data(MAILSTREAM *stream)
2985 PER_STREAM_S **pss = NULL;
2987 if(stream){
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);
2995 return(pss);
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
3003 * created.
3005 MSGNO_S *
3006 sp_msgmap(MAILSTREAM *stream)
3008 MSGNO_S **msgmap = NULL;
3009 PER_STREAM_S **pss = NULL;
3011 pss = sp_data(stream);
3013 if(pss && *pss
3014 && (*(msgmap = (MSGNO_S **) &(*pss)->msgmap) == NULL))
3015 mn_init(msgmap, stream->nmsgs);
3017 return(msgmap ? *msgmap : NULL);
3021 void
3022 sp_free_callback(void **sparep)
3024 PER_STREAM_S **pss;
3025 MAILSTREAM *stream = NULL, *m;
3026 int i;
3028 pss = (PER_STREAM_S **) sparep;
3030 if(pss && *pss){
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)
3040 stream = m;
3043 if(stream){
3044 if(ps_global->mail_stream == stream)
3045 ps_global->mail_stream = NULL;
3047 sp_delete(stream);
3050 sp_free(pss);
3056 * Free the data but don't mess with the stream pool.
3058 void
3059 sp_free(PER_STREAM_S **pss)
3061 if(pss && *pss){
3062 if((*pss)->msgmap){
3063 if(ps_global->msgmap == (*pss)->msgmap)
3064 ps_global->msgmap = NULL;
3066 mn_give(&(*pss)->msgmap);
3069 if((*pss)->fldr)
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
3082 candidate stream
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.
3102 ----*/
3103 MAILSTREAM *
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)
3122 && mb_s.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",
3134 name ? name : "?",
3135 (stream && stream->mailbox) ? stream->mailbox : "NULL"));
3136 return(stream);
3139 dprint((7, "same_stream: name->%s == stream->%s: no dice\n",
3140 name ? name : "?",
3141 (stream && stream->mailbox) ? stream->mailbox : "NULL"));
3142 return(NULL);
3147 /*----------------------------------------------------------------------
3148 See if this stream has the named mailbox selected.
3150 Accepts: mailbox name
3151 candidate stream
3152 Returns: stream if it can be used, else NIL
3153 ----*/
3154 MAILSTREAM *
3155 same_stream_and_mailbox(char *name, MAILSTREAM *stream)
3157 NETMBX mb_s, mb_n;
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"))))){
3166 dprint((7,
3167 "same_stream_and_mailbox: name->%s == stream->%s: yes\n",
3168 name ? name : "?",
3169 (stream && stream->mailbox) ? stream->mailbox : "NULL"));
3170 return(stream);
3173 dprint((7,
3174 "same_stream_and_mailbox: name->%s == stream->%s: no dice\n",
3175 name ? name : "?",
3176 (stream && stream->mailbox) ? stream->mailbox : "NULL"));
3177 return(NULL);
3181 * Args -- name1 and name2 are remote mailbox names.
3183 * Returns -- True if names refer to same mailbox accessed in same way
3184 * False if not
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)
3193 NETMBX mb1, mb2;
3194 char *cn1;
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
3260 ----*/
3262 streams_died(void)
3264 int rv = 0;
3265 int i;
3266 MAILSTREAM *m;
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)){
3274 rv++;
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",
3282 folder));
3283 if(rv == 1){
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);
3293 else{
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.
3304 #if 0
3305 if(sp_flagged(m, SP_USEPOOL))
3306 q_status_message(SM_ORDER, 3, 3,
3307 "Warning: Possible problem accessing remote data, connection died.");
3308 #endif
3310 dprint((6, "streams_died: not locked: \"%s\"\n",
3311 folder));
3314 pine_mail_actually_close(m);
3319 return(rv);
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)
3330 int rv = 0, i;
3331 MAILSTREAM *m;
3333 for(i = 0; rv == 0 && i < ps_global->s_pool.nstream; i++){
3334 m = ps_global->s_pool.streams[i];
3335 if(m && m->lock)
3336 rv++;
3339 return(rv);
3343 * Very simple version of appenduid_cb until we need something
3344 * more complex.
3347 static imapuid_t last_append_uid;
3349 void
3350 appenduid_cb(char *mailbox,unsigned long uidvalidity, SEARCHSET *set)
3352 last_append_uid = set ? set->first : 0L;
3356 imapuid_t
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
3368 MAILSTREAM *
3369 mail_cmd_stream(CONTEXT_S *context, int *closeit)
3371 char tmp[MAILTMPLEN];
3373 *closeit = 1;
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,
3378 NULL));
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.
3388 long
3389 dummy_soutr(void *stream, char *string)
3391 dprint((2, "dummy_soutr unexpected call, caught overflow\n"));
3392 return LONGT;