Java IRC bot
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SetCommand.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright (c) 2009-2010 Chris Smith
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT 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 THE
  20. * SOFTWARE.
  21. */
  22. package com.md87.charliebravo.commands;
  23. import com.dmdirc.parser.irc.ClientInfo;
  24. import com.md87.charliebravo.Command;
  25. import com.md87.charliebravo.CommandOptions;
  26. import com.md87.charliebravo.InputHandler;
  27. import com.md87.charliebravo.Response;
  28. /**
  29. *
  30. * @author chris
  31. */
  32. @CommandOptions(requireAuthorisation=true)
  33. public class SetCommand implements Command {
  34. public void execute(InputHandler handler, Response response, String line) throws Exception {
  35. final String openID = (String) handler.getParser().getClientInfoOrFake(response.getSource())
  36. .getMap().get("OpenID");
  37. String target = openID;
  38. String value = line;
  39. int offset;
  40. if ((offset = line.toLowerCase().lastIndexOf("on behalf of")) > -1) {
  41. if (!handler.getConfig().hasOption(openID, "admin.level")
  42. || Integer.parseInt(handler.getConfig().getOption(openID, "admin.level")) < 100) {
  43. response.sendMessage("You do not have sufficient access", true);
  44. return;
  45. } else {
  46. final String user = line.substring(offset + 13);
  47. value = line.substring(0, offset - 1);
  48. final ClientInfo client = handler.getParser().getClientInfo(user);
  49. if (client != null) {
  50. if (client.getMap().get("OpenID") == null) {
  51. response.sendMessage(client.getNickname() + " isn't authed", true);
  52. return;
  53. }
  54. target = (String) client.getMap().get("OpenID");
  55. } else if (handler.getConfig().hasOption(user, "internal.lastseen")) {
  56. target = user;
  57. } else {
  58. response.sendMessage("I couldn't find anyone by that name", true);
  59. return;
  60. }
  61. }
  62. }
  63. final String[] parts = value.split("\\s+", 2);
  64. if (openID == null) {
  65. response.sendMessage("You must be authorised to use this command", true);
  66. } else if (parts.length < 2) {
  67. response.sendMessage("You must specify a setting name and value", true);
  68. } else if (!handler.getConfig().isLegalSetting(parts[0])) {
  69. response.sendMessage("That isn't a legal setting", true);
  70. } else if (parts[0].startsWith("admin.")
  71. && (!handler.getConfig().hasOption(openID, "admin.level")
  72. || Integer.parseInt(handler.getConfig().getOption(openID, "admin.level")) < 100)) {
  73. response.sendMessage("You do not have sufficient access", true);
  74. } else {
  75. handler.getConfig().setOption(target, parts[0], parts[1]);
  76. response.sendMessage("OK", true);
  77. }
  78. }
  79. }