b9f685d95354b25e195139c084841775e6ffdb88
[PHP-P10.git] / Bots / ExampleBot.class.php
1 <?php
2 /********************************* PHP-P10 ******************************
3  *    P10 uplink class by pk910   (c)2011 pk910                         *
4  ************************************************************************
5  *                          Version 2 (OOP)                             *
6  *                                                                      *
7  * PHP-P10 is free software; you can redistribute it and/or modify      *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation; either version 2 of the License, or    *
10  * (at your option) any later version.                                  *
11  *                                                                      *
12  * This program is distributed in the hope that it will be useful,      *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
15  * GNU General Public License for more details.                         *
16  *                                                                      *
17  * You should have received a copy of the GNU General Public License    *
18  * along with PHP-P10; if not, write to the Free Software Foundation,   *
19  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.       *
20  *                                                                      *
21  ************************************************************************
22  * 
23  *  Bots/ExampleBot.class.php
24  *
25  * a simple example bot...
26  *
27  */
28
29 class {$_NAME} extends Bot {  // {$_NAME} will be replaced by our script later ;)
30         private $uplink;
31         private $example_bot;
32  
33         public function load($uplink, $old = false) { //load is called when the module gets included
34                 $this->uplink = $uplink; //Reference to the P10 Uplink
35                 
36                 //$old is only set, if the module is rehashed. It contains the return value of unload();
37                 
38                 if(!$old) {
39                         
40                         //Here you can define startup actions
41                         //We'll add an user to the Server
42                         // newuser($localid,$nick,$ident,$host,$realname,$uptime,$modes)   returns an array with the result
43                         // $localid  is just an id you can set (whatever you want) - maybe you'll use it to identify the bot/user later?
44                         
45                         $nick = "ExampleBot"; // Please note: If this user already exists on another Server it will be killed!
46                         $ident = "Example";
47                         $host = "Example.Bot";
48                         $realname = "Thats an example Bot :)";
49                         $modes = "i"; //we don't need a leading + (that will be added automatically)
50                         
51                         $this->example_bot = $this->uplink->addUser($nick,$ident,$host,$realname,$uptime,$modes); //addUser($nick, $ident, $host, $ip, $modes, $realname)
52                         if(is_a($this->example_bot, "P10_User")) { // A new user was created :)
53                                 //ok  let's join a channel
54                                 $this->uplink->join($this->example_bot, "#test");
55                                 //now we want to say something...
56                                 //but note: thats the startup procedure! the p10 server is not connected to an uplink, yet - so noone would recive our message (only the other bots on this server)
57                         }   
58                 } else {
59                         $this->example_bot = $old; //We've saved out Bot reference in $old so we can simply use it again...
60                 }
61                 
62                 //OK  send something to the Server is easy...
63                 //How to recive something from the IRC Server?
64                 
65                 //I've copied the eggdrop way to do that - bind
66                 ModCMD::bind($this, BIND_PRIVMSG, "recive_privmsg");
67                 ModCMD::bind($this, BIND_QUIT, "recive_quit");
68         }
69         
70         public function loop() { //this function is triggered as often as possible (at least one time per second!). Maybe you need it to do timed events
71                 //please don't trigger any blocking functions here... that would cause an extreme lagg!
72         }
73         
74         public function recive_privmsg($user, $channel, $message) {
75                 //We've got a privmsg...
76                 $exp=explode(" ",$message);
77                 if($exp[0] == "hi") {
78                         $this->uplink->privmsg($this->example_bot, $channel, "Hallo ".$user->getNick()); // We send a message back - documentation is following
79                 }
80                 if($exp[0] == "-users") {
81                         $user_str = "";
82                         foreach($channel->getUsers() as $chan_user) {
83                                 $extra="";
84                                 $privs = $channel->getUserPrivs($chan_user);
85                                 if(($privs & P10_Channel::USERPRIV_VOICE)) $extra= '+'; 
86                                 if(($privs & P10_Channel::USERPRIV_OPED)) $extra= '@'; 
87                                 $user_str .= " " . $extra . $chan_user->getNick();
88                         }
89                         $this->uplink->notice($this->example_bot, $user, "User in ".$channel->getName().":".$user_str);
90                 }
91         }
92         
93         function recive_quit($user, $reason) {
94                 if($user === $this->example_bot) { //maybe we got killed???
95                         $this->load($this->uplink);
96                 }
97         }
98         
99 }
100
101 ?>