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