Search in sources :

Example 56 with NetworkException

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(String account, String 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);
            for (MessageItem messageItem : abstractChat.getMessages()) {
                if (messageItem.getAction() != null) {
                    continue;
                }
                final String name;
                if (isMUC) {
                    name = messageItem.getResource();
                } else {
                    if (messageItem.isIncoming()) {
                        name = userName;
                    } else {
                        name = accountName;
                    }
                }
                out.write("<b>");
                out.write(StringUtils.escapeHtml(name));
                out.write("</b>&nbsp;(");
                out.write(StringUtils.getDateTimeText(messageItem.getTimestamp()));
                out.write(")<br />\n<p>");
                out.write(StringUtils.escapeHtml(messageItem.getText()));
                out.write("</p><hr />\n");
            }
        }
        out.write("</body></html>");
        out.close();
    } catch (IOException e) {
        throw new NetworkException(R.string.FILE_NOT_FOUND);
    }
    return file;
}
Also used : FileWriter(java.io.FileWriter) IOException(java.io.IOException) File(java.io.File) RoomChat(com.xabber.android.data.extension.muc.RoomChat) NetworkException(com.xabber.android.data.NetworkException) BufferedWriter(java.io.BufferedWriter)

Example 57 with NetworkException

use of com.xabber.android.data.NetworkException in project xabber-android by redsolution.

the class ReceiptManager method onPacket.

@Override
public void onPacket(ConnectionItem connection, String bareAddress, Stanza packet) {
    if (!(connection instanceof AccountItem))
        return;
    String account = ((AccountItem) connection).getAccount();
    final String user = packet.getFrom();
    if (user == null)
        return;
    if (!(packet instanceof Message))
        return;
    final Message message = (Message) packet;
    if (message.getType() == Message.Type.error) {
        final MessageItem messageItem = sent.remove(account, message.getPacketID());
        if (messageItem != null && !messageItem.isError()) {
            messageItem.markAsError();
            Application.getInstance().runInBackground(new Runnable() {

                @Override
                public void run() {
                    if (messageItem.getId() != null)
                        MessageTable.getInstance().markAsError(messageItem.getId());
                }
            });
            MessageManager.getInstance().onChatChanged(messageItem.getChat().getAccount(), messageItem.getChat().getUser(), false);
        }
    } else {
        // TODO setDefaultAutoReceiptMode should be used
        for (ExtensionElement packetExtension : message.getExtensions()) if (packetExtension instanceof DeliveryReceiptRequest) {
            String id = message.getPacketID();
            if (id == null)
                continue;
            Message receipt = new Message(user);
            receipt.addExtension(new DeliveryReceipt(id));
            // the key problem is Thread - smack does not keep it in auto reply
            receipt.setThread(message.getThread());
            try {
                ConnectionManager.getInstance().sendStanza(account, receipt);
            } catch (NetworkException e) {
                LogManager.exception(this, e);
            }
        }
    }
}
Also used : Message(org.jivesoftware.smack.packet.Message) AccountItem(com.xabber.android.data.account.AccountItem) DeliveryReceiptRequest(org.jivesoftware.smackx.receipts.DeliveryReceiptRequest) DeliveryReceipt(org.jivesoftware.smackx.receipts.DeliveryReceipt) ExtensionElement(org.jivesoftware.smack.packet.ExtensionElement) NetworkException(com.xabber.android.data.NetworkException)

Example 58 with NetworkException

use of com.xabber.android.data.NetworkException in project xabber-android by redsolution.

the class OTRManager method initSmp.

/**
     * Initiate request using SM protocol.
     */
public void initSmp(String account, String user, String question, String secret) throws NetworkException {
    LogManager.i(this, "initializing smp... " + user);
    removeSMRequest(account, user);
    addSMProgress(account, user);
    try {
        getOrCreateSession(account, user).initSmp(question, secret);
    } catch (OtrException e) {
        throw new NetworkException(R.string.OTR_ERROR, e);
    }
}
Also used : OtrException(net.java.otr4j.OtrException) NetworkException(com.xabber.android.data.NetworkException)

Example 59 with NetworkException

use of com.xabber.android.data.NetworkException in project xabber-android by redsolution.

the class OTRManager method startSession.

public void startSession(String account, String user) throws NetworkException {
    LogManager.i(this, "Starting session for " + user);
    try {
        getOrCreateSession(account, user).startSession();
    } catch (OtrException e) {
        throw new NetworkException(R.string.OTR_ERROR, e);
    }
    LogManager.i(this, "Started session for " + user);
}
Also used : OtrException(net.java.otr4j.OtrException) NetworkException(com.xabber.android.data.NetworkException)

Example 60 with NetworkException

use of com.xabber.android.data.NetworkException in project xabber-android by redsolution.

the class OTRManager method respondSmp.

/**
     * Respond using SM protocol.
     */
public void respondSmp(String account, String user, String question, String secret) throws NetworkException {
    LogManager.i(this, "responding smp... " + user);
    removeSMRequest(account, user);
    addSMProgress(account, user);
    try {
        getOrCreateSession(account, user).respondSmp(question, secret);
    } catch (OtrException e) {
        throw new NetworkException(R.string.OTR_ERROR, e);
    }
}
Also used : OtrException(net.java.otr4j.OtrException) NetworkException(com.xabber.android.data.NetworkException)

Aggregations

NetworkException (com.xabber.android.data.NetworkException)63 Message (org.jivesoftware.smack.packet.Message)13 OtrException (net.java.otr4j.OtrException)11 AccountItem (com.xabber.android.data.account.AccountItem)10 AbstractChat (com.xabber.android.data.message.AbstractChat)10 AccountJid (com.xabber.android.data.entity.AccountJid)9 OnResponseListener (com.xabber.android.data.connection.OnResponseListener)6 Date (java.util.Date)6 IQ (org.jivesoftware.smack.packet.IQ)6 UserJid (com.xabber.android.data.entity.UserJid)5 Presence (org.jivesoftware.smack.packet.Presence)4 Intent (android.content.Intent)3 Set (com.xabber.xmpp.rsm.Set)3 File (java.io.File)3 MessageItem (com.xabber.android.data.database.messagerealm.MessageItem)2 Captcha (com.xabber.android.data.extension.captcha.Captcha)2 RoomChat (com.xabber.android.data.extension.muc.RoomChat)2 NotificationState (com.xabber.android.data.message.NotificationState)2 RegularChat (com.xabber.android.data.message.RegularChat)2 OnRosterChangedListener (com.xabber.android.data.roster.OnRosterChangedListener)2