Le scriptage de csoftadm

This guide, aimed at more advanced users, describes the different methods of calling csoftadm from a program or script.

Note

A special note regarding e-mail configuration: Scripting csoftadm makes it easy to create forwarders with multiple recipient addresses, but this should never be used to implement "mailing lists", for a number of reasons (e.g., no bounce checking is done for forwarders, users cannot unsubscribe, etc). Our mail system provides support for real mailing lists (via Mailman). The mailing list system is also scriptable and should be used instead of forwarders.

Using the csoftadm command

Commands may be piped to the csoftadm program using the the -c or - option. The following command dumps the list of e-mail addresses to a file:

  $ csoftadm -c "mail alias list" > aliases.txt

The - option reads commands from the standard input:

  $ echo "mail alias list" | csoftadm - > aliases.txt
Using perl

A perl script may call the csoftadm program in the following ways:

  open(CSOFTADM, '|csoftadm -') || die;
  print CSOFTADM qq{mail alias add "user@domain.ext" "insn"};
  close(CSOFTADM);
  my @aliases = `csoftadm -c "mail alias list"`;
  print @aliases, "\n";

The MGID module, however, provides a much more efficient interface than the csoftadm program ("mgid" is the name of the basic open-source package which csoftadm is based on). MGID is a Perl XS module for libmgid (the csoftadm C library). This module is documented in the MGID(3p) manual page. The following script authenticates and outputs the server announcements using the low-level query interface:

#!/usr/bin/perl
use MGID;
my $mgi = MGID->new('localhost', 73, $USERNAME, $PASSWORD)
    || die MGID::Error();
if ($mgi->ModuleAvail('Info')) {
	print $mgi-<QueryList('Info', 'Motd'), "\n";
}

The following script uses the high-level interface to mail configuration (see MGID::MailDeliveryCfg for details):

#!/usr/bin/perl
use MGID;
my $mgi = MGID->new('localhost', 73, $USERNAME, $PASSWORD)
    || die MGID::Error();
unless ($mgi->ModuleAvail('Alias')) {
	die MGID::Error();
}
# Print addresses
my $mailCfg = $mgi->MailDeliveryCfg();
foreach my $addr ($mailCfg->Addresses()) {
	print $addr->AddressUTF8()."\n";
	foreach my $insn ($addr->Insns()) {
		foreach my $cond ($insn->Conds()) {
			print "(if )".$cond->String()."\n";
		}
		print $insn->String()."\n";
	}
}
# Configure new address
my $addr = MGID::MailAddress->new('test-addr@domain.ext');
$addr->Add(MGID::MailInsn->newForward('dest1@domain.ext'));
$addr->Add(MGID::MailInsn->newForward('dest2@domain.ext'));
$addr->Commit($mgi) || die MGID::Error();
Using C/C++

The C library libmgid is the most efficient interface to csoftadm. Programs using this library will establish a connection and speak directly with the csoftadm server. C/C++ programs may be compiled with `mgid-config --cflags` and linked against `mgid-config --libs`. The API is documented in mgid(3).


Ce site web - © 2012 Hébergement Csoft.net, Inc.
(Nous Rejoindre) (Politique de Confidentialité)
  LPF Valid HTML 4.01 Transitional