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