PasteBot.php
From ChekMate Security Group
Contents |
[edit]
Introduction
This is a bot that connects to irc. It allows users within IRC to paste from irc to pastebin and also provides query functions against the pastebin database.
[edit]
Change History
[edit]
Future Version
- Add IRC commands to maintain channel list the bot should exist in.
- Add debug switch to turn local echo on or off.
- Add timestamp to be displayed when querying database.
- Add timer to automatically perform an "endpaste" after a specified time period.
- Add timer function that monitors the database and reports to a default channel any new additions to the pastebin.
[edit]
Maintainer
Shannon McNaught (smcnaught)
[edit]
License
pastebot.php - Pastebot is an interface between PasteBin and IRC. Copyright (C) 2006 Shannon McNaught This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License, version 2, as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
[edit]
Installation
[edit]
pastebot.php
<?php
require_once('Net/SmartIRC.php');
class pastebot
{
var $db_server = 'localhost';
var $db_user = 'username';
var $db_pwd = 'password';
var $db_name = 'pastebin';
var $pastebinurl = 'http://pastebin.chekmate.org/';
function paste(&$irc, &$data)
{
global $pastebin;
$msg = $data->message;
$from = $data->from;
$host = $data->host;
$ident = $data->ident;
$user = $data->nick;
$channel = $data->channel;
if (!$channel) {$channel = $user;}
echo "$user($channel) : $msg\n";
if ((!ereg("^paste",$msg)) and !$pastebin[$user]) {
return 0;
} else {
if (ereg("^paste",$msg)) {
$irc->message(SMARTIRC_TYPE_CHANNEL, $channel, "Request: $msg");
list(,$msg1) = explode(" ",$data->message,2);
$msg1 = mysql_real_escape_string($msg1);
} else {
$msg1 = $msg;
$msg1 = mysql_real_escape_string($msg1);
}
}
$now = time();
echo "paste-user = " . $pastebin[$user] . "\n";
if (!$pastebin[$user]) {
$irc->message(SMARTIRC_TYPE_CHANNEL, $channel,
"Please \"/msg pb endpaste\" to save your paste to the pastebin." );
$pastebin[$user] = "$now|$msg1" ;
} else {
$now = $pastebin[$user];
if ($msg1 == "endpaste") {
list(,$channel) = explode("|",$now,2);
$msg1 = $pastebin[$now];
$msg1 = rtrim($msg1,"\n");
$pastebin[$now] = NULL;
$pastebin[$user] = 0;
$tomorrow = mktime(0, 0, 0, date("m", $now) , date("d", $now)+1, date("Y", $now));
$today = mktime(0, 0, 0, date("m", $now) , date("d", $now), date("Y", $now));
$db_link = @mysql_pconnect($this->db_server, $this->db_user, $this->db_pwd) or exit('Could not connect: ' . mysql_error());
$db = @mysql_select_db($this->db_name, $db_link) or exit('Could not select database: ' . mysql_error());
$query="INSERT INTO pastebin (poster, posted, code, parent_pid, format, codefmt, codecss, domain, expires, expiry_flag) VALUES ('$user',$today,'$msg1','0','text',NULL,NULL,'$host',$tomorrow,'d')";
$select=mysql_query($query) or die(mysql_error());
$pid = mysql_insert_id();
$irc->message(SMARTIRC_TYPE_CHANNEL, $channel,
$user . " performed pastebin: " . $this->pastebinurl . $pid);
$irc->message(SMARTIRC_TYPE_CHANNEL, $user,
$user . " performed pastebin: " . $this->pastebinurl . $pid);
mysql_close ($db_link);
} else {
$pastebin[$now] = $pastebin[$now] . $msg1 . "\n";
}
}
}
function queryuser(&$irc, &$data)
{
$msg = $data->message;
$from = $data->from;
$host = $data->host;
$ident = $data->ident;
$user = $data->nick;
$channel = $data->channel;
$channel = $user;
echo "$user($channel) : $msg\n";
$irc->message(SMARTIRC_TYPE_CHANNEL, $channel, "Request: $msg");
list(,$msg1,$options) = explode(" ",$data->message,3);
$msg1 = ereg_replace("[^A-Za-z0-9]", "", $msg1);
$msg1 = mysql_real_escape_string($msg1);
if (!$msg1) { $msg1 = "BogusSearchLine"; }
if (($options < 10) AND (preg_match('/[0-9]+/',$options))) {
$limits = "LIMIT 0,$options";
} else {
$irc->message(SMARTIRC_TYPE_CHANNEL, $channel,
"Limit max is 10");
$limits = "LIMIT 0,10";
}
$db_link = @mysql_pconnect($this->db_server, $this->db_user, $this->db_pwd) or exit('Could not connect: ' . mysql_error());
$db = @mysql_select_db($this->db_name, $db_link) or exit('Could not select database: ' . mysql_error());
$query = "SELECT pid, poster FROM pastebin where poster like '%$msg1%' ORDER BY 'pid' DESC $limits";
$select =mysql_query($query) or die(mysql_error());
while($row=mysql_fetch_object($select)) {
$irc->message(SMARTIRC_TYPE_CHANNEL, $channel,
$this->pastebinurl . $row->pid . " by poster: " . $row->poster);
}
mysql_close ($db_link);
}
function query(&$irc, &$data)
{
$msg = $data->message;
$from = $data->from;
$host = $data->host;
$ident = $data->ident;
$user = $data->nick;
$channel = $data->channel;
$channel = $user;
echo "$user($channel) : $msg\n";
$irc->message(SMARTIRC_TYPE_CHANNEL, $channel, "Request: $msg");
list(,$msg1,$options) = explode(" ",$data->message,3);
$msg1 = ereg_replace("[^A-Za-z0-9]", "", $msg1);
$msg1 = mysql_real_escape_string($msg1);
if (!$msg1) { $msg1 = "BogusSearchLine"; }
if (($options < 10) AND (preg_match('/^[0-9]+$/',$options))) {
$limits = "LIMIT 0,$options";
} else {
$irc->message(SMARTIRC_TYPE_CHANNEL, $channel,
"Limit max is 10");
$limits = "LIMIT 0,10";
}
$db_link = @mysql_pconnect($this->db_server, $this->db_user, $this->db_pwd) or exit('Could not connect: ' . mysql_error());
$db = @mysql_select_db($this->db_name, $db_link) or exit('Could not select database: ' . mysql_error());
$query = "SELECT pid, poster FROM pastebin where code like '%$msg1%' ORDER BY 'pid' DESC $limits";
$select =mysql_query($query) or die(mysql_error());
while($row=mysql_fetch_object($select)) {
$irc->message(SMARTIRC_TYPE_CHANNEL, $channel,
$this->pastebinurl . $row->pid . " by poster: " . $row->poster);
}
mysql_close ($db_link);
}
function latest(&$irc, &$data)
{
$msg = $data->message;
$from = $data->from;
$host = $data->host;
$ident = $data->ident;
$user = $data->nick;
$channel = $data->channel;
$channel = $user;
echo "$user($channel) : $msg\n";
$irc->message(SMARTIRC_TYPE_CHANNEL, $channel, "Request: $msg");
list(,$options,) = explode(" ",$data->message,3);
if (($options < 10) AND (preg_match('/[0-9]+/',$options))) {
$limits = "LIMIT 0,$options";
} else {
$irc->message(SMARTIRC_TYPE_CHANNEL, $channel,
"Limit max is 10");
$limits = "LIMIT 0,10";
}
$db_link = @mysql_pconnect($this->db_server, $this->db_user, $this->db_pwd) or exit('Could not connect: ' . mysql_error());
$db = @mysql_select_db($this->db_name, $db_link) or exit('Could not select database: ' . mysql_error());
$query = "SELECT pid, poster FROM pastebin ORDER BY 'pid' DESC $limits";
$select =mysql_query($query) or die(mysql_error());
while($row=mysql_fetch_object($select)) {
$irc->message(SMARTIRC_TYPE_CHANNEL, $channel,
$this->pastebinurl . $row->pid . " by poster: " . $row->poster);
}
mysql_close ($db_link);
}
function show(&$irc, &$data)
{
$msg = $data->message;
$from = $data->from;
$host = $data->host;
$ident = $data->ident;
$user = $data->nick;
$channel = $data->channel;
$channel = $user;
echo "$user($channel) : $msg\n";
$irc->message(SMARTIRC_TYPE_CHANNEL, $channel, "Request: $msg");
list(,$msg1,) = explode(" ",$data->message,3);
$msg1 = ereg_replace("[^A-Za-z0-9]", "", $msg1);
$msg1 = mysql_real_escape_string($msg1);
if (!$msg1) { $msg1 = "BogusSearchLine"; }
$db_link = @mysql_pconnect($this->db_server, $this->db_user, $this->db_pwd) or exit('Could not connect: ' . mysql_error());
$db = @mysql_select_db($this->db_name, $db_link) or exit('Could not select database: ' . mysql_error());
$query = "SELECT pid, poster, code FROM pastebin where pid = '$msg1'";
$select =mysql_query($query) or die(mysql_error());
while($row=mysql_fetch_object($select)) {
$irc->message(SMARTIRC_TYPE_CHANNEL, $channel,
$this->pastebinurl . $row->pid . " by poster: " . $row->poster . " contains:");
$countline = 0;
foreach ((explode("\n", $row->code)) as $line) {
$countline++;
$formattedline = sprintf("%03d",$countline);
$irc->message(SMARTIRC_TYPE_CHANNEL, $channel,
"$formattedline : $line");
}
}
mysql_close ($db_link);
}
function showhelp(&$irc, &$data)
{
$msg = $data->message;
$from = $data->from;
$host = $data->host;
$ident = $data->ident;
$user = $data->nick;
$channel = $data->channel;
$channel = $user;
echo "$user($channel) : $msg\n";
$irc->message(SMARTIRC_TYPE_CHANNEL, $channel, "Request: $msg");
$helpmessage = "Pastebot (0.01a) Interface between IRC and PasteBin";
$irc->message(SMARTIRC_TYPE_CHANNEL, $channel, $helpmessage);
$helpmessage = "-----";
$irc->message(SMARTIRC_TYPE_CHANNEL, $channel, $helpmessage);
$helpmessage = "help - This help list.\n";
$irc->message(SMARTIRC_TYPE_CHANNEL, $channel, $helpmessage);
$helpmessage = "query text [limit] - Look for a keyword within the pastebin code\n";
$irc->message(SMARTIRC_TYPE_CHANNEL, $channel, $helpmessage);
$helpmessage = "latest [limit] - Display the latest pastebin entries\n";
$irc->message(SMARTIRC_TYPE_CHANNEL, $channel, $helpmessage);
$helpmessage = "queryuser text [limit] - Look for a keyword of a poster's name \n";
$irc->message(SMARTIRC_TYPE_CHANNEL, $channel, $helpmessage);
$helpmessage = "show ## - Show the contents of a pastebin (Warning: This could flood)";
$irc->message(SMARTIRC_TYPE_CHANNEL, $channel, $helpmessage);
$helpmessage = "paste #channel - Paste from irc to pastebin. Use \"endpaste\" to commit paste.";
$irc->message(SMARTIRC_TYPE_CHANNEL, $channel, $helpmessage);
$helpmessage = " Example: /msg pb paste #paste";
$irc->message(SMARTIRC_TYPE_CHANNEL, $channel, $helpmessage);
$helpmessage = " /msg pb This is a sample line.";
$irc->message(SMARTIRC_TYPE_CHANNEL, $channel, $helpmessage);
$helpmessage = " /msg pb This is another sample line.";
$irc->message(SMARTIRC_TYPE_CHANNEL, $channel, $helpmessage);
$helpmessage = " /msg pb This is the last sample line.";
$irc->message(SMARTIRC_TYPE_CHANNEL, $channel, $helpmessage);
$helpmessage = " /msg pb endpaste";
$irc->message(SMARTIRC_TYPE_CHANNEL, $channel, $helpmessage);
}
}
$host = "localhost";
$port = 6697;
$nick = "pb";
$chan = "#paste";
$bot = &new pastebot( );
$irc = &new Net_SmartIRC( );
$irc->setUseSockets( TRUE );
$irc->registerActionhandler( SMARTIRC_TYPE_CHANNEL|SMARTIRC_TYPE_QUERY|SMARTIRC_TYPE_NOTICE,
'^queryuser', $bot, 'queryuser' );
$irc->registerActionhandler( SMARTIRC_TYPE_CHANNEL|SMARTIRC_TYPE_QUERY|SMARTIRC_TYPE_NOTICE,
'^query', $bot, 'query' );
$irc->registerActionhandler( SMARTIRC_TYPE_CHANNEL|SMARTIRC_TYPE_QUERY|SMARTIRC_TYPE_NOTICE,
'^latest', $bot, 'latest' );
$irc->registerActionhandler( SMARTIRC_TYPE_CHANNEL|SMARTIRC_TYPE_QUERY|SMARTIRC_TYPE_NOTICE,
'^show', $bot, 'show' );
$irc->registerActionhandler( SMARTIRC_TYPE_CHANNEL|SMARTIRC_TYPE_QUERY|SMARTIRC_TYPE_NOTICE,
'^help', $bot, 'showhelp' );
$irc->registerActionhandler( SMARTIRC_TYPE_QUERY,
'^paste', $bot, 'paste' );
$irc->registerActionhandler( SMARTIRC_TYPE_QUERY,
'', $bot, 'paste' );
$irc->connect( $host, $port );
$irc->login( $nick, 'paste bot', 0, $nick );
$irc->join( array( $chan ) );
$irc->listen( );
$irc->disconnect( );
?>
[edit]
Command
Execute the bot by running:
php ./pastebot.php &




