KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > IncomingMessageThread


1 import org.jivesoftware.smack.packet.*;
2 import org.jivesoftware.smack.*;
3 import org.jivesoftware.smack.util.StringUtils;
4 import whisper.*;
5
6 /** Thread that processes incoming messages.*/
7 public final class IncomingMessageThread extends Thread JavaDoc{
8     
9     private org.jivesoftware.smack.packet.Message message;
10     
11     public IncomingMessageThread(org.jivesoftware.smack.packet.Message message){
12         super("Incoming Message Thread");
13         setDaemon(true);
14         setPriority(Thread.MIN_PRIORITY);
15         this.message=message;
16     }
17     
18     public void run(){
19         // get from details
20
if(message.getBody()==null){
21             return;
22         }
23         final String JavaDoc fromID=message.getFrom();
24         String JavaDoc fromName=StringUtils.parseBareAddress(fromID.toLowerCase());
25         String JavaDoc tabName=StringUtils.parseName(fromID)+"@";
26         // see if its a MUC private message
27
final MUCPanel mp=(MUCPanel)WhisperIM.MainWindow.MUCs.get(StringUtils.parseBareAddress(fromID.toLowerCase()));
28         if(mp!=null && (!StringUtils.parseResource(fromID).equals(""))){
29             javax.swing.SwingUtilities.invokeLater(new Runnable JavaDoc(){
30                     public void run(){
31                         mp.pm(message.getBody(),StringUtils.parseResource(fromID),"",GUI.getTime(message));
32                     }
33                 });
34             return;
35         }
36         // see if its on roster so we can translate name
37
ContactListItem cli=(ContactListItem)WhisperIM.MainWindow.Contacts.get("jabber"+fromName);
38         if(cli!=null){
39             fromName=cli.toString();
40             tabName=fromName;
41         }
42         final String JavaDoc finaltabName=tabName;
43         final String JavaDoc finalfromName=fromName;
44         // see if its an error message
45

46         // see if message is encrypted or not
47
String JavaDoc m=message.getBody().trim().replaceAll("&lt;","<").replaceAll("&gt;",">");
48         if(m.startsWith("<wmsg>") ){
49             //its a whisper message
50
PublicKey pk;
51             try{
52                 final whisper.Message wm=new whisper.Message(m);
53                 // have we got a key?
54
pk=(PublicKey) WhisperIM.MainWindow.Keys.get("jabber"+StringUtils.parseBareAddress(fromID.toLowerCase()));
55                 if(pk==null){
56                     // try and fetch
57
pk=WhisperIM.MainWindow.getKey("jabber",fromID);
58                     if(pk==null){ //still no key, ignore message
59
javax.swing.SwingUtilities.invokeLater(new Runnable JavaDoc(){
60                             public void run(){
61                                 GUI.showInfo(WhisperIM.MainWindow,"bad message received","no public key",Lang.gs("from")+fromID);
62                             }
63                         });
64                         return;
65                     }
66                 }
67                 // ok, verfiy msg signature
68
if(!wm.isValid(pk)){
69                     javax.swing.SwingUtilities.invokeLater(new Runnable JavaDoc(){
70                             public void run(){
71                                 GUI.showInfo(WhisperIM.MainWindow,"bad message received","invalid signature",Lang.gs("from")+fromID);
72                             }
73                         });
74                     return;
75                 }
76                 // get the encrypted session key
77
EncryptedSessionKey esk=wm.getESK();
78                 if(esk==null){ // no ESK, cannot decrypt message
79
javax.swing.SwingUtilities.invokeLater(new Runnable JavaDoc(){
80                         public void run(){
81                             GUI.showInfo(WhisperIM.MainWindow,"bad message received","could not decrypt",Lang.gs("from")+fromID);
82                         }
83                     });
84                     return;
85                 }
86                 // decrypt!
87
while(WhisperIM.KEYPAIR==null){
88                     try{
89                         sleep(3000);
90                     }
91                     catch(InterruptedException JavaDoc ie){
92                         return;
93                     }
94                 }
95                 final String JavaDoc plaintext=wm.decrypt(esk.decrypt(WhisperIM.KEYPAIR));
96                 wm.wipe(); //wipe the message
97
// add tab
98
javax.swing.SwingUtilities.invokeLater(new Runnable JavaDoc(){
99                     public void run(){
100                         RecievedMessagePanel pe=new RecievedMessagePanel(plaintext,finalfromName,fromID,true,GUI.getDate(wm));
101                         WhisperIM.MainWindow.chatPane.addTab(finaltabName,Icons.NEW_MESSAGE,pe,Lang.gs("recieved message")+fromID);
102                         WhisperIM.MainWindow.statusLbl.setText(GUI.getTime()+" "+Lang.gs("recieved message")+finalfromName);
103                     }
104                 });
105                 Alert.newMessage();
106                 return;
107             }
108             catch(final Exception JavaDoc e){
109                 javax.swing.SwingUtilities.invokeLater(new Runnable JavaDoc(){
110                         public void run(){
111                             GUI.showInfo(WhisperIM.MainWindow,"bad message received","could not decrypt",Lang.gs("from")+fromID+"\r\n"+e.getMessage());
112                         }
113                     });
114                 e.printStackTrace();
115                 return;
116             }
117         }
118         // non encrypted message
119
javax.swing.SwingUtilities.invokeLater(new Runnable JavaDoc(){
120             public void run(){
121                 RecievedMessagePanel p=new RecievedMessagePanel(message.getBody(),finalfromName,fromID,false,GUI.getDate(message));
122                 WhisperIM.MainWindow.chatPane.addTab(finaltabName,Icons.NEW_MESSAGE,p,Lang.gs("recieved message")+fromID);
123                 WhisperIM.MainWindow.statusLbl.setText(GUI.getTime()+" "+Lang.gs("recieved message")+finalfromName);
124             }
125         });
126         Alert.newMessage();
127     }
128 }
Popular Tags