75 const std::string S_ToolName =
"zlasample";
76 const std::string S_ToolVersion =
"1.2";
85 "<redist-folder> <ticket> <product> <encryption-data> <encryption-key> <email-list-file> [<log-level>]\n"
86 " redist-folder - the folder where the DCA is installed to\n"
87 " ticket - a valid ticket\n"
88 " product - the product associated with your ticket\n"
89 " hex-encryption-data - the encryption data (as hex string) included in your license\n"
90 " encryption-key - the encryption key included in your license\n"
91 " email-list-file - file that includes the emails to classify\n"
92 " log-level - optional log-level, default = 3 (LOG_Notice)\n\n";
99 # define DCA_BINDIR "bin/Win32"
101 # define DCA_BINDIR "bin/linux"
108 #define DCA_INITDIR "init"
113 #define DCA_LOGDIR "./logs"
137 static void SetupLicense(
const std::string& ticket,
const std::string& product,
140 licenseData.
ticket = ticket;
152 static bool SetupConnectionData(
const std::string& encData,
const std::string& encKey,
155 const int iEncKey = atoi( encKey.c_str() );
176 std::cout <<
" ZLA_SPAM";
179 std::cout <<
" ZLA_HAM";
182 std::cout <<
" ZLA_NDR";
185 std::cout <<
" ZLA_UNKNOWN";
189 std::cout <<
" [UNKNOWN return from ZLA call]";
191 std::cout <<
"(" << (int)result.
result() <<
")";
194 std::cout <<
", No ZLA module";
197 std::cout <<
", DCA_ZLA_FLOW";
200 std::cout <<
", DCA_ZLA_SHINGLE_HEADERPRINT";
203 std::cout <<
", DCA_ZLA_SHINGLE_BAYES";
206 std::cout <<
", DCA_ZLA_SHINGLE_PRINT";
209 std::cout <<
", DCA_ZLA_URL";
212 std::cout <<
", DCA_ZLA_BLOCKALLOW";
215 std::cout <<
", DCA_ZLA_DBBLOCKALLOW";
218 std::cout <<
", DCA_ZLA_NDR";
222 std::cout <<
", [UNKNOWN module from ZLA call]";
224 std::cout <<
"(" << (int)result.
module() <<
")" << std::endl;
233 std::cout <<
"IBM DCA Sample: " << S_ToolName <<
" (" << S_ToolVersion <<
")" << std::endl;
242 std::cout <<
" usage:" << std::endl;
251 static void PrintDbConnectionInfo(
const DbConnection& aDbConnection )
253 std::cout <<
"Remote Database version is: " << aDbConnection.
getDatabaseVersion() << std::endl;
264 struct tm *expirationTime = localtime( &expirationDate );
266 std::cout <<
"License Info:" << std::endl;
267 std::cout <<
" DCA is " << ( aLicense.
isLicensed() ?
"licensed." :
268 "not licensed." ) << std::endl;
269 std::cout <<
" MaxUsers:" << aLicense.
getMaxUsers() <<
273 std::cout <<
" Ticket:" << aLicense.
getTicket() <<
275 std::cout <<
" Session:" << aLicense.
getSession() <<
279 std::cout <<
" Expiration Date:" << asctime( expirationTime ) <<
289 static void LoadEmailFile(
const std::string& fileName, std::vector<std::string>& emailList )
291 std::ifstream fstream( fileName.c_str(), std::ios::in );
292 if (!fstream.is_open())
return;
296 while ( std::getline(fstream, line) )
298 if( !line.empty() && line[line.length()-1] ==
'\r')
299 line.erase( line.length() - 1 );
302 emailList.push_back( line );
312 static bool LoadEmail(
const std::string& fileName, std::string& emailContents )
314 emailContents.clear();
316 FILE * file = fopen( fileName.c_str(),
"rb" );
320 fseek( file, 0, SEEK_END );
321 const int iFileLength = ftell( file );
322 fseek( file, 0, SEEK_SET );
328 char* pszBuffer =
new char[ iFileLength ];
334 const size_t iBytesRead = fread( pszBuffer, 1, iFileLength, file );
335 emailContents.append( pszBuffer, iBytesRead );
340 return !emailContents.empty();
358 void TestZlaClassification(
const std::string& aEmailListFile,
const DcaInstance& myDca,
361 size_t emailsRequested = 0;
363 std::cout <<
"Entering ZLA classification routine..." << std::endl;
367 std::vector<std::string> myEmailList;
368 LoadEmailFile( aEmailListFile, myEmailList );
370 for( std::vector<std::string>::const_iterator E = myEmailList.begin(),
371 EEnd = myEmailList.end(); E != EEnd; ++E ) {
373 const std::string& myEmailString = *E;
375 std::cout <<
" Starting ZLA classification for email '" <<
376 myEmailString <<
"'" << std::endl;
379 std::string emailContent;
380 if( !LoadEmail( myEmailString, emailContent ) ) {
381 std::cout <<
"Error loading given email file: " << myEmailString <<
", continuing with next file." << std::endl;
394 std::cerr <<
" Error from ZLA classification. Details: " << myFR.
getDescription() <<
395 " (" << myFR.
getReturnCode() <<
"). Continuing with next email." << std::endl;
399 std::cout <<
" Classification Result for email '" << myEmailString <<
"'" << std::endl;
400 PrintResult( myZlaClassificationResult );
403 std::cout <<
" Total Results: " << std::endl;
404 std::cout <<
" Emails requested:\t\t" << emailsRequested << std::endl;
406 std::cout <<
"Leaving ZLA classification routine." << std::endl;
416 std::string HexToString(
const std::string& arg )
418 if( (arg.length()%2) != 0 || arg.find(
"0x" ) != 0 )
421 const std::string cmd( arg.substr( 2 ) );
424 for( std::string::size_type i = 0; i < cmd.length(); i += 2 ) {
425 unsigned char hex[3] = { 0x00, 0x00, 0x00 };
429 sscanf( (
const char *)hex,
"%02X", &x );
430 result += (
unsigned char)x;
442 int main(
int argc,
char *argv[] )
454 std::string myRedistFolder = argv[ 1 ];
455 const std::string myTicket = argv[ 2 ];
456 const std::string myProduct = argv[ 3 ];
460 const std::string myEncData = HexToString( argv[ 4 ] );
462 const std::string myEncKey = argv[ 5 ];
463 const std::string myEmailList = argv[ 6 ];
467 G_LogLevel =
static_cast<LogLevel>( atoi( argv[7] ) );
470 if( myRedistFolder.empty() || myTicket.empty() ||
471 myProduct.empty() || myEmailList.empty() ||
472 myEncData.empty() || myEncKey.empty() ) {
478 char c = myRedistFolder[ myRedistFolder.length() - 1 ];
479 if( c !=
'/' && c !=
'\\' )
480 myRedistFolder +=
"/";
505 SetupConnectionData( myEncData, myEncKey, myDbConnectionData );
507 PrintDbConnectionInfo( myDbConnection );
514 TestZlaClassification( myEmailList, myDca, myZlaClassifier );
519 catch(
const ExDca& ex ) {
520 std::cerr <<
"DCA Exception occured. Details: " << ex.
getDescription() <<
524 catch(
const std::exception& s ) {
525 std::cerr <<
"std::exception occured. Details: " << s.what() <<
"." << std::endl;
529 std::cerr <<
"Unknown exception caught." << std::endl;
Is used to create a License object. A license first must be created with DcaInstance::createLicense t...
Exception class used in the DCA.
const DCA_ZLA_TYPE DCA_ZLA_URL
The URL module.
DCA_ZLA_RESULT result() const
Returns the result for the ZLA classification.
void InitCUrl()
Initializes libcurl. Do not use any DCA function before initializing libcurl.
std::string initDir
the directory in which the DCA init files are stored
ZLA classifier object for ZLA classification.
int getMaxSessions() const
Returns the maximum allowed sessions associated with your ticket/license.
time_t getExpirationDate() const
Returns the expiration date of the license in UTC.
std::string getDescription() const
Returns a description of the error.
static ZlaClassification create(const DcaInstance &aDcaInstance, const License &aLicense)
Initializes the ZlaClassification module.
DCA_RESULT_TYPE getReturnCode() const
Returns the last error code (if any).
static void SetupInitData(const std::string &redist_folder, InitData &initData)
Sets up the given initData by substituting the given redist_folder with DCA subdirectories.
static DCA_MODULE_ID_TYPE ID
The unique ID of the ZLA classification module.
const DCA_ZLA_TYPE DCA_NONE
Initial/no module.
std::string binDir
the directory in which the DCA binary (*.dca) files are stored
This header includes initialization/deinitialization support functions for the 3rd party libraries us...
unsigned int encryptionKey
The encryption key to be used (provided with your license)
const DCA_ZLA_TYPE DCA_ZLA_NDR
The Non-Delivery-Report module.
@ LOG_Notice
Write notices / important information to the log file.
void SetOpenSslCallbacks()
Initializes the required callbacks for OpenSSL when using HTTPS or SSL connections in a multi-threade...
Stores the connection data for a database.
void UnsetOpenSslCallbacks()
Unsets the openssl callbacks. Do not call any DCA function after you have called this function.
const DCA_ZLA_RESULT ZLA_HAM
The email data is classified as HAM.
int getMaxUsers() const
Returns the maximum allowed users associated with your ticket/license.
void DeinitCUrl()
Deinitializes libcurl. Do not call any DCA function after you have called this function.
The ZLA Classification module class.
Database connection class for a local or remote database.
const DCA_ZLA_TYPE DCA_ZLA_FLOW
The Flow Control module.
Encapsulates an email object.
#define DCA_LOGDIR
Relative directory for logfile(s).
#define DCA_INITDIR
DCA subdirectory of the DCA initialization data.
const DCA_ZLA_TYPE DCA_ZLA_DBBLOCKALLOW
The DB Block Allow module.
DCA_RESULT_TYPE getReturnCode() const
Gets the code of the error.
const DCA_ZLA_RESULT ZLA_UNKNOWN
The email data is unknown i.e. there was not enough data for classification or no ZLA match.
static void PrintToolHeader()
Prints out the name and the version of this sample.
This header includes all header files of the ZLA Classification Package.
LogLevel
This enum is used in all setLogLevel() functions to change the verbosity level of the classes.
Use a License to initialize a classification package or a toolbox package.
If you are using one or more proxy servers set up this structure and use it for e....
const DCA_ZLA_RESULT ZLA_SPAM
The email data is classified as SPAM.
bool isLicensed(DCA_MODULE_ID_TYPE id=0, bool force=false) const
Checks whether the given License is valid for the given module id.
static void PrintLicenseInfo(const License &aLicense)
Prints out the information about the provided License.
std::string ticket
The ticket as provided in the license.
This header includes all header files of the DCA Base Package.
Overall result of a ZLA classification.
bool useLocalDatabase
Set to true to connect to a local or custom database, set to false to use a remote database.
static Email create(const DcaInstance &aDcaInstance, const std::string &emailContent)
Creates an email object, used as an input parameter for ZLA classification.
Encapsulates the init and deinit of the DCA API.
std::string product
The product code used with the license.
DCA_ZLA_TYPE module() const
Returns the module type for the ZLA classification.
const DCA_ZLA_RESULT ZLA_NDR
The email data is classified as NDR (non-delivery-report)
const DCA_ZLA_TYPE DCA_ZLA_BLOCKALLOW
The Block Allow module.
FunctionResult classify(const Email &anEmail, ZlaClassificationResult &aZlaResult) const
The email classification method. The method takes an initialized Email object and returns the results...
const DbType DBT_Url
Used for DbConnection classes for URL classification.
std::string logDir
the directory in which the DCA log file should be created
static void PrintUsage(const char *name)
Prints out the syntax of the sample.
std::string getLastMessage() const
Returns the last message received from our license server or if none available the last available mes...
const DCA_ZLA_TYPE DCA_ZLA_SHINGLE_HEADERPRINT
The Shingle Header Print module.
DbType dbType
The type of the database.
std::string getDescription() const
Returns the description for the error or warning.
const DCA_ZLA_TYPE DCA_ZLA_SHINGLE_PRINT
The Shingle Print module.
DbConnectionRemoteServerData remoteServerData
If you are creating a remote database connection, this structure must be filled out with the encrypti...
std::string getTicket() const
Returns the ticket of the license as string.
#define DCA_BINDIR
DCA subdirectory of the DCA binaries.
Standard function result.
std::string getSession() const
Returns the session of the license as string.
const DCA_ZLA_TYPE DCA_ZLA_SHINGLE_BAYES
The Shingle Bayes module.
const std::string S_UsageString
Usage string, displayed if a parameter is missing.
ZlaClassifier createClassifier(const DbConnection &aDbConnection) const
Creates a ZlaClassifier used to classify Email objects.
DbConnection createDbConnection(const License &aLicense, const DbConnectionData &dbcData, const ProxySettings &proxySettings=ProxySettings(), LogLevel aLogLevel=LOG_Initial) const
Creates a DbConnection object using the given DbConnectionData.
This structure is used to initialize the DcaInstance.
std::string getDatabaseVersion() const
Returns the currently used database version.
static DcaInstance create(const InitData &initData)
Creates a DcaInstance, starts up the DCA API and initializes the required main module.
static void SetupLicense(const std::string &ticket, const std::string &product, LicenseData &licenseData)
Sets up the given licenseData by copying the given ticket and product strings.
std::string encryptionData
The encryption data to be used (provided with your license)
License createLicense(const LicenseData &licData, const ProxySettings &proxySettings=ProxySettings(), LogLevel aLogLevel=LOG_Initial) const
Creates a License object using the given LicenseData.
int main(int argc, char *argv[])
The main routine.