Subject: The mailing list for listmasters using Sympa
List archive
[sympa-users] Extending Sympa functionality without modifying the core code
- From: Steve Shipway <address@concealed>
- To: "'address@concealed'" <address@concealed>
- Subject: [sympa-users] Extending Sympa functionality without modifying the core code
- Date: Mon, 11 Nov 2013 04:45:45 +0000
I have just worked out a rather neat way to do this (extend Sympa) by using custom TT2 classes, that does not require code modifications to Sympa itself. So, for the benefit of anyone interested in hacking the Sympa web interface without changing the core code, here is a brief discussion.
The problem – we wanted to add some functionality to the WWSympa web frontend. We needed to have ‘whitelists’ as well as the existing blacklists. However, we did not want to make major changes to any of the existing Sympa code, as this is managed by RPM and changes here mean headaches at upgrade time.
By using an include.send.header scenario, we could add a rule to the start of all send scenari to check a per-list whitelist.txt file for matches. So far, so good.
To edit this file, create a list-custom-action using the existing LCA interface; that is, make a web_tt2/whitelist.tt2 file, and add a ‘Whitelist’ link in the nav.tt2 index pointing to /sympa/lca/whitelist/$list . Now, the whitelist.tt2 file can be made to look similar to the blacklist.tt2, allowing an ‘edit’ form. But, how to actually retrieve and save the file content without modifying the wwsympa code?
TT2 (the template language) provides a [PERL] tag, so that you can embed perl code into a template. Using this, I was able to add the short code snippets to save and retrieve the file content. However – the big problem was that this does not work unless you enable the EVAL_PERL option in TT2, which can only be done by adding a single line to the Sympa/lib/tt2.pm file. This is what we did, and the template worked (Hopefully, in 6.2.x, this will become a global configurable option in sympa.conf)
However, this is sub-optimal, partly because it requires a code modification, and partly because it enables Perl in ALL templates – which is a bit of a security issue, as list owners can modify the templates for their own lists, mailmerge lists can send tt2 code, and so on.
The next idea was to extend TT2 with custom functions. By creating a new Perl module, sympa/lib/Template/Plugin/whitelist.pm , I can define new TT2 functions. Then by using ‘USE whitelist’ I can link this new TT2 plugin in to the whitelist.tt2 template, and call its functions with ‘SET content = whitelist.fetch’ and similar. The plugin has its own stash under ‘whitelist.*’ so I can pass variables back and forth, and I no longer need to use the PERL tag in the TT2.
Changing over to use this, I find that it works! The only problem is lack of information in the TT2 stash and context available to the plugin – for some reason, ‘robot’ is available, but not ‘list’ or ‘config.home’ and so I cannot deduce the location of the list folder automatically. Fortunately, the list name can be passed from the whitelist.tt2 (which does have access to this information); however the global configuration information in /etc/sympa.cfg does not seem to be exposed into the stash and so the ‘home’ path /var/lib/sympa/list_data has to be hardcoded.
So, after all this, we have a working whitelist setup, that only required a custom scenario, a custom web_tt2 template, and a new Perl module – none of which will be affected by upgrading Sympa in the future.
Some of the code is below. I’d be interested in hearing anyone ideas or opinions, or anything similar you’ve worked out.
Steve
scenari/include.send.header title.gettext Process whitelist and modlist, if they exist search(whitelist.txt) smtp,smime -> do_it # note you must have a default empty whitelist.txt in the sympa/search_filters directory
web_tt2/whitelist.tt2 (part) [% USE whitelist; SET whitelist_action = cap.shift; SET whitelist.content = cap.join('/'); SET whitelist.search_filters = "/var/lib/sympa/list_data/$robot/$list/search_filters"; IF whitelist_action == 'save'; saveerror = whitelist.update; END; whitelist = whitelist.fetch; rows = whitelist.rows; %]
sympa/lib/Template/Plugin/whitelist.pm package Template::Plugin::UOA_whitelist; use base 'Template::Plugin'; use strict; sub new { my ($class, $context) = @_;; return bless { _CONTEXT => $context, search_filters => "", content => '', whitelist => '', saveerror => '' }, $class; } sub update { my( $obj, @args ) = @_; my($rv) = ""; my($sfdir)=''; my($content)=''; $sfdir = $obj->{'search_filters'}; if( $sfdir and ! -d $sfdir ) { mkdir $sfdir; } $content = $obj->{'content'}; $content =~ s/^.// ; # kill strange initial metacharacter $content =~ s/\s*[\r\n]\s+/\n/g; # kill blank lines if( defined $content ) { if( open WLFILE,">${sfdir}/whitelist.txt" ) { print WLFILE $content; close WLFILE; $obj->{saved}=1; } else { $rv = "Unable to save: $!"; $obj->{saveerror}=$rv; } } else { $rv = "No content available to save"; $obj->{saveerror}=$rv; } return $rv; } sub fetch { my( $obj, @args ) = @_; my($rv) = ""; my $fname = $obj->{'search_filters'}."/whitelist.txt"; my $whitelist = ""; if( -r $fname ) { open WLFILE,"<$fname"; my @whitelist = <WLFILE>; close WLFILE; $whitelist = join "",@whitelist; $obj->{rows}=($#whitelist + 1); } $obj->{whitelist}=$whitelist; return $whitelist; } 1;
Steve Shipway ITS Unix Services Design Lead University of Auckland, New Zealand Floor 1, 58 Symonds Street, Auckland Phone: +64 (0)9 3737599 ext 86487 DDI: +64 (0)9 923 6487 Mobile: +64 (0)21 753 189 Email: address@concealed P Please consider the environment before printing this e-mail : 打印本邮件,将减少一棵树存活的机会
|
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
- [sympa-users] Extending Sympa functionality without modifying the core code, Steve Shipway, 11/11/2013
Archive powered by MHonArc 2.6.19+.