-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconnection.js
182 lines (158 loc) · 4.78 KB
/
connection.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
var req = require('request');
var jsdom = require('jsdom');
var bot = require('./source/bot');
var IO = require('./source/IO');
require('./source/util');
var WebSocketClient = require('websocket').client;
var cookieJar = req.jar();
var request = req.defaults({jar: cookieJar});
var roomId = 1;
// The login process works in two stages. First, you go to the login page. From the login page, you take the fkey,
// and then you send a POST request to login, using your details and the fkey
// Similarly, the initLogin function here visits the login page, gets the fkey, and passes it all to auth, which does the authentication
// using a POST request with all the details
var initLogin = function (getURL, formURL, formObj, callback) {
request.get({
url: getURL
}, function (e, res, body) {
jsdom.env(body, function (errors, window) {
auth(window, formURL, formObj, callback);
});
});
};
// Note that the auth function does append the fkey to the formObj sent in the POST request
var auth = function (window, formURL, formObj, callback) {
var fkey = window.document.querySelector('input[name="fkey"]').value;
formObj.fkey = fkey;
request.post({
followAllRedirects: true,
url: formURL,
form: formObj
}, function(error, response, body) {
jsdom.env(body, function (errors, window){
callback();
});
});
};
var chatFkey;
var loginChat = function (callback) {
console.log('Getting fkey on chat');
request.get({
url: 'http://chat.stackoverflow.com'
}, function(error, response, body) {
jsdom.env(body, function (errors, window) {
chatFkey = window.document.querySelector('input[name="fkey"]').value;
bot.fkey = chatFkey;
bot.users = window.users;
callback();
});
});
};
// FIXME overwritting sendToRoom
var sendMessage = bot.adapter.out.sendToRoom = function (text) {
if (!chatFkey) {
console.log('fkey for chat missing');
return false;
}
request.post({
url: 'http://chat.stackoverflow.com/chats/' + roomId + '/messages/new',
form: {
text: text,
fkey: chatFkey
}
}, function(error, response, body) {
console.log('Message sent', text);
console.log(body);
IO.fire( 'sendoutput', body, text, roomId );
});
};
var loginSEOpenID = function (email, password, callback) {
initLogin(
'https://openid.stackexchange.com/account/login',
'https://openid.stackexchange.com/account/login/submit/',
{
email: email,
password: password
},
function (){
console.log('SE: Logged in');
loginSO(callback);
}
);
};
var loginSO = function (callback) {
initLogin(
'http://stackoverflow.com/users/login',
'http://stackoverflow.com/users/authenticate',
{
openid_identifier: 'https://openid.stackexchange.com'
},
function (){
console.log('SO: Logged in');
loginChat(callback);
}
);
};
var login = function (email, password, callback) {
loginSEOpenID(email, password, callback);
};
var getSocketURL = function (callback) {
if (!chatFkey) {
console.log('Cannot get ws URL without fkey');
return false;
}
request.post({
url: 'http://chat.stackoverflow.com/ws-auth',
form: {
roomid: roomId,
fkey: chatFkey
}
}, function (error, response, body) {
callback(JSON.parse(body).url);
});
};
var handleMessageObject = function ( msg ) {
var et = msg.event_type;
// console.log('Handling type', et);
if ( et === 1 || et === 2 ) {
IO.fire( 'input', msg );
}
};
var connect = function () {
getSocketURL(function (url) {
// two guys walk into a bar SO chat room. the bartender asks them "is this some kind of joke?"
bot.adapter.init(chatFkey, roomId);
var client = new WebSocketClient();
client.on('connectFailed', function(error) {
console.log('Connect Error: ' + error.toString());
});
client.on('connect', function(connection) {
console.log('WebSocket client connected');
connection.on('error', function(error) {
console.log("Connection Error: " + error.toString());
});
connection.on('close', function() {
console.log('Connection Closed');
});
connection.on('message', function(message) {
if (message.type === 'utf8') {
console.log('Received: ', message.utf8Data);
var obj = JSON.parse(message.utf8Data);
if (obj['r' + roomId] && obj['r' + roomId].e) {
// bot.parseMessage(obj['r' + roomId].e[0]);
var resp = obj['r' + roomId].e;
resp.forEach(handleMessageObject);
}
}
});
});
// Stack needs the ?l=SomeLargeNumber for all of its WebSocket connections for some reason
// The parameters for connect: requestURL, protocol, origin, headers
// This wasn't documented on their repo, but it's evident in the WebSocketClient.js file in the module
client.connect(url + '?l=9999999', null, 'http://chat.stackoverflow.com', {'Set-Cookie': cookieJar});
});
};
exports.login = login;
exports.sendMessage = sendMessage;
exports.connect = connect;
exports.cookieJar = cookieJar;