External Modules

  MsgCore privodes External Module feature which allows you write a program invoked by email for your own special purpose. For example, you can write a program named RunMe.exe and setup a special mailbox named AddOn in your mail server. Everytime a new message come to AddOn, MsgCore will execute RunMe.exe automatically.
The Protocol The protocol between MsgCore and external program is quite simple. Everytime when the external program is invoked, MsgCore redirects the standard input stream (stdin) of the external program to the mail server. The mail server will send a sequence of strings to the external program. They're the name of the sender, the hostname of the sender, the name of reciver, the name of the reciver's hostname, and the the message. All these string are ended with a zero. For example, a message from john@company.com sent to addon@myhost.com,then the program can read these data from input stream:
j o h n \0 c o m p a n y . c o m
\0 a d d o n \0 m y h o s t . c o
m \0 [ c o n t e n t s   o f   t
h e   m e s s a g e ] . . \0    
Create an Entry Add an entry for the externam module is easy. At "Ext. Module" panel of MailCenter, please select "Add Title" from menu, and a dialog box will be shown.

Please type the name of the mailbox at "Name", and the complete path of the external program at "Path of executable". The 3rd field is the command line parameters of the program, everytime MsgCore invoke the program, these parameters will be passed to the program.

Example Program Here is an example program for external module. The program create an text file using command line parameter as its name, and write sender and receiver information into the text file. The program didn't process the message text, just skip it over.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

/*
	String[]: buffer to store input stream
	MaxChar: maximum size of the buffer
	return 0 if EOF reached
*/
int ReadStringFromSTDIN ( char String[], int MaxChar )
{
	int idx;
	int ch;

	// retrive characaters from STDIN stream //
	for ( idx = 0 ; ( ch = fgetc ( stdin ) ) != EOF ; idx++ ) 
	{
		if ( idx < ( MaxChar - 1 ) ) String[idx] = (char)ch;
		if ( ch == 0 ) break;
	}
	String[idx] = 0;

	return ( ch == EOF ) ? 1 : 0;
}

/*
	Flush a string from STDIN 
*/
void FlushSTDIN ()
{
	int ch;
	while ( ( ch = fgetc ( stdin ) ) != EOF ) if ( ch == 0 ) break;
}

void main ( int argc, char* argv[] )
{
	char FromUser[256], FromHost[256], ToUser[256], ToHost[256];
	FILE *fpLog;
	struct tm *newtime;
	time_t aclock;

	/* Read fromuser@fromhost */
	ReadStringFromSTDIN ( FromUser, 256 );
	ReadStringFromSTDIN ( FromHost, 256 );
	/* Read touser@tohost */
	ReadStringFromSTDIN ( ToUser, 256 );
	ReadStringFromSTDIN ( ToHost, 256 );
	/* Skip the body of the message */
	FlushSTDIN ();

	/* keep into a log file */
	fpLog = fopen ( argv[1], "a+t" );
	if ( !fpLog ) return;
	fseek ( fpLog, 0, SEEK_END );

	/* get current time */
	time( &aclock );
	newtime = localtime( &aclock );

	/* write to file */
	fprintf ( fpLog, "From: <%s@%s> To: <%s@%s> At %s",
	FromUser, FromHost, ToUser, ToHost, asctime ( newtime ) );

	fflush ( fpLog );
	fclose ( fpLog );
}