/** 
 * Processes all jobs of a bot defined in config.php. 
 * @params
 *		$bot	:array, as defined in config.php
 * @returns nothing
 */
function process($bot)
{
    foreach ($bot['jobs'] as $job_label => $job) {
        $firstrun = !sizeof(get_logged_issues($job_label)) > 0;
        //Anything logged yet for this job?
        echo "\nProcessing job '{$job_label}'\n";
        echo "------------------------------------------------";
        $starting_id = get_last_initiative($job_label, $bot['starting_id']);
        //the bot's starting id is only used as fallback
        echo "[starting with initiative {$starting_id}]\n";
        if ($firstrun) {
            $event = array("issue_id" => -2, "initiative_id" => -2, "job" => $job_label);
            log_event($event);
            //initiatiate log table
            echo "First run for this job on this bot. No tweets will be sent.\n\n";
        }
        $query = $job['query'] . "&min_id=" . $starting_id;
        //make sure to get only recent initiatives
        $result = lf_query($bot['lf'], $query);
        //get all initiatives needed for the current job
        $job_labels = (array) $job['dont_retweet'];
        //this prevents issues beeing tweeted for multiple jobs
        $job_labels[] = $job_label;
        $logged_issues = get_logged_issues($job_labels);
        //events already twittered
        $todo = 0;
        //as of now there is no event to process
        $twittered = 0;
        //nothing has been twittered yet
        $new_event_found = false;
        //nothing new has been detected yet - suprise
        foreach ($result as $initiative) {
            log_and_flush();
            // Log and flush the output buffer :)
            if (in_array($initiative['issue_state'], (array) $job['states'])) {
                //process only those whose state is among those of interest
                $todo++;
                //one initiative found, so one more to do :)
                $issue_id = $initiative['issue_id'];
                //get the issue_id, that is NOT the initiative's id!
                $initiative_id = $initiative['id'];
                $event = array("issue_id" => $issue_id, "initiative_id" => $initiative_id, "job" => $job_label);
                //current event
                if (!in_array($issue_id, $logged_issues)) {
                    //dont tweet the same issue twice
                    $new_event_found = true;
                    echo "\n\tIssue {$initiative['issue_id']} has not yet been twittered for one of the following job(s): " . implode(', ', $job_labels) . ". ({$initiative_id})\n";
                    $url = get_issue_link($bot['lf'], $initiative);
                    echo "\t\tURL: {$url}\n";
                    if ($firstrun) {
                        $shorturl = true;
                    } else {
                        $shorturl = shortlink($url);
                        //bit.ly
                    }
                    if ($shorturl) {
                        //the url actually got shortened
                        echo "\t\tShortened URL: {$shorturl}\n";
                        $tweet = sprintf($job['format'], $bot['hashtag'], $shorturl, $issue_id);
                        echo "\t\tSending tweet: '{$tweet}'\n";
                        if ($firstrun) {
                            // Prevent the bot from tweeting on the first run, there'd be tons of old stuff otherwise
                            $done = true;
                            echo "\t\tNo tweets on first run.\n";
                        } else {
                            $done = twitter($bot['twitter'], $tweet);
                            //Here we go!
                        }
                        if ($done) {
                            log_event($event);
                            //keep this one in mind to not twitter it again in future runs for the same job
                            $logged_events[] = $event;
                            //keep this one in mind not to twitter it again this run
                            $twittered++;
                            //one more has been twittered
                            $todo--;
                            //this one is done :)
                        } else {
                            echo "\t\tAborted: unable to send tweet.\n";
                        }
                    } else {
                        echo "\t\tAborted: unable to shorten url.\n";
                    }
                } else {
                    $todo--;
                    //we dont have to deal with this one anymore
                    echo "\n\tIssue {$initiative['issue_id']} has already been twittered for one of the the following job(s): " . implode(', ', $job_labels) . ". ({$initiative_id})\n";
                }
            }
        }
        if (!$new_event_found) {
            echo "\n*No untwittered issues for job '{$job_label}'.\n\n";
        } else {
            echo "\n*{$twittered} issue(s) have been twittered for job '{$job_label}'.\n\n";
        }
        if ($todo > 0) {
            echo "\n*There are " . $todo . " initiative(s) left to process.\n\n";
        }
        echo "\n\n";
        log_and_flush();
        //Log and flush the output buffer :)
    }
}
function cleanshortlink($atts)
{
    ob_start();
    shortlink($atts);
    $link_output = ob_get_contents();
    ob_end_clean();
    return $link_output;
}