#include "libprivatebin.h" #include #include #include #include #include #include // Helper function to wait between API calls to avoid rate limiting void wait_between_calls() { std::cout << "[privatebinapi] Waiting 12 seconds to avoid rate limiting..." << std::endl; std::this_thread::sleep_for(std::chrono::seconds(12)); } int main(int argc, char* argv[]) { std::cout << "PrivateBin API C++ DLL Example" << std::endl; std::cout << "===============================" << std::endl; // Check if file upload mode is requested if (argc >= 3 && strcmp(argv[1], "--upload") == 0) { // File upload mode if (argc < 4) { std::cout << "Usage: " << argv[0] << " --upload [password] [expiration] [burn_after_reading] [open_discussion]" << std::endl; std::cout << "Example: " << argv[0] << " --upload https://privatebin.net test.txt mypassword 1day 0 0" << std::endl; std::cout << std::endl; std::cout << "Parameters:" << std::endl; std::cout << " server_url - URL of the PrivateBin server" << std::endl; std::cout << " file_path - Path to the file to upload" << std::endl; std::cout << " password - Optional: Password for the paste (default: none)" << std::endl; std::cout << " expiration - Optional: Expiration time (default: 1day)" << std::endl; std::cout << " burn_after_reading - Optional: Burn after reading (0=no, 1=yes, default: 0)" << std::endl; std::cout << " open_discussion - Optional: Allow discussion (0=no, 1=yes, default: 0)" << std::endl; return 1; } const char* server_url = argv[2]; const char* file_path = argv[3]; const char* password = (argc > 4) ? argv[4] : nullptr; const char* expiration = (argc > 5) ? argv[5] : "1day"; int burn_after_reading = (argc > 6) ? std::atoi(argv[6]) : 0; int open_discussion = (argc > 7) ? std::atoi(argv[7]) : 0; std::cout << "PrivateBin File Upload Example" << std::endl; std::cout << "===============================" << std::endl; std::cout << "Server: " << server_url << std::endl; std::cout << "File: " << file_path << std::endl; if (password) { std::cout << "Password: " << password << std::endl; } std::cout << "Expiration: " << expiration << std::endl; std::cout << "Burn after reading: " << (burn_after_reading ? "Yes" : "No") << std::endl; std::cout << "Allow discussion: " << (open_discussion ? "Yes" : "No") << std::endl; std::cout << std::endl; char* paste_url = nullptr; char* delete_token = nullptr; std::cout << "Uploading file..." << std::endl; int result = upload_file(server_url, file_path, password, expiration, burn_after_reading, open_discussion, &paste_url, &delete_token); if (result == 0) { std::cout << "Successfully uploaded!" << std::endl; std::cout << "Paste URL: " << paste_url << std::endl; std::cout << "Delete Token: " << delete_token << std::endl; std::cout << std::endl; std::cout << "Note: Keep the delete token safe to delete the paste later." << std::endl; // Free memory free_string(paste_url); free_string(delete_token); } else { std::cout << "Error uploading file!" << std::endl; std::cout << "Error code: " << result << std::endl; switch (result) { case 1: std::cout << "Error: Network problem" << std::endl; break; case 2: std::cout << "Error: Cryptographic error" << std::endl; break; case 3: std::cout << "Error: Invalid input (e.g., file not found or too large)" << std::endl; break; case 4: std::cout << "Error: Server error" << std::endl; break; case 5: std::cout << "Error: JSON parsing error" << std::endl; break; default: std::cout << "Error: Unknown error" << std::endl; break; } } return (result == 0) ? 0 : 1; } else { // Interactive mode - show usage and run basic example std::cout << "Usage:" << std::endl; std::cout << " " << argv[0] << " - Run basic example" << std::endl; std::cout << " " << argv[0] << " --upload - Upload a file" << std::endl; std::cout << std::endl; // Example of how to use the API with different formats std::cout << "Testing different text formats..." << std::endl; // Test 1: Plain text format std::cout << "\n1. Testing plaintext format..." << std::endl; char* paste_url = nullptr; char* delete_token = nullptr; int result = create_paste( "https://privatebin.medisoftware.org", // Server URL "Hello, PrivateBin from C++ Library!\nThis is plain text.", // Content nullptr, // No password "1hour", // Expire in 1 hour "plaintext", // Plain text format 0, // Don't burn after reading 0, // No discussion &paste_url, // Output: paste URL &delete_token // Output: delete token ); if (result == 0) { std::cout << "✓ Plaintext paste created successfully!" << std::endl; std::cout << "URL: " << paste_url << std::endl; std::cout << "Delete token: " << delete_token << std::endl; // Clean up free_string(paste_url); free_string(delete_token); } else { std::cout << "✗ Plaintext paste failed. Error code: " << result << std::endl; } // Wait between API calls to avoid rate limiting wait_between_calls(); // Test 2: Syntax highlighting format std::cout << "\n2. Testing syntax highlighting format..." << std::endl; char* code_paste_url = nullptr; char* code_delete_token = nullptr; const char* code_content = R"( #include int main() { std::cout << "Hello, World!" << std::endl; return 0; } )"; result = create_paste( "https://privatebin.medisoftware.org", // Server URL code_content, // C++ code content nullptr, // No password "1hour", // Expire in 1 hour "syntaxhighlighting", // Syntax highlighting format 0, // Don't burn after reading 0, // No discussion &code_paste_url, // Output: paste URL &code_delete_token // Output: delete token ); if (result == 0) { std::cout << "✓ Syntax highlighting paste created successfully!" << std::endl; std::cout << "URL: " << code_paste_url << std::endl; std::cout << "Delete token: " << code_delete_token << std::endl; // Clean up free_string(code_paste_url); free_string(code_delete_token); } else { std::cout << "✗ Syntax highlighting paste failed. Error code: " << result << std::endl; } // Wait between API calls to avoid rate limiting wait_between_calls(); // Test 3: Markdown format std::cout << "\n3. Testing markdown format..." << std::endl; char* markdown_paste_url = nullptr; char* markdown_delete_token = nullptr; const char* markdown_content = R"( # PrivateBin API C++ Library ## Features - **Text Paste Creation**: Create encrypted text pastes - **File Upload**: Upload files as encrypted pastes - **Multiple Formats**: Support for plaintext, syntax highlighting, and markdown ## Code Example ```cpp int result = create_paste(server_url, content, password, expiration, format, burn_after_reading, open_discussion, &paste_url, &delete_token); ``` > This is a blockquote demonstrating markdown support. )"; result = create_paste( "https://privatebin.medisoftware.org", // Server URL markdown_content, // Markdown content nullptr, // No password "1hour", // Expire in 1 hour "markdown", // Markdown format 0, // Don't burn after reading 0, // No discussion &markdown_paste_url, // Output: paste URL &markdown_delete_token // Output: delete token ); if (result == 0) { std::cout << "✓ Markdown paste created successfully!" << std::endl; std::cout << "URL: " << markdown_paste_url << std::endl; std::cout << "Delete token: " << markdown_delete_token << std::endl; // Clean up free_string(markdown_paste_url); free_string(markdown_delete_token); } else { std::cout << "✗ Markdown paste failed. Error code: " << result << std::endl; } // Wait between API calls to avoid rate limiting wait_between_calls(); // Test 4: Interactive format selection std::cout << "\n4. Interactive format selection..." << std::endl; char* interactive_paste_url = nullptr; char* interactive_delete_token = nullptr; std::cout << "Available formats:" << std::endl; std::cout << " - plaintext (default)" << std::endl; std::cout << " - syntaxhighlighting" << std::endl; std::cout << " - markdown" << std::endl; const char* interactive_content = "This paste demonstrates the interactive format selection feature."; result = create_paste( "https://privatebin.medisoftware.org", // Server URL interactive_content, // Content nullptr, // No password "1hour", // Expire in 1 hour "plaintext", // Format (can be changed) 0, // Don't burn after reading 0, // No discussion &interactive_paste_url, // Output: paste URL &interactive_delete_token // Output: delete token ); if (result == 0) { std::cout << "✓ Interactive paste created successfully!" << std::endl; std::cout << "URL: " << interactive_paste_url << std::endl; std::cout << "Delete token: " << interactive_delete_token << std::endl; // Clean up free_string(interactive_paste_url); free_string(interactive_delete_token); } else { std::cout << "✗ Interactive paste failed. Error code: " << result << std::endl; } std::cout << "\nAll format tests completed!" << std::endl; return 0; std::cout << "create_paste returned: " << result << std::endl; if (result == 0) { std::cout << "Paste created successfully!" << std::endl; std::cout << "URL: " << paste_url << std::endl; std::cout << "Delete token: " << delete_token << std::endl; // Parse paste_id and key from URL (format: "/?{pasteID}#{key}") std::string full_url = paste_url ? paste_url : ""; std::string paste_id; std::string key; auto qpos = full_url.find('?'); auto hpos = full_url.find('#'); if (qpos != std::string::npos) { if (hpos != std::string::npos && hpos > qpos + 1) { paste_id = full_url.substr(qpos + 1, hpos - (qpos + 1)); key = full_url.substr(hpos + 1); } else if (qpos + 1 < full_url.size()) { paste_id = full_url.substr(qpos + 1); } } // Try to fetch paste content back if (!paste_id.empty() && !key.empty()) { std::cout << "Fetching paste..." << std::endl; char* content = nullptr; int gr = get_paste("https://privatebin.medisoftware.org", paste_id.c_str(), key.c_str(), &content); std::cout << "get_paste returned: " << gr << std::endl; if (gr == 0 && content) { std::cout << "Content: " << content << std::endl; free_string(content); } } // Try to delete paste if (!paste_id.empty() && delete_token) { std::cout << "Deleting paste..." << std::endl; int dr = delete_paste("https://privatebin.medisoftware.org", paste_id.c_str(), delete_token); std::cout << "delete_paste returned: " << dr << std::endl; } // Clean up allocated memory free_string(paste_url); free_string(delete_token); } else { std::cout << "Failed to create paste. Error code: " << result << std::endl; // Print error codes explanation switch(result) { case 1: std::cout << "Network error occurred." << std::endl; break; case 2: std::cout << "Encryption/decryption error occurred." << std::endl; break; case 3: std::cout << "Invalid input provided." << std::endl; break; case 4: std::cout << "Server error occurred." << std::endl; break; case 5: std::cout << "JSON parsing error occurred." << std::endl; break; default: std::cout << "Unknown error occurred." << std::endl; break; } } std::cout << "Example completed." << std::endl; return result; } }