151:0178573216e3
Anton Shestakov <av6@dwimlabs.net>, Thu, 14 Apr 2016 02:05:24 +0800
index: remove offline contacts only when they change to being offline Since new contacts are currently added while they still have the default presence of 'unavailable', add event sees them as offline and throws them away immediately, before their presence could change to something else.

next change 178:226f648724e2
previous change 148:42a539ea9b97

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()
unregister: ->
iq = $iq(type: 'set').c('query', xmlns: Strophe.NS.REGISTER).c('remove')
@conn.send(iq.tree())
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')
@trigger('status', status)
generateVersionString: ->
ver = ''
ids = (i for i in @conn.disco._identities)
ids.sort (a, b) ->
if a.category > b.category
return 1
if a.category < b.category
return -1
if a.type > b.type
return 1
if a.type < b.type
return -1
if a.lang > b.lang
return 1
if a.lang < b.lang
return -1
return 0
features = (f for f in @conn.disco._features)
features.sort()
for id in ids
ver += "#{ id.category }/#{ id.type }/#{ id.lang ? '' }/#{ id.name }<"
for f in features
ver += "#{ f }<"
return b64_sha1(ver)
sendPresence: (attrs) ->
ver = @generateVersionString()
pres = $pres().c('c', xmlns: Strophe.NS.CAPS, hash: 'sha-1', node: Tram.info.url, ver: ver).up()
if attrs.priority?
pres.c('priority').t(attrs.priority).up()
if attrs.show?
pres.c('show').t(attrs.show).up()
if attrs.status?
pres.c('status').t(attrs.status).up()
@conn.send(pres.tree())
_(Tram.XMPPInterface.prototype).extend(Backbone.Events)