Main Menu
  • Welcome to The RPG Maker Resource Kit.

Jbot PHP coded IRC bot I've been working on.

Started by Jalkson, December 16, 2007, 09:44:12 PM

0 Members and 1 Guest are viewing this topic.

Jalkson

Very basic, and doesn't do anything yet, apart from connecting, and joining a channel. :P


<?php/*Jbot Created by JalksonSpecial thanks to:Jim @ combined-minds.netahref @ rmrk.netwingard @ fireandraingfx.co.nr*/      //disable the time limit    set_time_limit(0);   //allows you to close the browser, yet doesn't close the bot    ignore_user_abort(TRUE);   //Define your config variables    $config = parse_ini_file("conf.ini");    $serv = $config['serv'];    $port = $config['port'];    $name = $config['name'];    $nick = $config['nick'];    $host = $config['host'];    $chan = $config['chan'];    $pass = $config['pass'];    $p = $config['prefix'];   //Define and establishing a connection    $server = fsockopen($serv,$port);   //Telling the server your name/nick    fputs($server,"USER ".$name. "\n");    fputs($server,"NICK ".$nick. "\n");		   //Identifying your name   usleep(10000);    fputs($server,"MSG nickserv IDENTIFY ".$pass. "\n");   					   //Joining chosen channel    fputs($server,"JOIN ".$chan. "\n");      //Starting the loop **    while(1) {	   //Setting the allotted data usage.    while($data = fgets($server, 128)) {	   	   echo nl2br($data);	   flush();	   //Explode the data    $ex = explode(' ', $data);   //Replying to PING    if($ex[0] == "PING"){       fputs($server,"PONG ".$ex[1]."\n");    }	   //Stores Hostname, and Username in variables    preg_match('/^:(.*?)!(.*?)$/i', $ex[0], $matches);     $hostname = "!".$matches[2];     $username = $matches[1];   	   //Setting the command line    $command = str_replace(array(chr(10), chr(13)), '', $ex[3]);	   /*Bot commands From here down*/        #fputs($socket, "MODE ".$ex[2]." +v ".$ex[4]);    //Autogreet   	$channel = str_replace(array(chr(10), chr(13)), '', $ex[2]);	$channel = substr($channel, 1);	    if($ex[1] == "JOIN ") {	  fputs($server,"PRIVMSG ".$ex[2]." :Hello ".$username.", welcome to ".$channel."! \n");      }	     //kill command    if($username == "Jalkson" || "ahref") {	 if($command == ":".$p."die") {	 fputs($server,"QUIT :<3. \n");     die();	 } else {	 if($command == ":".$p."die") {     fputs($server,"PRIVMSG ".$ex[2]." :Sorry ".$username.", you lack access. \n");       }      }	      }		   //time command    $time = date("l, F j, Y g:i a");	    if($command == ":".$p."time") {	   fputs($server,"PRIVMSG ".$ex[2]." :".$time."\n");	   }	    /*join command    if($command == ":".$p."join") {	   fputs($server,"JOIN ".$chan. "\n");	   }      */	   	     //join specific channel    if ($ex[3] == ":".$p."join"){		if(isset($ex[4])){		fputs($server, "JOIN :".$ex[4]);	    }    }	   //Part command    if ($ex[3] == ":".$p."part"){		 if(isset($ex[4])){		fputs($server, "PART :".$ex[4]);	    }    }	   //Kick command    if($username == "Jalkson" || "ahref") {	 if($ex[3] == ":".$p."kick") {	 if(isset($ex[4])){	  fputs($server, "KICK #rmrevolution :".$ex[4]."\n");	  }     }    }     //** This last } ends the loop.  Keep everything else above it      }    }	?>




[config]
host = RmRevolution.com
serv = irc.acidchat.net
port = 6667
chan = #rmrevolution
name = Jbot RmRevolution.com Jbot :Jbot
nick = Jbot
pass =
prefix = @



:sauced.acidchat.net NOTICE AUTH :*** Looking up your hostname...
:sauced.acidchat.net NOTICE AUTH :*** Found your hostname (cached)
PING :D9A585C

Notice: Undefined offset: 3 in c:\program files\easyphp1-8\www\jbot.php on line 50
:sauced.acidchat.net 451 PRIVMSG :You have not registered
:sauced.acidchat.net 451 JOIN :You have not registered
:sauced.acidchat.net 001 Jbot :Welcome to the Acidchat IRC Network Jbot!Jbot@adsl-67-122-209-225.dsl.snfc21.pacbell.net
:sauced.acidchat.net 002 Jbot :Your host is sauced.acidchat.net, running version Unreal3.2.6
:sauced.acidchat.net 003 Jbot :This server was created Sun Jun 17 2007 at 18:42:35 MDT
:sauced.acidchat.net 004 Jbot sauced.acidchat.net Unreal3.2.6 iowghraAsORTVSxNCWqBzvdHtGpZ lvhopsmntikrRcaqOALQbSeIKVfMCuzNTGj


Notice: Undefined offset: 3 in c:\program files\easyphp1-8\www\jbot.php on line 50
:sauced.acidchat.net 005 Jbot CMDS=KNOCK,MAP,DCCALLOW,USERIP NAMESX SAFELIST HCN MAXCHANNELS=10 CHANLIMIT=#:10 MAXLIST=b:60,e:60,I:60 NICKLEN=30 CHANNELLEN=32 TOPICLEN=307 KICKLEN=307 AWAYLEN=307 MAXTARGETS=20 :are supported by this server
:sauced.acidchat.net 005 Jbot WALLCHOPS WATCH=128 SILENCE=15 MODES=12 CHANTYPES=# PREFIX=(qaohv)~&@%+ CHANMODES=beI,kfL,lj,psmntirRcOAQKVCuzNSMTG NETWORK=Acidchat CASEMAPPING=ascii EXTBAN=~,cqnr ELIST=MNUCT STATUSMSG=~&@%+ EXCEPTS :are supported by this server

Notice: Undefined offset: 3 in c:\program files\easyphp1-8\www\jbot.php on line 50
:sauced.acidchat.net 005 Jbot INVEX :are supported by this server
:sauced.acidchat.net 251 Jbot :There are 19 users and 65 invisible on 3 servers
:sauced.acidchat.net 252 Jbot 18 :operator(s) online
:sauced.acidchat.net 254 Jbot 23 :channels formed
:sauced.acidchat.net 255 Jbot :I have 35 clients and 1 servers
:sauced.acidchat.net 265 Jbot :Current Local Users: 35 Max: 223
:sauced.acidchat.net 266 Jbot :Current Global Users: 84 Max: 473
:sauced.acidchat.net 375 Jbot :- sauced.acidchat.net Message of the Day -
:sauced.acidchat.net 372 Jbot :- 17/6/2007 18:44
:sauced.acidchat.net 372 Jbot :-
:sauced.acidchat.net 372 Jbot :- _ _ _ ____ _ _
:sauced.acidchat.net 372 Jbot :- / \ ___(_) __| |/ ___| |__ __ _| |_
:sauced.acidchat.net 372 Jbot :- / _ \ / __| |/ _` | | | '_ \ / _` | __|
:sauced.acidchat.net 372 Jbot :- / ___ \ (__| | (_| | |___| | | | (_| | |_
:sauced.acidchat.net 372 Jbot :- /_/ \_\___|_|\__,_|\____|_| |_|\__,_|\__|
:sauced.acidchat.net 372 Jbot :-
:sauced.acidchat.net 372 Jbot :- Welcome to AcidChat IRC network
:sauced.acidchat.net 372 Jbot :- -------------------------------------------
:sauced.acidchat.net 372 Jbot :- you are on sauced.acidchat.net
:sauced.acidchat.net 372 Jbot :- talk to jent if you have any problems
:sauced.acidchat.net 372 Jbot :- -------------------------------------------
:sauced.acidchat.net 372 Jbot :-
:sauced.acidchat.net 372 Jbot :- Do not cause general problems.
:sauced.acidchat.net 372 Jbot :- Do not attend in any illegal activities.
:sauced.acidchat.net 372 Jbot :- Do not troll.
:sauced.acidchat.net 372 Jbot :- Always listen to the admin/opers.
:sauced.acidchat.net 372 Jbot :-
:sauced.acidchat.net 372 Jbot :-
:sauced.acidchat.net 372 Jbot :- Also respect others, if you dont like a
:sauced.acidchat.net 372 Jbot :- particular group or persons do not take
:sauced.acidchat.net 372 Jbot :- it upon yourself to "enlighten" them about
:sauced.acidchat.net 372 Jbot :- their faults and how your ways are better,
:sauced.acidchat.net 372 Jbot :- people are different, learn some social
:sauced.acidchat.net 372 Jbot :- skills and deal with it.
:sauced.acidchat.net 372 Jbot :-
:sauced.acidchat.net 372 Jbot :- ==================
:sauced.acidchat.net 372 Jbot :- Terms of use:
:sauced.acidchat.net 372 Jbot :-
:sauced.acidchat.net 372 Jbot :- Acidchat does not hold liability of content
:sauced.acidchat.net 372 Jbot :- on this network, it is your responsibility
:sauced.acidchat.net 372 Jbot :- to ignore any offensive topics, channels, etc.
:sauced.acidchat.net 372 Jbot :-
:sauced.acidchat.net 372 Jbot :- What users do in private is no concern of Acidchat.
:sauced.acidchat.net 372 Jbot :-
:sauced.acidchat.net 372 Jbot :- Warez, spambots, floodbots, trojan bots, etc
:sauced.acidchat.net 372 Jbot :- are strictly forbidden on this network.
:sauced.acidchat.net 372 Jbot :-
:sauced.acidchat.net 372 Jbot :- Chatrooms affiliated with terrorism, child
:sauced.acidchat.net 372 Jbot :- pornography or hate related subjects are
:sauced.acidchat.net 372 Jbot :- forbidden and will be locked out and the
:sauced.acidchat.net 372 Jbot :- owners permanently G:lined, Those in question
:sauced.acidchat.net 372 Jbot :- will have their information recorded for
:sauced.acidchat.net 372 Jbot :- legal reasons.
:sauced.acidchat.net 372 Jbot :-
:sauced.acidchat.net 372 Jbot :- Unauthorized web-based clients like cgi:IRC,
:sauced.acidchat.net 372 Jbot :- which allow abuse will be banned, due to CGI:IRC
:sauced.acidchat.net 372 Jbot :- using the same IP or hostname as the machine it
:sauced.acidchat.net 372 Jbot :- runs on for all clients. However, this doesnt
:sauced.acidchat.net 372 Jbot :- apply to properly set up local cgi:irc clients
:sauced.acidchat.net 372 Jbot :- (such as those hosted by acidchat.net and by proper,
:sauced.acidchat.net 372 Jbot :- those that will show the ip mask of each user.)
:sauced.acidchat.net 372 Jbot :-
:sauced.acidchat.net 372 Jbot :- Channels that share roms or mp3 files covered under
:sauced.acidchat.net 372 Jbot :- the RIAA will be shut down at once. As in the 3rd
:sauced.acidchat.net 372 Jbot :- line of the terms states: what users do in their
:sauced.acidchat.net 372 Jbot :- own personal messages is of no concern to Acidchat,
:sauced.acidchat.net 372 Jbot :- and are responsible for their own actions. ROMS
:sauced.acidchat.net 372 Jbot :- are illegal in most cases, unless they're homebrew.
:sauced.acidchat.net 372 Jbot :-
:sauced.acidchat.net 372 Jbot :- Spamming, harassing, berating others, and joinflooding
:sauced.acidchat.net 372 Jbot :- channels are against the rules.
:sauced.acidchat.net 372 Jbot :-
:sauced.acidchat.net 372 Jbot :- We have the right to refuse service to anyone.
:sauced.acidchat.net 372 Jbot :-
:sauced.acidchat.net 372 Jbot :- IRC opers are authorized to use action against mass spammers/advertisers
:sauced.acidchat.net 372 Jbot :-
:sauced.acidchat.net 372 Jbot :- Follow the terms and rules and you'll be fine.
:sauced.acidchat.net 372 Jbot :- ====================================================
:sauced.acidchat.net 376 Jbot :End of /MOTD command.
:Jbot MODE Jbot :+iwx
PING :sauced.acidchat.net

Notice: Undefined offset: 3 in c:\program files\easyphp1-8\www\jbot.php on line 50
PING :sauced.acidchat.net

Notice: Undefined offset: 3 in c:\program files\easyphp1-8\www\jbot.php on line 50
PING :sauced.acidchat.net

Notice: Undefined offset: 3 in c:\program files\easyphp1-8\www\jbot.php on line 50
PING :sauced.acidchat.net

Notice: Undefined offset: 3 in c:\program files\easyphp1-8\www\jbot.php on line 50
PING :sauced.acidchat.net

Notice: Undefined offset: 3 in c:\program files\easyphp1-8\www\jbot.php on line 50
:Jalkson!hithar@Acidchat-3C27DBE.dsl.pltn13.pacbell.net PRIVMSG jbot :@die

ahref

mine


:sauced.acidchat.net NOTICE AUTH :*** Looking up your hostname...
:sauced.acidchat.net NOTICE AUTH :*** Found your hostname (cached)
:sauced.acidchat.net 461  USER :Not enough parameters
PING :EC5D2DEE
:sauced.acidchat.net 451 JOIN :You have not registered

Kokowam

I want to make a bot... or learn PHP... or LEARN WINMOO *AHEM*

ahref

i have explained why you have not got the rest of the guide. I also have llamas to contend with

Jalkson

#4
New code, sort of.  It's just more comments, and easier to understand.  As well as some small stuff added.

Code (Jbot.php) Select

<?php/*Jbot Created by JalksonSpecial thanks to Jim @ combined-minds.net*/      //disable the time limit    set_time_limit(0);   //Define your config variables    $config = parse_ini_file("conf.ini");    $serv = $config['serv'];    $port = $config['port'];    $name = $config['name'];    $nick = $config['nick'];    $host = $config['host'];    $chan = $config['chan'];    $pass = $config['pass'];    $p = $config['prefix'];   //Define and establishing a connection    $server = fsockopen($serv,$port);   //Telling the server your name/nick    fputs($server,"USER ".$name. "\n");    fputs($server,"NICK ".$nick. "\n");		   //Identifying your name    fputs($server,"IDENTIFY ".$pass."\n");   			usleep(500);			   //Joining chosen channel    fputs($server,"JOIN ".$chan. "\n");      //Starting the loop **    while(1) {	   //Setting the allotted data usage.    while($data = fgets($server, 128)) {	   	   echo nl2br($data);	   flush();	   //Explode the data    $ex = explode(' ', $data);   //Replying to PING    if($ex[0] == "PING"){       fputs($server,"PONG ".$ex[1]."\n");    }	   //Stores Hostname, and Username in variables    preg_match('/^:(.*?)!(.*?)$/i', $ex[0], $matches);     $hostname = "!".$matches[2];     $username = $matches[1];   	   //Setting the command line    $command = str_replace(array(chr(10), chr(13)), '', $ex[3]);	   /*Bot commands From here down*/    $today = date("l, F j, Y, g:i a");	   //die command    if($command == ":".$p."die") {	   fputs($server,"QUIT ".$ex[2]." Session Terminated. \n");           die();	   }	      //join command    if($command == ":".$p."join") {	   fputs($server,"JOIN ".$chan. "\n");	   }        //Attempting to start on some sort of login system     	 	  }   //** This last } ends the loop.  Keep everything else above it    }	 ?>



Code (conf.ini) Select

[config]
host = RmRevolution.com
serv = irc.acidchat.net
port = 6667
chan = #rmrevolution
name = Jbot RmRevolution.com Jbot :Jbot
nick = Jbot
pass =
prefix = @

ahref

Quote//Explode the data
    $ex = explode(' ', $data);

change this to
$ex = explode('\n', $data);

and it will explode lines instead of spaces but thats all for now :D

Kokowam


<?php// Prevent PHP from stopping the script after 30 secset_time_limit(0);// Opening the socket to the Spicy Lemons network$socket = fsockopen("spicylemons.acidchat.net", 6667);// Send auth infofputs($socket,"USER MooBot rmrk.net MB:Moo Bot\n");fputs($socket,"NICK MooBot\n");// Join channelfputs($socket,"JOIN #crankeye\n");// Force an endless whilewhile(1) {    // Continue the rest of the script here		while($data = fgets($socket, 128)) {		echo nl2br($data);        flush();    }}?>


;-; Halp.

Jalkson

#7
Try using that, and the bot should connect to acidchat just fine.  Once it's connected, pm the bot the join command (/msg moobot !join) and it should join crankeye without issues. 

I'm still working on getting the bot to recognize what the user says, so I can make a join any channel command. (!join #channelname).  So far, I fail. :P

I'm also working on an admin/access system, which is actually coming along decently, but it probably won't work for another few days at least.  Depending on how much time I get to work on it.


<?php// Prevent PHP from stopping the script after 30 secset_time_limit(0);// Opening the socket to the Rizon network$socket = fsockopen("irc.acidchat.net", 6667);// Send auth infofputs($socket,"USER MooBot rmrk.net MB:Moo Bot\n");fputs($socket,"NICK MooBot\n");// Join channelfputs($socket,"JOIN #crankeye \n");// Force an endless whilewhile(1) {    // Continue the rest of the script here    while($data = fgets($socket, 128)) {        echo nl2br($data);        flush();        // Separate all data        $ex = explode(' ', $data);                // Send PONG back to the server        if($ex[0] == "PING"){            fputs($socket, "PONG ".$ex[1]."\n");        }        // Say something in the channel        $command = str_replace(array(chr(10), chr(13)), '', $ex[3]);        if ($command == ":!join") {            fputs($socket,"JOIN #crankeye \n");        }        }}?>


:)

now is this being made with a tutorial?

seems interesting 0_0
Watch out for: HaloOfTheSun

Jalkson

#9
Yea, it's from a tutorial. :]

www.combined-minds.net

It's actually a web coding site.  It's fairly new I think, and there isn't a bunch of info on the site/forums.  Though the people who are there are really helpful. =]

They have tutorials on HTML, PHP, JAVA, SEO, and photoshop.  Though, there are very few of them. 

/plug.

Anyways, I've got a code working in which you can type !join #channelname, and it will join whichever channel you say. =]

Code (snippet) Select

if ($ex[3] == ":".$p."join"){
if(isset($ex[4])){
fputs($server, "JOIN :".$ex[4]);
    }
    }


Just change the part that says == ":".$p."join" to whatever you'd like to use as the command, such as ":!join" etc.  Also, change the $server, to $socket or whatever your connection variable is.

@ahref, when I try doing as you said with the explode, it won't connect to IRC.  Not really sure what the issue could be, it stops at about the same point as it stopped for you before, minus the parameter error.

ahref

#10
ill bash it again a little later whilst im here though
echo nl2br($data);

can be changed to

echo ($data);

the n 2 br command just makes <br />s appear everywhere unless the bot is opened in a web browser or something anyway ill leave it as ive changed it in my version but its up to you... i confused myself

before you use $ex
  • add in an echo command echoing that varible and we can see what they contain :D:

    eg.

    echo ("MOOO: ".$ex[0]."\n");
    if($ex[0] == "PING"){


    adding in something obscure before the varible itself will help you recognise

Jalkson

#11
Thanks, fixed.

Updated first post with newest code.

ahref

#12
Just had a though:

instead of writing
fputs($server,"PRIVMSG ".$ex[2]." :Hello ".$username.", welcome to ".$channel."! \n");

when we want to send a message we could use something like

sendMessage(where,what);


eg sendMessage("#dino","moo");

i've wrote the above function so we can test it:

function sendMessage($where,$what) {
    fputs($server,"PRIVMSG ".$where." :".$what." \n");
}



when using it remember if its pure human text like this surrond it in "s to indicate its a string and that php doesnt need to do anything with it :D

UPDATE:
this might work:

    $channel = str_replace(array(chr(10), chr(13)), '', $ex[3]);
$channel = substr($channel, 1);
    if($ex[2] == "JOIN ") {
  fputs($server,"PRIVMSG ".$ex[3]." :Hello ".$username.", welcome to ".$channel."! \n");
      }

ahref

double post but meh its a breakthrough

line 85 change to:

if(($username == "Jalkson") || ($username == "ahref")) {


irock can no longer end the bot :D

and just take the whole autogreet block out and replace it with this


   //Autogreet
    if($ex[1] == "JOIN") {
$channel = str_replace(array(chr(10), chr(13), chr(58)), '', $ex[2]);
if($username != $nick) {
  fputs($server,"PRIVMSG ".$channel." :Hello ".$username.", welcome to ".$channel."! \n");
  }
      }

* ahref dances

Jalkson

Newest code.


<?php/*Jbot Created by JalksonSpecial thanks to:Jim @ combined-minds.netahref @ rmrk.netwingard @ fireandraingfx.co.nr*/      //disable the time limit    set_time_limit(0);   //allows you to close the browser, yet doesn't close the bot    ignore_user_abort(TRUE);   //Define your config variables    $config = parse_ini_file("conf.ini");    $serv = $config['serv'];    $port = $config['port'];    $name = $config['name'];    $nick = $config['nick'];    $host = $config['host'];    $chan = $config['chan'];    $pass = $config['pass'];    $p = $config['prefix'];   //Define and establishing a connection    $server = fsockopen($serv,$port);   //Telling the server your name/nick    fputs($server,"USER ".$name. "\n");    fputs($server,"NICK ".$nick. "\n");		   //Identifying your name   usleep(10000);    fputs($server,"MSG nickserv IDENTIFY ".$pass. "\n");   					   //Joining chosen channel    fputs($server,"JOIN ".$chan. "\n");      //Starting the loop **    while(1) {	   //Setting the allotted data usage.    while($data = fgets($server, 128)) {	   	   echo nl2br($data);	   flush();	   //Explode the data    $ex = explode(' ', $data);   //Replying to PING    if($ex[0] == "PING"){       fputs($server,"PONG ".$ex[1]."\n");    }	   //Stores Hostname, and Username in variables    preg_match('/^:(.*?)!(.*?)$/i', $ex[0], $matches);     $hostname = "!".$matches[2];     $username = $matches[1];   	   //Setting the command line    $command = str_replace(array(chr(10), chr(13)), '', $ex[3]);	   /*Bot commands From here down*/   //Autogreet    if($ex[1] == "JOIN") {	  $channel = str_replace(array(chr(10), chr(13), chr(58)), '', $ex[2]); 	 if($username != $nick) {	  fputs($server,"PRIVMSG ".$channel." :Hello ".$username.", welcome to ".$channel."! \n");	  fputs($server,"MODE ".$channel." +v ".$username." \n");	  }      }		     //kill command    if(($username == "Jalkson") || ($username == "ahref")) {	 if($command == ":".$p."die") {	 fputs($server,"QUIT : Message. \n");     die();	 } else {	 if($command == ":".$p."die") {     fputs($server,"PRIVMSG ".$ex[2]." :Sorry ".$username.", you lack access. \n");       }      }	      }	   //time command    $time = date("l, F j, Y g:i a");	    if($command == ":".$p."time") {	   fputs($server,"PRIVMSG ".$ex[2]." :".$time."\n");	   }	    /*join command    if($command == ":".$p."join") {	   fputs($server,"JOIN ".$chan. "\n");	   }      */	   	     //join specific channel    if ($ex[3] == ":".$p."join"){		if(isset($ex[4])){		fputs($server, "JOIN :".$ex[4]);	    }    }	   //Part command    if ($ex[3] == ":".$p."part"){		 if(isset($ex[4])){		fputs($server, "PART :".$ex[4]);	    }    }	   //Kick command    if(($username == "Jalkson") || ($username == "ahref")) {	 if($ex[3] == ":".$p."kick") {	 if(isset($ex[4])){	  fputs($server, "KICK #rmrevolution :".$ex[4]."\n");	  }     }    }     //** This last } ends the loop.  Keep everything else above it      }    }	?>


Maz

This is interesting :) I once wrote an IRC bot with C, using jumb tables and callbacks for executing specific actions as responce for specific commands. Since I am absolutely no expert with php, I am anxious to see how you handle the command parsing with php. One loooong switch - case sounds like horrible option to me :D

mmm.. I am also interested to see how you do escape inputs coming from the socket..

ahref