This file is indexed.

/usr/share/emacs/site-lisp/mew/mew-net.el is in mew 1:6.7-4.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
;;; mew-net.el

;; Author:  Kazu Yamamoto <Kazu@Mew.org>
;; Created: Feb 27, 2002

;;; Code:

(require 'mew)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Port DB
;;;

(defvar mew-port-db
  '(("smtp"        25)
    ;; This is officially assigned to another service (urd) by IANA.
    ;; IESG will never assign a port number to SMTP over SSL...
    ("smtps"      465)
    ("pop3"       110)
    ("pop3s"      995)
    ("nntp"       119)
    ("nntps"      563)
    ("imap"       143)
    ("imaps"      993)
    ("submission" 587)))

(defun mew-serv-to-port (serv)
  (cond
   ((integerp serv) serv)
   ((string-match "^[0-9]+$" serv)
    (string-to-number serv))
   (t
    (or (mew-alist-get-value (assoc serv mew-port-db)) 0))))

(defun mew-*-to-port (port)
  (cond
   ((integerp port) port)
   ((string-match "^[0-9]+$" port)
    (string-to-number port))
   (t port)))

(defun mew-*-to-string (any)
  (cond
   ((integerp any)
    (number-to-string any))
   (t any)))

(defun mew-port-equal (port1 port2)
  (string= (mew-*-to-string port1) (mew-*-to-string port2)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Net folders
;;;

(defvar mew-pop-folder  "+#pop/%s@%s#%s")
(defvar mew-imap-folder "+#imap/%s@%s#%s")
(defvar mew-nntp-folder "+#nntp/%s@%s#%s")

(defun mew-pop-folder (&optional case)
  (format mew-pop-folder
	  (mew-pop-user case) (mew-pop-server case) (mew-pop-port case)))

(defun mew-imap-folder (&optional case)
  (format mew-imap-folder
	  (mew-imap-user case) (mew-imap-server case) (mew-imap-port case)))

(defun mew-nntp-folder (&optional case)
  (format mew-nntp-folder
	  (or (mew-nntp-user case) "") ;; a user may be nil
	  (mew-nntp-server case) (mew-nntp-port case)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Net status
;;;

(defun mew-summary-lock (key status &optional secure)
  (setq mew-summary-buffer-process key)
  (setq mew-summary-buffer-process-status (format " %s" status))
  (setq mew-summary-buffer-secure-process secure))


(defun mew-summary-unlock ()
  (setq mew-summary-buffer-process nil)
  (setq mew-summary-buffer-process-status nil)
  (setq mew-summary-buffer-secure-process nil)
  (force-mode-line-update))

(defun mew-net-status-clear (buf)
  (when (and mew-use-net-status buf (get-buffer buf))
    (with-current-buffer buf
      (setq mew-summary-buffer-process-status nil)
      (setq mew-summary-buffer-secure-process nil))))

(defun mew-net-status (buf status &optional substatus secure)
  (when mew-use-net-status
    (with-current-buffer buf
      (if substatus
	  (setq mew-summary-buffer-process-status
		(format " %s:%s" status substatus))
	(setq mew-summary-buffer-process-status (format " %s" status)))
      (setq mew-summary-buffer-secure-process secure))))

(defun mew-net-status1 (buf msg ttl cnt &optional secure)
  (when mew-use-net-status
    (when (<= cnt ttl)
      (let ((substatus (format "%02d%%" (/ (* cnt 100) ttl))))
	(mew-net-status buf msg substatus secure)))))

(defvar mew-net-status-percent-size 51200)

(defun mew-net-status2 (buf rttl rcnt siz &optional zero secure)
  (when mew-use-net-status
    (when (<= rcnt rttl)
      (let ((msiz (string-to-number siz))
	    (bsiz (if zero 0 (buffer-size)))
	    percent substatus)
	(if (and (integerp mew-net-status-percent-size)
		 (< msiz mew-net-status-percent-size))
	    (progn
	      (setq substatus (format "%d/%d" rcnt rttl))
	      (mew-net-status buf "Retrieving" substatus secure))
	  (if (= msiz 0) (setq msiz 1))
	  (if (< 10000 msiz)
	      (setq percent (/ bsiz (/ msiz 100)))
	    (setq percent (/ (* bsiz 100) msiz)))
	  (setq substatus (format "%d/%d:%02d%%" rcnt rttl percent))
	  (mew-net-status buf "Retrieving" substatus secure))))))

(defvar mew-local-status-unit 10)
(defvar mew-local-status-threshold 100)

(defun mew-net-status3 (buf rttl rcnt)
  (when mew-use-net-status
    (if rttl
	(if (or (not (integerp mew-local-status-unit))
		(not (integerp mew-local-status-threshold))
		(< (- rttl rcnt) mew-local-status-threshold)
		(= 0 (% rcnt mew-local-status-unit)))
	    (mew-net-status buf "Scanning" (format "%d/%d" rcnt rttl)))
      (mew-net-status buf "Scanning" (format "%d" rcnt)))))

(defun mew-summary-visible-buffer (buf)
  (with-current-buffer buf
    (mew-elet (put-text-property (point-min) (point-max) 'invisible nil))
    (setq mew-summary-buffer-raw t)
    (if (eq (get-buffer-window buf) (selected-window))
	(mew-summary-cook-window))
    (set-buffer-modified-p nil)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Biff
;;;

(defvar mew-biff-string nil)
(defvar mew-biff-timer-id nil)

(defun mew-biff-bark (n)
  (if (= n 0)
      (setq mew-biff-string nil)
    (if (and mew-use-biff-bell (eq mew-biff-string nil))
	(beep))
    (setq mew-biff-string (format "Mail(%d)" n))))

(defun mew-biff-clear ()
  (setq mew-biff-string nil))

(defun mew-biff-setup ()
  (let ((inbox (mew-proto-inbox-folder nil mew-case))
	func)
    (if (not mew-use-biff)
	(mew-biff-clean-up)
      (if mew-biff-timer-id (cancel-timer mew-biff-timer-id))
      (cond
       ((mew-folder-localp inbox)
	(setq func 'mew-pop-biff))
       ((mew-folder-popp inbox)
	(setq func 'mew-pop-biff))
       ((mew-folder-imapp inbox)
	(setq func 'mew-imap-biff)))
      (if func
	  (setq mew-biff-timer-id (mew-timer (* 60 mew-biff-interval) func)))))
  (let ((ent '(mew-biff-string mew-biff-string)))
    (unless (member ent global-mode-string)
      (if global-mode-string
	  (setq global-mode-string
		(append global-mode-string (list "" ent)))
	(setq global-mode-string (list ent))))))

(defun mew-biff-clean-up ()
  (if mew-biff-timer-id (cancel-timer mew-biff-timer-id))
  (setq mew-biff-timer-id nil))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Keep time
;;;

(defun mew-time-diff (t1 t2)
  (/ (+ (* (- (nth 0 t2) (nth 0 t1)) 65536)
	(- (nth 1 t2) (nth 1 t1)))
     86400.0)) ;; one day (* 60 60 24)

(defun mew-expired-p (time keep)
  (cond
   ((and (consp keep)
	 (stringp (nth 0 keep)) (file-exists-p (nth 0 keep))
	 (integerp (nth 1 keep)))
    (if (>= (mew-time-diff time (mew-file-get-time (nth 0 keep))) (nth 1 keep))
	t))
   ((integerp keep)
    (if (>= (mew-time-diff time (current-time)) keep) t))
   ;; ((eq keep t) t)
   ;; This case MUST not be included because messages marked with 'T'
   ;; will be deleted.
   (t nil)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; UIDL
;;;

(defvar mew-net-uidl-file ".mew-uidl")
(defvar mew-net-uidl-db nil)

(defun mew-net-uidl-db-get (tag)
  (cdr (assoc tag mew-net-uidl-db)))

(defun mew-net-uidl-db-set (tag uidl)
  (let* ((ent (assoc tag mew-net-uidl-db)))
    (if ent
	(setcdr ent uidl)
      (setq mew-net-uidl-db (cons (cons tag uidl) mew-net-uidl-db)))
    (mew-lisp-save mew-net-uidl-file mew-net-uidl-db nil 'unlimit)))

(defun mew-net-setup ()
  (setq mew-net-uidl-db (mew-lisp-load mew-net-uidl-file))
  (add-hook 'kill-emacs-hook 'mew-net-clean-up))

(defun mew-net-clean-up ()
  (remove-hook 'kill-emacs-hook 'mew-net-clean-up)
  (mew-lisp-save mew-net-uidl-file mew-net-uidl-db nil 'unlimit))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; New message
;;;

(defun mew-net-get-new-message (pnm bnm msg get set)
  (if msg
      (cons msg (mew-expand-msg bnm msg)) ;; 'get
    (let ((msg (funcall get pnm))
	  file nxt)
      (cond
       (msg
	(setq nxt (number-to-string (1+ (string-to-number msg))))
	(funcall set pnm nxt)
	(setq file (mew-expand-new-msg bnm msg)))
       (t
	(setq file (mew-folder-new-message bnm))
	;; 1.eml => 1
	(setq msg (file-name-sans-extension (file-name-nondirectory file)))
	(setq nxt (number-to-string (1+ (string-to-number msg))))
	(funcall set pnm nxt)))
      (cons msg file))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Queue
;;;

(defun mew-queue-check-new-message (file)
  (if (not (file-exists-p (concat file mew-queue-work-suffix)))
      file
    (let* ((dir (file-name-directory file))
	   ;; 1.eml => 1
	   (num (file-name-sans-extension (file-name-nondirectory file)))
	   (n (1+ (string-to-number num))))
      (while (or (file-exists-p (format "%s%d" dir n))
		 (file-exists-p (format "%s%d%s" dir n mew-queue-work-suffix)))
	(setq n (1+ n)))
      (format "%s%d" dir n))))

(defun mew-queue-enqueue (work qfld) ;; 1.wrk or draft/1
  (let (orig info iwrk)
    (if (not (string-match (concat mew-queue-work-suffix "$") work))
	;; draft/1
	(setq orig (mew-queue-check-new-message (mew-folder-new-message qfld)))
      ;; 1.wrk
      (setq orig (file-name-sans-extension work)) ;; 1
      (setq iwrk (concat orig mew-queue-info-suffix mew-queue-work-suffix))
      (mew-delete-file iwrk) ;; 1.mqi.wrk
      (if (file-exists-p orig)
	  (setq orig (mew-queue-check-new-message (mew-folder-new-message qfld)))))
    (rename-file work orig) ;; An error is signaled if orig exists.
    (unless mew-use-nfs-hack
      ;; This cause an error when using NFS.
      (mew-set-file-modes orig))
    (setq info (concat orig mew-queue-info-suffix))
    (list orig info)))

(defun mew-queue-enqueue2 (work) ;; 1.wrk
  (let* ((dir (file-name-directory work))
	 (orig (file-name-sans-extension work)) ;; 1
	 (info (concat orig mew-imapq-info-suffix)) ;; 1.iqi
	 (iwrk (concat info mew-queue-work-suffix))) ;; 1.iqi.wrk
    (when (file-exists-p orig)
      (setq orig (mew-queue-check-new-message (mew-folder-new-message dir)))
      (setq info (concat orig mew-imapq-info-suffix)))
    (rename-file work orig) ;; An error is signaled if orig exists.
    (rename-file iwrk info 'override)))

(defun mew-queue-insert-file (pnm n qfld msg) ;; 1
  (let* ((file (mew-expand-new-msg qfld msg))
	 (work (concat file mew-queue-work-suffix)) ;; 1.wrk
	 (info (concat file mew-queue-info-suffix)) ;; 1.mqi
	 (iwrk (concat info mew-queue-work-suffix)) ;; 1.mqi.wrk
	 buf data)
    (when (and (file-readable-p file) (file-readable-p info))
      (rename-file file work 'override)
      ;; If an old buffer exists by accident, we MUST remove the buffer.
      (if (setq buf (get-file-buffer work)) (mew-remove-buffer buf))
      (mew-frwlet mew-cs-text-for-read mew-cs-dummy
	(set-buffer (mew-find-file-noselect work)))
      (setq data (mew-lisp-load info))
      (dotimes (i n)
	(aset (mew-info pnm) i (aref data i)))
      (rename-file info iwrk 'override)
      t)))

(defun mew-queue-get-next (pnm qfld msgs n func)
  (let (flushp)
    (catch 'loop
      (dolist (msg msgs)
	(if (mew-queue-insert-file pnm n qfld msg)
	    (throw 'loop (setq flushp t)))))
    (if (fboundp func) (funcall func pnm msgs)) ;; set-messages
    flushp))

(defun mew-queue-backup (work info-suffix) ;; 1.wrk
  (let* ((orig (file-name-sans-extension work)) ;; 1
	 (back (mew-prepend-prefix orig mew-backup-prefix)) ;; #1
	 (info (concat orig info-suffix)) ;; 1.sfx
	 (iwrk (concat info mew-queue-work-suffix)) ;; 1.sfx.wrk
	 (ibck (mew-prepend-prefix info mew-backup-prefix))) ;; #1.sfx
    (if (file-exists-p work) (rename-file work back 'override))
    (if (file-exists-p iwrk) (rename-file iwrk ibck 'override))
    back))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Fcc:
;;;

(defun mew-net-fcc-message (case fcc file)
  (let (flds fcc-file link fld imapp)
    (dolist (fc fcc)
      (if (mew-folder-imapp fc)
	  (progn
	    (setq flds (cons (mew-folder-imap-to-fcc case fc) flds))
	    (setq imapp t))
	(setq flds (cons fc flds))))
    (catch 'loop
      (while flds ;; cannot use dolist
	(setq fld (car flds))
	(mew-local-folder-check fld)
	(setq flds (cdr flds))
	(setq fcc-file (mew-folder-new-message fld))
	(when fcc-file
	  (copy-file file fcc-file)
	  (mew-set-file-modes fcc-file)
	  (mew-touch-folder fld)
	  (throw 'loop nil))))
    (dolist (fld flds)
      (mew-local-folder-check fld)
      (setq link (mew-folder-new-message fld))
      (when link
	(mew-link fcc-file link)
	(mew-touch-folder fld)))
    imapp))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Syncing
;;;

(defun mew-net-folder-sync (bnm hlds)
  (let (uid msg ulks new-hlds)
    (save-excursion
      (goto-char (point-min))
      (while (not (eobp))
	(setq uid (mew-summary-message-uid))
	(if (setq new-hlds (member uid hlds))
	    (progn
	      (setq hlds (cdr new-hlds))
	      (forward-line))
	  (setq msg (mew-summary-message-number))
	  (setq ulks (cons msg ulks))
	  (mew-mark-kill-line))))
    (when ulks
      (setq ulks (nreverse ulks))
      (let ((mew-trash-folder nil))
	(mew-mark-exec-delete bnm ulks))
      (mew-summary-folder-cache-save))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Cleaning
;;;

;; See also mew-summary-folder-cache-clean.
(defun mew-net-folder-clean ()
  (let* ((bnm (mew-summary-folder-name 'ext))
	 (dir (mew-expand-folder bnm))
	 (msgs (mew-dir-messages dir mew-regex-message-files3)))
    (mew-summary-unlink-msgs bnm msgs)
    (mew-erase-buffer)
    (setq mew-summary-buffer-raw nil)
    (mew-summary-folder-cache-save)))

(defun mew-net-invalid-cache-start ()
  (save-excursion
    (goto-char (point-min))
    (when (re-search-forward mew-regex-sumsyn-invalid nil t)
      (beginning-of-line)
      (point))))

(defun mew-net-invalid-cache-invisible ()
  (let ((beg (mew-net-invalid-cache-start)))
    (when beg
      (mew-elet
       (put-text-property beg (point-max) 'invisible t)))))

(defun mew-net-invalid-cache-clean ()
  (let* ((bnm (mew-summary-folder-name 'ext))
	 (dir (mew-expand-folder bnm))
	 (msgs (mew-dir-messages dir mew-regex-message-files4)))
    (mew-summary-unlink-msgs bnm msgs)
    (mew-mark-kill-invisible)
    (mew-summary-folder-cache-save)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Retrieving messages
;;;

(defun mew-summary-retrieve-gap (folder)
  (when (mew-summary-folder-dir-newp)
    (goto-char (point-max))
    (let ((range (mew-range-update folder)))
      (mew-local-retrieve 'scan folder range)
      (mew-rendezvous mew-summary-buffer-process))))

(defun mew-summary-case-proto ()
  (let (case:folder case proto)
    (cond
     ((mew-virtual-for-one-summary)
      (setq case:folder (mew-summary-physical-folder))
      (setq case (or (mew-case:folder-case case:folder)
		     mew-case))
      (setq proto (mew-folder-prefix (mew-case:folder-folder case:folder))))
     ((mew-virtual-p)
      (setq case (or mew-case mew-case-default))
      (setq proto (mew-proto case)))
     (t ;; Summary
      (setq case (or (mew-sinfo-get-case)   ;; remote
		     mew-case))	            ;; local
      (setq proto (mew-folder-prefix (mew-sinfo-get-folder)))))
    (list case proto)))

(defun mew-summary-retrieve (&optional no-flush)
  "In local folders, retrieve messages to +inbox asynchronously
according to 'mew-mailbox-type' and 'mew-case'.
If 'mew-auto-flush-queue' is non-nil, +queue is flushed.
If called with '\\[universal-argument]', +queue is not flushed.

In remote folders, visit an inbox folder and scan with 'update."
  (interactive "P")
  (let (case proto inbox case:inbox mailbox)
    (mew-set '(case proto) (mew-summary-case-proto))
    (setq inbox (mew-proto-inbox-folder proto case))
    (cond
     ((mew-folder-remotep proto)
      (setq case:inbox (mew-case-folder case inbox)))
     (t ;; local
      (setq case:inbox inbox)))
    ;; for mew-summary-exchange-point.
    (mew-sinfo-set-ret-pos (point))
    (mew-summary-visit-folder case:inbox)
    ;; in the inbox
    (when (mew-summary-exclusive-p)
      (cond
       ((mew-folder-remotep inbox)
	(mew-summary-ls nil 'goend 'update))
       (t ;; local
	;;
	(mew-summary-reset)
	(mew-summary-retrieve-gap inbox)
	;;
	(goto-char (point-max))
	(mew-sinfo-set-start-point (point))
	(mew-sinfo-set-direction 'down)
	(setq mailbox (mew-mailbox-type case))
	(cond
	 ((eq mailbox 'pop)
	  (mew-pop-retrieve case 'inc inbox (not no-flush)))
	 ((eq mailbox 'imap)
	  (mew-imap-retrieve case 'inc inbox (not no-flush)))
	 ((eq mailbox 'mbox)
	  (mew-mbox-retrieve case 'inc inbox (not no-flush)))))))))

(defun mew-summary-scan-boot (proto case)
  ;; called by mew()
  (let* ((inbox (mew-proto-inbox-folder proto case))
	 (case:inbox (mew-case-folder case inbox))
	 (dir (mew-expand-folder case:inbox))
	 mailbox)
    (unless (file-directory-p dir)
      (mew-make-directory dir))
    (mew-summary-switch-to-folder case:inbox)
    ;; in the inbox
    ;; for mew-summary-exchange-point.
    (mew-sinfo-set-ret-pos (point))
    (when (mew-summary-exclusive-p)
      (mew-summary-reset)
      ;;
      (if (mew-folder-localp inbox) (mew-summary-retrieve-gap case:inbox))
      ;;
      (goto-char (point-max))
      (mew-sinfo-set-start-point (point))
      (mew-sinfo-set-direction 'down)
      (cond
       ((mew-folder-localp inbox)
	(setq mailbox (mew-mailbox-type case))
	(cond
	 ((eq mailbox 'pop)
	  (mew-pop-retrieve case 'inc inbox))
	 ((eq mailbox 'imap)
	  (mew-imap-retrieve case 'inc inbox))
	 ((eq mailbox 'mbox)
	  (mew-mbox-retrieve case 'inc inbox))))
       ((mew-folder-popp inbox)
	(let ((get-body (if (mew-pop-header-only case) nil t)))
	  (mew-pop-retrieve case 'scan case:inbox nil get-body)))
       ((mew-folder-imapp inbox)
	(let ((get-body (if (mew-imap-header-only case) nil t)))
	  (mew-imap-retrieve case 'scan case:inbox nil get-body)))
       ((mew-folder-nntpp inbox)
	(let ((get-body (if (mew-nntp-header-only case) nil t)))
	  (mew-nntp-retrieve case 'scan case:inbox nil get-body)))))))

(defun mew-mbox-retrieve (case directive inbox &optional flush)
  ;; directive is 'inc
  (let* ((mbox-command (mew-mbox-command case))
	 (mbox-command-arg (mew-mbox-command-arg case))
	 dir mode opts)
    (if (not (mew-which-exec mbox-command))
	(message "'%s' not found!" mbox-command)
      (if mbox-command-arg
	  (if (not (string= mbox-command "incm"))
	      (setq opts (list "-e" mbox-command "-m" mbox-command-arg))
	    (setq dir (mew-expand-folder inbox))
	    (setq mode (mew-get-file-modes dir))
	    (if mew-use-suffix
		(setq opts (list "-e" mbox-command
				 "-m" (format "%s -o -p %d" mbox-command-arg mode)))
	      (setq opts (list "-e" mbox-command
			       "-m" (format "%s -p %d" mbox-command-arg mode)))))
	(setq opts (list "-e" mbox-command)))
      (mew-local-retrieve 'inc inbox flush opts))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Retrieving truncated messages
;;;

(defun mew-summary-get-inherit-case (fld msg)
  (let ((buf (mew-cache-hit fld msg))
	uid)
    (if buf
	(with-current-buffer buf
	  (setq uid (mew-header-get-value mew-x-mew-uidl:)))
      (with-temp-buffer
	(mew-insert-message fld msg mew-cs-text-for-read mew-header-reasonable-size)
	(setq uid (mew-header-get-value mew-x-mew-uidl:))))
    (mew-scan-uid-case uid)))

(mew-defstruct net-virtual-info virtual summary msgs threadp)

(defun mew-net-virtual-info (msgs)
  (mew-make-net-virtual-info :virtual (mew-summary-folder-name 'ext)
			     :summary (mew-summary-folder-name)
			     :msgs    msgs
			     :threadp (mew-thread-p)))

(mew-defstruct net-disp-info list fid buf fld msg)

(defun mew-net-disp-info ()
  (let* ((fid (mew-frame-id))
	 (buf (current-buffer))
	 (fld (mew-current-get-fld fid))
	 (msg (mew-current-get-msg fid))
	 (disp (mew-sinfo-get-disp-msg)))
    (if disp (mew-make-net-disp-info :fid fid :buf buf :fld fld :msg msg))))

(defun mew-net-disp-info-display (disp-info)
  (when disp-info
    (let ((fid (mew-net-disp-info-get-fid disp-info))
	  (buf (mew-net-disp-info-get-buf disp-info))
	  (fld (mew-net-disp-info-get-fld disp-info))
	  (msg (mew-net-disp-info-get-msg disp-info))
	  (nfid (mew-frame-id))
	  m)
      (when (and (equal fid nfid)
		 (equal fld (mew-current-get-fld nfid)))
	(with-current-buffer buf
	  (setq m (point-marker))
	  (set-marker-insertion-type m t)
	  (when (and (mew-sinfo-get-disp-msg)
		     (equal msg (mew-current-get-msg nfid))
		     (mew-summary-search-msg msg))
	    ;; The message was truncated probaby because it's too large.
	    (mew-summary-analyze-again))
	  (goto-char m))))))

(defun mew-summary-retrieve-message (&optional rev-del)
  "Retrieve the rest of a truncated('T') message.

In a LOCAL folder: a method to retrieve the message is determined
by 'mew-case' and 'mew-mailbox-type'.
If 'mew-pop-delete'/'mew-imap-delete' is non-nil, delete the message
from the mailbox. When executed with '\\[universal-argument]',
'mew-pop-delete'/'mew-imap-delete' is considered reversed.

In a REMOTE folder: case and protocol are determined by the folder.
The message in the server side is always retained."
  (interactive "P")
  (mew-pickable
   (mew-summary-msg
    (when (mew-sumsyn-match mew-regex-sumsyn-long)
      (let* ((msg (mew-sumsyn-message-number))
	     (uid (mew-sumsyn-message-uid))
	     (siz (mew-sumsyn-message-size))
	     (bnm (mew-summary-folder-name))
	     (disp-info (mew-net-disp-info))
	     (virtual-info (if (mew-virtual-for-one-summary)
			       (mew-net-virtual-info (list msg))))
	     folder case del rtr rtrs mailbox)
	(with-current-buffer bnm
	  (when (mew-summary-exclusive-p)
	    (setq folder (mew-sinfo-get-folder))
	    (setq case (mew-sinfo-get-case))
	    (when (mew-folder-localp folder)
	      (setq case (or (mew-summary-get-inherit-case folder msg) mew-case))
	      (setq mailbox (mew-mailbox-type case))
	      (cond
	       ((eq mailbox 'pop)
		(setq del (eq (mew-pop-delete case) t))) ;; delete may be number
	       ((eq mailbox 'imap)
		(setq del (eq (mew-imap-delete case) t)))) ;; delete may be number
	      (if rev-del (setq del (not del))))
	    (when (and uid (not (string= uid "")))
	      (setq rtr (mew-make-refileinfo :uid uid
					     :size siz
					     :delete del
					     :folders (list bnm msg)))
	      (setq rtrs (list rtr)))
	    (cond
	     ((not rtrs)
	      (message "No message to be retrieved"))
	     ((not (mew-msg-validp msg))
	      (message "No valid message to be retrieved"))
	     ((mew-folder-localp folder)
	      (cond
	       ((eq mailbox 'pop)
		(mew-pop-retrieve case 'get bnm rtrs virtual-info disp-info))
	       ((eq mailbox 'imap)
		(mew-imap-retrieve case 'get bnm rtrs virtual-info disp-info))))
	     ((mew-folder-popp folder)
	      (mew-pop-retrieve case 'get bnm rtrs virtual-info disp-info))
	     ((mew-folder-imapp folder)
	      (mew-imap-retrieve case 'get bnm rtrs virtual-info disp-info))
	     ((mew-folder-nntpp folder)
	      (mew-nntp-retrieve case 'get bnm rtrs virtual-info disp-info))))))))))

(defun mew-summary-retrieve-message-for-virtual-summary (msg)
  (save-excursion
    (when (mew-summary-search-msg msg)
      (let ((start (point)))
	(forward-line)
	(buffer-substring start (point))))))

(defun mew-summary-retrieve-message-for-virtual-selection (msg str)
  (save-excursion
    (when (mew-summary-search-msg msg)
      (let ((start (point)))
	(forward-line)
	(mew-elet
	 (delete-region start (point))
	 (insert str))))))

(defun mew-summary-retrieve-message-for-virtual-thread (msg str)
  (save-excursion
    (when (mew-summary-search-msg msg)
      (let* ((start (point))
	     (beg (mew-thread-next-property start))
	     (level (mew-thread-get-property beg))
	     (length (* mew-thread-indent-length level))
	     (end (+ beg length))
	     (idt-str (buffer-substring beg end)))
	(forward-line)
	(mew-elet
	 (delete-region start (point))
	 (insert str)
	 (goto-char beg)
	 (if (= level 0) (setq end (1+ end)))
	 (insert idt-str)
	 (mew-thread-put-property beg end level))))))

(defun mew-summary-retrieve-message-for-virtual (virtual-info)
  (let ((virtual (mew-net-virtual-info-get-virtual virtual-info))
	(summary (mew-net-virtual-info-get-summary virtual-info))
	(msgs    (mew-net-virtual-info-get-msgs    virtual-info))
	(func    (if (mew-net-virtual-info-get-threadp virtual-info)
		     'mew-summary-retrieve-message-for-virtual-thread
		   'mew-summary-retrieve-message-for-virtual-selection))
	str)
    (save-excursion
      (dolist (msg msgs)
	(set-buffer summary)
	;; Summary
	(setq str (mew-summary-retrieve-message-for-virtual-summary msg))
	(set-buffer virtual)
	;; Virtual
	(funcall func msg str))
      (mew-summary-unlock))))

(defun mew-summary-mark-retrieve-message (&optional rev-del)
  "Retrieve the rest of truncated('T') messages marked with '*'.

In a LOCAL folder: a method to retrieve the messages is determined by
'mew-case' and 'mew-mailbox-type'.
If 'mew-pop-delete'/'mew-imap-delete' is non-nil, delete the messages
from the mailbox.  When executed with '\\[universal-argument]',
'mew-pop-delete'/'mew-imap-delete' is considered reversed.

In a REMOTE folder: case and protocol are determined by the folder.
The messages in the server side is always retained."
  (interactive "P")
  (mew-summary-only
   (let* ((bnm (mew-summary-folder-name 'ext))
	  (folder (mew-sinfo-get-folder))
	  (case (mew-sinfo-get-case))
	  msg uid siz del rtr rtrs case-rtrs mailbox)
     (save-excursion
       (goto-char (point-min))
       (if (mew-folder-localp folder)
	   (let (tmp)
	     (while (re-search-forward mew-regex-msg-review nil t)
	       (when (mew-sumsyn-match mew-regex-sumsyn-long)
		 (setq uid (mew-sumsyn-message-uid))
		 (setq msg (mew-sumsyn-message-number))
		 (setq siz (mew-sumsyn-message-size))
		 (setq case (or (mew-summary-get-inherit-case folder msg) mew-case))
		 (setq mailbox (mew-mailbox-type case))
		 (when (and (mew-msg-validp msg) (mew-msg-truncatedp siz))
		   (cond
		    ((eq mailbox 'pop)
		     (setq del (eq (mew-pop-delete case) t))) ;; delete may be number
		    ((eq mailbox 'imap)
		     (setq del (eq (mew-imap-delete case) t))))	;; delete may be number
		   (if rev-del (setq del (not del)))
		   (setq rtr (mew-make-refileinfo :uid uid
						  :size siz
						  :delete del
						  :folders (list bnm msg)))
		   (if (setq tmp (nth 2 (assoc case case-rtrs)))
		       (nconc tmp (list rtr))
		     (setq case-rtrs (cons (list case mailbox (list rtr)) case-rtrs))))
		 (forward-line)))
	     (setq case-rtrs (nreverse case-rtrs)))
	 (while (re-search-forward mew-regex-msg-review nil t)
	   (when (mew-sumsyn-match mew-regex-sumsyn-long)
	   (setq uid (mew-sumsyn-message-uid))
	   (setq msg (mew-sumsyn-message-number))
	   (setq siz (mew-sumsyn-message-size))
	   (when (and (mew-msg-validp msg) (mew-msg-truncatedp siz))
	     (setq rtr (mew-make-refileinfo :uid uid
					    :size siz
					    :delete del
					    :folders (list bnm msg)))
	     (setq rtrs (cons rtr rtrs))))
	   (forward-line))
	 (setq rtrs (nreverse rtrs))))
     (if (and (not rtrs) (not case-rtrs))
	 (message "No message to be retrieved")
       (cond
	((mew-folder-localp folder)
	 (while case-rtrs ;; cannot use dolist
	   (setq case (nth 0 (car case-rtrs)))
	   (setq mailbox (nth 1 (car case-rtrs)))
	   (setq rtrs (nth 2 (car case-rtrs)))
	   (setq case-rtrs (cdr case-rtrs))
	   (cond
	    ((eq mailbox 'pop)
	     (mew-pop-retrieve case 'get bnm rtrs))
	    ((eq mailbox 'imap)
	     (mew-imap-retrieve case 'get bnm rtrs)))
	   (when case-rtrs
	     (mew-rendezvous mew-summary-buffer-process))))
	((mew-folder-popp folder)
	 (mew-pop-retrieve case 'get bnm rtrs))
	((mew-folder-imapp folder)
	 (mew-imap-retrieve case 'get bnm rtrs))
	((mew-folder-nntpp folder)
	 (mew-nntp-retrieve case 'get bnm rtrs)))))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Folder clean up
;;;

(defun mew-folder-clean-up ()
  (remove-hook 'kill-emacs-hook 'mew-folder-clean-up)
  (mew-local-folder-clean-up)
  (mew-imap-folder-clean-up)
  (mew-nntp-folder-clean-up))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Message packing
;;;

(defun mew-net-msg-pack (msgs)
  ;; (mew-net-msg-pack '("1" "3" "4" "5" "7" "8" "10"))
  ;; => ("1" "3:5" "7:8" "10")
  (let (pack a b s)
    (when (setq a (car msgs))
      (setq a (string-to-number a))
      (setq msgs (cdr msgs))
      (while (setq b (car msgs))
	(setq b (string-to-number b))
	(setq msgs (cdr msgs))
	(if (= (1+ a) b)
	    (if (null s)
		(setq s a))
	  (if (null s)
	      (setq pack (cons (format "%d" a) pack))
	    (setq pack (cons (format "%d:%d" s a) pack))
	    (setq s nil)))
	(setq a b))
      (if s
	  (setq pack (cons (format "%d:%d" s a) pack))
	(setq pack (cons (format "%d" a) pack)))
      (nreverse pack))))

(defvar mew-imap-message-cat-size 10)

(defun mew-net-msg-cat (lst)
  (let ((N (1- mew-imap-message-cat-size))
	(crn lst)
	prv nxt ret)
    (while (setq prv (nthcdr N crn))
      (setq nxt (cdr prv))
      (setcdr prv nil)
      (setq ret (cons (mew-join "," crn) ret))
      (setq crn nxt))
    (if crn (setq ret (cons (mew-join "," crn) ret)))
    (nreverse ret)))

(defun mew-net-msg-group (lst)
  (if (= (length lst) 1)
      lst ;; "1:*"
    (mew-net-msg-cat (mew-net-msg-pack lst))))

(provide 'mew-net)

;;; Copyright Notice:

;; Copyright (C) 2002-2015 Mew developing team.
;; All rights reserved.

;; Redistribution and use in source and binary forms, with or without
;; modification, are permitted provided that the following conditions
;; are met:
;;
;; 1. Redistributions of source code must retain the above copyright
;;    notice, this list of conditions and the following disclaimer.
;; 2. Redistributions in binary form must reproduce the above copyright
;;    notice, this list of conditions and the following disclaimer in the
;;    documentation and/or other materials provided with the distribution.
;; 3. Neither the name of the team nor the names of its contributors
;;    may be used to endorse or promote products derived from this software
;;    without specific prior written permission.
;;
;; THIS SOFTWARE IS PROVIDED BY THE TEAM AND CONTRIBUTORS ``AS IS'' AND
;; ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
;; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
;; PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE TEAM OR CONTRIBUTORS BE
;; LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
;; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
;; SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
;; BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
;; OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
;; IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

;;; mew-net.el ends here