| 1 | package Net::SMTP::Bulk::Coro; | ||
| 2 | |||
| 3 | use 5.006; | ||
| 4 | use strict; | ||
| 5 | use warnings FATAL => 'all'; | ||
| 6 | use Encode; | ||
| 7 | use Coro; | ||
| 8 | use Coro::Handle; | ||
| 9 | use AnyEvent::Socket; | ||
| 10 | |||
| 11 | |||
| 12 | =head1 NAME | ||
| 13 | |||
| 14 | Net::SMTP::Bulk::Coro - NonBlocking batch SMTP using Net::SMTP interface via Coro+AnyEvent | ||
| 15 | |||
| 16 | =head1 VERSION | ||
| 17 | |||
| 18 | Version 0.23 | ||
| 19 | |||
| 20 | =cut | ||
| 21 | |||
| 22 | our $VERSION = '0.23'; | ||
| 23 | |||
| 24 | |||
| 25 | =head1 SYNOPSIS | ||
| 26 | |||
| 27 | This is a rewrite of Net::SMTP using AnyEvent and Coro as a backbone. It supports AUTH, SSL and STARTTLS as well. This module can be used as a drop in replacement for Net::SMTP. At this point this module is EXPIREMENTAL, so use at your own risk. Functionality can change at any time. | ||
| 28 | |||
| 29 | use Net::SMTP::Bulk; | ||
| 30 | |||
| 31 | my $smtp = Net::SMTP::Bulk::Coro->new($server, %options); | ||
| 32 | |||
| 33 | |||
| 34 | See Net::SMTP for syntax. | ||
| 35 | |||
| 36 | =head1 SUBROUTINES/METHODS | ||
| 37 | |||
| 38 | =head2 new($server,%options) | ||
| 39 | |||
| 40 | =head2 new(%options) | ||
| 41 | |||
| 42 | Options: | ||
| 43 | Host - Hostname or IP address | ||
| 44 | |||
| 45 | Port - The port to which to connect to on the server (default: 25) | ||
| 46 | |||
| 47 | Hello - The domain name you wish to connect to (default: [same as server]) | ||
| 48 | |||
| 49 | Debug - Debug information (0-10 depending on level) (default: 0 [disabled]) OPTIONAL | ||
| 50 | |||
| 51 | Secure - If you wish to use a secure connection. (0 - None, 1 - SSL [no verify], 2 - SSL [verify], 3 - STARTTLS [no verify], 4 - STARTTLS [verify]) OPTIONAL [Requires Net::SSLeay] | ||
| 52 | |||
| 53 | Threads - How many concurrent connections per host (default: 2) OPTIONAL | ||
| 54 | |||
| 55 | Encode - Encode socket( 1: utf8 ) | ||
| 56 | |||
| 57 | Callbacks - You can supply callback functions on certain conditions, these conditions include: | ||
| 58 | |||
| 59 | connect_pass,connect_fail,auth_pass,auth_fail,reconnect_pass,reconnect_fail,pass,fail,hang | ||
| 60 | |||
| 61 | The callback must return 1 it to follow proper proceedures. You can overwrite the defaults by supplying a different return. | ||
| 62 | |||
| 63 | 1 - Default | ||
| 64 | |||
| 65 | 101 - Remove Thread permanently | ||
| 66 | |||
| 67 | 102 - Remove thread temporarily and reconnect at end of batch | ||
| 68 | |||
| 69 | 103 - Remove thread temporarily and restart at end of batch (If your using an SMTP server with short timeout, it is suggested to use this over reconnect) | ||
| 70 | |||
| 71 | 104 - Remove Thread temporarily | ||
| 72 | |||
| 73 | 202 - Reconnect now | ||
| 74 | |||
| 75 | 203 - Restart now | ||
| 76 | |||
| 77 | =head2 new(%options, Hosts=>[\%options2,\%options3]) | ||
| 78 | |||
| 79 | You can supply multiple hosts in an array. | ||
| 80 | |||
| 81 | |||
| 82 | =head2 auth( [ MECHANISM,] USERNAME, PASSWORD ) | ||
| 83 | |||
| 84 | *Requires Authen::SASL | ||
| 85 | |||
| 86 | =head2 mail( ADDRESS ) | ||
| 87 | |||
| 88 | =head2 to( ADDRESS ) | ||
| 89 | |||
| 90 | =head2 data() | ||
| 91 | |||
| 92 | =head2 datasend( DATA ) | ||
| 93 | |||
| 94 | =head2 dataend( DATA ) | ||
| 95 | |||
| 96 | =head2 reconnect( ) | ||
| 97 | |||
| 98 | =head2 quit( ) | ||
| 99 | |||
| 100 | =cut | ||
| 101 | |||
| 102 | sub new { | ||
| 103 | my $class=shift; | ||
| 104 | my %new=@_; | ||
| 105 | my $self={}; | ||
| 106 | |||
| 107 | if ($#_ % 2 == 0) { | ||
| 108 | $new{Host}=shift; | ||
| 109 | } | ||
| 110 | |||
| 111 | bless($self, $class||'Net::SMTP::Bulk::Coro'); | ||
| 112 | |||
| 113 | $self->{new}=\%new; | ||
| 114 | $self->{encode} = ( ($new{Encode}||'') eq '1' ) ? 'utf8':''; | ||
| 115 | $self->{debug} = (($new{Debug}||0) >= 1) ? int($new{Debug}):0; | ||
| 116 | |||
| 117 | if ( ($new{DebugPath}||'') ne '' ) { | ||
| 118 | $self->{debug_path} = ( lc($new{DebugPath}) eq 'default' ) ? 'debug_[HOST]_[THREAD].txt':$new{DebugPath}; | ||
| 119 | } | ||
| 120 | |||
| 121 | $self->{func} = $new{Callbacks}; | ||
| 122 | $self->{defaults}={ | ||
| 123 | threads=>$new{Threads}||2, | ||
| 124 | port=>$new{Port}||25, | ||
| 125 | timeout=>$new{Timeout}||30, | ||
| 126 | secure=>$new{Secure}||0 | ||
| 127 | }; | ||
| 128 | |||
| 129 | |||
| 130 | if (exists($new{Hosts})) { | ||
| 131 | $self->_PREPARE($new{Hosts}); | ||
| 132 | } else { | ||
| 133 | $self->_PREPARE([\%new]); | ||
| 134 | } | ||
| 135 | |||
| 136 | return $self; | ||
| 137 | } | ||
| 138 | |||
| 139 | sub mail { | ||
| 140 | my $self=shift; | ||
| 141 | my $str=shift; | ||
| 142 | my $k=shift||$self->{last}[0]; | ||
| 143 | $str=~s/\n$//s; | ||
| 144 | push( @{$self->{queue}{ $k->[0] }{ $k->[1] }}, ['MAIL',250,'MAIL FROM: '.$str] ); | ||
| 145 | |||
| 146 | } | ||
| 147 | |||
| 148 | sub to { | ||
| 149 | my $self=shift; | ||
| 150 | my $str=shift; | ||
| 151 | my $k=shift||$self->{last}[0]; | ||
| 152 | $str=~s/\n$//s; | ||
| 153 | push( @{$self->{queue}{ $k->[0] }{ $k->[1] }}, ['TO',250,'RCPT TO: '.$str] ); | ||
| 154 | } | ||
| 155 | |||
| 156 | sub data { | ||
| 157 | my $self=shift; | ||
| 158 | my $k=shift||$self->{last}[0]; | ||
| 159 | |||
| 160 | push( @{$self->{queue}{ $k->[0] }{ $k->[1] }}, ['DATA',354,'DATA'] ); | ||
| 161 | $self->{data}{ $k->[0] }{ $k->[1] }=''; | ||
| 162 | } | ||
| 163 | |||
| 164 | sub datasend { | ||
| 165 | my $self=shift; | ||
| 166 | my $str=shift; | ||
| 167 | my $k=shift||$self->{last}[0]; | ||
| 168 | $str=~s/\n$//s; | ||
| 169 | $self->{data}{ $k->[0] }{ $k->[1] }.=$str."\n"; | ||
| 170 | } | ||
| 171 | |||
| 172 | sub dataend { | ||
| 173 | my $self=shift; | ||
| 174 | my $k=shift||$self->{last}[0]; | ||
| 175 | |||
| 176 | push( @{$self->{queue}{ $k->[0] }{ $k->[1] }}, ['DATAEND',250,$self->{data}{ $k->[0] }{ $k->[1] }."\r\n."] ); | ||
| 177 | |||
| 178 | $self->{queue_size}=$#{$self->{queue}{ $k->[0] }{ $k->[1] }} if $self->{queue_size} < $#{$self->{queue}{ $k->[0] }{ $k->[1] }}; | ||
| 179 | |||
| 180 | |||
| 181 | |||
| 182 | $self->_BULK() if $self->{last}[1] == $#{ $self->{order} }; | ||
| 183 | |||
| 184 | $self->_NEXT(); | ||
| 185 | } | ||
| 186 | |||
| 187 | sub auth { | ||
| 188 | my $self=shift; | ||
| 189 | my $user; | ||
| 190 | my $pass; | ||
| 191 | my $k; | ||
| 192 | my $mech; | ||
| 193 | |||
| 194 | if ($#_ == 3 or $#_ == 2 and ref($_[2]) ne 'ARRAY') { | ||
| 195 | $mech=shift; | ||
| 196 | } | ||
| 197 | |||
| 198 | $user=shift||''; | ||
| 199 | $pass=shift||''; | ||
| 200 | $k=shift||$self->{last}[0]; | ||
| 201 | |||
| 202 | require MIME::Base64; | ||
| 203 | require Authen::SASL; | ||
| 204 | |||
| 205 | if ($self->{auth}{ $k->[0] }{ $k->[1] }[0] == 1) { | ||
| 206 | #already authenticated | ||
| 207 | } else { | ||
| 208 | |||
| 209 | $mech ||= uc(join(' ',(@{$self->{header}{$k->[0]}{$k->[1]}{auth}}))); | ||
| 210 | $self->{objects}{$k->[0]}{$k->[1]}{sasl} = Authen::SASL->new( | ||
| 211 | mechanism => $mech, | ||
| 212 | callback => { | ||
| 213 | user => $user, | ||
| 214 | pass => $pass, | ||
| 215 | authname => $user, | ||
| 216 | } | ||
| 217 | ) if (!exists($self->{objects}{$k->[0]}{$k->[1]}{sasl})); | ||
| 218 | $self->{objects}{ $k->[0] }{ $k->[1] }{sasl_client} = $self->{objects}{ $k->[0] }{ $k->[1] }{sasl}->client_new("smtp", $self->{host}{$k->[0]},1); | ||
| 219 | |||
| 220 | |||
| 221 | $self->_WRITE($k,'AUTH '.$self->{objects}{ $k->[0] }{ $k->[1] }{sasl_client}->mechanism); | ||
| 222 | $self->_READ($k); | ||
| 223 | if (my $str = $self->{objects}{ $k->[0] }{ $k->[1] }{sasl_client}->client_start) { | ||
| 224 | $self->_WRITE($k,MIME::Base64::encode_base64($str, '')); | ||
| 225 | } | ||
| 226 | |||
| 227 | do { | ||
| 228 | my $msg=MIME::Base64::decode_base64($self->{buffer}{ $k->[0] }{ $k->[1] }); | ||
| 229 | |||
| 230 | |||
| 231 | $self->_WRITE($k, | ||
| 232 | MIME::Base64::encode_base64($self->{objects}{$k->[0]}{$k->[1]}{sasl_client}->client_step($msg),'') | ||
| 233 | ); | ||
| 234 | $self->_READ($k); | ||
| 235 | } while($self->{status_code}{ $k->[0] }{ $k->[1] } == 334); | ||
| 236 | |||
| 237 | if ($self->{status_code}{ $k->[0] }{ $k->[1] } == 235) { | ||
| 238 | $self->{auth}{ $k->[0] }{ $k->[1] }=[1,$mech]; | ||
| 239 | my $r=$self->_FUNC('auth_pass',$self,$k,0,$self->{queue}{ $k->[0] }{ $k->[1] }); | ||
| 240 | |||
| 241 | if ($r != 1) { | ||
| 242 | $self->_FUNC_CALLBACK($k,0,$r); | ||
| 243 | } | ||
| 244 | return 1; | ||
| 245 | |||
| 246 | } else { | ||
| 247 | |||
| 248 | #AUTH FAILED | ||
| 249 | my $r=$self->_FUNC('auth_fail',$self,$k,0,$self->{queue}{ $k->[0] }{ $k->[1] }); | ||
| 250 | |||
| 251 | if ($r == 1) { | ||
| 252 | $self->_FUNC_CALLBACK($k,0,101); #remove thread | ||
| 253 | } else { | ||
| 254 | $self->_FUNC_CALLBACK($k,0,$r); | ||
| 255 | } | ||
| 256 | |||
| 257 | return 0; | ||
| 258 | } | ||
| 259 | |||
| 260 | |||
| 261 | } | ||
| 262 | |||
| 263 | |||
| 264 | |||
| 265 | } | ||
| 266 | |||
| 267 | sub quit { | ||
| 268 | my $self=shift; | ||
| 269 | |||
| 270 | |||
| 271 | |||
| 272 | $self->_BULK(); | ||
| 273 | |||
| 274 | foreach my $dfh ( keys(%{ $self->{debug_fh} }) ) { | ||
| 275 | close($self->{debug_fh}{$dfh}) | ||
| 276 | } | ||
| 277 | foreach my $h ( keys(%{ $self->{fh} }) ) { | ||
| 278 | foreach my $t ( keys(%{ $self->{fh}{$h} }) ) { | ||
| 279 | $self->{fh}{$h}{$t}->close(); | ||
| 280 | delete($self->{fh}{$h}{$t}); | ||
| 281 | } | ||
| 282 | } | ||
| 283 | } | ||
| 284 | |||
| 285 | |||
| 286 | sub reconnect{ | ||
| 287 | my $self=shift; | ||
| 288 | my $k=shift||$self->{last}[0]; | ||
| 289 | |||
| 290 | |||
| 291 | $self->{fh}{ $k->[0] }{ $k->[1] }->close() if defined($self->{fh}{ $k->[0] }{ $k->[1] }); | ||
| 292 | |||
| 293 | $self->_CONNECT($k); | ||
| 294 | $self->_SECURECHECK(); | ||
| 295 | $self->_HELO($k); | ||
| 296 | $self->_READ($k); | ||
| 297 | $self->_HEADER($k); | ||
| 298 | $self->_STARTTLS($k) if ($self->{secure}{ $k->[0] } == 3 or $self->{secure}{ $k->[0] } == 4); | ||
| 299 | if ( $self->{auth}{ $k->[0] }{ $k->[1] }[0] == 1 ) { | ||
| 300 | $self->{auth}{ $k->[0] }{ $k->[1] }[0]=0; | ||
| 301 | my $auth=$self->auth($self->{auth}{ $k->[0] }{ $k->[1] }[1],'','',$k); | ||
| 302 | |||
| 303 | $self->{order}[ $self->{last}[1] ][2]=2; | ||
| 304 | |||
| 305 | |||
| 306 | return 1 if $auth == 1; | ||
| 307 | } | ||
| 308 | return 0; | ||
| 309 | } | ||
| 310 | |||
| 311 | ######################################################################### | ||
| 312 | |||
| 313 | sub _BULK { | ||
| 314 | my $self=shift; | ||
| 315 | |||
| 316 | |||
| 317 | |||
| 318 | foreach my $k (@{$self->{order}}) { | ||
| 319 | |||
| 320 | $self->{stats}{ $k->[0] }{ $k->[1] }{queue}{total}=($#{$self->{queue}{ $k->[0] }{ $k->[1] }}+1); | ||
| 321 | |||
| 322 | $self->_DEBUG($k,'Set Queue : '.$self->{stats}{ $k->[0] }{ $k->[1] }{queue}{total},5) if $self->{debug} >= 1; | ||
| 323 | |||
| 324 | } | ||
| 325 | |||
| 326 | foreach my $q ( 0..$self->{queue_size} ) { | ||
| 327 | |||
| 328 | |||
| 329 | |||
| 330 | foreach my $k (@{$self->{order}}) { | ||
| 331 | if ($k->[2] == 1 and exists($self->{queue}{ $k->[0] }{ $k->[1] }[$q])) { | ||
| 332 | $self->_WRITE($k,$self->{queue}{ $k->[0] }{ $k->[1] }[$q][2]); | ||
| 333 | } | ||
| 334 | |||
| 335 | } | ||
| 336 | |||
| 337 | foreach my $order (0..$#{$self->{order}}) { | ||
| 338 | my $k=$self->{order}[$order]; | ||
| 339 | |||
| 340 | if ($k->[2] == 1 and exists($self->{queue}{ $k->[0] }{ $k->[1] }[$q])) { | ||
| 341 | $self->_READ($k); | ||
| 342 | |||
| 343 | if ($self->{status_code}{ $k->[0] }{ $k->[1] } == $self->{queue}{ $k->[0] }{ $k->[1] }[$q][1] ) { | ||
| 344 | #GOOD | ||
| 345 | no strict; | ||
| 346 | my $r=$self->_FUNC('pass',$self,$k,$q,$self->{queue}{ $k->[0] }{ $k->[1] }); | ||
| 347 | |||
| 348 | if ($r != 1) { | ||
| 349 | $self->_FUNC_CALLBACK($k,$q,$r); | ||
| 350 | } | ||
| 351 | |||
| 352 | } else { | ||
| 353 | #BAD | ||
| 354 | if ( $self->{status_code}{ $k->[0] }{ $k->[1] } == -1 ) { | ||
| 355 | |||
| 356 | my $r=$self->_FUNC('hang',$self,$k,$q,$self->{queue}{ $k->[0] }{ $k->[1] }); | ||
| 357 | |||
| 358 | if ($r == 1) { | ||
| 359 | #reconnect | ||
| 360 | $self->_FUNC_CALLBACK($k,$q,102); | ||
| 361 | |||
| 362 | } else { | ||
| 363 | $self->_FUNC_CALLBACK($k,$q,$r); | ||
| 364 | } | ||
| 365 | |||
| 366 | } else { | ||
| 367 | #error | ||
| 368 | my $r=$self->_FUNC('fail',$self,$k,$q,$self->{queue}{ $k->[0] }{ $k->[1] }); | ||
| 369 | |||
| 370 | if ($r != 1) { | ||
| 371 | $self->_FUNC_CALLBACK($k,$q,$r); | ||
| 372 | } | ||
| 373 | |||
| 374 | |||
| 375 | |||
| 376 | } | ||
| 377 | |||
| 378 | } | ||
| 379 | |||
| 380 | } | ||
| 381 | } | ||
| 382 | |||
| 383 | |||
| 384 | } | ||
| 385 | |||
| 386 | |||
| 387 | |||
| 388 | if (exists($self->{callback})) { | ||
| 389 | foreach my $call (keys(%{$self->{callback}})) { | ||
| 390 | my @callback=@{$self->{callback}{$call}}; | ||
| 391 | delete($self->{callback}{$call}); | ||
| 392 | $self->_FUNC_CALLBACK( @callback ); | ||
| 393 | } | ||
| 394 | delete($self->{callback}); | ||
| 395 | } | ||
| 396 | |||
| 397 | |||
| 398 | foreach my $order (0..$#{$self->{order}}) { | ||
| 399 | my $k=$self->{order}[$order]; | ||
| 400 | $self->{order}[ $order ][2]=1 if $self->{order}[ $order ][2] == 2; | ||
| 401 | $self->{queue}{ $k->[0] }{ $k->[1] }=[]; | ||
| 402 | |||
| 403 | } | ||
| 404 | |||
| 405 | } | ||
| 406 | |||
| 407 | sub _FUNC { | ||
| 408 | my $self=shift; | ||
| 409 | no strict; | ||
| 410 | my $func=shift; | ||
| 411 | return &{$self->{func}{$func}}(@_) if exists($self->{func}{$func}); | ||
| 412 | return 1; | ||
| 413 | } | ||
| 414 | |||
| 415 | sub _FUNC_CALLBACK { | ||
| 416 | my $self=shift; | ||
| 417 | my $k=shift; | ||
| 418 | my $q=shift; | ||
| 419 | my $r=shift; | ||
| 420 | |||
| 421 | if ($r == 101) { | ||
| 422 | #remove thread | ||
| 423 | $self->_DEBUG($k,'++REMOVE THREAD PERM(101)++') if $self->{debug} >= 1; | ||
| 424 | $k->[2]=0; | ||
| 425 | } elsif ($r == 102) { | ||
| 426 | #temp remove thread and reconnect in the end | ||
| 427 | $self->_DEBUG($k,'++REMOVE THREAD TEMP(102)++') if $self->{debug} >= 1; | ||
| 428 | $k->[2]=2; | ||
| 429 | $self->{callback}{'102:'.$k->[0].':'.$k->[1]}=[$k,$q,202]; | ||
| 430 | } elsif ($r == 103) { | ||
| 431 | #temp remove thread and restart in the end | ||
| 432 | $self->_DEBUG($k,'++REMOVE THREAD TEMP(103)++') if $self->{debug} >= 1; | ||
| 433 | $k->[2]=2; | ||
| 434 | $self->{callback}{'103'}=[$k,$q,203]; | ||
| 435 | } elsif ($r == 104) { | ||
| 436 | #temp remove thread | ||
| 437 | $self->_DEBUG($k,'++REMOVE THREAD TEMP(104)++') if $self->{debug} >= 1; | ||
| 438 | $k->[2]=2; | ||
| 439 | } elsif ($r == 202) { | ||
| 440 | #reconnect now | ||
| 441 | $self->_DEBUG($k,'++RECONNECT(202)++') if $self->{debug} >= 1; | ||
| 442 | my $reconnect=$self->reconnect($k); | ||
| 443 | if ($reconnect == 1) { | ||
| 444 | my $r2=$self->_FUNC('reconnect_pass',$self,$k,$q,$self->{queue}{ $k->[0] }{ $k->[1] }); | ||
| 445 | |||
| 446 | if ($r2 != 1) { | ||
| 447 | $self->_FUNC_CALLBACK($k,0,$r2); | ||
| 448 | } | ||
| 449 | } else { | ||
| 450 | my $r2=$self->_FUNC('reconnect_fail',$self,$k,$q,$self->{queue}{ $k->[0] }{ $k->[1] }); | ||
| 451 | |||
| 452 | if ($r2 == 1) { | ||
| 453 | $self->_FUNC_CALLBACK($k,0,101); #remove thread | ||
| 454 | } else { | ||
| 455 | $self->_FUNC_CALLBACK($k,0,$r2); | ||
| 456 | } | ||
| 457 | |||
| 458 | } | ||
| 459 | } elsif ($r == 203) { | ||
| 460 | #restart now | ||
| 461 | $self->_DEBUG($k,'++RESTART(203)++') if $self->{debug} >= 1; | ||
| 462 | foreach my $dfh ( keys(%{ $self->{debug_fh} }) ) { | ||
| 463 | close($self->{debug_fh}{$dfh}) | ||
| 464 | } | ||
| 465 | foreach my $h ( keys(%{ $self->{fh} }) ) { | ||
| 466 | foreach my $t ( keys(%{ $self->{fh}{$h} }) ) { | ||
| 467 | $self->{fh}{$h}{$t}->close(); | ||
| 468 | delete($self->{fh}{$h}{$t}); | ||
| 469 | } | ||
| 470 | } | ||
| 471 | |||
| 472 | |||
| 473 | if (exists($self->{new}{Hosts})) { | ||
| 474 | $self->_PREPARE($self->{new}{Hosts}); | ||
| 475 | } else { | ||
| 476 | $self->_PREPARE([$self->{new}]); | ||
| 477 | } | ||
| 478 | |||
| 479 | } | ||
| 480 | |||
| 481 | |||
| 482 | } | ||
| 483 | |||
| 484 | sub _PREPARE { | ||
| 485 | my $self=shift; | ||
| 486 | my $hosts=shift; | ||
| 487 | $self->{order}=[]; | ||
| 488 | |||
| 489 | my $id=time; | ||
| 490 | |||
| 491 | foreach my $i ( 0..$#{$hosts} ) { | ||
| 492 | |||
| 493 | my %new=( %{$hosts->[$i]} ); | ||
| 494 | $self->{host}{ $new{Host} }||=$new{Host}; | ||
| 495 | $self->{last}=[[$new{Host},0],0] if $i == 0; | ||
| 496 | if ($self->{host}{ $new{Host} }=~s/\:(\d+?)$//is) { | ||
| 497 | $self->{port}{ $new{Host} }=$1; | ||
| 498 | } | ||
| 499 | |||
| 500 | $self->{secure}{ $new{Host} }=$new{Secure}||$self->{defaults}{secure}; | ||
| 501 | $self->{port}{ $new{Host} }||=$new{Port}||$self->{defaults}{port}; | ||
| 502 | $self->{helo}{ $new{Host} }=$new{Hello}||$self->{host}{ $new{Host} }; | ||
| 503 | $self->{timeout}{ $new{Host} }=$new{Timeout}||$self->{defaults}{timeout}; | ||
| 504 | $self->{threads}{ $new{Host} }=($new{Threads}||$self->{defaults}{threads}) - 1; | ||
| 505 | $self->{queue_size}=-1; | ||
| 506 | |||
| 507 | foreach my $t ( 0..$self->{threads}{ $new{Host} } ) { | ||
| 508 | if ( exists($self->{debug_path}) ) { | ||
| 509 | my $path=$self->_STRFTIME(''.$self->{debug_path},time); | ||
| 510 | $path=~s/\[HOST\]/$new{Host}/gs; | ||
| 511 | $path=~s/\[THREAD\]/$t/gs; | ||
| 512 | |||
| 513 | $self->_MKDIRF($path); | ||
| 514 | open( $self->{debug_fh}{ $new{Host}.':'.$t } , '>>'.$path ); | ||
| 515 | binmode( $self->{debug_fh}{ $new{Host}.':'.$t } , ':utf8' ); | ||
| 516 | } | ||
| 517 | |||
| 518 | |||
| 519 | $self->{auth}{ $new{Host} }{$t}=[0,'']; | ||
| 520 | $self->{queue}{ $new{Host} }{$t}=[]; | ||
| 521 | |||
| 522 | $self->{stats}{ $new{Host} }{$t}{queue}{id}=$id; | ||
| 523 | |||
| 524 | push(@{$self->{order}}, [$new{Host},$t,1] ); | ||
| 525 | |||
| 526 | $self->_CONNECT([$new{Host},$t]); | ||
| 527 | |||
| 528 | } | ||
| 529 | |||
| 530 | $self->_SECURECHECK(); | ||
| 531 | |||
| 532 | foreach my $t ( 0..$self->{threads}{ $new{Host} } ) { | ||
| 533 | $self->_HELO([$new{Host},$t]); | ||
| 534 | } | ||
| 535 | foreach my $t ( 0..$self->{threads}{ $new{Host} } ) { | ||
| 536 | $self->_READ([$new{Host},$t]); | ||
| 537 | $self->_HEADER([$new{Host},$t]); | ||
| 538 | $self->_STARTTLS([$new{Host},$t]) if ($self->{secure}{ $new{Host} } == 3 or $self->{secure}{ $new{Host} } == 4); | ||
| 539 | } | ||
| 540 | |||
| 541 | |||
| 542 | } | ||
| 543 | } | ||
| 544 | |||
| 545 | sub _CONNECT { | ||
| 546 | my $self=shift; | ||
| 547 | my $k=shift; | ||
| 548 | |||
| 549 | my $cb=Coro::rouse_cb; | ||
| 550 | my $g=tcp_connect($self->{host}{ $k->[0] }, $self->{port}{ $k->[0] }, | ||
| 551 | #Coro::rouse_cb | ||
| 552 | sub{ | ||
| 553 | my $sock=$_[0]; | ||
| 554 | $self->{sock}{ $k->[0] }{ $k->[1] }=$sock; | ||
| 555 | $self->_SECURE($k,$sock) if ($self->{secure}{ $k->[0] } == 1 or $self->{secure}{ $k->[0] } == 2); | ||
| 556 | $cb->($sock); | ||
| 557 | |||
| 558 | } | ||
| 559 | ); | ||
| 560 | |||
| 561 | $self->{fh}{ $k->[0] }{ $k->[1] }=unblock +(Coro::rouse_wait)[0]; | ||
| 562 | |||
| 563 | |||
| 564 | |||
| 565 | |||
| 566 | |||
| 567 | } | ||
| 568 | |||
| 569 | sub _HELO { | ||
| 570 | my $self=shift; | ||
| 571 | my $k=shift; | ||
| 572 | |||
| 573 | |||
| 574 | my $fh= $self->{fh}{ $k->[0] }{ $k->[1] }; | ||
| 575 | my $nb_fh = $fh->fh; | ||
| 576 | my $buf = \$fh->rbuf; | ||
| 577 | |||
| 578 | while () { | ||
| 579 | # now use buffer contents, modifying | ||
| 580 | # if necessary to reflect the removed data | ||
| 581 | |||
| 582 | last if $$buf ne ""; # we have leftover data | ||
| 583 | |||
| 584 | # read another buffer full of data | ||
| 585 | $fh->readable or die "end of file"; | ||
| 586 | sysread $nb_fh, $$buf, 8192; | ||
| 587 | } | ||
| 588 | my $status=-1; | ||
| 589 | my $head=''; | ||
| 590 | foreach my $line (split/[\r\n]+/,$$buf) { | ||
| 591 | $line=~m/^((\d{3})[ \-](.*?)[\r\n]*?)$/is; | ||
| 592 | $self->_DEBUG($k,$1) if $self->{debug} >= 1; | ||
| 593 | $status = lc($2); | ||
| 594 | $head = lc($3); | ||
| 595 | } | ||
| 596 | |||
| 597 | $fh->rbuf=''; | ||
| 598 | |||
| 599 | if ($status == 220) { | ||
| 600 | my $r=$self->_FUNC('connect_pass',$self,$k,0,$self->{queue}{ $k->[0] }{ $k->[1] }); | ||
| 601 | |||
| 602 | if ($r != 1) { | ||
| 603 | $self->_FUNC_CALLBACK($k,0,$r); | ||
| 604 | } | ||
| 605 | |||
| 606 | } else { | ||
| 607 | #FAIL TO CONNECT | ||
| 608 | my $r=$self->_FUNC('connect_fail',$self,$k,0,$self->{queue}{ $k->[0] }{ $k->[1] }); | ||
| 609 | |||
| 610 | if ($r == 1) { | ||
| 611 | $self->_FUNC_CALLBACK($k,0,101); #remove thread | ||
| 612 | } else { | ||
| 613 | $self->_FUNC_CALLBACK($k,0,$r); | ||
| 614 | } | ||
| 615 | |||
| 616 | |||
| 617 | } | ||
| 618 | |||
| 619 | |||
| 620 | |||
| 621 | |||
| 622 | =head2 | ||
| 623 | $self->_READ($k); | ||
| 624 | |||
| 625 | |||
| 626 | |||
| 627 | if ($self->{status_code}{ $k->[0] }{ $k->[1] } == 220) { | ||
| 628 | my $r=$self->_FUNC('connect_pass',$self,$k,0,$self->{queue}{ $k->[0] }{ $k->[1] }); | ||
| 629 | |||
| 630 | if ($r != 1) { | ||
| 631 | $self->_FUNC_CALLBACK($k,0,$r); | ||
| 632 | } | ||
| 633 | |||
| 634 | } else { | ||
| 635 | #FAIL TO CONNECT | ||
| 636 | my $r=$self->_FUNC('connect_fail',$self,$k,0,$self->{queue}{ $k->[0] }{ $k->[1] }); | ||
| 637 | |||
| 638 | if ($r == 1) { | ||
| 639 | $self->_FUNC_CALLBACK($k,0,101); #remove thread | ||
| 640 | } else { | ||
| 641 | $self->_FUNC_CALLBACK($k,0,$r); | ||
| 642 | } | ||
| 643 | |||
| 644 | |||
| 645 | } | ||
| 646 | =cut | ||
| 647 | |||
| 648 | |||
| 649 | $self->_WRITE($k,'EHLO '.$self->{helo}{ $k->[0] }); | ||
| 650 | |||
| 651 | |||
| 652 | } | ||
| 653 | |||
| 654 | sub _NEXT { | ||
| 655 | my $self=shift; | ||
| 656 | my $k=shift; | ||
| 657 | |||
| 658 | my @next; | ||
| 659 | |||
| 660 | while (!exists($next[0])) { | ||
| 661 | $self->{last}[1]++; | ||
| 662 | |||
| 663 | if (exists($self->{order}[ $self->{last}[1] ])) { | ||
| 664 | if ($self->{order}[ $self->{last}[1] ][2]==1) { | ||
| 665 | @next=( $self->{order}[ $self->{last}[1] ], $self->{last}[1] ); | ||
| 666 | } | ||
| 667 | } else { | ||
| 668 | @next=($self->{order}[0],0); | ||
| 669 | } | ||
| 670 | |||
| 671 | |||
| 672 | |||
| 673 | } | ||
| 674 | |||
| 675 | |||
| 676 | $self->{last}=\@next; | ||
| 677 | } | ||
| 678 | |||
| 679 | sub _STARTTLS { | ||
| 680 | my $self=shift; | ||
| 681 | my $k=shift; | ||
| 682 | |||
| 683 | if ( exists($self->{header}{ $k->[0] }{ $k->[1] }{starttls}) ) { | ||
| 684 | $self->_WRITE($k,'STARTTLS'); | ||
| 685 | $self->_READ($k); | ||
| 686 | $self->_SECURE($k,$self->{sock}{ $k->[0] }{ $k->[1] }); | ||
| 687 | $self->_SECURECHECK($k); | ||
| 688 | $self->_WRITE($k,'EHLO '.$self->{helo}{ $k->[0] }); | ||
| 689 | $self->_READ($k); | ||
| 690 | $self->_HEADER($k); | ||
| 691 | } | ||
| 692 | |||
| 693 | |||
| 694 | |||
| 695 | } | ||
| 696 | |||
| 697 | sub _SECURE { | ||
| 698 | my $self=shift; | ||
| 699 | my $k=shift; | ||
| 700 | my $sock=shift; | ||
| 701 | $self->{secure_sock}{ $k->[0] }{ $k->[1] }=$sock; | ||
| 702 | require IO::Select; | ||
| 703 | require IO::Socket::SSL; | ||
| 704 | |||
| 705 | my $sel = IO::Select->new($sock); # wait until it connected | ||
| 706 | $self->{secure_sel}{ $k->[0] }{ $k->[1] }=$sel; | ||
| 707 | if ($sel->can_write) { | ||
| 708 | $self->_DEBUG($k,'IO::Socket::INET connected') if $self->{debug} >= 1; | ||
| 709 | } | ||
| 710 | |||
| 711 | my %extra; | ||
| 712 | if ($self->{secure}{ $k->[0] } == 1 or $self->{secure}{ $k->[0] } == 3) { | ||
| 713 | $extra{SSL_verify_mode}=Net::SSLeay::VERIFY_NONE(), | ||
| 714 | } | ||
| 715 | |||
| 716 | |||
| 717 | |||
| 718 | IO::Socket::SSL->start_SSL($sock, %extra, SSL_startHandshake => 0); | ||
| 719 | =head2 | ||
| 720 | while (1) { | ||
| 721 | if ($sock->connect_SSL) { # will not block | ||
| 722 | $self->_DEBUG($k,'IO::Socket::SSL connected') if $self->{debug} >= 1; | ||
| 723 | last; | ||
| 724 | } | ||
| 725 | else { # handshake still incomplete | ||
| 726 | $self->_DEBUG($k,'IO::Socket::SSL not connected yet') if $self->{debug} >= 1; | ||
| 727 | if ( IO::Socket::SSL->want_read() ) { | ||
| 728 | $sel->can_read; | ||
| 729 | } | ||
| 730 | elsif ( IO::Socket::SSL->want_write()) { | ||
| 731 | $sel->can_write; | ||
| 732 | } | ||
| 733 | else { | ||
| 734 | $self->_DEBUG($k,'IO::Socket::SSL unknown error: '. $IO::Socket::SSL::SSL_ERROR) if $self->{debug} >= 1; | ||
| 735 | #SSL ERROR | ||
| 736 | } | ||
| 737 | } | ||
| 738 | } | ||
| 739 | =cut | ||
| 740 | } | ||
| 741 | |||
| 742 | |||
| 743 | sub _SECURECHECK { | ||
| 744 | my $self=shift; | ||
| 745 | if (exists($self->{secure_sel})) { | ||
| 746 | while (1) { | ||
| 747 | |||
| 748 | foreach my $h ( keys(%{$self->{secure_sel}}) ) { | ||
| 749 | foreach my $t ( keys(%{$self->{secure_sel}{$h}}) ) { | ||
| 750 | $self->_SECURECHECK_PROCESS([$h,$t],$self->{secure_sock}{$h}{$t},$self->{secure_sel}{$h}{$t} ); | ||
| 751 | |||
| 752 | } | ||
| 753 | if ( keys(%{$self->{secure_sel}{$h}}) == 0 ) { | ||
| 754 | |||
| 755 | delete($self->{secure_sel}{$h}); | ||
| 756 | } | ||
| 757 | } | ||
| 758 | if ( keys(%{$self->{secure_sel}}) == 0 ) { | ||
| 759 | delete($self->{secure_sel}); | ||
| 760 | last; | ||
| 761 | } | ||
| 762 | } | ||
| 763 | } | ||
| 764 | } | ||
| 765 | |||
| 766 | sub _SECURECHECK_PROCESS { | ||
| 767 | my $self=shift; | ||
| 768 | my $k=shift; | ||
| 769 | my $sock=shift; | ||
| 770 | my $sel=shift; | ||
| 771 | |||
| 772 | |||
| 773 | if ($sock->connect_SSL) { # will not block | ||
| 774 | $self->_DEBUG($k,'IO::Socket::SSL connected') if $self->{debug} >= 1; | ||
| 775 | delete($self->{secure_sel}{$k->[0]}{$k->[1]}); | ||
| 776 | } else { # handshake still incomplete | ||
| 777 | $self->_DEBUG($k,'IO::Socket::SSL not connected yet') if $self->{debug} >= 1; | ||
| 778 | if ( $sock->want_read() ) { | ||
| 779 | $sel->can_read; | ||
| 780 | } elsif ( $sock->want_write()) { | ||
| 781 | $sel->can_write; | ||
| 782 | } else { | ||
| 783 | $self->_DEBUG($k,'IO::Socket::SSL unknown error: '. $sock->errstr()) if $self->{debug} >= 1; | ||
| 784 | #SSL ERROR | ||
| 785 | } | ||
| 786 | } | ||
| 787 | |||
| 788 | } | ||
| 789 | |||
| 790 | sub _HEADER { | ||
| 791 | my $self=shift; | ||
| 792 | my $k=shift; | ||
| 793 | |||
| 794 | my $fh= $self->{fh}{ $k->[0] }{ $k->[1] }; | ||
| 795 | my $nb_fh = $fh->fh; | ||
| 796 | my $buf = \$fh->rbuf; | ||
| 797 | |||
| 798 | while () { | ||
| 799 | # now use buffer contents, modifying | ||
| 800 | # if necessary to reflect the removed data | ||
| 801 | |||
| 802 | last if $$buf ne ""; # we have leftover data | ||
| 803 | |||
| 804 | # read another buffer full of data | ||
| 805 | $fh->readable or die "end of file"; | ||
| 806 | sysread $nb_fh, $$buf, 8192; | ||
| 807 | } | ||
| 808 | |||
| 809 | foreach my $line (split/[\r\n]+/,$$buf) { | ||
| 810 | $line=~m/^((\d{3})[ \-](\w+?)(?: (.*?)|)[\r\n]*?)$/is; | ||
| 811 | $self->_DEBUG($k,$1) if $self->{debug} >= 1; | ||
| 812 | my $status = lc($2); | ||
| 813 | my $head = lc($3); | ||
| 814 | $self->{header}{ $k->[0] }{ $k->[1] }{$head}=[split/ /,($4||'')]; | ||
| 815 | } | ||
| 816 | $fh->rbuf=''; | ||
| 817 | } | ||
| 818 | |||
| 819 | sub _READ { | ||
| 820 | my $self=shift; | ||
| 821 | my $k=shift; | ||
| 822 | |||
| 823 | my $str; | ||
| 824 | my $waitcount=0; | ||
| 825 | |||
| 826 | if ($self->{fh}{ $k->[0] }{ $k->[1] }->readable()) { | ||
| 827 | $str=$self->{fh}{ $k->[0] }{ $k->[1] }->readline(); | ||
| 828 | } | ||
| 829 | |||
| 830 | |||
| 831 | if (defined($str) and $str=~m/^((\d{3})(.)(.*?))[\r\n]+?$/) { | ||
| 832 | $self->_DEBUG($k,$1) if $self->{debug} >= 1; | ||
| 833 | $self->{buffer}{ $k->[0] }{ $k->[1] }=$1; | ||
| 834 | $self->{status_code}{ $k->[0] }{ $k->[1] }=$2; | ||
| 835 | $self->{status_mode}{ $k->[0] }{ $k->[1] }=$3; | ||
| 836 | $self->{status_text}{ $k->[0] }{ $k->[1] }=$4; | ||
| 837 | } else { | ||
| 838 | $self->{buffer}{ $k->[0] }{ $k->[1] }=''; | ||
| 839 | $self->{status_code}{ $k->[0] }{ $k->[1] }=-1; | ||
| 840 | $self->{status_mode}{ $k->[0] }{ $k->[1] }=''; | ||
| 841 | $self->{status_text}{ $k->[0] }{ $k->[1] }=''; | ||
| 842 | } | ||
| 843 | |||
| 844 | } | ||
| 845 | |||
| 846 | sub _WRITE { | ||
| 847 | my $self=shift; | ||
| 848 | my $k=shift; | ||
| 849 | my $str=shift; | ||
| 850 | $str=~s/[\r\n]+?$//s; | ||
| 851 | $self->_DEBUG($k,'>>'.$str) if $self->{debug} >= 1; | ||
| 852 | |||
| 853 | $self->{fh}{ $k->[0] }{ $k->[1] }->print( ($self->{encode} ne '') ? Encode::encode($self->{encode}=>$str."\r\n"):$str."\r\n" ); | ||
| 854 | } | ||
| 855 | |||
| 856 | |||
| 857 | |||
| 858 | sub _DEBUG { | ||
| 859 | my $self=shift; | ||
| 860 | my $k=shift; | ||
| 861 | my $str=shift||''; | ||
| 862 | my $dlevel = shift||10; | ||
| 863 | |||
| 864 | if ( $dlevel <= $self->{debug} ) { | ||
| 865 | my $out = '['.$k->[0].':'.$k->[1].':'.$self->{stats}{ $k->[0] }{ $k->[1] }{queue}{id}.']['.$self->_STRFTIME('[YYYY]-[MM]-[DD] [hh]:[mm]:[ss]',time).'] '.$str."\n"; | ||
| 866 | |||
| 867 | if ( exists($self->{debug_path}) ) { | ||
| 868 | print { $self->{debug_fh}{ $k->[0].':'.$k->[1] } } $out; | ||
| 869 | } else { | ||
| 870 | print $out; | ||
| 871 | } | ||
| 872 | |||
| 873 | } | ||
| 874 | } | ||
| 875 | |||
| 876 | sub _STRFTIME { | ||
| 877 | my $self = shift; | ||
| 878 | my $format=shift; | ||
| 879 | my $time=shift; | ||
| 880 | |||
| 881 | my @time=localtime($time); | ||
| 882 | |||
| 883 | my %DT=( | ||
| 884 | 'YYYY'=>$time[5]+1900, | ||
| 885 | 'MM'=>sprintf('%.2d',$time[4]+1), | ||
| 886 | 'DD'=>sprintf('%.2d',$time[3]), | ||
| 887 | 'hh'=>sprintf('%.2d',$time[2]), | ||
| 888 | 'mm'=>sprintf('%.2d',$time[1]), | ||
| 889 | 'ss'=>sprintf('%.2d',$time[0]), | ||
| 890 | #'MNA'=>$mon3[($time[4])], | ||
| 891 | #'DNAME'=>$day6[($time[6])], | ||
| 892 | 'WK'=>(( ($time[7]+1-$time[6]) <= 7) ? '01':sprintf('%.2d',($time[7]+1-$time[6])/7)+1) | ||
| 893 | ); | ||
| 894 | |||
| 895 | $format=~s/\[(YYYY|MM|DD|hh|mm|ss|MNA|DNAME|WK)\]/$DT{$1}/gs; | ||
| 896 | |||
| 897 | return $format; | ||
| 898 | |||
| 899 | } | ||
| 900 | |||
| 901 | sub _MKDIRF { | ||
| 902 | my $self=shift; | ||
| 903 | my $path=shift; | ||
| 904 | |||
| 905 | my $sep=0; | ||
| 906 | my $np2=''; | ||
| 907 | |||
| 908 | if ($path=~s!^(\w\:|)\/!!s) { | ||
| 909 | $np2=''.($1||''); | ||
| 910 | } else { | ||
| 911 | $sep=1; | ||
| 912 | } | ||
| 913 | my @f=split/\//,$path; | ||
| 914 | |||
| 915 | my $fl=pop(@f); | ||
| 916 | foreach my $f (@f) { | ||
| 917 | if ($sep == 0) { | ||
| 918 | $np2.='/'.$f; | ||
| 919 | } else { | ||
| 920 | $np2.=$f; | ||
| 921 | $sep=0; | ||
| 922 | } | ||
| 923 | |||
| 924 | #print "DIR($np2)\n"; | ||
| 925 | if (!(-e $np2)) { | ||
| 926 | #print "GOOD\n"; | ||
| 927 | my $mk=mkdir($np2); | ||
| 928 | #print "MK($mk)\n"; | ||
| 929 | } | ||
| 930 | } | ||
| 931 | |||
| 932 | } | ||
| 933 | |||
| 934 | |||
| 935 | |||
| 936 | =head1 AUTHOR | ||
| 937 | |||
| 938 | KnowZero | ||
| 939 | |||
| 940 | =head1 BUGS | ||
| 941 | |||
| 942 | Please report any bugs or feature requests to C<bug-net-smtp-bulk at rt.cpan.org>, or through | ||
| 943 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-SMTP-Bulk>. I will be notified, and then you'll | ||
| 944 | automatically be notified of progress on your bug as I make changes. | ||
| 945 | |||
| 946 | |||
| 947 | |||
| 948 | |||
| 949 | =head1 SUPPORT | ||
| 950 | |||
| 951 | You can find documentation for this module with the perldoc command. | ||
| 952 | |||
| 953 | perldoc Net::SMTP::Bulk | ||
| 954 | |||
| 955 | |||
| 956 | You can also look for information at: | ||
| 957 | |||
| 958 | =over 4 | ||
| 959 | |||
| 960 | =item * RT: CPAN's request tracker (report bugs here) | ||
| 961 | |||
| 962 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-SMTP-Bulk> | ||
| 963 | |||
| 964 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 965 | |||
| 966 | L<http://annocpan.org/dist/Net-SMTP-Bulk> | ||
| 967 | |||
| 968 | =item * CPAN Ratings | ||
| 969 | |||
| 970 | L<http://cpanratings.perl.org/d/Net-SMTP-Bulk> | ||
| 971 | |||
| 972 | =item * Search CPAN | ||
| 973 | |||
| 974 | L<http://search.cpan.org/dist/Net-SMTP-Bulk/> | ||
| 975 | |||
| 976 | =back | ||
| 977 | |||
| 978 | |||
| 979 | =head1 ACKNOWLEDGEMENTS | ||
| 980 | |||
| 981 | |||
| 982 | =head1 LICENSE AND COPYRIGHT | ||
| 983 | |||
| 984 | Copyright 2013 KnowZero. | ||
| 985 | |||
| 986 | This program is free software; you can redistribute it and/or modify it | ||
| 987 | under the terms of either: the GNU General Public License as published | ||
| 988 | by the Free Software Foundation; or the Artistic License. | ||
| 989 | |||
| 990 | See L<http://dev.perl.org/licenses/> for more information. | ||
| 991 | |||
| 992 | |||
| 993 | =cut | ||
| 994 | |||
| 995 | 1; # End of Net::SMTP::Bulk |