Anton Shestakov <av6@dwimlabs.net>, Mon, 21 Mar 2016 21:40:30 +0800
index: send presence on connect, handle incoming presences, show contacts
coffee/index.coffee
Permissions: -rw-r--r--
window.contacts ?= new Tram.Contacts() window.contactsApp ?= new Tram.ContactsApp(el: $('[data-app="contacts"]'), collection: contacts) window.messages ?= new Tram.Messages() window.logApp ?= new Tram.LogApp(el: $('[data-app="log"]'), collection: messages) X.conn.addHandler(onPresence, null, 'presence') X.conn.send($pres().c('priority').t('1').up().c('status').t('Online').tree()) if el.childNodes.length is 0 and el.nodeType is Strophe.ElementType.TEXT for node in el.childNodes if node.nodeType is Strophe.ElementType.TEXT iq = $iq(type: 'get', id: X.conn.getUniqueId('version'), to: Tram.config.domain).c('query', xmlns: Strophe.NS.VERSION) name = getText stanza.getElementsByTagName('name')[0] version = getText stanza.getElementsByTagName('version')[0] os = getText stanza.getElementsByTagName('os')[0] delay = stanza.getElementsByTagName('delay')[0] stamp = if delay then new Date(delay.getAttribute('stamp')) else new Date() id: stanza.getAttribute('id') type: stanza.getAttribute('type') from: stanza.getAttribute('from') text: "connected to #{ name } #{ version } on #{ os }" console.error("couldn't get version", stanza?.innerHTML) X.conn.sendIQ(iq.tree(), okcb, failcb, 5000) contact = contacts.get(from) self = from is X.conn.jid type: if self then 'self' else 'contact' getContactProfile(contact) getContactProfile = (contact) -> vcard = stanza.getElementsByTagName('vCard')[0] console.warn("no vcard in response", stanza) nickname: getText(vcard.querySelector('NICKNAME')) fullname: getText(vcard.querySelector('FN')) firstname: getText(vcard.querySelector('N > GIVEN')) lastname: getText(vcard.querySelector('N > FAMILY')) console.warn("couldn't get vcard", stanza) bjid = Strophe.getBareJidFromJid(contact.get('jid')) if bjid is Strophe.getBareJidFromJid(X.conn.jid) X.conn.vcard.get(okcb, bjid, failcb) from = stanza.getAttribute('from') type = stanza.getAttribute('type') ? 'online' console.warn('not handling <presence type="subscribe">', stanza) console.warn('not handling <presence type="subscribed">', stanza) console.error('got <presence type="error">', stanza) delay = stanza.getElementsByTagName('delay')[0] stamp = if delay then new Date(delay.getAttribute('stamp')) else new Date() priority = stanza.getElementsByTagName('priority')[0] priority = if priority then getText(priority) else '0' show = stanza.getElementsByTagName('show')[0] show = if show then getText(show) else undefined s = stanza.getElementsByTagName('status')[0] s = if s then getText(s) else undefined contact = getContact(from) id: stanza.getAttribute('id') text: 'is now ' + (show ? type) window.X = new Tram.XMPPInterface() $('[data-msg^="a/"]').addClass('uk-hidden') $('[data-msg="a/fail"]').removeClass('uk-hidden') $('[data-step="login"]').removeClass('uk-hidden') $('[data-step="main"]').addClass('uk-hidden') X.on 'connected attached', -> $('[data-step="login"]').addClass('uk-hidden') $('[data-step="main"]').removeClass('uk-hidden') $('button[data-disconnect]').on 'click', -> X.disconnect('Logged out') $(window).on 'beforeunload unload', -> X.disconnect('Window closed') $form = $('[data-form="connect"]') $form.find('input').on 'keydown', (e) -> if (not @required or @value isnt '') and e.keyCode is 13 index = $form.find('input').index(@) $next = $form.find('input').eq(index + 1) $form.find('button').trigger('click') $form.find('button').on 'click', (e) -> Tram.validation.unsetError($('#username, #password')) ok &= Tram.validation.validateRequired($('#username'), true) ok &= Tram.validation.validateRequired($('#password')) X.connect($('#username').val().trim(), $('#password').val())