Search in sources :

Example 1 with XmppIdentity

use of com.googlecode.asmack.XmppIdentity in project AsmackService by rtreffer.

the class DiscoReceiver method onReceive.

/**
     * Called on incoming stanzas and replies on service discovery requests.
     * @param context The current context.
     * @param intent The stanza intent.
     */
@Override
public void onReceive(Context context, Intent intent) {
    Stanza stanza = intent.getParcelableExtra("stanza");
    // only accept IQ stanzas
    if (!"iq".equals(stanza.getName())) {
        return;
    }
    // of type="get"
    if (!"get".equals(stanza.getAttributeValue("type"))) {
        return;
    }
    // from / to / id are mandatory
    Attribute from = stanza.getAttribute("from");
    Attribute to = stanza.getAttribute("to");
    Attribute id = stanza.getAttribute("id");
    if (id == null || from == null || to == null) {
        return;
    }
    try {
        Node node = stanza.getDocumentNode();
        Node query = XMLUtils.getFirstChild(node, "http://jabber.org/protocol/disco#info", "query");
        if (query == null || !query.hasAttributes()) {
            return;
        }
        Node discoAttributeNode = query.getAttributes().getNamedItem("node");
        String discoNode = null;
        if (discoAttributeNode != null) {
            discoNode = discoAttributeNode.getTextContent();
        }
        // we got a disco, reply
        StringBuilder payload = new StringBuilder("<iq type='result'");
        payload.append(" from='");
        payload.append(XMLUtils.xmlEscape(to.getValue()));
        payload.append("' to='");
        payload.append(XMLUtils.xmlEscape(from.getValue()));
        payload.append("'>");
        payload.append("<query xmlns='");
        payload.append("http://jabber.org/protocol/disco#info");
        if (discoNode != null) {
            payload.append("' node='");
            payload.append(XMLUtils.xmlEscape(discoNode));
        }
        payload.append("'>");
        String myJid = XMPPUtils.getBareJid(to.getValue());
        for (XmppIdentity identity : Database.getIdentities(context, myJid, null)) {
            payload.append("<identity");
            if (identity.getCategory().length() > 0) {
                payload.append(" category='");
                payload.append(XMLUtils.xmlEscape(identity.getCategory()));
                payload.append('\'');
            }
            if (identity.getType().length() > 0) {
                payload.append(" type='");
                payload.append(XMLUtils.xmlEscape(identity.getType()));
                payload.append('\'');
            }
            if (identity.getLang().length() > 0) {
                payload.append(" lang='");
                payload.append(XMLUtils.xmlEscape(identity.getLang()));
                payload.append('\'');
            }
            if (identity.getName().length() > 0) {
                payload.append(" name='");
                payload.append(XMLUtils.xmlEscape(identity.getName()));
                payload.append('\'');
            }
            payload.append("/>");
        }
        for (String feature : Database.getFeatures(context, myJid, null)) {
            payload.append("<feature var='");
            payload.append(XMLUtils.xmlEscape(feature));
            payload.append("'/>");
        }
        payload.append("</query></iq>");
        Stanza discoReply = new Stanza("iq", "", XMPPUtils.getBareJid(to.getValue()), payload.toString(), Arrays.asList(new Attribute[] { id }));
        intent = new Intent();
        intent.setAction(XmppTransportService.XMPP_STANZA_SEND_INTENT);
        intent.putExtra("stanza", discoReply);
        intent.addFlags(Intent.FLAG_FROM_BACKGROUND);
        context.sendBroadcast(intent, XmppTransportService.XMPP_STANZA_SEND_INTENT);
    } catch (XmppMalformedException e) {
        // Impossible
        Log.e("AsmackService", "Please report (impossible condition)", e);
    }
}
Also used : XmppMalformedException(com.googlecode.asmack.XmppMalformedException) Attribute(com.googlecode.asmack.Attribute) XmppIdentity(com.googlecode.asmack.XmppIdentity) Stanza(com.googlecode.asmack.Stanza) Node(org.w3c.dom.Node) Intent(android.content.Intent)

Example 2 with XmppIdentity

use of com.googlecode.asmack.XmppIdentity in project AsmackService by rtreffer.

the class Database method computeVerificationHash.

/**
     * Compute the entity capabilities as described in
     * <a href="http://xmpp.org/extensions/xep-0115.html#ver">
     * XEP-0115 / 5. Verification String</a>. Please note that data forms
     * (XEP-0128) are not yet supported.
     * @param context The application/service context.
     * @param jid The user jid.
     * @param factory The cursor factory (can be null).
     * @return The base64 encoded sha-1 of the verification string.
     */
public static synchronized String computeVerificationHash(Context context, String jid, CursorFactory factory) {
    StringBuilder sb = new StringBuilder();
    for (XmppIdentity identity : getIdentities(context, jid, factory)) {
        sb.append(identity.getCategory());
        sb.append('/');
        sb.append(identity.getType());
        sb.append('/');
        sb.append(identity.getLang());
        sb.append('/');
        sb.append(identity.getName());
        sb.append('<');
    }
    for (String feature : getFeatures(context, jid, factory)) {
        sb.append(feature);
        sb.append('<');
    }
    Log.d("XMPP/DISCO", "Feature string for " + jid + ": " + sb.toString());
    try {
        MessageDigest sha1 = MessageDigest.getInstance("SHA-1");
        byte[] digest = sha1.digest(sb.toString().getBytes());
        String base64 = Base64.encodeToString(digest, Base64.NO_WRAP);
        return base64.trim();
    } catch (NoSuchAlgorithmException e) {
        // This should never happen.
        Log.e("AsmackService", "Please report (impossible condition)", e);
    }
    return sb.toString();
}
Also used : XmppIdentity(com.googlecode.asmack.XmppIdentity) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Example 3 with XmppIdentity

use of com.googlecode.asmack.XmppIdentity in project AsmackService by rtreffer.

the class Database method getIdentities.

/**
     * Retrieve a list of all identities on a given connection.
     * @param context The current context.
     * @param jid The user account jid.
     * @param factory A cursor factory (may be null).
     * @return
     */
public static synchronized XmppIdentity[] getIdentities(Context context, String jid, CursorFactory factory) {
    ArrayList<XmppIdentity> identities = new ArrayList<XmppIdentity>();
    SQLiteDatabase database = getDatabase(context, factory);
    Cursor result = database.query(true, "identity", new String[] { "category", "type", "lang", "name" }, "jid=? OR (jid IS NULL)", new String[] { jid }, null, null, "category ASC, type ASC, lang ASC, name ASC", null);
    if (result.getCount() > 0) {
        result.moveToFirst();
        int categoryIndex = result.getColumnIndex("category");
        int typeIndex = result.getColumnIndex("type");
        int langIndex = result.getColumnIndex("lang");
        int nameIndex = result.getColumnIndex("name");
        do {
            identities.add(new XmppIdentity(result.getString(categoryIndex), result.getString(typeIndex), result.getString(langIndex), result.getString(nameIndex)));
            result.moveToNext();
        } while (!result.isAfterLast());
    }
    result.close();
    XmppIdentity[] output = new XmppIdentity[identities.size()];
    return identities.toArray(output);
}
Also used : SQLiteDatabase(android.database.sqlite.SQLiteDatabase) XmppIdentity(com.googlecode.asmack.XmppIdentity) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor)

Aggregations

XmppIdentity (com.googlecode.asmack.XmppIdentity)3 Intent (android.content.Intent)1 Cursor (android.database.Cursor)1 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)1 Attribute (com.googlecode.asmack.Attribute)1 Stanza (com.googlecode.asmack.Stanza)1 XmppMalformedException (com.googlecode.asmack.XmppMalformedException)1 MessageDigest (java.security.MessageDigest)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 ArrayList (java.util.ArrayList)1 Node (org.w3c.dom.Node)1