summaryrefslogtreecommitdiff
blob: 03875f16f7cd96f1ecac7afb8afa1b99bcff4789 (plain)
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
#!/usr/bin/perl -w

# $Id: tonline.pl,v 1.5 2003/12/10 08:55:40 endresct Exp $

use strict;
use HTML::Entities;
use MIME::Base64;
use Net::SMTP;
use LWP;
use LWP::Debug qw(+);

if ($ARGV[0] eq "--help" ) {
    print 'usage: tonline.pl {USER}  {PASS} {LOCALUSER}'."\n";
    print 'usage: tonline.pl hmuster secret heinz@localserver'."\n";
    exit;
}

# Configuration
my $uname     = $ARGV[0];   # change to your t-online name
my $pword     = $ARGV[1];   # change to your password
my $localname = $ARGV[2];   # change to your local name
my $deliver   = 'smtp';  # change to 'smtp', if Hamster or
                         # <yourfavoriteunixsmtpserver>
                         # is running on the same machine

my $url   = 'https://modem.webmail.t-online.de';
my $ua    = LWP::UserAgent->new();
my $spool = '/var/spool/mail/';

my $location = createLogin( $ua, $url, $uname, $pword );

# Comment out the first line an uncomment the second to fetch the "Ablage" folder
my $inbox = $ua->get($location);

if ($location) {

    $location =~ s/main.cgp.*//;

    my @ids = grepIDs($inbox);

    for ( my $i = 0 ; $i < @ids ; $i++ ) {
	my $mail = fetchFile( $location, $ids[$i] );
	open(LOGFILE, "+>>", "/root/heide.mail.log");
	print LOGFILE $mail;
	close(LOGFILE);
	my $issave = fileMail( $mail, $spool, $localname );
	
	if ($issave) {
	    deleteMail($location, $ids[$i], $i);
	    # it's commented out, but: BE CAREFUL - please, save FIRST.
	}

	sleep(2);  # don't kill webservers with too fast polls
    } ## end for ( my $i =...
}

$ua->get( $url . "/logout.cgp" );

sub createLogin {
    my ( $ua, $url, $uname, $pword ) = @_;

    my $location;
    my $form_login;
    my $form_pass;
    my $form;
    my $id;
    my $resp;
    
    push @{ $ua->requests_redirectable() }, 'POST';
    
    $form = $ua->get($url . "/index.cgp") or die "Couldn't fetch $url";
    die $form->message() if $form->is_error();
    
    ($id) = $form->content() =~ m{/([^/]+)/login_in_frame\.cgp}s;
    
    ($form_login) = $form->content() =~ m{/.*type="text" name='([^']+)}s;
    ($form_pass) = $form->content() =~ m{/.*type="password" name='([^']+)}s;
    $resp = $ua->post(
        $url . "/main.cgp",
        [
            $form_login => $uname,
            $form_pass  => $pword,
            'js'        => '0',
            'sessionid' => $id
        ]
    );

    if ($resp->header('Refresh')) {
        ($location) = ( $resp->header('Refresh') =~ m/URL=(.*)/ );
        return $url . $location;
    } else {
        return
    }

} ## end sub createLogin

sub grepIDs {
    my $mbox = shift;
    my %ids;

    foreach my $key ( $mbox->content() =~ m/MAIL=(\d+?)\"/sg ) {
        $ids{$key} = 1;
    }

  return keys(%ids);
} ## end sub grepIDs

sub deleteMail {
    my ( $url, $id, $count ) = @_;
    my $resp =
      $ua->get( $url . "main.cgp?MAIL[$count]=" . $id . "&Loeschen.x=1" );

  return 0 unless ( $resp->status_line() =~ /OK/ );
} ## end sub deleteMail

sub fileMail {
    my ( $mail, $spool, $localname ) = @_;

    if ( $deliver eq 'smtp' ) {
        my ($from) = ( $mail =~ m/From: .+?<([^<]+)>/ );
        my $smtp = Net::SMTP->new('localhost')
          or die "Can't connect SMTP localhost!\n";

	$mail =~ s/^From\s([^@]+@[^@ ]+)\s.*/From: $1/;
	## Hopefully fixes mailing problems
	$mail =~ s/^-- /\#\#-- /;
	$mail =~ s/^----------/\#\#---------/;

        $smtp->mail($from);
        $smtp->to($localname);
        $smtp->data();
        $smtp->datasend($mail);
        $smtp->dataend();
        $smtp->quit;

	## AutoResponder
        $smtp = Net::SMTP->new('localhost')
          or die "Can't connect SMTP localhost!\n";

	$mail = "From: Heide u.Bernd Wrobel <hbwrobel\@torp4.de>\n";
	$mail .= "Subject: Adressaenderung\n";
	$mail .= "Content-Type: text/plain; charset=UTF-8\n";
	$mail .= "Date: " . scalar(localtime()) . "\n";
	$mail .= '
Automatische Mitteilung
#---------------------#

Lieber Absender,

Sie haben eine E-Mail an die Adresse hbwrobel@t-online.de versendet.
Da wir diese Adresse innerhalb des nächsten halben Jahres löschen
möchten, bitten wir Sie unseren Eintrag in Ihrem Adressbuch auf 
hbwrobel@torp4.de zu aktualisieren.

Vielen Dank!

Mit freundlichen Grüßen

Heide und Bernd Wrobel
';

        $smtp->mail('hbwrobel@torp4.de');
        $smtp->to($from);
        $smtp->data();
        $smtp->datasend($mail);
        $smtp->dataend();
        $smtp->quit;
      return 1;
    } ## end if ( $deliver...
    elsif ( $deliver eq 'mbox' ) {
        open( MBOX, ">>$spool$localname" )
          or die "Can't open Mailbox of $localname!\n";
        print MBOX $mail;
        close MBOX;
      return 1;
    } ## end elsif ( $deliver...
    else {
      return 0;
    }
} ## end sub fileMail

sub fetchFile{
# fetches via t-online the complete mail with headers and attachments as file. The file
# isn't compatible to mbox. Please, adjust by yourself.
# Create a unique ID at the main for-loop - just the content is
# returned by this function, the filename has to be done by yourself.

    my ( $url, $mail_id ) = @_;
    my $resp =
      $ua->get( $url
          . "main.cgp?MAIL[0]="
          . $mail_id
          . "&Speichern.x=1&Speichern.y=1" );
    my $mail;

    if ( $resp->is_redirect() ) {
        my $mail = $ua->get( $resp->headers()->{'location'} );
      return $mail->content();
    } else {
	return $resp->content();
    }
} ## end sub fetchFile