Permalink
Browse files

forgot to add otr-keymanager.js

  • Loading branch information...
mnaamani committed Jul 4, 2012
1 parent cf3de83 commit f5558538e626fe218e6b2a8671c3007d952823e5
Showing with 423 additions and 0 deletions.
  1. +40 −0 experiment/make-test-keys.js
  2. +131 −0 experiment/otr-keymanager.js
  3. +252 −0 experiment/otr-talk.js
@@ -0,0 +1,40 @@
+var otr=require("otr");
+console.log("libotr version:",otr.version());
+
+var users = {
+ alice: new otr.UserState(),
+ bob: new otr.UserState(),
+};
+
+users.alice.conf ={
+ keys: "./keys/alice.keys",
+ fingerprints: "./keys/alice.fp",
+ account: "alice@telechat.org",
+ proto: "telechat"
+};
+
+users.bob.conf={
+ keys: "./keys/bob.keys",
+ fingerprints: "./keys/bob.fp",
+ account: "bob@telechat.org",
+ proto: "telechat"
+};
+
+function genKey(user,cb){
+ var conf = user.conf;
+ console.log("Generating OTR key for:", conf.proto+":"+conf.account);
+ user.generateKey(conf.keys,conf.account,conf.proto,function(err){
+ if(!err){
+ console.log("Key Generated. for:",conf.proto+":"+conf.account);
+ }else{
+ console.log(err);
+ }
+ if(cb) cb(user);
+ });
+}
+
+
+genKey(users.alice, function(){
+ genKey(users.bob);
+});
+
@@ -0,0 +1,131 @@
+var process_exit = process.exit;
+process.exit=exit;
+
+var otr=require("otr");
+var options = require("optimist").argv;
+var fs = require("fs");
+var path = require("path");
+var prompt = require("prompt");
+
+prompt.message="";
+prompt.delimiter="";
+
+prompt.start();
+
+var oKeysFile;
+var oFingerprintsFile = "~/.purple/otr.fingerprints"
+var oAccountName;
+var oProtocol;
+var oList=false;
+var oGenerate=false;
+var fileExists = false;
+var user = new otr.UserState();
+var key_generation_inprogress = false;
+
+oKeysFile = options.f ? options.f : (options.file? options.file : "~/.purple/otr.private_key");
+oAccountName = options.a ? options.a : (options.accountname? options.accountname : undefined );
+oProtocol = options.p ? options.p : (options.protocol? options.protocol : undefined );
+oList = options.l || options.list;
+oGenerate = options.g || options.generate;
+
+if(options.h || options.help){
+ usage();
+ exit();
+}
+
+//if(fs.exists(oKeysFile)){ //-->nodejs v0.8+
+if(path.existsSync(oKeysFile)){
+ fileExists=true;
+ try{
+ user.readKeysSync(oKeysFile);
+ }catch(e){
+ if(oList){
+ console.log("Error Reading File:".red,oKeysFile);
+ console.log(e);
+ exit();
+ }
+ }
+}
+
+if(oList){
+ if(!fileExists){
+ console.log("File: ",oKeysFile,"doesn't exist.".red);
+ }else{
+ if(oAccountName && oProtocol){
+ console.log(oProtocol+":"+oAccountName,"fingerprint:",user.fingerprint(oAccountName,oProtocol));
+ }else{
+
+ console.log(user.accounts());
+ }
+ }
+ exit();
+}else if(oGenerate){
+ if(!oAccountName || !oProtocol) {
+ console.error("Cannot generate keypair. Please specify both accountname and protocol.");
+ usagehint();
+ exit();
+ }else{
+ if(user.fingerprint(oAccountName,oProtocol)){
+ //keypair already exists..
+ console.log("A keypair already exists for specified account.".yellow);
+ console.log("Generating a new keypair will overwrite the existing one.");
+ prompt.confirm("Do you wish to proceed [Y/n]?",function(err,result){
+ if(err) exit();
+ if(result){
+ generate_key();
+ }else exit();
+ });
+
+ }else{
+ generate_key();
+ }
+ }
+}else{
+ console.log("No action specified.".grey);
+ usagehint();
+ exit();
+}
+
+
+function usagehint(){
+ console.log("For help, type:",options.$0,"-h");
+}
+
+function usage(){
+ console.log("Usage: ",options.$0, "[OPTION] [ACTION]");
+ console.log("Manage your OTR keys. Default file location is ~/.purple/otr.private_key (Pidgin)\n");
+ console.log("options:");
+ console.log(" -f, --file\t\tpath to file containing otr keys");
+ console.log(" -a, --accountname\taccountname");
+ console.log(" -p, --protocol\tprotocol");
+ console.log("actions:");
+ console.log(" -l, --list\t\tdisplays public key fingerprint of specified account/protocol.[all if not specified]");
+ console.log(" -g, --generate\tgenerate keypair for specified accountname/protocol.");
+ console.log
+}
+
+function generate_key(){
+ key_generation_inprogress=true;
+ console.log("Generating OTR keypair for account:", oProtocol+":"+oAccountName);
+ console.log("Target file:",oKeysFile);
+ console.log("This will take a few minutes...".yellow);
+ //todo: make a backup file in case anything goes wrong or process terminates during key generation..
+ user.generateKey(oKeysFile,oAccountName,oProtocol,function(err){
+ key_generation_inprogress=false;
+ if(!err){
+ console.log("Key Generated Successfully".green);
+ }else{
+ console.log(err);
+ }
+ exit();
+ });
+}
+
+function exit(val){
+ if(key_generation_inprogress) {
+ console.log("Please wait until key generation is complete".yellow);
+ return;
+ }
+ prompt.pause();
+ process_exit(val);
+}
Oops, something went wrong.

0 comments on commit f555853

Please sign in to comment.