Skip to content

Instantly share code, notes, and snippets.

@dex4er
Forked from anonymous/vanillacomment.pl
Created February 9, 2014 23:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dex4er/8907516 to your computer and use it in GitHub Desktop.
Save dex4er/8907516 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use strict;
use warnings;
use Mail::Message;
use Mail::Message::Convert::HtmlFormatText;
my $af = Mail::Message::Convert::HtmlFormatText->new;
use Encode qw(decode encode_utf8);
binmode STDOUT, ':utf8';
my $from_line = <>;
my $msg = Mail::Message->read(\*STDIN);
my $from = $msg->study('from');
my $subject = $msg->study('subject');
my @parts = $msg->parts('RECURSE');
my @content_types = map { $_->contentType } @parts;
my $text = '';
foreach my $part (@parts) {
my $content_type = $part->contentType;
### $content_type
if (grep { $_ eq 'text/plain' } @content_types) {
next if $content_type ne 'text/plain';
} else {
next if $content_type ne 'text/html';
}
my $body = $part->body;
$text .= do {
if ($content_type eq 'text/html') {
$body = $af->format($body);
decode $body->charset, $body;
} else {
$body->decoded;
}
};
}
$subject =~ s/\[pp-org\]\s//;
while ($subject =~ s/^(Re|Odp|Fwd):\s//) {};
$text =~ s/(^|\n)_{46}.*//s;
print "$subject\n";
print "Od: $from\n";
print $text;
#!/usr/bin/env perl
use strict;
use warnings;
use Smart::Comments;
use DBI;
use Perl6::Slurp;
use POSIX;
binmode STDIN, ':utf8';
my $subject = <STDIN>;
my $from = <STDIN>;
my $body = slurp \*STDIN;
$from =~ s/<([^>]*)>/&lt;$1&gt;/;
$from =~ s/@[^.]*\./@*./;
$body = "$from\n$body";
my $dbh = DBI->connect('DBI:mysql:database=vanilla;host=127.2.3.1;port=3309', 'vanillabot', 'vanillabot', {
RaiseError => 1, AutoCommit => 0, mysql_enable_utf8 => 1,
});
my @row = $dbh->selectrow_array("SELECT DiscussionID FROM GDN_Discussion WHERE Name = ?", {}, $subject);
if (not @row) {
my $sth = $dbh->prepare("INSERT INTO GDN_Discussion (CategoryID, InsertUserID, Name, Body, Format, DateInserted, InsertIPAddress, DateLastComment) VALUES (?, ?, ?, ?, ?, NOW(), ?, NOW())");
$sth->execute(8, 9, $subject, $body, 'Html', '0.0.0.1');
} else {
my $id = $row[0];
my $sth = $dbh->prepare("INSERT INTO GDN_Comment (DiscussionID, InsertUserID, Body, Format, DateInserted, InsertIPAddress) VALUES (?, ?, ?, ?, NOW(), ?)");
$sth->execute($id, 9, $body, 'Html', '0.0.0.1');
$dbh->do("UPDATE GDN_Discussion SET DateLastComment = NOW()");
}
$dbh->commit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment