39:28ad6d3e2618
Anton Shestakov <av6@dwimlabs.net>, Wed, 23 Mar 2016 16:55:52 +0800
index: maintain only one contact with type 'self' This isn't done in an event handler of contacts collection because doing it in 'add' handler would trigger 'sort' event after the model has been added, but before its 'add' event has propagated, and that's dumb.

next change 117:477f7f7cce15
previous change 22:dba8124befc3

coffee/xmpp.coffee

Permissions: -rw-r--r--

Other formats: Feeds:
class Tram.XMPPInterface
conn: null
constructor: ->
_prepareConnection: ->
if @conn?
console.debug('connection exists, not reconnecting')
@conn = new Strophe.Connection("https://#{ Tram.config.host }/http-bind")
connect: (node, pass) ->
@_prepareConnection()
jid = "#{ node }@#{ Tram.config.domain }"
@conn.connect(jid, pass, @onConnect)
disconnect: (reason) ->
if not @conn?
return
@conn.disconnect(reason)
@conn = null
return
startRegistration: ->
@_prepareConnection()
@conn.register.connect(Tram.config.domain, @onConnect)
register: (username, password) ->
@conn.register.fields.username = username
@conn.register.fields.password = password
@conn.register.submit()
finishRegistration: ->
@conn.authenticate()
savevCard: (data, okcb, failcb) ->
$vcard = $iq(type: 'set').c('vCard', xmlns: Strophe.NS.VCARD)
if data.nickname
$vcard.c('NICKNAME').t(data.nickname).up()
if data.fullname
$vcard.c('FN').t(data.fullname).up()
if data.avatar
b = ';base64,'
d = 'data:'
dl = d.length
bl = b.length
bi = data.avatar.indexOf(b)
type = data.avatar.substr(dl, bi - dl)
binval = data.avatar.substr(bi + bl)
$vcard.c('PHOTO')
.c('TYPE').t(type).up()
.c('BINVAL').t(binval)
@conn.sendIQ($vcard.tree(), okcb, failcb)
onConnect: (status, error) =>
switch status
when Strophe.Status.CONNECTING
console.debug('Strophe is connecting.')
@trigger('connecting')
when Strophe.Status.AUTHENTICATING
console.debug('Strophe is authenticating.')
@trigger('authenticating')
when Strophe.Status.AUTHFAIL
console.debug('Strophe failed to authenticate:', error)
@trigger('authfail')
when Strophe.Status.ERROR
console.debug('Strophe received an error:', error)
@trigger('error')
when Strophe.Status.CONNFAIL
console.debug('Strophe failed to connect:', error)
@trigger('connfail')
when Strophe.Status.DISCONNECTING
console.debug('Strophe is disconnecting.')
@trigger('disconnecting')
when Strophe.Status.DISCONNECTED
console.debug('Strophe is disconnected.')
@trigger('disconnected')
when Strophe.Status.CONNECTED
console.debug('Strophe is connected.')
console.info('My jid:', @conn.jid)
@trigger('connected')
when Strophe.Status.ATTACHED
console.debug('Strophe is attached.')
console.info('My jid:', @conn.jid)
@trigger('attached')
when Strophe.Status.REGISTER
console.debug('Got registration prompt.')
@trigger('register')
when Strophe.Status.REGISTERED
console.debug('Registered!')
@trigger('registered')
when Strophe.Status.CONFLICT
console.debug('Contact already exists!')
@trigger('conflict')
when Strophe.Status.NOTACCEPTABLE
console.debug('Registration form not properly filled out.')
@trigger('notacceptable')
when Strophe.Status.REGIFAIL
console.debug('The server does not support In-Band Registration.')
@trigger('regifail')
_(Tram.XMPPInterface.prototype).extend(Backbone.Events)