Commit ad53929ef6ea275ee6d3e2a49fed9a7ecbda9271

Authored by Adhidarma Hadiwinoto
1 parent 5c1b4f6e88
Exists in master

abaikan jid == 'undefined'

Showing 1 changed file with 3 additions and 1 deletions Inline Diff

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