//
// CCgeepApiXmlRpcExample.cpp class
// Copyright (c) SafeLogic, 2000 - 2009
//
// Main method with example of cGeep API calls
//
// Last Updates: 
// 25 nov. 2009 18:05 Nicolas de Pomereu
//
 
// The XML-RPC Wrapper class definitions:
#include "CgeepApiXmlRpc.h"

//
// Main method to launch cGeep API calls from C++ using the XML-RPC C++ api
// 

int main(int argc, char* argv[])
{
	
    std::string tempDir;
	
    std::string theOS;
    if (getenv("OS") == NULL)
    {
        theOS = "unknown_probably_unix";
    }
    else
    {
        theOS = getenv("OS");
    }
	
    if (theOS.find("Windows") != std::string::npos)
    {
        tempDir = "c:\\TEMP\\";
    }
    else
    {
        tempDir = "/tmp/";
    }
	
    int port = 8081; // default port
    std::string host = "127.0.0.1";
	
    if (argc != 3) 
    {
        std::cout << "Usage: CgeepApiXmlRpcExample <serverHost> <serverPort>\n";            
    }
    else
    {           
        port = atoi(argv[2]);
        host = argv[1];
    }
	
    // Create CCgeepApiXmlRpc instance
    CCgeepApiXmlRpc cgeepApiXmlRpc;
	
    // Connect to the Server
    cgeepApiXmlRpc.connectToServer((char *)host.c_str(), port);
	
    // 1) Set the keyring in c:\temp
    cgeepApiXmlRpc.setKeyRingDirectory((char *)tempDir.c_str());
	
    if (!cgeepApiXmlRpc.isOperationOk())
    {
		std::cout << "Error    : " << cgeepApiXmlRpc.getErrorCode() << "\n";
		std::cout << "Exception: " << cgeepApiXmlRpc.getException() << "\n\n";
		return -1;
    }
    else
    {
        std::cout << "setKeyRingDirectory() done" << "\n";
    }
	
	
    // 2) Create two key pairs
    // (it will generate a private key ring & a public key ring in c:\\temp)
    
    // Create a key with test1@test.com as PGP UserId
    cgeepApiXmlRpc.generateKeyPair("test1@test.com", "passphrase",
		"RSA", 1024, "AES-256", "NEVER");
	
    if (!cgeepApiXmlRpc.isOperationOk())
    {
		std::cout << "Error    : " << cgeepApiXmlRpc.getErrorCode() << "\n";
		std::cout << "Exception: " << cgeepApiXmlRpc.getException() << "\n\n";
		return -1;
    }
    else
    {
        std::cout << "generateKeyPair() done" << "\n";
    }
	
	
    // Create a key with test2@test.com as PGP UserId
    cgeepApiXmlRpc.generateKeyPair("test2@test.com", "passphrase",
		"RSA", 1024, "AES-256", "NEVER");
	
    if (!cgeepApiXmlRpc.isOperationOk())
    {
		std::cout << "Error    : " << cgeepApiXmlRpc.getErrorCode() << "\n";
		std::cout << "Exception: " << cgeepApiXmlRpc.getException() << "\n\n";
		return -1;
    }
    else
    {
        std::cout << "generateKeyPair() done" << "\n";
    }
    
    // We display the key list
    std::string list = cgeepApiXmlRpc.listKeys("");
	
    if(!cgeepApiXmlRpc.isOperationOk())
    {
		std::cout << "Error    : " << cgeepApiXmlRpc.getErrorCode() << "\n";
		std::cout << "Exception: " << cgeepApiXmlRpc.getException() << "\n\n";
		return -1;
    }     
    else
    {
        std::cout << "listKeys() done" << "\n";
    }   
    
	std::cout << "listKeys: " << list << "\n";
    
    //
    // 3) Symmetric encryption & decryption
    //
    
    // encryption
    std::string fileIn   = tempDir + "text.txt";
    std::string fileOut  = tempDir + "text.txt.pgp"; // The ecnrypted file
    cgeepApiXmlRpc.encryptSymmetric((char * ) fileIn.c_str(), 
		(char * ) fileOut.c_str(), 
		"passphrase", 
		0);         
    
    if (!cgeepApiXmlRpc.isOperationOk())
    {
		std::cout << "Error    : " << cgeepApiXmlRpc.getErrorCode() << "\n";
		std::cout << "Exception: " << cgeepApiXmlRpc.getException() << "\n\n";
		return -1;
    }
    else
    {
        std::cout << "encryptSymmetric() done" << "\n";
    }
    
    // decryption
    std::string fileEncrypted   = tempDir + "text.txt.pgp";
    std::string fileDecrypted   = tempDir + "\\text_decrypted.txt"; // The decrypted file
    cgeepApiXmlRpc.decryptSymmetric((char * ) fileEncrypted.c_str(), 
		(char * ) fileDecrypted.c_str(),
		"passphrase",
		0);         
    
    if (!cgeepApiXmlRpc.isOperationOk())
    {
		std::cout << "Error    : " << cgeepApiXmlRpc.getErrorCode() << "\n";
		std::cout << "Exception: " << cgeepApiXmlRpc.getException() << "\n\n";
		return -1;
    }      
    else
    {
        std::cout << "decryptSymmetric() done" << "\n";
    }
	
    //
    // 4) Asymmetric encryption & decryption
    //        
    
    // We reset the recipients list (Cautious to be done before each encryption)
    cgeepApiXmlRpc.resetRecipientsKeys();
    
    // We add test1@test.com as first recipient
    cgeepApiXmlRpc.addRecipientKey("test1@test.com");
    
    // We add test2@test.com as second recipient
    cgeepApiXmlRpc.addRecipientKey("test2@test.com");
    
    fileIn   = tempDir + "text.txt";
    fileOut  = tempDir + "text.txt.pgp"; // The ecnrypted file    
    
    // Now we encrypt a file for these two keys / recipients
    cgeepApiXmlRpc.encrypt((char * )fileIn.c_str(), (char * )fileOut.c_str(), 0);       
    
    if (!cgeepApiXmlRpc.isOperationOk())
    {
		std::cout << "Error    : " << cgeepApiXmlRpc.getErrorCode() << "\n";
		std::cout << "Exception: " << cgeepApiXmlRpc.getException() << "\n\n";
		return -1;
    }     
    else
    {
        std::cout << "encrypt() done" << "\n";
    }
    
    // Now, we try to decrypt the encrypted file.
    // We can decrypt with test1@test.com, our first private key:
    
    cgeepApiXmlRpc.decrypt((char * )fileEncrypted.c_str(), 
		(char * )fileDecrypted.c_str(), 
		"test1@test.com", "passphrase", 
		0); 
    
    if (!