Last active
December 22, 2015 09:45
-
-
Save masahitojp/de7408dd6bda4e409b48 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package handlers.alias; | |
| import com.github.masahitojp.botan.Robot; | |
| import com.github.masahitojp.botan.handler.BotanMessageHandlers; | |
| import com.github.masahitojp.botan.message.BotanMessageSimple; | |
| import org.slf4j.Marker; | |
| import org.slf4j.MarkerFactory; | |
| import org.slf4j.Logger; | |
| import org.slf4j.LoggerFactory; | |
| import java.util.stream.Collectors; | |
| public class AliasHandlers implements BotanMessageHandlers { | |
| private static final Logger LOGGER = LoggerFactory.getLogger(AliasHandlers.class); | |
| private static final Marker MARKER = MarkerFactory.getMarker("AliasHandlers"); | |
| private static final String NAMESPACE = "alias."; | |
| @Override | |
| public void register(final Robot robot) { | |
| robot.respond( | |
| "alias (?<from>.+) (->|->) (?<to>.+)\\z", | |
| "Create alias message", | |
| message -> { | |
| final String from = message.getMatcher().group("from"); | |
| final String to = message.getMatcher().group("to"); | |
| message.getBrain().getData().put(NAMESPACE + from, to); | |
| message.reply(String.format("Registered alias: %s -> %s", from, to)); | |
| } | |
| ); | |
| robot.respond( | |
| "alias ls", | |
| "List aliases", | |
| message -> { | |
| final String result = message.getBrain().getData().entrySet().stream() | |
| .filter(e -> e.getKey().startsWith(NAMESPACE)) | |
| .map(e -> String.format("%s -> %s", e.getKey().replace(NAMESPACE, ""), e.getValue())) | |
| .collect(Collectors.joining("\n")); | |
| if (!result.equals("")) { | |
| message.reply(result); | |
| } else { | |
| message.reply("No alias registered"); | |
| } | |
| } | |
| ); | |
| robot.respond( | |
| "alias rm (?<from>.+)", | |
| "Delete alias", | |
| message -> { | |
| final String key = NAMESPACE + message.getMatcher().group("from"); | |
| if (message.getBrain().getData().containsKey(key)) { | |
| message.getBrain().getData().remove(key); | |
| message.reply("deleted"); | |
| } else { | |
| message.reply("Not found"); | |
| } | |
| } | |
| ); | |
| robot.respond( | |
| "(?<body>.+)", | |
| "Resolve alias if registered", | |
| message -> { | |
| final String from = message.getBody().replace(robot.getName() + " ", ""); | |
| final String key = NAMESPACE + from; | |
| if (message.getBrain().getData().containsKey(key)) { | |
| final String aliased = message.getBrain().getData().get(key); | |
| final String body = String.format("%s %s", robot.getName(), aliased); | |
| robot.receive( | |
| new BotanMessageSimple(body, message.getFrom(), message.getFromName(), message.getTo(), message.getType()) | |
| ); | |
| } | |
| } | |
| ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment