Subject: RE: Agent created throgh CAPI - Forced to edit & save to work properly
Here is the code Mr.Markus Seitz:
In this code the following Lines are not working. I think this is the culprit, that is the lotusscript attached with Agent not getting compiled. So it says ‘Invalid document’, if you open and save it gets corrected and working properly afterwards.
/* Compile the LS script to object code. */
if (error = NSFNoteLSCompile(hDb, hAgent, 0))
{
printf("Error: unable to compile Agent note to database.\n");
goto Exit1;
}
STATUS LNPUBLIC AddBackgroundAgent2( DBHANDLE hDb )
{
STATUS error = NOERROR;
NOTEHANDLE hAgent;
char szAssistFlags[DESIGN_FLAGS_MAX];
char szDesignFlags[DESIGN_FLAGS_MAX];
ODS_ASSISTSTRUCT AgentInfo;
CDACTIONHEADER AgentHeader;
CDACTIONLOTUSSCRIPT AgentScript;
BYTE *AgentAction;
BYTE *buff_ptr;
DWORD dwBuffLen;
char *pFormattedLS;
int FormattedLSLen;
int sourceAllocated=0;
int destAllocated=0;
int errorBufferAllocated=0;
HANDLE hSource=NULL;
HANDLE hDest=NULL;
HANDLE hErrorBuffer=NULL;
HANDLE hUnused=NULL;
static char *szTitle = AGENT_BACKGROUND2;
static char *szComment =
"Send selected Mails";
static char *szScript =
"Sub Initialize\n\
Dim sess As New NotesSession\n\
Dim maildb As NotesDatabase\n\
Set Maildb = sess.CurrentDatabase\n\
Messagebox maildb.MaxSize\n\
Dim i As NotesDocument\n\
Set i = maildb.AllDocuments.GetLastDocument()\n\
Messagebox i.Created\n\
Messagebox i.NotesURL\n\
Messagebox i.GetItemValue(\"Body\")(0)\n\
Messagebox i.GetItemValue(\"Subject\")(0)\n\
Messagebox i.GetItemValue(\"SendTo\")(0)\n\
Messagebox i.GetItemValue(\"PostedDate\")(0)\n\
Dim mobj\n\
Dim m As String\n\
Set mobj = CreateObject(\"lnotes_mail.mailclasslib\")\n\
m = mobj.WriteMailToFile(i.GetItemValue(\"Subject\")(0), i.GetItemValue(\"SendTo\")(0), i.GetItemValue(\"Body\")(0))\n\
Messagebox \"New mail has arrived\"\n\
End Sub\n";
/* Create Agent note and set note class to
* NOTE_CLASS_PRIVATE | NOTE_CLASS_FILTER
*/
if (error = CreateAgentNote(hDb, &hAgent, SHARED_AGENT))
goto Exit0;
/* $Title */
if (error = SetAgentTitle(hAgent, szTitle))
goto Exit1;
/* $Comment */
if (error = SetAgentComment(hAgent, szComment))
goto Exit1;
/* $Flags */
szDesignFlags[0] = DESIGN_FLAG_V4AGENT;
szDesignFlags[1] = DESIGN_FLAG_LOTUSSCRIPT_AGENT;
szDesignFlags[2] = DESIGN_FLAG_HIDE_FROM_V3;
szDesignFlags[3] = DESIGN_FLAG_NEEDSREFRESH;
szDesignFlags[4] = '\0';
if (error = SetAgentDesignFlags(hAgent, szDesignFlags))
goto Exit1;
/* $AssistVersion */
if (error = SetAgentVersion(hAgent))
goto Exit1;
/* $AssistType */
if (error = SetAgentType(hAgent, SIG_ACTION_LOTUSSCRIPT))
goto Exit1;
/* $AssistLastRun, $AssistDocCount */
if (error = SetAgentLastInfo(hAgent))
goto Exit1;
/* $AssistFlags */
szAssistFlags[0] = ASSIST_FLAG_ENABLED;
szAssistFlags[1] = ASSIST_FLAG_NEWCOPY;
szAssistFlags[2] = '\0';
if (error = SetAgentAssistFlags(hAgent, szAssistFlags))
goto Exit1;
/* $AssistTrigger */
if (error = SetAgentTrigger(hAgent,
ASSISTTRIGGER_TYPE_MANUAL))
goto Exit1;
/* $AssistInfo */
(void) memset (&AgentInfo, 0, sizeof(ODS_ASSISTSTRUCT));
AgentInfo.wVersion = (WORD) 0;
AgentInfo.wTriggerType = (WORD) ASSISTTRIGGER_TYPE_MANUAL;
AgentInfo.wSearchType = (WORD) ASSISTSEARCH_TYPE_MODIFIED;
AgentInfo.dwFlags = (WORD)0; /* explicit search criteria not necessary */
if (error = SetAgentInfo(hAgent, AgentInfo))
goto Exit1;
/* Following steps will convert raw Lotus Script to a format
that IDE uses when rendering the script. */
/*** Be sure to allocate enough memory for the script and the
null terminator. */
if (error = OSMemAlloc(0,strlen(szScript)+1,&hSource))
{
printf("Error: unable to alloc hSource.\n");
goto Exit1;
}
sourceAllocated=1;
/*** Copy the raw Lotus Script into the newly allocated memory
space. */
pFormattedLS=OSLock(char,hSource);
strcpy(pFormattedLS,szScript);
OSUnlock(hSource);
/*** Convert the raw Lotus Script to IDE compliant format. */
error = AgentLSTextFormat(hSource,&hDest, &hErrorBuffer,0,&hUnused);
/*** Free hSource handle, and update memory allocation flags for
hSource, hDest and hErrorBuffer handles. */
OSMemFree(hSource);
sourceAllocated=0;
destAllocated=1;
errorBufferAllocated=1;
if (error )
{
printf("Error: Error in AgentLSTextFormat.\n");
goto Exit1;
}
/*** If any script error, retrieve the error text from hErrorBuffer
handle; otherwise, retrieve the IDE compliant script. */
if (hErrorBuffer)
{
pFormattedLS=OSLock(char,hErrorBuffer);
printf("\nError from AgentLSTextFormat: %s\n",pFormattedLS);
OSUnlock(hErrorBuffer);
error=1;
goto Exit1;
}
else if (hDest)
{
OSMemFree(hErrorBuffer);
errorBufferAllocated=0;
pFormattedLS=OSLock(char,hDest);
/*** When saving the formatted LS in the "$AssistAction" item,
the script should be ended with a null terminator, therefore,
we'll add one to the strlen(pFormattedLS) and save it in the
FormattedLSLen variable. */
FormattedLSLen=strlen(pFormattedLS)+1;
}
/* $AssistQuery and $AssistAction */
/* Use ODS routines to build CDACTIONHEADER and CDACTIONFORMULA buffer */
AgentAction = (BYTE *) malloc( ODSLength (_CDACTIONHEADER) +
ODSLength (_CDACTIONLOTUSSCRIPT) +
FormattedLSLen);
if (AgentAction == (BYTE *) NULL)
{
printf("Error: unable to allocate memory.\n");
goto Exit1;
}
buff_ptr = AgentAction;
AgentHeader.Header.Signature = SIG_ACTION_HEADER;
AgentHeader.Header.Length = (BYTE) ODSLength(_CDACTIONHEADER);
ODSWriteMemory( &buff_ptr, _CDACTIONHEADER, &AgentHeader, 1 );
AgentScript.Header.Signature = SIG_ACTION_LOTUSSCRIPT;
AgentScript.Header.Length = ODSLength(_CDACTIONLOTUSSCRIPT) +
FormattedLSLen;
AgentScript.dwFlags = (DWORD) 0;
AgentScript.dwScriptLen = (DWORD) FormattedLSLen;
ODSWriteMemory( &buff_ptr, _CDACTIONLOTUSSCRIPT, &AgentScript, 1 );
memcpy( (char *)buff_ptr, pFormattedLS, (WORD)AgentScript.dwScriptLen);
buff_ptr += (WORD) AgentScript.dwScriptLen;
dwBuffLen = (DWORD)(buff_ptr - AgentAction);
OSUnlock(hDest);
OSMemFree(hDest);
destAllocated=0;
error = SetAgentAction(hAgent, AgentAction, dwBuffLen);
free (AgentAction);
if (error)
goto Exit1;
/* $AssistRunInfo */
if (error = SetAgentRunInfo(hDb, hAgent, SHARED_AGENT))
goto Exit1;
/* $Signature */
if (error = NSFNoteSign(hAgent))
goto Exit1;
/* Update note */
if (error = NSFNoteUpdate(hAgent, 0))
{
printf("Error: unable to update background Agent note to database.\n");
goto Exit1;
}
/* Compile the LS script to object code. */
if (error = NSFNoteLSCompile(hDb, hAgent, 0))
{
printf("Error: unable to compile Agent note to database.\n");
goto Exit1;
}
/* Finally update note */
if (error = NSFNoteUpdate(hAgent, 1))
{
printf("Error: unable to do final update on the Agent note to database.\n");
goto Exit1;
}
Exit1:
if (sourceAllocated)
OSMemFree(hSource);
if (destAllocated)
OSMemFree(hDest);
if (errorBufferAllocated)
OSMemFree(hErrorBuffer);
NSFNoteClose(hAgent);
Exit0:
return (error);
}