RedChu

iRCbot Plug-in Writing Guide

May 18th, 2011
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.98 KB | None | 0 0
  1. ***************************************
  2. * iRCbot Plug-in Writing Guide *
  3. * *
  4. * last updated *
  5. * may 18 2011 *
  6. ***************************************
  7.  
  8.  
  9.  
  10. ==============================
  11. VERSION HISTORY
  12. ==============================
  13.  
  14. v 1 - may 18 2011 - may 18 2011
  15. ----------- Finished writing the guide. It's all here, everything I
  16. could think of to include. All variables currently
  17. defined, arrays, functions, etc... it should help those
  18. who wish to create a plug-in for iRCbot to do so.
  19.  
  20. I even included templates to help you get started.
  21.  
  22. v 1.0.1 - may 18 2011
  23. ----------- Fixed some minor errors.
  24.  
  25.  
  26.  
  27.  
  28. ==============================
  29. Table of Contents
  30. ==============================
  31.  
  32. 1. Introduction
  33. 1.1 How iRCbot works
  34. 1.2 Commands and parameters
  35. 2. Core features
  36. 2.1 Functions
  37. 2.2 Variables
  38. 2.3 Arrays
  39. 3. Making a plug-in
  40. 3.1 Help parameter
  41. 3.2 Requiring a certain level
  42. 3.3 Relaying errors
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52. --------------------------------------------------------------------------------------------
  53. 1. Introduction
  54. --------------------------------------------------------------------------------------------
  55.  
  56. 1.1 How iRCbot Works
  57.  
  58. iRCbot is a modular IRC bot written in PHP. iRCbot uses plug-ins
  59. which extend upon it's core functionality. This guide is meant to
  60. explain how to create plug-ins for iRCbot, but first we must know
  61. how iRCbot works.
  62.  
  63. It is assumed that you have an understanding of PHP as all
  64. plug-ins must be written in it. It is not required that you be an
  65. expert with it, as long as you have a decent understanding of
  66. how to use it for what you want to achieve with it in your
  67. plug-in.
  68.  
  69. iRCbot is essentially a one piece program, meaning that it is
  70. perfectly capable of running on its own. Plug-ins extend the
  71. functionality of iRCbot by providing it with more capabilities
  72. than just idling in a channel and stalking the users.
  73.  
  74. Basically, iRCbot alone can only connect to the server, join the
  75. default channels and then idle. All other functionality is provided
  76. through plug-ins which are executed by commands that users
  77. send to iRCbot through a PRIVMSG, which if you're unfamiliar
  78. with IRC, is either a query to iRCbot or a message sent in a
  79. channel which iRCbot is present.
  80.  
  81. 1.2 Commands and parameters
  82.  
  83. All commands are recognized by a ~ (tilde). The command must
  84. be started at the beginning of a PRIVMSG for it to be recognized
  85. by iRCbot.
  86.  
  87. All commands use the file name of the plug-in in the plug-in
  88. directory where iRCbot is stored. You cannot change the name of
  89. a command unless the plug-in file's name is changed as well,
  90. at least at this time.
  91.  
  92. Sample command:
  93.  
  94. ~test donkey kong
  95.  
  96. In the above sample, we have the command (~test) and two
  97. parameters (donkey, kong).
  98.  
  99. Parameters do not always have to be required for a command.
  100. If a plug-in requires two parameters, then two parameters should
  101. be returned, ignoring others unless otherwise specified.
  102.  
  103. Some plug-ins, such as say, use an infinite amount of parameters,
  104. not one of which are required but the first.
  105.  
  106. It's a bit difficult to explain without seeing an already made plug-in,
  107. but we'll get to that point later in this guide.
  108.  
  109. So far, you know that a command is what executes a plug-in and
  110. that they share the same name, and that plug-ins may have up to
  111. any number of parameters depending on the circumstances.
  112.  
  113. Let's say you want to add a user to a list... let's use the adduser
  114. plug-in as a reference:
  115.  
  116. You want to add a user to the list along with a numeric value for
  117. the user's level. You create a plug-in that will be executed by the
  118. command ~adduser. You need to get the nick of the user as well
  119. as the level you want to add the user as having, so you require
  120. two parameters to ~adduser:
  121.  
  122. ~adduser <nick> <level>
  123.  
  124. Parameter <nick> will contain the nickname of the user, and
  125. parameter <level> will contain the numeric value for the level.
  126.  
  127. Now that you know how plug-ins are executed, let's learn how
  128. to make the most of them!
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138. --------------------------------------------------------------------------------------------
  139. 2. Core features
  140. --------------------------------------------------------------------------------------------
  141.  
  142. 2.1 Functions
  143.  
  144. iRCbot contains a number of ready made functions that make
  145. the life of an iRCbot plug-in creator easier. Gone are the worries
  146. of meddling with the IRC protocol, as difficult and daunting a
  147. task that it isn't.
  148.  
  149. The functions that you will likely be using the most is the privmsg()
  150. function, which is what iRCbot uses to send messages to a
  151. channel or nick (whichever works).
  152.  
  153. All functions must be used in this fashion:
  154.  
  155. $this->functionnamehere();
  156.  
  157. The reason for this is because of the way iRCbot is designed. As
  158. a plug-in, you are directly adding to iRCbot's class. iRCbot is
  159. in a continuous loop state until a command is thrown its way,
  160. which then if the plug-in exists, it executes the plug-in until the
  161. end and then it goes back into its loop state.
  162.  
  163. $this-> signifies that it is part of the class. iRCbot is an object
  164. so to speak, and since the functions are defined within
  165. the object, you have to signify that you want to use them in
  166. this object... I might be wrong as I'm fairly new to object
  167. oriented programming, but that's how I would explain it
  168. and that's how I did!
  169.  
  170. Anyway, here's a list of the currently available functions and
  171. how to use them:
  172.  
  173. privmsg($channel, $message)
  174. Sends $message to $channel. $channel can be either a
  175. channel name or a nick.
  176.  
  177. action($channel, $message)
  178. Sends an action command containing $message to
  179. $channel. An action command is essentially a /me
  180. command.
  181.  
  182. nick($name)
  183. Changes the nick of iRCbot to $name if it is not already
  184. in use.
  185.  
  186. kick($nick, $message = null)
  187. Kicks $nick out of the channel it's called from. $message
  188. is an optional parameter, but if used will use a custom
  189. kick message instead of the default one.
  190.  
  191. join($channel)
  192. Joins $channel. If $channel is an array, it joins all of the
  193. channels in the array... if possible, that is.
  194.  
  195. part($channel)
  196. Leaves (or parts) $channel. If no channel is specified, it
  197. parts the channel from which it was called.
  198.  
  199. quit($message)
  200. Disconnects from the server with an optional custom quit
  201. $message. iRCbot's default quit message is "My time has
  202. come...".
  203.  
  204. notice($nick, $message)
  205. Sends a notice containing $message to $nick. $nick can
  206. also be a channel instead.
  207.  
  208. send($type, $param = null)
  209. Sends raw data to the server. Basically useful if you want
  210. to do something that a function can't or when there is
  211. no function that does what you want to do. $type is the
  212. IRC command, such as PRIVMSG or something similar,
  213. and $param is an optional parameter. As it is optional,
  214. $param doesn't have to be used and $type can be the
  215. only parameter used.
  216.  
  217. 2.2 Variables
  218.  
  219. iRCbot contains a number of variables and arrays that contain
  220. data from the server to make your life easier. Most variables and
  221. arrays contain specific data that has been parsed from the main
  222. buffer to make it easier on plug-in creators to create plug-ins, so
  223. they don't have to know how to get the information themselves
  224. from the raw buffer data.
  225.  
  226. Unlike functions, no variables require $this-> prior to the variable
  227. name. Here's a sample variable call:
  228.  
  229. $channel
  230.  
  231. It's obvious and easy, and all of the variables have self-explanatory
  232. names so you'll know what they contain.
  233.  
  234. Here is the list of pre-defined variables and what they contain:
  235.  
  236. $channel
  237. Contains the channel which the command was received. If it
  238. was received via query to iRCbot, it returns iRCbot's current
  239. nickname. The functions privmsg and action take this into
  240. account so it is not necessary to check for this if using those
  241. functions.
  242.  
  243. $nick
  244. Contains the nick of the person that used a command.
  245.  
  246. $name
  247. Contains the username of the person that used a command.
  248.  
  249. $host
  250. Contains the hostmask of the person that used a command,
  251. but does not include the nick or username portions of of the
  252. hostmask, and only that after @.
  253.  
  254. $level
  255. Contains the level of the user that used a command. Returns
  256. nothing if the user is not in the user list.
  257.  
  258. $call_help
  259. Indicates whether the -help parameter was used. Returns 1 if
  260. so, otherwise returns 0.
  261.  
  262. $data
  263. The buffer. Contains raw data received from the socket with a
  264. maximum size of 512 bytes.
  265.  
  266. 2.3 Arrays
  267.  
  268. Arrays have the same use as variables, but some arrays do indeed
  269. use $this->. However, all of the arrays that have been made to make
  270. your job easier do not require $this->. Here's an example of calling an
  271. array:
  272.  
  273. $parameter[0]
  274.  
  275. As you should already know, arrays start at 0 increase in value by 1
  276. for each value within the array.
  277.  
  278. Here is the list of pre-defined arrays and the information they contain:
  279.  
  280. $command
  281. Contains the command called. Value starts at 1 rather than 0.
  282.  
  283. $parameter
  284. Contains the parameter(s) after a command. Value starts at 0.
  285. Pretty much contains everything that is said after a command.
  286.  
  287. $default_chan
  288. Contains the list of channels that iRCbot joins on startup.
  289.  
  290. $plugins
  291. Contains the list of loaded plug-ins. Is automatically updated
  292. when a plug-in is loaded.
  293.  
  294. $users
  295. Contains the list of users and their levels. In the array, value 0
  296. is the user and value 1 is the level. It follows this pattern for all
  297. users in the list.
  298.  
  299. $ignored
  300. Contains the list of users currently being ignored by iRCbot.
  301.  
  302. $this->full_data
  303. Contains the data from the buffer, but is exploded by whitespace.
  304. Not necessary unless wanting to achieve something that iRCbot's
  305. core functionality cannot.
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315. --------------------------------------------------------------------------------------------
  316. 3. Making a plug-in
  317. --------------------------------------------------------------------------------------------
  318.  
  319. 3.1 Help parameter
  320.  
  321. It is not required for a plug-in to respond to a -help parameter,
  322. but it is good if it does as users who want to know what it does
  323. and how to use it will be able to find out by using it.
  324.  
  325. A help parameter is used like so:
  326.  
  327. ~command -help
  328.  
  329. iRCbot checks whether the parameter is used before if executes
  330. the plug-in, and it is not recognized if not used as the only
  331. parameter.
  332.  
  333. When the -help parameter is used, it sets the variable $call_help
  334. to 1 before the plug-in is executed. It is up to the plug-in to check
  335. if $call_help is true (1 is true, 0 is false) and then act accordingly.
  336.  
  337. If you wish to acknowledge the help parameter, you may do so
  338. however you like, but it is encouraged that you use (or at least
  339. follow) this basic template:
  340.  
  341. <?php
  342.  
  343. $usage = "Use: ~command <parameter>";
  344. $info = "This is information on what the plug-in does and how it works.";
  345.  
  346. if($call_help)
  347. {
  348. $this->privmsg($channel, $usage);
  349. $this->privmsg($channel, $info);
  350. }
  351. else
  352. {
  353. // Plug-in code goes here
  354. }
  355.  
  356. ?>
  357.  
  358. 3.2 Requiring a certain level
  359.  
  360. Some plug-ins, depending on what it does, might be best if use
  361. is restricted to users with a certain minimum level. Users that are
  362. not on the user list or whose level is less than the minimum
  363. required level will not be able to use it. This is good if the plug-in
  364. does something that you don't want everyone to be able to do,
  365. such as changing the topic of a channel, kicking a user, etc...
  366.  
  367. As there is no limit to the level one can have, it is best if you
  368. check if a user's level is greater than or equal to the desired
  369. minimum level, rather than just equal to. This allows users with
  370. a higher level than the required level to use the plug-in. I hope
  371. that you already knew that, though.
  372.  
  373. Here is a template that you can use for a plug-in which requires
  374. the minimum level of 150.
  375.  
  376. <?php
  377.  
  378. $usage = "Use: ~command <parameter>";
  379. $info = "This is information on what the plug-in does and how it works.";
  380.  
  381. if($call_help)
  382. {
  383. $this->privmsg($channel, $usage);
  384. $this->privmsg($channel, $info);
  385. }
  386. elseif($level >= 150)
  387. {
  388. // Plug-in code goes here
  389. }
  390. else
  391. {
  392. $this->privmsg($channel, "Can't let you do that, $nick.");
  393. }
  394.  
  395. ?>
  396.  
  397. 3.3 Relaying errors
  398.  
  399. Sometimes a user won't use a plug-in properly, but if it doesn't
  400. say what's wrong, they won't know! It's not required that you
  401. relay messages telling the user what they did wrong, but it
  402. providesa better user experience and also helps for debugging
  403. your plug-in.
  404.  
  405. It's best to check to make sure that parameters are not empty,
  406. or at least the parameters required. You can relay specific
  407. messages to the user when one is empty so they know what
  408. it was they did wrong. This is just an example of when to relay
  409. a message containing an error or what happened that was
  410. wrong.
Advertisement
Add Comment
Please, Sign In to add comment