Commit 63d74f8e014f491df3d9ff8e26c39b7c0bb3561e

Authored by Adhidarma Hadiwinoto
1 parent ad53929ef6
Exists in master

add friend if not friend

Showing 1 changed file with 15 additions and 2 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; } 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)) {
90 addFriend(partner);
91 }
92
89 if (!matrix.buddies[partner]) { return false; } 93 if (!matrix.buddies[partner]) { return false; }
90 if (!matrix.buddies[partner].resources) { return false; }; 94 if (!matrix.buddies[partner].resources) { return false; };
91 95
92 var resources = matrix.buddies[partner].resources; 96 var resources = matrix.buddies[partner].resources;
93 for (var key in resources) { 97 for (var key in resources) {
94 if (resources.hasOwnProperty(key)) { 98 if (resources.hasOwnProperty(key)) {
95 var resource = resources[key]; 99 var resource = resources[key];
96 if (resources[key].state == 'online') { 100 if (resources[key].state == 'online') {
97 return false; 101 return false;
98 } 102 }
99 } 103 }
100 } 104 }
101 logger.verbose('Offline partner detected: ' + partner); 105 logger.verbose('Offline partner detected: ' + partner);
102 return true; 106 return true;
103 } 107 }
104 108
105 function init(_username, _password, _logger, _callbacks) { 109 function init(_username, _password, _logger, _callbacks) {
106 username = _username; 110 username = _username;
107 password = _password; 111 password = _password;
108 logger = _logger; 112 logger = _logger;
109 callbacks = _callbacks; 113 callbacks = _callbacks;
110 114
111 xmpp.on('online', onOnline); 115 xmpp.on('online', onOnline);
112 xmpp.on('chat', onPM); 116 xmpp.on('chat', onPM);
113 xmpp.on('error', onError); 117 xmpp.on('error', onError);
114 xmpp.on('subscribe', onSubscribe); 118 xmpp.on('subscribe', onSubscribe);
115 xmpp.on('unsubscribe', onUnsubscribe); 119 xmpp.on('unsubscribe', onUnsubscribe);
116 xmpp.on('buddy', onBuddy); 120 xmpp.on('buddy', onBuddy);
117 121
118 xmpp.unsubscribe(); 122 xmpp.unsubscribe();
119 123
120 xmpp.connect({ 124 xmpp.connect({
121 jid: username, 125 jid: username,
122 password: password 126 password: password
123 }); 127 });
124 } 128 }
125 129
126 function setOptions(options) { 130 function setOptions(options) {
127 if (options.matrix) { 131 if (options.matrix) {
128 matrix = options.matrix; 132 matrix = options.matrix;
129 } 133 }
130 } 134 }
131 135
132 function sendMessage(destination, msg) { 136 function sendMessage(destination, msg) {
133 if (!destination) { 137 if (!destination) {
134 logger.warn('adaptorXmpp.sendMessage: Undefined destination, send message aborted', {destination: destination, msg: msg}); 138 logger.warn('adaptorXmpp.sendMessage: Undefined destination, send message aborted', {destination: destination, msg: msg});
135 } 139 }
136 140
137 if (!msg) { 141 if (!msg) {
138 logger.warn('adaptorXmpp.sendMessage: Undefined message, send message aborted', {destination: destination, msg: msg}); 142 logger.warn('adaptorXmpp.sendMessage: Undefined message, send message aborted', {destination: destination, msg: msg});
139 } 143 }
140 144
141 145
142 if (destination.toLowerCase() != username.replace(/\/.*/, '').toLowerCase()) { 146 if (destination.toLowerCase() != username.replace(/\/.*/, '').toLowerCase()) {
143 logger.verbose('Sending message', {from: username, destination: destination, msg: msg}); 147 logger.verbose('Sending message', {from: username, destination: destination, msg: msg});
144 } 148 }
145 149
146 xmpp.send(destination, msg); 150 xmpp.send(destination, msg);
147 151
148 if (!matrix) { 152 if (!matrix) {
149 return; 153 return;
150 } 154 }
151 155
152 if (!matrix.buddies) { 156 if (!matrix.buddies) {
153 matrix.buddies = {}; 157 matrix.buddies = {};
154 } 158 }
155 159
156 if (!matrix.buddies[destination]) { 160 if (!matrix.buddies[destination]) {
157 matrix.buddies[destination] = {}; 161 matrix.buddies[destination] = {};
158 } 162 }
159 163
160 matrix.buddies[destination].lastOutgoing = { 164 matrix.buddies[destination].lastOutgoing = {
161 msg: msg, 165 msg: msg,
162 lastUpdate: moment().format('YYYY-MM-DD HH:mm:ss') 166 lastUpdate: moment().format('YYYY-MM-DD HH:mm:ss')
163 } 167 }
164 } 168 }
165 169
166 function addFriend(friend) { 170 function addFriend(friend) {
167 xmpp.subscribe(friend); 171 xmpp.subscribe(friend);
172 }
173
174 function isAFriend(jid) {
175 if (!matrix) { return false; };
176 if (!matrix.buddies) { return false; }
177 if (!matrix.buddies[jid]) { return false; }
178
179 return true;
168 } 180 }
169 181
170 exports.init = init; 182 exports.init = init;
171 exports.sendMessage = sendMessage; 183 exports.sendMessage = sendMessage;
172 exports.setOptions = setOptions; 184 exports.setOptions = setOptions;
173 exports.addFriend = addFriend; 185 exports.addFriend = addFriend;
174 exports.isPartnerOffline = isPartnerOffline; 186 exports.isPartnerOffline = isPartnerOffline;
187 exports.isAFriend = isAFriend;
175 188