| 1 | package Mail::MtPolicyd::Plugin::SqlUserConfig; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use namespace::autoclean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 2 | our $VERSION = '1.12'; # VERSION | |||||||||
| 7 | # ABSTRACT: mtpolicyd plugin for retrieving the user config of a user | ||||||||||
| 8 | |||||||||||
| 9 | 2 | extends 'Mail::MtPolicyd::Plugin'; | |||||||||
| 10 | |||||||||||
| 11 | |||||||||||
| 12 | 2 | ▶ | use Mail::MtPolicyd::Plugin::Result; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 2 | ▶ | use JSON; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | |||||||||||
| 15 | 2 | has 'sql_query' => ( | |||||||||
| 16 | is => 'rw', isa => 'Str', | ||||||||||
| 17 | default => 'SELECT config FROM user_config WHERE address=?', | ||||||||||
| 18 | ); | ||||||||||
| 19 | |||||||||||
| 20 | has '_json' => ( | ||||||||||
| 21 | is => 'ro', isa => 'JSON', lazy => 1, | ||||||||||
| 22 | default => sub { | ||||||||||
| 23 | 0 | ▶ | return JSON->new; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 24 | } | ||||||||||
| 25 | 2 | ); | |||||||||
| 26 | |||||||||||
| 27 | sub _get_config { | ||||||||||
| 28 | 0 | ▶ | my ( $self, $r ) = @_; | ||||||||
|
Subroutine
_get_config: not called CC=2 SCAR=17.9
| |||||||||||
| 29 | 0 | my $rcpt = $r->attr('recipient'); | |||||||||
| 30 | |||||||||||
| 31 | 0 | my $dbh = $r->server->get_dbh; | |||||||||
| 32 | 0 | my $sth = $dbh->prepare( $self->sql_query ); | |||||||||
| 33 | 0 | $sth->execute( $rcpt ); | |||||||||
| 34 | 0 | my ( $json ) = $sth->fetchrow_array; | |||||||||
| 35 | 0 | ▶ | if( ! defined $json ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 36 | 0 | die( 'no user-config found for '.$rcpt ); | |||||||||
| 37 | } | ||||||||||
| 38 | 0 | return $self->_json->decode( $json ); | |||||||||
| 39 | } | ||||||||||
| 40 | |||||||||||
| 41 | sub run { | ||||||||||
| 42 | 0 | ▶ | my ( $self, $r ) = @_; | ||||||||
|
Subroutine
run: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||
| 43 | 0 | my $config; | |||||||||
| 44 | |||||||||||
| 45 | 0 | ▶ | if( ! defined $r->attr('recipient') ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 46 | 0 | $self->log($r, 'no attribute \'recipient\' in request'); | |||||||||
| 47 | 0 | return; | |||||||||
| 48 | } | ||||||||||
| 49 | |||||||||||
| 50 | 0 | eval { $config = $self->_get_config($r) }; | |||||||||
| 51 | 0 | ▶ | if( $@ ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 52 | 0 | $self->log($r, 'unable to retrieve user-config: '.$@); | |||||||||
| 53 | 0 | return; | |||||||||
| 54 | } | ||||||||||
| 55 | |||||||||||
| 56 | 0 | foreach my $key ( keys %$config ) { | |||||||||
| 57 | 0 | $r->session->{$key} = $config->{$key}; | |||||||||
| 58 | } | ||||||||||
| 59 | |||||||||||
| 60 | 0 | return; | |||||||||
| 61 | } | ||||||||||
| 62 | |||||||||||
| 63 | |||||||||||
| 64 | 2 | __PACKAGE__->meta->make_immutable; | |||||||||
| 65 | |||||||||||
| 66 | 2 | 1; | |||||||||
| 67 | |||||||||||
| 68 | |||||||||||
| 69 | __END__ | ||||||||||
| 70 | =pod | ||||||||||
| 71 | |||||||||||
| 72 | =head1 NAME | ||||||||||
| 73 | |||||||||||
| 74 | Mail::MtPolicyd::Plugin::SqlUserConfig - mtpolicyd plugin for retrieving the user config of a user | ||||||||||
| 75 | |||||||||||
| 76 | =head1 VERSION | ||||||||||
| 77 | |||||||||||
| 78 | version 1.12 | ||||||||||
| 79 | |||||||||||
| 80 | =head1 DESCRIPTION | ||||||||||
| 81 | |||||||||||
| 82 | This plugin will retrieve a JSON string from an SQL database and will merge the data structure | ||||||||||
| 83 | into the current session. | ||||||||||
| 84 | |||||||||||
| 85 | This could be used to retrieve configuration values for users from a database. | ||||||||||
| 86 | |||||||||||
| 87 | =head1 PARAMETERS | ||||||||||
| 88 | |||||||||||
| 89 | =over | ||||||||||
| 90 | |||||||||||
| 91 | =item sql_query (default: SELECT config FROM user_config WHERE address=?) | ||||||||||
| 92 | |||||||||||
| 93 | The SQL query to retrieve the JSON configuration string. | ||||||||||
| 94 | |||||||||||
| 95 | The content of the first row/column is used. | ||||||||||
| 96 | |||||||||||
| 97 | =back | ||||||||||
| 98 | |||||||||||
| 99 | =head1 EXAMPLE USER SPECIFIC GREYLISTING | ||||||||||
| 100 | |||||||||||
| 101 | Create the following table in the SQL database: | ||||||||||
| 102 | |||||||||||
| 103 | CREATE TABLE `user_config` ( | ||||||||||
| 104 | `id` int(11) NOT NULL AUTO_INCREMENT, | ||||||||||
| 105 | `address` varchar(255) DEFAULT NULL, | ||||||||||
| 106 | `config` TEXT NOT NULL, | ||||||||||
| 107 | PRIMARY KEY (`id`), | ||||||||||
| 108 | UNIQUE KEY `address` (`address`) | ||||||||||
| 109 | ) ENGINE=MyISAM DEFAULT CHARSET=latin1 | ||||||||||
| 110 | |||||||||||
| 111 | INSERT INTO TABLE `user_config` VALUES( NULL, 'karlson@vomdach.de', '{"greylisting":"on"}' ); | ||||||||||
| 112 | |||||||||||
| 113 | In mtpolicyd.conf: | ||||||||||
| 114 | |||||||||||
| 115 | db_dsn="dbi:mysql:mail" | ||||||||||
| 116 | db_user=mail | ||||||||||
| 117 | db_password=password | ||||||||||
| 118 | |||||||||||
| 119 | <Plugin user-config> | ||||||||||
| 120 | module = "SqlUserConfig" | ||||||||||
| 121 | sql_query = "SELECT config FROM user_config WHERE address=?" | ||||||||||
| 122 | </Plugin> | ||||||||||
| 123 | <Plugin greylist> | ||||||||||
| 124 | enabled = "off" # off by default | ||||||||||
| 125 | uc_enabled = "greylisting" # override with value of key 'greylisting' is set in session | ||||||||||
| 126 | module = "Greylist" | ||||||||||
| 127 | score = -5 | ||||||||||
| 128 | mode = "passive" | ||||||||||
| 129 | </Plugin> | ||||||||||
| 130 | |||||||||||
| 131 | =head1 AUTHOR | ||||||||||
| 132 | |||||||||||
| 133 | Markus Benning <ich@markusbenning.de> | ||||||||||
| 134 | |||||||||||
| 135 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 136 | |||||||||||
| 137 | This software is Copyright (c) 2014 by Markus Benning <ich@markusbenning.de>. | ||||||||||
| 138 | |||||||||||
| 139 | This is free software, licensed under: | ||||||||||
| 140 | |||||||||||
| 141 | The GNU General Public License, Version 2, June 1991 | ||||||||||
| 142 | |||||||||||
| 143 | =cut | ||||||||||
| 144 | |||||||||||