-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathui.js
More file actions
292 lines (269 loc) · 7.4 KB
/
ui.js
File metadata and controls
292 lines (269 loc) · 7.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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
var persist = require('./persistence');
if(typeof(String.prototype.trim) === "undefined")
{
String.prototype.trim = function()
{
return String(this).replace(/^\s+|\s+$/g, '');
};
}
function mysplit(s,delim,n) {
var results = [];
var times = 0;
var pos = 0;
while(s.length) {
var pos = s.indexOf(delim);
if(pos==-1 || times >= n) {
results.push(s);
return results;
}
results.push(s.slice(0,pos));
s = s.slice(pos+delim.length);
times += 1;
}
return results;
}
var readline = require('readline-history');
var net = require('./nets');
var commands = Object(null);
var globalDone = false;
var aliases = null;
var raliases = {};
function maybeAlias(id) {
var alias = aliases[id];
if (alias)
return alias;
return id;
}
function deAlias(alias) {
var id = raliases[alias];
if(!id)
id = alias;
return id;
}
function monitor() {
persist('aliases',function(as) {
aliases = as;
for(var key in aliases) {
raliases[aliases[key]] = key;
}
readline.createInterface({
path: "history.log",
maxLength: 0x100,
input: process.stdin,
output: process.stdout,
completer: function (line) {
var stuff = mysplit(line," ",1);
var lname = stuff[0];
var hits = [];
for(var name in commands) {
if (name.indexOf(lname)==0) {
if(name == lname) {
var command = commands[name];
if(command.complete) {
command.complete(hits,stuff[1]);
continue;
}
}
hits.push(name);
}
}
return [hits,line];
},
next: function(rl) {
rl.setPrompt("> ");
rl.prompt();
rl.on('line', function (cmd) {
// [cmd,args] = cmd.split(" ",2);
if(cmd == '') {
commands.help();
} else if(cmd[0]=='/') {
var stuff = mysplit(cmd," ",1);
cmd = stuff[0].slice(1);
var args = stuff[1];
cmd = commands[cmd];
if (cmd == undefined) {
print("I don't recognize that command.");
print("Commands:");
commands.help();
} else {
cmd(args);
}
} else {
for(var friend in net.friends) {
net.sendMessage(friend,cmd);
}
}
if(globalDone) {
rl.close();
} else {
rl.prompt();
}
}).on('close', function () {
print("Shutting down.");
process.exit(0);
});
}
});
});
}
function command(info,f) {
f.doc = info.doc;
f.fulldoc = info.fulldoc;
return f;
}
function register(info,f) {
commands[info['name']] = command(info,f);
}
function alias(name,alias) {
commands[alias] = commands[name];
}
register({
name: 'help',
doc: "help on various things."
},
function(args) {
function shortHelp(name) {
var command = commands[name];
if (command == undefined) return;
var doc = command.doc;
if(doc != undefined) {
print('/'+name + ": " + doc);
}
return command;
}
if(args == undefined || args == '') {
for (var name in commands) {
shortHelp(name);
}
} else {
// [subcommand,args] = args.split(" ",2);
var stuff = mysplit(args," ", 2);
var name = stuff[0].slice(1);
args = stuff[1];
subcommand = shortHelp(name);
if (subcommand) {
if(subcommand.fulldoc) {
subcommand.fulldoc(args);
}
} else {
print(name+" is not a registered command.");
}
}
});
register({
name: 'quit',
doc: "SHUT. DOWN. EVERYTHING.",
fulldoc: function(args) {
print("This quit command's full documentation...");
print("Is just a test of the fulldoc interface.");
}
},
function(args) {
globalDone = true;
});
register({
name: 'add',
doc: "Add a friend to chat with you!",
fulldoc: function(args) {
print('syntax: /add ip/port');
print('host is a cjdns host (hopefully)');
print('port is an integer.');
print("each person's host/port is listed at the top of the chat.");
}},
function add(args) {
var stuff = args.split('/');
if (stuff.length != 2) {
add.fulldoc();
return;
}
host = stuff[0];
port = stuff[1];
net.friends[host] = true;
net.ports[host] = port;
});
register({
name: 'remove',
doc: "Remove someone from being able to chat.",
fulldoc: function(args) {
print('syntax: /remove host');
print('host is a cjdns host (hopefully)');
print('port is an integer.');
print("each person's host/port is listed at the top of the chat.");
}},
function(args) {
var name = args;
var host = raliases[name];
if(!host) {
host = name = mysplit(name,'/',1)[0];
}
print("Removing "+name+" from your buddy list.");
delete net.friends[host];
var alias = aliases[host];
if(alias) {
delete aliases[host];
delete raliases[alias];
}
delete net.ports[host];
});
alias('remove','ignore');
register({
name: 'send',
doc: "Offer to send someone a file",
fulldoc: function(args) {
print('Syntax: /send <friend> <filename>');
}},
function(args) {
stuff = mysplit(args,' ',1)
if(stuff.length != 2) {
this.fulldoc();
return;
}
friend = stuff[0]
filename = stuff[1]
print('Under construction.');
// now get the basename, copy the file into public, then send them the URL to public/basename
});
register({
name: 'get',
doc: "Get a file",
fulldoc: function(args) {
print('syntax /get <url>');
}},
function(args) {
net.download(args);
});
register({
name: 'list',
doc: "List your friends."
},
function(args) {
for(var friend in net.friends) {
print(friend+'/'+net.ports[friend]);
var alias = aliases[friend];
if (alias) {
print('Alias:',alias);
}
}
});
/* XXX: ui.alias is NOT the same as the ui command /alias.
* 1st = alias for command names
* 2nd = alias for friends
*
* better name for 1/2 = ?
*/
register({
name: 'alias',
doc: "Create an easy to recognize alias for your friend."
},
function(args) {
var stuff = mysplit(args,' ',1);
var host = stuff[0].trim();
host = mysplit(host,'/')[0];
aliases[host] = stuff[1].trim();
});
var print = exports.print = console.log;
exports.register = register;
exports.alias = alias;
exports.monitor = monitor;
exports.maybeAlias = maybeAlias
exports.deAlias = deAlias