1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
|
/*
* ========================================================================
* Copyright 2013-2022 Eduardo Chappa
* Copyright 2006-2007 University of Washington
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* ========================================================================
*/
#include "../pith/headers.h"
#include "../pith/newmail.h"
#include "../pith/conf.h"
#include "../pith/flag.h"
#include "../pith/mailindx.h"
#include "../pith/msgno.h"
#include "../pith/bldaddr.h"
#include "../pith/stream.h"
#include "../pith/sort.h"
#include "../pith/status.h"
#include "../pith/util.h"
#include "../pith/thread.h"
#include "../pith/options.h"
#include "../pith/folder.h"
#include "../pith/ablookup.h"
#include "../pith/init.h"
#ifdef _WINDOWS
#include "../pico/osdep/mswin.h"
#endif
/*
* Hooks for various stuff
*/
void (*pith_opt_newmail_announce)(MAILSTREAM *, long, long);
void (*pith_opt_newmail_check_cue)(int);
void (*pith_opt_checkpoint_cue)(int);
void (*pith_opt_icon_text)(char *, int);
void fixup_flags(MAILSTREAM *, MSGNO_S *);
int check_point(MAILSTREAM *, CheckPointTime, int);
/*----------------------------------------------------------------------
new_mail() - check for new mail in the inbox
Input: force -- flag indicating we should check for new mail no matter
time_for_check_point -- 0: GoodTime, 1: BadTime, 2: VeryBadTime
flags -- whether to q a new mail status message or defer the sort
Result: returns -1 if there was no new mail. Otherwise returns the
sorted message number of the smallest message number that
was changed. That is the screen needs to be repainted from
that message down.
Limit frequency of checks because checks use some resources. That is
they generate an IMAP packet or require locking the local mailbox.
(Actually the lock isn't required, a stat will do, but the current
c-client mail code locks before it stats.)
Returns >= 0 only if there is a change in the given mail stream. Otherwise
this returns -1. On return the message counts in the pine
structure are updated to reflect the current number of messages including
any new mail and any expunging.
--- */
long
new_mail(int force_arg, CheckPointTime time_for_check_point, int flags)
{
static time_t last_check_point_call = 0;
long since_last_input;
time_t expunged_reaper_to, adj_idle_timeout, interval, adj;
static int nexttime = 0;
time_t now;
long n, rv = 0, t_nm_count = 0, exp_count;
MAILSTREAM *m;
int force, i, started_on;
int new_mail_was_announced = 0;
int have_pinged_non_special = 0;
int timeo;
dprint((9, "new mail called (force=%d %s flags=0x%x)\n",
force_arg,
time_for_check_point == GoodTime ? "GoodTime" :
time_for_check_point == BadTime ? "BadTime" :
time_for_check_point == VeryBadTime ? "VeryBad" :
time_for_check_point == DoItNow ? "DoItNow" : "?",
flags));
force = force_arg;
now = time(0);
timeo = get_input_timeout();
if(time_for_check_point == GoodTime){
adrbk_maintenance();
html_dir_clean(0);
}
if(time_for_check_point == GoodTime || force_arg)
folder_unseen_count_updater(UFU_ANNOUNCE | (force_arg ? UFU_FORCE : 0));
if(sp_need_to_rethread(ps_global->mail_stream))
force = 1;
if(!force && sp_unsorted_newmail(ps_global->mail_stream))
force = !(flags & NM_DEFER_SORT);
/* Some servers prefer to close the connection when the access token expires,
* while some others prefer to keep the connection alive. This means we
* need to check if the access token is about to expire, and if so renew
* the access token and the stream. Under normal circumstances this is done
* invisible to the user. Hide error messages here, in case there are any.
* The worst thing that could happen is that the user will SEE the error
* later, when the connection is closed by the server (and the error will
* be seen then.) Sending error messages at this time will confuse users.
* Avoid it now.
*/
for(i = 0; i < ps_global->s_pool.nstream; i++){
m = ps_global->s_pool.streams[i];
if(m && m->auth.name
&& (!strucmp(m->auth.name, OA2NAME) || !strucmp(m->auth.name, BEARERNAME))
&& now + 60 > m->auth.expiration){ /* procastinate doing this */
int skip = m->auth.expiration == 0 ? 1 : 0;
dprint((9, "renew_accesstoken: %s: now = %lu, auth = %s, expiration = %lu\n", STREAMNAME(m), now, m->auth.name, m->auth.expiration));
ps_global->noshow_error = 1; /* make this invisible to the user */
renew_accesstoken(m);
if(skip == 0) mail_renew_stream(m);
ps_global->noshow_error = 0; /* return to normal status */
dprint((9, "renew_accesstoken: %s: result: expiration = %lu\n", STREAMNAME(m), m->auth.expiration));
}
}
if(!ps_global->mail_stream
|| !(timeo || force || sp_a_locked_stream_changed()))
return(-1);
last_check_point_call = now;
since_last_input = (long) now - (long) time_of_last_input();
/*
* We have this for loop followed by the do-while so that we will prefer
* to ping the active streams before we ping the inactive ones, in cases
* where the pings or checks are taking a long time.
*/
for(i = 0; i < ps_global->s_pool.nstream; i++){
m = ps_global->s_pool.streams[i];
if(!m || m->halfopen ||
(m != ps_global->mail_stream &&
!(force_arg && sp_flagged(m, SP_LOCKED))))
continue;
/*
* This for() loop is only the current folder, unless the user
* has forced the check, in which case it is all locked folders.
*/
/*
* After some amount of inactivity on a stream, the server may
* close the stream. Each protocol has its own idea of how much
* inactivity should be allowed before the stream is closed. For
* example, IMAP specifies that the server should not close the
* stream unilaterally until at least 30 minutes of inactivity.
* The GET_IDLETIMEOUT call gives us that time in minutes. We
* want to be sure to keep the stream alive if it is about to die
* due to inactivity.
*/
adj_idle_timeout = 60 * (long) mail_parameters(m,GET_IDLETIMEOUT,NULL);
if(adj_idle_timeout <= 0)
adj_idle_timeout = 600;
adj = (adj_idle_timeout >= 50 * FUDGE) ? 5 * FUDGE :
(adj_idle_timeout >= 30 * FUDGE) ? 3 * FUDGE :
(adj_idle_timeout >= 20 * FUDGE) ? 2 * FUDGE : FUDGE;
adj_idle_timeout = MAX(adj_idle_timeout - adj, 120);
/*
* Set interval to mail-check-interval unless
* mail-check-interval-noncurrent is nonzero and this is not inbox
* or current stream.
*/
if(ps_global->check_interval_for_noncurr > 0
&& m != ps_global->mail_stream
&& !sp_flagged(m, SP_INBOX))
interval = ps_global->check_interval_for_noncurr;
else
interval = timeo;
/*
* We want to make sure that we notice expunges, but we don't have
* to be fanatical about it. Every once in a while we'll do a ping
* because we haven't had a command that notices expunges for a
* while. It's also a larger number than interval so it gives us a
* convenient interval to do slower pinging than interval if we
* are busy.
*/
if(interval <= adj_idle_timeout)
expunged_reaper_to = MIN(MAX(2*interval,180), adj_idle_timeout);
else
expunged_reaper_to = interval;
/*
* User may want to avoid new mail checking while composing.
* In this case we will only do the keepalives.
*/
if(timeo == 0
|| (flags & NM_FROM_COMPOSER
&& ((F_ON(F_QUELL_PINGS_COMPOSING, ps_global)
&& !sp_flagged(m, SP_INBOX))
||
(F_ON(F_QUELL_PINGS_COMPOSING_INBOX, ps_global)
&& sp_flagged(m, SP_INBOX))))){
interval = expunged_reaper_to = 0;
}
dprint((9,
"%s: force=%d interval=%ld exp_reap_to=%ld adj_idle_to=%ld\n",
STREAMNAME(m), force_arg, (long) interval,
(long) expunged_reaper_to, (long) adj_idle_timeout));
dprint((9,
" since_last_ping=%ld since_last_reap=%ld\n",
(long) (now - sp_last_ping(m)),
(long) (now - sp_last_expunged_reaper(m))));
/* if check_point does a check it resets last_ping time */
if(force_arg || (timeo && expunged_reaper_to > 0))
(void) check_point(m, time_for_check_point, flags);
/*
* Remember that unless force_arg is set, this check is really
* only for the current folder. This is usually going to fire
* on the first test, which is the interval the user set.
*/
if(force_arg
||
(timeo
&&
((interval && (now - sp_last_ping(m) >= interval-1))
||
(expunged_reaper_to
&& (now - sp_last_expunged_reaper(m)) >= expunged_reaper_to)
||
(now - sp_last_ping(m) >= adj_idle_timeout)))){
if((flags & NM_STATUS_MSG) && pith_opt_newmail_check_cue)
(*pith_opt_newmail_check_cue)(TRUE);
dprint((7, "Mail_Ping(%s): lastping=%ld er=%ld%s%s %s\n",
STREAMNAME(m),
(long) (now - sp_last_ping(m)),
(long) (now - sp_last_expunged_reaper(m)),
force_arg ? " [forced]" :
interval && (now-sp_last_ping(m) >= interval-1)
? " [it's time]" :
expunged_reaper_to
&& (now - sp_last_expunged_reaper(m) >= expunged_reaper_to)
? " [expunged reaper]"
: " [keepalive]",
m == ps_global->mail_stream ? " [current]" : "",
debug_time(0,1,ps_global->signal_in_progress)));
/*
* We're about to ping the stream.
* If the stream is a #move Mail Drop there is a minimum time
* between re-opens of the mail drop to check for new mail.
* If the check is forced by the user, they want it to
* happen now. We use knowledge of c-client internals to
* make this happen.
*/
if(force_arg && m && m->snarf.name)
m->snarf.time = 0;
/*-- Ping the stream to check for new mail --*/
if(sp_dead_stream(m)){
dprint((6, "no check: stream is dead\n"));
}
else if(!pine_mail_ping(m)){
dprint((6, "ping failed: stream is dead\n"));
sp_set_dead_stream(m, 1);
}
dprint((7, "Ping complete: %s\n", debug_time(0,1,ps_global->signal_in_progress)));
if((flags & NM_STATUS_MSG) && pith_opt_newmail_check_cue)
(*pith_opt_newmail_check_cue)(FALSE);
}
}
if(nexttime < 0 || nexttime >= ps_global->s_pool.nstream)
nexttime = 0;
i = started_on = nexttime;
do {
m = ps_global->s_pool.streams[i];
nexttime = i;
if(ps_global->s_pool.nstream > 0)
i = (i + 1) % ps_global->s_pool.nstream;
/*
* This do() loop handles all the other streams that weren't covered
* in the for() loop above.
*/
if((!m || m->halfopen) ||
(m == ps_global->mail_stream ||
(force_arg && sp_flagged(m, SP_LOCKED)))){
nexttime = i;
continue;
}
/*
* If it is taking an extra long time to do the pings and checks,
* think about skipping some of them. Always do at least one of
* these non-special streams (if they are due to be pinged).
* The nexttime variable keeps track of where we were so that we
* don't always ping the first one in the list and then skip out.
*/
if((time(0) - now >= 5) && have_pinged_non_special){
dprint((7, "skipping pings due to delay: %s\n",
debug_time(0,1,ps_global->signal_in_progress)));
break;
}
nexttime = i;
have_pinged_non_special++;
adj_idle_timeout = 60 * (long) mail_parameters(m,GET_IDLETIMEOUT,NULL);
if(adj_idle_timeout <= 0)
adj_idle_timeout = 600;
adj = (adj_idle_timeout >= 50 * FUDGE) ? 5 * FUDGE :
(adj_idle_timeout >= 30 * FUDGE) ? 3 * FUDGE :
(adj_idle_timeout >= 20 * FUDGE) ? 2 * FUDGE : FUDGE;
adj_idle_timeout = MAX(adj_idle_timeout - adj, 120);
if(ps_global->check_interval_for_noncurr > 0
&& m != ps_global->mail_stream
&& !sp_flagged(m, SP_INBOX))
interval = ps_global->check_interval_for_noncurr;
else
interval = timeo;
if(interval <= adj_idle_timeout)
expunged_reaper_to = MIN(MAX(2*interval,180), adj_idle_timeout);
else
expunged_reaper_to = interval;
if(timeo == 0
|| (flags & NM_FROM_COMPOSER
&& ((F_ON(F_QUELL_PINGS_COMPOSING, ps_global)
&& !sp_flagged(m, SP_INBOX))
||
(F_ON(F_QUELL_PINGS_COMPOSING_INBOX, ps_global)
&& sp_flagged(m, SP_INBOX))))){
interval = expunged_reaper_to = 0;
}
dprint((9,
"%s: force=%d interval=%ld exp_reap_to=%ld adj_idle_to=%ld\n",
STREAMNAME(m), force_arg, (long) interval,
(long) expunged_reaper_to, (long) adj_idle_timeout));
dprint((9,
" since_last_ping=%ld since_last_reap=%ld since_last_input=%ld\n",
(long) (now - sp_last_ping(m)),
(long) (now - sp_last_expunged_reaper(m)),
(long) since_last_input));
/* if check_point does a check it resets last_ping time */
if(force_arg || (timeo && expunged_reaper_to > 0))
(void) check_point(m, time_for_check_point, flags);
/*
* The check here is a little bit different from the current folder
* check in the for() loop above. In particular, we defer our
* pinging for awhile if the last input was recent (except for the
* inbox!). We ping streams which are cached but not actively being
* used (that is, the non-locked streams) at a slower rate.
* If we don't use them for a long time we will eventually close them
* (in maybe_kill_old_stream()) but we do it when we want to instead
* of when the server wants us to by attempting to keep it alive here.
* The other reason to ping the cached streams is that we only get
* told the new mail in those streams is recent one time, the messages
* that weren't handled here will no longer be recent next time
* we open the folder.
*/
if((force_arg && sp_flagged(m, SP_LOCKED))
||
(timeo
&&
((interval
&& sp_flagged(m, SP_LOCKED)
&& ((since_last_input >= 3
&& (now-sp_last_ping(m) >= interval-1))
|| (sp_flagged(m, SP_INBOX)
&& (now-sp_last_ping(m) >= interval-1))))
||
(expunged_reaper_to
&& sp_flagged(m, SP_LOCKED)
&& (now-sp_last_expunged_reaper(m) >= expunged_reaper_to))
||
(expunged_reaper_to
&& !sp_flagged(m, SP_LOCKED)
&& since_last_input >= 3
&& (now-sp_last_ping(m) >= expunged_reaper_to))
||
(now - sp_last_ping(m) >= adj_idle_timeout)))){
if((flags & NM_STATUS_MSG) && pith_opt_newmail_check_cue)
(*pith_opt_newmail_check_cue)(TRUE);
dprint((7,
"Mail_Ping(%s): lastping=%ld er=%ld%s idle: %ld %s\n",
STREAMNAME(m),
(long) (now - sp_last_ping(m)),
(long) (now - sp_last_expunged_reaper(m)),
(force_arg && sp_flagged(m, SP_LOCKED)) ? " [forced]" :
(interval
&& sp_flagged(m, SP_LOCKED)
&& ((since_last_input >= 3
&& (now-sp_last_ping(m) >= interval-1))
|| (sp_flagged(m, SP_INBOX)
&& (now-sp_last_ping(m) >= interval-1))))
? " [it's time]" :
(expunged_reaper_to
&& sp_flagged(m, SP_LOCKED)
&& (now-sp_last_expunged_reaper(m) >= expunged_reaper_to))
? " [expunged reaper]" :
(expunged_reaper_to
&& !sp_flagged(m, SP_LOCKED)
&& since_last_input >= 3
&& (now-sp_last_ping(m) >= expunged_reaper_to))
? " [slow ping]" : " [keepalive]",
since_last_input,
debug_time(0,1,ps_global->signal_in_progress)));
/*
* We're about to ping the stream.
* If the stream is a #move Mail Drop there is a minimum time
* between re-opens of the mail drop to check for new mail.
* If the check is forced by the user, they want it to
* happen now. We use knowledge of c-client internals to
* make this happen.
*/
if(force_arg && m && m->snarf.name)
m->snarf.time = 0;
/*-- Ping the stream to check for new mail --*/
if(sp_dead_stream(m)){
dprint((6, "no check: stream is dead\n"));
}
else if(!pine_mail_ping(m)){
dprint((6, "ping failed: stream is dead\n"));
sp_set_dead_stream(m, 1);
}
dprint((7, "Ping complete: %s\n", debug_time(0,1,ps_global->signal_in_progress)));
if((flags & NM_STATUS_MSG) && pith_opt_newmail_check_cue)
(*pith_opt_newmail_check_cue)(FALSE);
}
} while(i != started_on);
/*
* Current mail box state changed, could be additions or deletions.
* Also check if we need to do sorting that has been deferred.
* We handle the current stream separately from the rest in the
* similar loop that follows this paragraph.
*/
m = ps_global->mail_stream;
if(sp_mail_box_changed(m) || sp_unsorted_newmail(m)
|| sp_need_to_rethread(m)){
dprint((7,
"Cur new mail, %s, new_mail_count: %ld expunge: %ld, max_msgno: %ld\n",
(m && m->mailbox) ? m->mailbox : "?",
sp_new_mail_count(m),
sp_expunge_count(m),
mn_get_total(sp_msgmap(m))));
new_mail_was_announced = 0;
if(sp_mail_box_changed(m))
fixup_flags(m, sp_msgmap(m));
if(sp_new_mail_count(m))
process_filter_patterns(m, sp_msgmap(m), sp_new_mail_count(m));
/* worry about sorting */
if((sp_new_mail_count(m) > 0L
|| sp_unsorted_newmail(m)
|| sp_need_to_rethread(m))
&& !((flags & NM_DEFER_SORT)
|| any_lflagged(sp_msgmap(m), MN_HIDE)))
refresh_sort(m, sp_msgmap(m),
(flags & NM_STATUS_MSG) ? SRT_VRB : SRT_NON);
else if(sp_new_mail_count(m) > 0L)
sp_set_unsorted_newmail(m, 1);
if(sp_new_mail_count(m) > 0L){
sp_set_mail_since_cmd(m, sp_mail_since_cmd(m)+sp_new_mail_count(m));
rv += (t_nm_count = sp_new_mail_count(m));
sp_set_new_mail_count(m, 0L);
if((flags & NM_STATUS_MSG) && pith_opt_newmail_announce){
for(n = m->nmsgs; n > 1L; n--)
if(!get_lflag(m, NULL, n, MN_EXLD))
break;
(*pith_opt_newmail_announce)(m, n, t_nm_count);
if(n)
new_mail_was_announced++;
}
}
update_folder_unseen_by_stream(m, new_mail_was_announced ? UFU_NONE : UFU_ANNOUNCE);
if(flags & NM_STATUS_MSG)
sp_set_mail_box_changed(m, 0);
}
/* the rest of the streams */
for(i = 0; i < ps_global->s_pool.nstream; i++){
m = ps_global->s_pool.streams[i];
if(!m || m == ps_global->mail_stream)
continue;
if(sp_mail_box_changed(m)){
/*-- New mail for this stream, queue up the notification --*/
dprint((7,
"New mail, %s, new_mail_count: %ld expunge: %ld, max_msgno: %ld\n",
(m && m->mailbox) ? m->mailbox : "?",
sp_new_mail_count(m),
sp_expunge_count(m),
mn_get_total(sp_msgmap(m))));
new_mail_was_announced = 0;
fixup_flags(m, sp_msgmap(m));
if(sp_new_mail_count(m))
process_filter_patterns(m, sp_msgmap(m), sp_new_mail_count(m));
if(sp_new_mail_count(m) > 0){
sp_set_unsorted_newmail(m, 1);
sp_set_mail_since_cmd(m, sp_mail_since_cmd(m) +
sp_new_mail_count(m));
if(sp_flagged(m, SP_LOCKED) && sp_flagged(m, SP_USERFLDR))
rv += (t_nm_count = sp_new_mail_count(m));
sp_set_new_mail_count(m, 0L);
/* messages only for user's streams */
if(flags & NM_STATUS_MSG
&& pith_opt_newmail_announce
&& sp_flagged(m, SP_LOCKED)
&& sp_flagged(m, SP_USERFLDR)){
for(n = m->nmsgs; n > 1L; n--)
if(!get_lflag(m, NULL, n, MN_EXLD))
break;
(*pith_opt_newmail_announce)(m, n, t_nm_count);
if(n)
new_mail_was_announced++;
}
}
update_folder_unseen_by_stream(m, new_mail_was_announced ? UFU_NONE : UFU_ANNOUNCE);
if(flags & NM_STATUS_MSG)
sp_set_mail_box_changed(m, 0);
}
}
/* so quit_screen can tell new mail from expunged mail */
exp_count = sp_expunge_count(ps_global->mail_stream);
/* see if we want to kill any cached streams */
for(i = 0; i < ps_global->s_pool.nstream; i++){
m = ps_global->s_pool.streams[i];
if(sp_last_ping(m) >= now)
maybe_kill_old_stream(m);
}
/*
* This is here to prevent banging on the down arrow key (or holding
* it down and repeating) at the end of the index from causing
* a whole bunch of new mail checks. Last_nextitem_forcechk does get
* set at the place it is tested in mailcmd.c, but this is here to
* reset it to a little bit later in case it takes a second or more
* to check for the mail. If we didn't do this, we'd just check every
* keystroke as long as the checks took more than a second.
*/
if(force_arg)
ps_global->last_nextitem_forcechk = time(0);
dprint((6, "******** new mail returning %ld ********\n",
rv ? rv : (exp_count ? 0 : -1)));
return(rv ? rv : (exp_count ? 0 : -1));
}
/*
* format_new_mail_msg - actual work of generating intro,
* from, subject and expanded subject
*/
void
format_new_mail_msg(char *folder, long int number, ENVELOPE *e,
char *intro, char *from, char *subj, char *subjex,
size_t buflen) /* min length of each of the 4 above if they're non-null */
{
char *p, tmp[MAILTMPLEN+1], subj_leadin[MAILTMPLEN];
static char *carray[] = { "regarding",
"concerning",
"about",
"as to",
"as regards",
"as respects",
"in re",
"re",
"respecting",
"in point of",
"with regard to",
"subject:"
};
if(buflen > 0){
if(intro)
intro[0] = '\0';
if(from)
from[0] = '\0';
if(subj)
subj[0] = '\0';
if(subjex)
subjex[0] = '\0';
}
if(from && e && e->from){
if(e->from->personal && e->from->personal[0]){
/*
* The reason we use so many characters for tmp is because we
* may have multiple MIME3 chunks and we don't want to truncate
* in the middle of one of them before decoding.
*/
snprintf(tmp, sizeof(tmp), "%s", e->from->personal);
p = (char *) rfc1522_decode_to_utf8((unsigned char *) tmp_20k_buf, SIZEOF_20KBUF, tmp);
removing_leading_and_trailing_white_space(p);
if(*p)
snprintf(from, buflen, "%.200s", p);
}
if(!from[0]){
snprintf(tmp, sizeof(tmp), "%s%s%s",
e->from->mailbox,
e->from->host ? "@" : "",
e->from->host ? e->from->host : "");
snprintf(from, buflen, "%.200s", tmp);
}
}
if(number <= 1L){
if(e && e->subject && e->subject[0]){
snprintf(tmp, sizeof(tmp), "%s", e->subject);
p = (char *) rfc1522_decode_to_utf8((unsigned char *)tmp_20k_buf, SIZEOF_20KBUF, tmp);
if(subj)
snprintf(subj, buflen, "%.200s", p);
}
snprintf(subj_leadin, sizeof(subj_leadin), " %s ", carray[(unsigned)random()%12]);
if(!(from && from[0]))
subj_leadin[1] = toupper((unsigned char)subj_leadin[1]);
}
if(subj && subjex){
if(subj[0]){
snprintf(subjex, buflen, "%s%.200s%s",
(number <= 1L) ? (subj[0] ? subj_leadin : "")
: "",
(number <= 1L) ? (subj[0] ? subj
: from ? " w" : " W")
: "",
(number <= 1L) ? (subj[0] ? "" : "ith no subject")
: "");
}
else
subj[0] = subjex[0] = '\0';
}
if(intro){
if(!folder){
if(number > 1)
/* TRANSLATORS: The argument is the number of new messages */
snprintf(intro, buflen, _("%ld new messages!"), number);
else
/* TRANSLATORS: The argument is either " to you" or nothing */
snprintf(intro, buflen, _("New mail%s!"),
(e && address_is_us(e->to, ps_global)) ? _(" to you") : "");
}
else {
long fl, tot, newfl;
char *fname = folder ? (char *) folder_name_decoded((unsigned char *) folder) : "";
if(number > 1)
snprintf(intro, buflen, _("%ld messages saved to folder \"%.80s\""),
number, fname);
else
snprintf(intro, buflen, _("Mail saved to folder \"%.80s\""), fname);
if((fl=utf8_width(fname)) > 10 &&
(tot=utf8_width(intro) + utf8_width(from ? from : "") + utf8_width(subj ? subj : "")) >
ps_global->ttyo->screen_cols - 2){
char *f = fs_get((strlen(fname) + 1)*sizeof(char));
newfl = MAX(10, fl-(tot-(ps_global->ttyo->screen_cols - 2)));
utf8_to_width_rhs(f, fname, strlen(fname) + 1, newfl-3);
if(number > 1)
snprintf(intro, buflen, _("%ld messages saved to folder \"...%.80s\""), number, f);
else
snprintf(intro, buflen, _("Mail saved to folder \"...%.80s\""), f);
if(f) fs_give((void **)&f);
}
if (fname && *fname)
fs_give((void **)&fname);
}
}
}
/*----------------------------------------------------------------------
Straighten out any local flag problems here. We can't take care of
them in the mm_exists or mm_expunged callbacks since the flags
themselves are in an MESSAGECACHE and we're not allowed to reenter
c-client from a callback...
Args: stream -- mail stream to operate on
msgmap -- messages in that stream to fix up
Result: returns with local flags as they should be
----*/
void
fixup_flags(MAILSTREAM *stream, MSGNO_S *msgmap)
{
/*
* Deal with the case where expunged away all of the
* zoomed messages. Unhide everything in that case...
*/
if(mn_get_total(msgmap) > 0L){
long i;
if(any_lflagged(msgmap, MN_HIDE) >= mn_get_total(msgmap)){
for(i = 1L; i <= mn_get_total(msgmap); i++)
set_lflag(stream, msgmap, i, MN_HIDE, 0);
mn_set_cur(msgmap, THREADING()
? first_sorted_flagged(F_NONE, stream, 0L,
(THREADING() ? 0 : FSF_SKIP_CHID)
| FSF_LAST)
: mn_get_total(msgmap));
}
else if(any_lflagged(msgmap, MN_HIDE)){
/*
* if we got here, there are some hidden messages and
* some not. Make sure the current message is one
* that's not...
*/
for(i = mn_get_cur(msgmap); i <= mn_get_total(msgmap); i++)
if(!msgline_hidden(stream, msgmap, i, 0)){
mn_set_cur(msgmap, i);
break;
}
for(i = mn_get_cur(msgmap); i > 0L; i--)
if(!msgline_hidden(stream, msgmap, i, 0)){
mn_set_cur(msgmap, i);
break;
}
}
}
}
/*----------------------------------------------------------------------
Force write of the main file so user doesn't lose too much when
something bad happens. The only thing that can get lost is flags, such
as when new mail arrives, is read, deleted or answered.
Args: timing -- indicates if it's a good time for to do a checkpoint
Result: returns 1 if checkpoint was written,
0 if not.
NOTE: mail_check will also notice new mail arrival, so it's imperative that
code exist after this function is called that can deal with updating the
various pieces of pine's state associated with the message count and such.
Only need to checkpoint current stream because there are no changes going
on with other streams when we're not manipulating them.
----*/
int
check_point(MAILSTREAM *stream, CheckPointTime timing, int flags)
{
int freq, tm, check_count, tst1 = 0, tst2 = 0, accel;
long since_last_input, since_first_change;
time_t now, then;
#define AT_LEAST (180)
#define MIN_LONG_CHK_IDLE (10)
if(!stream || stream->rdonly || stream->halfopen
|| (check_count = sp_check_cnt(stream)) == 0)
return(0);
since_last_input = (long) time(0) - (long) time_of_last_input();
if(timing == GoodTime && since_last_input <= 0)
timing = VeryBadTime;
else if(timing == GoodTime && since_last_input <= 4)
timing = BadTime;
dprint((9, "check_point(%s: %s)\n",
timing == GoodTime ? "GoodTime" :
timing == BadTime ? "BadTime" :
timing == VeryBadTime ? "VeryBadTime" : "DoItNow",
STREAMNAME(stream)));
freq = CHECK_POINT_FREQ * (timing==GoodTime ? 1 : timing==BadTime ? 3 : 4);
tm = CHECK_POINT_TIME * (timing==GoodTime ? 2 : timing==BadTime ? 4 : 6);
tm = MAX(100, tm);
if(timing == DoItNow){
dprint((9, "DoItNow\n"));
}
else{
since_first_change = (long) (time(0) - sp_first_status_change(stream));
accel = MIN(3, MAX(1, (4 * since_first_change)/tm));
tst1 = ((check_count * since_last_input) >= (30/accel) * freq);
tst2 = ((since_first_change >= tm)
&& (since_last_input >= MIN_LONG_CHK_IDLE));
dprint((9,
"Chk changes(%d) x since_last_input(%ld) (=%ld) >= freq(%d) x 30/%d (=%d) ? %s\n",
check_count, since_last_input,
check_count * since_last_input, freq, accel, (30/accel)*freq,
tst1 ? "Yes" : "No"));
dprint((9,
" or since_1st_change(%ld) >= tm(%d) && since_last_input >= %d ? %s\n",
since_first_change, tm, MIN_LONG_CHK_IDLE,
tst2 ? "Yes" : "No"));
}
if(timing == DoItNow || tst1 || tst2){
if(timing == DoItNow
|| time(0) - sp_last_chkpnt_done(stream) >= AT_LEAST){
then = time(0);
dprint((2, "Checkpoint: %s Since 1st change: %ld secs idle: %ld secs\n",
debug_time(0,1,ps_global->signal_in_progress),
(long) (then - sp_first_status_change(stream)),
since_last_input))
;
if((flags & NM_STATUS_MSG) && pith_opt_checkpoint_cue)
(*pith_opt_checkpoint_cue)(TRUE);
pine_mail_check(stream); /* write file state */
now = time(0);
dprint((2,
"Checkpoint complete: %s%s%s%s\n",
debug_time(0,1,ps_global->signal_in_progress),
(now-then > 0) ? " (elapsed: " : "",
(now-then > 0) ? comatose((long)(now-then)) : "",
(now-then > 0) ? " secs)" : ""));
if((flags & NM_STATUS_MSG) && pith_opt_checkpoint_cue)
(*pith_opt_checkpoint_cue)(FALSE);
return(1);
}
else{
dprint((9,
"Skipping checkpoint since last was only %ld secs ago (< %d)\n",
(long) (time(0) - sp_last_chkpnt_done(stream)), AT_LEAST));
}
}
return(0);
}
/*----------------------------------------------------------------------
Call this when we need to tell the check pointing mechanism about
mailbox state changes.
----------------------------------------------------------------------*/
void
check_point_change(MAILSTREAM *stream)
{
if(!sp_check_cnt(stream))
sp_set_first_status_change(stream, time(0));
sp_set_check_cnt(stream, sp_check_cnt(stream)+1);
dprint((9, "check_point_change(%s): increment to %d\n",
STREAMNAME(stream), sp_check_cnt(stream)));
}
/*----------------------------------------------------------------------
Call this when a mail file is written to reset timer and counter
for next check_point.
----------------------------------------------------------------------*/
void
reset_check_point(MAILSTREAM *stream)
{
time_t now;
now = time(0);
sp_set_check_cnt(stream, 0);
sp_set_first_status_change(stream, 0);
sp_set_last_chkpnt_done(stream, now);
sp_set_last_ping(stream, now);
sp_set_last_expunged_reaper(stream, now);
}
int
changes_to_checkpoint(MAILSTREAM *stream)
{
return(sp_check_cnt(stream) > 0);
}
/*----------------------------------------------------------------------
Zero the counters that keep track of mail accumulated between
commands.
----*/
void
zero_new_mail_count(void)
{
int i;
MAILSTREAM *m;
dprint((9, "New_mail_count zeroed\n"));
for(i = 0; i < ps_global->s_pool.nstream; i++){
m = ps_global->s_pool.streams[i];
if(m && sp_flagged(m, SP_LOCKED) && sp_flagged(m, SP_USERFLDR)
&& sp_mail_since_cmd(m)){
if(pith_opt_icon_text)
(*pith_opt_icon_text)(NULL, IT_NEWMAIL);
break;
}
}
for(i = 0; i < ps_global->s_pool.nstream; i++){
m = ps_global->s_pool.streams[i];
if(m && sp_flagged(m, SP_LOCKED))
sp_set_mail_since_cmd(m, 0L);
}
}
|