📄 mail_doc.cpp
字号:
//===========================================================================
//
// Module: MAIL_DOC.CPP
//
// Syntax: mail_doc [<dbname> <server> <"search string"> <user name>]
//
// Note: All arguments are optional, you will be prompted and
// provided with working defaults (except for user name).
//
// Description:
// Sample C++ API program that searches the documents in a Notes
// database for a specified string and, once found, sends the
// document as a mail message to a specified user.
// Note:
// This program is coded to search for must the specified string
// which must be contained in a rich text 'Body' item of a
// document.
//
//===========================================================================
#ifndef MAIL_DOC_HPP
#include "mail_doc.hpp"
#endif
// Input parameter strings.
IOParameter CommandBuf;
IOParameter PromptString1;
IOParameter ParamString1;
IOParameter PromptString2;
IOParameter ParamString2;
IOParameter PromptString3;
IOParameter ParamString3;
IOParameter PromptString4;
IOParameter ParamString4;
//---------------------------------------------------------------------------
//
// MAIN
//
//---------------------------------------------------------------------------
int main( int argc, char *argv[] )
{
int ProgramStatus = 0;
LNNotesSession Session;
// Begin TRY block.
// Throw all errors encountered during command execution.
LNSetThrowAllErrors (TRUE);
try
{
LNDatabase DocDB;
LNDocumentArray DocArray;
LNString DatabasePath;
LNString ServerPath;
LNString SearchStr;
LNString UserName;
LNString SearchFormula;
LNINT Index;
LNINT Count;
// Initialize the API.
Session.Init (argc, argv);
// Parse the argument list.
ProcessArguments (argc, argv, &DatabasePath, &ServerPath, &SearchStr, &UserName);
// Get the specified Notes database.
Session.GetDatabase(DatabasePath, &DocDB, ServerPath);
// Open the database.
DocDB.Open();
cout << "Scanning database: " << DatabasePath << endl;
// Get an array of all the docs in the database.
DocDB.GetDocuments( &DocArray );
LNBOOL isDocFound = FALSE;
Count = DocArray.GetCount();
for (Index=0; Index<Count; Index++)
{
LNDocument CurDoc;
// Get the next document in the array.
CurDoc = DocArray[Index];
// Open it.
CurDoc.Open( LNNOTEOPENFLAGS_DEFAULT );
if ( CurDoc.HasItem("Body") )
{
// This Document has a Body field, so search for the string in it.
if ( SearchDocument( CurDoc, "Body", SearchStr ) )
{
// String was found; e-mail the current document to username.
CurDoc.SendTo( UserName, TRUE );
cout << "Sent Document to: " << UserName << endl;
isDocFound = TRUE;
}
}
CurDoc.Close();
}
if ( !isDocFound )
cout << "Search String was not found" << endl;
// Close the database.
DocDB.Close();
ProgramStatus = 0;
} // END try block.
catch (LNSTATUS lnerror)
{
char ErrorBuf[LNERROR_MESSAGE_LENGTH];
LNGetErrorMessage(lnerror, ErrorBuf);
cout << "\nError: " << ErrorBuf << endl;
ProgramStatus = 2;
}
catch (const char *pErrorMessage)
{
cout << "\nError: " << pErrorMessage << endl;
ProgramStatus = 1;
}
cout << endl;
cout << "Hit Return To Exit: ";
cin >> CommandBuf;
// Terminate the API
Session.Term();
return (ProgramStatus);
} // END MAIN
//---------------------------------------------------------------------------
//
// Name:
// SearchDocument
//
// Description:
// Searches stylized text elements in a document's rich text fields
// for the presence of a search string and returns TRUE if found.
//---------------------------------------------------------------------------
LNBOOL SearchDocument( const LNDocument &Doc,
const LNString &FieldName,
const LNString &SearchStr )
{
LNRichText RT;
LNRTCursor Cursor;
// Get the document's Body field as a rich text item.
Doc.GetItem( FieldName, &RT );
// Get the position of the very beginning of the RT field.
RT.GetCursor( &Cursor );
// Find the first stylized text in the rich text.
if ( Cursor.GotoFirst(SearchStr) == LNWARN_NOT_FOUND )
return FALSE;
return TRUE;
} // END SearchDocument()
//---------------------------------------------------------------------------
//
// Name:
// ProcessArguments
//
// Description:
// Scan the input command line and sort out the input strings.
// If no arguments were supplied, prompt for them.
//
// Throws exceptions:
// char * Argument error description
//
//---------------------------------------------------------------------------
void ProcessArguments ( int argc,
char *argv[],
LNString *DatabasePath,
LNString *ServerPath,
LNString *SearchStr,
LNString *UserName )
{
LNBOOL Prompt = FALSE;
if (argc != 5)
Prompt = TRUE;
// Set up the default parameters first.
InitDefaultParams();
*DatabasePath = ParamString1;
*ServerPath = ParamString2;
*SearchStr = ParamString3;
*UserName = ParamString4;
// Check the command line.
if (Prompt)
{
// Get user input data.
GetArguments();
*DatabasePath = ParamString1;
*ServerPath = ParamString2;
*SearchStr = ParamString3;
*UserName = ParamString4;
}
else
{
// Get info from command line.
*DatabasePath = argv[1];
*ServerPath = argv[2];
*SearchStr = argv[3];
*UserName = argv[4];
}
} // END ProcessArguments()
//---------------------------------------------------------------------------
//
// Name:
// InitDefaultParams
//
// Description:
// Set up all the default parameter strings and numbers for user
// input. Called by ProcessArguments() if no user input params
// were provided.
//
// NOTE:
//
// You only need to edit this function and ProcessArguments()
// when creating a new sample. The rest of the user interface
// functions below are called by ProcessArguments() and are
// generic, which means that they can stay the same unless
// you modify the number of parameters to use. If you do modify
// the number of parameters used, you will need to modify the
// switch statements.
//---------------------------------------------------------------------------
void InitDefaultParams()
{
// Initialize default parameter strings.
PromptString1 = "NSF file path name : [";
ParamString1 = "dumpme.nsf";
PromptString2 = "Server path name : [";
ParamString2 = "";
PromptString3 = "Search string : [";
ParamString3 = "I think";
PromptString4 = "User name : [";
ParamString4 = "UserName@SomeDomain";
} // END InitDefaultParams()
//---------------------------------------------------------------------------
//
// Name:
// GetArguments
//
// Description:
// Allows the user to change any or all of the input parameter
// strings. The inputs can also be left as they are if desired.
//---------------------------------------------------------------------------
void GetArguments()
{
BOOL IsCorrectData = FALSE;
QueryArguments();
while( !IsCorrectData )
{
PrintArguments();
cout << "Are these current data settings correct? [Y] ";
cin >> CommandBuf;
switch( CommandBuf[0] )
{
case 'Y':
case 'y':
IsCorrectData = TRUE; // All done, get out now!
break;
default:
// Prompt again for anything other than a "Y" or carriage return.
if( (CommandBuf) != (const char *)"" )
QueryArguments();
else
IsCorrectData = TRUE; // All done, get out now!
} // END switch
} // END while
} // END GetArguments()
//---------------------------------------------------------------------------
//
// Name:
// PrintArguments
//
// Description:
// Prints out all of the current input parameters to use.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -