1 function jappixmini_addon_xor(str1, str2) {
2 if (str1.length != str2.length) throw "not same length";
6 for (i=0; i<str1.length;i++) {
7 var a = str1.charCodeAt(i);
8 var b = str2.charCodeAt(i);
11 encoded += String.fromCharCode(c);
17 function jappixmini_addon_set_client_secret(password) {
18 if (!password) return;
20 salt1 = "h8doCRekWto0njyQohKpdx6BN0UTyC6N";
21 salt2 = "jdX8OwFC1kWAq3s9uOyAcE8g3UNNO5t3";
23 client_secret1 = str_sha1(salt1+password);
24 client_secret2 = str_sha1(salt2+password);
25 client_secret = client_secret1 + client_secret2;
27 setPersistent('jappix-mini', 'client-secret', client_secret);
28 console.log("client secret set");
31 function jappixmini_addon_get_client_secret(callback) {
32 client_secret = getPersistent('jappix-mini', 'client-secret');
33 if (client_secret===null) {
34 div = document.getElementById("#jappixmini-password-query-div");
37 div = $('<div id="jappixmini-password-query-div" style="position:fixed;padding:1em;background-color:#F00;color:#fff;top:50px;left:50px;">Retype your Friendica password for chatting:<br></div>');
39 input = $('<input type="password" id="jappixmini-password-query-input">')
42 button = $('<input type="button" value="OK" id="jappixmini-password-query-button">');
45 $("body").append(div);
48 button.click(function(){
49 password = $("#jappixmini-password-query-input").val();
50 jappixmini_addon_set_client_secret(password);
53 client_secret = getPersistent('jappix-mini', 'client-secret');
54 callback(client_secret);
58 callback(client_secret);
62 function jappixmini_addon_encrypt_password(password, callback) {
63 jappixmini_addon_get_client_secret(function(client_secret){
64 // add \0 to password until it has the same length as secret
65 if (password.length>client_secret.length-1) throw "password too long";
66 while (password.length<client_secret.length) {
70 // xor password with secret
71 encrypted_password = jappixmini_addon_xor(client_secret, password);
73 encrypted_password = encodeURI(encrypted_password)
74 callback(encrypted_password);
78 function jappixmini_addon_decrypt_password(encrypted_password, callback) {
79 encrypted_password = decodeURI(encrypted_password);
81 jappixmini_addon_get_client_secret(function(client_secret){
82 // xor password with secret
83 password = jappixmini_addon_xor(client_secret, encrypted_password);
86 first_null = password.indexOf("\0")
87 if (first_null==-1) throw "Decrypted password does not contain \\0";
88 password = password.substr(0, first_null);
94 function jappixmini_manage_roster(contacts, autoapprove, autosubscribe) {
95 // listen for subscriptions
96 con.registerHandler('presence',function(presence){
97 var type = presence.getType();
98 if (type != "subscribe") return;
100 var from = fullXID(getStanzaFrom(presence));
101 var xid = bareXID(from);
102 var pstatus = presence.getStatus();
104 if (autoapprove && contacts[xid]!==undefined) {
105 // approve known address
107 console.log("Approve known Friendica contact "+xid+".");
109 else if (autoapprove && pstatus && pstatus.indexOf("Friendica")!=-1) {
110 // Unknown address claims to be a Friendica contact.
111 // This is probably because the other side knows our
112 // address, but we do not know the other side yet.
113 // But it's only a matter of time, so wait - do not
114 // approve yet and do not annoy the user by asking.
116 console.log("Do not approve unknown Friendica contact "+xid+" - wait instead.");
119 // In all other cases, ask the user.
120 message = "Accept "+xid+" for chat?";
121 if (pstatus) message += "\n\nStatus:\n"+pstatus;
122 approve = confirm(message);
124 // do not ask any more
125 if (!approve) sendSubscribe(xid, "unsubscribed");
129 name = contacts[xid];
130 if (!name) name = xid;
132 acceptSubscribe(xid, contacts[xid]);
133 console.log("Accepted "+xid+" for chat.");
138 if (!autosubscribe) return;
140 var get_roster = new JSJaCIQ();
141 get_roster.setType('get');
142 get_roster.setQuery(NS_ROSTER);
144 con.send(get_roster, function(iq){
145 var handleXML = iq.getQuery();
147 // filter out contacts that are already in the roster
148 $(handleXML).find('item').each(function() {
149 xid = $(this).attr("jid");
150 name = $(this).attr("name");
151 subscription = $(this).attr("subscription");
153 // ignore accounts not in the list
154 if (contacts[xid]===undefined) return;
156 // add to Friendica group if necessary
158 $(this).find('group').each(function() {
159 var group_text = $(this).text();
160 if(group_text) groups.push(group_text);
163 if ($.inArray("Friendica", groups)==-1) {
164 console.log("Add "+xid+" to Friendica group.");
165 groups.push("Friendica");
166 sendRoster(xid, null, null, groups);
167 console.log("Added "+xid+" to Friendica group.");
170 // authorize if necessary
171 if (subscription=="to") {
172 sendSubscribe(xid, 'subscribed');
176 delete contacts[xid];
179 // go through remaining contacts
180 for (var xid in contacts) {if(!contacts.hasOwnProperty(xid)) continue;
182 var presence = new JSJaCPresence();
184 presence.setType("subscribe");
186 // must contain the word "~Friendica" so the other side knows
187 // how to handle this
188 presence.setStatus("I'm "+MINI_NICKNAME+" from ~Friendica.\n[machine-generated message]");
191 console.log("subscribed to "+xid);
194 var iq = new JSJaCIQ();
196 var iqQuery = iq.setQuery(NS_ROSTER);
197 var item = iqQuery.appendChild(iq.buildNode('item', {'xmlns': NS_ROSTER, 'jid': xid}));
198 item.setAttribute('name', contacts[xid]);
199 item.appendChild(iq.buildNode('group', {'xmlns': NS_ROSTER}, "Friendica"));
201 console.log("added to roster: "+xid);
206 function jappixmini_addon_subscribe() {
208 alert("Not connected.");
212 xid = prompt("Jabber address");
213 sendSubscribe(xid, "subscribe");
216 function jappixmini_addon_start(server, username, proxy, bosh, encrypted, password, nickname, contacts, autoapprove, autosubscribe) {
217 handler = function(password){
218 // check if settings have changed, reinitialize jappix mini if this is the case
219 settings_identifier = str_sha1(server);
220 settings_identifier += str_sha1(username);
221 settings_identifier += str_sha1(proxy);
222 settings_identifier += str_sha1(bosh);
223 settings_identifier += str_sha1(password);
224 settings_identifier += str_sha1(nickname);
226 saved_identifier = getDB("jappix-mini", "settings_identifier");
227 if (saved_identifier != settings_identifier) removeDB('jappix-mini', 'dom');
228 setDB("jappix-mini", "settings_identifier", settings_identifier);
232 HOST_BOSH = proxy+"?host_bosh="+encodeURI(bosh);
237 MINI_NICKNAME = nickname;
239 launchMini(true, false, server, username, password);
241 // increase priority over other Jabber clients - does not seem to work?
243 presenceMini(null,null,priority);
245 jappixmini_manage_roster(contacts, autoapprove, autosubscribe)
248 // decrypt password if necessary
250 jappixmini_addon_decrypt_password(password, handler);