/lib/Net/Socket/Tiarra.php

https://github.com/tyoro/tiarraMetro · PHP · 253 lines · 125 code · 18 blank · 110 comment · 1 complexity · 1f3ddd4d4352958215d6a70d3cdf0bd3 MD5 · raw file

  1. <?php
  2. /**
  3. * Net_Socket_Tiarra
  4. *
  5. * Using socket of tiarra to send message.
  6. *
  7. * PHP version 5
  8. *
  9. * The MIT License
  10. *
  11. * Copyright (c) 2009 sotarok <sotaro.k /at/ gmail.com>
  12. *
  13. * Permission is hereby granted, free of charge, to any person obtaining a copy
  14. * of this software and associated documentation files (the "Software"), to deal
  15. * in the Software without restriction, including without limitation the rights
  16. * to use, copy, modify, merge, publish, diENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. * THE SOFTWARE.
  21. *
  22. * @category Net/Socket
  23. * @package Net_Socket_Tiarra
  24. * @author sotarok <sotaro.k /at/ gmail.com> <author@mail.com>
  25. * @copyright 2009 sotarok <sotaro.k /at/ gmail.com>
  26. * @license http://openpear.org/package/Net_Socket_Tiarra
  27. * @version SVN: $Id$
  28. * @link http://pear.php.net/package/Net_Socket_Tiarra
  29. */
  30. require_once 'Net/Socket/Tiarra/Exception.php';
  31. /**
  32. * Net_Socket_Tiarra
  33. *
  34. * @category Net/Socket
  35. * @package Net_Socket_Tiarra
  36. * @author sotarok <sotaro.k /at/ gmail.com> <author@mail.com>
  37. * @copyright 2009 sotarok <sotaro.k /at/ gmail.com>
  38. * @license http://openpear.org/package/Net_Socket_Tiarra
  39. * @version Release: @package_version@
  40. * @link http://pear.php.net/package/Net_Socket_Tiarra
  41. */
  42. class Net_Socket_Tiarra
  43. {
  44. /**
  45. * Sender name
  46. */
  47. const sender = "PHP/Net_IRC_TIarra";
  48. /**
  49. * Tiarra Protocol
  50. */
  51. const protocol = "TIARRACONTROL/1.0";
  52. /**
  53. * Message template
  54. */
  55. const request_template = "
  56. NOTIFY System::SendMessage %s\r
  57. Sender: %s\r
  58. Notice: %s\r
  59. Channel: %s\r
  60. Charset: %s\r
  61. Text: %s\r
  62. \r
  63. ";
  64. /**
  65. * Nick receiver Message template
  66. */
  67. const nick_request_template = "
  68. NOTIFY System::SendMessage %s\r
  69. Sender: %s\r
  70. Notice: %s\r
  71. Nick: %s\r
  72. Charset: %s\r
  73. Text: %s\r
  74. \r
  75. ";
  76. /**
  77. * Socket name to connect
  78. * @var string
  79. * @access protected
  80. */
  81. protected $_socket_name;
  82. /**
  83. * Socket resource
  84. * @var resource
  85. * @access protected
  86. */
  87. protected $_socket_resource;
  88. /**
  89. * Options
  90. * @var array
  91. * @access protected
  92. */
  93. protected $_options;
  94. /**
  95. * Default opsions
  96. * @var array
  97. * @access protected
  98. */
  99. protected $_options_default = array(
  100. 'charset' => 'UTF-8',
  101. );
  102. /**
  103. * constructor
  104. *
  105. * @param string $socketname socket name (defined in tiarra.conf)
  106. * @param array $options options array
  107. * @access public
  108. */
  109. public function __construct($socketname, Array $options = array())
  110. {
  111. $this->setOptions($this->_options_default);
  112. $this->setOptions($options);
  113. $this->_socket_name = $socketname;
  114. }
  115. /**
  116. * setOptions
  117. *
  118. * @param array $options
  119. * @return object Return
  120. * @access public
  121. */
  122. public function setOptions(Array $options)
  123. {
  124. foreach ($options as $key => $value) {
  125. $this->_setOption($key, $value);
  126. }
  127. return $this;
  128. }
  129. /**
  130. * _setOption
  131. *
  132. * @param unknown $key
  133. * @param unknown $value
  134. * @return object Return
  135. * @access protected
  136. */
  137. protected function _setOption($key, $value)
  138. {
  139. $this->_options[$key] = $value;
  140. return $this;
  141. }
  142. /**
  143. * getOption
  144. *
  145. * @param string $key
  146. * @return array Return
  147. * @access public
  148. */
  149. public function getOption($key)
  150. {
  151. return $this->_options[$key];
  152. }
  153. /**
  154. * message
  155. *
  156. * @param string $channel
  157. * @param string $text
  158. * @param boolean $use_notice
  159. * @return void
  160. * @access public
  161. * @throws Net_Socket_Tiarra_Exception Exception
  162. */
  163. public function message($channel, $text, $use_notice = false, $to_nick = false)
  164. {
  165. foreach ($this->strSplit($text, 430) as $k => $t) {
  166. $this->_socket_resource = fsockopen("unix:///tmp/tiarra-control/" . $this->_socket_name);
  167. if (!$this->_socket_resource) {
  168. throw new Net_Socket_Tiarra_Exception("error: cannot connect tiarra's socket");
  169. }
  170. fwrite($this->_socket_resource,
  171. sprintf(
  172. ($to_nick ? Net_Socket_Tiarra::nick_request_template : Net_Socket_Tiarra::request_template), // template
  173. Net_Socket_Tiarra::protocol,
  174. Net_Socket_Tiarra::sender,
  175. ($use_notice ? 'yes' : 'no'),
  176. $channel,
  177. $this->getOption('charset'),
  178. $t
  179. )
  180. );
  181. $response = '';
  182. while (!feof($this->_socket_resource)) {
  183. $response .= fgets($this->_socket_resource, 128);
  184. }
  185. if(!preg_match('@^' . preg_quote(Net_Socket_Tiarra::protocol, '@') . ' 200 OK@', $response)) {
  186. throw new Net_Socket_Tiarra_Exception("error: " . $response);
  187. };
  188. fclose($this->_socket_resource);
  189. }
  190. }
  191. /**
  192. * noticeMessage
  193. *
  194. * @param string $channel Parameter
  195. * @param string $text Parameter
  196. * @return void
  197. * @access public
  198. */
  199. public function noticeMessage($channel, $text)
  200. {
  201. $this->message($channel, $text, true,false);
  202. }
  203. public function tonickMessage($nick, $text){
  204. $this->message($nick, $text, false, true);
  205. }
  206. /**
  207. * strSplit
  208. *
  209. * @param string $text Parameter
  210. * @param int $count Parameter
  211. * @return array
  212. */
  213. public function strSplit($text, $count)
  214. {
  215. $result = array();
  216. $start = 0;
  217. while(1) {
  218. $substr = mb_substr($text, $start, $count, $this->getOption('charset'));
  219. $result[] = $substr;
  220. if (mb_strlen($substr, $this->getOption('charset')) < $count) {
  221. break;
  222. }
  223. $start += $count;
  224. }
  225. return $result;
  226. }
  227. }