/
index.js
executable file
·269 lines (246 loc) · 7.38 KB
/
index.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#!/usr/bin/env node
// Wikipedia telnet server
//
// To install dependencies:
// npm install mw-ocg-texter
var Promise = require('babybird');
var fs = require('fs');
var path = require('path');
var querystring = require('querystring');
var readline = require('readline');
var request = require('request');
var telnet = require('telnet');
var texter = require('mw-ocg-texter/lib/standalone');
var port = parseInt(process.argv[2]) || 1081;
var separator = '\x1b[46;1m\x1b[K\n\x1b[44;1m\x1b[K\n\x1b[39;49m\x1b[K';
separator = '\n'; // Fancy separator seems to have some boldface issues.
var ps1 = '>>> ';
// Cache siteinfo requests for some extra efficiency.
var cachedSiteinfo = Object.create(null);
var siteinfoCacher = function(bundler, wikis, log) {
var key = '$' + wikis.map(function(w) { return w.baseurl; } ).join('|');
if (!cachedSiteinfo[key]) {
cachedSiteinfo[key] = new bundler.siteinfo(
wikis,
function () { /* don't log request retries */ }
);
}
return cachedSiteinfo[key];
};
// Attempt to render welcome message from :en:User:cscott/Telnet,
// but fall back to the contents of wiki-logo.txt if there is a
// problem.
var getLogoP = function() {
return (function() {
console.log('Fetching logo...');
var cachedLogo = '';
return texter.convert({
domain: 'en.wikipedia.org',
title: 'User:cscott/Telnet',
siteinfo: siteinfoCacher,
stream: {
write: function(chunk, cb) {
cachedLogo += chunk.toString();
return cb();
}
}
}).then(function() {
// Remove initial "title" line from output.
return cachedLogo.replace(/^\S+[\n\r]+/, '');
});
})().catch(function() {
return fs.readFileSync(path.join(__dirname, 'wiki-logo.txt'));
});
};
var logoP = getLogoP();
// Refresh this every six hours.
setInterval(function() { logoP = getLogoP(); }, 6*60*60*1000 );
var tabCompleteQuery = function(domain, search_term) {
var apiURL = 'https://' + domain + '/w/api.php';
var queryobj = {
action: 'query',
format: 'json',
prop: 'pageprops',
generator: 'prefixsearch',
ppprop: 'displaytitle',
gpssearch: search_term,
gpsnamespace: 0,
gpslimit: 6
};
apiURL += '?' + querystring.stringify(queryobj);
var user = process.env.USER || process.env.LOGNAME || process.env.HOME ||
'unknown';
return new Promise(function(resolve, reject) {
request({
url: apiURL,
encoding: 'utf8',
headers: {
'User-Agent': 'wikipedia-telnet/1.0.0/' + user
},
pool: false
}, function(error, response, body) {
if (error || response.statusCode !== 200) {
return reject(error || new Error("Unexpected HTTP status: " + response.statusCode));
}
return resolve(body);
});
}).then(function(body) {
var result = JSON.parse(body);
if (!result || result.error) {
throw new Error(result.error.info || "Bad request");
}
return result;
});
};
function completer(linePartial, callback) {
var basicCmds = ':quit|:use en.wikipedia.org|:use es.wikipedia.org|:use ja.wikipedia.org|:use de.wikipedia.org|:use ru.wikipedia.org|:use fr.wikipedia.org|:use it.wikipedia.org|:use pt.wikipedia.org|:use zh.wikipedia.org|:use pl.wikipedia.org'.split('|');
var hits = basicCmds.filter(function(c) {
return c.slice(0, linePartial.length) === linePartial;
});
// Also do an API query with the title suggester:
if (linePartial === '') {
return callback(null, [hits.length ? hits : basicCmds, linePartial]);
}
tabCompleteQuery(this.domain, linePartial).then(function(resp) {
var result = [];
Object.keys(resp.query.pages).forEach(function(pageid) {
var page = resp.query.pages[pageid];
result[page.index - 1] = page.title;
});
hits = hits.concat(result);
}, function(e) { /* some error occurred, ignore it. */ }).then(function() {
callback(null, [hits.length ? hits : basicCmds, linePartial]);
});
}
// This is a hack, it doesn't really do full Unicode normalization. Oh, well.
function normalizeCase(str) {
str = str.replace(/^\s+|\s+$/g, ''); // trim
str = str.toLowerCase();
// remove accents, swap ñ for n, etc
var from = "àáäâèéëêìíïîòóöôùúüûñç·/_,:;";
var to = "aaaaeeeeiiiioooouuuunc------";
for (var i=0, l=from.length ; i<l ; i++) {
str = str.replace(from.charAt(i), to.charAt(i));
}
str = str
.replace(/\s+/g, '-') // collapse whitespace and replace by -
.replace(/-+/g, '-'); // collapse dashes
return str;
}
function recv(rl, client, line) {
rl.pause();
line = line.trim();
var m = /^:(host|use)\s+(\S+\.org)$/i.exec(line);
if (m) {
this.domain = m[2];
client.write('Using '+this.domain+' for future articles.\n');
rl.prompt();
return;
}
if (line === ':quit') {
client.write('Bye!\n');
rl.close();
return;
}
texter.convert({
domain: this.domain,
title: line,
stream: client,
// siteinfo cacher is optional, but it speeds things up
// by eliminating an unnecessary action API request for each article
siteinfo: siteinfoCacher,
}).catch(function(e) {
// Before failing, see if case differences could account for the
// failure.
return tabCompleteQuery(this.domain, line).then(function(resp) {
var best = null, bestIndex = 0, target = normalizeCase(line);
Object.keys(resp.query.pages).forEach(function(pageid) {
var page = resp.query.pages[pageid];
if (best === null || bestIndex > page.index) {
if (target === normalizeCase(page.title)) {
best = page.title; bestIndex = page.index;
}
}
});
if (!best) { throw e; }
return texter.convert({
domain: this.domain,
title: best,
stream: client,
siteinfo: siteinfoCacher
});
}).catch(function(e) {
client.write('Sorry! Could not fetch "' + line + '" for you.\n' +
'No worries. There are lots of other pages to read.\n' +
'Pick a different title.\n');
});
}).then(function() {
client.write(separator);
rl.prompt();
});
}
var server = telnet.createServer(function (client) {
// some state:
var state = {
domain: 'en.wikipedia.org'
};
client.on('window size', function(e) {
if (e.command === 'sb') {
// A real "resize" event; readline listens for this.
client.columns = e.columns;
client.rows = e.rows;
client.emit('resize');
}
});
// 'readline' will call `setRawMode` when it is a function
client.setRawMode = setRawMode;
// Make unicode characters work properly
client.do.transmit_binary();
// Make the client emit 'window size' events
client.do.window_size();
// Create a read line interface.
var rl = readline.createInterface({
input: client,
output: client,
terminal: true,
completer: completer.bind(state)
});
rl.setPrompt(ps1); rl.pause();
rl.on('close', function() { client.end(); });
logoP.then(function(logo) {
client.write(logo);
rl.prompt();
rl.on('line', recv.bind(state, rl, client));
} );
});
server.on('error', function (err) {
if (err.code === 'EACCES') {
console.error(
'%s: You must be "root" to bind to port %d', err.code, port
);
} else {
throw err;
}
});
server.on('listening', function () {
console.log(
'wikipedia telnet server listening on port %d', this.address().port
);
console.log(' $ telnet localhost' + (port != 23 ? ' ' + port : ''));
});
server.listen(port);
/**
* The equivalent of "raw mode" via telnet option commands.
* Set this function on a telnet `client` instance.
*/
function setRawMode (mode) {
if (mode) {
this.do.suppress_go_ahead();
this.will.suppress_go_ahead();
this.will.echo();
} else {
this.dont.suppress_go_ahead();
this.wont.suppress_go_ahead();
this.wont.echo();
}
}