1# Never modify Config.PPM, always Config.PL
2# (Whenever you generate a distribution Config.PPM is overwriten by Config.PL)
3
4# just to tell make we are done with configuring
5open CFG, '>Config';print CFG "\n";close CFG;
6
7use Cwd;
8use FileHandle;
9use Socket;
10
11if ($ENV{PERL_MM_USE_DEFAULT} || !-t STDIN) { exit(); }
12
13print "\n\n
14################# Default new object settings for Mail::Sender #################
15\n";
16
17sub Ignore {
18    print "\nDo you want to change the setting or use this one in spite of the errors?\n\t(C)hange / (U)se : ";
19    local $_;
20    while (<STDIN>) {
21        return 1 if /^u$/i;
22        return if /^c$/i;
23        print "\t(C)hange / (U)se : ";
24    }
25    exit;
26}
27
28
29if ($0 =~ /\.PPM$/i) {
30
31print "Running from PPM...\n\n";
32
33chdir '..';
34
35    # find Mail/Sender.pm
36    foreach $dir (@INC) {
37        if (-e $dir.'/Mail/Sender.pm') {
38            $configfile = $dir.'/Mail/Sender.config';
39            last;
40        }
41    }
42    unless ($configfile) {
43        print STDERR "The module Sender.pm is not found. Are you realy running the script via PPM install ???\n";
44        <STDIN>;
45        exit(1);
46    }
47    if (-e $configfile) {
48        print "You have already specified some defaults for Mail::Sender,
49They are saved in $configfile.
50If you go on you will OVERWRITE the whole file!
51You'd better edit the file by hand.\n";
52        $new=' new and OVERWRITE old';
53    }
54
55} else { # called directly
56    die "Do not execute this script directly, run:\n\tperl Makefile.PL
57\tmake\n\tmake test\n\tmake install\n
58If you are using ActivePerl under Windows use 'nmake' instead of 'make'.
59nmake.exe may be found at ftp://ftp.microsoft.com/Softlib/MSLFILES/nmake15.exe\n"
60     unless -d 'blib/lib/Mail';
61    {
62     my $dir;
63     foreach $dir (@INC) {
64        if (-e $dir.'/Mail/Sender.config') {
65            print "You have already specified some defaults for Mail::Sender,
66    They are saved in $dir/Mail/Sender.config.
67    If you go on you will OVERWRITE the whole file!
68    You'd better edit the file by hand.\n";
69            $new=' new and OVERWRITE old';
70            last;
71        }
72     }
73    }
74}
75
76print "\nSpecify$new defaults for Mail::Sender? (y/N) ";
77$res = <STDIN>;
78exit unless $res =~ /^y$/i;
79
80if (! $configfile) {
81 print "\n\t... will be saved to .../lib/Mail/Sender.config\n";
82 $configfile = 'blib/lib/Mail/Sender.config';
83} else {
84 print "\n\t... will be saved to $configfile\n";
85}
86
87while (1) {
88 print "\nDefault SMTP server (hostname or IP address)\n\n\t: ";
89 $smtp=<STDIN>;chomp $smtp;
90 last unless $smtp;
91
92 print "\n\t...Trying $smtp...";
93
94 my $proto = (getprotobyname('tcp'))[2];
95 my $port  = getservbyname('smtp', 'tcp');
96
97 $smtp =~ s/^\s+//g; # remove spaces around $smtp
98 $smtp =~ s/\s+$//g;
99
100 my $smtpaddr = inet_aton($smtp);
101
102 unless ($smtpaddr) {
103  print "FAILED\n\t...cannot find server '$smtp' !!!\n";
104  if (Ignore()) {last} else {next};
105  next;
106 }
107
108 my $s = &FileHandle::new(FileHandle);
109
110 if (!socket($s, AF_INET, SOCK_STREAM, $proto)) {
111  die <<"*END*";
112Call to socket() failed. You either do not have enough permissions
113or something really strange happened. Until you fix this neither Mail::Sender
114nor any other network related module will work. :-((
115*END*
116  if (Ignore()) {last} else {next};
117 }
118
119 if (!connect($s, sockaddr_in($port, $smtpaddr))) {
120  print "FAILED\n\t...the server is either down or doesn't accept connections\n\ton port 25(SMTP).";
121  if (Ignore()) {last} else {next};
122 }
123
124 my($oldfh) = select($s); $| = 1; select($oldfh);
125
126 $_ = <$s>;
127 if (/^[45]\d*\s*(.*)$/) {
128  close $s;
129  print "FAILED\n\t...the server replied '$1'.\n";
130  if (Ignore()) {last} else {next};
131 }
132
133 print $s "helo localhost\r\n";
134 $_ = <$s>;
135 if (/^[45]\d*\s*(.*)$/) {
136  close $s;
137  print <<"*END*";
138To my greeting the server replied '$1'.
139It probably meens that you will have to specify the client name
140when connecting to this server. You should specify the default
141client name later during this configuration, otherwise you will have
142to include parameter client=> in every "new Mail::Sender" call.
143*END*
144
145  if (Ignore()) {last} else {next};
146 }
147
148 print $s "quit\r\n";
149 print "successful :-)\n";
150 last;
151
152}
153
154$default{smtp} = "'$smtp'";
155
156print <<"*END*";
157
158*********************************************************************
159**** ALL of the following Defaults are interpreted as perl code *****
160*********************************************************************
161
162*********************************************************************
163Default FROM value (must be perl code / ENTER for none):
164
165\tExamples:
166\t   *   'user\@yourdomain.com'
167\t   *   getlogin() . '\@yourdomain.com'
168\t   *   'Your Name <yourname\@yourdomain.com>'\n
169*END*
170print "\t: ";
171$default{from}=<STDIN>;
172for ($default{from}) {
173	chomp;
174	$_ = "'$_'" if /^[\w\d.-]+@(?:[\w\d-]+\.)*[\w\d]+$/;
175}
176delete $default{from} unless $default{from};
177
178print "\n*********************************************************************\n";
179print "Default for REPLY-TO field (must be perl code / ENTER for none):\n\n\t: ";
180$default{replyto}=<STDIN>;
181for ($default{replyto}) {
182	chomp;
183	$_ = "'$_'" if /^[\w\d.-]+@(?:[\w\d-]+\.)*[\w\d]+$/;
184}
185delete $default{replyto} unless $default{replyto};
186
187print "\n*********************************************************************\n";
188print "Default for CC field (must be perl code / ENTER for none):\n\n\t: ";
189$default{cc}=<STDIN>;
190for ($default{cc}) {
191	chomp;
192	$_ = "'$_'" if /^[\w\d.-]+@(?:[\w\d-]+\.)*[\w\d]+$/;
193}
194delete $default{cc} unless $default{cc};
195
196print "\n*********************************************************************\n";
197print "Default for BCC field (must be perl code / ENTER for none):\n\n\t: ";
198$default{bcc}=<STDIN>;
199for ($default{bcc}) {
200	chomp;
201	$_ = "'$_'" if /^[\w\d.-]+@(?:[\w\d-]+\.)*[\w\d]+$/;
202}
203delete $default{bcc} unless $default{bcc};
204
205print "\n*********************************************************************\n";
206print "Default name of the client MACHINE used when connecting\nto the SMTP server (must be perl code / ENTER for none):\n\n\t: ";
207$default{client}=<STDIN>;
208chomp $default{client};delete $default{client} unless $default{client};
209
210print "\n*********************************************************************\n";
211print "Default additional headers (must be perl code / ENTER for none):\n\n\t: ";
212$default{headers}=<STDIN>;
213chomp $default{headers};delete $default{headers} unless $default{headers};
214
215print "\n*********************************************************************\n";
216print "Default encoding of message bodies (N)one, (Q)uoted-printable, (B)ase64:\n\n\t: ";
217while (<STDIN>) {
218 chomp;
219 /^q$/i and $default{encoding} = "'Quoted-printable'" and last;
220 /^b$/i and $default{encoding} = "'Base64'" and last;
221 (/^n$/i or /^$/) and last;
222}
223
224print "\n*********************************************************************\n";
225print "Default charset of message bodies (must be perl code / ENTER for none):\n\n\t: ";
226$default{charset}=<STDIN>;
227chomp $default{charset};
228unless ($default{charset}) {
229 delete $default{charset}
230} else {
231 $default{charset} =~ /^[\w\d_-]+$/ and $default{charset} = "'$default{charset}'";
232}
233
234print "\n*********************************************************************\n";
235print "*********************************************************************\n";
236
237#sleep 20;
238
239open CFG, '>'.$configfile;
240select CFG;
241print "### Config file for Mail::Sender\n### generated by Config.PL (".scalar(localtime).")\n\n";
242
243print "%default = (\n";
244
245foreach (keys %default) {
246    print "\t$_ => $default{$_},\n";
247}
248
249print ");\n\n";
250
251print <<'*END*';
252# The contents of this variable will be inserted into the headers of every
253# mail message sent by Mail::Sender.
254#
255#  $Mail::Sender::SITE_HEADERS = "X-Sender: ".getlogin()."\@mysite.com";
256
257
258# This function will be evaluated for every mail message before contacting the server.
259# You may change all the options of the message and even cancel the message.
260#
261# sub SiteHook {
262# }
263
264
2651;
266
267*END*
268
269select STDOUT;
270close CFG;
271
272print "The defaults were saved into $configfile
273Please review the code in there if 'make test' or
274every 'use Mail::Sender;' command fails.";
275
2761;
277