Commit f767d3c54e5b4a77dcfa7f6a2a90cdd1a1e48657
1 parent
adca6db607
Exists in
master
penanganan khusus jika pesan dari diri sendiri
Showing 1 changed file with 8 additions and 2 deletions Inline Diff
adaptor-ym.js
1 | var YM; | 1 | var YM; |
2 | 2 | ||
3 | var username; | 3 | var username; |
4 | var password; | 4 | var password; |
5 | var callbacks; | 5 | var callbacks; |
6 | 6 | ||
7 | var isOnline = false; | 7 | var isOnline = false; |
8 | var msSleepBeforeResendOnFail = 10000; | 8 | var msSleepBeforeResendOnFail = 10000; |
9 | 9 | ||
10 | function onReady() { | 10 | function onReady() { |
11 | logger.info('Going to login to YM as ' + username); | 11 | logger.info('Going to login to YM as ' + username); |
12 | YM.login(username, password); | 12 | YM.login(username, password); |
13 | } | 13 | } |
14 | 14 | ||
15 | function onOnline(data) { | 15 | function onOnline(data) { |
16 | isOnline = true; | 16 | isOnline = true; |
17 | 17 | ||
18 | logger.info('YM login successful', {data: data}); | 18 | logger.info('YM login successful', {data: data}); |
19 | if (callbacks.onOnline) { | 19 | if (callbacks.onOnline) { |
20 | callbacks.onOnline(); | 20 | callbacks.onOnline(); |
21 | } | 21 | } |
22 | } | 22 | } |
23 | 23 | ||
24 | function onLoginError(data) { | 24 | function onLoginError(data) { |
25 | logger.warn('YM login error', {data: data}); | 25 | logger.warn('YM login error', {data: data}); |
26 | } | 26 | } |
27 | 27 | ||
28 | function onFriendsList(data) { | 28 | function onFriendsList(data) { |
29 | logger.verbose('Got list of friendlist', {data: data}); | 29 | logger.verbose('Got list of friendlist', {data: data}); |
30 | } | 30 | } |
31 | 31 | ||
32 | function onPM(data) { | 32 | function onPM(data) { |
33 | if (data.sender == username) { | ||
34 | return; | ||
35 | } | ||
36 | |||
33 | logger.verbose('Got a message', {data: data}) | 37 | logger.verbose('Got a message', {data: data}) |
34 | if (callbacks.onPM) { | 38 | if (callbacks.onPM) { |
35 | callbacks.onPM(data.sender, data.message); | 39 | callbacks.onPM(data.sender, data.message); |
36 | } | 40 | } |
37 | } | 41 | } |
38 | 42 | ||
39 | function onPing(data) { | 43 | function onPing(data) { |
40 | logger.verbose('Got ping', {data: data}); | 44 | logger.verbose('Got ping', {data: data}); |
41 | } | 45 | } |
42 | 46 | ||
43 | function init(_username, _password, _logger, _callbacks) { | 47 | function init(_username, _password, _logger, _callbacks) { |
44 | username = _username; | 48 | username = _username; |
45 | password = _password; | 49 | password = _password; |
46 | logger = _logger; | 50 | logger = _logger; |
47 | callbacks = _callbacks; | 51 | callbacks = _callbacks; |
48 | 52 | ||
49 | delete YM; | 53 | delete YM; |
50 | YM = require('yahoomessenger'); | 54 | YM = require('yahoomessenger'); |
51 | 55 | ||
52 | YM.on('ready', onReady); | 56 | YM.on('ready', onReady); |
53 | YM.on('loginSuccessful', onOnline); | 57 | YM.on('loginSuccessful', onOnline); |
54 | YM.on('loginError', onLoginError); | 58 | YM.on('loginError', onLoginError); |
55 | YM.on('friendsList', onFriendsList); | 59 | YM.on('friendsList', onFriendsList); |
56 | YM.on('pm', onPM); | 60 | YM.on('pm', onPM); |
57 | YM.on('offlinePM', onPM); | 61 | YM.on('offlinePM', onPM); |
58 | 62 | ||
59 | YM.newInstance(); | 63 | YM.newInstance(); |
60 | } | 64 | } |
61 | 65 | ||
62 | function sendMessage(destination, msg, requestId) { | 66 | function sendMessage(destination, msg, requestId) { |
63 | if (!isOnline) { | 67 | if (!isOnline) { |
64 | logger.info('We are not online right now, resending in ' + msSleepBeforeResendOnFail + 'ms', {destination: destination, msg: msg, requestId: requestId}); | 68 | logger.info('We are not online right now, resending in ' + msSleepBeforeResendOnFail + 'ms', {destination: destination, msg: msg, requestId: requestId}); |
65 | 69 | ||
66 | setTimeout( | 70 | setTimeout( |
67 | sendMessage, | 71 | sendMessage, |
68 | msSleepBeforeResendOnFail, | 72 | msSleepBeforeResendOnFail, |
69 | destination, | 73 | destination, |
70 | msg, | 74 | msg, |
71 | requestId | 75 | requestId |
72 | ) | 76 | ) |
73 | return; | 77 | return; |
74 | } | 78 | } |
75 | 79 | ||
76 | logger.verbose('Sending message', {from: username, destination: destination, msg: msg, requestId: requestId}); | 80 | if (destination != username) { |
77 | 81 | logger.verbose('Sending message', {from: username, destination: destination, msg: msg, requestId: requestId}); | |
82 | } | ||
83 | |||
78 | try { | 84 | try { |
79 | YM.sendPM(destination, msg); | 85 | YM.sendPM(destination, msg); |
80 | } | 86 | } |
81 | catch(e) { | 87 | catch(e) { |
82 | logger.warn(__filename + ': Exception on sending message: ' + e, {e: e, destination: destination, msg: msg, requestId: requestId}); | 88 | logger.warn(__filename + ': Exception on sending message: ' + e, {e: e, destination: destination, msg: msg, requestId: requestId}); |
83 | logger.info('Trying to relogin to YM in 5 secs'); | 89 | logger.info('Trying to relogin to YM in 5 secs'); |
84 | 90 | ||
85 | setTimeout( | 91 | setTimeout( |
86 | init, | 92 | init, |
87 | 5000, | 93 | 5000, |
88 | username, password, logger, callbacks | 94 | username, password, logger, callbacks |
89 | ); | 95 | ); |
90 | } | 96 | } |
91 | } | 97 | } |
92 | 98 | ||
93 | function logout(){ | 99 | function logout(){ |
94 | isOnline = false; | 100 | isOnline = false; |
95 | try { | 101 | try { |
96 | YM.logout(); | 102 | YM.logout(); |
97 | } catch(e) {} | 103 | } catch(e) {} |
98 | } | 104 | } |
99 | 105 | ||
100 | exports.init = init; | 106 | exports.init = init; |
101 | exports.sendMessage = sendMessage; | 107 | exports.sendMessage = sendMessage; |
102 | exports.isOnline = isOnline; | 108 | exports.isOnline = isOnline; |
103 | 109 |