use of com.xabber.android.data.NetworkException in project xabber-android by redsolution.
the class MessageManager method sendMessageWithoutChat.
// send messages without creating chat and adding to roster
// used for service auto-generated messages
public void sendMessageWithoutChat(Jid to, String threadId, AccountJid account, String text) {
Message message = new Message();
message.setTo(to);
message.setType(Message.Type.chat);
message.setBody(text);
message.setThread(threadId);
// send auto-generated messages without carbons
CarbonManager.getInstance().setMessageToIgnoreCarbons(message);
try {
StanzaSender.sendStanza(account, message);
} catch (NetworkException e) {
e.printStackTrace();
}
}
use of com.xabber.android.data.NetworkException in project xabber-android by redsolution.
the class MessageManager method exportChat.
/**
* Export chat to file with specified name.
*
* @param account
* @param user
* @param fileName
* @throws NetworkException
*/
public File exportChat(AccountJid account, UserJid user, String fileName) throws NetworkException {
final File file = new File(Environment.getExternalStorageDirectory(), fileName);
try {
BufferedWriter out = new BufferedWriter(new FileWriter(file));
final String titleName = RosterManager.getInstance().getName(account, user) + " (" + user + ")";
out.write("<html><head><title>");
out.write(StringUtils.escapeHtml(titleName));
out.write("</title></head><body>");
final AbstractChat abstractChat = getChat(account, user);
if (abstractChat != null) {
final boolean isMUC = abstractChat instanceof RoomChat;
final String accountName = AccountManager.getInstance().getNickName(account);
final String userName = RosterManager.getInstance().getName(account, user);
Realm realm = MessageDatabaseManager.getInstance().getNewBackgroundRealm();
RealmResults<MessageItem> messageItems = MessageDatabaseManager.getChatMessages(realm, account, user);
for (MessageItem messageItem : messageItems) {
if (messageItem.getAction() != null) {
continue;
}
final String name;
if (isMUC) {
name = messageItem.getResource().toString();
} else {
if (messageItem.isIncoming()) {
name = userName;
} else {
name = accountName;
}
}
out.write("<b>");
out.write(StringUtils.escapeHtml(name));
out.write("</b> (");
out.write(StringUtils.getDateTimeText(new Date(messageItem.getTimestamp())));
out.write(")<br />\n<p>");
out.write(StringUtils.escapeHtml(messageItem.getText()));
out.write("</p><hr />\n");
}
realm.close();
}
out.write("</body></html>");
out.close();
} catch (IOException e) {
throw new NetworkException(R.string.FILE_NOT_FOUND);
}
return file;
}
use of com.xabber.android.data.NetworkException in project xabber-android by redsolution.
the class PresenceManager method onStanza.
@Override
public void onStanza(ConnectionItem connection, Stanza stanza) {
if (!(connection instanceof AccountItem)) {
return;
}
if (!(stanza instanceof Presence)) {
return;
}
Presence presence = (Presence) stanza;
UserJid from;
try {
from = UserJid.from(stanza.getFrom());
} catch (UserJid.UserJidCreateException e) {
LogManager.exception(this, e);
return;
}
if (presence.getType() == Presence.Type.subscribe) {
AccountJid account = connection.getAccount();
// reject all subscribe-requests
if (SettingsManager.spamFilterMode() == SettingsManager.SpamFilterMode.noAuth) {
// send a warning message to sender
MessageManager.getInstance().sendMessageWithoutChat(from.getJid(), StringUtils.randomString(12), account, Application.getInstance().getResources().getString(R.string.spam_filter_ban_subscription));
// and discard subscription
try {
discardSubscription(account, UserJid.from(from.toString()));
} catch (NetworkException | UserJid.UserJidCreateException e) {
e.printStackTrace();
}
return;
}
// require captcha for subscription
if (SettingsManager.spamFilterMode() == SettingsManager.SpamFilterMode.authCaptcha) {
Captcha captcha = CaptchaManager.getInstance().getCaptcha(account, from);
// if captcha for this user already exist, check expires time and discard if need
if (captcha != null) {
if (captcha.getExpiresDate() < System.currentTimeMillis()) {
// discard subscription
try {
discardSubscription(account, UserJid.from(from.toString()));
} catch (NetworkException | UserJid.UserJidCreateException e) {
e.printStackTrace();
}
return;
}
// skip subscription, waiting for captcha in messageManager
return;
} else {
// generate captcha
String captchaQuestion = CaptchaManager.getInstance().generateAndSaveCaptcha(account, from);
// send captcha message to sender
MessageManager.getInstance().sendMessageWithoutChat(from.getJid(), StringUtils.randomString(12), account, Application.getInstance().getResources().getString(R.string.spam_filter_limit_subscription) + " " + captchaQuestion);
// and skip subscription, waiting for captcha in messageManager
return;
}
}
// subscription request
handleSubscriptionRequest(account, from);
}
}
use of com.xabber.android.data.NetworkException in project xabber-android by redsolution.
the class XabberAccountManager method updateLocalAccount.
public void updateLocalAccount(XMPPAccountSettings account) {
// if account synced
if (isAccountSynchronize(account.getJid()) || SettingsManager.isSyncAllAccounts()) {
AccountJid accountJid = getExistingAccount(account.getJid());
// create new xmpp-account
if (accountJid == null && !account.isDeleted()) {
try {
AccountJid jid = AccountManager.getInstance().addAccount(account.getJid(), "", account.getToken(), false, true, true, false, false, true);
AccountManager.getInstance().setColor(jid, ColorManager.getInstance().convertColorNameToIndex(account.getColor()));
AccountManager.getInstance().setOrder(jid, account.getOrder());
AccountManager.getInstance().setTimestamp(jid, account.getTimestamp());
AccountManager.getInstance().onAccountChanged(jid);
} catch (NetworkException e) {
Application.getInstance().onError(e);
}
// update existing xmpp-account
// now we are updated only color of account
} else if (accountJid != null && !account.isDeleted()) {
AccountManager.getInstance().setOrder(accountJid, account.getOrder());
AccountManager.getInstance().setTimestamp(accountJid, account.getTimestamp());
AccountManager.getInstance().setColor(accountJid, ColorManager.getInstance().convertColorNameToIndex(account.getColor()));
AccountManager.getInstance().onAccountChanged(accountJid);
// delete existing account
} else if (accountJid != null && account.isDeleted()) {
AccountManager.getInstance().removeAccountWithoutSync(accountJid);
}
}
}
use of com.xabber.android.data.NetworkException in project xabber-android by redsolution.
the class StanzaSender method getXmppTcpConnection.
@NonNull
private static XMPPTCPConnection getXmppTcpConnection(AccountJid account) throws NetworkException {
AccountItem accountItem = AccountManager.getInstance().getAccount(account);
if (accountItem == null) {
throw new NetworkException(R.string.NOT_CONNECTED);
}
XMPPTCPConnection returnConnection = accountItem.getConnection();
if (!returnConnection.isAuthenticated()) {
throw new NetworkException(R.string.NOT_CONNECTED);
}
return returnConnection;
}
Aggregations