123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- /*
- * Copyright (c) 2006-2017 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.parser.irc;
-
- import com.dmdirc.parser.common.CallbackManager;
- import com.dmdirc.parser.events.NumericEvent;
- import com.dmdirc.parser.events.UserInfoEvent;
- import com.dmdirc.parser.events.UserInfoEvent.UserInfoType;
- import com.dmdirc.parser.interfaces.Parser;
-
- import java.time.LocalDateTime;
- import java.util.EnumMap;
- import java.util.Map;
-
- import javax.annotation.Nullable;
-
- import net.engio.mbassy.listener.Handler;
-
- /**
- * Monitors for whois responses and raises a {@link UserInfoEvent} with the results.
- */
- public class WhoisResponseHandler {
-
- private final Parser parser;
- private final CallbackManager manager;
-
- private final Map<UserInfoType, String> info = new EnumMap<>(UserInfoType.class);
-
- @Nullable private String client;
-
- public WhoisResponseHandler(final Parser parser, final CallbackManager manager) {
- this.parser = parser;
- this.manager = manager;
- }
-
- public void start() {
- manager.subscribe(this);
- }
-
- public void stop() {
- manager.unsubscribe(this);
- }
-
- @Handler(condition = "msg.numeric == 311")
- void handleStartOfWhois(final NumericEvent event) {
- client = event.getToken()[3];
- info.clear();
-
- // :server 311 DMDirc User ~Ident host.dmdirc.com * :Real name
- info.put(UserInfoType.ADDRESS,
- event.getToken()[3] + '!' + event.getToken()[4] + '@' + event.getToken()[5]);
- info.put(UserInfoType.REAL_NAME, event.getToken()[7]);
- }
-
- @Handler(condition = "msg.numeric == 318")
- void handleEndOfWhois(final NumericEvent event) {
- if (client != null) {
- sendEvent();
- client = null;
- }
- }
-
- @Handler(condition = "msg.numeric == 301")
- void handleAwayMessage(final NumericEvent event) {
- // :server 301 DMDirc User :away message
- info.put(UserInfoType.AWAY_MESSAGE, event.getToken()[4]);
- }
-
- @Handler(condition = "msg.numeric == 312")
- void handleServerInfo(final NumericEvent event) {
- // :server 312 DMDirc User *.quakenet.org :QuakeNet IRC Server
- info.put(UserInfoType.SERVER_NAME, event.getToken()[4]);
- info.put(UserInfoType.SERVER_INFO, event.getToken()[5]);
- }
-
- @Handler(condition = "msg.numeric == 313")
- void handleUserPrivileges(final NumericEvent event) {
- // :server 313 DMDirc User :is an IRC Operator
- info.put(UserInfoType.SERVER_OPER, event.getToken()[4]);
- }
-
- @Handler(condition = "msg.numeric == 378")
- void handleConnectingFrom(final NumericEvent event) {
- // :server 378 DMDirc User :is connecting from *@hostname.tld xx.xx.xx.xx
- if (client.equalsIgnoreCase(event.getToken()[3])) {
- info.put(UserInfoType.REAL_ADDRESS, event.getToken()[4]);
- }
- }
-
- @Handler(condition = "msg.numeric == 671")
- void handleSecureConnection(final NumericEvent event) {
- // :server 671 DMDirc User :is using a secure connection
- info.put(UserInfoType.CONNECTION_SECURITY, event.getToken()[4]);
- }
-
- @Handler(condition = "msg.numeric == 319")
- void handleChannelList(final NumericEvent event) {
- // :server 319 DMDirc User :@#channel1 +#channel2 ...
- info.put(UserInfoType.GROUP_CHAT_LIST, event.getToken()[4]);
- }
-
- @Handler(condition = "msg.numeric == 317")
- void handleIdleTime(final NumericEvent event) {
- // :server 317 DMDirc User 305 1422561556 :seconds idle, signon time
- info.put(UserInfoType.IDLE_TIME, event.getToken()[4]);
- info.put(UserInfoType.CONNECTION_TIME, event.getToken()[5]);
- }
-
- @Handler(condition = "msg.numeric == 330")
- void handleAccount(final NumericEvent event) {
- // :server 330 DMDirc User Account :is authed as
- info.put(UserInfoType.ACCOUNT_NAME, event.getToken()[4]);
- }
-
- private void sendEvent() {
- manager.publish(new UserInfoEvent(parser, LocalDateTime.now(),
- parser.getClient(client), info));
- }
-
- }
|