diff --git a/src/actions.cpp b/src/actions.cpp index bff4558..ab81a82 100644 --- a/src/actions.cpp +++ b/src/actions.cpp @@ -22,6 +22,8 @@ #include "generator.h" #include "str_match.h" +#include + int action_gen_key (const std::string& algspec, const std::string&name, keyring&KR, algorithm_suite&AS) { @@ -204,12 +206,49 @@ int action_export (bool armor, const std::string&filter, const std::string&name, keyring&KR) { + //TODO return 0; } int action_delete (bool yes, const std::string&filter, keyring&KR) { + int kc = 0; + for (keyring::pubkey_storage::iterator + i = KR.pubs.begin(), e = KR.pubs.end(); + i != e; ++i) { + if (keyspec_matches (filter, i->second.name, i->first) ) + ++kc; + } + if (!kc) { + err ("no such key"); + return 0; + } + if (kc > 1 && !yes) { + bool okay = false; + ask_for_yes (okay, "This will delete " << kc + << " pubkeys from your keyring. Continue?"); + if (!okay) return 0; + } + + //all clear, delete them + std::list todel; + for (keyring::pubkey_storage::iterator + i = KR.pubs.begin(), e = KR.pubs.end(); + i != e; ++i) { + if (keyspec_matches (filter, i->second.name, i->first) ) + todel.push_back (i); + } + + for (std::list::iterator + i = todel.begin(), e = todel.end(); i != e; ++i) + KR.pubs.erase (*i); + + if (!KR.save() ) { + err ("error: couldn't save keyring"); + return 1; + } + return 0; } @@ -252,12 +291,49 @@ int action_export_sec (bool armor, const std::string&filter, const std::string&name, keyring&KR) { + //TODO return 0; } int action_delete_sec (bool yes, const std::string&filter, keyring&KR) { + int kc = 0; + for (keyring::keypair_storage::iterator + i = KR.pairs.begin(), e = KR.pairs.end(); + i != e; ++i) { + if (keyspec_matches (filter, i->second.pub.name, i->first) ) + ++kc; + } + if (!kc) { + err ("no such key"); + return 0; + } + if (!yes) { + bool okay = false; + ask_for_yes (okay, "This will delete " << kc + << " secrets from your keyring. Continue?"); + if (!okay) return 0; + } + + //all clear, delete them + std::list todel; + for (keyring::keypair_storage::iterator + i = KR.pairs.begin(), e = KR.pairs.end(); + i != e; ++i) { + if (keyspec_matches (filter, i->second.pub.name, i->first) ) + todel.push_back (i); + } + + for (std::list::iterator + i = todel.begin(), e = todel.end(); i != e; ++i) + KR.pairs.erase (*i); + + if (!KR.save() ) { + err ("error: couldn't save keyring"); + return 1; + } + return 0; } diff --git a/src/iohelpers.h b/src/iohelpers.h index fe3939b..e8dbc9d 100644 --- a/src/iohelpers.h +++ b/src/iohelpers.h @@ -32,7 +32,10 @@ #define err(x) std::cerr << x << std::endl #define erreol std::cerr << std::endl #define progerr(x) std::cerr << argv[0] << ": " << x << std::endl -#define in(x) std::cin >> x; + +#define ask_for_yes(ok,x) do {std::cout << x << " (y/n): "; \ + std::string answer; std::cin >> answer; \ + ok=(answer=="y");} while(0) bool redirect_cin (const std::string& fn); bool redirect_cout (const std::string& fn);