KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SnowMailClient > gnupg > MessageActions


1 package SnowMailClient.gnupg;
2
3 import SnowMailClient.model.*;
4 import snow.utils.gui.*;
5 import snow.concurrent.*;
6 import SnowMailClient.SnowMailClientApp;
7 import SnowMailClient.gnupg.model.*;
8 import SnowMailClient.Language.*;
9 import java.util.*;
10
11 /** used during send/receive
12 */

13 public final class MessageActions
14 {
15   public MessageActions()
16   {
17   }
18
19   /** signing requires password
20       ensures that it is available here.
21       the mail will be signed on send
22   */

23   public static void prepare_Signing_Mail( MailMessage mess, Interrupter interrupter) throws Exception JavaDoc
24   {
25      String JavaDoc fromMailAddress = mess.getFromAddress().getMailAddress();
26      GnuPGLink gpg = SnowMailClientApp.getInstance().getGnuPGLink();
27
28      if(!gpg.isGPG_available())
29      {
30        throw new Exception JavaDoc(Language.translate("GnuPG is not available, please install it first or send without signing."));
31      }
32
33      GnuPGKeyID[] kids = gpg.getSecretKeyIDForAddress(fromMailAddress);
34      if(kids.length==0) throw new Exception JavaDoc(Language.translate("No key to sign with %", fromMailAddress));
35
36      byte[] pass = gpg.getPasswordForKeyAskIfNotFoundOrNotValid(kids[0], true, SnowMailClientApp.getInstance());
37      if(pass==null) throw new Exception JavaDoc(Language.translate("No password given to sign with %", fromMailAddress));
38
39      //mess.signContent(gpg, kids[0], pass, pmd);
40

41      if(mess.getMimeTree().isMimeMessageFormat())
42      {
43        throw new Exception JavaDoc("Signing messages with attachement is not implemented yet !");
44      }
45
46      String JavaDoc signedTest = gpg.sign(mess.getCompleteContentAsString(), kids[0], pass, interrupter);
47
48
49   }
50
51
52   /** ensures that all recipients have a public key available
53   */

54   public static void prepareEncryptMail(MailMessage mess) throws Exception JavaDoc
55   {
56      Vector<Address> toMailAddress = mess.getToAddresses();
57      if(toMailAddress.size()>1)
58      {
59        throw new Exception JavaDoc(Language.translate("Only one recipient is allowed for encrypted mail"));
60      }
61
62      GnuPGLink gpg = SnowMailClientApp.getInstance().getGnuPGLink();
63
64      if(!gpg.isGPG_available())
65      {
66        throw new Exception JavaDoc(Language.translate("GnuPG is not available, please install it first or send without encrypting."));
67      }
68
69      StringBuffer JavaDoc errorBuffer = new StringBuffer JavaDoc();
70      for(Address to: toMailAddress)
71      {
72         String JavaDoc toAddress = to.getMailAddress();
73         GnuPGKeyID[] kids = gpg.getPublicKeyIDForAddress(toAddress);
74         if(kids.length==0)
75         {
76           errorBuffer.append("\n" + toAddress);
77         }
78         else
79         {
80           // ### ask which ?
81
GnuPGKeyID kid = kids[0];
82           String JavaDoc ct = kid.getCalculatedTrust();
83           if(!(ct.equals("u") || ct.equals("f")))
84           {
85             errorBuffer.append("\n"+Language.translate("Bad trust for %", toAddress)+": "+kid.getCalculatedTrustMessage());
86           }
87         }
88      }
89
90      if(errorBuffer.length()>0)
91      {
92        throw new Exception JavaDoc(
93        Language.translate("No public keys found for following recipient,\nplease import them first or do not encrypt.")
94        +"\n"+errorBuffer.toString());
95      }
96
97   }
98
99 } // MessageActions
Popular Tags