Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

fixed create_privkey callback, properly sending OTR query

  • Loading branch information...
commit f899e77253b70281ee8236dcef2dcaade7140aec 1 parent b2370cc
@mnaamani authored
Showing with 56 additions and 29 deletions.
  1. +32 −22 examples/test.js
  2. +5 −2 index.js
  3. +15 −3 src/message.cc
  4. +4 −2 src/message.hpp
View
54 examples/test.js
@@ -1,8 +1,6 @@
var libotr = require('otr3');
console.log("libotr version:",libotr.version());
-var TEST_PASSED=false;
-
var verbose = true;//TODO: read command line param -v / --verbose
if(verbose){
@@ -26,7 +24,8 @@ var otrchan_b = new libotr.OTRChannel(bob, ALICE,{policy:libotr.POLICY("ALWAYS")
console.log("Alice's OTR fingerprint:",alice.state.fingerprint("alice@telechat.org","telechat"));
console.log("Bob's OTR fingerprint:",bob.state.fingerprint("bob@telechat.org","telechat"));
-console.log(ALICE.accountname,"<==>",BOB.accountname);
+console.log(alice.accounts());
+console.log(bob.accounts());
//dump the OTR channel objects
console.log(otrchan_a);
@@ -69,7 +68,7 @@ otrchan_b.on("shutdown",function(){
//because otrchan_b was closed otrchan_a get a remote_disconnect event.
otrchan_a.on("remote_disconnected",function(){
console.log("Bob disconnected");
- exit_test("");
+ exit_test(true);
});
//connection is encrypted..
@@ -86,31 +85,42 @@ otrchan_b.on("smp_request",function(){
this.respond_smp('s3cr37');
});
+otrchan_a.on("create_privkey",function(account,protocol){
+ console.log("Alice doesn't have a key for",account,protocol," creating one now..");
+ alice.generateKey(account,protocol,function(err){
+ if(err){
+ console.log(err);
+ }
+ console.log("Key Generation Done.");
+ //restart the OTR conversation
+ otrchan_a.connect();
+ });
+ console.log("handled create_privkey");
+});
+otrchan_b.on("create_privkey",function(account,protocol){
+ console.log("Bob doesn't have a key for",account,protocol," creating one now..");
+ bob.generateKey(account,protocol,function(err){
+ if(err){
+ console.log(err);
+ }
+ console.log("Key Generation Done.");
+ //restart the OTR conversation
+ otrchan_b.connect();
+ });
+ console.log("handled create_privkey");
+});
-//alice sends a message to bob
-otrchan_a.send("Hello, World!"); //will get reset encrypted
+
+otrchan_a.connect();
var loop = setInterval(function(){
if(otrchan_a.isEncrypted() && otrchan_a.isAuthenticated()){
- console.log("Finger print verification successful");
- dumpConnContext(otrchan_a,"Alice's ConnContext:");
- dumpConnContext(otrchan_b,"Bob's ConnContext:");
- TEST_PASSED=true;
- if(loop) clearInterval(loop);
- //otrchan_b.send("Meet me at midnight...near the docks...");
+ if(loop) clearInterval(loop);
otrchan_b.close();
}
-},500);
-
-var test_timeout = setTimeout(function(){
- if(loop) clearInterval(loop);
- exit_test("Timeout! - try again..");//usually something went wrong during SMP authentication
-},10000);
+},1000);
-function exit_test(msg){
- dumpConnContext(otrchan_a,"Alice's ConnContext:");
- dumpConnContext(otrchan_b,"Bob's ConnContext:");
- console.log(msg);
+function exit_test(TEST_PASSED){
if(TEST_PASSED){ console.log("== TEST PASSED ==\n"); } else { console.log("== TEST FAILED ==\n"); }
process.exit();
}
View
7 index.js
@@ -77,6 +77,9 @@ User.prototype.writeFingerprints = function(){
this.state.writeFingerprintsSync(this.fingerprints);
}
+User.prototype.accounts = function(){
+ return this.state.accounts();
+};
function OTRChannel(user, context, parameters){
events.EventEmitter.call(this);
@@ -87,7 +90,7 @@ function OTRChannel(user, context, parameters){
}
OTRChannel.prototype.connect = function(){
- return this.send("");
+ return this.send("?OTR?");
};
OTRChannel.prototype.send = function(message){
//message can be any object that can be serialsed to a string using it's .toString() method.
@@ -163,7 +166,7 @@ function OtrEventHandler( otrChannel ){
case "still_secure": emit(o.EVENT);break;
case "update_context_list": emit(o.EVENT);break;
case "inject_message": emit(o.EVENT,o.message);break;
- case "create_privkey": emit(o.EVENT);break;
+ case "create_privkey": emit(o.EVENT,o.accountname,o.protocol);break;
case "display_otr_message": emit(o.EVENT,o.message);break;
case "notify": emit(o.EVENT,o.title,o.primary,o.secondary);break;
case "log_message": emit(o.EVENT,o.message);break;
View
18 src/message.cc
@@ -442,7 +442,7 @@ void MessageAppOps::op_create_privkey(void *opdata, const char *accountname, con
eobj->Set(String::NewSymbol("accountname"), String::New(accountname));
eobj->Set(String::NewSymbol("protocol"), String::New(protocol));
- QueEvent(eobj,ops->ui_event_);
+ SyncEvent(eobj,ops->ui_event_);
}
int MessageAppOps::op_is_logged_in(void *opdata, const char *accountname,const char *protocol, const char *recipient){
@@ -558,7 +558,7 @@ void MessageAppOps::op_new_fingerprint(void *opdata, OtrlUserState us, const cha
otrl_privkey_hash_to_human(human, fingerprint);
eobj->Set(String::NewSymbol("fingerprint"), String::New(human));
- QueEvent(eobj,ops->ui_event_);
+ SyncEvent(eobj,ops->ui_event_);
}
void MessageAppOps::op_write_fingerprints(void *opdata){
@@ -567,7 +567,7 @@ void MessageAppOps::op_write_fingerprints(void *opdata){
Local<Object> eobj = Object::New();
eobj->Set(String::NewSymbol("EVENT"),String::New("write_fingerprints"));
- QueEvent(eobj,ops->ui_event_);
+ SyncEvent(eobj,ops->ui_event_);
}
void MessageAppOps::op_gone_secure(void *opdata, ConnContext *context){
@@ -636,6 +636,18 @@ void MessageAppOps::contextSecureStatusUpdate(void *opdata, ConnContext* context
QueEvent(eobj,ops->ui_event_);
}
+void MessageAppOps::SyncEvent(Local<Object> obj, Persistent<Function> callback){
+ HandleScope scope;
+ const unsigned argc = 1;
+ Local<Value> argv[argc] = { obj };
+ TryCatch try_catch;
+ callback->Call(Context::GetCurrent()->Global(), argc, argv);
+ if (try_catch.HasCaught()) {
+ puts(">> Node Fatal Exception <<");
+ node::FatalException(try_catch);
+ }
+ scope.Close(Undefined());
+}
void MessageAppOps::QueEvent(Local<Object> obj, Persistent<Function> callback){
EventBaton *baton = new EventBaton();
baton->request.data = baton;
View
6 src/message.hpp
@@ -73,10 +73,12 @@ class MessageAppOps : public node::ObjectWrap {
static v8::Handle<v8::Value> Respond_SMP(const v8::Arguments& args);
static void QueEvent(v8::Local<v8::Object> obj, v8::Persistent<v8::Function> callback);
- static void FireEvent(uv_work_t* req);
+ static void FireEvent(uv_work_t* req);
+ static void SyncEvent(v8::Local<v8::Object> obj, v8::Persistent<v8::Function> callback);
+
};
-struct EventBaton {
+struct EventBaton {
uv_work_t request;
v8::Persistent<v8::Function> callback;
v8::Persistent<v8::Object> event;
Please sign in to comment.
Something went wrong with that request. Please try again.