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.

ShowTopic.java 4.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.commandparser.commands.channel;
  18. import com.dmdirc.Topic;
  19. import com.dmdirc.commandparser.BaseCommandInfo;
  20. import com.dmdirc.commandparser.CommandArguments;
  21. import com.dmdirc.commandparser.CommandInfo;
  22. import com.dmdirc.commandparser.CommandType;
  23. import com.dmdirc.commandparser.commands.BaseCommand;
  24. import com.dmdirc.commandparser.commands.CommandOptions;
  25. import com.dmdirc.commandparser.commands.ExternalCommand;
  26. import com.dmdirc.commandparser.commands.context.ChannelCommandContext;
  27. import com.dmdirc.commandparser.commands.context.CommandContext;
  28. import com.dmdirc.events.ChannelGotTopicEvent;
  29. import com.dmdirc.events.ChannelNoTopicEvent;
  30. import com.dmdirc.interfaces.CommandController;
  31. import com.dmdirc.interfaces.Connection;
  32. import com.dmdirc.interfaces.GroupChat;
  33. import com.dmdirc.interfaces.GroupChatUser;
  34. import com.dmdirc.interfaces.User;
  35. import com.dmdirc.interfaces.WindowModel;
  36. import javax.annotation.Nonnull;
  37. import javax.inject.Inject;
  38. import java.util.Optional;
  39. /**
  40. * The show topic command shows the user the current topic.
  41. */
  42. @CommandOptions(allowOffline = false)
  43. public class ShowTopic extends BaseCommand implements ExternalCommand {
  44. /** A command info object for this command. */
  45. public static final CommandInfo INFO = new BaseCommandInfo("topic",
  46. "topic - displays the current topic\ntopic <newtopic> - sets the channel topic",
  47. CommandType.TYPE_CHANNEL);
  48. /**
  49. * Creates a new instance of this command.
  50. *
  51. * @param controller The controller to use for command information.
  52. */
  53. @Inject
  54. public ShowTopic(final CommandController controller) {
  55. super(controller);
  56. }
  57. @Override
  58. public void execute(@Nonnull final WindowModel origin,
  59. final CommandArguments args, final CommandContext context) {
  60. final GroupChat channel = ((ChannelCommandContext) context).getGroupChat();
  61. if (args.getArguments().length == 0) {
  62. final Optional<Topic> topic = channel.getCurrentTopic();
  63. if (topic.isPresent()) {
  64. final User user = topic
  65. .flatMap(Topic::getClient)
  66. .map(GroupChatUser::getUser)
  67. .orElse(null);
  68. channel.getEventBus().publishAsync(
  69. new ChannelGotTopicEvent(channel, topic.get(), user));
  70. } else {
  71. channel.getEventBus().publishAsync(
  72. new ChannelNoTopicEvent(channel));
  73. }
  74. } else {
  75. channel.setTopic(args.getArgumentsAsString());
  76. }
  77. }
  78. @Override
  79. public void execute(final WindowModel origin, final Connection connection,
  80. final String channel, final boolean isSilent, final CommandArguments args) {
  81. if (args.getArguments().length == 0) {
  82. connection.getParser().get().sendRawMessage("TOPIC " + channel);
  83. } else {
  84. connection.getParser().get().sendRawMessage("TOPIC " + channel + " :" + args.
  85. getArgumentsAsString());
  86. }
  87. }
  88. }