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