]> mj.ucw.cz Git - pciids.git/blobdiff - scripts/mailbot
Sort no longer support +1, using -k1
[pciids.git] / scripts / mailbot
index e8a4a3be9c78e615bfecd91aaf2c03574b4288da..f2eab76113cdb67bbb7ae74e1f5f128885114567 100755 (executable)
@@ -1,7 +1,7 @@
 #!/usr/bin/perl
 # Mail robot for processing of PCI ID submissions
 # (c) 2001--2002 Martin Mares <mj@ucw.cz>
-# 2008 Michal Vaner <vorner@ucw.cz>
+# (c) 2008 Michal Vaner <vorner@ucw.cz>
 
 use Mail::Header;
 use Getopt::Long;
@@ -34,43 +34,37 @@ my $subject = "";
 my $tables = PciIds::DBQ::new( connectDb() );
 
 my $hasAuth = $tables->dbh()->prepare( 'SELECT id FROM users WHERE email = ?' );
-my $addAuth = $tables->dbh()->prepare( "INSERT INTO users (email, passwd) VALUES(?, '')" );
 my $hasItem = $tables->dbh()->prepare( "SELECT 1 FROM locations WHERE id = ?" );
 my $addItem = $tables->dbh()->prepare( "INSERT INTO locations (id, parent) VALUES (?, ?)" );
-my $addComment = $tables->dbh()->prepare( "INSERT INTO history (owner, location, text, nodename, nodedescription) VALUES (?, ?, ?, ?, ?)" );
-
-sub getAuthor( $ ) {
-       my( $mail ) = @_;
-       $hasAuth->execute( $mail );
-       if( my( $id ) = $hasAuth->fetchrow_array ) {
-               return $id;
-       } else {
-               tlog( "mailbot: Creating user $mail" );
-               $addAuth->execute( $mail );
-               my $nid = $dbh->last_insert_id( undef, undef, undef, undef );
-               tlog( "mailbot: User ($mail) id: $nid" );
-               return $nid;
-       }
-}
+my $addComment = $tables->dbh()->prepare( "INSERT INTO history (owner, location, discussion, nodename, nodenote) VALUES (?, ?, ?, ?, ?)" );
 
 sub submitItem( $$$$$ ) {
        my( $id, $name, $description, $text, $author ) = @_;
        my $created;
-       $id =~ s/(.{8})(.*)/$1\\$2/;
-       $id =~ s/(.{4})(.*)/$1\\$2/;
+       $id =~ s/(.{8})(.+)/$1\/$2/;
+       $id =~ s/(.{4})(.+)/$1\/$2/;
        $id = "PC/$id";
+       if( length $id > 12 ) {
+               my( $vendor ) = ( $id =~ /^PC\/....\/....\/(....)/ );
+               $vendor = "PC/$vendor";
+               $hasItem->execute( $vendor );
+               error( "Missing subsystem vendor" ) unless( $hasItem->fetchrow_array );
+       }
        $hasItem->execute( $id );
        unless( $hasItem->fetchrow_array ) {
                tlog( "mailbot: Item created (empty) $id" );
                my $parent = $id;
-               $parent =~ s/\/[^\/]*//;
+               $parent =~ s/\/[^\/]*$//;
                $addItem->execute( $id, $parent );
                $created = 1;
        }
        $addComment->execute( $author, $id, $text, $name, $description );
        my $hid = $tables->last();
-       tlog( "mailbot: Comment created $hid $id ".logEscape( $name )." ".logEscape( $description )." ".logEscape( $text ) );
+       tlog( "mailbot: History created $hid $id ".logEscape( $name )." ".logEscape( $description )." ".logEscape( $text ) );
        notify( $tables, $id, $hid, $created ? 2 : 1, $created ? 0 : 1 );
+       if( !$tables->notifExists( $author, $id ) ) {
+               $tables->submitNotification( $author, $id, { 'recursive' => 0, 'notification' => 0, 'way' => 0 } );
+       }
 }
 
 if (!$patch) {
@@ -104,9 +98,18 @@ if (!$patch) {
        $author = $reply_plain;
 }
 
-$tprefix = "tmp/mbot-$$";
-$home = "../..";
-mkdir("tmp", 0777);
+$home = "$ENV{HOME}/";
+$tprefix = "${home}tmp/mbot-$$";
+# Little hack to stop spam: ignore everything from people not already in database
+$hasAuth->execute( $author );
+my( $authorId ) = ( $hasAuth->fetchrow_array );
+if( defined $authorId ) {
+       tlog( "mailbot: Active user ($author) id: $authorId" );
+} else {
+       blackhole("Dropping email from $author, not in database.\n");
+}
+
+mkdir("${home}tmp", 0777);
 mkdir($tprefix, 0777) || error("Cannot create tmpdir");
 chdir($tprefix) || error("Cannot chdir to tmpdir");
 
@@ -193,13 +196,13 @@ sub process
                } else {
                        print LOG "Patch succeeded.\n";
                        print LOG "Parsing patched file.\n";
-                       print LOG `$home/tools/ids_to_dbdump <$orig 2>&1 >orig.db.unsorted`;
+                       print LOG `$home/bin/ids_to_dbdump <$orig 2>&1 >orig.db.unsorted`;
                        $? && error("Error parsing original ID database");
-                       print LOG `sort +1 <orig.db.unsorted >orig.db`;
+                       print LOG `sort -k1 <orig.db.unsorted >orig.db`;
                        $? && error("Error sorting original ID database");
-                       print LOG `$home/tools/ids_to_dbdump <pci.ids 2>&1 >new.db.unsorted`;
+                       print LOG `$home/bin/ids_to_dbdump <pci.ids 2>&1 >new.db.unsorted`;
                        $? && error("Error parsing the patched pci.ids file");
-                       print LOG `sort +1 <new.db.unsorted >new.db`;
+                       print LOG `sort -k1 <new.db.unsorted >new.db`;
                        $? && error("Error sorting the patched pci.ids file");
                        print LOG "Finding ID differences.\n";
                        `diff -U0 new.db orig.db >diffs`;
@@ -207,7 +210,6 @@ sub process
                        elsif (!$?) { error("No ID changes encountered."); }
                        open(DIFF, "diffs") || error("Cannot open the diff");
                        $subject = undef if $subject eq '';
-                       my $authorId = getAuthor( $author );
                        my $live = (!$emulate && !$debug);
                        while (<DIFF>) {
                                chomp;
@@ -223,11 +225,11 @@ sub process
                                print LOG "$id\t$name\t$cmt\n";
                                submitItem( $id, $name, $cmt, $subject, $authorId ) if $live;
                        }
-                       $dbh->commit();
+                       $tables->dbh->commit();
                        close DIFF;
                        $time = localtime;
-                       `echo >>$home/log/mailbot.log "## $time $reply"`;
-                       `cat result >>$home/log/mailbot.log`;
+                       `echo >>$home/mailbot.log "## $time $reply"`;
+                       `cat result >>$home/mailbot.log`;
                        print LOG "Done.\n";
                        mail_reply("OK");
                        cleanup();
@@ -239,7 +241,7 @@ sub process
 sub mail_reply
 {
        my $reason = shift @_;
-       my $sendmail_opts = "-fmj+iderr\@ucw.cz '$reply_plain' mj+idecho\@ucw.cz";
+       my $sendmail_opts = "-fvorner+iderr\@ucw.cz '$reply_plain' vorner+idecho\@ucw.cz";
        if ($debug || $reply eq "") {
                print "$reason\n";
                return;
@@ -251,7 +253,7 @@ sub mail_reply
                print LOG "Unable to ask mailer for replying!!!\n";
                exit 1;
        }
-       print MAIL "From: The PCI ID Robot <mj+iderr\@ucw.cz>\n";
+       print MAIL "From: The PCI ID Robot <vorner+iderr\@ucw.cz>\n";
        print MAIL "To: $reply\n";
        print MAIL "Subject: IDbot: $reason\n";
        print MAIL "In-Reply-To: $msgid\n" if $msgid ne "";