aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/thedevstack/conversationsplus/xmpp/muc/MucPacketParser.java
blob: d7ec6b38bd605df4f46bde398ac2e187b5658c5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package de.thedevstack.conversationsplus.xmpp.muc;

import de.thedevstack.conversationsplus.entities.Account;
import de.thedevstack.conversationsplus.entities.Conversation;
import de.thedevstack.conversationsplus.persistance.DatabaseBackend;
import de.thedevstack.conversationsplus.utils.UiUpdateHelper;
import de.thedevstack.conversationsplus.utils.XmppConnectionServiceAccessor;
import de.thedevstack.conversationsplus.xml.Element;
import de.thedevstack.conversationsplus.xmpp.jid.Jid;

/**
 */
public class MucPacketParser {
    private static class Invite {
        Jid jid;
        String password;
        Invite(Jid jid, String password) {
            this.jid = jid;
            this.password = password;
        }

        public boolean execute(Account account) {
            if (jid != null) {
                Conversation conversation = XmppConnectionServiceAccessor.xmppConnectionService.findOrCreateConversation(account, jid, true);
                if (!conversation.getMucOptions().online()) {
                    conversation.getMucOptions().setPassword(password);
                    DatabaseBackend.getInstance().updateConversation(conversation);
                    XmppConnectionServiceAccessor.xmppConnectionService.joinMuc(conversation);
                    UiUpdateHelper.updateConversationUi();
                }
                return true;
            }
            return false;
        }
    }

    private static Invite extractInvite(Element message) {
        Element x = message.findChild("x", "http://jabber.org/protocol/muc#user");
        if (x != null) {
            Element invite = x.findChild("invite");
            if (invite != null) {
                Element pw = x.findChild("password");
                return new Invite(message.getAttributeAsJid("from"), pw != null ? pw.getContent(): null);
            }
        } else {
            x = message.findChild("x","jabber:x:conference");
            if (x != null) {
                return new Invite(x.getAttributeAsJid("jid"),x.getAttribute("password"));
            }
        }
        return null;
    }

    public static boolean extractAndExecuteInvite(Account account, Element packet) {
        Invite invite = extractInvite(packet);
        if (invite != null && invite.execute(account)) {
            return true;
        }

        return false;
    }
}