Posted By

mladoux on 09/25/12


Tagged

irc


Versions (?)

Mark's IRC API Library


 / Published in: PHP
 

URL: http://markladoux.com/

Formats data for sending to an IRC network. This library does not handle * actual connecting to the IRC server or parsing the IRC servers output. * * The raw_send method can be used to send data to the IRC in formats that are * either not standard, or not yet implemented here. I know there is a lot more * to implement, and I plan to do so as time goes on.

  1. <?php
  2. /**
  3.  * Mark's IRC API Library
  4.  *
  5.  * Formats data for sending to an IRC network. This library does not handle
  6.  * actual connecting to the IRC server or parsing the IRC servers output.
  7.  *
  8.  * The raw_send method can be used to send data to the IRC in formats that are
  9.  * either not standard, or not yet implemented here. I know there is a lot more
  10.  * to implement, and I plan to do so as time goes on.
  11.  *
  12.  * @author Mark LaDoux
  13.  * @copyright Copyright © 2012, Mark LaDoux
  14.  * @version 1.0.0
  15.  * @link http://markladoux.com/
  16.  */
  17.  
  18. class IRC
  19. {
  20. /**
  21. * Library version
  22. *
  23. * @access protected
  24. * @var string
  25. */
  26. protected $_version = '1.0.0';
  27.  
  28. /**
  29. * Connection socket
  30. *
  31. * @access protected
  32. * @var object
  33. */
  34. protected $_socket;
  35.  
  36. /**
  37. * Constructor
  38. *
  39. * configures the class for use
  40. *
  41. * @access public
  42. * @param object
  43. * @return void
  44. */
  45. public function __construct($socket)
  46. {
  47. $this->_socket = $socket;
  48. }
  49.  
  50. /**
  51. * version
  52. *
  53. * Reports the libraries version
  54. *
  55. * @access public
  56. * @return string
  57. */
  58. public function version()
  59. {
  60. return $this->_version;
  61. }
  62.  
  63. /**
  64. * say
  65. *
  66. * send a message to the target
  67. *
  68. * @access public
  69. * @param string $target
  70. * @param string $message
  71. * @return void
  72. */
  73. public function say($target, $message)
  74. {
  75. $data = "PRIVMSG {$target} :{$message}";
  76. $this->raw_send($data);
  77. }
  78.  
  79. /**
  80. * emote
  81. *
  82. * send an emote to the target ( equiv to IRC client /me does something )
  83. *
  84. * @access public
  85. * @param string $target
  86. * @param string $message
  87. * @return void
  88. */
  89. public function emote($target, $message)
  90. {
  91. $data = chr(1)."ACTION {$message}".chr(1);
  92. $this->say($target, $data);
  93. }
  94.  
  95. /**
  96. * ctcp
  97. *
  98. * send a ctcp to the target
  99. *
  100. * @access public
  101. * @param string $target
  102. * @param string $message
  103. * @return void
  104. */
  105. public function ctcp($target, $message)
  106. {
  107. $data = chr(1).$message.chr(1);
  108. $this->say($target, $data);
  109. }
  110.  
  111. /**
  112. * notice
  113. *
  114. * send a notice to the target
  115. *
  116. * @access public
  117. * @param string $target
  118. * @param string $message
  119. * @return void
  120. */
  121. public function notice($target, $message)
  122. {
  123. $data = "NOTICE {$target} :{$message}";
  124. $this->raw_send($data);
  125. }
  126.  
  127. /**
  128. * set_nick
  129. *
  130. * change user nick name
  131. *
  132. * @access public
  133. * @param string
  134. * @return void
  135. */
  136. public function set_nick($nick)
  137. {
  138. $data = "NICK {$nick}";
  139. $this->raw_send($data);
  140. }
  141.  
  142. /**
  143. * set_user
  144. *
  145. * prepare user ident string
  146. *
  147. * @access public
  148. * @param string
  149. * @return void
  150. */
  151. public function set_user($nick)
  152. {
  153. $data = "USER $nick $nick $nick $nick :$nick";
  154. $this->raw_send($data);
  155. }
  156.  
  157. /**
  158. * send_pass
  159. *
  160. * Identify with nickserv
  161. *
  162. * @access public
  163. * @param string
  164. * @return void
  165. */
  166. public function send_pass($password)
  167. {
  168. $this->say('nickserv', "IDENTIFY {$password}");
  169. }
  170.  
  171. /**
  172. * disconnect
  173. *
  174. * Terminates IRC connection. Does not terminate reset process in the case
  175. * you have implemented a reconnect function
  176. *
  177. * @access public
  178. * @param string|null
  179. * @return void
  180. */
  181. public function disconnect($message = NULL)
  182. {
  183. $data = ($message !== NULL) ? "QUIT :{$message}" : "QUIT";
  184. $this->raw_send($data);
  185. }
  186.  
  187. /**
  188. * enter_channel
  189. *
  190. * enter an IRC channel
  191. *
  192. * @access public
  193. * @param string
  194. * @return void
  195. */
  196. public function enter_channel($channel)
  197. {
  198. $data = ($channel[0] == '#') ? "JOIN {$channel}" : "JOIN #{$channel}";
  199. $this->raw_send($data);
  200. }
  201.  
  202. /**
  203. * leave_channel
  204. *
  205. * leaves an IRC channel
  206. *
  207. * @access public
  208. * @param string
  209. * @return void
  210. */
  211. public function leave_channel($channel)
  212. {
  213. $data = ($channel[0] == '#') ? "PART {$channel}" : "PART #{$channel}";
  214. $this->raw_send($data);
  215. }
  216.  
  217. /**
  218. * raw_send
  219. *
  220. * Sends raw commands to the IRC server. I left it a public function for
  221. * hackability reasons. Makes it easier to add custom commands for
  222. * non-standard servers, or to test new features without having to edit
  223. * the class directly every time. Of course you could always just extend
  224. * the class.
  225. *
  226. * @access public
  227. * @return void
  228. */
  229. public function raw_send($data)
  230. {
  231. fwrite($this->_socket, $data."\n");
  232. }
  233.  
  234. }

Report this snippet  

You need to login to post a comment.