Commit 70b9ea66ba642c0b33ceeddbe2419152b9f0879d
1 parent
be363af3c0
Exists in
master
coba matrix-util
Showing 2 changed files with 64 additions and 30 deletions Side-by-side Diff
adaptor-xmpp.js
1 | 1 | var xmpp = require('simple-xmpp'); |
2 | 2 | var moment = require('moment'); |
3 | +var MatrixUtil = require('./matrix-util'); | |
3 | 4 | |
4 | 5 | var username; |
5 | 6 | var password; |
6 | 7 | |
7 | 8 | var callbacks; |
8 | 9 | var matrix; |
10 | +var matrixUtil; | |
9 | 11 | |
10 | 12 | function onOnline(data) { |
11 | 13 | logger.info('XMPP login successful', {data: data}); |
... | ... | @@ -60,34 +62,7 @@ function onUnsubscribe(sender) { |
60 | 62 | } |
61 | 63 | |
62 | 64 | function onBuddy(jid, state, statusText, resource) { |
63 | - if (jid == 'undefined') {return; } | |
64 | - | |
65 | - logger.verbose('Buddy state change', {jid: jid, state: state, statusText: statusText, resource: resource}); | |
66 | - | |
67 | - if (!matrix) { | |
68 | - return; | |
69 | - } | |
70 | - | |
71 | - if (!matrix.buddies) { | |
72 | - matrix.buddies = {}; | |
73 | - } | |
74 | - | |
75 | - if (!matrix.buddies[jid]) { | |
76 | - matrix.buddies[jid] = {resources: {}}; | |
77 | - } | |
78 | - | |
79 | - matrix.buddies[jid].resources[resource] = { | |
80 | - state: state, | |
81 | - statusText: statusText, | |
82 | - lastUpdate: moment().format('YYYY-MM-DD HH:mm:ss') | |
83 | - } | |
84 | - | |
85 | - if (resource != 'undefined' && matrix.buddies[jid].resources.undefined) { | |
86 | - try { | |
87 | - delete matrix.buddies[jid].resources.undefined; | |
88 | - } | |
89 | - catch(e) {}; | |
90 | - } | |
65 | + matrixUtil.updateBuddyState(jid, state, statusText, resource); | |
91 | 66 | } |
92 | 67 | |
93 | 68 | function isPartnerOffline(partner) { |
... | ... | @@ -136,9 +111,12 @@ function init(_username, _password, _logger, _callbacks) { |
136 | 111 | } |
137 | 112 | |
138 | 113 | function setOptions(options) { |
139 | - if (options.matrix) { | |
140 | - matrix = options.matrix; | |
114 | + if (!options.matrix) { | |
115 | + return; | |
141 | 116 | } |
117 | + | |
118 | + matrix = options.matrix; | |
119 | + matrixUtil = new MatrixUtil({matrix: matrix, logger: logger}); | |
142 | 120 | } |
143 | 121 | |
144 | 122 | function sendMessage(destination, msg) { |
matrix-util.js
... | ... | @@ -0,0 +1,56 @@ |
1 | +var moment = require('moment'); | |
2 | + | |
3 | +module.exports = MatrixUtil; | |
4 | + | |
5 | +function MatrixUtil(options) { | |
6 | + if (!options) { | |
7 | + console.trace('Undefined options'); | |
8 | + process.exit(1); | |
9 | + } | |
10 | + | |
11 | + this.matrix = options.matrix; | |
12 | + if (!this.matrix) { | |
13 | + console.trace("Matrix not set"); | |
14 | + process.exit(1); | |
15 | + } | |
16 | + | |
17 | + this.logger = options.logger; | |
18 | + if (!this.logger) { | |
19 | + console.trace("Logger not set"); | |
20 | + process.exit(1); | |
21 | + } | |
22 | +} | |
23 | + | |
24 | +MatrixUtil.prototype.updateBuddyState = function(jid, state, statusText, resource) { | |
25 | + if (jid == 'undefined') {return; } | |
26 | + | |
27 | + var logger = this.logger; | |
28 | + var matrix = this.matrix; | |
29 | + | |
30 | + logger.verbose('Buddy state change', {jid: jid, state: state, statusText: statusText, resource: resource}); | |
31 | + | |
32 | + if (!matrix) { | |
33 | + return; | |
34 | + } | |
35 | + | |
36 | + if (!matrix.buddies) { | |
37 | + matrix.buddies = {}; | |
38 | + } | |
39 | + | |
40 | + if (!matrix.buddies[jid]) { | |
41 | + matrix.buddies[jid] = {resources: {}}; | |
42 | + } | |
43 | + | |
44 | + matrix.buddies[jid].resources[resource] = { | |
45 | + state: state, | |
46 | + statusText: statusText, | |
47 | + lastUpdate: moment().format('YYYY-MM-DD HH:mm:ss') | |
48 | + } | |
49 | + | |
50 | + if (resource != 'undefined' && matrix.buddies[jid].resources.undefined) { | |
51 | + try { | |
52 | + delete matrix.buddies[jid].resources.undefined; | |
53 | + } | |
54 | + catch(e) {}; | |
55 | + } | |
56 | +} |