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.net.URISyntaxException;
021    import java.util.Arrays;
022    
023    import org.apache.camel.RuntimeCamelException;
024    import org.apache.camel.util.ObjectHelper;
025    import org.schwering.irc.lib.ssl.SSLDefaultTrustManager;
026    import org.schwering.irc.lib.ssl.SSLTrustManager;
027    
028    public class IrcConfiguration implements Cloneable {
029        private String target;
030        private String hostname;
031        private String password;
032        private String nickname;
033        private String realname;
034        private String username;
035        private SSLTrustManager trustManager = new SSLDefaultTrustManager();
036        private boolean usingSSL;
037        private boolean persistent = true;
038        private boolean colors = true;
039        private boolean onNick = true;
040        private boolean onQuit = true;
041        private boolean onJoin = true;
042        private boolean onKick = true;
043        private boolean onMode = true;
044        private boolean onPart = true;
045        private boolean onTopic = true;
046        private boolean onPrivmsg = true;
047        private int[] ports = {6667, 6668, 6669};
048        
049        public IrcConfiguration() {
050        }
051    
052        public IrcConfiguration(String hostname, String nickname, String displayname, String target) {
053            this.target = target;
054            this.hostname = hostname;
055            this.nickname = nickname;
056            this.username = nickname;
057            this.realname = displayname;
058        }
059    
060        public IrcConfiguration(String hostname, String username, String password, String nickname, String displayname, String target) {
061            this.target = target;
062            this.hostname = hostname;
063            this.username = username;
064            this.password = password;
065            this.nickname = nickname;
066            this.realname = displayname;
067        }
068    
069        public IrcConfiguration copy() {
070            try {
071                return (IrcConfiguration) clone();
072            } catch (CloneNotSupportedException e) {
073                throw new RuntimeCamelException(e);
074            }
075        }
076    
077        public String getCacheKey() {
078            return hostname + ":" + nickname;
079        }
080    
081        public void configure(String uriStr) throws URISyntaxException {
082            // fix provided URI and handle that we can use # to indicate the IRC room
083            if (uriStr.startsWith("ircs")) {
084                setUsingSSL(true);
085                if (!uriStr.startsWith("ircs://")) {
086                    uriStr = uriStr.replace("ircs:", "ircs://");
087                }
088            } else if (!uriStr.startsWith("irc://")) {
089                uriStr = uriStr.replace("irc:", "irc://");
090            }
091            
092            if (uriStr.contains("?")) {
093                uriStr = ObjectHelper.before(uriStr, "?");
094            }
095            
096            URI uri = new URI(uriStr);
097    
098            setNickname(uri.getUserInfo());
099            setUsername(uri.getUserInfo());
100            setRealname(uri.getUserInfo());
101            setHostname(uri.getHost());
102    
103            if (uri.getFragment() == null || uri.getFragment().length() == 0) {
104                throw new RuntimeCamelException("The IRC channel name is required but not configured");
105            }
106    
107            String channel = uri.getFragment();
108    
109            setTarget("#" + channel);
110        }
111    
112        public void setTrustManager(SSLTrustManager trustManager) {
113            this.trustManager = trustManager;
114        }
115    
116        public SSLTrustManager getTrustManager() {
117            return trustManager;
118        }
119    
120        public boolean getUsingSSL() {
121            return usingSSL;
122        }
123    
124        private void setUsingSSL(boolean usingSSL) {
125            this.usingSSL = usingSSL;
126        }
127    
128        public String getHostname() {
129            return hostname;
130        }
131    
132        public void setHostname(String hostname) {
133            this.hostname = hostname;
134        }
135    
136        public String getPassword() {
137            return password;
138        }
139    
140        public void setPassword(String password) {
141            this.password = password;
142        }
143    
144        public String getNickname() {
145            return nickname;
146        }
147    
148        public void setNickname(String nickname) {
149            this.nickname = nickname;
150        }
151    
152        public String getRealname() {
153            return realname;
154        }
155    
156        public void setRealname(String realname) {
157            this.realname = realname;
158        }
159    
160        public String getUsername() {
161            return username;
162        }
163    
164        public void setUsername(String username) {
165            this.username = username;
166        }
167    
168        public int[] getPorts() {
169            return ports;
170        }
171    
172        public void setPorts(int[] ports) {
173            this.ports = ports;
174        }
175    
176        public String getTarget() {
177            return target;
178        }
179    
180        public void setTarget(String target) {
181            this.target = target;
182        }
183    
184        public boolean isPersistent() {
185            return persistent;
186        }
187    
188        public void setPersistent(boolean persistent) {
189            this.persistent = persistent;
190        }
191    
192        public boolean isColors() {
193            return colors;
194        }
195    
196        public void setColors(boolean colors) {
197            this.colors = colors;
198        }
199    
200        public boolean isOnNick() {
201            return onNick;
202        }
203    
204        public void setOnNick(boolean onNick) {
205            this.onNick = onNick;
206        }
207    
208        public boolean isOnQuit() {
209            return onQuit;
210        }
211    
212        public void setOnQuit(boolean onQuit) {
213            this.onQuit = onQuit;
214        }
215    
216        public boolean isOnJoin() {
217            return onJoin;
218        }
219    
220        public void setOnJoin(boolean onJoin) {
221            this.onJoin = onJoin;
222        }
223    
224        public boolean isOnKick() {
225            return onKick;
226        }
227    
228        public void setOnKick(boolean onKick) {
229            this.onKick = onKick;
230        }
231    
232        public boolean isOnMode() {
233            return onMode;
234        }
235    
236        public void setOnMode(boolean onMode) {
237            this.onMode = onMode;
238        }
239    
240        public boolean isOnPart() {
241            return onPart;
242        }
243    
244        public void setOnPart(boolean onPart) {
245            this.onPart = onPart;
246        }
247    
248        public boolean isOnTopic() {
249            return onTopic;
250        }
251    
252        public void setOnTopic(boolean onTopic) {
253            this.onTopic = onTopic;
254        }
255    
256        public boolean isOnPrivmsg() {
257            return onPrivmsg;
258        }
259    
260        public void setOnPrivmsg(boolean onPrivmsg) {
261            this.onPrivmsg = onPrivmsg;
262        }
263    
264        public String toString() {
265            return "IrcConfiguration[hostname: " + hostname + ", ports=" + Arrays.toString(ports) + ", target: " + target + ", username=" + username + "]";
266        }
267    }