| 1 | package Mail::Decency::Policy::Throttle; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | extends 'Mail::Decency::Policy::Core'; | |||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use version 0.74; our $VERSION = qv( "v0.1.4" ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use mro 'c3'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Scalar::Util qw/ weaken /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use POSIX qw/ ceil /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | =head1 NAME | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | Mail::Decency::Policy::Throttle | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | Throtle mail sending for dedicated sources (sender ip, sender username, sender address, "account") | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | =over | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | =item * client address | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | The IP of the client connecting to the server. | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | =item * sender username, sender domain, sender address | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | The username is the (if any provided) sasl username | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | =item * recipient domain, recipient address | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | Caution with this. You don't want your incoming mails to be rejected! | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | =item * account | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | An account is a kind of context. You can associate each row in the database with an column called "account". The counter for throtteling will then be applied to this value instead of sender or such. | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | Example usecase: There are domain1.tld and domain2.tld. The task is to limit the max amount for sending to 500 Mails per day for both domains together. | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | =back | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | =head1 CONFIG | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | Those are the config params in YAML | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | --- | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | disable: 0 | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | # Wheter affect ONLY those having a sasl username set. | ||||||||||||||||||||||||||||
| 53 | # Use this if you have one mailserver for incoming AND outgoing | ||||||||||||||||||||||||||||
| 54 | # mails. This does not check the validity of the sasl user! | ||||||||||||||||||||||||||||
| 55 | require_sasl_username: 1 | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | # Wheter use the counters generated by the Mail::Decency::LogParser::Stats | ||||||||||||||||||||||||||||
| 58 | # module which has to be up and running. | ||||||||||||||||||||||||||||
| 59 | # Your limits might be a little streched, depending on the speed of | ||||||||||||||||||||||||||||
| 60 | # the log parser (+1 or +2 or something). | ||||||||||||||||||||||||||||
| 61 | # However, this is the only way to assure only not-rejected mails | ||||||||||||||||||||||||||||
| 62 | # get accounted for. | ||||||||||||||||||||||||||||
| 63 | # If disabled, ANY mail seen will increment the counter (if a later | ||||||||||||||||||||||||||||
| 64 | # configuration setting does reject it, it still counts!) | ||||||||||||||||||||||||||||
| 65 | use_stats_cache: 0 | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | # The default limits, if no exception is in the exception database. | ||||||||||||||||||||||||||||
| 68 | # You can use: | ||||||||||||||||||||||||||||
| 69 | # * client_address (ip of sending client) | ||||||||||||||||||||||||||||
| 70 | # * sender_domain (domain part of sender) | ||||||||||||||||||||||||||||
| 71 | # * sender_address (email address of sender) | ||||||||||||||||||||||||||||
| 72 | # * sasl_username (the sasl username, if any) | ||||||||||||||||||||||||||||
| 73 | # * sender_domain (domain part of sender) | ||||||||||||||||||||||||||||
| 74 | # * recipient_domain (domain part of the recipient) | ||||||||||||||||||||||||||||
| 75 | # * recipient_address (email address of recipient) | ||||||||||||||||||||||||||||
| 76 | # * account (the account.. see above) | ||||||||||||||||||||||||||||
| 77 | default_limits: | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | # the following can be read as: | ||||||||||||||||||||||||||||
| 80 | # * Account per sender domain | ||||||||||||||||||||||||||||
| 81 | # * It is not allowed to send more then: | ||||||||||||||||||||||||||||
| 82 | # * 1 Mail per 10 Seconds | ||||||||||||||||||||||||||||
| 83 | # * 50 Mails per 10 Minutes | ||||||||||||||||||||||||||||
| 84 | # * 1000 Mails per day | ||||||||||||||||||||||||||||
| 85 | sender_domain: | ||||||||||||||||||||||||||||
| 86 | - | ||||||||||||||||||||||||||||
| 87 | maximum: 1 | ||||||||||||||||||||||||||||
| 88 | interval: 10 | ||||||||||||||||||||||||||||
| 89 | - | ||||||||||||||||||||||||||||
| 90 | maximum: 50 | ||||||||||||||||||||||||||||
| 91 | interval: 600 | ||||||||||||||||||||||||||||
| 92 | - | ||||||||||||||||||||||||||||
| 93 | maximum: 1000 | ||||||||||||||||||||||||||||
| 94 | interval: 86400 | ||||||||||||||||||||||||||||
| 95 | account: | ||||||||||||||||||||||||||||
| 96 | - | ||||||||||||||||||||||||||||
| 97 | maximum: 50 | ||||||||||||||||||||||||||||
| 98 | interval: 600 | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | # which exception database to use (see above) | ||||||||||||||||||||||||||||
| 101 | # use only those you really have to. Don't activate all | ||||||||||||||||||||||||||||
| 102 | # without actually having data! | ||||||||||||||||||||||||||||
| 103 | exception_databases: | ||||||||||||||||||||||||||||
| 104 | - sender_domain | ||||||||||||||||||||||||||||
| 105 | - sender_address | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | # The reject messages per interval (above) | ||||||||||||||||||||||||||||
| 108 | # Don't forget the rejection code (better use 4xx for | ||||||||||||||||||||||||||||
| 109 | # temporary, instead of 5xx for hard) | ||||||||||||||||||||||||||||
| 110 | reject_messages: | ||||||||||||||||||||||||||||
| 111 | 10: | ||||||||||||||||||||||||||||
| 112 | message: 'Sorry, nicht mehr als eine Mail in 10 Sekunden' | ||||||||||||||||||||||||||||
| 113 | code: 450 | ||||||||||||||||||||||||||||
| 114 | 600: | ||||||||||||||||||||||||||||
| 115 | message: 'Sorry, nicht mehr als 50 Mails in 10 Minuten' | ||||||||||||||||||||||||||||
| 116 | code: 450 | ||||||||||||||||||||||||||||
| 117 | 86400: | ||||||||||||||||||||||||||||
| 118 | message: 'Sorry, nicht mehr als 1000 Mails pro Tag' | ||||||||||||||||||||||||||||
| 119 | code: 450 | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | # The default error message which will be used if none is set | ||||||||||||||||||||||||||||
| 122 | # for the interval.. comes in handy if you use exception | ||||||||||||||||||||||||||||
| 123 | # database with custom intervals | ||||||||||||||||||||||||||||
| 124 | # Variables you can use are: | ||||||||||||||||||||||||||||
| 125 | # * %maximum% (limit of mails in interval) | ||||||||||||||||||||||||||||
| 126 | # * %interval% (interval in seconds) | ||||||||||||||||||||||||||||
| 127 | # * %interval_minutes% (interval in minutes, round up) | ||||||||||||||||||||||||||||
| 128 | # * %interval_hours% (interval in hours, round up) | ||||||||||||||||||||||||||||
| 129 | # * %interval_days% (interval in days, round up) | ||||||||||||||||||||||||||||
| 130 | default_reject_message: | ||||||||||||||||||||||||||||
| 131 | message: 'Sorry, nicht mehr als %maximum% Mails in %interval_minutes% Minuten' | ||||||||||||||||||||||||||||
| 132 | code: 450 | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | =head1 DATABASE | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | CREATE TABLE throttle_client_address ( | ||||||||||||||||||||||||||||
| 139 | id INTEGER PRIMARY KEY, | ||||||||||||||||||||||||||||
| 140 | client_address VARCHAR( 255 ), | ||||||||||||||||||||||||||||
| 141 | interval INTEGER, | ||||||||||||||||||||||||||||
| 142 | maximum INTEGER, | ||||||||||||||||||||||||||||
| 143 | account VARCHAR( 25 ) | ||||||||||||||||||||||||||||
| 144 | ); | ||||||||||||||||||||||||||||
| 145 | CREATE UNIQUE INDEX throttle_client_address_uk ON throttle_client_address( client_address, interval ); | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | CREATE TABLE throttle_sender_domain ( | ||||||||||||||||||||||||||||
| 148 | id INTEGER PRIMARY KEY, | ||||||||||||||||||||||||||||
| 149 | sender_domain VARCHAR( 255 ), | ||||||||||||||||||||||||||||
| 150 | interval INTEGER, | ||||||||||||||||||||||||||||
| 151 | maximum INTEGER, | ||||||||||||||||||||||||||||
| 152 | account VARCHAR( 25 ) | ||||||||||||||||||||||||||||
| 153 | ); | ||||||||||||||||||||||||||||
| 154 | CREATE UNIQUE INDEX throttle_sender_domain_uk ON throttle_sender_domain( sender_domain, interval ); | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | CREATE TABLE throttle_sender_address( | ||||||||||||||||||||||||||||
| 157 | id INTEGER PRIMARY KEY, | ||||||||||||||||||||||||||||
| 158 | sender_address VARCHAR( 255 ), | ||||||||||||||||||||||||||||
| 159 | interval INTEGER, | ||||||||||||||||||||||||||||
| 160 | maximum INTEGER, | ||||||||||||||||||||||||||||
| 161 | account VARCHAR( 25 ) | ||||||||||||||||||||||||||||
| 162 | ); | ||||||||||||||||||||||||||||
| 163 | CREATE UNIQUE INDEX throttle_sender_address_uk ON throttle_sender_address( sender_address, interval ); | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | CREATE TABLE throttle_sasl_username( | ||||||||||||||||||||||||||||
| 166 | id INTEGER PRIMARY KEY, | ||||||||||||||||||||||||||||
| 167 | sasl_username VARCHAR( 255 ), | ||||||||||||||||||||||||||||
| 168 | interval INTEGER, | ||||||||||||||||||||||||||||
| 169 | maximum INTEGER, | ||||||||||||||||||||||||||||
| 170 | account VARCHAR( 25 ) | ||||||||||||||||||||||||||||
| 171 | ); | ||||||||||||||||||||||||||||
| 172 | CREATE UNIQUE INDEX throttle_sasl_username_uk ON throttle_sasl_username( sasl_username, interval ); | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | CREATE TABLE throttle_recipient_domain( | ||||||||||||||||||||||||||||
| 175 | id INTEGER PRIMARY KEY, | ||||||||||||||||||||||||||||
| 176 | recipient_domain VARCHAR( 255 ), | ||||||||||||||||||||||||||||
| 177 | interval INTEGER, | ||||||||||||||||||||||||||||
| 178 | maximum INTEGER, | ||||||||||||||||||||||||||||
| 179 | account VARCHAR( 25 ) | ||||||||||||||||||||||||||||
| 180 | ); | ||||||||||||||||||||||||||||
| 181 | CREATE UNIQUE INDEX throttle_recipient_domain_uk ON throttle_recipient_domain( recipient_domain, interval ); | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | CREATE TABLE throttle_recipient_address( | ||||||||||||||||||||||||||||
| 184 | id INTEGER PRIMARY KEY, | ||||||||||||||||||||||||||||
| 185 | recipient_address VARCHAR( 255 ), | ||||||||||||||||||||||||||||
| 186 | interval INTEGER, | ||||||||||||||||||||||||||||
| 187 | maximum INTEGER, | ||||||||||||||||||||||||||||
| 188 | account VARCHAR( 25 ) | ||||||||||||||||||||||||||||
| 189 | ); | ||||||||||||||||||||||||||||
| 190 | CREATE UNIQUE INDEX throttle_recipient_address_uk ON throttle_recipient_address( recipient_address, interval ); | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | CREATE TABLE throttle_account( | ||||||||||||||||||||||||||||
| 193 | id INTEGER PRIMARY KEY, | ||||||||||||||||||||||||||||
| 194 | account VARCHAR( 255 ), | ||||||||||||||||||||||||||||
| 195 | interval INTEGER, | ||||||||||||||||||||||||||||
| 196 | maximum INTEGER | ||||||||||||||||||||||||||||
| 197 | ); | ||||||||||||||||||||||||||||
| 198 | CREATE UNIQUE INDEX throttle_account_uk ON throttle_account( account, interval ); | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | =cut | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | 1 | our @REGULAR_DATABASES = qw/ client_address sender_domain sasl_username sender_address recipient_domain recipient_address /; | |||||||||||||||||||||||||||
| 203 | 1 | our @ALL_DATABASES = ( @REGULAR_DATABASES, 'account' ); | |||||||||||||||||||||||||||
| 204 | 1 | our %ALLOWED_DATABASES = map { ( $_ => 1 ) } @ALL_DATABASES; | |||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | =head1 CLASS ATTRIBUTES | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | =head2 default_reject_message : HashRef | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | Defauilt reject message and default reject code | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | { | ||||||||||||||||||||||||||||
| 215 | message => 'Sorry, Limit reached (%maximum% mails in %interval% seconds)', | ||||||||||||||||||||||||||||
| 216 | code => 450 | ||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | =cut | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | 0 | ▶ | has default_reject_message => ( is => 'rw', isa => 'HashRef', default => sub { { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 222 | 0 | message => 'Sorry, Limit reached (%maximum% mails in %interval% seconds)', | |||||||||||||||||||||||||||
| 223 | code => 450 | ||||||||||||||||||||||||||||
| 224 | 1 | } } ); | |||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | =head2 reject_messages : HashRef[HashRef] | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | Reject messages per interval | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | { | ||||||||||||||||||||||||||||
| 231 | 10 => { | ||||||||||||||||||||||||||||
| 232 | message => 'Sorry, Limit reached .. not more then 99 in 10 seconds', | ||||||||||||||||||||||||||||
| 233 | code => 450 | ||||||||||||||||||||||||||||
| 234 | }, | ||||||||||||||||||||||||||||
| 235 | 86400 => { | ||||||||||||||||||||||||||||
| 236 | message => 'Sorry, Limit reached .. not more then 101 in 24 hours', | ||||||||||||||||||||||||||||
| 237 | code => 450 | ||||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | =cut | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | 0 | ▶ | has reject_messages => ( is => 'rw', isa => 'HashRef[HashRef]', default => sub { {} } ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | =head2 default_limits : HashRef | ||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | Limits per attribute ( sender_domain, client_address, recipient_domain, account, .. ) | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | { | ||||||||||||||||||||||||||||
| 250 | sender_domain => [ | ||||||||||||||||||||||||||||
| 251 | { | ||||||||||||||||||||||||||||
| 252 | maximum => 99, | ||||||||||||||||||||||||||||
| 253 | interval => 10, | ||||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||
| 255 | ], | ||||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | =cut | ||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | 1 | has default_limits => ( is => 'rw', isa => 'HashRef' ); | |||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | =head2 default_limit_databases : ArrayRef[HashRef] | ||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | Which limit databases .. internal usage | ||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | =cut | ||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | 0 | ▶ | has default_limit_databases => ( is => 'rw', isa => 'ArrayRef[HashRef]', default => sub { [] } ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | =head2 exception_database : HashRef | ||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | Exception from default values (eg per account, per sender_domain, whatever) | ||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | =cut | ||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | 0 | ▶ | has exception_database => ( is => 'rw', isa => 'HashRef', default => sub { {} } ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | =head2 used_databases : ArrayRef[Str] | ||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | Internal usage.. | ||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||
| 282 | =cut | ||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||
| 284 | 0 | ▶ | has used_databases => ( is => 'rw', isa => 'ArrayRef[Str]', default => sub { [] } ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | =head2 use_accounts : Bool | ||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||
| 289 | Wheter accounts (see description) should be used or not (cost performance) | ||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||
| 291 | =cut | ||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||
| 293 | 1 | has use_accounts => ( is => 'rw', isa => 'Bool', default => 0 ); | |||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | =head2 counter_increment : CodeRef | ||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | Internal usage.. | ||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | =cut | ||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||
| 301 | 1 | has counter_increment => ( is => 'rw', isa => 'CodeRef' ); | |||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||
| 303 | =head2 counter_read : CodeRef | ||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||
| 305 | Internal usage.. | ||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | =cut | ||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||
| 309 | 1 | has counter_read => ( is => 'rw', isa => 'CodeRef' ); | |||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | =head2 require_sasl_username : CodeRef | ||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||
| 313 | Enable throttle module only if sasl_username is given (this happens if the SMTP connection contains AUTH information.. ) | ||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | =cut | ||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||
| 317 | 1 | has require_sasl_username => ( is => 'rw', isa => 'Bool', default => 0 ); | |||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||
| 319 | =head2 schema_definition | ||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | Database .. | ||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | =cut | ||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||
| 325 | 0 | ▶ | has schema_definition => ( is => 'ro', isa => 'HashRef[HashRef]', default => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 326 | { | ||||||||||||||||||||||||||||
| 327 | 0 | throttle => { | |||||||||||||||||||||||||||
| 328 | client_address => { | ||||||||||||||||||||||||||||
| 329 | client_address => [ varchar => 39 ], | ||||||||||||||||||||||||||||
| 330 | interval => 'integer', | ||||||||||||||||||||||||||||
| 331 | maximum => 'integer', | ||||||||||||||||||||||||||||
| 332 | account => [ varchar => 100 ], | ||||||||||||||||||||||||||||
| 333 | -unique => [ 'client_address', 'interval' ] | ||||||||||||||||||||||||||||
| 334 | }, | ||||||||||||||||||||||||||||
| 335 | sender_domain => { | ||||||||||||||||||||||||||||
| 336 | sender_domain => [ varchar => 255 ], | ||||||||||||||||||||||||||||
| 337 | interval => 'integer', | ||||||||||||||||||||||||||||
| 338 | maximum => 'integer', | ||||||||||||||||||||||||||||
| 339 | account => [ varchar => 100 ], | ||||||||||||||||||||||||||||
| 340 | -unique => [ 'sender_domain', 'interval' ] | ||||||||||||||||||||||||||||
| 341 | }, | ||||||||||||||||||||||||||||
| 342 | sender_address => { | ||||||||||||||||||||||||||||
| 343 | sender_address => [ varchar => 255 ], | ||||||||||||||||||||||||||||
| 344 | interval => 'integer', | ||||||||||||||||||||||||||||
| 345 | maximum => 'integer', | ||||||||||||||||||||||||||||
| 346 | account => [ varchar => 100 ], | ||||||||||||||||||||||||||||
| 347 | -unique => [ 'sender_address', 'interval' ] | ||||||||||||||||||||||||||||
| 348 | }, | ||||||||||||||||||||||||||||
| 349 | sasl_username => { | ||||||||||||||||||||||||||||
| 350 | sasl_username => [ varchar => 255 ], | ||||||||||||||||||||||||||||
| 351 | interval => 'integer', | ||||||||||||||||||||||||||||
| 352 | maximum => 'integer', | ||||||||||||||||||||||||||||
| 353 | account => [ varchar => 100 ], | ||||||||||||||||||||||||||||
| 354 | -unique => [ 'sasl_username', 'interval' ] | ||||||||||||||||||||||||||||
| 355 | }, | ||||||||||||||||||||||||||||
| 356 | recipient_domain => { | ||||||||||||||||||||||||||||
| 357 | recipient_domain => [ varchar => 255 ], | ||||||||||||||||||||||||||||
| 358 | interval => 'integer', | ||||||||||||||||||||||||||||
| 359 | maximum => 'integer', | ||||||||||||||||||||||||||||
| 360 | account => [ varchar => 100 ], | ||||||||||||||||||||||||||||
| 361 | -unique => [ 'recipient_domain', 'interval' ] | ||||||||||||||||||||||||||||
| 362 | }, | ||||||||||||||||||||||||||||
| 363 | recipient_address => { | ||||||||||||||||||||||||||||
| 364 | recipient_address => [ varchar => 255 ], | ||||||||||||||||||||||||||||
| 365 | interval => 'integer', | ||||||||||||||||||||||||||||
| 366 | maximum => 'integer', | ||||||||||||||||||||||||||||
| 367 | account => [ varchar => 100 ], | ||||||||||||||||||||||||||||
| 368 | -unique => [ 'recipient_address', 'interval' ] | ||||||||||||||||||||||||||||
| 369 | }, | ||||||||||||||||||||||||||||
| 370 | account => { | ||||||||||||||||||||||||||||
| 371 | account => [ varchar => 100 ], | ||||||||||||||||||||||||||||
| 372 | interval => 'integer', | ||||||||||||||||||||||||||||
| 373 | maximum => 'integer', | ||||||||||||||||||||||||||||
| 374 | account => [ varchar => 100 ], | ||||||||||||||||||||||||||||
| 375 | -unique => [ 'account', 'interval' ] | ||||||||||||||||||||||||||||
| 376 | }, | ||||||||||||||||||||||||||||
| 377 | } | ||||||||||||||||||||||||||||
| 378 | }; | ||||||||||||||||||||||||||||
| 379 | 1 | } ); | |||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||
| 383 | =head1 METHODS | ||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||
| 386 | =head2 init | ||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||
| 388 | =cut | ||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||
| 390 | 0 | ▶ | sub init { | ||||||||||||||||||||||||||
|
Subroutine
init: not called CC=20 SCAR=60.4
Pod
documented
| |||||||||||||||||||||||||||||
| 391 | 0 | my ( $self ) = @_; | |||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||
| 393 | 0 | $self->next::method(); | |||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||
| 395 | 0 | my %used_database = (); | |||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||
| 397 | # having exception databases ? which to use ? | ||||||||||||||||||||||||||||
| 398 | 0 | my $min_ok = 0; | |||||||||||||||||||||||||||
| 399 | 0 | ▶ | if ( $self->config->{ exception_databases } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 400 | die "exception_databases has to be an array\n" | ||||||||||||||||||||||||||||
| 401 | 0 | ▶ | unless ref( $self->config->{ exception_databases } ) eq 'ARRAY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||
| 403 | # having non empty amount of databases | ||||||||||||||||||||||||||||
| 404 | 0 | ▶ | if ( scalar @{ $self->config->{ exception_databases } } > 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||
| 406 | # check database | ||||||||||||||||||||||||||||
| 407 | 0 | foreach my $db( @{ $self->config->{ exception_databases } } ) { | |||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||
| 409 | # oops, unknwon database name! | ||||||||||||||||||||||||||||
| 410 | die "Unknown database '$db' in 'exception_databases' (allowed databases: ". join( ", ", @ALL_DATABASES ). ")\n" | ||||||||||||||||||||||||||||
| 411 | 0 | ▶ | unless $ALLOWED_DATABASES{ $db }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||
| 413 | # found ok, rememeber | ||||||||||||||||||||||||||||
| 414 | 0 | $min_ok++; | |||||||||||||||||||||||||||
| 415 | 0 | $used_database{ $db } ++; | |||||||||||||||||||||||||||
| 416 | 0 | $self->exception_database->{ $db } ++; | |||||||||||||||||||||||||||
| 417 | } | ||||||||||||||||||||||||||||
| 418 | } | ||||||||||||||||||||||||||||
| 419 | } | ||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||
| 421 | # oops, no having any | ||||||||||||||||||||||||||||
| 422 | 0 | ▶ | if ( $self->config->{ default_limits } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 423 | die "default_limits has to be a hash\n" | ||||||||||||||||||||||||||||
| 424 | 0 | ▶ | unless ref( $self->config->{ default_limits } ) eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||
| 426 | # parse limit config.. | ||||||||||||||||||||||||||||
| 427 | 0 | ▶ | while( my ( $db, $ref ) = each %{ $self->config->{ default_limits } } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 428 | die "Unknown database '$db' in 'default_limits' (allowed databases: ". join( ", ", @ALL_DATABASES ). ")\n" | ||||||||||||||||||||||||||||
| 429 | 0 | ▶ | unless $ALLOWED_DATABASES{ $db }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||
| 431 | # remember as used | ||||||||||||||||||||||||||||
| 432 | 0 | $used_database{ $db } ++; | |||||||||||||||||||||||||||
| 433 | 0 | $min_ok ++; | |||||||||||||||||||||||||||
| 434 | } | ||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||
| 436 | 0 | $self->default_limits( $self->config->{ default_limits } ); | |||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||
| 438 | # add ORDERED list of default limit databaseds | ||||||||||||||||||||||||||||
| 439 | 0 | foreach my $db( @ALL_DATABASES ) { | |||||||||||||||||||||||||||
| 440 | 0 | push @{ $self->default_limit_databases }, $db | |||||||||||||||||||||||||||
| 441 | 0 | ▶ | if $self->default_limits->{ $db }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 442 | } | ||||||||||||||||||||||||||||
| 443 | } | ||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||
| 445 | # min reqs not satisfied | ||||||||||||||||||||||||||||
| 446 | 0 | ▶ | die "Min requirements for Throttle: 'default_limits' and/or 'exception_databases'\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 447 | unless $min_ok; | ||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||
| 449 | # build up ORDERED list of actual used databases | ||||||||||||||||||||||||||||
| 450 | 0 | foreach my $db( @ALL_DATABASES ) { | |||||||||||||||||||||||||||
| 451 | 0 | push @{ $self->used_databases }, $db | |||||||||||||||||||||||||||
| 452 | 0 | ▶ | if $used_database{ $db }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 453 | } | ||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||
| 455 | # enabled account style ? | ||||||||||||||||||||||||||||
| 456 | 0 | ▶ | $self->use_accounts( 1 ) if $used_database{ account }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||
| 458 | # read oyt reject messages (per interval and default) | ||||||||||||||||||||||||||||
| 459 | 0 | ▶ | $self->reject_messages( $self->config->{ reject_messages } || {} ); | ||||||||||||||||||||||||||
|
Condition0%0/2
$self->config->{'reject_messages'} || {}
MC/DC0%0/1
$self->config->{'reject_messages'} || {}
$self->config->{'reject_messages'}
Truth table0%0/2
A
A= $self->config->{'reject_messages'}
| |||||||||||||||||||||||||||||
| 460 | $self->default_reject_message( $self->config->{ default_reject_message } ) | ||||||||||||||||||||||||||||
| 461 | 0 | ▶ | if $self->config->{ default_reject_message }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||
| 463 | # init count method - either via LogParser::Stats or stand alone | ||||||||||||||||||||||||||||
| 464 | 0 | weaken( my $self_weak = $self ); | |||||||||||||||||||||||||||
| 465 | 0 | ▶ | if ( $self->config->{ use_stats_cache } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 466 | 0 | my %map_to_logparser_cache = qw( | |||||||||||||||||||||||||||
| 467 | client_address ip | ||||||||||||||||||||||||||||
| 468 | sender_address from_address | ||||||||||||||||||||||||||||
| 469 | recipient_address to_address | ||||||||||||||||||||||||||||
| 470 | sender_domain from_domain | ||||||||||||||||||||||||||||
| 471 | recipient_domain to_domain | ||||||||||||||||||||||||||||
| 472 | ); | ||||||||||||||||||||||||||||
| 473 | |||||||||||||||||||||||||||||
| 474 | 0 | ▶ | $self->counter_read( sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 475 | 0 | my ( $name, $value, $interval ) = @_; # client_address, 127.0.0.1, 3600 | |||||||||||||||||||||||||||
| 476 | 0 | my $time = time(); | |||||||||||||||||||||||||||
| 477 | 0 | $time -= $time % $interval; | |||||||||||||||||||||||||||
| 478 | 0 | my $attr = $map_to_logparser_cache{ $name }; | |||||||||||||||||||||||||||
| 479 | 0 | ▶ | return $self_weak->cache->get( 'lp-stats-sent-'. $attr. '-'. $value. '-'. $time ) || 0; | ||||||||||||||||||||||||||
|
Condition0%0/2
$self_weak->cache->get('lp-stats-sent-' . $attr . '-' . $value . '-' . $time) || 0
MC/DC0%0/1
$self_weak->cache->get('lp-stats-sent-' . $attr . '-' . $value . '-' . $time) || 0
$self_weak->cache->get('lp-stats-sent-' . $attr . '-' . $value . '-' . $time)
Truth table0%0/2
A
A= $self_weak->cache->get('lp-stats-sent-' . $attr . '-' . $value . '-' . $time)
| |||||||||||||||||||||||||||||
| 480 | 0 | } ); | |||||||||||||||||||||||||||
| 481 | 0 | ▶ | $self->counter_increment( sub {} ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 482 | } | ||||||||||||||||||||||||||||
| 483 | else { | ||||||||||||||||||||||||||||
| 484 | 0 | ▶ | $self->counter_read( sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 485 | 0 | my ( $name, $value, $interval ) = @_; | |||||||||||||||||||||||||||
| 486 | 0 | my $time = time(); | |||||||||||||||||||||||||||
| 487 | 0 | $time -= $time % $interval; | |||||||||||||||||||||||||||
| 488 | 0 | ▶ | return $self_weak->cache->get( 'throttle-'. $name. '-'. $value. '-'. $time ) || 0; | ||||||||||||||||||||||||||
|
Condition0%0/2
$self_weak->cache->get('throttle-' . $name . '-' . $value . '-' . $time) || 0
MC/DC0%0/1
$self_weak->cache->get('throttle-' . $name . '-' . $value . '-' . $time) || 0
$self_weak->cache->get('throttle-' . $name . '-' . $value . '-' . $time)
Truth table0%0/2
A
A= $self_weak->cache->get('throttle-' . $name . '-' . $value . '-' . $time)
| |||||||||||||||||||||||||||||
| 489 | 0 | } ); | |||||||||||||||||||||||||||
| 490 | 0 | ▶ | $self->counter_increment( sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 491 | 0 | my ( $name, $value, $interval ) = @_; | |||||||||||||||||||||||||||
| 492 | 0 | my $time = time(); | |||||||||||||||||||||||||||
| 493 | 0 | $time -= $time % $interval; | |||||||||||||||||||||||||||
| 494 | 0 | my $amount = $self->counter_read->( $name, $value, $interval ); | |||||||||||||||||||||||||||
| 495 | 0 | return $self_weak->cache->set( 'throttle-'. $name. '-'. $value. '-'. $time, $amount+1 ); | |||||||||||||||||||||||||||
| 496 | 0 | } ); | |||||||||||||||||||||||||||
| 497 | } | ||||||||||||||||||||||||||||
| 498 | |||||||||||||||||||||||||||||
| 499 | # wheter require require_sasl_username | ||||||||||||||||||||||||||||
| 500 | 0 | ▶ | $self->require_sasl_username( $self->config->{ require_sasl_username } ? 1 : 0 ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 501 | |||||||||||||||||||||||||||||
| 502 | 0 | return ; | |||||||||||||||||||||||||||
| 503 | } | ||||||||||||||||||||||||||||
| 504 | |||||||||||||||||||||||||||||
| 505 | |||||||||||||||||||||||||||||
| 506 | |||||||||||||||||||||||||||||
| 507 | =head2 handle | ||||||||||||||||||||||||||||
| 508 | |||||||||||||||||||||||||||||
| 509 | =cut | ||||||||||||||||||||||||||||
| 510 | |||||||||||||||||||||||||||||
| 511 | 0 | ▶ | sub handle { | ||||||||||||||||||||||||||
|
Subroutine
handle: not called CC=27 SCAR=66.3
Pod
documented
| |||||||||||||||||||||||||||||
| 512 | 0 | my ( $self, $server, $attrs_ref ) = @_; | |||||||||||||||||||||||||||
| 513 | |||||||||||||||||||||||||||||
| 514 | # check for sasl username | ||||||||||||||||||||||||||||
| 515 | 0 | ▶ | return if $self->require_sasl_username && ! $attrs_ref->{ sasl_username }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->require_sasl_username and not $attrs_ref->{"sasl_username"}
MC/DC0%0/2
$self->require_sasl_username and not $attrs_ref->{"sasl_username"}
$self->require_sasl_username
not $attrs_ref->{"sasl_username"}
Truth table0%0/3
A and not(B)
A= $self->require_sasl_usernameB= not $attrs_ref->{"sasl_username"}
| |||||||||||||||||||||||||||||
| 516 | |||||||||||||||||||||||||||||
| 517 | |||||||||||||||||||||||||||||
| 518 | # | ||||||||||||||||||||||||||||
| 519 | # CACHES | ||||||||||||||||||||||||||||
| 520 | # | ||||||||||||||||||||||||||||
| 521 | |||||||||||||||||||||||||||||
| 522 | # check all caches | ||||||||||||||||||||||||||||
| 523 | 0 | my %cache_name = (); | |||||||||||||||||||||||||||
| 524 | 0 | foreach my $db( @{ $self->used_databases } ) { | |||||||||||||||||||||||||||
| 525 | 0 | ▶ | next if $db eq 'account'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 526 | 0 | $cache_name{ $db } = "Throttle-$db-$attrs_ref->{ $db }"; | |||||||||||||||||||||||||||
| 527 | 0 | my $cached = $self->cache->get( $cache_name{ $db } ); | |||||||||||||||||||||||||||
| 528 | 0 | ▶ | if ( $cached ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 529 | 0 | $self->logger->debug0( "Cache hit for '$db' = '$attrs_ref->{ $db }'" ); | |||||||||||||||||||||||||||
| 530 | 0 | $self->go_final_state( $cached ); | |||||||||||||||||||||||||||
| 531 | } | ||||||||||||||||||||||||||||
| 532 | } | ||||||||||||||||||||||||||||
| 533 | |||||||||||||||||||||||||||||
| 534 | # check accont cache | ||||||||||||||||||||||||||||
| 535 | 0 | my $account; | |||||||||||||||||||||||||||
| 536 | 0 | ▶ | if ( $self->use_accounts ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 537 | |||||||||||||||||||||||||||||
| 538 | # and again: for any database (should be faster that way then one loop, cause we | ||||||||||||||||||||||||||||
| 539 | # reduce database reads this way! | ||||||||||||||||||||||||||||
| 540 | 0 | foreach my $db( @{ $self->used_databases } ) { | |||||||||||||||||||||||||||
| 541 | 0 | ▶ | next if $db eq 'account'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 542 | |||||||||||||||||||||||||||||
| 543 | # try find account | ||||||||||||||||||||||||||||
| 544 | 0 | $account = $self->get_account( $db => $attrs_ref->{ $db } ); | |||||||||||||||||||||||||||
| 545 | 0 | ▶ | next unless $account; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 546 | |||||||||||||||||||||||||||||
| 547 | # found account | ||||||||||||||||||||||||||||
| 548 | 0 | $cache_name{ account } = "Throttle-account-$account"; | |||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||
| 550 | # try cache | ||||||||||||||||||||||||||||
| 551 | 0 | ▶ | if ( defined( my $cached = $self->cache->get( $cache_name{ account } ) ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 552 | 0 | $self->logger->debug3( "Cache hit for account '$account' of '$db' = '$attrs_ref->{ $db }'" ); | |||||||||||||||||||||||||||
| 553 | 0 | $self->go_final_state( $cached ); | |||||||||||||||||||||||||||
| 554 | } | ||||||||||||||||||||||||||||
| 555 | |||||||||||||||||||||||||||||
| 556 | # anyway, we know who the account is for now | ||||||||||||||||||||||||||||
| 557 | 0 | last; | |||||||||||||||||||||||||||
| 558 | } | ||||||||||||||||||||||||||||
| 559 | } | ||||||||||||||||||||||||||||
| 560 | |||||||||||||||||||||||||||||
| 561 | |||||||||||||||||||||||||||||
| 562 | # | ||||||||||||||||||||||||||||
| 563 | # DATABASE | ||||||||||||||||||||||||||||
| 564 | # | ||||||||||||||||||||||||||||
| 565 | 0 | ▶ | $attrs_ref->{ account } = $account || "**UNDEFINED ACCOUNT**"; | ||||||||||||||||||||||||||
|
Condition0%0/2
$account || '**UNDEFINED ACCOUNT**'
MC/DC0%0/1
$account || '**UNDEFINED ACCOUNT**'
$account
Truth table0%0/2
A
A= $account
| |||||||||||||||||||||||||||||
| 566 | |||||||||||||||||||||||||||||
| 567 | # try database now | ||||||||||||||||||||||||||||
| 568 | CHECK_DATABASE: | ||||||||||||||||||||||||||||
| 569 | 0 | foreach my $db( @{ $self->used_databases } ) { | |||||||||||||||||||||||||||
| 570 | 0 | ▶ | next if $db eq 'account' && ! $account; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$db eq "account" and not $account
MC/DC0%0/2
$db eq "account" and not $account
$db eq "account"
not $account
Truth table0%0/3
A and not(B)
A= $db eq "account"B= not $account
| |||||||||||||||||||||||||||||
| 571 | |||||||||||||||||||||||||||||
| 572 | 0 | my @limits; | |||||||||||||||||||||||||||
| 573 | 0 | my $exception = 0; | |||||||||||||||||||||||||||
| 574 | |||||||||||||||||||||||||||||
| 575 | # we could find in exception database.. | ||||||||||||||||||||||||||||
| 576 | 0 | ▶ | if ( $self->exception_database->{ $db } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 577 | @limits = $self->database->search( throttle => $db, { | ||||||||||||||||||||||||||||
| 578 | 0 | $db => $attrs_ref->{ $db } | |||||||||||||||||||||||||||
| 579 | } ); | ||||||||||||||||||||||||||||
| 580 | 0 | ▶ | $exception ++ if @limits; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 581 | |||||||||||||||||||||||||||||
| 582 | } | ||||||||||||||||||||||||||||
| 583 | |||||||||||||||||||||||||||||
| 584 | # no exceptions? -> try fallback to defaults | ||||||||||||||||||||||||||||
| 585 | 0 | ▶ | if ( ! @limits && $self->default_limits->{ $db } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not @limits and $self->default_limits->{$db}
MC/DC0%0/2
not @limits and $self->default_limits->{$db}
not @limits
$self->default_limits->{$db}
Truth table0%0/3
not(A) and B
A= not @limitsB= $self->default_limits->{$db}
| |||||||||||||||||||||||||||||
| 586 | 0 | @limits = @{ $self->default_limits->{ $db } }; | |||||||||||||||||||||||||||
| 587 | } | ||||||||||||||||||||||||||||
| 588 | |||||||||||||||||||||||||||||
| 589 | # no limits at all -> bye | ||||||||||||||||||||||||||||
| 590 | 0 | ▶ | next CHECK_DATABASE unless @limits; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 591 | |||||||||||||||||||||||||||||
| 592 | 0 | ▶ | $self->logger->debug3( "Using ". ( $exception ? "Exception" : "Default" ). " limits for $attrs_ref->{ $db } ($db)" ) | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 593 | if @limits; | ||||||||||||||||||||||||||||
| 594 | |||||||||||||||||||||||||||||
| 595 | # go through all limits -> first rejection counts | ||||||||||||||||||||||||||||
| 596 | 0 | foreach my $limit_ref( @limits ) { | |||||||||||||||||||||||||||
| 597 | |||||||||||||||||||||||||||||
| 598 | # now .. | ||||||||||||||||||||||||||||
| 599 | 0 | my $now = time(); | |||||||||||||||||||||||||||
| 600 | |||||||||||||||||||||||||||||
| 601 | # get counter, which is the max counter for all db items | ||||||||||||||||||||||||||||
| 602 | 0 | my $count = $self->counter_read->( $db, $attrs_ref->{ $db }, $limit_ref->{ interval } ); | |||||||||||||||||||||||||||
| 603 | |||||||||||||||||||||||||||||
| 604 | 0 | $self->logger->debug3( " Limit $count / $limit_ref->{ maximum } in $limit_ref->{ interval }" ); | |||||||||||||||||||||||||||
| 605 | |||||||||||||||||||||||||||||
| 606 | # limit reached | ||||||||||||||||||||||||||||
| 607 | 0 | ▶ | if ( $limit_ref->{ maximum } >= 0 && $count >= $limit_ref->{ maximum } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$limit_ref->{"maximum"} >= 0 and $count >= $limit_ref->{"maximum"}
MC/DC0%0/2
$limit_ref->{"maximum"} >= 0 and $count >= $limit_ref->{"maximum"}
$limit_ref->{"maximum"} >= 0
$count >= $limit_ref->{"maximum"}
Truth table0%0/3
A and B
A= $limit_ref->{"maximum"} >= 0B= $count >= $limit_ref->{"maximum"}
| |||||||||||||||||||||||||||||
| 608 | |||||||||||||||||||||||||||||
| 609 | # get reject message (either special for this limit or default) | ||||||||||||||||||||||||||||
| 610 | my $reject_message_ref = | ||||||||||||||||||||||||||||
| 611 | $self->reject_messages->{ $limit_ref->{ interval } } | ||||||||||||||||||||||||||||
| 612 | 0 | ▶ | || $self->default_reject_message; | ||||||||||||||||||||||||||
|
Condition0%0/3
$self->reject_messages->{$limit_ref->{'interval'}} || $self->default_reject_message
MC/DC0%0/2
$self->reject_messages->{$limit_ref->{'interval'}} || $self->default_reject_message
$self->reject_messages->{$limit_ref->{'interval'}}
$self->default_reject_message
Truth table0%0/3
A || B
A= $self->reject_messages->{$limit_ref->{'interval'}}B= $self->default_reject_message
| |||||||||||||||||||||||||||||
| 613 | |||||||||||||||||||||||||||||
| 614 | 0 | my $reject_message = $reject_message_ref->{ message }; | |||||||||||||||||||||||||||
| 615 | |||||||||||||||||||||||||||||
| 616 | # parse output message | ||||||||||||||||||||||||||||
| 617 | 0 | $reject_message =~ s/%interval%/$limit_ref->{ interval }/g; | |||||||||||||||||||||||||||
| 618 | |||||||||||||||||||||||||||||
| 619 | 0 | my $interval_minutes = ceil( $limit_ref->{ interval } / 60 ); | |||||||||||||||||||||||||||
| 620 | 0 | $reject_message =~ s/%interval_minutes%/$interval_minutes/g; | |||||||||||||||||||||||||||
| 621 | |||||||||||||||||||||||||||||
| 622 | 0 | my $interval_hours = ceil( $limit_ref->{ interval } / 3600 ); | |||||||||||||||||||||||||||
| 623 | 0 | $reject_message =~ s/%interval_hours%/$interval_hours/g; | |||||||||||||||||||||||||||
| 624 | |||||||||||||||||||||||||||||
| 625 | 0 | my $interval_days = ceil( $limit_ref->{ interval } / 86400 ); | |||||||||||||||||||||||||||
| 626 | 0 | $reject_message =~ s/%interval_days%/$interval_days/g; | |||||||||||||||||||||||||||
| 627 | |||||||||||||||||||||||||||||
| 628 | 0 | $reject_message =~ s/%maximum%/$limit_ref->{ maximum }/g; | |||||||||||||||||||||||||||
| 629 | |||||||||||||||||||||||||||||
| 630 | # write state to cache | ||||||||||||||||||||||||||||
| 631 | 0 | my $timeout = $limit_ref->{ interval } - ( $now % $limit_ref->{ interval } ); | |||||||||||||||||||||||||||
| 632 | 0 | $self->cache->set( $cache_name{ $db }, $reject_message, $timeout ); | |||||||||||||||||||||||||||
| 633 | |||||||||||||||||||||||||||||
| 634 | # log event | ||||||||||||||||||||||||||||
| 635 | 0 | $self->logger->debug0( " !! Limit reached for '$db' = '$attrs_ref->{ $db }' ($limit_ref->{ interval })!!" ); | |||||||||||||||||||||||||||
| 636 | |||||||||||||||||||||||||||||
| 637 | # get reject code | ||||||||||||||||||||||||||||
| 638 | 0 | ▶ | my $reject_code = $reject_message_ref->{ code } || 450; | ||||||||||||||||||||||||||
|
Condition0%0/2
$reject_message_ref->{'code'} || 450
MC/DC0%0/1
$reject_message_ref->{'code'} || 450
$reject_message_ref->{'code'}
Truth table0%0/2
A
A= $reject_message_ref->{'code'}
| |||||||||||||||||||||||||||||
| 639 | |||||||||||||||||||||||||||||
| 640 | # set final state (throws exception) | ||||||||||||||||||||||||||||
| 641 | 0 | $self->go_final_state( $reject_code => $reject_message ); | |||||||||||||||||||||||||||
| 642 | } | ||||||||||||||||||||||||||||
| 643 | else { | ||||||||||||||||||||||||||||
| 644 | 0 | $self->logger->debug3( " No limit issue" ); | |||||||||||||||||||||||||||
| 645 | } | ||||||||||||||||||||||||||||
| 646 | |||||||||||||||||||||||||||||
| 647 | |||||||||||||||||||||||||||||
| 648 | # increase counter now | ||||||||||||||||||||||||||||
| 649 | 0 | $self->counter_increment->( $db, $attrs_ref->{ $db }, $limit_ref->{ interval } ); | |||||||||||||||||||||||||||
| 650 | } | ||||||||||||||||||||||||||||
| 651 | } | ||||||||||||||||||||||||||||
| 652 | } | ||||||||||||||||||||||||||||
| 653 | |||||||||||||||||||||||||||||
| 654 | |||||||||||||||||||||||||||||
| 655 | |||||||||||||||||||||||||||||
| 656 | =head2 get_account $db, $value | ||||||||||||||||||||||||||||
| 657 | |||||||||||||||||||||||||||||
| 658 | returns account by attribute lookup | ||||||||||||||||||||||||||||
| 659 | |||||||||||||||||||||||||||||
| 660 | my $account = $self->get_account( sender_domain => 'sender.tld' ); | ||||||||||||||||||||||||||||
| 661 | |||||||||||||||||||||||||||||
| 662 | =cut | ||||||||||||||||||||||||||||
| 663 | |||||||||||||||||||||||||||||
| 664 | 0 | ▶ | sub get_account { | ||||||||||||||||||||||||||
|
Subroutine
get_account: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 665 | 0 | my ( $self, $db, $value ) = @_; | |||||||||||||||||||||||||||
| 666 | |||||||||||||||||||||||||||||
| 667 | # from cache ?? | ||||||||||||||||||||||||||||
| 668 | 0 | my $cache_name = "Throttle-account-$db-$value"; | |||||||||||||||||||||||||||
| 669 | 0 | ▶ | if ( defined( my $cached = $self->cache->get( $cache_name ) ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 670 | 0 | return $cached; | |||||||||||||||||||||||||||
| 671 | } | ||||||||||||||||||||||||||||
| 672 | |||||||||||||||||||||||||||||
| 673 | # from database .. | ||||||||||||||||||||||||||||
| 674 | 0 | my $ref = $self->database->get( throttle => $db => { $db => $value } ); | |||||||||||||||||||||||||||
| 675 | 0 | ▶ | if ( $ref && $ref->{ account } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$ref and $ref->{"account"}
MC/DC0%0/2
$ref and $ref->{"account"}
$ref
$ref->{"account"}
Truth table0%0/3
A and B
A= $refB= $ref->{"account"}
| |||||||||||||||||||||||||||||
| 676 | |||||||||||||||||||||||||||||
| 677 | # save to cache | ||||||||||||||||||||||||||||
| 678 | 0 | $self->cache->set( $cache_name => $ref->{ account } ); | |||||||||||||||||||||||||||
| 679 | 0 | return $ref->{ account }; | |||||||||||||||||||||||||||
| 680 | } | ||||||||||||||||||||||||||||
| 681 | |||||||||||||||||||||||||||||
| 682 | # nothing found | ||||||||||||||||||||||||||||
| 683 | 0 | return; | |||||||||||||||||||||||||||
| 684 | } | ||||||||||||||||||||||||||||
| 685 | |||||||||||||||||||||||||||||
| 686 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 687 | |||||||||||||||||||||||||||||
| 688 | Ulrich Kautz <uk@fortrabbit.de> | ||||||||||||||||||||||||||||
| 689 | |||||||||||||||||||||||||||||
| 690 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||
| 691 | |||||||||||||||||||||||||||||
| 692 | Copyright (c) 2010 the L</AUTHOR> as listed above | ||||||||||||||||||||||||||||
| 693 | |||||||||||||||||||||||||||||
| 694 | =head1 LICENCSE | ||||||||||||||||||||||||||||
| 695 | |||||||||||||||||||||||||||||
| 696 | This library is free software and may be distributed under the same terms as perl itself. | ||||||||||||||||||||||||||||
| 697 | |||||||||||||||||||||||||||||
| 698 | =cut | ||||||||||||||||||||||||||||
| 699 | |||||||||||||||||||||||||||||
| 700 | |||||||||||||||||||||||||||||
| 701 | |||||||||||||||||||||||||||||
| 702 | |||||||||||||||||||||||||||||
| 703 | 1 | 1; | |||||||||||||||||||||||||||