+ if (!autosubscribe) return;
+
+ var stored_hash = getPersistent("jappix-mini", "contacts-hash");
+ var contacts_changed = (stored_hash != contacts_hash); // stored_hash gets updated later if everything was successful
+ if (!contacts_changed) return;
+
+ console.log("Start autosubscribe.");
+
+ 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() {
+ var node = $(this);
+ var xid = node.attr("jid");
+ var name = node.attr("name");
+ var subscription = node.attr("subscription");
+
+ // ignore accounts that are not in the list
+ if (contacts[xid]===undefined) return;
+
+ // add to Friendica group or change name if necessary
+ var groups = [];
+ var group_missing = false;
+ node.find('group').each(function() {
+ var group_text = $(this).text();
+ if (group_text) groups.push(group_text);
+ });
+ if ($.inArray("Friendica", groups)==-1) {
+ group_missing = true;
+ groups.push("Friendica");
+ }
+
+ if (group_missing || name!=contacts[xid]) {
+ sendRoster(xid, null, contacts[xid], groups);
+ console.log("Added "+xid+" to Friendica group and set name to "+contacts[xid]+".");
+ }
+
+ // authorize if necessary
+ if (subscription=="to") {
+ sendSubscribe(xid, 'subscribed');
+ console.log("Authorized "+xid+" automatically.");
+ }
+
+ // 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");
+
+ // must contain the word "~Friendica" so the other side knows
+ // how to handle this
+ presence.setStatus("I'm "+MINI_NICKNAME+" from ~Friendica.\n[machine-generated message]");
+
+ con.send(presence);
+ console.log("Subscribed to "+xid+" automatically.");
+
+ // 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 "+xid+" ("+contacts[xid]+") to roster.");