| 1 |
|
|---|
| 2 |
use strict; |
|---|
| 3 |
|
|---|
| 4 |
use DBD::mysql; |
|---|
| 5 |
use FeedMeLinks::Environment; |
|---|
| 6 |
use LWP; |
|---|
| 7 |
use POSIX qw(strftime); |
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
my $verbose = 0; |
|---|
| 15 |
my $now = strftime "%a %b %e %H:%M:%S %Y", localtime; |
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
my $headersDone = 0; |
|---|
| 19 |
my $messageDone = 0; |
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
my $title; |
|---|
| 23 |
my $submitter; |
|---|
| 24 |
my $link; |
|---|
| 25 |
my $body = ""; |
|---|
| 26 |
|
|---|
| 27 |
my $line; |
|---|
| 28 |
my $tagName = "new"; |
|---|
| 29 |
|
|---|
| 30 |
open(LOG, ">>" . FeedMeLinks::Environment::get_webserver_root() . "/_logs/mail.log") or die "can't open file"; |
|---|
| 31 |
print LOG "--- starting run ---\n" if( $verbose ); |
|---|
| 32 |
|
|---|
| 33 |
while( <> ) { |
|---|
| 34 |
my $line = $_; |
|---|
| 35 |
chomp( $line ); |
|---|
| 36 |
if( !$messageDone ) { |
|---|
| 37 |
if( !$headersDone ) { |
|---|
| 38 |
if( $line eq "" || m/^\s*$/ ) { |
|---|
| 39 |
$headersDone = 1; |
|---|
| 40 |
} elsif( m/^Subject:/ ) { |
|---|
| 41 |
print LOG "THIS IS THE LINK TITLE! $line\n" if( $verbose ); |
|---|
| 42 |
$title = $line; |
|---|
| 43 |
$title =~ s/Subject: //; |
|---|
| 44 |
} elsif( m/^From:/ ) { |
|---|
| 45 |
if( ! $submitter ) { |
|---|
| 46 |
$submitter = $line; |
|---|
| 47 |
$submitter =~ s/From: //; |
|---|
| 48 |
$submitter =~ s/^.*<//; |
|---|
| 49 |
$submitter =~ s/>.*$//; |
|---|
| 50 |
print LOG "THIS IS WHO FROM! $submitter\n" if( $verbose ); |
|---|
| 51 |
} |
|---|
| 52 |
} |
|---|
| 53 |
} else { |
|---|
| 54 |
if( m/^\s*(http|ftp)/ ) { |
|---|
| 55 |
if( ! $link ) { |
|---|
| 56 |
print LOG "THIS IS THE LINK: $line\n" if( $verbose ); |
|---|
| 57 |
$link = $line; |
|---|
| 58 |
$link =~ s/^\s*//; |
|---|
| 59 |
$link =~ s/\s*$//; |
|---|
| 60 |
} else { |
|---|
| 61 |
|
|---|
| 62 |
$body = "$body\n$line"; |
|---|
| 63 |
$body =~ s/Sent via BlackBerry from Cingular Wireless//g; |
|---|
| 64 |
print LOG "$body\n" if( $verbose ); |
|---|
| 65 |
} |
|---|
| 66 |
} elsif( m/^\s*--\s*$/ ) { |
|---|
| 67 |
print LOG "message is done\n" if( $verbose ); |
|---|
| 68 |
$messageDone = 1 |
|---|
| 69 |
} else { |
|---|
| 70 |
next if( m/^$/ ); |
|---|
| 71 |
$body = "$body\n$line"; |
|---|
| 72 |
print LOG "added line to body\n" if( $verbose ); |
|---|
| 73 |
} |
|---|
| 74 |
} |
|---|
| 75 |
} |
|---|
| 76 |
} |
|---|
| 77 |
|
|---|
| 78 |
if( $headersDone && $title && $submitter && $link ) { |
|---|
| 79 |
print LOG "adding link to db\n" if( $verbose ); |
|---|
| 80 |
my $dsn = "DBI:mysql:database=" . FeedMeLinks::Environment::get_database_name() . ";host=localhost"; |
|---|
| 81 |
my $dbh = DBI->connect($dsn, FeedMeLinks::Environment::get_database_user(), FeedMeLinks::Environment::get_database_password() ); |
|---|
| 82 |
|
|---|
| 83 |
if( ! $dbh ) { |
|---|
| 84 |
print LOG "$now [link-by-mail error] cannot connect to DB]\n"; |
|---|
| 85 |
die( "can't connect to DB, fatal error\n" ); |
|---|
| 86 |
} |
|---|
| 87 |
|
|---|
| 88 |
my $qs = "SELECT userid, password FROM users WHERE email = ? OR email = ? LIMIT 1"; |
|---|
| 89 |
my $sth = $dbh->prepare( $qs ); |
|---|
| 90 |
|
|---|
| 91 |
|
|---|
| 92 |
my $encoded_submitter = $submitter; |
|---|
| 93 |
$encoded_submitter =~ s/@/%40/; |
|---|
| 94 |
|
|---|
| 95 |
$sth->execute( $submitter, $encoded_submitter ); |
|---|
| 96 |
my @ary = $sth->fetchrow_array; |
|---|
| 97 |
my $dbUser = $ary[0]; |
|---|
| 98 |
my $password = $ary[1]; |
|---|
| 99 |
if( $dbUser ) { |
|---|
| 100 |
print LOG "we found you and you are $dbUser\n" if( $verbose ); |
|---|
| 101 |
|
|---|
| 102 |
$qs = "INSERT INTO links VALUES( NULL, ?, ?, NULL, NULL, ?, NULL, NULL)"; |
|---|
| 103 |
$sth = $dbh->prepare( $qs ); |
|---|
| 104 |
$sth->execute( $link, $title, $dbUser ); |
|---|
| 105 |
|
|---|
| 106 |
$qs = "SELECT LAST_INSERT_ID() AS last"; |
|---|
| 107 |
$sth = $dbh->prepare( $qs ); |
|---|
| 108 |
$sth->execute(); |
|---|
| 109 |
my $linkID = $sth->fetchrow_array; |
|---|
| 110 |
if( $linkID ) { |
|---|
| 111 |
print LOG "$now [link-by-mail] $dbUser added $linkID\n"; |
|---|
| 112 |
|
|---|
| 113 |
$qs = "SELECT ID FROM tags WHERE name = ? AND userid = ? AND isPrivate IS NULL"; |
|---|
| 114 |
$sth = $dbh->prepare( $qs ); |
|---|
| 115 |
$sth->execute( $tagName, $dbUser ); |
|---|
| 116 |
@ary = $sth->fetchrow_array; |
|---|
| 117 |
my $tagID = @ary[0]; |
|---|
| 118 |
if( ! $tagID ) { |
|---|
| 119 |
print LOG "couldn't find a tag named $tagName; trying to insert it\n" if( $verbose ); |
|---|
| 120 |
$qs = "INSERT INTO tags VALUES( NULL, ?, ?, NULL, NULL)"; |
|---|
| 121 |
print LOG "SQL: $qs with $dbUser, $tagName\n" if( $verbose ); |
|---|
| 122 |
$sth = $dbh->prepare( $qs ); |
|---|
| 123 |
$sth->execute( $dbUser, $tagName ); |
|---|
| 124 |
|
|---|
| 125 |
$qs = "SELECT LAST_INSERT_ID() AS last"; |
|---|
| 126 |
$sth = $dbh->prepare( $qs ); |
|---|
| 127 |
$sth->execute(); |
|---|
| 128 |
@ary = $sth->fetchrow_array; |
|---|
| 129 |
$tagID = @ary[0]; |
|---|
| 130 |
if( $tagID ) { |
|---|
| 131 |
print LOG "created new tag '$tagName' with ID $tagID\n" if( $verbose ); |
|---|
| 132 |
} else { |
|---|
| 133 |
print LOG "creating new tag '$tagName' failed!\n" if( $verbose ); |
|---|
| 134 |
} |
|---|
| 135 |
} |
|---|
| 136 |
if( $tagID ) { |
|---|
| 137 |
print LOG "found existing tag as ID $tagID\n" if( $verbose ); |
|---|
| 138 |
$qs = "INSERT INTO links_tags_xref VALUES( ?, ?)"; |
|---|
| 139 |
$sth = $dbh->prepare( $qs ); |
|---|
| 140 |
$sth->execute( $linkID, $tagID ); |
|---|
| 141 |
|
|---|
| 142 |
my $ua = LWP::UserAgent->new; |
|---|
| 143 |
my $url = "http://feedmelinks.com/xml/set/comment"; |
|---|
| 144 |
my $response = $ua->post( $url, |
|---|
| 145 |
[ 'id' => $linkID, 'comments' => $body, 'user' => $dbUser, 'password' => $password ] |
|---|
| 146 |
); |
|---|
| 147 |
print LOG "response: " . $response->content . "\n" if( $verbose ); |
|---|
| 148 |
die "$url error: ", $response->status_line |
|---|
| 149 |
unless $response->is_success; |
|---|
| 150 |
|
|---|
| 151 |
} else { |
|---|
| 152 |
print LOG "$now [link-by-mail error] no tag found: $dbUser, tag: '$tagName'\n"; |
|---|
| 153 |
die( "cant find a tag for this link, searching for '$tagName' failed, and adding a new one failed" ); |
|---|
| 154 |
} |
|---|
| 155 |
} |
|---|
| 156 |
} else { |
|---|
| 157 |
print LOG "$now [link-by-mail error] unregistered user $submitter\n"; |
|---|
| 158 |
die( "The email address $submitter isn't registered with any Feed Me Links user. Perhaps you registered under another name?" ); |
|---|
| 159 |
} |
|---|
| 160 |
} |
|---|
| 161 |
|
|---|
| 162 |
print LOG "body!: $body\n" if( $verbose ); |
|---|
| 163 |
print LOG "--- ending run ---\n\n" if( $verbose ); |
|---|
| 164 |
close(LOG); |
|---|