Auto-Submit and Verification @ Spamcop

Discussion in 'Technical' started by sjau, May 21, 2006.

Thread Status:
Not open for further replies.
  1. sjau

    sjau Local Meanie Moderator

    Hiya

    some of you may also use spamcop in order to send their spams to. However the annoying part is, that you have to manually verify the submitted spam at their page.

    Well, I kinda was bored doing so and thought this could be automatized.

    I have now come with a couple of shell scripts (2 pcs) and 1 php script which does the job now.

    All I have to do now is putting the spam that went into my inbox into my "spam" box :)

    In order to use those you may have to modifiy them :) especially regarding paths and stuff.

    1.) Signup at www.spamcop.net --> make sure to use a unique email address which you can then move to a certain folder. In my case I used "spamcop a.t. roleplayer.org" and I have a procmail recipe like that:

    Code:
    :0:
    * ^To: spamcop a.t. roleplayer.org
    Maildir/.Spamcop-Reply/
    
    All email that goes to spamcop .a.t. roleplayer.org will be put into my little Spamcop-Reply folder.

    2.) Forwarding all your spam to the unique spamcop email address that you got when you signed up:

    fe.sh (forward email)
    Code:
    #!/bin/bash
    
    # ENTER PATH OF THE EMAILS THAT ARE TO BE SUBMITTED TO SPAMCOP
    PATH="/home/mail/web4p1/Maildir/.Spam/cur"
    
    # ENTER YOUR SPAMCOP EMAIL ADDRESS
    EMAIL="........ a.t. spam.spamcop.net"
    
    #################################################################
    #################################################################
    
    cd $PATH
    
    for FILENAME in *
    do
    
            # Create email and submit it to the supplied spamcop address
            /usr/bin/mime-construct \
            --subject "Forwarded spam (MIME encoded)" \
            --attachment "Original message" \
            --type message/rfc822 \
            --encoding base64 \
            --file $FILENAME \
            --to "$EMAIL"
    
            # Train this email to be spam to the bayesian SA filters
            /usr/bin/sa-learn --spam $FILENAME
    
            # Delete email
            /bin/rm $FILENAME
    
    done
    
    Just define your paths there and enter your spamcop email address...
    then the script loops though the spamfolder and sends it as attachment to the spamcop email address. (mime-encode is required here: for debian: apt-get install mime-encode).

    Then I do let Spamassassin train the spam as spam (maybe you need to set other paths)

    And finally it is being deleted...

    If you don't want SA to go over the spam or not having it deleted, you could move it somewhere else instead.

    3.) The fe.sh script only forwards the mail to your spamcop account. You will then receive a reply to your submitted email address that you used when you signed up and then you are required to verify that it is spam.

    I wrote now this little script here to "automate" this process:

    vs.sh (verify spam)
    Code:
    #!/bin/bash
    
    # ENTER PATH OF THE VERIFICATION EMAILS FROM SPAMCOP
    PATH="/home/mail/web4p1/Maildir/.Spamcop-Reply/cur"
    
    # ENTER WEBPATH TO PHP SCRIPT
    URL="http://www.roleplayer.org/spamcop/index.php"
    
    #################################################################
    #################################################################
    
    cd $PATH
    
    for FILENAME in *
    do
    
            # Get the supplied URL from the spamcop email
            DATA=`/bin/grep -F http://www.spamcop.net/sc?id= $FILENAME`
            echo $DATA
    
            # Submit the URL to the PHP script
            /usr/bin/lynx -dump $URL?data=$DATA
    
            # Remove that file
            /bin/rm $FILENAME
    
    done
    
    Again, a very simple script. You need to alter the paths to your system and then put the following PHP file somewhere on your web and enter the path here.
    The script goes again to the path where those verifcations email lay and loops trought them. It extracts the line with the ID code, echoes it (for you to see if you manually call the script) and then it calls the php script with Lynx. The PHP Script does the rest.
    Finally the verification email is being deleted.

    index.php
    PHP:
    <?php

    // Function for displaying an array in a table (also works on multidimensional arrays)
    function displayArray($aArray) {
        if (
    is_array($aArray) && (count($aArray) > 0)) {
            print(
    "<table border=1>");
            print(
    "<tr><th>Key</th><th>Value</th></tr>");
            foreach (
    $aArray as $aKey => $aValue) {
                print(
    "<tr>");
                if (!
    is_array($aValue)) {
                    if (empty(
    $aValue)) {
                        print(
    "<td>$aKey</td><td><i>$aValue</i></td>");
                    } else {
                        print(
    "<td>$aKey</td><td>$aValue</td>");
                    }
                } else {
                    print(
    "<td>$aKey(array)</td><td>");
                    
    displayArray($aValue);
                    print(
    "</td>");
                }
                print(
    "</tr>");
            }
            print(
    "</table>");
        } else {
            print(
    "<i>empty or invalid</i>");
        }
    }

    // The default form fields (those are being repeated to everyone the mail is sent to)
    $offender = array("type""master""info""sc_comment""comment");

    // The default form fields (these are the unique fields)
    $form_vars = array("action""spamid""crc""date""source""reports""goodrelay""max""notes");

    // Get the URL from the attached parameters
    $data_org $_GET["data"];

    // Split it at sc?id= so that you have the "id code" only
    $data explode("sc?id="$data_org);
    $data $data[1];

    // Just some verification
    echo "SC-ID: " $data;

    if(
    $data == "") {
        echo 
    "done";
        exit;
    }

    echo 
    "<hr>";

    // Require the snoopy class for retrieving the form
    require_once("Snoopy.class.php");

    $snoopy = new Snoopy;

    $snoopy->fetch("http://www.spamcop.net/sc?id=" $data);

    $results $snoopy->results;

    // Another verification that it is actually a spam email that can be submitted....
    $results explode('<form action="/sc"'$results);
    $results $results[1];

    if(
    $results == "") {
        echo 
    "done";
        exit;
    }

    // Count the number of recipients
    $i substr_count($results'textarea name="comment');

    while (
    $i 0) {

        foreach(
    $offender as $val) {

            
    // Get Field Value
            
    $findme 'name="' $val $i '"';
            
    $offset strlen($findme);
            
    $pos_start strpos($results$findme) + $offset;
            
    $pos_end strpos($results">"$pos_start);
            
    $res substr($results$pos_start$pos_end);    
            
    $res explode('"'$res);
            
    $res $res[1];
            if(
    $val == "comment") { $res ""; }

            
    $submit_vars["send".$i] = "on";
            
    $submit_vars[$val.$i] = $res;

        }

        
    $i--;

    }

    $submit_vars["submit"] = "Send Spam Report(s) Now";

    foreach(
    $form_vars as $val) {

        
    // Get Field Value
        
    $findme   'name="' $val '"';
        
    $offset strlen($findme);
        
    $pos_start strpos($results$findme) + $offset;
        
    $pos_end strpos($results">"$pos_start);
        
    $res substr($results$pos_start$pos_end);    
        
    $res explode('"'$res);
        
    $res $res[1];
        if(
    $val == "notes") { $res ""; }

        
    $submit_vars[$val] = $res;

    }

    // Display the data to be sent --> can be deactivated
    displayArray($submit_vars);

    // Create a new instance to submit the form data
    $snoopy = new Snoopy;
        
    $submit_url "http://www.spamcop.net/sc";

    if(
    $snoopy->submit($submit_url,$submit_vars)) {
        while(list(
    $key,$val) = each($snoopy->headers)) {
            echo 
    $key.": ".$val."<br>\n";
        }
        echo 
    "<p>\n";
        echo 
    "<PRE>".htmlspecialchars($snoopy->results)."</PRE>\n";
    } else {
        echo 
    "error fetching document: ".$snoopy->error."\n";
    }

    ?>
    The PHP script makes use of the snoopy class (thx to Timme for pointing this out to me), it then retrieves the form fields on for this id and generates the fields that are required to be filled in and submitted.
    Snoopy can be downloaded here: http://sourceforge.net/projects/snoopy/

    4.) Finally, if everything runs fine, then you want to create a cron to do this.

    Here's what I use:

    web4p1cron.txt
    Code:
    0,20,40 * * * * /bin/sh /home/mail/web4p1/fe.sh > /home/mail/web4p1/output1.txt
    10,30,50 * * * * /bin/sh /home/mail/web4p1/vs.sh > /home/mail/web4p1/output2.txt
    
    If you are root and want to enter the cron as special user, just enter:
    crontab -uUSER CRON.TXT where USER=desired user and CRON.TXT file that contains the cronjob info.

    If you have any suggestions on how to improve that, please let me know :)

    P.S.: I made the shell scripts executable for the user and the group where web4p1 does belong to. Don't really know if this is necessary but it works :)

    P.P.S.: I've attached a file containing all those scripts.
     

    Attached Files:

    Last edited: May 21, 2006
Thread Status:
Not open for further replies.

Share This Page