use of org.awesomeapp.messenger.model.Message in project Zom-Android by zom.
the class XmppConnection method handleMessage.
private void handleMessage(org.jivesoftware.smack.packet.Message smackMessage, boolean isOmemo, boolean notifyUser) {
String body = smackMessage.getBody();
boolean isGroupMessage = smackMessage.getType() == org.jivesoftware.smack.packet.Message.Type.groupchat;
if (smackMessage.getError() != null) {
// smackMessage.getError().getCode();
String error = "Error " + smackMessage.getError() + " (" + smackMessage.getError().getCondition() + "): " + smackMessage.getError().getConditionText();
debug(TAG, error);
return;
}
if (body == null) {
Collection<org.jivesoftware.smack.packet.Message.Body> mColl = smackMessage.getBodies();
for (org.jivesoftware.smack.packet.Message.Body bodyPart : mColl) {
String msg = bodyPart.getMessage();
if (msg != null) {
body = msg;
break;
}
}
}
ChatSession session = findOrCreateSession(smackMessage.getFrom().toString(), isGroupMessage);
if (// not subscribed so don't do anything
session != null) {
if (body != null && session != null) {
Message rec = new Message(body);
rec.setTo(new XmppAddress(smackMessage.getTo().toString()));
rec.setFrom(new XmppAddress(smackMessage.getFrom().toString()));
rec.setDateTime(new Date());
rec.setID(smackMessage.getStanzaId());
if (isOmemo)
rec.setType(Imps.MessageType.INCOMING_ENCRYPTED_VERIFIED);
else
rec.setType(Imps.MessageType.INCOMING);
// Detect if this was said by us, and mark message as outgoing
if (isGroupMessage) {
if (TextUtils.isEmpty(rec.getFrom().getResource())) {
// do nothing if there is no resource since that is a system message
return;
} else if (rec.getFrom().getResource().equals(rec.getTo().getUser())) {
try {
// rec.setType(Imps.MessageType.OUTGOING);
Occupant oc = mChatGroupManager.getMultiUserChat(rec.getFrom().getBareAddress()).getOccupant(JidCreate.entityFullFrom(rec.getFrom().getAddress()));
if (oc != null && oc.getJid().equals(mUser.getAddress().getAddress()))
// do nothing if it is from us
return;
} catch (Exception e) {
debug(TAG, "error parsing address", e);
}
}
}
boolean good = session.onReceiveMessage(rec, notifyUser);
if (smackMessage.getExtension("request", DeliveryReceipt.NAMESPACE) != null) {
if (good) {
debug(TAG, "sending delivery receipt");
// got XEP-0184 request, send receipt
sendReceipt(smackMessage);
session.onReceiptsExpected(true);
} else {
debug(TAG, "not sending delivery receipt due to processing error");
}
} else {
// no request for delivery receipt
session.onReceiptsExpected(false);
}
}
}
}
use of org.awesomeapp.messenger.model.Message in project Zom-Android by zom.
the class XmppConnection method initConnection.
// Runs in executor thread
private void initConnection(Imps.ProviderSettings.QueryMap providerSettings, String userName) throws InterruptedException, NoSuchAlgorithmException, KeyManagementException, XMPPException, SmackException, IOException {
// never! // providerSettings.getAllowPlainAuth();
boolean allowPlainAuth = false;
// providerSettings.getRequireTls(); //always!
boolean requireTls = true;
boolean doDnsSrv = providerSettings.getDoDnsSrv();
// boolean tlsCertVerify = providerSettings.getTlsCertVerify();
// boolean useSASL = true;//!allowPlainAuth;
// boolean useProxy = providerSettings.getUseTor();
String domain = providerSettings.getDomain();
mPriority = providerSettings.getXmppResourcePrio();
int serverPort = providerSettings.getPort();
String server = providerSettings.getServer();
if ("".equals(server))
server = null;
if (domain.equals("dukgo.com")) {
doDnsSrv = false;
server = "dukgo.com";
}
debug(TAG, "TLS required? " + requireTls);
if (// if serverPort is set to 0 then use 5222 as default
serverPort == 0)
serverPort = 5222;
mConfig = XMPPTCPConnectionConfiguration.builder();
mConfig.setServiceName(JidCreate.domainBareFrom(domain));
mConfig.setPort(serverPort);
mConfig.setCompressionEnabled(true);
mConfig.setConnectTimeout(CONNECT_TIMEOUT);
mConfig.setXmppDomain(domain);
mConfig.setHost(domain);
if (!TextUtils.isEmpty(server))
mConfig.setHost(server);
if (!TextUtils.isEmpty(Preferences.getProxyServerHost())) {
setProxy("SOCKS5", Preferences.getProxyServerHost(), Preferences.getProxyServerPort());
} else if (Preferences.useAdvancedNetworking()) {
setProxy("SOCKS5", "127.0.0.1", 31059);
} else {
mProxyInfo = null;
// SRV lookup shouldn't be done through a proxy
if (doDnsSrv) {
// java.lang.System.setProperty("java.net.preferIPv4Stack", "true");
// java.lang.System.setProperty("java.net.preferIPv6Addresses", "false");
debug(TAG, "(DNS SRV) resolving: " + domain);
List<HostAddress> listHostsFailed = new ArrayList<>();
List<HostAddress> listHosts = DNSUtil.resolveXMPPServiceDomain(domain, listHostsFailed, ConnectionConfiguration.DnssecMode.disabled);
if (listHosts.size() > 0) {
server = listHosts.get(0).getFQDN();
serverPort = listHosts.get(0).getPort();
debug(TAG, "(DNS SRV) resolved: " + domain + "=" + server + ":" + serverPort);
if (!TextUtils.isEmpty(server))
mConfig.setHost(server);
if (serverPort != -1)
mConfig.setPort(serverPort);
}
}
if (!TextUtils.isEmpty(server)) {
try {
String[] addressParts = server.split("\\.");
if (Integer.parseInt(addressParts[0]) != -1) {
byte[] parts = new byte[addressParts.length];
for (int i = 0; i < 4; i++) parts[i] = (byte) Integer.parseInt(addressParts[i]);
byte[] ipAddr = new byte[] { parts[0], parts[1], parts[2], parts[3] };
InetAddress addr = InetAddress.getByAddress(ipAddr);
mConfig.setHostAddress(addr);
} else {
mConfig.setHostAddress(InetAddress.getByName(server));
}
} catch (Exception e) {
debug(TAG, "error parsing server as IP address; using as hostname instead");
mConfig.setHostAddress(InetAddress.getByName(server));
}
}
}
mConfig.setProxyInfo(mProxyInfo);
mConfig.setDebuggerEnabled(Debug.DEBUG_ENABLED);
SmackConfiguration.DEBUG = Debug.DEBUG_ENABLED;
SmackConfiguration.setDebuggerFactory(new SmackDebuggerFactory() {
@Override
public SmackDebugger create(XMPPConnection xmppConnection, Writer writer, Reader reader) throws IllegalArgumentException {
return new AndroidDebugger(xmppConnection, writer, reader);
}
});
// mConfig.setSASLAuthenticationEnabled(useSASL);
// Android has no support for Kerberos or GSSAPI, so disable completely
SASLAuthentication.unregisterSASLMechanism("KERBEROS_V4");
SASLAuthentication.unregisterSASLMechanism("GSSAPI");
if (allowPlainAuth)
SASLAuthentication.unBlacklistSASLMechanism("PLAIN");
SASLAuthentication.unBlacklistSASLMechanism("DIGEST-MD5");
if (mMemTrust == null)
mMemTrust = new MemorizingTrustManager(mContext);
if (sslContext == null) {
sslContext = SSLContext.getInstance(SSLCONTEXT_TYPE);
secureRandom = new java.security.SecureRandom();
sslContext.init(null, MemorizingTrustManager.getInstanceList(mContext), secureRandom);
while (true) {
try {
if (Build.VERSION.SDK_INT >= 20) {
sslContext.getDefaultSSLParameters().setCipherSuites(XMPPCertPins.SSL_IDEAL_CIPHER_SUITES_API_20);
} else {
sslContext.getDefaultSSLParameters().setCipherSuites(XMPPCertPins.SSL_IDEAL_CIPHER_SUITES);
}
break;
} catch (IllegalStateException e) {
debug(TAG, "error setting cipher suites; waiting for SSLContext to init...");
try {
Thread.sleep(1000);
} catch (Exception e2) {
}
}
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
mConfig.setKeystoreType("AndroidCAStore");
mConfig.setKeystorePath(null);
} else {
mConfig.setKeystoreType("BKS");
String path = System.getProperty("javax.net.ssl.trustStore");
if (path == null)
path = System.getProperty("java.home") + File.separator + "etc" + File.separator + "security" + File.separator + "cacerts.bks";
mConfig.setKeystorePath(path);
}
// wait a second while the ssl context init's
try {
Thread.sleep(1000);
} catch (Exception e) {
}
}
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= 16) {
while (true) {
try {
mConfig.setEnabledSSLProtocols(new String[] { "TLSv1.2", "TLSv1.1", "TLSv1" });
sslContext.getDefaultSSLParameters().setProtocols(new String[] { "TLSv1.2", "TLSv1.1", "TLSv1" });
break;
} catch (IllegalStateException ise) {
try {
Thread.sleep(1000);
} catch (Exception e) {
}
}
}
}
if (currentapiVersion >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
mConfig.setEnabledSSLCiphers(XMPPCertPins.SSL_IDEAL_CIPHER_SUITES);
}
mConfig.setCustomSSLContext(sslContext);
mConfig.setSecurityMode(ConnectionConfiguration.SecurityMode.required);
mConfig.setHostnameVerifier(mMemTrust.wrapHostnameVerifier(new org.apache.http.conn.ssl.StrictHostnameVerifier()));
mConfig.setSendPresence(true);
XMPPTCPConnection.setUseStreamManagementDefault(true);
XMPPTCPConnection.setUseStreamManagementResumptiodDefault(true);
mConnection = new XMPPTCPConnection(mConfig.build());
DeliveryReceiptManager.getInstanceFor(mConnection).addReceiptReceivedListener(new ReceiptReceivedListener() {
@Override
public void onReceiptReceived(Jid fromJid, Jid toJid, String receiptId, Stanza receipt) {
ChatSession session = mSessionManager.findSession(fromJid.asBareJid());
if (session != null)
session.onMessageReceipt(receiptId);
}
});
mConnection.addSyncStanzaListener(new StanzaListener() {
@Override
public void processStanza(Stanza stanza) {
debug(TAG, "receive message: " + stanza.getFrom() + " to " + stanza.getTo());
org.jivesoftware.smack.packet.Message smackMessage = (org.jivesoftware.smack.packet.Message) stanza;
handleMessage(smackMessage, false, true);
String msg_xml = smackMessage.toXML().toString();
try {
handleChatState(smackMessage.getFrom().toString(), msg_xml);
} catch (RemoteException re) {
// no worries
}
}
}, new StanzaTypeFilter(org.jivesoftware.smack.packet.Message.class));
mConnection.addSyncStanzaListener(new StanzaListener() {
@Override
public void processStanza(Stanza packet) {
org.jivesoftware.smack.packet.Presence presence = (org.jivesoftware.smack.packet.Presence) packet;
qPresence.add(presence);
}
}, new StanzaTypeFilter(org.jivesoftware.smack.packet.Presence.class));
if (mTimerPackets != null)
mTimerPackets.cancel();
initPacketProcessor();
if (mTimerPresence != null)
mTimerPresence.cancel();
initPresenceProcessor();
if (mTimerNewContacts != null)
mTimerNewContacts.cancel();
initNewContactProcessor();
ConnectionListener connectionListener = new ConnectionListener() {
/**
* Called from smack when connect() is fully successful
*
* This is called on the executor thread while we are in reconnect()
*/
@Override
public void reconnectionSuccessful() {
if (mStreamHandler == null || !mStreamHandler.isResumePending()) {
debug(TAG, "Reconnection success");
onReconnectionSuccessful();
mRoster = Roster.getInstanceFor(mConnection);
sendPresencePacket();
mChatGroupManager.reconnectAll();
} else {
debug(TAG, "Ignoring reconnection callback due to pending resume");
}
}
@Override
public void reconnectionFailed(Exception e) {
debug(TAG, "reconnection failed", e);
// We are not using the reconnection manager
// throw new UnsupportedOperationException();
execute(new Runnable() {
public void run() {
mNeedReconnect = true;
setState(LOGGING_IN, new ImErrorInfo(ImErrorInfo.NETWORK_ERROR, "network error"));
reconnect();
}
});
}
@Override
public void reconnectingIn(int seconds) {
// // We are not using the reconnection manager
// throw new UnsupportedOperationException();
debug(TAG, "reconnecting in " + seconds + " seconds...");
}
@Override
public void connectionClosedOnError(final Exception e) {
/*
* This fires when:
* - Packet reader or writer detect an error
* - Stream compression failed
* - TLS fails but is required
* - Network error
* - We forced a socket shutdown
*/
debug(TAG, "reconnect on error: " + e.getMessage(), e);
if (e.getMessage().contains("conflict")) {
execute(new Runnable() {
@Override
public void run() {
// disconnect();
disconnected(new ImErrorInfo(ImpsErrorInfo.ALREADY_LOGGED, "logged in from another location"));
}
});
} else if (!mNeedReconnect) {
execute(new Runnable() {
public void run() {
if (getState() == LOGGED_IN) {
mNeedReconnect = true;
setState(LOGGING_IN, new ImErrorInfo(ImErrorInfo.NETWORK_ERROR, "network error"));
reconnect();
}
}
});
}
}
@Override
public void connected(XMPPConnection connection) {
debug(TAG, "connected");
try {
initOmemo((XMPPTCPConnection) connection);
} catch (Exception e) {
debug("OMEMO", "There was a problem init'g omemo", e);
}
}
@Override
public void authenticated(XMPPConnection connection, boolean resumed) {
debug(TAG, "authenticated: resumed=" + resumed);
sendPresencePacket();
mChatGroupManager.reconnectAll();
}
@Override
public void connectionClosed() {
debug(TAG, "connection closed");
// if the state is logged in, we should try to reconnect!
if (getState() == LOGGED_IN) {
execute(new Runnable() {
public void run() {
mNeedReconnect = true;
setState(LOGGING_IN, new ImErrorInfo(ImErrorInfo.NETWORK_ERROR, "network error"));
reconnect();
}
});
}
}
};
mConnection.addConnectionListener(connectionListener);
mStreamHandler = new XmppStreamHandler(mConnection, connectionListener);
Exception xmppConnectException = null;
AbstractXMPPConnection conn = mConnection.connect();
}
use of org.awesomeapp.messenger.model.Message in project Zom-Android by zom.
the class ChatSessionAdapter method sendPushWhitelistToken.
public boolean sendPushWhitelistToken(@NonNull String token) {
if (mConnection.getState() == ImConnection.SUSPENDED) {
// TODO Is it possible to postpone a TLV message? e.g: insertMessageInDb with type QUEUED
return false;
}
// Whitelist tokens are intended for one recipient, for now
if (isGroupChatSession())
return false;
org.awesomeapp.messenger.model.Message msg = new org.awesomeapp.messenger.model.Message("");
msg.setFrom(mConnection.getLoginUser().getAddress());
msg.setType(Imps.MessageType.OUTGOING);
mChatSession.sendPushWhitelistTokenAsync(msg, new String[] { token });
return true;
}
use of org.awesomeapp.messenger.model.Message in project Zom-Android by zom.
the class XmppConnection method initOmemo.
private synchronized Omemo initOmemo(XMPPTCPConnection conn) throws Exception {
if (conn != null && conn.isConnected()) {
mOmemoInstance = new Omemo(conn, mUserJid);
mOmemoInstance.getManager().addOmemoMessageListener(new OmemoMessageListener() {
@Override
public void onOmemoKeyTransportReceived(CipherAndAuthTag cipherAndAuthTag, org.jivesoftware.smack.packet.Message message, org.jivesoftware.smack.packet.Message message1, OmemoMessageInformation omemoMessageInformation) {
debug(TAG, "omemo key transport received");
}
@Override
public void onOmemoMessageReceived(String body, org.jivesoftware.smack.packet.Message message, org.jivesoftware.smack.packet.Message message1, OmemoMessageInformation omemoMessageInformation) {
if (body != null) {
debug(TAG, "got inbound message omemo: from:" + message.getFrom() + "=" + message.getBody());
message.setBody(body);
handleMessage(message, true, true);
} else {
debug(TAG, "got empty ibound message omemo: from:" + message.getFrom().toString());
}
}
});
mOmemoInstance.getManager().addOmemoMucMessageListener(new OmemoMucMessageListener() {
@Override
public void onOmemoMucMessageReceived(MultiUserChat muc, BareJid from, String decryptedBody, org.jivesoftware.smack.packet.Message message, org.jivesoftware.smack.packet.Message wrappingMessage, OmemoMessageInformation omemoInformation) {
if (decryptedBody != null) {
debug(TAG, "got inbound MUC message omemo: from:" + message.getFrom() + "=" + message.getBody());
org.jivesoftware.smack.packet.Message messagePlain = new org.jivesoftware.smack.packet.Message();
messagePlain.setType(message.getType());
messagePlain.setFrom(message.getFrom());
messagePlain.setTo(message.getTo());
messagePlain.setBody(decryptedBody);
messagePlain.setStanzaId(message.getStanzaId());
messagePlain.addExtensions(message.getExtensions());
messagePlain.setThread(message.getThread());
messagePlain.setSubject(message.getSubject());
handleMessage(messagePlain, true, true);
} else {
debug(TAG, "got empty ibound MUC message omemo: from:" + message.getFrom().toString());
}
}
@Override
public void onOmemoKeyTransportReceived(MultiUserChat muc, BareJid from, CipherAndAuthTag cipherAndAuthTag, org.jivesoftware.smack.packet.Message message, org.jivesoftware.smack.packet.Message wrappingMessage, OmemoMessageInformation omemoInformation) {
debug(TAG, "got OmemoKey Transport from: " + from.toString());
}
});
addOmemoListener(mOmemoInstance.getManager());
}
return mOmemoInstance;
}
use of org.awesomeapp.messenger.model.Message in project Zom-Android by zom.
the class ChatSessionAdapter method sendMessage.
public void sendMessage(String text, boolean isResend) {
if (mConnection.getState() != ImConnection.LOGGED_IN) {
// connection has been suspended, save the message without send it
long now = System.currentTimeMillis();
insertMessageInDb(null, text, now, Imps.MessageType.QUEUED, null);
return;
}
org.awesomeapp.messenger.model.Message msg = new org.awesomeapp.messenger.model.Message(text);
msg.setID(nextID());
msg.setFrom(mConnection.getLoginUser().getAddress());
msg.setType(Imps.MessageType.QUEUED);
long sendTime = System.currentTimeMillis();
if (!isResend) {
insertMessageInDb(null, text, sendTime, msg.getType(), 0, msg.getID(), null);
insertOrUpdateChat(text);
}
int newType = mChatSession.sendMessageAsync(msg);
if (msg.getDateTime() != null)
sendTime = msg.getDateTime().getTime();
updateMessageInDb(msg.getID(), newType, sendTime, null);
}
Aggregations