<commit_before>angular.module('jabber.services', [])
.factory('XmppSvc', function() {
  var client = null,
      status = 'offline',
      account = {},
      roster = [],
      listeners = {};

  var handleResult = function(stanza) {
    console.log(stanza.attrs.id, stanza.attrs.id == 'roster_0')
    if(stanza.attrs.id == 'roster_0') {
      // We've received the list of contacts

      var contacts = stanza.getChild('query').getChildren('item');
      for(var i=0, len = contacts.length; i<len; i++) {
        roster.push({nick: contacts[i].attrs.name, jid: contacts[i].attrs.jid, status: 'offline'});
      }
      if(listeners.contactsReady) listeners.contactsReady(roster);
    }
  }

  var handlePresence = function(stanza) {
    if(stanza.attrs.type == 'subscribe') {
      if(listeners.subscriptionRequest) listeners.subscriptionRequest(stanza.attrs.from);
    }
  }

  var completeSubscriptionRequest = function(to, isAllowed) {
    client.send(new XMPP.Element('presence', {
      to: to,
      type: isAllowed ? 'subscribed' : 'unsubscribed'
    }))
  }

  var setupDefaultListeners = function(client) {
    client.addListener(
        'online',
        function() {
            console.log('online');
            client.send(new XMPP.Element('presence'));
            status = 'online';
        }
    );

    client.addListener(
        'error',
        function(e) {
            console.error(e);
            alert('Error: ' + e);
            status = 'offline';
        }
    );

    client.on('stanza', function(stanza) {
      console.log('stanza:', stanza);
      if (stanza.is('message')) {
        //
      } else if(stanza.is('iq')) {
        handleResult(stanza);
      } else if(stanza.is('presence')) {
        handlePresence(stanza);
      }
    });
  };

  return {
    connect: function(acc) {
      client = new XMPP.Client({
          boshURL: acc.serverUrl,
          jid: acc.jid,
          password: <PASSWORD>,
          preferred: 'PLAIN' // BOSH only allows plain logins.
      });

      setupDefaultListeners(client);
      account = acc;

      return client;
    },
    addEventListener: function(event, listener) {
      listeners[event] = listener;
    },
    addConnectionListener: function(event, listener) {
      client.addListener(event, listener);
    },
    getStatus: function() { return status; },
    getContacts: function(callback) {
      var getRosterMsg = new XMPP.Element('iq', {
          id: 'roster_0',
          type: 'get'
        }).c('query', {
          xmlns: 'jabber:iq:roster'
        });
      client.send(getRosterMsg);
      listeners.contactsReady = callback;
    },
    getUserName: function() { return account.nickname || account.jid; },
    allowSubscription: function(to) { completeSubscriptionRequest(to, true ); },
    denySubscription:  function(to) { completeSubscriptionRequest(to, false); }
  }
})

.factory('storageSrv', function() {
    var storageKey = '';

    return {
        setKey: function(key) {
          storageKey = key;
        },
        get: function(key) {
            if (storageKey === '') { storageKey = key; }
            var item = localStorage.getItem(storageKey);
            var object;
            try {
                object = JSON.parse(item);
            }
            catch (exception) {
                console.warn(exception.message);
                console.warn(item);
                return {};
            }
            return object || {};
        },

        save: function(object, key) {<commit_msg>Allow handlePresence to announce presence change<commit_after>angular.module('jabber.services', [])
.factory('XmppSvc', function() {
  var client = null,
      status = 'offline',
      account = {},
      roster = [],
      listeners = {};

  var announcePresence = function(status) {
    var presenceStanza = new XMPP.Element('presence');
    // if status is undefined, we send the initial broadcast presence
    // if status is set, we are changing status...
    if(status) {
      presenceStanza.c('show').t(status);
    }
    client.send(presenceStanza);
  };

  var handleResult = function(stanza) {
    console.log(stanza.attrs.id, stanza.attrs.id == 'roster_0')
    if(stanza.attrs.id == 'roster_0') {
      // We've received the list of contacts
      var contacts = stanza.getChild('query').getChildren('item');
      for(var i=0, len = contacts.length; i<len; i++) {
        roster.push({nick: contacts[i].attrs.name, jid: contacts[i].attrs.jid, status: 'offline'});
      }
      if(listeners.contactsReady) listeners.contactsReady(roster);
      announcePresence();
    }
  }

  var handlePresence = function(stanza) {
    if(stanza.attrs.type == 'subscribe') {
      if(listeners.subscriptionRequest) listeners.subscriptionRequest(stanza.attrs.from);
    } else {
      var from = stanza.attrs.from.split('/')[0];
      for(var i=0, len = roster.length; i<len; i++) {
        if(roster[i].jid == from) {
          roster[i].status = stanza.getChildText('show') || 'online';
          if(listeners.contactPresenceChanged) listeners.contactPresenceChanged(roster[i], i);
          return;
        }
      }
    }
  }

  var completeSubscriptionRequest = function(to, isAllowed) {
    client.send(new XMPP.Element('presence', {
      to: to,
      type: isAllowed ? 'subscribed' : 'unsubscribed'
    }))
  }

  var setupDefaultListeners = function(client) {
    client.addListener(
        'online',
        function() {
            console.log('online');
            status = 'online';
        }
    );

    client.addListener(
        'error',
        function(e) {
            console.error(e);
            alert('Error: ' + e);
            status = 'offline';
        }
    );

    client.on('stanza', function(stanza) {
      console.log('stanza:', stanza);
      if (stanza.is('message')) {
        //
      } else if(stanza.is('iq')) {
        handleResult(stanza);
      } else if(stanza.is('presence')) {
        handlePresence(stanza);
      }
    });
  };

  return {
    connect: function(acc) {
      client = new XMPP.Client({
          boshURL: acc.serverUrl,
          jid: acc.jid,
          password: <PASSWORD>,
          preferred: 'PLAIN' // BOSH only allows plain logins.
      });

      setupDefaultListeners(client);
      account = acc;

      return client;
    },
    addEventListener: function(event, listener) {
      listeners[event] = listener;
    },
    addConnectionListener: function(event, listener) {
      client.addListener(event, listener);
    },
    getStatus: function() { return status; },
    getContacts: function(callback) {
      var getRosterMsg = new XMPP.Element('iq', {
          id: 'roster_0',
          type: 'get'
        }).c('query', {
          xmlns: 'jabber:iq:roster'
        });
      client.send(getRosterMsg);
      listeners.contactsReady = callback;
    },
    getUserName: function() { return account.nickname || account.jid; },
    allowSubscription: function(to) { completeSubscriptionRequest(to, true ); },
    denySubscription:  function(to) { completeSubscriptionRequest(to, false); },
    announcePresence: announcePresence
  };
})

.factory('storageSrv', function() {
    var storageKey = '';

    return {
        setKey: function(key) {
          storageKey = key;
        },
        get: function(key) {
            if (storageKey === '') { storageKey = key; }
            var item = localStorage.getItem(storageKey);
            var object;
            try {
                object = JSON.parse(item);
            }
            catch (exception) {
                console.warn(exception.message);
                console.warn(item);
                return {};
            }
            return object || {};
        },

        save: function(object, key) {
{'fraction_non_alphanumeric': 0.14272844272844273, 'fraction_numerical': 0.001287001287001287, 'mean_word_length': 2.1979423868312757, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1966709', 'n_tokens_mistral': 2338, 'n_tokens_neox': 2331, 'n_words': 437}