use of org.jivesoftware.smack.packet.Message in project xabber-android by redsolution.
the class RoomChat method onPacket.
@Override
protected boolean onPacket(UserJid bareAddress, Stanza stanza, boolean isCarbons) {
if (!super.onPacket(bareAddress, stanza, isCarbons)) {
return false;
}
// MUCUser mucUserExtension = MUCUser.from(stanza);
// if (mucUserExtension != null && mucUserExtension.getInvite() != null) {
// return false;
// }
final org.jxmpp.jid.Jid from = stanza.getFrom();
final Resourcepart resource = from.getResourceOrNull();
if (stanza instanceof Message) {
final Message message = (Message) stanza;
if (message.getType() == Message.Type.error) {
UserJid invite = invites.remove(message.getStanzaId());
if (invite != null) {
newAction(nickname, invite.toString(), ChatAction.invite_error);
}
return true;
}
MUCUser mucUser = MUCUser.from(stanza);
if (mucUser != null && mucUser.getDecline() != null) {
onInvitationDeclined(mucUser.getDecline().getFrom(), mucUser.getDecline().getReason());
return true;
}
if (mucUser != null && mucUser.getStatus() != null && mucUser.getStatus().contains(MUCUser.Status.create("100")) && ChatManager.getInstance().isSuppress100(account, user)) {
// 'This room is not anonymous'
return true;
}
final String text = message.getBody();
final String subject = message.getSubject();
if (text == null && subject == null) {
return true;
}
if (subject != null) {
if (this.subject.equals(subject)) {
return true;
}
this.subject = subject;
RosterManager.onContactChanged(account, bareAddress);
newAction(resource, subject, ChatAction.subject);
} else {
boolean notify = true;
String stanzaId = message.getStanzaId();
// disabling because new messages without stanza will be repeated
// if (stanzaId == null) stanzaId = UUID.randomUUID().toString();
DelayInformation delayInformation = DelayInformation.from(message);
Date delay = null;
if (delayInformation != null) {
delay = delayInformation.getStamp();
}
if (delay != null) {
notify = false;
}
final long startTime = System.currentTimeMillis();
Realm realm = MessageDatabaseManager.getInstance().getRealmUiThread();
final MessageItem sameMessage = realm.where(MessageItem.class).equalTo(MessageItem.Fields.STANZA_ID, stanzaId).findFirst();
// Server send our own message back
if (sameMessage != null) {
// realm.beginTransaction();
// sameMessage.setDelivered(true);
// realm.commitTransaction();
LogManager.d("REALM", Thread.currentThread().getName() + " save message delivered: " + (System.currentTimeMillis() - startTime));
return true;
}
if (isSelf(resource)) {
// Own message from other client
notify = false;
}
updateThreadId(message.getThread());
createAndSaveNewMessage(resource, text, null, delay, true, notify, false, false, stanzaId);
}
} else if (stanza instanceof Presence) {
Presence presence = (Presence) stanza;
if (presence.getType() == Presence.Type.available) {
Occupant oldOccupant = occupants.get(resource);
Occupant newOccupant = createOccupant(resource, presence);
newOccupant.setJid(from);
occupants.put(resource, newOccupant);
if (oldOccupant == null) {
onAvailable(resource);
RosterManager.onContactChanged(account, user);
} else {
boolean changed = false;
if (oldOccupant.getAffiliation() != newOccupant.getAffiliation()) {
changed = true;
onAffiliationChanged(resource, newOccupant.getAffiliation());
}
if (oldOccupant.getRole() != newOccupant.getRole()) {
changed = true;
onRoleChanged(resource, newOccupant.getRole());
}
if (oldOccupant.getStatusMode() != newOccupant.getStatusMode() || !oldOccupant.getStatusText().equals(newOccupant.getStatusText())) {
changed = true;
onStatusChanged(resource, newOccupant.getStatusMode(), newOccupant.getStatusText());
}
if (changed) {
RosterManager.onContactChanged(account, user);
}
}
} else if (presence.getType() == Presence.Type.unavailable && state == RoomState.available) {
occupants.remove(resource);
MUCUser mucUser = MUCUser.from(presence);
if (mucUser != null && mucUser.getStatus() != null) {
if (mucUser.getStatus().contains(MUCUser.Status.KICKED_307)) {
onKick(resource, mucUser.getItem().getActor());
} else if (mucUser.getStatus().contains(MUCUser.Status.BANNED_301)) {
onBan(resource, mucUser.getItem().getActor());
} else if (mucUser.getStatus().contains(MUCUser.Status.NEW_NICKNAME_303)) {
Resourcepart newNick = mucUser.getItem().getNick();
if (newNick == null) {
return true;
}
onRename(resource, newNick);
Occupant occupant = createOccupant(newNick, presence);
occupants.put(newNick, occupant);
} else if (mucUser.getStatus().contains(MUCUser.Status.REMOVED_AFFIL_CHANGE_321)) {
onRevoke(resource, mucUser.getItem().getActor());
}
} else {
onLeave(resource);
}
RosterManager.onContactChanged(account, user);
}
}
return true;
}
use of org.jivesoftware.smack.packet.Message in project xabber-android by redsolution.
the class OTRManager method injectMessage.
private void injectMessage(String account, String user, String msg) throws OtrException {
LogManager.i(this, "injectMessage. user: " + user + " message: " + msg);
AbstractChat abstractChat = getChat(account, user);
SSNManager.getInstance().setSessionOtrMode(account, user, abstractChat.getThreadId(), OtrMode.prefer);
Message message = abstractChat.createMessagePacket(msg);
CarbonManager.getInstance().setMessageToIgnoreCarbons(message);
try {
StanzaSender.sendStanza(abstractChat.getAccount(), message);
} catch (NetworkException e) {
throw new OtrException(e);
}
}
use of org.jivesoftware.smack.packet.Message in project xabber-android by redsolution.
the class AbstractChat method createMessagePacket.
/**
* @return New message packet to be sent.
*/
public Message createMessagePacket(String body) {
Message message = new Message();
message.setTo(getTo());
message.setType(getType());
message.setBody(body);
message.setThread(threadId);
return message;
}
use of org.jivesoftware.smack.packet.Message in project xabber-android by redsolution.
the class AbstractChat method sendMessage.
@SuppressWarnings("WeakerAccess")
boolean sendMessage(MessageItem messageItem) {
String text = prepareText(messageItem.getText());
messageItem.setEncrypted(OTRManager.getInstance().isEncrypted(text));
Long timestamp = messageItem.getTimestamp();
Date currentTime = new Date(System.currentTimeMillis());
Date delayTimestamp = null;
if (timestamp != null) {
if (currentTime.getTime() - timestamp > 60000) {
delayTimestamp = currentTime;
}
}
Message message = null;
if (text != null) {
message = createMessagePacket(text);
}
if (message != null) {
ChatStateManager.getInstance().updateOutgoingMessage(AbstractChat.this, message);
CarbonManager.getInstance().updateOutgoingMessage(AbstractChat.this, message);
if (delayTimestamp != null) {
message.addExtension(new DelayInformation(delayTimestamp));
}
final String messageId = messageItem.getUniqueId();
try {
StanzaSender.sendStanza(account, message, new StanzaListener() {
@Override
public void processStanza(Stanza packet) throws SmackException.NotConnectedException {
Realm realm = MessageDatabaseManager.getInstance().getNewBackgroundRealm();
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
MessageItem acknowledgedMessage = realm.where(MessageItem.class).equalTo(MessageItem.Fields.UNIQUE_ID, messageId).findFirst();
if (acknowledgedMessage != null) {
acknowledgedMessage.setAcknowledged(true);
}
}
});
realm.close();
}
});
} catch (NetworkException e) {
return false;
}
}
if (message == null) {
messageItem.setError(true);
messageItem.setErrorDescription("Internal error: message is null");
} else {
messageItem.setStanzaId(message.getStanzaId());
}
if (delayTimestamp != null) {
messageItem.setDelayTimestamp(delayTimestamp.getTime());
}
if (messageItem.getTimestamp() == null) {
messageItem.setTimestamp(currentTime.getTime());
}
messageItem.setSent(true);
return true;
}
use of org.jivesoftware.smack.packet.Message in project xabber-android by redsolution.
the class MessageManager method onStanza.
@Override
public void onStanza(ConnectionItem connection, Stanza stanza) {
if (stanza.getFrom() == null) {
return;
}
AccountJid account = connection.getAccount();
final UserJid user;
try {
user = UserJid.from(stanza.getFrom()).getBareUserJid();
} catch (UserJid.UserJidCreateException e) {
return;
}
boolean processed = false;
for (AbstractChat chat : chats.getNested(account.toString()).values()) {
if (chat.onPacket(user, stanza, false)) {
processed = true;
break;
}
}
final AbstractChat chat = getChat(account, user);
if (chat != null && stanza instanceof Message) {
if (chat.isPrivateMucChat() && !chat.isPrivateMucChatAccepted()) {
if (mucPrivateChatRequestProvider.get(chat.getAccount(), chat.getUser()) == null) {
mucPrivateChatRequestProvider.add(new MucPrivateChatNotification(account, user), true);
}
}
return;
}
if (!processed && stanza instanceof Message) {
final Message message = (Message) stanza;
final String body = message.getBody();
if (body == null) {
return;
}
// check for spam
if (SettingsManager.spamFilterMode() != SettingsManager.SpamFilterMode.disabled && RosterManager.getInstance().getRosterContact(account, user) == null) {
String thread = ((Message) stanza).getThread();
if (SettingsManager.spamFilterMode() == SettingsManager.SpamFilterMode.authCaptcha) {
// check if this message is captcha-answer
Captcha captcha = CaptchaManager.getInstance().getCaptcha(account, user);
if (captcha != null) {
// attempt limit overhead
if (captcha.getAttemptCount() > CaptchaManager.CAPTCHA_MAX_ATTEMPT_COUNT) {
// remove this captcha
CaptchaManager.getInstance().removeCaptcha(account, user);
// discard subscription
try {
PresenceManager.getInstance().discardSubscription(account, user);
} catch (NetworkException e) {
e.printStackTrace();
}
sendMessageWithoutChat(user.getJid(), thread, account, Application.getInstance().getResources().getString(R.string.spam_filter_captcha_many_attempts));
return;
}
if (body.equals(captcha.getAnswer())) {
// captcha solved successfully
// remove this captcha
CaptchaManager.getInstance().removeCaptcha(account, user);
// show auth
PresenceManager.getInstance().handleSubscriptionRequest(account, user);
sendMessageWithoutChat(user.getJid(), thread, account, Application.getInstance().getResources().getString(R.string.spam_filter_captcha_correct));
return;
} else {
// captcha solved unsuccessfully
// increment attempt count
captcha.setAttemptCount(captcha.getAttemptCount() + 1);
// send warning-message
sendMessageWithoutChat(user.getJid(), thread, account, Application.getInstance().getResources().getString(R.string.spam_filter_captcha_incorrect));
return;
}
} else {
// no captcha exist and user not from roster
sendMessageWithoutChat(user.getJid(), thread, account, Application.getInstance().getResources().getString(R.string.spam_filter_limit_message));
// and skip received message as spam
return;
}
} else {
// if message from not-roster user
// send a warning message to sender
sendMessageWithoutChat(user.getJid(), thread, account, Application.getInstance().getResources().getString(R.string.spam_filter_limit_message));
// and skip received message as spam
return;
}
}
if (message.getType() == Message.Type.chat && MUCManager.getInstance().hasRoom(account, user.getJid().asEntityBareJidIfPossible())) {
try {
createPrivateMucChat(account, user.getJid().asFullJidIfPossible()).onPacket(user, stanza, false);
} catch (UserJid.UserJidCreateException e) {
LogManager.exception(this, e);
}
mucPrivateChatRequestProvider.add(new MucPrivateChatNotification(account, user), true);
return;
}
for (ExtensionElement packetExtension : message.getExtensions()) {
if (packetExtension instanceof MUCUser) {
return;
}
}
createChat(account, user).onPacket(user, stanza, false);
}
}
Aggregations