001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.component.irc;
018    
019    import java.net.URI;
020    import java.util.Arrays;
021    
022    import org.apache.camel.RuntimeCamelException;
023    
024    public class IrcConfiguration implements Cloneable {
025        private String target;
026        private String hostname;
027        private String password;
028        private String nickname;
029        private String realname;
030        private String username;
031        private boolean persistent = true;
032        private boolean colors = true;
033        private boolean onNick = true;
034        private boolean onQuit = true;
035        private boolean onJoin = true;
036        private boolean onKick = true;
037        private boolean onMode = true;
038        private boolean onPart = true;
039        private boolean onTopic = true;
040        private boolean onPrivmsg = true;
041        private int[] ports = {6667, 6668, 6669};
042    
043        public IrcConfiguration() {
044        }
045    
046        public IrcConfiguration(String hostname, String nickname, String displayname, String target) {
047            this.target = target;
048            this.hostname = hostname;
049            this.nickname = nickname;
050            this.username = nickname;
051            this.realname = displayname;
052        }
053    
054        public IrcConfiguration(String hostname, String username, String password, String nickname, String displayname, String target) {
055            this.target = target;
056            this.hostname = hostname;
057            this.username = username;
058            this.password = password;
059            this.nickname = nickname;
060            this.realname = displayname;
061        }
062    
063        public IrcConfiguration copy() {
064            try {
065                return (IrcConfiguration) clone();
066            } catch (CloneNotSupportedException e) {
067                throw new RuntimeCamelException(e);
068            }
069        }
070    
071        public String getCacheKey() {
072            return hostname + ":" + nickname;
073        }
074    
075        public void configure(URI uri) {
076            // fix provided URI and handle that we can use # to indicate the IRC room
077    
078            String fixedUri = uri.toString();
079            if (!fixedUri.startsWith("irc://")) {
080                fixedUri = fixedUri.replace("irc:", "irc://");
081                uri = uri.resolve(fixedUri);
082            }
083    
084            setNickname(uri.getUserInfo());
085            setUsername(uri.getUserInfo());
086            setRealname(uri.getUserInfo());
087            setHostname(uri.getHost());
088    
089            if (uri.getFragment() == null || uri.getFragment().length() == 0) {
090                throw new RuntimeCamelException("The IRC channel name is required but not configured");
091            }
092    
093            setTarget("#" + uri.getFragment());
094        }
095    
096        public String getHostname() {
097            return hostname;
098        }
099    
100        public void setHostname(String hostname) {
101            this.hostname = hostname;
102        }
103    
104        public String getPassword() {
105            return password;
106        }
107    
108        public void setPassword(String password) {
109            this.password = password;
110        }
111    
112        public String getNickname() {
113            return nickname;
114        }
115    
116        public void setNickname(String nickname) {
117            this.nickname = nickname;
118        }
119    
120        public String getRealname() {
121            return realname;
122        }
123    
124        public void setRealname(String realname) {
125            this.realname = realname;
126        }
127    
128        public String getUsername() {
129            return username;
130        }
131    
132        public void setUsername(String username) {
133            this.username = username;
134        }
135    
136        public int[] getPorts() {
137            return ports;
138        }
139    
140        public void setPorts(int[] ports) {
141            this.ports = ports;
142        }
143    
144        public String getTarget() {
145            return target;
146        }
147    
148        public void setTarget(String target) {
149            this.target = target;
150        }
151    
152        public boolean isPersistent() {
153            return persistent;
154        }
155    
156        public void setPersistent(boolean persistent) {
157            this.persistent = persistent;
158        }
159    
160        public boolean isColors() {
161            return colors;
162        }
163    
164        public void setColors(boolean colors) {
165            this.colors = colors;
166        }
167    
168        public boolean isOnNick() {
169            return onNick;
170        }
171    
172        public void setOnNick(boolean onNick) {
173            this.onNick = onNick;
174        }
175    
176        public boolean isOnQuit() {
177            return onQuit;
178        }
179    
180        public void setOnQuit(boolean onQuit) {
181            this.onQuit = onQuit;
182        }
183    
184        public boolean isOnJoin() {
185            return onJoin;
186        }
187    
188        public void setOnJoin(boolean onJoin) {
189            this.onJoin = onJoin;
190        }
191    
192        public boolean isOnKick() {
193            return onKick;
194        }
195    
196        public void setOnKick(boolean onKick) {
197            this.onKick = onKick;
198        }
199    
200        public boolean isOnMode() {
201            return onMode;
202        }
203    
204        public void setOnMode(boolean onMode) {
205            this.onMode = onMode;
206        }
207    
208        public boolean isOnPart() {
209            return onPart;
210        }
211    
212        public void setOnPart(boolean onPart) {
213            this.onPart = onPart;
214        }
215    
216        public boolean isOnTopic() {
217            return onTopic;
218        }
219    
220        public void setOnTopic(boolean onTopic) {
221            this.onTopic = onTopic;
222        }
223    
224        public boolean isOnPrivmsg() {
225            return onPrivmsg;
226        }
227    
228        public void setOnPrivmsg(boolean onPrivmsg) {
229            this.onPrivmsg = onPrivmsg;
230        }
231    
232        public String toString() {
233            return "IrcConfiguration[hostname: " + hostname + ", ports=" + Arrays.toString(ports) + ", target: " + target + ", username=" + username + "]";
234        }
235    }