function emailListener()
{
    $connection = establishConnection();
    $dbConn = establishDBConnection();
    $messagestatus = "UNSEEN";
    $emails = imap_search($connection, $messagestatus);
    if ($emails) {
        rsort($emails);
        foreach ($emails as $email_number) {
            // echo "in email loop";
            $header = imap_headerinfo($connection, $email_number);
            $message = imap_fetchbody($connection, $email_number, 1.1);
            if ($message == "") {
                $message = imap_fetchbody($connection, $email_number, 1);
            }
            $emailaddress = substr($header->senderaddress, stripos($header->senderaddress, "<") + 1, stripos($header->senderaddress, ">") - (stripos($header->senderaddress, ">") + 1));
            if (!detectOOOmessage($header->subject, $message, $emailaddress)) {
                detectBIOmessage($header->subject, $emailaddress);
            }
            imap_delete($connection, 1);
            //this might bug out but should delete the top message that was just parsed
        }
    }
    $dbConn->query("DELETE FROM away_mentor WHERE tiStamp <= DATE_ADD(NOW(), INTERVAL -1 DAY) limit 1");
    //delete mentors that have been away for more than 24 hours from the away list
}
function emailListener()
{
    // $connection = establishConnection();
    $dbConn = establishDBConnection();
    $path = '/home/fiucoplat/Maildir/new';
    $files = scandir($path);
    foreach ($files as $afile) {
        $file = fopen($path . "/" . $afile, "r");
        $from = "";
        $subject = "";
        $body = "";
        $isbody = 0;
        $fromisSet = 0;
        $subjectisSet = 0;
        while ($line = fgets($file)) {
            //echo $line . "\n\n";
            if ($isbody == 1) {
                $body = $body . $line;
            }
            if (strstr($line, "From: ") && strstr($line, "<") && $fromisSet == 0) {
                //   echo $line;
                $from = $line;
                $from = substr($from, stripos($from, ":") + 2);
                if (stristr($from, "<")) {
                    $from = substr($from, stripos($from, "<"));
                }
                $from = str_replace(array("<", ">", " ", "\n", "\r"), "", $from);
                $fromisSet = 1;
            }
            if (strstr($line, "Subject: ") && $subjectisSet == 0) {
                //  echo $line;
                $subject = $line;
                $subjectisSet = 1;
            }
            if (stristr($line, "content-type: ")) {
                $isbody = 1;
            }
        }
        //echo "\n\n\nPARSED INFORMATION \n\n\nfrom:".$from."99\n";
        //echo "subject: ".$subject."\n";
        //echo "body: ".$body."\n";
        fclose($file);
        if (strlen($body) > 5) {
            $result1 = detectOOOmessage($subject, $body, $from);
            if ($result1 == 0) {
                detectBIOmessage($subject, $from);
            }
            if ($result1 != 3) {
                unlink($path . "/" . $afile);
            } else {
                //send an email to the sys admin saying that the system got confused by this email look at it
            }
        }
    }
    $daysAway = $dbConn->query("Select setting from reassign_rules where rule_id  = 2")->fetch_assoc()["setting"];
    $daysAway = $daysAway * -1;
    $dbConn->query("DELETE FROM away_mentor WHERE tiStamp <= DATE_ADD(NOW(), INTERVAL {$daysAway} DAY) limit 10");
    //delete mentors that have been away for more than 24 hours from the away list
}