command.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #!/usr/bin/env php
  2. <?php
  3. /*
  4. * StatusNet - a distributed open-source microblogging tool
  5. * Copyright (C) 2008, 2009, 2010 StatusNet, Inc.
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
  21. $shortoptions = 'i:n:o';
  22. $longoptions = array('id=', 'nickname=', 'owner');
  23. $helptext = <<<END_OF_USERROLE_HELP
  24. command.php [options] [command line]
  25. Perform commands on behalf of a user, such as sub, unsub, join, drop
  26. -i --id ID of the user
  27. -n --nickname nickname of the user
  28. -o --owner use the site owner
  29. END_OF_USERROLE_HELP;
  30. require_once INSTALLDIR.'/scripts/commandline.inc';
  31. function interpretCommand($user, $body)
  32. {
  33. $inter = new CommandInterpreter();
  34. $chan = new CLIChannel();
  35. $cmd = $inter->handle_command($user, $body);
  36. if ($cmd) {
  37. $cmd->execute($chan);
  38. return true;
  39. } else {
  40. $chan->error($user, "Not a valid command. Try 'help'?");
  41. return false;
  42. }
  43. }
  44. if (have_option('i', 'id')) {
  45. $id = get_option_value('i', 'id');
  46. $user = User::getKV('id', $id);
  47. if (empty($user)) {
  48. print "Can't find user with ID $id\n";
  49. exit(1);
  50. }
  51. } else if (have_option('n', 'nickname')) {
  52. $nickname = get_option_value('n', 'nickname');
  53. $user = User::getKV('nickname', $nickname);
  54. if (empty($user)) {
  55. print "Can't find user with nickname '$nickname'\n";
  56. exit(1);
  57. }
  58. } else if (have_option('o', 'owner')) {
  59. try {
  60. $user = User::siteOwner();
  61. } catch (ServerException $e) {
  62. print "Site has no owner.\n";
  63. exit(1);
  64. }
  65. } else {
  66. print "You must provide either an ID or a nickname.\n\n";
  67. print $helptext;
  68. exit(1);
  69. }
  70. // @todo refactor the interactive console in console.php and use
  71. // that to optionally make an interactive test console here too.
  72. // Would be good to help people test commands when XMPP or email
  73. // isn't available locally.
  74. interpretCommand($user, implode(' ', $args));