Files
lib-privatebin/example/example.cpp

101 lines
3.9 KiB
C++

#include "privatebinapi.h"
#include <iostream>
#include <cstring>
int main() {
std::cout << "PrivateBin API C++ DLL Example" << std::endl;
std::cout << "===============================" << std::endl;
// Example of how to use the API
char* paste_url = nullptr;
char* delete_token = nullptr;
std::cout << "Creating paste on https://privatebin.medisoftware.org..." << std::endl;
// Testing against https://privatebin.medisoftware.org
int result = create_paste(
"https://privatebin.medisoftware.org", // Server URL
"Hello, PrivateBin from C++ Library!", // 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
);
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;
}