123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- /*
- * Copyright (c) 2006-2015 DMDirc Developers
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
- package com.dmdirc.ui.messages;
-
- import com.dmdirc.events.ChannelHighlightEvent;
- import com.dmdirc.events.ChannelMessageEvent;
- import com.dmdirc.events.DisplayProperty;
- import com.dmdirc.events.QueryHighlightEvent;
- import com.dmdirc.events.QueryMessageEvent;
- import com.dmdirc.events.ServerConnectedEvent;
- import com.dmdirc.events.ServerNickChangeEvent;
- import com.dmdirc.interfaces.User;
- import com.dmdirc.interfaces.WindowModel;
-
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.Optional;
- import java.util.regex.Pattern;
-
- import net.engio.mbassy.listener.Handler;
-
- /**
- * Looks for highlights in messages.
- */
- public class HighlightManager {
-
- private static final String REGEX_TEPLATE = "(?i).*(\\p{Space}|^|\\p{Punct})\\Q%s\\E" +
- "(\\p{Space}|\\p{Punct}|$).*";
-
- private final Collection<Pattern> patterns = new ArrayList<>();
- private final WindowModel serverWindow;
-
- private Optional<Pattern> nicknamePattern = Optional.empty();
-
- public HighlightManager(final WindowModel serverWindow) {
- this.serverWindow = serverWindow;
- }
-
- @Handler(rejectSubtypes = true)
- void handleChannelMessage(final ChannelMessageEvent event) {
- if (event.getChannel().getConnection().get().getWindowModel().equals(serverWindow)
- && patterns.stream().anyMatch(p -> p.matcher(event.getMessage()).matches())) {
- event.setDisplayProperty(DisplayProperty.DO_NOT_DISPLAY, true);
- event.getChannel().getEventBus().publishAsync(
- new ChannelHighlightEvent(
- event.getTimestamp(), event.getChannel(), event.getClient(),
- event.getMessage()));
- }
- }
-
- @Handler(rejectSubtypes = true)
- void handleQueryMessage(final QueryMessageEvent event) {
- if (event.getUser().getConnection().getWindowModel().equals(serverWindow)
- && patterns.stream().anyMatch(p -> p.matcher(event.getMessage()).matches())) {
- event.setDisplayProperty(DisplayProperty.DO_NOT_DISPLAY, true);
- event.getQuery().getEventBus().publishAsync(
- new QueryHighlightEvent(
- event.getTimestamp(), event.getQuery(), event.getUser(),
- event.getMessage()));
- }
- }
-
- @Handler
- void handleNickChange(final ServerNickChangeEvent event) {
- if (event.getConnection().getWindowModel().equals(serverWindow)) {
- setNickname(event.getNewNick());
- }
- }
-
- @Handler
- void handleConnected(final ServerConnectedEvent event) {
- if (event.getConnection().getWindowModel().equals(serverWindow)) {
- patterns.clear();
-
- event.getConnection().getProfile().getHighlights()
- .stream()
- .map(this::compile)
- .forEach(patterns::add);
-
- event.getConnection().getLocalUser()
- .map(User::getNickname)
- .ifPresent(this::setNickname);
- }
- }
-
- private void setNickname(final String newNick) {
- nicknamePattern.ifPresent(patterns::remove);
- nicknamePattern = Optional.of(compile(newNick));
- patterns.add(nicknamePattern.get());
- }
-
- private Pattern compile(final String word) {
- return Pattern.compile(String.format(REGEX_TEPLATE, word));
- }
-
- }
|