diff --git a/index.js b/index.js index 1ebb6e0..b66f788 100644 --- a/index.js +++ b/index.js @@ -75,6 +75,9 @@ User.prototype.ConnContext = function(accountname, protocol, recipient){ User.prototype.writeFingerprints = function(){ this.state.writeFingerprintsSync(this.fingerprints); } +User.prototype.writeTrustedFingerprints = function(){ + this.state.writeTrustedFingerprintsSync(this.fingerprints); +} User.prototype.accounts = function(){ return this.state.accounts(); diff --git a/src/otr.hpp b/src/otr.hpp index 476167b..888adb4 100644 --- a/src/otr.hpp +++ b/src/otr.hpp @@ -23,6 +23,8 @@ #include #include #include +#include +#include #include #include "userstate.hpp" diff --git a/src/userstate.cc b/src/userstate.cc index 6855835..e332c8a 100644 --- a/src/userstate.cc +++ b/src/userstate.cc @@ -33,7 +33,7 @@ UserState::UserState(OtrlUserState userstate) : ObjectWrap(), userstate_(userstate) {}; UserState::~UserState(){ - if(!reference){ + if(!reference){ if(userstate_!=NULL) { otrl_userstate_free(userstate_); } @@ -46,24 +46,25 @@ void UserState::Init(Handle target) { // Prepare constructor template Local tpl = FunctionTemplate::New(New); Local name = String::NewSymbol("UserState"); - + constructor = Persistent::New(tpl); // ObjectWrap uses the first internal field to store the wrapped pointer. constructor->InstanceTemplate()->SetInternalFieldCount(1); - constructor->SetClassName(name); - + constructor->SetClassName(name); + NODE_SET_PROTOTYPE_METHOD(constructor, "generateKey",Generate_Key); NODE_SET_PROTOTYPE_METHOD(constructor, "readKeys",Read_Keys); NODE_SET_PROTOTYPE_METHOD(constructor, "readFingerprints",Read_Fingerprints); NODE_SET_PROTOTYPE_METHOD(constructor, "writeFingerprints",Write_Fingerprints); - - NODE_SET_PROTOTYPE_METHOD(constructor, "fingerprint",Fingerprint); + + NODE_SET_PROTOTYPE_METHOD(constructor, "fingerprint",GetFingerprint); NODE_SET_PROTOTYPE_METHOD(constructor, "accounts",Accounts); NODE_SET_PROTOTYPE_METHOD(constructor, "readKeysSync",Read_Keys_Sync); NODE_SET_PROTOTYPE_METHOD(constructor, "readFingerprintsSync",Read_Fingerprints_Sync); NODE_SET_PROTOTYPE_METHOD(constructor, "writeFingerprintsSync",Write_Fingerprints_Sync); + NODE_SET_PROTOTYPE_METHOD(constructor, "writeTrustedFingerprintsSync",Write_Fingerprints_Sync); NODE_SET_PROTOTYPE_METHOD(constructor, "free",Free); - + target->Set(name, constructor->GetFunction()); } @@ -93,7 +94,7 @@ Handle UserState::WrapUserState(OtrlUserState userstate) return o; } -Handle UserState::Fingerprint(const Arguments& args) { +Handle UserState::GetFingerprint(const Arguments& args) { HandleScope scope; UserState* obj = ObjectWrap::Unwrap(args.This()); @@ -276,6 +277,49 @@ void UserState::Worker_Write_Fingerprints(uv_work_t* req){ baton->error = otrl_privkey_write_fingerprints(baton->userstate, baton->arg0.c_str()); } +Handle UserState::Write_Trusted_Fingerprints_Sync(const Arguments& args) { + HandleScope scope; + UserState* obj = ObjectWrap::Unwrap(args.This()); + OtrlUserState us = obj->userstate_; + FILE *storef; + ConnContext *context; + Fingerprint *fingerprint; + gcry_error_t error; + + if(!args.Length() > 0 || !args[0]->IsString()){ + return scope.Close(V8EXCEPTION("Invalid arguments. One argument 'filename' (string) excpected.")); + } + String::Utf8Value filename(args[0]->ToString()); + + storef = fopen(*filename, "wb"); + + if (!storef) { + error = gcry_error_from_errno(errno); + }else{ + + for(context = us->context_root; context; context = context->next) { + + /* Don't bother with the first (fingerprintless) entry. */ + for (fingerprint = context->fingerprint_root.next; fingerprint && fingerprint->trust; + fingerprint = fingerprint->next) { + int i; + fprintf(storef, "%s\t%s\t%s\t", context->username, + context->accountname, context->protocol); + for(i=0;i<20;++i) { + fprintf(storef, "%02x", fingerprint->fingerprint[i]); + } + fprintf(storef, "\t%s\n", fingerprint->trust ? fingerprint->trust : ""); + } + } + + error = gcry_error(GPG_ERR_NO_ERROR); + fclose(storef); + } + + if(error) return scope.Close(GCRY_EXCEPTION(error)); + return scope.Close(Undefined()); +} + Handle UserState::Generate_Key(const Arguments& args) { HandleScope scope; UserState* obj = ObjectWrap::Unwrap(args.This()); diff --git a/src/userstate.hpp b/src/userstate.hpp index aca7294..4a609e0 100644 --- a/src/userstate.hpp +++ b/src/userstate.hpp @@ -38,10 +38,10 @@ class UserState : public node::ObjectWrap { friend class ConnectionCtx; OtrlUserState userstate_; bool reference; - + UserState(OtrlUserState userstate); ~UserState(); - + static v8::Handle New(const v8::Arguments& args); static v8::Handle WrapUserState(OtrlUserState userstate); static v8::Handle Destroy(const v8::Arguments &args); @@ -51,24 +51,25 @@ class UserState : public node::ObjectWrap { static v8::Handle Read_Fingerprints(const v8::Arguments& args); static v8::Handle Write_Fingerprints(const v8::Arguments& args); //Sync - static v8::Handle Fingerprint(const v8::Arguments& args); + static v8::Handle GetFingerprint(const v8::Arguments& args); static v8::Handle Accounts(const v8::Arguments& args); - static v8::Handle Get_Key(const v8::Arguments& args); + static v8::Handle Get_Key(const v8::Arguments& args); static v8::Handle Read_Keys_Sync(const v8::Arguments& args); static v8::Handle Read_Fingerprints_Sync(const v8::Arguments& args); - static v8::Handle Write_Fingerprints_Sync(const v8::Arguments& args); + static v8::Handle Write_Fingerprints_Sync(const v8::Arguments& args); + static v8::Handle Write_Trusted_Fingerprints_Sync(const v8::Arguments& args); static v8::Handle Free(const v8::Arguments& args); //Workers static void Worker_Generate_Key (uv_work_t* req); static void Worker_Read_Keys (uv_work_t* req); static void Worker_Read_Fingerprints (uv_work_t* req); static void Worker_Write_Fingerprints (uv_work_t* req); - static void Worker_After (uv_work_t* req); - + static void Worker_After (uv_work_t* req); + }; //information about the asynchronous "work request". -struct Baton { +struct Baton { uv_work_t request; bool hasCallback; v8::Persistent callback;