1 import org.jivesoftware.smack.packet.*; 2 import org.jivesoftware.smack.*; 3 import org.jivesoftware.smack.util.StringUtils; 4 import whisper.*; 5 6 7 public final class IncomingMessageThread extends Thread { 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 if(message.getBody()==null){ 21 return; 22 } 23 final String fromID=message.getFrom(); 24 String fromName=StringUtils.parseBareAddress(fromID.toLowerCase()); 25 String tabName=StringUtils.parseName(fromID)+"@"; 26 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 (){ 30 public void run(){ 31 mp.pm(message.getBody(),StringUtils.parseResource(fromID),"",GUI.getTime(message)); 32 } 33 }); 34 return; 35 } 36 ContactListItem cli=(ContactListItem)WhisperIM.MainWindow.Contacts.get("jabber"+fromName); 38 if(cli!=null){ 39 fromName=cli.toString(); 40 tabName=fromName; 41 } 42 final String finaltabName=tabName; 43 final String finalfromName=fromName; 44 46 String m=message.getBody().trim().replaceAll("<","<").replaceAll(">",">"); 48 if(m.startsWith("<wmsg>") ){ 49 PublicKey pk; 51 try{ 52 final whisper.Message wm=new whisper.Message(m); 53 pk=(PublicKey) WhisperIM.MainWindow.Keys.get("jabber"+StringUtils.parseBareAddress(fromID.toLowerCase())); 55 if(pk==null){ 56 pk=WhisperIM.MainWindow.getKey("jabber",fromID); 58 if(pk==null){ javax.swing.SwingUtilities.invokeLater(new Runnable (){ 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 if(!wm.isValid(pk)){ 69 javax.swing.SwingUtilities.invokeLater(new Runnable (){ 70 public void run(){ 71 GUI.showInfo(WhisperIM.MainWindow,"bad message received","invalid signature",Lang.gs("from")+fromID); 72 } 73 }); 74 return; 75 } 76 EncryptedSessionKey esk=wm.getESK(); 78 if(esk==null){ javax.swing.SwingUtilities.invokeLater(new Runnable (){ 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 while(WhisperIM.KEYPAIR==null){ 88 try{ 89 sleep(3000); 90 } 91 catch(InterruptedException ie){ 92 return; 93 } 94 } 95 final String plaintext=wm.decrypt(esk.decrypt(WhisperIM.KEYPAIR)); 96 wm.wipe(); javax.swing.SwingUtilities.invokeLater(new Runnable (){ 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 e){ 109 javax.swing.SwingUtilities.invokeLater(new Runnable (){ 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 javax.swing.SwingUtilities.invokeLater(new Runnable (){ 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 |