changed file headers & added AUTHORS file
[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                         $realname = "Thats an example Bot :)";
46                         $modes = "i"; //we don't need a leading + (that will be added automatically)
47                         
48                         $this->example_bot = $this->uplink->addUser($nick,$ident,$host,$realname,$uptime,$modes); //addUser($nick, $ident, $host, $ip, $modes, $realname)
49                         if(is_a($this->example_bot, "P10_User")) { // A new user was created :)
50                                 //ok  let's join a channel
51                                 $this->uplink->join($this->example_bot, "#test");
52                                 //now we want to say something...
53                                 //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)
54                         }
55                         
56                 } else {
57                         $this->example_bot = $old; //We've saved out Bot reference in $old so we can simply use it again...
58                 }
59                 
60                 //OK  send something to the Server is easy...
61                 //How to recive something from the IRC Server?
62                 
63                 //I've copied the eggdrop way to do that - bind
64                 ModCMD::bind($this, BIND_PRIVMSG, "recive_privmsg");
65                 ModCMD::bind($this, BIND_QUIT, "recive_quit");
66         }
67         
68         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
69                 //please don't trigger any blocking functions here... that would cause an extreme lagg!
70         }
71         
72         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.
73                 if($rehash) {
74                         return $this->example_bot;
75                 } else {
76                         $this->uplink->delUser($this->example_bot, "Bye.");
77                 }
78         }
79         
80         public function recive_privmsg($user, $channel, $message) {
81                 //We've got a privmsg...
82                 $exp=explode(" ",$message);
83                 if($exp[0] == "hi") {
84                         $this->uplink->privmsg($this->example_bot, $channel, "Hallo ".$user->getNick()); // We send a message back - documentation is following
85                 }
86                 if($exp[0] == "-users") {
87                         $user_str = "";
88                         foreach($channel->getUsers() as $chan_user) {
89                                 $extra="";
90                                 $privs = $channel->getUserPrivs($chan_user);
91                                 if(($privs & P10_Channel::USERPRIV_VOICE)) $extra= '+'; 
92                                 if(($privs & P10_Channel::USERPRIV_OPED)) $extra= '@'; 
93                                 $user_str .= " " . $extra . $chan_user->getNick();
94                         }
95                         $this->uplink->notice($this->example_bot, $user, "User in ".$channel->getName().":".$user_str);
96                 }
97         }
98         
99         function recive_quit($user, $reason) {
100                 if($user === $this->example_bot) { //maybe we got killed???
101                         $this->load($this->uplink);
102                 }
103         }
104         
105 }
106
107 ?>