blob: 3fd887850503534e960481bb2db2dd8e37ab52e5 [file] [log] [blame] [raw]
#!/opt/node/bin/node
const DEFAULT_PORT = 22;
const DEFAULT_USER_NAME = "sshout";
var fs = require("fs");
var util = require("util");
var sshout = require("sshout");
var ALGORITHMS = require("ssh2-streams").constants.ALGORITHMS;
var os = require("os");
var url = require("url");
var StringStartsWith = function(s, sub) {
if(s.length < sub.length) return false;
for(var i=0; i<sub.length; i++) {
if(s[i] != sub[i]) return false;
}
return true;
};
var print_usage = function(name) {
process.stderr.write(util.format("Usage: %s [-i <identify-file>] [-v] ssh://[<user-name>@]<sshout-server>[:<port>][/]\n", name));
};
var server_url = null;
//var identify_files = [];
var identify_file = null;
var verbose = 0;
for(var i=2; i<process.argv.length; i++) {
if(process.argv[i][0] == "-") {
var o = process.argv[i];
for(var j=1; j<o.length; j++) switch(o[j]) {
case "h":
print_usage(process.argv[1]);
process.exit(0);
break;
case "i":
if(++i >= process.argv.length) fatal_option_require_argument("u");
//identify_files.push(process.argv[i]);
identify_file = process.argv[i];
break;
case "v":
verbose++;
break;
}
} else {
if(server_url !== null) {
process.stderr.write("Extra string after URL\n");
process.exit(-1);
}
//url = new URL(process.argv[i]);
server_url = url.parse(process.argv[i], false, false);
if(server_url.protocol !== "ssh:") {
process.stderr.write("Protocol name in URL must be 'ssh'\n");
process.exit(-1);
}
}
}
if(server_url === null) {
process.stderr.write("Need an URL\n");
print_usage(process.argv[1]);
process.exit(-1);
}
var kex_list = ALGORITHMS.SUPPORTED_KEX;
var request_user_name = server_url.auth;
if(request_user_name === null) {
request_user_name = DEFAULT_USER_NAME;
} else {
var colon_i = request_user_name.indexOf(":");
if(colon_i !== -1) request_user_name = request_user_name.substring(0, last_i);
}
var private_key = identify_file === null ? null : fs.readFileSync(identify_file);
var debug_print = verbose ? function(s) {
if(verbose < 2 && StringStartsWith(s, "DEBUG: Parser: ")) return;
process.stderr.write(s);
process.stderr.write("\n");
} : null;
var sshout_client = new sshout.Client({
algorithms:{ kex:kex_list },
host:server_url.hostname,
port:server_url.port ? server_url.port : DEFAULT_PORT,
username:request_user_name,
privateKey:private_key,
tryKeyboard:true, // Needed for public chatrooms
debug:debug_print
});
sshout_client.on("ready", function(canoncal_user_name) {
process.stderr.write(util.format("SSHOUT is ready, my user name is '%s'\n", canoncal_user_name));
sshout_client.send_message("GLOBAL", "plain", "TEST");
sshout_client.request_user_info();
});
sshout_client.on("error", function(e, msg) {
console.error(typeof e);
});
sshout_client.on("message", function(o) {
console.log(o);
});
sshout_client.on("user-state-changed", function(state, user) {
process.stdout.write(util.format("User %s is %s\n", user, state ? "online" : "offline"));
});
sshout_client.on("motd", function(s) {
process.stdout.write("Message of the day:\n");
process.stdout.write(s);
});
sshout_client.on("user-info", function(my_id, user_info) {
process.stdout.write(util.format("My ID: %d\n", my_id));
console.log(user_info);
});
sshout_client.start();