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