299f151bdfbd8d4ff03d4a0f0f96e3beff3d9e8f
[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                         
59                 } else {
60                         $this->example_bot = $old; //We've saved out Bot reference in $old so we can simply use it again...
61                 }
62                 
63                 //OK  send something to the Server is easy...
64                 //How to recive something from the IRC Server?
65                 
66                 //I've copied the eggdrop way to do that - bind
67                 ModCMD::bind($this, BIND_PRIVMSG, "recive_privmsg");
68                 ModCMD::bind($this, BIND_QUIT, "recive_quit");
69         }
70         
71         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
72                 //please don't trigger any blocking functions here... that would cause an extreme lagg!
73         }
74         
75         public function unload($rehash = false) { //this function is triggered, when the Bot is unloaded... If it's just a rehash the return value of this method is passed to $old in the load method.
76                 if($rehash) {
77                         return $this->example_bot;
78                 } else {
79                         $this->uplink->delUser($this->example_bot, "Bye.");
80                 }
81         }
82         
83         public function recive_privmsg($user, $channel, $message) {
84                 //We've got a privmsg...
85                 $exp=explode(" ",$message);
86                 if($exp[0] == "hi") {
87                         $this->uplink->privmsg($this->example_bot, $channel, "Hallo ".$user->getNick()); // We send a message back - documentation is following
88                 }
89                 if($exp[0] == "-users") {
90                         $user_str = "";
91                         foreach($channel->getUsers() as $chan_user) {
92                                 $extra="";
93                                 $privs = $channel->getUserPrivs($chan_user);
94                                 if(($privs & P10_Channel::USERPRIV_VOICE)) $extra= '+'; 
95                                 if(($privs & P10_Channel::USERPRIV_OPED)) $extra= '@'; 
96                                 $user_str .= " " . $extra . $chan_user->getNick();
97                         }
98                         $this->uplink->notice($this->example_bot, $user, "User in ".$channel->getName().":".$user_str);
99                 }
100         }
101         
102         function recive_quit($user, $reason) {
103                 if($user === $this->example_bot) { //maybe we got killed???
104                         $this->load($this->uplink);
105                 }
106         }
107         
108 }
109
110 ?>