commandinterpreter.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. if (Event::handle('StartInterpretCommand', array($cmd, $arg, $user, &$result))) {
  34. switch($cmd) {
  35. case 'help':
  36. if ($arg) {
  37. $result = null;
  38. } else {
  39. $result = new HelpCommand($user);
  40. }
  41. break;
  42. case 'login':
  43. if ($arg) {
  44. $result = null;
  45. } else {
  46. $result = new LoginCommand($user);
  47. }
  48. break;
  49. case 'lose':
  50. if ($arg) {
  51. list($other, $extra) = self::split_arg($arg);
  52. if ($extra) {
  53. $result = null;
  54. } else {
  55. $result = new LoseCommand($user, $other);
  56. }
  57. } else {
  58. $result = null;
  59. }
  60. break;
  61. case 'subscribers':
  62. if ($arg) {
  63. $result = null;
  64. } else {
  65. $result = new SubscribersCommand($user);
  66. }
  67. break;
  68. case 'subscriptions':
  69. if ($arg) {
  70. $result = null;
  71. } else {
  72. $result = new SubscriptionsCommand($user);
  73. }
  74. break;
  75. case 'groups':
  76. if ($arg) {
  77. $result = null;
  78. } else {
  79. $result = new GroupsCommand($user);
  80. }
  81. break;
  82. case 'on':
  83. if ($arg) {
  84. list($other, $extra) = self::split_arg($arg);
  85. if ($extra) {
  86. $result = null;
  87. } else {
  88. $result = new OnCommand($user, $other);
  89. }
  90. } else {
  91. $result = new OnCommand($user);
  92. }
  93. break;
  94. case 'off':
  95. if ($arg) {
  96. list($other, $extra) = self::split_arg($arg);
  97. if ($extra) {
  98. $result = null;
  99. } else {
  100. $result = new OffCommand($user, $other);
  101. }
  102. } else {
  103. $result = new OffCommand($user);
  104. }
  105. break;
  106. case 'stop':
  107. case 'quit':
  108. if ($arg) {
  109. $result = null;
  110. } else {
  111. $result = new OffCommand($user);
  112. }
  113. break;
  114. case 'join':
  115. if (!$arg) {
  116. $result = null;
  117. } else {
  118. list($other, $extra) = self::split_arg($arg);
  119. if ($extra) {
  120. $result = null;
  121. } else {
  122. $result = new JoinCommand($user, $other);
  123. }
  124. }
  125. break;
  126. case 'drop':
  127. if (!$arg) {
  128. $result = null;
  129. } else {
  130. list($other, $extra) = self::split_arg($arg);
  131. if ($extra) {
  132. $result = null;
  133. } else {
  134. $result = new DropCommand($user, $other);
  135. }
  136. }
  137. break;
  138. case 'follow':
  139. case 'sub':
  140. if (!$arg) {
  141. $result = null;
  142. } else {
  143. list($other, $extra) = self::split_arg($arg);
  144. if ($extra) {
  145. $result = null;
  146. } else {
  147. $result = new SubCommand($user, $other);
  148. }
  149. }
  150. break;
  151. case 'leave':
  152. case 'unsub':
  153. if (!$arg) {
  154. $result = null;
  155. } else {
  156. list($other, $extra) = self::split_arg($arg);
  157. if ($extra) {
  158. $result = null;
  159. } else {
  160. $result = new UnsubCommand($user, $other);
  161. }
  162. }
  163. break;
  164. case 'get':
  165. case 'last':
  166. if (!$arg) {
  167. $result = null;
  168. }
  169. list($other, $extra) = self::split_arg($arg);
  170. if ($extra) {
  171. $result = null;
  172. } else {
  173. $result = new GetCommand($user, $other);
  174. }
  175. break;
  176. case 'r':
  177. case 'reply':
  178. if (!$arg) {
  179. $result = null;
  180. }
  181. list($other, $extra) = self::split_arg($arg);
  182. if (!$extra) {
  183. $result = null;
  184. } else {
  185. $result = new ReplyCommand($user, $other, $extra);
  186. }
  187. break;
  188. case 'whois':
  189. if (!$arg) {
  190. $result = null;
  191. } else {
  192. list($other, $extra) = self::split_arg($arg);
  193. if ($extra) {
  194. $result = null;
  195. } else {
  196. $result = new WhoisCommand($user, $other);
  197. }
  198. }
  199. break;
  200. case 'nudge':
  201. if (!$arg) {
  202. $result = null;
  203. } else {
  204. list($other, $extra) = self::split_arg($arg);
  205. if ($extra) {
  206. $result = null;
  207. } else {
  208. $result = new NudgeCommand($user, $other);
  209. }
  210. }
  211. break;
  212. case 'stats':
  213. if ($arg) {
  214. $result = null;
  215. } else {
  216. $result = new StatsCommand($user);
  217. }
  218. break;
  219. case 'invite':
  220. if (!$arg) {
  221. $result = null;
  222. } else {
  223. list($other, $extra) = self::split_arg($arg);
  224. if ($extra) {
  225. $result = null;
  226. } else {
  227. $result = new InviteCommand($user, $other);
  228. }
  229. }
  230. break;
  231. case 'list':
  232. case 'tag':
  233. if (!$arg) {
  234. $result = null;
  235. break;
  236. }
  237. list($other, $tags) = self::split_arg($arg);
  238. if (!$tags) {
  239. $result = null;
  240. } else {
  241. $result = new TagCommand($user, $other, $tags);
  242. }
  243. break;
  244. case 'unlist':
  245. case 'untag':
  246. if (!$arg) {
  247. $result = null;
  248. break;
  249. }
  250. list($other, $tags) = self::split_arg($arg);
  251. if (!$tags) {
  252. $result = null;
  253. } else {
  254. $result = new UntagCommand($user, $other, $tags);
  255. }
  256. break;
  257. case 'track':
  258. if (!$arg) {
  259. $result = null;
  260. } else {
  261. list($word, $extra) = self::split_arg($arg);
  262. if ($extra) {
  263. $result = null;
  264. } else if ($word == 'off') {
  265. $result = new TrackOffCommand($user);
  266. } else {
  267. $result = new TrackCommand($user, $word);
  268. }
  269. }
  270. break;
  271. case 'untrack':
  272. if (!$arg) {
  273. $result = null;
  274. } else {
  275. list($word, $extra) = self::split_arg($arg);
  276. if ($extra) {
  277. $result = null;
  278. } else if ($word == 'all') {
  279. $result = new TrackOffCommand($user);
  280. } else {
  281. $result = new UntrackCommand($user, $word);
  282. }
  283. }
  284. break;
  285. case 'tracks':
  286. case 'tracking':
  287. if ($arg) {
  288. $result = null;
  289. } else {
  290. $result = new TrackingCommand($user);
  291. }
  292. break;
  293. default:
  294. $result = false;
  295. }
  296. Event::handle('EndInterpretCommand', array($cmd, $arg, $user, &$result));
  297. }
  298. return $result;
  299. }
  300. /**
  301. * Split arguments without triggering a PHP notice warning
  302. */
  303. static function split_arg($text)
  304. {
  305. $pieces = explode(' ', $text, 2);
  306. if (count($pieces) == 1) {
  307. $pieces[] = null;
  308. }
  309. return $pieces;
  310. }
  311. }