Messaging Server Sample Plug-in
Table of Contents | Previous | Next | Index

Messaging Server Plug-in API Guide


Chapter 4
Messaging Server Sample Plug-in

This sample plug-in DLL shows how to add a recipient to a message that goes through the server. This chapter is a tutorial that takes you step by step through constructing a plug-in using the Messaging Server Plug-in API.

The SamplePlugin plug-in performs several operations:

NOTE: If you would like to see this plug-in in action, save and build the code in "Plug- in Code." If you prefer, you can download a zip file, msplugin.zip, that contains the sample plug-in file, sample.c, and the makefile for building the sample. §
This list summarizes the plug-in operations. Each operation links to the line in the DLL that performs it.

[Top]

The SamplePlugin DLL

Include header files

Be sure to include the msg4plugins.h header file and the standard input/output and allocation header files.

#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include "msg4plugins.h"
If you do not have the Messaging Server plug-in header file yet, see "Where to Find the Messaging Server Plug-in File."

Implement the initialization function

To initialize the plug-in, call the initialization function. It is called once at MTA start-up to initialize local server data, load configuration information off the disk, declare global variables, and return an integer that indicates whether the initialization function completed. This simple version of the initialization function only performs the last of these tasks. This is one of the two function prototypes that the plug-in must export in order to load.

SAMPLE_EXPORT int SamplePluginInit(pblock *Config)
{
return 1;
}

Define the plug-in function

The plug-in function, SamplePlugin, loads the parameter block pointed to by the Config parameter and creates pointers to the input (InMessage) and output (OutMessage) messages. The output parameter represents the result of the execution of the plug-in code. This is one of the two function prototypes that the plug-in must export in order to load.

SAMPLE_EXPORT int SamplePlugin(pblock *Config,
                   Message **ppInMsg,
                   Message ***pppOutMsg)
The plug-in code returns either MSG_CONTINUE or MSG_NOACTION to Messaging Server. MSG_CONTINUE indicates that the plug-in worked; message can go on to Messaging Server for further processing. MSG_NOACTION indicates that the plug-in did not do anything.

For information about defining this function, see "Writing the Plug-in Function."

Define the recipients of the message and its body and temporary files

{
char *bodyfile;
Recipient *pRecip;
Recipient new_rcpt;
FILE *f;
FILE *body;
char *ControlText;

Create a log file

f = fopen ("log.out","w");

Find and print out the first recipient

To get a pointer to the first recipient in the message, use GetFirstRecipient. The fprintf routine calls GetRecipientAddress to get the address for printing.

pRecip = GetFirstRecipient (ppInMsg[0]);
if (pRecip) {
fprintf (f,"Recip #1 is %s\n",GetRecipientAddress(pRecip));
}

Write the control data for the message

The plug-in next writes the control data for the message to the log file that was created earlier. Control data includes SMTP envelope and connection information gathered during message reception.

ControlText = GetControlData(ppInMsg[0]);
if (ControlText) {
fprintf(f, "Control Data follows\n%s", ControlText);
}

Free the control data

After getting message control data, always free it with this command.

FreeControlData(ControlText);

Add a new recipient to the message

Define the new recipient with any required flags or an extension.

new_rcpt.Addr821 = (char*)malloc (strlen ("<testuser@test.com>") + 1);
strcpy(new_rcpt.Addr821, "<testuser@test.com>");
new_rcpt.Ext = NULL;
new_rcpt.flags = 0;
This is the tricky part - you must set this magic value.

new_rcpt.magic = 0xdeadbeef;
Make the AddRecipient call.

AddRecipient(ppInMsg[0], &new_rcpt);
free (new_rcpt.Addr821);
For more information, see "Add and remove recipients."

Add text to the body of the message

Open the body and add some text. This sample either adds, and prints, the lines of text. If it can't add the text for some reason, it prints an error message.

First, the plug-in gets the message:

bodyfile = GetMessageFile (*ppInMsg);
fprintf (f,"GetMessageFile = %s\n", bodyfile);
Then it opens the message body and adds text to the end:

body = fopen (bodyfile,"a");
if (body == NULL)
{
fprintf (f,"Couldn't append to body\n");
}
else{
fprintf (body,"This is a new body line\n");
fprintf (body,"This is a new body line\n");
fprintf (body,"This is a new body line\n");
fprintf (body,"This is a new body line\n");
fprintf (body,"This is a new body line\n");
fprintf (body,"This is a new body line\n");
fprintf (body,"This is a new body line\n");
fprintf (body,"This is a new body line\n");
fprintf (body,"This is a new body line\n");
fprintf (body,"This is a new body line\n");
fclose (body);
}

Send the message

If the plug-in returns MSG_CONTINUE, the message continues on its way. For other return messages, see "Result Codes."

return MSG_CONTINUE;
NOTE: You can find the code for this plug-in in "Plug-in Code." For information about building the plug-in, see "Building the Plug-in." §
[Top]


Table of Contents | Previous | Next | Index

Last Updated: 11/19/98 10:24:12

[an error occurred while processing this directive]