Adding a new package to amavisd-new

Introduction

If you want to add a new "package" to amavisd-new, you can not simply insert a line anywhere and write

package Amavis::Example::Package;
...

amavisd-new loads code dynamically: Essentially, most of the code declared as __DATA__ which tells the Perl compiler that the following text should not be compiled. In Perl, it is possible to access the lines of text after the __DATA__ token by using <packagename::DATA>.

In amavisd-new the __DATA__\n token is also used as line delimiter. Therefore Perl considers each package as one "line". The first thing that the amavisd-new program does, is loading all packages one by one (search for the comment # Main program starts here in the source code). During this process, the order and total number of the packages is very important. If you don't change the dynamic module loader, you will experience wierd crashes as some seemingly random subroutines can not be found.

Changing the Module Loader

First, you have to add a new variable which will contain the code of your package. Look for

use vars qw(
   $extra_code_db $extra_code_cache ...

(line 6183 in amavisd-new 2.4.0-pre7) and add a variable.

Second, you have to change the module loader itself. Look for chomp($_ = <Amavis::DATA>) (line 8806 in amavisd-new 2.4.0-pre7). Insert your variable at the appropriate place. Order DOES matter: Your variable MUST appear in the same order as your package does in the file!

To be continued (logging of loaded packages is missing).