File: sending.adb

package info (click to toggle)
topal 72-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 1,004 kB
  • ctags: 102
  • sloc: ada: 9,192; ansic: 768; sh: 233; makefile: 148
file content (1181 lines) | stat: -rw-r--r-- 56,090 bytes parent folder | download
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
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
-- Topal: GPG/GnuPG and Alpine/Pine integration
-- Copyright (C) 2001--2010  Phillip J. Brooke
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License version 3 as
-- published by the Free Software Foundation.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program.  If not, see <http://www.gnu.org/licenses/>.

with Ada.Command_Line;
with Ada.Strings.Fixed;
with Ada.Text_IO;
with Configuration;
with Attachments;
with Externals;             use Externals;
with Externals.GPG;
with Externals.Mail;
with Externals.Simple;      use Externals.Simple;
with Keys;
with Menus;                 use Menus;
with Misc;                  use Misc;
with Readline;
with Remote_Mode;

package body Sending is

   Send_Modes_Text : constant array (Send_Modes) of String(1..34)
     := (Encrypt       => "Encrypt                           ",
         SignEncrypt   => "Sign + encrypt                    ",
         ClearSign     => "Clear sign                        ",
         NoGPG         => "No GPG                            ",
         EncryptPO     => "Encrypt (preserve original)       ",
         SignEncryptPO => "Sign + encrypt (preserve original)",
         ClearSignPO   => "Clear sign (preserve original)    ",
         DetachSignPO  => "Detached signature                ");

   Mime_Modes_Text : constant array (Mime_Modes) of String(1..22)
     := (InlinePlain    => "Inline plain          ",
         AppPGP         => "Application/PGP       ",
         Multipart      => "Multipart             ",
         MultipartEncap => "Multipart encapsulated");

   Exit_Send_Loop : exception; -- Subroutine says `bail out, please'.

   procedure Key_Preselect (K          : in out Keys.Key_List;
                            Recipients : in     UBS_Array;
                            Alt_Sign   : in     String       := "";
                            Missing    :    out Boolean) is
   begin
      -- Can we preselect any keys?
      Keys.Empty_Keylist(K);
      Keys.Use_Keylist(Recipients, K, Missing);
      -- Add our own key.  If Alt_Sign is non-empty, then try and add
      --  keys for that instead.
      declare
         AKL     : Keys.Key_List;
         Found   : Boolean       := False;
         KC      : Natural;
         The_Key : UBS;
      begin
         if Alt_Sign /= "" then
            Ada.Text_IO.Put_Line("Looking for signing key for `"&Alt_Sign&"' on SAKE list...");
            for J in 1..Config.SAKE_Count loop
               if ToStr(UAP.Value(Config.SAKE_Email, J)) = Alt_Sign then
                  Keys.Add_Keys_By_Fingerprint(UAP.Value(Config.SAKE_Key, J),
                                               K, Keys.Both, Found);
                  if Found then
                     Ada.Text_IO.Put_Line(Do_SGR(Config.UBS_Opts(Colour_Important))
                                            & "Added key `"&ToStr(UAP.Value(Config.SAKE_Key, J))&"' for signing..."
                                         & Reset_SGR);
                     Config.UBS_Opts(My_Key) := UAP.Value(Config.SAKE_Key, J);
                  else
                     -- Try again, but only as an encryptor.
                     Keys.Add_Keys_By_Fingerprint(UAP.Value(Config.SAKE_Key, J),
                                                  K, Keys.Encrypter, Found);
                     if Found then
                        Ada.Text_IO.Put_Line(Do_SGR(Config.UBS_Opts(Colour_Important))
                                               & "Added own key `"&ToStr(UAP.Value(Config.SAKE_Key, J))&"' BUT only as encryptor..."
                                               & Reset_SGR);
                        -- We'll not change My_Key here.
                     else
                        Ada.Text_IO.Put_Line(Do_SGR(Config.UBS_Opts(Colour_Important))
                                               & "Couldn't add key `"&ToStr(UAP.Value(Config.SAKE_Key, J))&"' for signing..."
                                               & Reset_SGR);
                     end if;
                  end if;
               end if;
            end loop;
         end if;
         if Alt_Sign /= "" and (not Found) then
            Keys.Empty_Keylist(AKL);
            Ada.Text_IO.Put_Line("Looking for signing key for `"&Alt_Sign&"' via keyring (& XK list)...");
            Keys.Add_Secret_Keys(ToUBS(Alt_Sign),
                                 AKL,
                                 Keys.Both);
            -- Any keys to remove on the SXK list?
            for J in 1..Config.SXK_Count loop
               Keys.Remove_Key(UAP.Value(Config.SXK_Key, J), AKL);
            end loop;
            KC := Keys.Count(AKL);
            Found := KC > 0;
            if Found then
               if KC = 1 then
                  Ada.Text_IO.Put_Line(Do_SGR(Config.UBS_Opts(Colour_Important))
                                         & "Added one signing key..."
                                         & Reset_SGR);
                  -- Add this one.
                  Keys.First_Key_From_List(AKL, The_Key);
                  Keys.Add_Key(The_Key, K, Keys.Both);
                  Config.UBS_Opts(My_Key) := The_Key;
               else
                  -- Multiple keys...
                  Ada.Text_IO.Put_Line("Found multiple possible signing keys...");
                  -- Choose one.  If aborted, fall-back to my-key.
                  Keys.Select_Key_From_List(AKL, The_Key, Found);
                  -- Found is not aborted...
                  Found := not Found;
                  if Found then
                     Keys.Add_Key(The_Key, K, Keys.Both);
                     Config.UBS_Opts(My_Key) := The_Key;
                  else
                     Ada.Text_IO.Put_Line("Aborted selection of secret key, will consider my-key instead");
                  end if;
               end if;
            else
               Ada.Text_IO.Put_Line("Can't find alternative key, trying my-key...");
            end if;
         end if;
         if Alt_Sign /= "" and (not Found) then
            Keys.Add_Keys_By_Fingerprint(ToUBS(Alt_Sign),
                                         K,
                                         Keys.Encrypter,
                                         Found);
            if Found then
               Ada.Text_IO.Put_Line("Added encryptor key(s) for self");
            end if;
         end if;
         if (not Found) then
            if ToStr(Config.UBS_Opts(My_Key)) = "" then
               Ada.Text_IO.Put_Line(Do_SGR(Config.UBS_Opts(Colour_Important))
                                      & "my-key is empty; not selecting any keys for self"
                                      & Reset_SGR);
            else
               Keys.Add_Keys_By_Fingerprint(Value_Nonempty(Config.UBS_Opts(My_Key)),
                                            K,
                                            Keys.Encrypter,
                                            Found);
               if Found then
                  Ada.Text_IO.Put_Line("Added `my-key' key(s) for self");
               else
                  Ada.Text_IO.Put_Line(Do_SGR(Config.UBS_Opts(Colour_Important))
                                            & "Warning: my-key="
                                         & ToStr(Config.UBS_Opts(My_key))
                                         & " does not find keys"
                                         & Reset_SGR);
               end if;
            end if;
         end if;
      end;
   exception
      when others =>
         Ada.Text_IO.Put_Line(Ada.Text_IO.Standard_Error,
                              "Exception raised in Sending.Key_Preselect");
         raise;
   end Key_Preselect;

   procedure Mode_Preselect (K          : in Keys.Key_List;
                             Recipients : in UBS_Array;
                             SI         :    out Send_Modes;
                             MI         :    out MIME_Modes) is
      procedure Set_I (S : in UBS) is
         S2 : constant String := ToStr(S);
         use Ada.Strings.Fixed;
      begin
         if Index(S2, "n") /= 0 then SI := NoGPG; end if;
         if Index(S2, "e") /= 0 then SI := Encrypt; end if;
         if Index(S2, "s") /= 0 then SI := SignEncrypt; end if;
         if Index(S2, "c") /= 0 then SI := ClearSign; end if;
         if Index(S2, "I") /= 0 then MI := InlinePlain; end if;
         if Index(S2, "A") /= 0 then MI := AppPGP; end if;
         if Index(S2, "M") /= 0 then MI := Multipart; end if;
         if Index(S2, "E") /= 0 then MI := MultipartEncap; end if;
      end Set_I;

      use type UBS;
   begin
      SI := NoGPG;
      MI := InlinePlain;
      -- Consider each SD item.  We do them in this order so that
      --  later overrides earlier.
      for J in 1..Config.SD_Count loop
         if ToStr(UAP.Value(Config.SD_KE, J)) = "@ANY@" then
            -- User default!
            Set_I(UAP.Value(Config.SD_Mode, J));
         elsif Ada.Strings.Fixed.Index(ToStr(UAP.Value(Config.SD_KE, J)), "@") /= 0 then
            -- Is it an email address?  If so...
            -- Do any recipients match?
            for I in Recipients'Range loop
               if UAP.Value(Config.SD_KE, J) = Recipients(I) then
                  Set_I(UAP.Value(Config.SD_Mode, J));
               end if;
            end loop;
         else
            -- Do any keys match?
            if Keys.Contains(K, UAP.Value(Config.SD_KE, J)) then
               Set_I(UAP.Value(Config.SD_Mode, J));
            end if;
         end if;
      end loop;
      if SI /= NoGPG then
         Ada.Text_IO.Put_Line(Do_SGR(Config.UBS_Opts(Colour_Important))
                                & "Sending defaults to " & Send_Modes_Text(SI)
                                & Reset_SGR);
      end if;
      if MI /= InlinePlain then
         Ada.Text_IO.Put_Line(Do_SGR(Config.UBS_Opts(Colour_Important))
                                & "Sending defaults to " & Mime_Modes_Text(MI)
                                & Reset_SGR);
      end if;
   exception
      when others =>
         Ada.Text_IO.Put_Line(Ada.Text_IO.Standard_Error,
                              "Exception raised in Sending.Mode_Preselect");
         raise;
   end Mode_Preselect;

   function The_Content_Type return String is
      -- What is the content type for outbound messages?  (at least,
      --  the bit we're bothered about)
      Current_Charset : UBS;
   begin
      Current_Charset := ToUBS(Externals.Simple.Get_Charmap);
      return "text/plain; charset=""" & ToStr(Current_Charset) & """";
   exception
      when others =>
         Ada.Text_IO.Put_Line(Ada.Text_IO.Standard_Error,
                              "Exception raised in Sending.The_Content_Type");
         raise;
   end;


   procedure Check_Send (Tmpfile   : in String;
                         Non_Pine  : in Boolean;
                         Mime      : in Boolean;
                         Mimefile  : in String;
                         Hdrfile   : in String;
                         Recipients : in UBS_Array) is
   begin
      if not Non_Pine then
         if Mime then
            if Externals.Simple.Test_R(Mimefile) then
               Ada.Text_IO.Put_Line("The Content-Type being returned is: ");
               Cat(Mimefile);
            else
               Ada.Text_IO.Put_Line("Unchanged Content-Type.");
            end if;
         end if;
         Wait;
         Pager(Tmpfile);
         Externals.Simple.Clear;
         Ada.Text_IO.Put_Line(Do_SGR(Config.UBS_Opts(Colour_Important))
                                & "Recipients (to, cc, bcc; not lcc): "
                                & Reset_SGR);
         for I in Recipients'Range loop
            Ada.Text_IO.Put_Line("  " & ToStr(Recipients(I)));
         end loop;
         Ada.Text_IO.New_Line(1);
         if YN_Menu(Do_SGR(Config.UBS_Opts(Colour_Important))
                      & "Okay to send? "
                      & Reset_SGR) = No then
            Ada.Command_Line.Set_Exit_Status(Ada.Command_Line.Failure);
         end if;
         -- If save on send is set, then we write a copy of Tmpfile.
         if Config.Boolean_Opts(Save_On_Send) then
            declare
               LM : constant String := ToStr(Topal_Directory) & "/lastmail";
            begin
               -- If the Hdrfile is readable, that first.
               if Hdrfile /= "" and then Test_R(Hdrfile) then
                  Cat_Out(Hdrfile, LM);
                  Echo_Append("", LM);
                  Cat_Append(Tmpfile, LM);
               else
                  Cat_Out(Tmpfile, LM);
               end if;
            end;
         end if;
      end if;
   exception
      when others =>
         Ada.Text_IO.Put_Line(Ada.Text_IO.Standard_Error,
                              "Exception raised in Sending.Check_Send");
         raise;
   end Check_Send;

   procedure Encrypt (Tmpfile   : in String;
                      Non_Pine  : in Boolean;
                      Mime      : in Boolean;
                      Mimefile  : in String;
                      Send_Keys : in Keys.Key_List;
                      Selection : in Send_Modes;
                      Mime_Selection : in MIME_Modes;
                      AL        : in Attachments.Attachment_List;
                      Hdrfile   : in String;
                      Recipients : in UBS_Array) is
      Out_File       : constant String := Temp_File_Name("out");
      -- PCT = Prepend content-type.
   begin
      if Mime then
         begin
            Ada.Text_IO.New_Line(3);
            if Mime_Selection = Multipart then
               declare
                  PCT : constant String := Temp_File_Name("pct");
               begin
                  Echo_Out("Content-Type: " & The_Content_Type, PCT);
                  Echo_Append("", PCT);
                  Cat_Append(Tmpfile, PCT);
                  Mv_F(PCT, Tmpfile);
                  -- Call out to attachments in case we have to modify Tmpfile.
                  Attachments.Replace_Tmpfile(Tmpfile, AL);
               end;
            end if;
         exception
            when others =>
               Ada.Text_IO.Put_Line(Ada.Text_IO.Standard_Error,
                                    "Exception raised in Sending.Encrypt (MIME block 1)");
               raise;
         end;
      end if;

      begin
         -- Run GPG.
         Externals.GPG.GPG_Wrap(ToStr(Config.UBS_Opts(Gpg_Options))
                      & " --encrypt --armor "
                      & ToStr(Config.UBS_Opts(general_options))
                      & " "
                      & ToStr(Config.UBS_Opts(sending_options))
                      & " "
                      & " --output "
                      & Out_File
                      & " "
                      & Keys.Processed_Recipient_List(Send_Keys)
                      & " "
                      & Tmpfile,
                      Out_File);
      exception
         when others =>
            Ada.Text_IO.Put_Line(Ada.Text_IO.Standard_Error,
                                 "Exception raised in Sending.Encrypt (GPG block)");
            raise;
      end;

      begin
         if Selection = EncryptPO then
            -- Rename the file appropriately.
            Mv_F(Out_File, Tmpfile & ".asc");
         else
            Mv_F(Out_File, Tmpfile);
         end if;
      exception
         when others =>
            Ada.Text_IO.Put_Line(Ada.Text_IO.Standard_Error,
                                 "Exception raised in Sending.Encrypt (mv block)");
            raise;
      end;

      if Mime then
         begin
            case Mime_Selection is
               when InlinePlain => -- Inline plain text
                  Echo_Out("Content-Type: text/plain",
                           Mimefile);
               when AppPGP => -- application/pgp
                  Echo_Out_N("Content-Type: application/pgp; format=text; x-action=encrypt ",
                             Mimefile);
               when Multipart => -- RFC2015 multipart
                                 -- This is the nasty one.
                  declare
                     Blk1   : constant String := Temp_File_Name("mp1");
                     Blk2   : constant String := Temp_File_Name("mp2");
                     MC     : constant String := Temp_File_Name("mc");
                  begin
                     -- We first create the two blocks.
                     -- The first block is easy:
                     Echo_Out("Content-Type: application/pgp-encrypted",
                              Blk1);
                     Echo_Append("", Blk1);
                     Echo_Append("Version: 1", Blk1);
                     -- The second block starts with a
                     -- content-type, then is the tmpfile we've
                     -- put together.
                     Echo_Out("Content-Type: application/octet-stream",
                              Blk2);
                     Echo_Append("Content-Disposition: attachment; filename=""message.asc""", Blk2);
                     Echo_Append("", Blk2);
                     Cat_Append(Tmpfile, Blk2);
                     -- Now we put them together.
                     Externals.Mail.Mimeconstruct2(Part1_Filename  => Blk1,
                                                   Part2_Filename  => Blk2,
                                                   Output_Filename => MC,
                                                   Content_Type    => "multipart/encrypted; protocol=""application/pgp-encrypted""",
                                                   Prolog          => "This is an OpenPGP/MIME encrypted message (RFC2440, RFC3156).");
                     -- Now we need to split these up.
                     Mail.Extract_Content_Type_From_Header(MC, Mimefile);
                     Mail.Extract_Body(MC, Tmpfile);
                  end;
               when MultipartEncap =>
                  Error("Menu should not have allowed MultipartEncap here");
            end case;
         exception
            when others =>
               Ada.Text_IO.Put_Line(Ada.Text_IO.Standard_Error,
                                    "Exception raised in Sending.Encrypt (MIME block 2)");
               raise;
         end;
      end if;

      Check_Send(Tmpfile, Non_Pine, Mime, Mimefile, Hdrfile, Recipients);

   exception
      when others =>
         Ada.Text_IO.Put_Line(Ada.Text_IO.Standard_Error,
                              "Exception raised in Sending.Encrypt");
         raise;
   end Encrypt;

   procedure Sign_Encrypt  (Tmpfile   : in String;
                            Non_Pine  : in Boolean;
                            Mime      : in Boolean;
                            Mimefile  : in String;
                            Send_Keys : in Keys.Key_List;
                            Selection : in Send_Modes;
                            Mime_Selection : in MIME_Modes;
                            AL        : in Attachments.Attachment_List;
                            Hdrfile   : in String;
                            Recipients : in UBS_Array) is
      Out_File       : constant String   := Temp_File_Name("out");
      DTBL_File      : constant String   := Temp_File_Name("dtbl");
      QP_File        : constant String   := Temp_File_Name("qp");
      TDS_File       : constant String   := Temp_File_Name("tds");
   begin
      -- Run GPG.
      if Mime and then Mime_Selection = MultipartEncap then
         -- We need to do something different with Tmpfile.
         -- First, we delete all trailing blank lines.
         begin
            Mail.Delete_Trailing_Blank_Lines(Infile  => Tmpfile,
                                             Outfile => DTBL_File);
            -- Then we turn it into quoted printable.
            -- Also turn it into DOS line endings.
            Externals.Mail.Mimeconstruct_Subpart(Infile       => DTBL_File,
                                       Outfile      => QP_File,
                                       Content_Type => The_Content_Type,
                                       Dos2UnixU    => True,
                                       Use_Encoding => True,
                                       Encoding     => "quoted-printable");
            -- Call out to attachments in case we have to modify QP_File.
            Attachments.Replace_Tmpfile(QP_File, AL);
            -- Then we clearsign it.
            Externals.GPG.GPG_Wrap(ToStr(Config.UBS_Opts(Gpg_Options))
                         & " --detach-sign --textmode --armor --local-user "
                         & Value_Nonempty(Config.UBS_Opts(my_key))
                         & " "
                         & ToStr(Config.UBS_Opts(general_options))
                         & " "
                         & ToStr(Config.UBS_Opts(sending_options))
                         & " "
                         & UGA_Str(Signing => True)
                         & " --output "
                         & TDS_File
                         & " "
                         & QP_File,
                         TDS_File);
         exception
            when others =>
               Ada.Text_IO.Put_Line(Ada.Text_IO.Standard_Error,
                                    "Exception raised in Sending.Sign_Encrypt (GPG MIME/4 block)");
               raise;
         end;

      else

         if Mime and then Mime_Selection = Multipart then
            -- We need to do something different with Tmpfile.
            -- First, we delete all trailing blank lines.
            begin
               Mail.Delete_Trailing_Blank_Lines(Infile  => Tmpfile,
                                                Outfile => DTBL_File);
               -- Then we turn it into quoted printable.
               -- Also turn it into DOS line endings.
               Externals.Mail.Mimeconstruct_Subpart(Infile       => DTBL_File,
                                                    Outfile      => QP_File,
                                                    Content_Type => The_Content_Type,
                                                    Dos2UnixU    => True,
                                                    Use_Encoding => True,
                                                    Encoding     => "quoted-printable",
                                                    Attachment_Name => "message.txt",
                                                    Disposition => Externals.Mail.Inline);
               -- Call out to attachments in case we have to modify QP_File.
               Attachments.Replace_Tmpfile(QP_File, AL);
               -- Then rename QP_File to Tmpfile...
               Mv_F(QP_File, Tmpfile);
            exception
               when others =>
                  Ada.Text_IO.Put_Line(Ada.Text_IO.Standard_Error,
                                       "Exception raised in Sending.Sign_Encrypt (Pre-GPG MIME/3 block)");
                  raise;
            end;
         end if;

         begin
            Externals.GPG.GPG_Wrap(ToStr(Config.UBS_Opts(Gpg_Options))
                         & " --encrypt --sign --textmode --armor --local-user "
                         & Value_Nonempty(Config.UBS_Opts(my_key))
                         & " "
                         & ToStr(Config.UBS_Opts(general_options))
                         & " "
                         & ToStr(Config.UBS_Opts(sending_options))
                         & " "
                         & UGA_Str(Signing => True)
                         & " "
                         & " --output "
                         & Out_File
                         & " "
                         & Keys.Processed_Recipient_List(Send_Keys)
                         & " "
                         & Tmpfile,
                         Out_File);
         exception
            when others =>
               Ada.Text_IO.Put_Line(Ada.Text_IO.Standard_Error,
                                    "Exception raised in Sending.Sign_Encrypt (GPG block)");
               raise;
         end;

      end if;

      begin
         if not (Mime and then Mime_Selection = MultipartEncap) then
            if Selection = SignEncryptPO then
               -- Rename the file appropriately.
               Mv_F(Out_File, Tmpfile & ".asc");
            else
               Mv_F(Out_File, Tmpfile);
            end if;
         end if;
      exception
         when others =>
            Ada.Text_IO.Put_Line(Ada.Text_IO.Standard_Error,
                                 "Exception raised in Sending.Sign_Encrypt (MV block)");
            raise;
      end;

      begin
         if Mime then
            case Mime_Selection is
               when InlinePlain => -- Inline plain text
                  Echo_Out("Content-Type: text/plain",
                           Mimefile);
               when AppPGP => -- application/pgp
                  Echo_Out("Content-Type: application/pgp; format=text; x-action=encrypt",
                           Mimefile);
               when Multipart => -- RFC2015 multipart
                                 -- This is the nasty one.
                                 -- We are using the combined method allowed in section 6.2.
                  declare
                     Blk1   : constant String := Temp_File_Name("mp1");
                     Blk2   : constant String := Temp_File_Name("mp2");
                     MC     : constant String := Temp_File_Name("mc");
                  begin
                     -- We first create the two blocks.
                     -- The first block is easy:
                     Echo_Out("Content-Type: application/pgp-encrypted",
                              Blk1);
                     Echo_Append("", Blk1);
                     Echo_Append("Version: 1", Blk1);
                     -- The second block starts with a
                     -- content-type, then is the tmpfile we've
                     -- put together.
                     Echo_Out("Content-Type: application/octet-stream",
                              Blk2);
                     Echo_Append("Content-Disposition: attachment; filename=""message.asc""", Blk2);
                     Echo_Append("", Blk2);
                     Cat_Append(Tmpfile, Blk2);
                     -- Now we put them together.
                     Externals.Mail.Mimeconstruct2(Part1_Filename  => Blk1,
                                                   Part2_Filename  => Blk2,
                                                   Output_Filename => MC,
                                                   Content_Type    => "multipart/encrypted; protocol=""application/pgp-encrypted""",
                                                   Prolog          => "This is an OpenPGP/MIME encrypted message (RFC2440, RFC3156).");
                     -- Now we need to split these up.
                     Mail.Extract_Content_Type_From_Header(MC, Mimefile);
                     Mail.Extract_Body(MC, Tmpfile);
                  end;
               when MultipartEncap => -- RFC2015 multipart encapsulated
                                      -- This is the REALLY nasty one.  We are
                                      -- encapsulating, so we have to sign then
                                      -- encrypt.
                  declare
                     Blk1   : constant String := Temp_File_Name("mp1");
                     Blk2   : constant String := Temp_File_Name("mp2");
                     MCS    : constant String := Temp_File_Name("mcs");
                     MC     : constant String := Temp_File_Name("mc");
                  begin
                     -- We first create the two blocks.
                     Externals.Mail.Mimeconstruct_Subpart(Infile       => TDS_File,
                                                          Outfile      => Blk2,
                                                          Content_Type => "application/pgp-signature",
                                                          Dos2UnixU    => False,
                                                          Use_Encoding => False,
                                                          Attachment_Name => "signature.asc",
                                                          Disposition => Externals.Mail.Attachment);
                     Externals.Mail.Mimeconstruct2(Part1_Filename  => QP_File,
                                                   Part2_Filename  => Blk2,
                                                   Output_Filename => MCS,
                                                   Content_Type    => "multipart/signed; protocol=""application/pgp-signature""; micalg="""
                                                     & Externals.GPG.Micalg_From_Filename(TDS_File)
                                                     & """",
                                                   Prolog          => "This is an OpenPGP/MIME signed message (RFC2440, RFC3156).");
                     -- Now the encrypt bit....
                     Rm_File(Tmpfile);
                     Externals.GPG.GPG_Wrap(ToStr(Config.UBS_Opts(Gpg_Options))
                                  & " --encrypt --armor "
                                  & ToStr(Config.UBS_Opts(general_options))
                                  & " "
                                  & ToStr(Config.UBS_Opts(sending_options))
                                  & " "
                                  & " --output "
                                  & Tmpfile
                                  & " "
                                  & Keys.Processed_Recipient_List(Send_Keys)
                                  & " "
                                  & MCS,
                                  Tmpfile);
                     -- The first block is easy:
                     Echo_Out("Content-Type: application/pgp-encrypted",
                              Blk1);
                     Echo_Append("", Blk1);
                     Echo_Append("Version: 1", Blk1);
                     -- The second block starts with a
                     -- content-type, then is the tmpfile we've
                     -- put together.
                     Echo_Out("Content-Type: application/octet-stream",
                              Blk2);
                     Echo_Append("Content-Disposition: attachment; filename=""message.asc""", Blk2);
                     Echo_Append("", Blk2);
                     Cat_Append(Tmpfile, Blk2);
                     -- Now we put them together.
                     Externals.Mail.Mimeconstruct2(Part1_Filename => Blk1,
                                                   Part2_Filename => Blk2,
                                                   Output_Filename => MC,
                                                   Content_Type => "multipart/encrypted; protocol=""application/pgp-encrypted""",
                                                   Prolog          => "This is an OpenPGP/MIME encrypted message (RFC2440, RFC3156).");
                     -- Now we need to split these up.
                     Mail.Extract_Content_Type_From_Header(MC, Mimefile);
                     Mail.Extract_Body(MC, Tmpfile);
                  end;
            end case;
         end if;
      exception
         when others =>
            Ada.Text_IO.Put_Line(Ada.Text_IO.Standard_Error,
                                 "Exception raised in Sending.Sign_Encrypt (MIME block 2)");
            raise;
      end;

      Check_Send(Tmpfile, Non_Pine, Mime, Mimefile, Hdrfile, Recipients);

   exception
      when others =>
         Ada.Text_IO.Put_Line(Ada.Text_IO.Standard_Error,
                              "Exception raised in Sending.Sign_Encrypt");
         raise;
   end Sign_Encrypt;

   procedure Clearsign  (Tmpfile   : in String;
                         Non_Pine  : in Boolean;
                         Mime      : in Boolean;
                         Mimefile  : in String;
                         Selection : in Send_Modes;
                         Mime_Selection : in MIME_Modes;
                         AL        : in Attachments.Attachment_List;
                         Hdrfile   : in String;
                         Recipients : in UBS_Array) is
      Out_File       : constant String   := Temp_File_Name("out");
      DTBL_File      : constant String   := Temp_File_Name("dtbl");
      QP_File        : constant String   := Temp_File_Name("qp");
      TDS_File       : constant String   := Temp_File_Name("tds");
   begin
      -- Run GPG.
      if Mime and then Mime_Selection = Multipart then
         -- We need to do something different with Tmpfile.
         -- First, we delete all trailing blank lines.

         begin
            Mail.Delete_Trailing_Blank_Lines(Tmpfile, DTBL_File);
            -- Then we turn it into quoted printable.
            -- Also turn it into DOS line endings.
            Externals.Mail.Mimeconstruct_Subpart(Infile       => DTBL_File,
                                                 Outfile      => QP_File,
                                                 Content_Type => The_Content_Type,
                                                 Dos2UnixU    => True,
                                                 Use_Encoding => True,
                                                 Encoding     => "quoted-printable",
                                                 Attachment_Name => "message.txt",
                                                 Disposition => Externals.Mail.Inline);
            -- Call out to attachments in case we have to modify QP_File.
            Attachments.Replace_Tmpfile(QP_File, AL);
            -- Then we clearsign it.
            Externals.GPG.GPG_Wrap(ToStr(Config.UBS_Opts(Gpg_Options))
                         & " --detach-sign --textmode --armor --local-user "
                         & Value_Nonempty(Config.UBS_Opts(my_key))
                         & " "
                         & ToStr(Config.UBS_Opts(general_options))
                         & " "
                         & ToStr(Config.UBS_Opts(sending_options))
                         & " "
                         & UGA_Str(Signing => True)
                         & " "
                         & " --output "
                         & TDS_File
                         & " "
                         & QP_File,
                         TDS_File);
         exception
            when others =>
               Ada.Text_IO.Put_Line(Ada.Text_IO.Standard_Error,
                                    "Exception raised in Sending.Clearsign (GPG MIME/3 block 1)");
               raise;
         end;
      else
         begin
            Externals.GPG.GPG_Wrap(ToStr(Config.UBS_Opts(Gpg_Options))
                         & " --clearsign --textmode --local-user "
                         & Value_Nonempty(Config.UBS_Opts(my_key))
                         & " "
                         & ToStr(Config.UBS_Opts(general_options))
                         & " "
                         & ToStr(Config.UBS_Opts(sending_options))
                         & " "
                         & UGA_Str(Signing => True)
                         & " "
                         & " --output "
                         & Out_File
                         & " "
                         & Tmpfile,
                         Out_File);
         exception
            when others =>
               Ada.Text_IO.Put_Line(Ada.Text_IO.Standard_Error,
                                    "Exception raised in Sending.Clearsign (GPG block)");
               raise;
         end;
      end if;

      begin
         if not (Mime and then Mime_Selection = Multipart) then
            if Selection = ClearsignPO then
               -- Rename the file appropriately.
               Mv_F(Out_File, Tmpfile & ".asc");
            else
               Mv_F(Out_File, Tmpfile);
            end if;
         end if;
      exception
         when others =>
            Ada.Text_IO.Put_Line(Ada.Text_IO.Standard_Error,
                                 "Exception raised in Sending.Clearsign (MV block)");
            raise;
      end;

      begin
         if Mime then
            case Mime_Selection is
               when InlinePlain => -- Inline plain text
                  Echo_Out("Content-Type: text/plain", Mimefile);
               when AppPGP => -- application/pgp
                  Echo_Out("Content-Type: application/pgp; format=text; x-action=sign",
                           Mimefile);
               when Multipart => -- RFC2015 multipart
                                 -- This is the _really_ nasty one.
                                 -- At this point, we have QP_File, the quoted-printable, and TDS_File, then detached signature.
                  declare
                     Blk2   : constant String := Temp_File_Name("mp2");
                     MC     : constant String := Temp_File_Name("mc");
                  begin
                     Externals.Mail.Mimeconstruct_Subpart(Infile       => TDS_File,
                                                          Outfile      => Blk2,
                                                          Content_Type => "application/pgp-signature",
                                                          Dos2UnixU    => False,
                                                          Use_Encoding => False,
                                                          Attachment_Name => "signature.asc",
                                                          Disposition => Externals.Mail.Attachment);
                     Externals.Mail.Mimeconstruct2(Part1_Filename  => QP_File,
                                                   Part2_Filename  => Blk2,
                                                   Output_Filename => MC,
                                                   Content_Type    => "multipart/signed; protocol=""application/pgp-signature""; micalg="""
                                                     & Externals.GPG.Micalg_From_Filename(TDS_File)
                                                     & """",
                                                   Prolog          => "This is an OpenPGP/MIME signed message (RFC2440, RFC3156).");
                     -- Now we need to split these up.
                     Mail.Extract_Content_Type_From_Header(MC, Mimefile);
                     Mail.Extract_Body(MC, Tmpfile);
                  end;
               when MultipartEncap =>
                  Error("Menu should not have allowed MultipartEncap here");
            end case;
         end if;
      exception
         when others =>
            Ada.Text_IO.Put_Line(Ada.Text_IO.Standard_Error,
                                 "Exception raised in Sending.Clearsign (MIME block 2)");
            raise;
      end;

      Check_Send(Tmpfile, Non_Pine, Mime, Mimefile, Hdrfile, Recipients);

   exception
      when others =>
         Ada.Text_IO.Put_Line(Ada.Text_IO.Standard_Error,
                              "Exception raised in Sending.Clearsign");
         raise;
   end Clearsign;

   procedure Detached_Sig  (Tmpfile   : in String) is
   begin
      -- Run GPG.
      -- Not using --textmode here, because it could be a binary file.
      Externals.GPG.GPG_Wrap(ToStr(Config.UBS_Opts(Gpg_Options))
                   & " --detach-sign --armor --local-user "
                   & Value_Nonempty(Config.UBS_Opts(my_key))
                   & " "
                   & ToStr(Config.UBS_Opts(general_options))
                   & " "
                   & ToStr(Config.UBS_Opts(sending_options))
                   & " "
                   & UGA_Str(Signing => True)
                   & " "
                   & " --output "
                   & Tmpfile
                   & ".asc "
                   & Tmpfile,
                   Tmpfile & ".asc");
      Wait;
   exception
      when others =>
         Ada.Text_IO.Put_Line(Ada.Text_IO.Standard_Error,
                              "Exception raised in Sending.Detached_Sig");
         raise;
   end Detached_Sig;

   procedure Send_Unchanged  (Tmpfile   : in String;
                              Non_Pine  : in Boolean;
                              Mime      : in Boolean;
                              Mimefile  : in String;
                              Hdrfile   : in String;
                              Recipients : in UBS_Array) is
   begin
      Check_Send(Tmpfile, Non_Pine, Mime, Mimefile, Hdrfile, Recipients);
   exception
      when others =>
         Ada.Text_IO.Put_Line(Ada.Text_IO.Standard_Error,
                              "Exception raised in Sending.Send_Unchanged");
         raise;
   end Send_Unchanged;

   -- We want to send a message.
   procedure Send (Tmpfile    : in String;
                   Recipients : in UBS_Array;
                   Non_Pine   : in Boolean   := False;
                   Mime       : in Boolean   := False;
                   Mimefile   : in String    := "") is
      Send_Keys      : Keys.Key_List;
      Selection      : SendMime_Index;
      Send_Mode      : Send_Modes;
      Mime_Mode      : MIME_Modes;
      Originfile     : constant String := Temp_File_Name("origin");
      Hdrfile        : constant String := Temp_File_Name("hdr");
      Tmpfile2       : constant String := Temp_File_Name("tmp2");
      Fromfile       : constant String := Temp_File_Name("from");
      Fromfile2      : constant String := Temp_File_Name("from2");
      Alt_Sign       : UBS;
      AL             : Attachments.Attachment_List;
      My_Key_View    : UBS;
      Missing        : Boolean;

      procedure Set_My_Key_View is
      begin
         if ToStr(Config.UBS_Opts(My_Key)) = "" then
            My_Key_View := ToUBS("(not set)");
         else
            declare
            KP : Keys.Key_Properties;
            begin
               Keys.Process_Key_By_FP(ToStr(Config.UBS_Opts(My_Key)), My_Key_View, KP);
            end;
         end if;
      end Set_My_Key_View;

   begin
      Debug("+Send");
      -- Save the original input.
      Externals.Simple.Cat_Out(Tmpfile, Originfile);
      -- If Config.Boolean_Opts(All_Headers) is set, then split
      --  tmpfile into header and body.  But we'll copy the tmpfile
      --  into another tmp first, then put the body alone back in
      --  Tmpfile.
      if Config.Boolean_Opts(All_Headers) then
         Externals.Simple.Mv_F(Tmpfile, Tmpfile2);
         -- Make sure the line endings are correct.
         Externals.Simple.Dos2Unix(Tmpfile2);
         Externals.Mail.Extract_Header(Email_Filename => Tmpfile2,
                                       Target_Filename => Hdrfile);
         Externals.Mail.Extract_Body(Email_Filename => Tmpfile2,
                                     Target_Filename => Tmpfile);
      end if;
      -- If Config.UBS_Opts(Read_From) is set, then parse the header to find the
      --  fromline and set my-key appropriately.  This implies that
      --  All_Headers was set, too.
      if Config.Boolean_Opts(Read_From) then
         Externals.Mail.Formail_Concat_Extract_InOut("From:",
                                                     Hdrfile,
                                                     Fromfile);
         Externals.Simple.Sed_InOut("s/^.*<//; s/>.*$//",
                                    Fromfile, Fromfile2);
         declare
            F : Ada.Text_IO.File_Type;
         begin
            Ada.Text_IO.Open(File => F,
                             Mode => Ada.Text_IO.In_File,
                             Name => Fromfile2);
            Alt_Sign := Unbounded_Get_Line(F);
            Ada.Text_IO.Close(F);
         end;
      end if;
      -- Sort out the send menus/msgs.
      Key_Preselect(Send_Keys, Recipients, ToStr(Alt_Sign), Missing);
      -- Sort out default mode.
      Mode_Preselect(Send_Keys, Recipients, Send_Mode, Mime_Mode);
      if Missing and then
        (Send_Mode = Encrypt or Send_Mode = SignEncrypt) then
         Ada.Text_IO.New_Line(2);
         Ada.Text_IO.Put_Line(Do_SGR(Config.UBS_Opts(Colour_Important))
                                & "Sending is *ENCRYPTING* but some recipient keys were *MISSING*."
                                & NL
                                & "Those recipients will not be able to decrypt this message."
                                & Reset_SGR);
      end if;
      -- Initialise attachments.
      Attachments.Empty_Attachment_List(AL);
      -- View of the current key.
      Set_My_Key_View;
      -- Loop around doingstuff, now.
  Send_Loop:
      loop
         Ada.Text_IO.New_Line(5);
         Ada.Text_IO.Put_Line(Do_SGR(Config.UBS_Opts(Colour_Menu_Title))
                                &  "** Main send menu:"
                                & Reset_SGR);
         Ada.Text_IO.Put_Line(" Sending mode: "
                                & Do_SGR(Config.UBS_Opts(Colour_Info))
                                & Send_Modes_Text(Send_Mode)
                                & Reset_SGR);
         if Mime then
            Ada.Text_IO.Put("               "
                              & Do_SGR(Config.UBS_Opts(Colour_Info))
                              & Mime_Modes_Text(Mime_Mode)
                              & Reset_SGR);
            if Mime_Mode = MultipartEncap and then Send_Mode /= SignEncrypt then
               Ada.Text_IO.Put(Do_SGR(Config.UBS_Opts(Colour_Important))
                                 & " (will downgrade to Multipart)"
                                 & Reset_SGR);
            end if;
            Ada.Text_IO.New_Line;
         end if;
         Ada.Text_IO.Put_Line(" Signing key: "
                                & Do_SGR(Config.UBS_Opts(Colour_Info))
                                & ToStr(My_Key_View)
                                & Reset_SGR);
         Ada.Text_IO.Put(Do_SGR(Config.UBS_Opts(Colour_Info))
                           & Integer'Image(Keys.Count(Send_Keys))
                           & " key(s)"
                           & Reset_SGR
                           & " in key list     ");
         if Non_Pine then
            Ada.Text_IO.New_Line;
            Selection := Send_Menu_NP;
         elsif Mime then
            Ada.Text_IO.Put_Line(Do_SGR(Config.UBS_Opts(Colour_Info))
                                   & Integer'Image(Attachments.Count(AL))
                                   & " attachment(s)"
                                   & Reset_SGR);
            Selection := Send_Menu_Pine_Mime;
         else
            Ada.Text_IO.New_Line;
            Selection := Send_Menu_Pine_Plain;
         end if;
         begin
            case Selection is
               when AAbort => -- Abort
                  Ada.Text_IO.Put_Line("Aborting");
                  Ada.Command_Line.Set_Exit_Status(Ada.Command_Line.Failure);
                  exit Send_Loop;
               when AddOwn => -- Add own key
                  if ToStr(Config.UBS_Opts(My_Key)) = "" then
                  Ada.Text_IO.Put_Line("my-key is empty; not selecting any keys for self");
                  else
                     Keys.Add_Keys_By_Fingerprint(Value_Nonempty(Config.UBS_Opts(My_Key)),
                                                  Send_Keys,
                                                  Keys.Encrypter);
                  end if;
               when EditOwn => -- Change own key
                  Configuration.Edit_Own_Key;
                  -- Reset view of the current key.
                  Set_My_Key_View;
               when ViewMail => -- View the input email.
                  Pager(Tmpfile);
               when EditMail => -- Edit the input email.
                  declare
                     Editor_Command : UBS;
                     Dummy          : Integer;
                     use Ada.Text_IO;
                     use type UBS;
                     pragma Unreferenced(Dummy);
                  begin
                     New_Line(1);
                     Put_Line("Edit input email:");
                     Editor_Command
                       := ToUBS(Readline.Get_String("Which editor command? (filename will be appended) "));
                     if ToStr(Editor_Command)'Length > 0 then
                        Dummy := Externals.Simple.System(Editor_Command & ToUBS(" " & Tmpfile));
                     end if;
                  end;
               when AttachEdit => -- Edit the attachment list.
                  Attachments.List(AL);
                  if Attachments.Count(AL) > 0 then
                     if (not Mime) then
                        Ada.Text_IO.New_Line;
                        Ada.Text_IO.Put_Line(Do_SGR(Config.UBS_Opts(Colour_Important))
                                               & "WARNING: attachments not included in non-MIME emails!"
                                               & Reset_SGR);
                        Ada.Text_IO.New_Line;
                     elsif Mime and then not (Mime_Mode = MultipartEncap
                                                or Mime_Mode = Multipart) then
                        Mime_Mode := Multipart;
                        Ada.Text_IO.New_Line;
                        Ada.Text_IO.Put_Line(Do_SGR(Config.UBS_Opts(Colour_Important))
                                               & "Note: Forcing sending mode to multipart/* for attachments"
                                               & Reset_SGR);
                        Ada.Text_IO.New_Line;
                     end if;
                  end if;
               when Configure => -- Configuration
                  Configuration.Edit_Configuration;
               when ListEdit => -- List keys
                  Keys.List_Keys(Send_Keys);
               when Remote => -- Run remote connection
                              -- Restore original tmpfile from origin.
                  Externals.Simple.Cat_Out(Originfile, Tmpfile);
                  Remote_Mode.Send(Tmpfile, Mime, Mimefile, Recipients);
                  exit Send_Loop;
               when Encrypt | SignEncrypt | ClearSign
                 | NoGPG
                 | EncryptPO | SignEncryptPO | ClearSignPO
                 | DetachSignPO => -- Set Send_Mode.
                  Send_Mode := Selection;
               when InlinePlain | AppPGP
                 | Multipart | MultipartEncap => -- Set Mime_Mode.
                  Mime_Mode := Selection;
               when Go =>

                  -- MultipartEncap only applies to SignEncrypt.
                  --  Downgrade to Multipart otherwise.
                  if Mime_Mode = MultipartEncap
                    and then Send_Mode /= SignEncrypt then
                     Ada.Text_IO.Put_Line(Do_SGR(Config.UBS_Opts(Colour_Important))
                                            & "NOTE: replacing MultipartEncap with Multipart for this operation."
                                            & Reset_SGR);
                     Mime_Mode := Multipart;
                  end if;

                  -- Need MIME for attachments.
                  if not (Mime
                            and then (Mime_Mode = MultipartEncap
                                        or Mime_Mode = Multipart)) then
                     if Attachments.Count(AL) > 0 then
                        Ada.Text_IO.Put_Line(Do_SGR(Config.UBS_Opts(Colour_Important))
                                               & "WARNING: attachments not included in non-MIME emails!"
                                               & Reset_SGR);
                        Ada.Text_IO.New_Line;
                     end if;
                  end if;

                  case Send_Mode is
                     when Encrypt | EncryptPO => -- Do GPG: encrypt
                        if Keys.Count(Send_Keys) = 0 then
                        Ada.Text_IO.Put_Line(Do_SGR(Config.UBS_Opts(Colour_Important))
                                               & "No keys are in the key list.  Can't encrypt until you select at least one key."
                                               & Reset_SGR);
                        else
                        Encrypt(Tmpfile, Non_Pine,
                                Mime, Mimefile,
                                Send_Keys, Send_Mode, Mime_Mode,
                                AL, Hdrfile, Recipients);
                        exit Send_Loop;
                        end if;
                     when SignEncrypt | SignEncryptPO => -- Do GPG:  sign-encrypt
                        if ToStr(Config.UBS_Opts(My_Key)) = "" then
                        Ada.Text_IO.Put_Line(Do_SGR(Config.UBS_Opts(Colour_Important))
                                               & "Own key not set!  Can't sign until you do this."
                                               & Reset_SGR);
                        elsif Keys.Count(Send_Keys) = 0 then
                        Ada.Text_IO.Put_Line(Do_SGR(Config.UBS_Opts(Colour_Important))
                                               & "No keys are in the key list.  Can't encrypt until you select at least one key."
                                               & Reset_SGR);
                        else
                           Sign_Encrypt(Tmpfile, Non_Pine,
                                        Mime, Mimefile,
                                        Send_Keys, Send_Mode, Mime_Mode,
                                        AL, Hdrfile, Recipients);
                           exit Send_Loop;
                        end if;
                     when Clearsign | ClearSignPO => -- Do GPG:  clearsign
                        if ToStr(Config.UBS_Opts(My_Key)) = "" then
                        Ada.Text_IO.Put_Line(Do_SGR(Config.UBS_Opts(Colour_Important))
                                               & "Own key not set!  Can't sign until you do this."
                                               & Reset_SGR);
                        else
                           Clearsign(Tmpfile, Non_Pine,
                                     Mime, Mimefile,
                                     Send_Mode, Mime_Mode,
                                     AL, Hdrfile, Recipients);
                           exit Send_Loop;
                        end if;
                     when DetachSignPO => -- Do GPG:  create detached signature
                        if ToStr(Config.UBS_Opts(My_Key)) = "" then
                           Ada.Text_IO.Put_Line(Do_SGR(Config.UBS_Opts(Colour_Important))
                                                  & "Own key not set!  Can't sign until you do this."
                                                  & Reset_SGR);
                        else
                           Detached_Sig(Tmpfile);
                           exit Send_Loop;
                        end if;
                     when NoGPG => -- Pass thu' unchanged.
                        Send_Unchanged(Tmpfile, Non_Pine, Mime, Mimefile, Hdrfile, Recipients);
                        exit Send_Loop;
                  end case;
            end case;
         exception
            when Exit_Send_Loop =>
               exit Send_Loop;
            when others =>
               Ada.Text_IO.Put_Line(Ada.Text_IO.Standard_Error,
                                    "Exception raised in Sending.Send (Send_Loop)");
               raise;
         end;
      end loop Send_Loop;
   exception
      when others =>
         declare
            use Ada.Text_IO;
         begin
            Put_Line(Standard_Error, "Exception raised in Sending.Send");
            Put_Line(Standard_Error, "Send_Keys count = " & Integer'Image(Keys.Count(Send_Keys)));
            Put_Line(Standard_Error, "Send_Mode = " & Send_Modes_Text(Send_Mode));
            Put_Line(Standard_Error, "Mime_Mode = " & Mime_Modes_Text(Mime_Mode));
            Put_Line(Standard_Error, "My_Key = " & ToStr(My_Key_View));
            Put_Line(Standard_Error, "AL count = " & Integer'Image(Attachments.Count(AL)));
         end;
         raise;
   end Send;

end Sending;