Commit ddffb312fc3601adf800e357410e2bcb6c34240a

Authored by Adhidarma Hadiwinoto
1 parent 70b9ea66ba
Exists in master

tambah teman jika bukan ketika cek offline

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