Commit 0a0dd5297c596578497d9880dbae583f322e7a28

Authored by Adhidarma Hadiwinoto
1 parent 637ced97f0
Exists in master

_cleanPartnerId

Showing 1 changed file with 29 additions and 19 deletions Inline Diff

1 'use strict'; 1 'use strict';
2 2
3 var moment = require('moment'); 3 var moment = require('moment');
4 4
5 module.exports = MatrixUtil; 5 module.exports = MatrixUtil;
6 6
7 function _cleanPartnerId(partnerId) {
8 let cleaned = partnerId;
9
10 try {
11 cleaned = cleaned.toLocaleString();
12 cleaned = cleaned.trim().toLowerCase();
13 } catch(e) {
14 return partnerId;
15 }
16
17 return cleaned;
18 }
19
7 function MatrixUtil(options) { 20 function MatrixUtil(options) {
8 if (!options) { 21 if (!options) {
9 console.trace('Undefined options'); 22 console.trace('Undefined options');
10 process.exit(1); 23 process.exit(1);
11 } 24 }
12 25
13 this.matrix = options.matrix; 26 this.matrix = options.matrix;
14 if (!this.matrix) { 27 if (!this.matrix) {
15 console.trace("Matrix not set"); 28 console.trace("Matrix not set");
16 process.exit(1); 29 process.exit(1);
17 } 30 }
18 31
19 this.logger = options.logger; 32 this.logger = options.logger;
20 if (!this.logger) { 33 if (!this.logger) {
21 console.trace("Logger not set"); 34 console.trace("Logger not set");
22 process.exit(1); 35 process.exit(1);
23 } 36 }
24 } 37 }
25 38
26 MatrixUtil.prototype.updateBuddyState = function(jid, state, statusText, resource) { 39 MatrixUtil.prototype.updateBuddyState = function(jid, state, statusText, resource) {
27 if (!jid) {return; } 40 if (!jid) {return; }
28 if (jid == 'undefined') {return; } 41 if (jid == 'undefined') {return; }
29 42
30 try { 43 jid = _cleanPartnerId(jid);
31 jid = jid.toLowerCase();
32 jid = jid.toLocaleString();
33 } catch(e) { }
34 44
35 if (!resource) { 45 if (!resource) {
36 resource = 'undefined'; 46 resource = 'undefined';
37 } 47 }
38 48
39 let logger = this.logger; 49 let logger = this.logger;
40 let matrix = this.matrix; 50 let matrix = this.matrix;
41 51
42 logger.verbose('Buddy state change', {jid: jid, state: state, statusText: statusText, resource: resource}); 52 logger.verbose('Buddy state change', {jid: jid, state: state, statusText: statusText, resource: resource});
43 53
44 if (!matrix) { 54 if (!matrix) {
45 return; 55 return;
46 } 56 }
47 57
48 if (!matrix.buddies) { 58 if (!matrix.buddies) {
49 matrix.buddies = {}; 59 matrix.buddies = {};
50 } 60 }
51 61
52 if (!matrix.buddies[jid]) { 62 if (!matrix.buddies[jid]) {
53 matrix.buddies[jid] = {resources: {}}; 63 matrix.buddies[jid] = {resources: {}};
54 } 64 }
55 65
56 try { 66 try {
57 matrix.buddies[jid]['resources'][resource] = { 67 matrix.buddies[jid]['resources'][resource] = {
58 state: state, 68 state: state,
59 statusText: statusText, 69 statusText: statusText,
60 lastUpdate: moment().format('YYYY-MM-DD HH:mm:ss') 70 lastUpdate: moment().format('YYYY-MM-DD HH:mm:ss')
61 } 71 }
62 } 72 }
63 catch(e) { 73 catch(e) {
64 logger.warn('MatrixUtil: Exception on update resources on matrix', {jid: jid, state: state, statusText: statusText, resource: resource}); 74 logger.warn('MatrixUtil: Exception on update resources on matrix', {jid: jid, state: state, statusText: statusText, resource: resource});
65 } 75 }
66 76
67
68
69 if (resource != 'undefined' && matrix.buddies[jid].resources['undefined']) { 77 if (resource != 'undefined' && matrix.buddies[jid].resources['undefined']) {
70 try { 78 try {
71 delete matrix.buddies[jid].resources['undefined']; 79 delete matrix.buddies[jid].resources['undefined'];
72 } 80 }
73 catch(e) {}; 81 catch(e) {};
74 } 82 }
75 } 83 }
76 84
77 MatrixUtil.prototype.isAFriend = function(jid) { 85 MatrixUtil.prototype.isAFriend = function(jid) {
78 if (!jid) { return; } 86 if (!jid) { return; }
79 jid = jid.toLowerCase(); 87
88 jid = _cleanPartnerId(jid);
80 89
81 let matrix = this.matrix; 90 let matrix = this.matrix;
82 91
83 if (!matrix) { return false; }; 92 if (!matrix) { return false; };
84 if (!matrix.buddies) { return false; } 93 if (!matrix.buddies) { return false; }
85 if (!matrix.buddies[jid]) { return false; } 94 if (!matrix.buddies[jid]) { return false; }
86 95
87 return true; 96 return true;
88 } 97 }
89 98
90 MatrixUtil.prototype.isPartnerOffline = function(partner) { 99 MatrixUtil.prototype.isPartnerOffline = function(partner) {
91 if (!partner) { return; } 100 if (!partner) { return; }
92 partner = partner.toLowerCase(); 101
102 partner = _cleanPartnerId(partner);
93 103
94 let matrix = this.matrix; 104 let matrix = this.matrix;
95 let logger = this.logger; 105 let logger = this.logger;
96 106
97 if (!matrix) { return false; } 107 if (!matrix) { return false; }
98 108
99 if (!matrix.buddies[partner]) { return false; } 109 if (!matrix.buddies[partner]) { return false; }
100 if (!matrix.buddies[partner].resources) { return false; }; 110 if (!matrix.buddies[partner].resources) { return false; };
101 111
102 let resources = matrix.buddies[partner].resources; 112 let resources = matrix.buddies[partner].resources;
103 for (let key in resources) { 113 for (let key in resources) {
104 if (resources.hasOwnProperty(key)) { 114 if (resources.hasOwnProperty(key)) {
105 let resource = resources[key]; 115 let resource = resources[key];
106 if (resources[key].state == 'online') { 116 if (resources[key].state == 'online') {
107 return false; 117 return false;
108 } 118 }
109 } 119 }
110 } 120 }
111 logger.verbose('Offline partner detected: ' + partner); 121 logger.verbose('Offline partner detected: ' + partner);
112 return true; 122 return true;
113 } 123 }
114 124
115 MatrixUtil.prototype.updateLastIncoming = function(sender, msg) { 125 MatrixUtil.prototype.updateLastIncoming = function(partner, msg) {
116 if (!sender) { return; } 126 if (!partner) { return; }
117 sender = sender.toLowerCase(); 127 partner = _cleanPartnerId(partner);
118 128
119 let matrix = this.matrix; 129 let matrix = this.matrix;
120 130
121 if (!matrix) { 131 if (!matrix) {
122 return; 132 return;
123 } 133 }
124 134
125 if (!matrix.buddies) { 135 if (!matrix.buddies) {
126 matrix.buddies = {}; 136 matrix.buddies = {};
127 } 137 }
128 138
129 if (!matrix.buddies[sender]) { 139 if (!matrix.buddies[partner]) {
130 matrix.buddies[sender] = {}; 140 matrix.buddies[partner] = {};
131 } 141 }
132 142
133 matrix.buddies[sender].lastIncoming = { 143 matrix.buddies[partner].lastIncoming = {
134 msg: msg, 144 msg: msg,
135 lastUpdate: moment().format('YYYY-MM-DD HH:mm:ss') 145 lastUpdate: moment().format('YYYY-MM-DD HH:mm:ss')
136 } 146 }
137 } 147 }
138 148
139 MatrixUtil.prototype.updateLastOutgoing = function(destination, msg) { 149 MatrixUtil.prototype.updateLastOutgoing = function(partner, msg) {
140 if (!destination) { return; } 150 if (!partner) { return; }
141 destination = destination.toLowerCase(); 151 partner = _cleanPartnerId(partner);
142 152
143 let matrix = this.matrix; 153 let matrix = this.matrix;
144 154
145 if (!matrix) { 155 if (!matrix) {
146 return; 156 return;
147 } 157 }
148 158
149 if (!matrix.buddies) { 159 if (!matrix.buddies) {
150 matrix.buddies = {}; 160 matrix.buddies = {};
151 } 161 }
152 162
153 if (!matrix.buddies[destination]) { 163 if (!matrix.buddies[partner]) {
154 matrix.buddies[destination] = {}; 164 matrix.buddies[partner] = {};
155 } 165 }
156 166
157 matrix.buddies[destination].lastOutgoing = { 167 matrix.buddies[destination].lastOutgoing = {