]> git.somenet.org - irc/pjirc-ng.git/blob - src/main/java/irc/Status.java
Pjirc 2.2.1 as available on the net, reformatted and made it compile.
[irc/pjirc-ng.git] / src / main / java / irc / Status.java
1 /*****************************************************/
2 /*          This java file is a part of the          */
3 /*                                                   */
4 /*           -  Plouf's Java IRC Client  -           */
5 /*                                                   */
6 /*   Copyright (C)  2002 - 2005 Philippe Detournay   */
7 /*                                                   */
8 /*         All contacts : theplouf@yahoo.com         */
9 /*                                                   */
10 /*  PJIRC is free software; you can redistribute     */
11 /*  it and/or modify it under the terms of the GNU   */
12 /*  General Public License as published by the       */
13 /*  Free Software Foundation; version 2 or later of  */
14 /*  the License.                                     */
15 /*                                                   */
16 /*  PJIRC is distributed in the hope that it will    */
17 /*  be useful, but WITHOUT ANY WARRANTY; without     */
18 /*  even the implied warranty of MERCHANTABILITY or  */
19 /*  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU   */
20 /*  General Public License for more details.         */
21 /*                                                   */
22 /*  You should have received a copy of the GNU       */
23 /*  General Public License along with PJIRC; if      */
24 /*  not, write to the Free Software Foundation,      */
25 /*  Inc., 59 Temple Place, Suite 330, Boston,        */
26 /*  MA  02111-1307  USA                              */
27 /*                                                   */
28 /*****************************************************/
29
30 package irc;
31
32 /**
33  * The status.
34  */
35 public class Status extends IRCSource implements ReplyServerListener {
36         private ListenerGroup _listeners;
37
38         /**
39          * Create a new Status.
40          * 
41          * @param config
42          *          global irc configuration.
43          * @param s
44          *          server.
45          */
46         public Status(IRCConfiguration config, IRCServer s) {
47                 super(config, s);
48                 s.addReplyServerListener(this);
49                 _listeners = new ListenerGroup();
50                 setInterpretor(new StatusInterpretor(config));
51         }
52
53         @Override
54         public void release() {
55                 ((IRCServer) _server).removeReplyServerListener(this);
56                 super.release();
57         }
58
59         @Override
60         public String getType() {
61                 return "Status";
62         }
63
64         @Override
65         public String getName() {
66                 return getServerName();
67         }
68
69         /**
70          * Get the server name.
71          * 
72          * @return the server name.
73          */
74         public String getServerName() {
75                 return getIRCServer().getServerName();
76         }
77
78         @Override
79         public boolean talkable() {
80                 return false;
81         }
82
83         @Override
84         public void leave() {
85                 if (!_ircConfiguration.getB("multiserver"))
86                         return;
87
88                 // sendString("/quit");
89                 getIRCServer().leaveStatus(getName());
90         }
91
92         /**
93          * Get this status nick.
94          * 
95          * @return status nick.
96          */
97         public String getNick() {
98                 return _server.getNick();
99         }
100
101         /**
102          * Get this status mode.
103          * 
104          * @return status mode.
105          */
106         public String getMode() {
107                 return getIRCServer().getMode();
108         }
109
110         /**
111          * Add listener.
112          * 
113          * @param lis
114          *          listener to add.
115          */
116         public void addStatusListener(StatusListener lis) {
117                 _listeners.addListener(lis);
118         }
119
120         /**
121          * Remove listener.
122          * 
123          * @param lis
124          *          listener to remove.
125          */
126         public void removeStatusListener(StatusListener lis) {
127                 _listeners.removeListener(lis);
128         }
129
130         /**
131          * Notify this status that the nick has changed.
132          * 
133          * @param nick
134          *          new nick.
135          */
136         public void nickChanged(String nick) {
137                 _listeners.sendEvent("nickChanged", nick, this);
138         }
139
140         /**
141          * Notify this status that the mode has changed.
142          * 
143          * @param mode
144          *          new mode.
145          */
146         public void modeChanged(String mode) {
147                 _listeners.sendEvent("modeChanged", mode, this);
148         }
149
150         /**
151          * We've been invited on a channel.
152          * 
153          * @param channel
154          *          channel we're invited to.
155          * @param who
156          *          nickname who invited us.
157          */
158         public void invited(String channel, String who) {
159                 _listeners.sendEvent("invited", channel, who, this);
160         }
161
162         @Override
163         public Boolean replyReceived(String prefix, String id, String params[], IRCServer server) {
164                 if (id.equals("322"))
165                         return Boolean.FALSE; // chanlist
166                 if (_ircConfiguration.getB("useinfo")) {
167                         int i = new Integer(id).intValue();
168                         if ((i >= 300) && (i != 372))
169                                 return Boolean.FALSE;
170                 }
171                 if (id.equals("401")) // no such nick/channel
172                 {
173                         Source src = getIRCServer().getDefaultSource();
174                         String toSend = "";
175                         for (int i = 1; i < params.length; i++)
176                                 toSend += " " + params[i];
177                         toSend = toSend.substring(1);
178                         if (src != null)
179                                 src.report(toSend);
180                 } else if (id.equals("317")) // idle time
181                 {
182                         if (params.length > 3) {
183                                 String time = params[2];
184                                 long tme = Long.parseLong(time);
185                                 long seconds = (tme) % 60;
186                                 long mins = (tme / (60)) % 60;
187                                 long hours = (tme / (60 * 60)) % 24;
188                                 long days = (tme / (60 * 60 * 24)) % 7;
189                                 long weeks = (tme / (60 * 60 * 24 * 7));
190
191                                 String res = "";
192
193                                 if (tme > (60 * 60 * 24 * 7))
194                                         res += weeks + " weeks ";
195                                 if (tme > (60 * 60 * 24))
196                                         res += days + " days ";
197                                 if (tme > (60 * 60))
198                                         res += hours + " hours ";
199                                 if (tme > (60))
200                                         res += mins + " minutes ";
201                                 res += seconds + " seconds";
202                                 res = res.trim();
203
204                                 String signon = new java.util.Date(1000 * Long.parseLong(params[3])).toString();
205
206                                 report(_ircConfiguration.getText(IRCTextProvider.REPLY_IDLE, params[1], res));
207                                 report(_ircConfiguration.getText(IRCTextProvider.REPLY_SIGNON, params[1], signon));
208                         }
209
210                 } else {
211                         // Source src=getIRCServer().getDefaultSource();
212                         String toSend = "";
213                         for (int i = 1; i < params.length; i++)
214                                 toSend += " " + params[i];
215                         toSend = toSend.substring(1);
216                         /* if(src!=null) src. */report(toSend);
217                 }
218                 return Boolean.FALSE;
219         }
220 }