18:be40f584efc1
Anton Shestakov <av6@dwimlabs.net>, Sun, 20 Mar 2016 23:30:16 +0800
register: improve scripting, move XMPP interface to a separate file

next change 20:aa1eeed14fff

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, onConnect) ->
@_prepareConnection()
jid = "#{ node }@#{ Tram.config.domain }"
@conn.connect(jid, pass, onConnect)
disconnect: (reason) ->
if not @conn?
return
@conn.disconnect(reason)
@conn = null
return
startRegistration: (onConnect) ->
@_prepareConnection()
@conn.register.connect(Tram.config.domain, onConnect)
register: (username, password) ->
@conn.register.fields.username = username
@conn.register.fields.password = password
@conn.register.submit()
savevCard: (data) ->
$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)
okcb = =>
console.debug('vcard is set!', arguments)
@trigger('v/done')
failcb = =>
console.warn('vcard is NOT set!', arguments)
@trigger('v/failed')
@conn.sendIQ($vcard.tree(), okcb, failcb)
_(Tram.XMPPInterface.prototype).extend(Backbone.Events)