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