]> git.mxchange.org Git - friendica-addons.git/blobdiff - jappixmini/lib.js
jappixmini: increase client priority
[friendica-addons.git] / jappixmini / lib.js
index 351d03f97717a61508e6b0593fc4af79262d3617..d02f5d5c348727aa63678d4caa268df2093938fe 100644 (file)
@@ -24,12 +24,12 @@ function jappixmini_addon_set_client_secret(password) {
        client_secret2 = str_sha1(salt2+password);
        client_secret = client_secret1 + client_secret2;
 
-       setDB('jappix-mini', 'client-secret', client_secret);
+       setPersistent('jappix-mini', 'client-secret', client_secret);
        console.log("client secret set");
 }
 
 function jappixmini_addon_get_client_secret(callback) {
-       client_secret = getDB('jappix-mini', 'client-secret');
+       client_secret = getPersistent('jappix-mini', 'client-secret');
        if (client_secret===null) {
                div = document.getElementById("#jappixmini-password-query-div");
 
@@ -50,7 +50,7 @@ function jappixmini_addon_get_client_secret(callback) {
                        jappixmini_addon_set_client_secret(password);
                        div.remove();
 
-                       client_secret = getDB('jappix-mini', 'client-secret');
+                       client_secret = getPersistent('jappix-mini', 'client-secret');
                        callback(client_secret);
                });
        }
@@ -111,13 +111,53 @@ function jappixmini_manage_roster(contacts, autoapprove, autosubscribe) {
        });
 
        // autosubscribe
-       if (autosubscribe) {
-               for (i=0; i<contacts.length; i++) {
-                       xid = contacts[i];
-                       sendSubscribe(xid, "subscribe");
-                       console.log("Subscribed to "+xid);
+       if (!autosubscribe) return;
+
+       var get_roster = new JSJaCIQ();
+       get_roster.setType('get');
+       get_roster.setQuery(NS_ROSTER);
+
+       con.send(get_roster, function(iq){
+               var handleXML = iq.getQuery();
+
+               // filter out contacts that are already in the roster
+               $(handleXML).find('item').each(function() {
+                       xid = $(this).attr("jid");
+                       name = $(this).attr("name");
+                       subscription = $(this).attr("subscription");
+                       console.log(xid+" "+subscription);
+
+                       // ignore accounts not in the list
+                       if (contacts[xid]===undefined) return;
+
+                       // TODO: add to Friendica group
+
+                       // TODO: unblock and authorize if necessary
+
+                       // remove from list
+                       delete contacts[xid];
+               });
+
+               // go through remaining contacts
+               for (var xid in contacts) {if(!contacts.hasOwnProperty(xid)) continue;
+                       // subscribe
+                       var presence = new JSJaCPresence();
+                       presence.setTo(xid);
+                       presence.setType("subscribe");
+                       con.send(presence);
+                       console.log("subscribed to "+xid);
+
+                       // add to roster
+                       var iq = new JSJaCIQ();
+                       iq.setType('set');
+                       var iqQuery = iq.setQuery(NS_ROSTER);
+                       var item = iqQuery.appendChild(iq.buildNode('item', {'xmlns': NS_ROSTER, 'jid': xid}));
+                       item.setAttribute('name', contacts[xid]);
+                       item.appendChild(iq.buildNode('group', {'xmlns': NS_ROSTER}, "Friendica"));
+                       con.send(iq);
+                       console.log("added to roster "+xid);
                }
-       }
+       });
 }
 
 function jappixmini_addon_subscribe() {
@@ -130,7 +170,7 @@ function jappixmini_addon_subscribe() {
        sendSubscribe(xid, "subscribe");
 }
 
-function jappixmini_addon_start(server, username, bosh, encrypted, password, nickname, contacts, autoapprove, autosubscribe) {
+function jappixmini_addon_start(server, username, proxy, bosh, encrypted, password, nickname, contacts, autoapprove, autosubscribe) {
     handler = function(password){
         // check if settings have changed, reinitialize jappix mini if this is the case
         settings_identifier = str_sha1(server);
@@ -143,15 +183,23 @@ function jappixmini_addon_start(server, username, bosh, encrypted, password, nic
         if (saved_identifier != settings_identifier) removeDB('jappix-mini', 'dom');
         setDB("jappix-mini", "settings_identifier", settings_identifier);
 
-        // set bosh host
-        if (bosh)
-            HOST_BOSH = HOST_BOSH+"?host_bosh="+encodeURI(bosh);
+        // set HOST_BOSH
+       if (proxy)
+               HOST_BOSH = proxy+"?host_bosh="+encodeURI(bosh);
+       else
+               HOST_BOSH = bosh;
 
         // start jappix mini
         MINI_NICKNAME = nickname;
+        LOCK_HOST = "off";
         console.log("launchMini");
         launchMini(true, false, server, username, password);
-       jappixmini_manage_roster(contacts, autoapprove, autosubscribe)
+
+        // increase priority over other Jabber clients
+        priority = 101;
+        sendPresence(null,null,priority);
+
+        jappixmini_manage_roster(contacts, autoapprove, autosubscribe)
     }
 
     // decrypt password if necessary