/
MailOut.pm
3275 lines (2270 loc) · 89.3 KB
/
MailOut.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package DADA::Mail::MailOut;
use Carp;
$SIG{ __DIE__ } = sub { Carp::confess( @_ ) };
use lib qw(../../ ../../DADA ../../perllib);
use lib "../../";
use lib "../../DADA/perllib";
use lib './';
use lib './DADA/perllib';
use CGI::Carp qw(croak carp);
use Fcntl qw(
:DEFAULT
:flock
LOCK_SH
O_RDONLY
O_CREAT
O_WRONLY
O_TRUNC
);
my $dbi_obj;
use DADA::Config qw(!:DEFAULT);
my $t = $DADA::Config::DEBUG_TRACE->{DADA_Mail_MailOut};
use DADA::App::Guts;
use DADA::Logging::Usage;
my $log = new DADA::Logging::Usage;
require Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(current_mailouts mailout_exists monitor_mailout);
use vars qw(@EXPORT $AUTOLOAD);
use strict;
my $file_names = {
batchlock => 'batchlock.txt',
tmp_subscriber_list => 'tmp_subscriber_list.txt',
problem_subscriber_list => 'problem_subscriber_list.txt',
num_sending_to => 'num_sending_to.txt',
counter => 'counter.txt',
first_access => 'first_access.txt',
last_access => 'last_access.txt',
raw_message => 'raw_message.txt',
pause => 'paused.txt',
pid => 'pid.txt',
'log' => 'log.txt',
};
my %allowed = (
message => undef,
_internal_message_id => undef,
mailout_type => undef,
list => undef,
dir => undef,
subscriber_list => undef,
sent_amount => 0,
total_sending_out_num => undef,
#auto_pickup_status => 0,
use_semaphore_locking => 0,
_cache => {},
);
sub new {
my $that = shift;
my $class = ref($that) || $that;
my $self = {
_permitted => \%allowed,
%allowed,
};
bless $self, $class;
my ($args) = @_;
if ( !$args->{-list} ) {
carp "You need to supply a list ->new({-list => your_list}) in the constructor.";
return undef;
}
if ( $self->_init($args) ) {
warn 'MailOut object created successfully.'
if $t;
return $self;
}
else {
warn 'MailOut object was NOT created successfully.'
if $t;
return undef;
}
}
sub AUTOLOAD {
my $self = shift;
my $type = ref($self)
or croak "$self is not an object";
return if(substr($AUTOLOAD, -7) eq 'DESTROY');
my $name = $AUTOLOAD;
$name =~ s/.*://; #strip fully qualifies portion
unless ( exists $self->{_permitted}->{$name} ) {
croak "Can't access '$name' field in object of class $type";
}
if (@_) {
return $self->{$name} = shift;
}
else {
return $self->{$name};
}
}
sub _init {
my $self = shift;
my ($args) = @_;
if ( !$self->_sanity_test($args) ) {
carp "sanity test failed - something's wrong with the parameters you passed.";
return undef;
}
else {
my $ls = undef;
if ( exists( $args->{-ls_obj} ) ) {
$ls = $args->{-ls_obj};
}
else {
# Or... I could just *pass* $li somehow...
require DADA::App::DBIHandle;
$self->{dbi_handle} = DADA::App::DBIHandle->new;
require DADA::MailingList::Settings;
$ls = DADA::MailingList::Settings->new( { -list => $args->{-list} } );
}
my $li = $ls->get();
$self->list( $args->{-list} );
$self->{ls} = $ls;
$self->_dir_setup();
return 1;
}
}
sub _dir_setup {
my $self = shift;
# Directory?
if ( -d $DADA::Config::TMP ) {
# Write to it?
if ( !-r $DADA::Config::TMP || !-w $DADA::Config::TMP || !-x $DADA::Config::TMP ) {
carp "Can't write into, '" . $DADA::Config::TMP . "', will try to change directory permissions...";
chmod( $DADA::Config::DIR_CHMOD, $DADA::Config::TMP );
}
else {
# good.
}
}
}
sub _sanity_test {
my $self = shift;
my ($args) = @_;
return undef if !$args;
return $self->_list_name_check( $args->{-list} );
}
sub batch_params {
my $self = shift;
my ($args) = @_;
# We can override things, for previewing:
my $sending_method = $self->{ls}->param('sending_method');
if ( exists( $args->{-sending_method} ) ) {
$sending_method = $args->{-sending_method};
}
my $amazon_ses_auto_batch_settings = $self->{ls}->param('amazon_ses_auto_batch_settings');
if ( exists( $args->{-amazon_ses_auto_batch_settings} ) ) {
$amazon_ses_auto_batch_settings = $args->{-amazon_ses_auto_batch_settings};
}
my $enable_bulk_batching = $self->{ls}->param('enable_bulk_batching');
if ( exists( $args->{-enable_bulk_batching} ) ) {
$enable_bulk_batching = $args->{-enable_bulk_batching};
}
my $can_use_Amazon_SES = DADA::App::Guts::can_use_Amazon_SES();
#my $ses;
#if($can_use_Amazon_SES){
require DADA::App::AmazonSES;
my $ses = DADA::App::AmazonSES->new;
#}
# Amazon SES:
if (
$amazon_ses_auto_batch_settings == 1
&& $can_use_Amazon_SES == 1
&&
((
$sending_method eq 'amazon_ses'
)
||
(
$self->{ls}->param('sending_method') eq 'smtp'
&& $self->{ls}->param('smtp_server') =~ m/amazonaws\.com/
&& $ses->has_ses_options_set == 1
))
)
{
if ( exists( $self->{_cache}->{batch_params} ) ) {
if ( exists( $self->{_cache}->{batch_params}->{cached_at} ) ) {
if ( ( int( $self->{_cache}->{batch_params}->{cached_at} ) + 600 ) > time ) {
carp 'batch settings (cached): Enabled: '
. $self->{_cache}->{batch_params}->{enable_bulk_batching}
. ' Batch Size: '
. $self->{_cache}->{batch_params}->{batch_size}
. ' Batch Wait: '
. $self->{_cache}->{batch_params}->{batch_wait}
if $t;
return (
$self->{_cache}->{batch_params}->{enable_bulk_batching},
$self->{_cache}->{batch_params}->{batch_size},
$self->{_cache}->{batch_params}->{batch_wait}
);
}
}
else {
carp 'Love to use the batch settings cache, but it\'s too old by: '
. int( ($self->{_cache}->{batch_params}->{cached_at} ) + 600) - time
if $t;
}
}
else {
#warn 'no $self->{_cache}->{batch_params}';
}
}
#/ We can override things, for previewing:
# Amazon SES:
my $using_amazon_ses = 0;
if (
$amazon_ses_auto_batch_settings == 1
&& $can_use_Amazon_SES == 1
&&
((
$sending_method eq 'amazon_ses'
)
||
(
$self->{ls}->param('sending_method') eq 'smtp'
&& $self->{ls}->param('smtp_server') =~ m/amazonaws\.com/
&& $ses->has_ses_options_set == 1
))
)
{
$using_amazon_ses = 1;
}
require POSIX;
my $batch_size = 0;
my $batch_wait = 0;
if ( $using_amazon_ses == 1)
{
my $status = undef;
my $SentLast24Hours = undef;
my $Max24HourSend = undef;
my $MaxSendRate = undef;
my $quota_MaxSendRate = undef;
my $quota_Max24HourSend = undef;
if ( $using_amazon_ses == 1 ) {
# Save stats, between executions:
( $status, $SentLast24Hours, $Max24HourSend, $MaxSendRate );
if ( $ses->_should_get_saved_ses_stats == 1 ) {
( $status, $SentLast24Hours, $Max24HourSend, $MaxSendRate ) = $ses->_get_saved_ses_stats;
}
else {
( $status, $SentLast24Hours, $Max24HourSend, $MaxSendRate ) = $ses->get_stats;
# 0 10_000 5
$ses->_save_ses_stats( $status, $SentLast24Hours, $Max24HourSend, $MaxSendRate );
}
$quota_Max24HourSend = ( $Max24HourSend * $ses->allowed_sending_quota_percentage ) / 100;
$quota_MaxSendRate = ( $MaxSendRate * $ses->allowed_sending_quota_percentage ) / 100;
#warn q{$quota_Max24HourSend} . $quota_Max24HourSend;
}
else {
die "what?";
}
# if($t) {
# warn '$status ' . $status;
# warn '$SentLast24Hours' . $SentLast24Hours;
# warn '$Max24HourSend' . $Max24HourSend;
# warn '$MaxSendRate' . $MaxSendRate
# }
# warn '$quota_Max24HourSend bef' , $quota_Max24HourSend;
$quota_Max24HourSend = POSIX::floor($quota_Max24HourSend);
# warn '$quota_Max24HourSend' . $quota_Max24HourSend
# if $t;
if ( $SentLast24Hours >= $Max24HourSend
|| $SentLast24Hours >= $quota_Max24HourSend )
{
# Yikes! We're over our limit!
carp 'batch settings (OVER OUR LIMIT!): Enabled: '
. $enable_bulk_batching
. ' Batch Size: '
. 0
. ' Batch Wait: '
. 300
if $t;
return ( $enable_bulk_batching, 0, 300, );
}
else {
if ( $amazon_ses_auto_batch_settings == 1 ) {
# dada automatically manages batch settings
if ( $quota_Max24HourSend < 86_400 ) {
if(($SentLast24Hours * 3) < $quota_Max24HourSend) {
# Turbo!
$batch_wait = 1;
$batch_size = $quota_MaxSendRate;
}
elsif($SentLast24Hours > ($quota_Max24HourSend * .85)) {
# Um, the opposite of turbo!
my $whats_left = $quota_Max24HourSend - $SentLast24Hours;
$batch_size = 100;
$batch_wait = int($whats_left / $quota_Max24HourSend) * 100;
}
else {
#warn q{$quota_Max24HourSend < 86_400};
$batch_size = 1;
$batch_size = $batch_size * $quota_MaxSendRate;
$batch_wait = 86_400 / $quota_Max24HourSend; # 8.64
$batch_wait = $batch_wait * $quota_MaxSendRate;
$batch_wait = $batch_wait + ( $batch_wait * .10 ); # 38.8
}
}
else {
if(($SentLast24Hours * 3) < $quota_Max24HourSend) {
# Turbo!
$batch_wait = 1;
$batch_size = $quota_MaxSendRate;
}
else {
$batch_wait = 1 * $quota_MaxSendRate;
$batch_size = ($quota_Max24HourSend / 86_400) * $quota_MaxSendRate;
}
}
# This slows the batch settings down, if there's > 1 mailout going on
# at one time.
if ( $DADA::Config::MAILOUT_AT_ONCE_LIMIT > 1 ) {
# can't call monitor_mailout, because monitor_mailout
# will eventually call batch_params,
# leading to an infinite loop!
my @mailouts = $self->current_mailouts;
my $unpaused_mailouts_num = 0;
for(@mailouts){
if(exists($_->{unpaused})){
if($_->{unpaused} == 1) {
$unpaused_mailouts_num++;
}
}
}
# If the batch size is larger than how many messages we have,
# better to divide that up, rather than the span between messages
# Round DOWN just to be on the safe side of things.
if ( $batch_size > $unpaused_mailouts_num ) {
# warn '$batch_size > $unpaused_mailouts_num'
# if $t;
if ( $unpaused_mailouts_num > 1 ) {
if ( $unpaused_mailouts_num < $DADA::Config::MAILOUT_AT_ONCE_LIMIT ) {
$batch_size = POSIX::floor( ( $batch_size / $unpaused_mailouts_num ) );
}
else {
$batch_wait = POSIX::floor( ( $batch_size / $DADA::Config::MAILOUT_AT_ONCE_LIMIT ) );
}
}
}
else {
# else, make the wait longer
if ( $unpaused_mailouts_num > 1 ) {
if ( $unpaused_mailouts_num < $DADA::Config::MAILOUT_AT_ONCE_LIMIT ) {
$batch_wait = ( $batch_wait * $unpaused_mailouts_num );
}
else {
$batch_wait = ( $batch_wait * $DADA::Config::MAILOUT_AT_ONCE_LIMIT );
}
}
}
}
}
else {
# we manage our own batch settings
$batch_size = $self->{ls}->param('mass_send_amount');
$batch_wait = $self->{ls}->param('bulk_sleep_amount');
if ( $batch_size > $MaxSendRate ) {
$batch_size = $MaxSendRate;
}
if ( $batch_wait < 1 ) {
warn 'why is $bulk_sleep_amount < 1?!';
$batch_wait = 1;
}
}
$batch_size = POSIX::floor($batch_size);
$batch_wait = POSIX::ceil($batch_wait);
$self->{_cache}->{batch_params} = {
enable_bulk_batching => $enable_bulk_batching,
batch_size => $batch_size,
batch_wait => $batch_wait,
cached_at => time,
};
carp 'batch settings (Fresh, SES): Enabled: '
. $enable_bulk_batching
. ' Batch Size: '
. $batch_size
. ' Batch Wait: '
. $batch_wait
if $t;
return ( $enable_bulk_batching, $batch_size, $batch_wait, );
}
}
else {
my $mass_send_amount = $self->{ls}->param('mass_send_amount');
my $bulk_sleep_amount = $self->{ls}->param('bulk_sleep_amount');
if ( $bulk_sleep_amount < 1 ) {
warn 'why is $bulk_sleep_amount < 1?!';
$bulk_sleep_amount = 1;
}
carp 'batch settings (bulk_sleep_amount set weird): Enabled: '
. $enable_bulk_batching
. ' Batch Size: '
. $mass_send_amount
. ' Batch Wait: '
. $bulk_sleep_amount
if $t;
return ( $enable_bulk_batching, $mass_send_amount, $bulk_sleep_amount, );
}
}
sub reset_batch_params_cache {
my $self = shift;
$self->{_cache}->{batch_params} = undef;
delete( $self->{_cache}->{batch_params} );
return 1;
}
sub create {
my $self = shift;
my ($args) = @_;
# = ( -fields => {}, -mh_obj => {}, -list_type => 'list', @_ );
croak "You did not supply a list type! (list, black_list, invite_list, etc)"
if !exists( $args->{-list_type} );
croak "The Actual Message Fields have not been passed. "
if !keys %{ $args->{-fields} };
croak "The Message ID has not been passed. "
if !exists( $args->{-fields}->{'Message-ID'} );
croak "The DADA::Mail::Send object has not been passed "
if !exists( $args->{-mh_obj} );
croak "The DADA::Mail::Send object has been passed, but it's not isa DADA::Mail::Send! "
unless $args->{-mh_obj}->isa('DADA::Mail::Send');
# default:
my $list_type = $args->{-list_type};
$self->_internal_message_id( $args->{-fields}->{'Message-ID'} );
warn '_internal_message_id set to '
. $self->_internal_message_id
. ' from original value: '
. $args->{-fields}->{'Message-ID'}
if $t;
$self->mailout_type($list_type);
warn 'mailout_type set to ' . $self->mailout_type . ' from original value: ' . $list_type
if $t;
$self->message( $args->{-fields} );
# dir should now be set to the TMP_ directory
$self->_create_initial_directory();
# The Temporary Subscriber List
$self->create_subscriber_list(
{
-mh_obj => $args->{-mh_obj},
-partial_sending => $args->{-partial_sending},
-exclude_from => $args->{-exclude_from},
-mass_mailing_params => $args->{-mass_mailing_params},
}
);
#The Total Amount of Recipients Amount
$self->create_total_sending_out_num();
# Log File
$self->create_log_file();
#The Amount Sent Counter
$self->create_counter();
# Create "First Accessed", File
$self->create_first_accessed_file();
# The, "Last Accessed", File
$self->create_last_accessed_file();
# A Copy of the Message Being Sent Out
$self->create_raw_message();
# Problem List!
$self->problem_subscriber_list_file();
# Everything made? OK, let's change the dir name, save that name, and move on,
my $orig_name = $self->dir;
my $live_name = $orig_name;
$live_name =~ s/TMP_sendout/sendout/;
$live_name = make_safer($live_name);
warn "moving directory from: '$orig_name', to: '$live_name'..."
if $t;
require File::Copy;
File::Copy::move( $orig_name, $live_name )
or warn "could not move file from, '$orig_name', to, '$live_name': $!";
$self->dir($live_name);
# I have to reset this to, as it's set in, create_subscriber_list();
# For whatever reason.
$self->subscriber_list( $self->dir . '/' . $file_names->{tmp_subscriber_list} );
#well, I guess I'll return 1 for success...
return 1;
}
sub associate {
my $self = shift;
my $id = shift;
my $type = shift;
croak "Object already associated with: "
. $self->_internal_message_id
. "This can only be set once and is read-only via (_internal_message_id) after that."
if $self->_internal_message_id;
$id =~ s/\@/_at_/g;
$id =~ s/\>|\<//g;
# This kinda sucks:
$self->_internal_message_id($id);
if ( $self->mailout_type ) {
croak "Why is mailout_type already set?!";
}
if ( !$type ) {
croak "You didn't supply a list mailing type (list, black_list invite_list, etc)";
}
# BUGFIX:
# 2219972 3.0.0 - Mailout.pm - associate method broken
# https://sourceforge.net/tracker2/?func=detail&aid=2219972&group_id=13002&atid=113002
$self->mailout_type($type);
$self->dir( $self->mailout_directory_name );
$self->total_sending_out_num( _poll( $self->dir . '/' . $file_names->{num_sending_to} ) );
$self->subscriber_list( $self->dir . '/' . $file_names->{tmp_subscriber_list} );
#/BUGFIX
return 1;
}
sub batch_lock {
my $self = shift;
$self->create_batch_lock();
warn 'created batch lock '
if $t;
return 1;
}
sub unlock_batch_lock {
my $self = shift;
my $file = $self->dir . '/' . $file_names->{batchlock};
$file = make_safer($file);
if ( !-e $file ) {
warn "Batch lock at '$file' doesn't exist to unlock?!"
if $t;
}
else {
my $worked = unlink($file);
if ( $worked == 1 ) {
warn 'Unlocking Batch Lock successfully. '
if $t;
return 1;
}
else {
warn "Batch lock at, '$file' could not be unlinked (deleted). Reasons:" . $!
if $t;
if ( -e $file ) {
carp "File exists check turns up a positive - the batch lock file is *really really* there.";
}
else {
carp "Strange, unlinking didn't work, but the batch lock doesn't seem to be around anymore.";
}
return 0;
}
}
}
sub is_batch_locked {
my $self = shift;
if ( -e $self->dir . '/' . $file_names->{batchlock} ) {
warn 'Returning that the batch is locked.'
if $t;
return 1;
}
else {
warn 'Returning that the batch is NOT locked.'
if
return 0;
}
}
sub _create_initial_directory {
my $self = shift;
my $tmp_dir_name = make_safer( $self->mailout_directory_name( { -tmp => 1 } ) );
if ( -d $tmp_dir_name ) {
croak "Mass Mailing directory, '" . $tmp_dir_name . "' already exists?!";
}
if ( !-d $tmp_dir_name ) {
croak "$DADA::Config::PROGRAM_NAME $DADA::Config::VER warning! Could not create, '" . $tmp_dir_name . "'- $!"
unless mkdir( $tmp_dir_name, $DADA::Config::DIR_CHMOD );
chmod( $DADA::Config::DIR_CHMOD, $tmp_dir_name )
if -d $tmp_dir_name;
}
if ( !-d $tmp_dir_name ) {
croak "Mass Mailing Directory never created at: '" . $tmp_dir_name . "'";
}
$self->dir($tmp_dir_name);
warn 'Directory created at: ' . $tmp_dir_name . ' saved in $self->dir as: ' . $self->dir
if $t;
return 1;
}
sub mailout_directory_name {
my $self = shift;
my ($args) = @_;
if ( !exists( $args->{-tmp} ) ) {
$args->{-tmp} = 0;
}
my $tmp = $self->_internal_message_id;
my $letter_id = $tmp;
$letter_id =~ s/\@/_at_/g;
$letter_id =~ s/\>|\<//g;
$letter_id = DADA::App::Guts::strip($letter_id);
my $prepend = '';
if ( $args->{-tmp} == 1 ) {
$prepend = 'TMP_';
}
return make_safer( $DADA::Config::TMP . '/'
. $prepend
. 'sendout-'
. $self->{list} . '-'
. $self->mailout_type . '-'
. $letter_id );
}
sub create_subscriber_list {
my $self = shift;
my ($args) = @_;
unless ( $args->{-mh_obj}->isa('DADA::Mail::Send') ) {
croak "The DADA::Mail::Send object has been passed, but it's not isa DADA::Mail::Send! ";
}
# That's gonna be harder....
require DADA::MailingList::Subscribers;
my $lh = DADA::MailingList::Subscribers->new( { -list => $self->list } );
my $file = $self->dir . '/' . $file_names->{tmp_subscriber_list};
my $lock = $self->lock_file($file);
my %cmf_args = (
-ID => $self->_internal_message_id,
-Type => $self->mailout_type,
-Save_At => $self->dir . '/' . $file_names->{tmp_subscriber_list},
-Ban => $args->{-mh_obj}->{do_not_send_to},
-Create_Tokens => ( $args->{-mh_obj}->list_type eq 'invite_list' || $args->{-mh_obj}->list_type eq 'list' )
? 1
: 0,
# I'm pretty scoobied why these are passed as params, and the above are just
# culled from the $mh object. Like, what?
-partial_sending => $args->{-partial_sending},
-mass_mailing_params => $args->{-mass_mailing_params},
);
my ( $path_to_list, $total_sending_out_num ) = $lh->create_mass_sending_file(%cmf_args);
$self->unlock_file($lock);
if ( !-e $self->dir . '/' . $file_names->{tmp_subscriber_list} ) {
croak "Temporary Sending List was never created at: '"
. $self->dir . '/'
. $file_names->{tmp_subscriber_list} . "'";
}
$self->subscriber_list( $self->dir . '/' . $file_names->{tmp_subscriber_list} );
$self->total_sending_out_num($total_sending_out_num);
chmod( $DADA::Config::FILE_CHMOD, $self->subscriber_list );
warn 'create_subscriber_list successful, subscriber_list set to: '
. $self->dir . '/'
. $file_names->{tmp_subscriber_list}
. 'total sending out set to: '
. $total_sending_out_num
if $t;
undef $lh;
return 1;
}
sub create_total_sending_out_num {
my $self = shift;
my $file = $self->dir . '/' . $file_names->{num_sending_to};
$file = make_safer($file);
my $lock = $self->lock_file($file);
sysopen( COUNTER, $file, O_WRONLY | O_TRUNC | O_CREAT, $DADA::Config::FILE_CHMOD )
or croak "Couldn't create the counter of how many subscribers I need to send to at: '$file' because: $!";
print COUNTER $self->total_sending_out_num
or croak "Couldn't write to: '" . $file . "' because: " . $!;
close(COUNTER)
or croak "'" . $file . "' didn't close poperly because: " . $!;
$self->unlock_file($lock);
chmod( $DADA::Config::FILE_CHMOD, $file );
warn 'num_sending_to file created at ' . $file . ' and given value of: ' . $self->total_sending_out_num
if $t;
return 1;
}
sub create_log_file {
my $self = shift;
my $file = $self->dir . '/' . $file_names->{'log'};
$file = make_safer($file);
open( LOG, '>>:encoding(' . $DADA::Config::HTML_CHARSET . ')', $file )
or croak "Couldn't create the log file at: '" . $file . " 'because: " . $!;
close(LOG)
or croak " didn't close '$file' poperly because: " . $!;
chmod( $DADA::Config::FILE_CHMOD, $file );
warn 'log file created at ' . $file
if $t;
return 1;
}
sub problem_subscriber_list_file {
my $self = shift;
my $file = $self->dir . '/' . $file_names->{'problem_subscriber_list'};
$file = make_safer($file);
open( LOG, '>>:encoding(' . $DADA::Config::HTML_CHARSET . ')', $file )
or croak "Couldn't create the problem_subscriber_list file at: '" . $file . " 'because: " . $!;
close(LOG)
or croak " didn't close '$file' poperly because: " . $!;
chmod( $DADA::Config::FILE_CHMOD, $file );
warn 'problem_subscriber_list file created at ' . $file
if $t;
return 1;
}
sub create_counter {
my $self = shift;
my $file = $self->dir . '/' . $file_names->{counter};
$file = make_safer($file);
my $lock = $self->lock_file($file);
sysopen( COUNTER, $file, O_WRONLY | O_TRUNC | O_CREAT, $DADA::Config::FILE_CHMOD )
or croak "couldn't open counter at: '$file' because: $!";
print COUNTER '0';
close(COUNTER)
or croak "couldn't close counter at: '$file' because: $!";
$self->unlock_file($lock);
chmod( $DADA::Config::FILE_CHMOD, $file );
warn 'counter created at ' . $file
if $t;
return 1;
}
sub create_first_accessed_file {
my $self = shift;
my $file = $self->dir . '/' . $file_names->{first_access};
$file = make_safer($file);
my $lock = $self->lock_file($file);
sysopen( ACCESS, $file, O_WRONLY | O_TRUNC | O_CREAT, $DADA::Config::FILE_CHMOD )
or croak "Couldn't open '$file' because: " . $!;
print ACCESS time;
close(ACCESS)
or croak "Couldn't close '$file' because: " . $!;
$self->unlock_file($lock);
chmod( $DADA::Config::FILE_CHMOD, $file );
warn 'create_first_accessed_file created at ' . $file
if $t;
}
sub create_last_accessed_file {
my $self = shift;
my $file = $self->dir . '/' . $file_names->{last_access};
$file = make_safer($file);
my $lock = $self->lock_file($file);
sysopen( ACCESS, $file, O_WRONLY | O_TRUNC | O_CREAT, $DADA::Config::FILE_CHMOD )
or croak "Couldn't open '$file' because: " . $!;
print ACCESS time;
close(ACCESS)
or croak "Couldn't close '$file' because: " . $!;
$self->unlock_file($lock);
chmod( $DADA::Config::FILE_CHMOD, $file );
warn 'create_last_accessed_file created at ' . $file
if $t;
return 1;
}
sub create_batch_lock {
my $self = shift;
my $file = $self->dir . '/' . $file_names->{batchlock};
$file = make_safer($file);
my $lock = $self->lock_file($file);
sysopen( BATCHLOCK, $file, O_WRONLY | O_TRUNC | O_CREAT, $DADA::Config::FILE_CHMOD )
or croak "Couldn't open, '$file' because: " . $!;
print BATCHLOCK time;