001package com.github.theholywaffle.teamspeak3;
002
003/*
004 * #%L
005 * TeamSpeak 3 Java API
006 * %%
007 * Copyright (C) 2014 Bert De Geyter
008 * %%
009 * Permission is hereby granted, free of charge, to any person obtaining a copy
010 * of this software and associated documentation files (the "Software"), to deal
011 * in the Software without restriction, including without limitation the rights
012 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
013 * copies of the Software, and to permit persons to whom the Software is
014 * furnished to do so, subject to the following conditions:
015 * 
016 * The above copyright notice and this permission notice shall be included in
017 * all copies or substantial portions of the Software.
018 * 
019 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
020 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
021 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
022 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
023 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
024 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
025 * THE SOFTWARE.
026 * #L%
027 */
028
029import com.github.theholywaffle.teamspeak3.api.event.*;
030import com.github.theholywaffle.teamspeak3.api.exception.TS3UnknownEventException;
031import com.github.theholywaffle.teamspeak3.commands.response.DefaultArrayResponse;
032
033import java.util.LinkedList;
034import java.util.List;
035import java.util.Map;
036
037public class EventManager {
038
039        private final List<TS3Listener> listeners = new LinkedList<>();
040
041        public void addListeners(TS3Listener... listeners) {
042                for (final TS3Listener l : listeners) {
043                        this.listeners.add(l);
044                }
045        }
046
047        public void removeListeners(TS3Listener... listeners) {
048                for (final TS3Listener l : listeners) {
049                        this.listeners.remove(l);
050                }
051        }
052
053        public void fireEvent(String notifyName, String notifyBody) {
054                TS3Event event = createEvent(notifyName, notifyBody);
055
056                for (final TS3Listener listener : listeners) {
057                        event.fire(listener);
058                }
059        }
060
061        private TS3Event createEvent(String notifyName, String notifyBody) {
062                final DefaultArrayResponse response = new DefaultArrayResponse(notifyBody);
063                final Map<String, String> eventData = response.getFirstResponse().getMap();
064
065                switch (notifyName) {
066                        case "notifytextmessage":
067                                return new TextMessageEvent(eventData);
068                        case "notifycliententerview":
069                                return new ClientJoinEvent(eventData);
070                        case "notifyclientleftview":
071                                return new ClientLeaveEvent(eventData);
072                        case "notifyserveredited":
073                                return new ServerEditedEvent(eventData);
074                        case "notifychanneledited":
075                                return new ChannelEditedEvent(eventData);
076                        case "notifychanneldescriptionchanged":
077                                return new ChannelDescriptionEditedEvent(eventData);
078                        case "notifyclientmoved":
079                                return new ClientMovedEvent(eventData);
080                        case "notifychannelcreated":
081                                return new ChannelCreateEvent(eventData);
082                        case "notifychanneldeleted":
083                                return new ChannelDeletedEvent(eventData);
084                        case "notifychannelmoved":
085                                return new ChannelMovedEvent(eventData);
086                        case "notifychannelpasswordchanged":
087                                return new ChannelPasswordChangedEvent(eventData);
088                        case "notifytokenused":
089                                return new PrivilegeKeyUsedEvent(eventData);
090                        default:
091                                throw new TS3UnknownEventException(notifyName + " " + notifyBody);
092                }
093        }
094}