X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=jappixmini%2Flib.js;h=31302732874ea9c6bf186c4e981442f34ec2cd57;hb=d9330e3b05c71bbd172293fb223271033d3b19f7;hp=bbbc7e34e008b057baa8879912789c1283985968;hpb=0164bdf11e1dd8ca2b89955a1c390057858e3f8e;p=friendica-addons.git diff --git a/jappixmini/lib.js b/jappixmini/lib.js index bbbc7e34..31302732 100644 --- a/jappixmini/lib.js +++ b/jappixmini/lib.js @@ -1,9 +1,15 @@ +// +// Copyright 2012 "Leberwurscht" +// +// This file is dual-licensed under the MIT license (see MIT.txt) and the AGPL license (see jappix/COPYING). +// + function jappixmini_addon_xor(str1, str2) { if (str1.length != str2.length) throw "not same length"; - encoded = ""; + var encoded = ""; - for (i=0; iRetype your Friendica password for chatting:
'); + div = $('
Retype your Friendica password for chatting:
'); - input = $('') + var input = $('') div.append(input); - button = $(''); + var button = $(''); div.append(button); $("body").append(div); } button.click(function(){ - password = $("#jappixmini-password-query-input").val(); + var password = $("#jappixmini-password-query-input").val(); jappixmini_addon_set_client_secret(password); div.remove(); - client_secret = getPersistent('jappix-mini', 'client-secret'); + var client_secret = getPersistent('jappix-mini', 'client-secret'); callback(client_secret); }); } @@ -68,7 +74,7 @@ function jappixmini_addon_encrypt_password(password, callback) { } // xor password with secret - encrypted_password = jappixmini_addon_xor(client_secret, password); + var encrypted_password = jappixmini_addon_xor(client_secret, password); encrypted_password = encodeURI(encrypted_password) callback(encrypted_password); @@ -80,10 +86,10 @@ function jappixmini_addon_decrypt_password(encrypted_password, callback) { jappixmini_addon_get_client_secret(function(client_secret){ // xor password with secret - password = jappixmini_addon_xor(client_secret, encrypted_password); + var password = jappixmini_addon_xor(client_secret, encrypted_password); // remove \0 - first_null = password.indexOf("\0") + var first_null = password.indexOf("\0") if (first_null==-1) throw "Decrypted password does not contain \\0"; password = password.substr(0, first_null); @@ -91,7 +97,7 @@ function jappixmini_addon_decrypt_password(encrypted_password, callback) { }); } -function jappixmini_manage_roster(contacts, autoapprove, autosubscribe) { +function jappixmini_manage_roster(contacts, contacts_hash, autoapprove, autosubscribe) { // listen for subscriptions con.registerHandler('presence',function(presence){ var type = presence.getType(); @@ -101,6 +107,8 @@ function jappixmini_manage_roster(contacts, autoapprove, autosubscribe) { var xid = bareXID(from); var pstatus = presence.getStatus(); + var approve; + if (autoapprove && contacts[xid]!==undefined) { // approve known address approve = true; @@ -117,20 +125,32 @@ function jappixmini_manage_roster(contacts, autoapprove, autosubscribe) { } else { // In all other cases, ask the user. - message = "Accept "+xid+" for chat?"; + var message = "Accept "+xid+" for chat?"; if (pstatus) message += "\n\nStatus:\n"+pstatus; approve = confirm(message); + + // do not ask any more + if (!approve) sendSubscribe(xid, "unsubscribed"); } if (approve) { - acceptSubscribe(xid, contacts[xid]); - console.log("Accepted "+xid+" for chat."); + var name = contacts[xid]; + if (!name) name = xid; + + acceptSubscribe(xid, name); + console.log("Accepted "+xid+" ("+name+") for chat."); } }); // autosubscribe 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); @@ -140,30 +160,35 @@ function jappixmini_manage_roster(contacts, autoapprove, autosubscribe) { // 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"); + var node = $(this); + var xid = node.attr("jid"); + var name = node.attr("name"); + var subscription = node.attr("subscription"); - // ignore accounts not in the list + // ignore accounts that are not in the list if (contacts[xid]===undefined) return; - // add to Friendica group if necessary - groups = []; - $(this).find('group').each(function() { + // 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 (group_text) groups.push(group_text); }); - if ($.inArray("Friendica", groups)==-1) { - console.log("Add "+xid+" to Friendica group."); + group_missing = true; groups.push("Friendica"); - sendRoster(xid, null, null, groups); - console.log("Added "+xid+" to Friendica group."); + } + + 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 @@ -182,7 +207,7 @@ function jappixmini_manage_roster(contacts, autoapprove, autosubscribe) { presence.setStatus("I'm "+MINI_NICKNAME+" from ~Friendica.\n[machine-generated message]"); con.send(presence); - console.log("subscribed to "+xid); + console.log("Subscribed to "+xid+" automatically."); // add to roster var iq = new JSJaCIQ(); @@ -192,9 +217,13 @@ function jappixmini_manage_roster(contacts, autoapprove, autosubscribe) { item.setAttribute('name', contacts[xid]); item.appendChild(iq.buildNode('group', {'xmlns': NS_ROSTER}, "Friendica")); con.send(iq); - console.log("added to roster: "+xid); + console.log("Added "+xid+" ("+contacts[xid]+") to roster."); } + + setPersistent("jappix-mini", "contacts-hash", contacts_hash); + console.log("Autosubscribe done."); }); + } function jappixmini_addon_subscribe() { @@ -203,40 +232,45 @@ function jappixmini_addon_subscribe() { return; } - xid = prompt("Jabber address"); + var xid = prompt("Jabber address"); sendSubscribe(xid, "subscribe"); } -function jappixmini_addon_start(server, username, proxy, bosh, encrypted, password, nickname, contacts, autoapprove, autosubscribe) { - handler = function(password){ +function jappixmini_addon_start(server, username, proxy, bosh, encrypted, password, nickname, contacts, contacts_hash, autoapprove, autosubscribe, groupchats) { + var handler = function(password){ // check if settings have changed, reinitialize jappix mini if this is the case - settings_identifier = str_sha1(server); + var settings_identifier = str_sha1(server); settings_identifier += str_sha1(username); settings_identifier += str_sha1(proxy); settings_identifier += str_sha1(bosh); settings_identifier += str_sha1(password); settings_identifier += str_sha1(nickname); - saved_identifier = getDB("jappix-mini", "settings_identifier"); - if (saved_identifier != settings_identifier) removeDB('jappix-mini', 'dom'); - setDB("jappix-mini", "settings_identifier", settings_identifier); + var saved_identifier = getDB("jappix-mini", "settings-identifier"); + if (saved_identifier != settings_identifier) { + disconnectMini(); + removeDB('jappix-mini', 'dom'); + removePersistent("jappix-mini", "contacts-hash"); + } + setDB("jappix-mini", "settings-identifier", settings_identifier); // set HOST_BOSH - if (proxy) - HOST_BOSH = proxy+"?host_bosh="+encodeURI(bosh); - else - HOST_BOSH = bosh; + if (proxy) + HOST_BOSH = proxy+"?host_bosh="+encodeURI(bosh); + else + HOST_BOSH = bosh; // start jappix mini + MINI_GROUPCHATS = groupchats; MINI_NICKNAME = nickname; LOCK_HOST = "off"; launchMini(true, false, server, username, password); - // increase priority over other Jabber clients - priority = 101; - sendPresence(null,null,priority); + // increase priority over other Jabber clients - does not seem to work? + var priority = 101; + presenceMini(null,null,priority); - jappixmini_manage_roster(contacts, autoapprove, autosubscribe) + jappixmini_manage_roster(contacts, contacts_hash, autoapprove, autosubscribe) } // decrypt password if necessary