Commit fa1dcf6b00ae291849289e41a0c2efab83aa02ce
1 parent
e756cfc78d
Exists in
master
unsubscribe
Showing 1 changed file with 7 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 onUnsubscribe(sender) { | ||
59 | xmpp.acceptUnsubscription(sender); | ||
60 | } | ||
61 | |||
58 | function onBuddy(jid, state, statusText, resource) { | 62 | function onBuddy(jid, state, statusText, resource) { |
59 | logger.verbose('Buddy state change', {jid: jid, state: state, statusText: statusText, resource: resource}); | 63 | logger.verbose('Buddy state change', {jid: jid, state: state, statusText: statusText, resource: resource}); |
60 | 64 | ||
61 | if (!matrix) { | 65 | if (!matrix) { |
62 | return; | 66 | return; |
63 | } | 67 | } |
64 | 68 | ||
65 | if (!matrix.buddies) { | 69 | if (!matrix.buddies) { |
66 | matrix.buddies = {}; | 70 | matrix.buddies = {}; |
67 | } | 71 | } |
68 | 72 | ||
69 | if (!matrix.buddies[jid]) { | 73 | if (!matrix.buddies[jid]) { |
70 | matrix.buddies[jid] = {resources: {}}; | 74 | matrix.buddies[jid] = {resources: {}}; |
71 | } | 75 | } |
72 | 76 | ||
73 | matrix.buddies[jid].resources[resource] = { | 77 | matrix.buddies[jid].resources[resource] = { |
74 | state: state, | 78 | state: state, |
75 | statusText: statusText, | 79 | statusText: statusText, |
76 | lastUpdate: moment().format('YYYY-MM-DD HH:mm:ss') | 80 | lastUpdate: moment().format('YYYY-MM-DD HH:mm:ss') |
77 | } | 81 | } |
78 | } | 82 | } |
79 | 83 | ||
80 | function isPartnerOffline(partner) { | 84 | function isPartnerOffline(partner) { |
81 | if (!matrix) { return false; } | 85 | if (!matrix) { return false; } |
82 | 86 | ||
83 | if (!matrix.buddies[partner]) { return false; } | 87 | if (!matrix.buddies[partner]) { return false; } |
84 | if (!matrix.buddies[partner].resources) { return false; }; | 88 | if (!matrix.buddies[partner].resources) { return false; }; |
85 | 89 | ||
86 | var resources = matrix.buddies[partner].resources; | 90 | var resources = matrix.buddies[partner].resources; |
87 | for (var key in resources) { | 91 | for (var key in resources) { |
88 | if (resources.hasOwnProperty(key)) { | 92 | if (resources.hasOwnProperty(key)) { |
89 | var resource = resources[key]; | 93 | var resource = resources[key]; |
90 | if (resources[key].state == 'online') { | 94 | if (resources[key].state == 'online') { |
91 | return false; | 95 | return false; |
92 | } | 96 | } |
93 | } | 97 | } |
94 | } | 98 | } |
95 | logger.verbose('Offline partner detected: ' + partner); | 99 | logger.verbose('Offline partner detected: ' + partner); |
96 | return true; | 100 | return true; |
97 | } | 101 | } |
98 | 102 | ||
99 | function init(_username, _password, _logger, _callbacks) { | 103 | function init(_username, _password, _logger, _callbacks) { |
100 | username = _username; | 104 | username = _username; |
101 | password = _password; | 105 | password = _password; |
102 | logger = _logger; | 106 | logger = _logger; |
103 | callbacks = _callbacks; | 107 | callbacks = _callbacks; |
104 | 108 | ||
105 | xmpp.on('online', onOnline); | 109 | xmpp.on('online', onOnline); |
106 | xmpp.on('chat', onPM); | 110 | xmpp.on('chat', onPM); |
107 | xmpp.on('error', onError); | 111 | xmpp.on('error', onError); |
108 | xmpp.on('subscribe', onSubscribe); | 112 | xmpp.on('subscribe', onSubscribe); |
113 | xmpp.on('unsubscribe', onUnsubscribe); | ||
109 | xmpp.on('buddy', onBuddy); | 114 | xmpp.on('buddy', onBuddy); |
110 | 115 | ||
116 | xmpp.unsubscribe(); | ||
117 | |||
111 | xmpp.connect({ | 118 | xmpp.connect({ |
112 | jid: username, | 119 | jid: username, |
113 | password: password | 120 | password: password |
114 | }); | 121 | }); |
115 | } | 122 | } |
116 | 123 | ||
117 | function setOptions(options) { | 124 | function setOptions(options) { |
118 | if (options.matrix) { | 125 | if (options.matrix) { |
119 | matrix = options.matrix; | 126 | matrix = options.matrix; |
120 | } | 127 | } |
121 | } | 128 | } |
122 | 129 | ||
123 | function sendMessage(destination, msg) { | 130 | function sendMessage(destination, msg) { |
124 | if (!destination) { | 131 | if (!destination) { |
125 | logger.warn('adaptorXmpp.sendMessage: Undefined destination, send message aborted', {destination: destination, msg: msg}); | 132 | logger.warn('adaptorXmpp.sendMessage: Undefined destination, send message aborted', {destination: destination, msg: msg}); |
126 | } | 133 | } |
127 | 134 | ||
128 | if (!msg) { | 135 | if (!msg) { |
129 | logger.warn('adaptorXmpp.sendMessage: Undefined message, send message aborted', {destination: destination, msg: msg}); | 136 | logger.warn('adaptorXmpp.sendMessage: Undefined message, send message aborted', {destination: destination, msg: msg}); |
130 | } | 137 | } |
131 | 138 | ||
132 | 139 | ||
133 | if (destination.toLowerCase() != username.replace(/\/.*/, '').toLowerCase()) { | 140 | if (destination.toLowerCase() != username.replace(/\/.*/, '').toLowerCase()) { |
134 | logger.verbose('Sending message', {from: username, destination: destination, msg: msg}); | 141 | logger.verbose('Sending message', {from: username, destination: destination, msg: msg}); |
135 | } | 142 | } |
136 | 143 | ||
137 | xmpp.send(destination, msg); | 144 | xmpp.send(destination, msg); |
138 | 145 | ||
139 | if (!matrix) { | 146 | if (!matrix) { |
140 | return; | 147 | return; |
141 | } | 148 | } |
142 | 149 | ||
143 | if (!matrix.buddies) { | 150 | if (!matrix.buddies) { |
144 | matrix.buddies = {}; | 151 | matrix.buddies = {}; |
145 | } | 152 | } |
146 | 153 | ||
147 | if (!matrix.buddies[destination]) { | 154 | if (!matrix.buddies[destination]) { |
148 | matrix.buddies[destination] = {}; | 155 | matrix.buddies[destination] = {}; |
149 | } | 156 | } |
150 | 157 | ||
151 | matrix.buddies[destination].lastOutgoing = { | 158 | matrix.buddies[destination].lastOutgoing = { |
152 | msg: msg, | 159 | msg: msg, |
153 | lastUpdate: moment().format('YYYY-MM-DD HH:mm:ss') | 160 | lastUpdate: moment().format('YYYY-MM-DD HH:mm:ss') |
154 | } | 161 | } |
155 | } | 162 | } |
156 | 163 | ||
157 | function addFriend(friend) { | 164 | function addFriend(friend) { |
158 | xmpp.subscribe(friend); | 165 | xmpp.subscribe(friend); |
159 | } | 166 | } |
160 | 167 | ||
161 | exports.init = init; | 168 | exports.init = init; |
162 | exports.sendMessage = sendMessage; | 169 | exports.sendMessage = sendMessage; |
163 | exports.setOptions = setOptions; | 170 | exports.setOptions = setOptions; |
164 | exports.addFriend = addFriend; | 171 | exports.addFriend = addFriend; |
165 | exports.isPartnerOffline = isPartnerOffline; | 172 | exports.isPartnerOffline = isPartnerOffline; |
166 | 173 |