commandinterpreter.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. <?php
  2. /*
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2008, 2009, StatusNet, Inc.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
  20. require_once INSTALLDIR.'/lib/command.php';
  21. class CommandInterpreter
  22. {
  23. function handle_command($user, $text)
  24. {
  25. // XXX: localise
  26. $text = preg_replace('/\s+/', ' ', trim($text));
  27. list($cmd, $arg) = self::split_arg($text);
  28. // We try to support all the same commands as Twitter, see
  29. // http://getsatisfaction.com/twitter/topics/what_are_the_twitter_commands
  30. // There are a few compatibility commands from earlier versions of
  31. // StatusNet
  32. $cmd = strtolower($cmd);
  33. $result = false;
  34. if (Event::handle('StartInterpretCommand', array($cmd, $arg, $user, &$result))) {
  35. switch($cmd) {
  36. case 'help':
  37. if ($arg) {
  38. $result = null;
  39. } else {
  40. $result = new HelpCommand($user);
  41. }
  42. break;
  43. case 'login':
  44. if ($arg) {
  45. $result = null;
  46. } else {
  47. $result = new LoginCommand($user);
  48. }
  49. break;
  50. case 'lose':
  51. if ($arg) {
  52. list($other, $extra) = self::split_arg($arg);
  53. if ($extra) {
  54. $result = null;
  55. } else {
  56. $result = new LoseCommand($user, $other);
  57. }
  58. } else {
  59. $result = null;
  60. }
  61. break;
  62. case 'subscribers':
  63. if ($arg) {
  64. $result = null;
  65. } else {
  66. $result = new SubscribersCommand($user);
  67. }
  68. break;
  69. case 'subscriptions':
  70. if ($arg) {
  71. $result = null;
  72. } else {
  73. $result = new SubscriptionsCommand($user);
  74. }
  75. break;
  76. case 'groups':
  77. if ($arg) {
  78. $result = null;
  79. } else {
  80. $result = new GroupsCommand($user);
  81. }
  82. break;
  83. case 'on':
  84. if ($arg) {
  85. list($other, $extra) = self::split_arg($arg);
  86. if ($extra) {
  87. $result = null;
  88. } else {
  89. $result = new OnCommand($user, $other);
  90. }
  91. } else {
  92. $result = new OnCommand($user);
  93. }
  94. break;
  95. case 'off':
  96. if ($arg) {
  97. list($other, $extra) = self::split_arg($arg);
  98. if ($extra) {
  99. $result = null;
  100. } else {
  101. $result = new OffCommand($user, $other);
  102. }
  103. } else {
  104. $result = new OffCommand($user);
  105. }
  106. break;
  107. case 'stop':
  108. case 'quit':
  109. if ($arg) {
  110. $result = null;
  111. } else {
  112. $result = new OffCommand($user);
  113. }
  114. break;
  115. case 'join':
  116. if (!$arg) {
  117. $result = null;
  118. } else {
  119. list($other, $extra) = self::split_arg($arg);
  120. if ($extra) {
  121. $result = null;
  122. } else {
  123. $result = new JoinCommand($user, $other);
  124. }
  125. }
  126. break;
  127. case 'drop':
  128. if (!$arg) {
  129. $result = null;
  130. } else {
  131. list($other, $extra) = self::split_arg($arg);
  132. if ($extra) {
  133. $result = null;
  134. } else {
  135. $result = new DropCommand($user, $other);
  136. }
  137. }
  138. break;
  139. case 'follow':
  140. case 'sub':
  141. if (!$arg) {
  142. $result = null;
  143. } else {
  144. list($other, $extra) = self::split_arg($arg);
  145. if ($extra) {
  146. $result = null;
  147. } else {
  148. $result = new SubCommand($user, $other);
  149. }
  150. }
  151. break;
  152. case 'leave':
  153. case 'unsub':
  154. if (!$arg) {
  155. $result = null;
  156. } else {
  157. list($other, $extra) = self::split_arg($arg);
  158. if ($extra) {
  159. $result = null;
  160. } else {
  161. $result = new UnsubCommand($user, $other);
  162. }
  163. }
  164. break;
  165. case 'get':
  166. case 'last':
  167. if (!$arg) {
  168. $result = null;
  169. }
  170. list($other, $extra) = self::split_arg($arg);
  171. if ($extra) {
  172. $result = null;
  173. } else {
  174. $result = new GetCommand($user, $other);
  175. }
  176. break;
  177. case 'r':
  178. case 'reply':
  179. if (!$arg) {
  180. $result = null;
  181. }
  182. list($other, $extra) = self::split_arg($arg);
  183. if (!$extra) {
  184. $result = null;
  185. } else {
  186. $result = new ReplyCommand($user, $other, $extra);
  187. }
  188. break;
  189. case 'whois':
  190. if (!$arg) {
  191. $result = null;
  192. } else {
  193. list($other, $extra) = self::split_arg($arg);
  194. if ($extra) {
  195. $result = null;
  196. } else {
  197. $result = new WhoisCommand($user, $other);
  198. }
  199. }
  200. break;
  201. case 'nudge':
  202. if (!$arg) {
  203. $result = null;
  204. } else {
  205. list($other, $extra) = self::split_arg($arg);
  206. if ($extra) {
  207. $result = null;
  208. } else {
  209. $result = new NudgeCommand($user, $other);
  210. }
  211. }
  212. break;
  213. case 'stats':
  214. if ($arg) {
  215. $result = null;
  216. } else {
  217. $result = new StatsCommand($user);
  218. }
  219. break;
  220. case 'invite':
  221. if (!$arg) {
  222. $result = null;
  223. } else {
  224. list($other, $extra) = self::split_arg($arg);
  225. if ($extra) {
  226. $result = null;
  227. } else {
  228. $result = new InviteCommand($user, $other);
  229. }
  230. }
  231. break;
  232. case 'list':
  233. case 'tag':
  234. if (!$arg) {
  235. $result = null;
  236. break;
  237. }
  238. list($other, $tags) = self::split_arg($arg);
  239. if (!$tags) {
  240. $result = null;
  241. } else {
  242. $result = new TagCommand($user, $other, $tags);
  243. }
  244. break;
  245. case 'unlist':
  246. case 'untag':
  247. if (!$arg) {
  248. $result = null;
  249. break;
  250. }
  251. list($other, $tags) = self::split_arg($arg);
  252. if (!$tags) {
  253. $result = null;
  254. } else {
  255. $result = new UntagCommand($user, $other, $tags);
  256. }
  257. break;
  258. case 'track':
  259. if (!$arg) {
  260. $result = null;
  261. } else {
  262. list($word, $extra) = self::split_arg($arg);
  263. if ($extra) {
  264. $result = null;
  265. } else if ($word == 'off') {
  266. $result = new TrackOffCommand($user);
  267. } else {
  268. $result = new TrackCommand($user, $word);
  269. }
  270. }
  271. break;
  272. case 'untrack':
  273. if (!$arg) {
  274. $result = null;
  275. } else {
  276. list($word, $extra) = self::split_arg($arg);
  277. if ($extra) {
  278. $result = null;
  279. } else if ($word == 'all') {
  280. $result = new TrackOffCommand($user);
  281. } else {
  282. $result = new UntrackCommand($user, $word);
  283. }
  284. }
  285. break;
  286. case 'tracks':
  287. case 'tracking':
  288. if ($arg) {
  289. $result = null;
  290. } else {
  291. $result = new TrackingCommand($user);
  292. }
  293. break;
  294. }
  295. Event::handle('EndInterpretCommand', array($cmd, $arg, $user, &$result));
  296. }
  297. return $result;
  298. }
  299. /**
  300. * Split arguments without triggering a PHP notice warning
  301. */
  302. static function split_arg($text)
  303. {
  304. $pieces = explode(' ', $text, 2);
  305. if (count($pieces) == 1) {
  306. $pieces[] = null;
  307. }
  308. return $pieces;
  309. }
  310. }