Commit c3f61306aa6a2347ce05f0cea19891916a6901e0

Authored by Adhidarma Hadiwinoto
1 parent ddffb312fc
Exists in master

migrate matrix to matrix-util

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