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.ArrayList;
022    import java.util.Arrays;
023    import java.util.Dictionary;
024    import java.util.Hashtable;
025    import java.util.List;
026    
027    import org.apache.camel.RuntimeCamelException;
028    import org.apache.camel.util.ObjectHelper;
029    import org.schwering.irc.lib.ssl.SSLDefaultTrustManager;
030    import org.schwering.irc.lib.ssl.SSLTrustManager;
031    
032    public class IrcConfiguration implements Cloneable {
033        private String target;
034        private List<String> channels = new ArrayList<String>();
035        private Dictionary<String, String> keys = new Hashtable<String, String>();
036        private String hostname;
037        private String password;
038        private String nickname;
039        private String realname;
040        private String username;
041        private SSLTrustManager trustManager = new SSLDefaultTrustManager();
042        private boolean usingSSL;
043        private boolean persistent = true;
044        private boolean colors = true;
045        private boolean onNick = true;
046        private boolean onQuit = true;
047        private boolean onJoin = true;
048        private boolean onKick = true;
049        private boolean onMode = true;
050        private boolean onPart = true;
051        private boolean onReply;
052        private boolean onTopic = true;
053        private boolean onPrivmsg = true;
054        private boolean autoRejoin = true;
055        private int[] ports = {6667, 6668, 6669};
056    
057        /*
058         * Temporary storage for when keys are listed in the parameters before channels.
059         */
060        private String channelKeys;
061    
062        public IrcConfiguration() {
063        }
064    
065        public IrcConfiguration(String hostname, String nickname, String displayname, List<String> channels) {
066            this.channels = channels;
067            this.hostname = hostname;
068            this.nickname = nickname;
069            this.username = nickname;
070            this.realname = displayname;
071        }
072    
073        public IrcConfiguration(String hostname, String username, String password, String nickname, String displayname, List<String> channels) {
074            this.channels = channels;
075            this.hostname = hostname;
076            this.username = username;
077            this.password = password;
078            this.nickname = nickname;
079            this.realname = displayname;
080        }
081    
082        public IrcConfiguration copy() {
083            try {
084                return (IrcConfiguration) clone();
085            } catch (CloneNotSupportedException e) {
086                throw new RuntimeCamelException(e);
087            }
088        }
089    
090        public String getCacheKey() {
091            return hostname + ":" + nickname;
092        }
093    
094        public String getListOfChannels() {
095            String retval = "";
096            for (String channel : channels) {
097                retval += channel + " ";
098            }
099            return retval.trim();
100        }
101    
102        public void configure(String uriStr) throws URISyntaxException {
103            // fix provided URI and handle that we can use # to indicate the IRC room
104            if (uriStr.startsWith("ircs")) {
105                setUsingSSL(true);
106                if (!uriStr.startsWith("ircs://")) {
107                    uriStr = uriStr.replace("ircs:", "ircs://");
108                }
109            } else if (!uriStr.startsWith("irc://")) {
110                uriStr = uriStr.replace("irc:", "irc://");
111            }
112    
113            if (uriStr.contains("?")) {
114                uriStr = ObjectHelper.before(uriStr, "?");
115            }
116    
117            URI uri = new URI(uriStr);
118    
119            setNickname(uri.getUserInfo());
120            setUsername(uri.getUserInfo());
121            setRealname(uri.getUserInfo());
122            setHostname(uri.getHost());
123    
124            if (uri.getFragment() != null && uri.getFragment().length() != 0) {
125                String channel = "#" + uri.getFragment();
126                addChannel(channel);
127            }
128        }
129    
130        public void addChannel(String channel) {
131            boolean alreadyHave = false;
132            for (String aChannel : channels) {
133                if (channel.contentEquals(aChannel)) {
134                    alreadyHave = true;
135                }
136            }
137            if (!alreadyHave) {
138                channels.add(channel);
139            }
140        }
141    
142        public void setChannels(String channels) {
143            String[] args = channels.split(",");
144    
145            for (String channel : args) {
146                channel = channel.trim();
147                if (channel.startsWith("#")) {
148                    addChannel(channel);
149                }
150            }
151    
152            if (keys.size() == 0 && channelKeys != null) {
153                setKeys(channelKeys);
154            }
155        }
156    
157        public void setKeys(String keys) {
158            if (channels.size() == 0) {
159                // keys are listed in the parameters before channels
160                // store the string and process after channels
161                channelKeys = keys;
162            } else {
163                String[] s = keys.split(",");
164                int index = 0;
165                for (String key : s) {
166                    this.keys.put(channels.get(index), key);
167                    index++;
168                }
169            }
170        }
171    
172        public String getKey(String channel) {
173            return keys.get(channel);
174        }
175    
176        public Dictionary<String, String> getKeys() {
177            return keys;
178        }
179    
180        public void setTrustManager(SSLTrustManager trustManager) {
181            this.trustManager = trustManager;
182        }
183    
184        public SSLTrustManager getTrustManager() {
185            return trustManager;
186        }
187    
188        public boolean getUsingSSL() {
189            return usingSSL;
190        }
191    
192        private void setUsingSSL(boolean usingSSL) {
193            this.usingSSL = usingSSL;
194        }
195    
196        public String getHostname() {
197            return hostname;
198        }
199    
200        public void setHostname(String hostname) {
201            this.hostname = hostname;
202        }
203    
204        public String getPassword() {
205            return password;
206        }
207    
208        public void setPassword(String password) {
209            this.password = password;
210        }
211    
212        public String getNickname() {
213            return nickname;
214        }
215    
216        public void setNickname(String nickname) {
217            this.nickname = nickname;
218        }
219    
220        public String getRealname() {
221            return realname;
222        }
223    
224        public void setRealname(String realname) {
225            this.realname = realname;
226        }
227    
228        public String getUsername() {
229            return username;
230        }
231    
232        public void setUsername(String username) {
233            this.username = username;
234        }
235    
236        public int[] getPorts() {
237            return ports;
238        }
239    
240        public void setPorts(int[] ports) {
241            this.ports = ports;
242        }
243    
244        public List<String> getChannels() {
245            return channels;
246        }
247    
248        public boolean isPersistent() {
249            return persistent;
250        }
251    
252        public void setPersistent(boolean persistent) {
253            this.persistent = persistent;
254        }
255    
256        public boolean isColors() {
257            return colors;
258        }
259    
260        public void setColors(boolean colors) {
261            this.colors = colors;
262        }
263    
264        public boolean isOnNick() {
265            return onNick;
266        }
267    
268        public void setOnNick(boolean onNick) {
269            this.onNick = onNick;
270        }
271    
272        public boolean isOnQuit() {
273            return onQuit;
274        }
275    
276        public void setOnQuit(boolean onQuit) {
277            this.onQuit = onQuit;
278        }
279    
280        public boolean isOnJoin() {
281            return onJoin;
282        }
283    
284        public void setOnJoin(boolean onJoin) {
285            this.onJoin = onJoin;
286        }
287    
288        public boolean isOnKick() {
289            return onKick;
290        }
291    
292        public void setOnKick(boolean onKick) {
293            this.onKick = onKick;
294        }
295    
296        public boolean isOnMode() {
297            return onMode;
298        }
299    
300        public void setOnMode(boolean onMode) {
301            this.onMode = onMode;
302        }
303    
304        public boolean isOnPart() {
305            return onPart;
306        }
307    
308        public void setOnPart(boolean onPart) {
309            this.onPart = onPart;
310        }
311    
312        public boolean isOnReply() {
313            return onReply;
314        }
315    
316        public void setOnReply(boolean onReply) {
317            this.onReply = onReply;
318        }
319    
320        public boolean isOnTopic() {
321            return onTopic;
322        }
323    
324        public void setOnTopic(boolean onTopic) {
325            this.onTopic = onTopic;
326        }
327    
328        public boolean isOnPrivmsg() {
329            return onPrivmsg;
330        }
331    
332        public void setOnPrivmsg(boolean onPrivmsg) {
333            this.onPrivmsg = onPrivmsg;
334        }
335    
336        public boolean isAutoRejoin() {
337            return autoRejoin;
338        }
339    
340        public void setAutoRejoin(boolean autoRejoin) {
341            this.autoRejoin = autoRejoin;
342        }
343    
344        public String toString() {
345            return "IrcConfiguration[hostname: " + hostname + ", ports=" + Arrays.toString(ports) + ", target: " + target + ", username=" + username + "]";
346        }
347    }