Commit 131ea90c8fe94a8b05c2c76dbc3c64d3785725e1

Authored by Adhidarma Hadiwinoto
1 parent fb48d75a33
Exists in master

jangan kirim pesan jika destination atau msg kosong

Showing 1 changed file with 9 additions and 0 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 onBuddy(jid, state, statusText, resource) { 58 function onBuddy(jid, state, statusText, resource) {
59 logger.verbose('Buddy state change', {jid: jid, state: state, statusText: statusText, resource: resource}); 59 logger.verbose('Buddy state change', {jid: jid, state: state, statusText: statusText, resource: resource});
60 60
61 if (!matrix) { 61 if (!matrix) {
62 return; 62 return;
63 } 63 }
64 64
65 if (!matrix.buddies) { 65 if (!matrix.buddies) {
66 matrix.buddies = {}; 66 matrix.buddies = {};
67 } 67 }
68 68
69 if (!matrix.buddies[jid]) { 69 if (!matrix.buddies[jid]) {
70 matrix.buddies[jid] = {states: {}}; 70 matrix.buddies[jid] = {states: {}};
71 } 71 }
72 72
73 matrix.buddies[jid].states[resource] = { 73 matrix.buddies[jid].states[resource] = {
74 state: state, 74 state: state,
75 statusText: statusText, 75 statusText: statusText,
76 lastUpdate: moment().format('YYYY-MM-DD HH:mm:ss') 76 lastUpdate: moment().format('YYYY-MM-DD HH:mm:ss')
77 } 77 }
78 } 78 }
79 79
80 function init(_username, _password, _logger, _callbacks) { 80 function init(_username, _password, _logger, _callbacks) {
81 username = _username; 81 username = _username;
82 password = _password; 82 password = _password;
83 logger = _logger; 83 logger = _logger;
84 callbacks = _callbacks; 84 callbacks = _callbacks;
85 85
86 xmpp.on('online', onOnline); 86 xmpp.on('online', onOnline);
87 xmpp.on('chat', onPM); 87 xmpp.on('chat', onPM);
88 xmpp.on('error', onError); 88 xmpp.on('error', onError);
89 xmpp.on('subscribe', onSubscribe); 89 xmpp.on('subscribe', onSubscribe);
90 xmpp.on('buddy', onBuddy); 90 xmpp.on('buddy', onBuddy);
91 91
92 xmpp.connect({ 92 xmpp.connect({
93 jid: username, 93 jid: username,
94 password: password 94 password: password
95 }); 95 });
96 } 96 }
97 97
98 function setOptions(options) { 98 function setOptions(options) {
99 if (options.matrix) { 99 if (options.matrix) {
100 matrix = options.matrix; 100 matrix = options.matrix;
101 } 101 }
102 } 102 }
103 103
104 function sendMessage(destination, msg) { 104 function sendMessage(destination, msg) {
105 if (!destination) {
106 logger.warn('adaptorXmpp.sendMessage: Undefined destination, send message aborted', {destination: destination, msg: msg});
107 }
108
109 if (!msg) {
110 logger.warn('adaptorXmpp.sendMessage: Undefined message, send message aborted', {destination: destination, msg: msg});
111 }
112
113
105 if (destination.toLowerCase() != username.replace(/\/.*/, '').toLowerCase()) { 114 if (destination.toLowerCase() != username.replace(/\/.*/, '').toLowerCase()) {
106 logger.verbose('Sending message', {from: username, destination: destination, msg: msg}); 115 logger.verbose('Sending message', {from: username, destination: destination, msg: msg});
107 } 116 }
108 117
109 xmpp.send(destination, msg); 118 xmpp.send(destination, msg);
110 119
111 if (!matrix) { 120 if (!matrix) {
112 return; 121 return;
113 } 122 }
114 123
115 if (!matrix.buddies) { 124 if (!matrix.buddies) {
116 matrix.buddies = {}; 125 matrix.buddies = {};
117 } 126 }
118 127
119 if (!matrix.buddies[destination]) { 128 if (!matrix.buddies[destination]) {
120 matrix.buddies[destination] = {}; 129 matrix.buddies[destination] = {};
121 } 130 }
122 131
123 matrix.buddies[destination].lastOutgoing = { 132 matrix.buddies[destination].lastOutgoing = {
124 msg: msg, 133 msg: msg,
125 lastUpdate: moment().format('YYYY-MM-DD HH:mm:ss') 134 lastUpdate: moment().format('YYYY-MM-DD HH:mm:ss')
126 } 135 }
127 } 136 }
128 137
129 function addFriend(friend) { 138 function addFriend(friend) {
130 xmpp.subscribe(friend); 139 xmpp.subscribe(friend);
131 } 140 }
132 141
133 exports.init = init; 142 exports.init = init;
134 exports.sendMessage = sendMessage; 143 exports.sendMessage = sendMessage;
135 exports.setOptions = setOptions; 144 exports.setOptions = setOptions;
136 exports.addFriend = addFriend; 145 exports.addFriend = addFriend;
137 146