command.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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', dirname(__DIR__));
  21. define('PUBLICDIR', INSTALLDIR . DIRECTORY_SEPARATOR . 'public');
  22. $shortoptions = 'i:n:o';
  23. $longoptions = array('id=', 'nickname=', 'owner');
  24. $helptext = <<<END_OF_USERROLE_HELP
  25. command.php [options] [command line]
  26. Perform commands on behalf of a user, such as sub, unsub, join, drop
  27. -i --id ID of the user
  28. -n --nickname nickname of the user
  29. -o --owner use the site owner
  30. END_OF_USERROLE_HELP;
  31. require_once INSTALLDIR . '/scripts/commandline.inc';
  32. function interpretCommand($user, $body)
  33. {
  34. $inter = new CommandInterpreter();
  35. $chan = new CLIChannel();
  36. $cmd = $inter->handle_command($user, $body);
  37. if ($cmd) {
  38. $cmd->execute($chan);
  39. return true;
  40. } else {
  41. $chan->error($user, "Not a valid command. Try 'help'?");
  42. return false;
  43. }
  44. }
  45. if (have_option('i', 'id')) {
  46. $id = get_option_value('i', 'id');
  47. $user = User::getKV('id', $id);
  48. if (empty($user)) {
  49. print "Can't find user with ID $id\n";
  50. exit(1);
  51. }
  52. } else if (have_option('n', 'nickname')) {
  53. $nickname = get_option_value('n', 'nickname');
  54. $user = User::getKV('nickname', $nickname);
  55. if (empty($user)) {
  56. print "Can't find user with nickname '$nickname'\n";
  57. exit(1);
  58. }
  59. } else if (have_option('o', 'owner')) {
  60. try {
  61. $user = User::siteOwner();
  62. } catch (ServerException $e) {
  63. print "Site has no owner.\n";
  64. exit(1);
  65. }
  66. } else {
  67. print "You must provide either an ID or a nickname.\n\n";
  68. print $helptext;
  69. exit(1);
  70. }
  71. // @todo refactor the interactive console in console.php and use
  72. // that to optionally make an interactive test console here too.
  73. // Would be good to help people test commands when XMPP or email
  74. // isn't available locally.
  75. interpretCommand($user, implode(' ', $args));