Commit 70b9ea66ba642c0b33ceeddbe2419152b9f0879d
1 parent
be363af3c0
Exists in
master
coba matrix-util
Showing 2 changed files with 64 additions and 30 deletions Inline Diff
adaptor-xmpp.js
1 | var xmpp = require('simple-xmpp'); | 1 | var xmpp = require('simple-xmpp'); |
2 | var moment = require('moment'); | 2 | var moment = require('moment'); |
3 | var MatrixUtil = require('./matrix-util'); | ||
3 | 4 | ||
4 | var username; | 5 | var username; |
5 | var password; | 6 | var password; |
6 | 7 | ||
7 | var callbacks; | 8 | var callbacks; |
8 | var matrix; | 9 | var matrix; |
10 | var matrixUtil; | ||
9 | 11 | ||
10 | function onOnline(data) { | 12 | function onOnline(data) { |
11 | logger.info('XMPP login successful', {data: data}); | 13 | logger.info('XMPP login successful', {data: data}); |
12 | 14 | ||
13 | xmpp.getRoster(); | 15 | xmpp.getRoster(); |
14 | 16 | ||
15 | if (callbacks.onOnline) { | 17 | if (callbacks.onOnline) { |
16 | callbacks.onOnline(); | 18 | callbacks.onOnline(); |
17 | } | 19 | } |
18 | } | 20 | } |
19 | 21 | ||
20 | function onPM(sender, msg) { | 22 | function onPM(sender, msg) { |
21 | if (sender.toLowerCase() == username.replace(/\/.*/, '').toLowerCase()) { | 23 | if (sender.toLowerCase() == username.replace(/\/.*/, '').toLowerCase()) { |
22 | return; | 24 | return; |
23 | } | 25 | } |
24 | 26 | ||
25 | logger.verbose('Got a message', {from: sender, msg: msg}); | 27 | logger.verbose('Got a message', {from: sender, msg: msg}); |
26 | 28 | ||
27 | if (callbacks.onPM) { | 29 | if (callbacks.onPM) { |
28 | callbacks.onPM(sender, msg); | 30 | callbacks.onPM(sender, msg); |
29 | } | 31 | } |
30 | 32 | ||
31 | if (!matrix) { | 33 | if (!matrix) { |
32 | return; | 34 | return; |
33 | } | 35 | } |
34 | 36 | ||
35 | if (!matrix.buddies) { | 37 | if (!matrix.buddies) { |
36 | matrix.buddies = {}; | 38 | matrix.buddies = {}; |
37 | } | 39 | } |
38 | 40 | ||
39 | if (!matrix.buddies[sender]) { | 41 | if (!matrix.buddies[sender]) { |
40 | matrix.buddies[sender] = {}; | 42 | matrix.buddies[sender] = {}; |
41 | } | 43 | } |
42 | 44 | ||
43 | matrix.buddies[sender].lastIncoming = { | 45 | matrix.buddies[sender].lastIncoming = { |
44 | msg: msg, | 46 | msg: msg, |
45 | lastUpdate: moment().format('YYYY-MM-DD HH:mm:ss') | 47 | lastUpdate: moment().format('YYYY-MM-DD HH:mm:ss') |
46 | } | 48 | } |
47 | } | 49 | } |
48 | 50 | ||
49 | function onError(err) { | 51 | function onError(err) { |
50 | logger.warn('XMPP error, terminating in 3 secs', {err: err}); | 52 | logger.warn('XMPP error, terminating in 3 secs', {err: err}); |
51 | setTimeout(process.exit, 3000, 1); | 53 | setTimeout(process.exit, 3000, 1); |
52 | } | 54 | } |
53 | 55 | ||
54 | function onSubscribe(sender) { | 56 | function onSubscribe(sender) { |
55 | xmpp.acceptSubscription(sender); | 57 | xmpp.acceptSubscription(sender); |
56 | } | 58 | } |
57 | 59 | ||
58 | function onUnsubscribe(sender) { | 60 | function onUnsubscribe(sender) { |
59 | xmpp.acceptUnsubscription(sender); | 61 | xmpp.acceptUnsubscription(sender); |
60 | } | 62 | } |
61 | 63 | ||
62 | function onBuddy(jid, state, statusText, resource) { | 64 | function onBuddy(jid, state, statusText, resource) { |
63 | if (jid == 'undefined') {return; } | 65 | matrixUtil.updateBuddyState(jid, state, statusText, resource); |
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 | } | ||
91 | } | 66 | } |
92 | 67 | ||
93 | function isPartnerOffline(partner) { | 68 | function isPartnerOffline(partner) { |
94 | if (!matrix) { return false; } | 69 | if (!matrix) { return false; } |
95 | 70 | ||
96 | if (!isAFriend(partner)) { | 71 | if (!isAFriend(partner)) { |
97 | logger.verbose(partner + ' is not a friend yet, adding it as a friend') | 72 | logger.verbose(partner + ' is not a friend yet, adding it as a friend') |
98 | addFriend(partner); | 73 | addFriend(partner); |
99 | } | 74 | } |
100 | 75 | ||
101 | if (!matrix.buddies[partner]) { return false; } | 76 | if (!matrix.buddies[partner]) { return false; } |
102 | if (!matrix.buddies[partner].resources) { return false; }; | 77 | if (!matrix.buddies[partner].resources) { return false; }; |
103 | 78 | ||
104 | var resources = matrix.buddies[partner].resources; | 79 | var resources = matrix.buddies[partner].resources; |
105 | for (var key in resources) { | 80 | for (var key in resources) { |
106 | if (resources.hasOwnProperty(key)) { | 81 | if (resources.hasOwnProperty(key)) { |
107 | var resource = resources[key]; | 82 | var resource = resources[key]; |
108 | if (resources[key].state == 'online') { | 83 | if (resources[key].state == 'online') { |
109 | return false; | 84 | return false; |
110 | } | 85 | } |
111 | } | 86 | } |
112 | } | 87 | } |
113 | logger.verbose('Offline partner detected: ' + partner); | 88 | logger.verbose('Offline partner detected: ' + partner); |
114 | return true; | 89 | return true; |
115 | } | 90 | } |
116 | 91 | ||
117 | function init(_username, _password, _logger, _callbacks) { | 92 | function init(_username, _password, _logger, _callbacks) { |
118 | username = _username; | 93 | username = _username; |
119 | password = _password; | 94 | password = _password; |
120 | logger = _logger; | 95 | logger = _logger; |
121 | callbacks = _callbacks; | 96 | callbacks = _callbacks; |
122 | 97 | ||
123 | xmpp.on('online', onOnline); | 98 | xmpp.on('online', onOnline); |
124 | xmpp.on('chat', onPM); | 99 | xmpp.on('chat', onPM); |
125 | xmpp.on('error', onError); | 100 | xmpp.on('error', onError); |
126 | xmpp.on('subscribe', onSubscribe); | 101 | xmpp.on('subscribe', onSubscribe); |
127 | xmpp.on('unsubscribe', onUnsubscribe); | 102 | xmpp.on('unsubscribe', onUnsubscribe); |
128 | xmpp.on('buddy', onBuddy); | 103 | xmpp.on('buddy', onBuddy); |
129 | 104 | ||
130 | xmpp.unsubscribe(); | 105 | xmpp.unsubscribe(); |
131 | 106 | ||
132 | xmpp.connect({ | 107 | xmpp.connect({ |
133 | jid: username, | 108 | jid: username, |
134 | password: password | 109 | password: password |
135 | }); | 110 | }); |
136 | } | 111 | } |
137 | 112 | ||
138 | function setOptions(options) { | 113 | function setOptions(options) { |
139 | if (options.matrix) { | 114 | if (!options.matrix) { |
140 | matrix = options.matrix; | 115 | return; |
141 | } | 116 | } |
117 | |||
118 | matrix = options.matrix; | ||
119 | matrixUtil = new MatrixUtil({matrix: matrix, logger: logger}); | ||
142 | } | 120 | } |
143 | 121 | ||
144 | function sendMessage(destination, msg) { | 122 | function sendMessage(destination, msg) { |
145 | if (!destination) { | 123 | if (!destination) { |
146 | logger.warn('adaptorXmpp.sendMessage: Undefined destination, send message aborted', {destination: destination, msg: msg}); | 124 | logger.warn('adaptorXmpp.sendMessage: Undefined destination, send message aborted', {destination: destination, msg: msg}); |
147 | } | 125 | } |
148 | 126 | ||
149 | if (!msg) { | 127 | if (!msg) { |
150 | logger.warn('adaptorXmpp.sendMessage: Undefined message, send message aborted', {destination: destination, msg: msg}); | 128 | logger.warn('adaptorXmpp.sendMessage: Undefined message, send message aborted', {destination: destination, msg: msg}); |
151 | } | 129 | } |
152 | 130 | ||
153 | 131 | ||
154 | if (destination.toLowerCase() != username.replace(/\/.*/, '').toLowerCase()) { | 132 | if (destination.toLowerCase() != username.replace(/\/.*/, '').toLowerCase()) { |
155 | logger.verbose('Sending message', {from: username, destination: destination, msg: msg}); | 133 | logger.verbose('Sending message', {from: username, destination: destination, msg: msg}); |
156 | } | 134 | } |
157 | 135 | ||
158 | xmpp.send(destination, msg); | 136 | xmpp.send(destination, msg); |
159 | 137 | ||
160 | if (!matrix) { | 138 | if (!matrix) { |
161 | return; | 139 | return; |
162 | } | 140 | } |
163 | 141 | ||
164 | if (!matrix.buddies) { | 142 | if (!matrix.buddies) { |
165 | matrix.buddies = {}; | 143 | matrix.buddies = {}; |
166 | } | 144 | } |
167 | 145 | ||
168 | if (!matrix.buddies[destination]) { | 146 | if (!matrix.buddies[destination]) { |
169 | matrix.buddies[destination] = {}; | 147 | matrix.buddies[destination] = {}; |
170 | } | 148 | } |
171 | 149 | ||
172 | matrix.buddies[destination].lastOutgoing = { | 150 | matrix.buddies[destination].lastOutgoing = { |
173 | msg: msg, | 151 | msg: msg, |
174 | lastUpdate: moment().format('YYYY-MM-DD HH:mm:ss') | 152 | lastUpdate: moment().format('YYYY-MM-DD HH:mm:ss') |
175 | } | 153 | } |
176 | } | 154 | } |
177 | 155 | ||
178 | function addFriend(friend) { | 156 | function addFriend(friend) { |
179 | logger.verbose('Adding XMP friend: ' + friend); | 157 | logger.verbose('Adding XMP friend: ' + friend); |
180 | xmpp.subscribe(friend); | 158 | xmpp.subscribe(friend); |
181 | } | 159 | } |
182 | 160 | ||
183 | function isAFriend(jid) { | 161 | function isAFriend(jid) { |
184 | if (!matrix) { return false; }; | 162 | if (!matrix) { return false; }; |
185 | if (!matrix.buddies) { return false; } | 163 | if (!matrix.buddies) { return false; } |
186 | if (!matrix.buddies[jid]) { return false; } | 164 | if (!matrix.buddies[jid]) { return false; } |
187 | 165 | ||
188 | return true; | 166 | return true; |
189 | } | 167 | } |
190 | 168 | ||
191 | exports.init = init; | 169 | exports.init = init; |
192 | exports.sendMessage = sendMessage; | 170 | exports.sendMessage = sendMessage; |
matrix-util.js
File was created | 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 | } | ||
57 |