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    
021    import org.apache.camel.RuntimeCamelException;
022    import org.apache.camel.util.ObjectHelper;
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            String channel = uri.getFragment();
094    
095            if (channel.contains("?")) {
096                //Need to strip off the query string from this fragment
097                channel = ObjectHelper.before(uri.getFragment(), "?");
098            }
099    
100            setTarget("#" + channel);
101        }
102    
103        public String getHostname() {
104            return hostname;
105        }
106    
107        public void setHostname(String hostname) {
108            this.hostname = hostname;
109        }
110    
111        public String getPassword() {
112            return password;
113        }
114    
115        public void setPassword(String password) {
116            this.password = password;
117        }
118    
119        public String getNickname() {
120            return nickname;
121        }
122    
123        public void setNickname(String nickname) {
124            this.nickname = nickname;
125        }
126    
127        public String getRealname() {
128            return realname;
129        }
130    
131        public void setRealname(String realname) {
132            this.realname = realname;
133        }
134    
135        public String getUsername() {
136            return username;
137        }
138    
139        public void setUsername(String username) {
140            this.username = username;
141        }
142    
143        public int[] getPorts() {
144            return ports;
145        }
146    
147        public void setPorts(int[] ports) {
148            this.ports = ports;
149        }
150    
151        public String getTarget() {
152            return target;
153        }
154    
155        public void setTarget(String target) {
156            this.target = target;
157        }
158    
159        public boolean isPersistent() {
160            return persistent;
161        }
162    
163        public void setPersistent(boolean persistent) {
164            this.persistent = persistent;
165        }
166    
167        public boolean isColors() {
168            return colors;
169        }
170    
171        public void setColors(boolean colors) {
172            this.colors = colors;
173        }
174    
175        public boolean isOnNick() {
176            return onNick;
177        }
178    
179        public void setOnNick(boolean onNick) {
180            this.onNick = onNick;
181        }
182    
183        public boolean isOnQuit() {
184            return onQuit;
185        }
186    
187        public void setOnQuit(boolean onQuit) {
188            this.onQuit = onQuit;
189        }
190    
191        public boolean isOnJoin() {
192            return onJoin;
193        }
194    
195        public void setOnJoin(boolean onJoin) {
196            this.onJoin = onJoin;
197        }
198    
199        public boolean isOnKick() {
200            return onKick;
201        }
202    
203        public void setOnKick(boolean onKick) {
204            this.onKick = onKick;
205        }
206    
207        public boolean isOnMode() {
208            return onMode;
209        }
210    
211        public void setOnMode(boolean onMode) {
212            this.onMode = onMode;
213        }
214    
215        public boolean isOnPart() {
216            return onPart;
217        }
218    
219        public void setOnPart(boolean onPart) {
220            this.onPart = onPart;
221        }
222    
223        public boolean isOnTopic() {
224            return onTopic;
225        }
226    
227        public void setOnTopic(boolean onTopic) {
228            this.onTopic = onTopic;
229        }
230    
231        public boolean isOnPrivmsg() {
232            return onPrivmsg;
233        }
234    
235        public void setOnPrivmsg(boolean onPrivmsg) {
236            this.onPrivmsg = onPrivmsg;
237        }
238    
239        public String toString() {
240            return "IrcConfiguration{" + "target='" + target + '\'' + ", hostname='" + hostname + '\'' + ", password='" + password + '\'' + ", nickname='" + nickname + '\'' + ", realname='" + realname
241                   + '\'' + ", username='" + username + '\'' + ", persistent=" + persistent + ", colors=" + colors + ", onNick=" + onNick + ", onQuit=" + onQuit + ", onJoin=" + onJoin + ", onKick="
242                   + onKick + ", onMode=" + onMode + ", onPart=" + onPart + ", onTopic=" + onTopic + ", onPrivmsg=" + onPrivmsg + ", ports=" + ports + '}';
243        }
244    }