| 6 import java.util.List; |
6 import java.util.List; |
| 7 import java.util.concurrent.BlockingQueue; |
7 import java.util.concurrent.BlockingQueue; |
| 8 import java.util.concurrent.LinkedBlockingQueue; |
8 import java.util.concurrent.LinkedBlockingQueue; |
| 9 import java.util.logging.Level; |
9 import java.util.logging.Level; |
| 10 import java.util.logging.Logger; |
10 import java.util.logging.Logger; |
| |
11 import net.java.otr4j.OtrException; |
| |
12 import net.java.otr4j.OtrPolicy; |
| |
13 import net.java.otr4j.OtrPolicyImpl; |
| |
14 import net.java.otr4j.OtrSessionManager; |
| |
15 import net.java.otr4j.OtrSessionManagerImpl; |
| |
16 import net.java.otr4j.session.InstanceTag; |
| |
17 import net.java.otr4j.session.Session; |
| |
18 import net.java.otr4j.session.SessionID; |
| |
19 import net.java.otr4j.session.SessionImpl; |
| |
20 import net.java.otr4j.session.TLV; |
| 11 import org.jivesoftware.smack.ConnectionConfiguration; |
21 import org.jivesoftware.smack.ConnectionConfiguration; |
| 12 import org.jivesoftware.smack.SmackException; |
22 import org.jivesoftware.smack.SmackException; |
| 13 import org.jivesoftware.smack.XMPPException; |
23 import org.jivesoftware.smack.XMPPException; |
| 14 import org.jivesoftware.smack.filter.MessageWithBodiesFilter; |
24 import org.jivesoftware.smack.filter.MessageWithBodiesFilter; |
| 15 import org.jivesoftware.smack.packet.Message; |
25 import org.jivesoftware.smack.packet.Message; |
| 23 public class Xmpp extends Thread { |
33 public class Xmpp extends Thread { |
| 24 private final XMPPTCPConnectionConfiguration config; |
34 private final XMPPTCPConnectionConfiguration config; |
| 25 |
35 |
| 26 private XMPPTCPConnection connection = null; |
36 private XMPPTCPConnection connection = null; |
| 27 |
37 |
| |
38 private final OTR otr; |
| |
39 |
| |
40 private final OtrSessionManager otrSM; |
| |
41 |
| 28 // BlockingQueue for event-driven communication |
42 // BlockingQueue for event-driven communication |
| 29 private final BlockingQueue<XmppEvent> eventQueue = new LinkedBlockingQueue<>(); |
43 private final BlockingQueue<XmppEvent> eventQueue = new LinkedBlockingQueue<>(); |
| 30 |
44 |
| 31 public Xmpp(XMPPTCPConnectionConfiguration xmppConfig) { |
45 public Xmpp(XMPPTCPConnectionConfiguration xmppConfig) { |
| 32 config = xmppConfig; |
46 config = xmppConfig; |
| |
47 otr = new OTR(this); |
| |
48 otrSM = new OtrSessionManagerImpl(otr); |
| |
49 |
| |
50 } |
| |
51 |
| |
52 public OTR getOTR() { |
| |
53 return otr; |
| |
54 } |
| |
55 |
| |
56 public OtrSessionManager getOtrSM() { |
| |
57 return otrSM; |
| |
58 } |
| |
59 |
| |
60 public void startOTR(String xid) { |
| |
61 String account = config.getUsername() + "@" + config.getHostString() + "/IM5"; |
| |
62 SessionID sid = new SessionID(account, xid, "xmpp"); |
| |
63 |
| |
64 Session session = new SessionImpl(sid, otr); |
| |
65 String[] outgoingMessage; |
| |
66 try { |
| |
67 session.startSession(); |
| |
68 } catch (OtrException ex) { |
| |
69 Logger.getLogger(Xmpp.class.getName()).log(Level.SEVERE, null, ex); |
| |
70 } |
| |
71 |
| 33 } |
72 } |
| 34 |
73 |
| 35 // Method to send a message (this will be called from another thread) |
74 // Method to send a message (this will be called from another thread) |
| 36 public void sendMessage(String to, String message, boolean encrypted) { |
75 public void sendMessage(String to, String message, boolean encrypted) { |
| 37 try { |
76 try { |
| 40 } catch (InterruptedException e) { |
79 } catch (InterruptedException e) { |
| 41 Logger.getLogger(Xmpp.class.getName()).log(Level.SEVERE, "Error adding event to queue", e); |
80 Logger.getLogger(Xmpp.class.getName()).log(Level.SEVERE, "Error adding event to queue", e); |
| 42 } |
81 } |
| 43 } |
82 } |
| 44 |
83 |
| |
84 public void send(String to, String message) { |
| |
85 sendMessage(to, message, false); |
| |
86 } |
| |
87 |
| 45 private void connect() throws SmackException, IOException, XMPPException, InterruptedException { |
88 private void connect() throws SmackException, IOException, XMPPException, InterruptedException { |
| 46 connection = new XMPPTCPConnection(config); |
89 connection = new XMPPTCPConnection(config); |
| 47 connection.setUseStreamManagement(false); |
90 connection.setUseStreamManagement(false); |
| 48 connection.connect(); |
91 connection.connect(); |
| 49 connection.login(); |
92 connection.login(); |
| 50 |
|
| 51 } |
93 } |
| 52 |
94 |
| 53 public List<RosterEntry> getRosterItems() throws SmackException.NotLoggedInException, SmackException.NotConnectedException, InterruptedException { |
95 public List<RosterEntry> getRosterItems() throws SmackException.NotLoggedInException, SmackException.NotConnectedException, InterruptedException { |
| 54 try { |
96 try { |
| 55 // Ensure we are connected |
97 // Ensure we are connected |