forked from Armax/lolchat.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlolchat.io.js
193 lines (166 loc) · 4.97 KB
/
lolchat.io.js
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// Code by @Arm4x
var Client = require('node-xmpp-client')
var EventEmitter = require('events').EventEmitter
// Basic infoz
var p_prefix = "AIR_";
var xiff = 'xiff';
var domain = 'pvp.net'
var port = 5223
var allFriends = {}; // JID
var onlineFriends = {}; // SummonerName
// Servers
var SERVERS = {
eu_west: 'chat.euw1.lol.riotgames.com',
eu_ordic: 'chat.eun1.riotgames.com',
us: 'chat.na2.lol.riotgames.com',
pbe: 'chat.pbe1.lol.riotgames.com',
oceania: 'chat.oc1.lol.riotgames.com',
brazil: 'chat.br.lol.riotgames.com',
turkey: 'chat.tr.lol.riotgames.com',
russia: 'chat.ru.lol.riotgames.com',
latin_america_north: 'chat.la1.lol.riotgames.com',
latin_america_south: 'chat.la2.lol.riotgames.com'
}
var status = "<body>\
<profileIcon>973</profileIcon>\
<level>30</level>\
<statusMsg>node.js chat lib</statusMsg>\
<rankedWins>1337</rankedWins>\
<gameStatus>outOfGame</gameStatus>\
<rankedLeagueName>Twitch's Commanders</rankedLeagueName>\
<rankedLeagueDivision>Zero</rankedLeagueDivision>\
<rankedLeagueTier>DIAMOND</rankedLeagueTier>\
</body>\
";
// Functionz
function getRoster(connection_object) {
var roster = new Client.Element('iq', { id: 'roster_0', type: 'get' });
roster.c('query', { xmlns: 'jabber:iq:roster' });
connection_object.send(roster);
}
function Chat() {
// Setup
var self = this
var client;
var events;
self.events = new EventEmitter();
// Core function
self.connect = function(username,password,server) {
// Creating client
var client = new Client({
jid: username+'@'+domain+'/'+xiff,
password: p_prefix + password,
host: SERVERS[server],
port: port,
legacySSL: true,
status: status
});
self.client = client;
// Online
client.on('online', function(data) {
client.send(new Client.Element('presence', { })
.c('status').t(status)
)
self.events.emit('connected')
if(client.connection.socket) {
client.connection.socket.setTimeout(0);
client.connection.socket.setKeepAlive(true, 10000);
}
getRoster(client);
});
// Handling room
client.on('stanza', function(stanza) {
// Getting all friends
if(stanza.is('iq')) {
for (var f in stanza.children[0].children) {
allFriends[stanza.children[0].children[f].attrs.jid] = stanza.children[0].children[f].attrs.name;
}
}
// Online friends
if (stanza.is('presence')) {
try {
var friendname = allFriends[stanza.attrs.from.split('/')[0]];
var toSplit = stanza.attrs.from.split('/');
var friend = toSplit[0];
if (stanza.attrs.type && stanza.attrs.type === 'unavailable') {
delete onlineFriends[friendname];
}
else if (stanza.children.length > 0) {
var friendstuff = {
status: stanza.children[0].children[0],
body: stanza.children[1].children[0]
};
var name = allFriends[friend];
var body = friendstuff.body;
var info;
addInfo = {
status: friendstuff.status,
body: friendstuff.body,
jid: friend
}
if (name) {
self.events.emit('onlineUpdate', name);
}
}
}
catch(ex) {
//todo
}
}
// Getting message
if(stanza.is('message')) {
if(stanza.attrs.type == 'chat') {
var body = stanza.getChild('body');
if(body) {
var msg = body.getText();
var author = stanza.attrs.from;
var id = author.split('/')[0];
var friendName = allFriends[id];
console.log('['+friendName+'] ' + msg);
}
}
}
});
// Handling messages
client.on('receiveMessage', function(from, msg) {
console.log('['+author+'] ' + msg);
});
// Error & Disconnected handlers
client.on('error', function(err) {
console.log('[!] ' + err);
});
client.on('close', function() {
console.log('[!] Disconnected');
});
}
self.sendMsg = function(to, msg) {
to += '/xiff';
var stanza = new Client.Element('message', { to: to, type: 'chat' });
stanza.c('body').t(msg);
self.client.send(stanza);
}
self.getFriendJid = function(friend) {
for(var x in allFriends) {
if(allFriends[x].toLowerCase()==friend.toLowerCase()) {
return(x);
}
}
}
self.editStatus = function(status) {
status = status;
self.client.send(new Client.Element('presence', { })
.c('status').t(status)
)
}
self.getAllFriends = function() {
return allFriends;
}
self.getOnlineFriends = function() {
return onlineFriends;
}
self.getCurrentStatus = function() {
return status;
}
}
module.exports = new Chat();
module.exports.Chat = Chat;