1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
|
/*
* Jitsi, the OpenSource Java VoIP and Instant Messaging client.
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package net.java.sip.communicator.impl.protocol.irc;
import java.util.*;
import com.ircclouds.irc.api.Callback;
import com.ircclouds.irc.api.IRCApi;
import com.ircclouds.irc.api.IRCApiImpl;
import com.ircclouds.irc.api.IServerParameters;
import com.ircclouds.irc.api.domain.IRCServer;
import com.ircclouds.irc.api.state.IIRCState;
import net.java.sip.communicator.service.protocol.*;
import net.java.sip.communicator.util.*;
/**
* An implementation of the PircBot IRC stack.
*/
public class IrcStack
{
private static final Logger LOGGER = Logger.getLogger(IrcStack.class);
private final ProtocolProviderServiceIrcImpl provider;
private final IRCApi irc = new IRCApiImpl(true);
private final ServerParameters params;
private IIRCState connectionState;
public IrcStack(final ProtocolProviderServiceIrcImpl parentProvider, final String nick, final String login, final String version, final String finger)
{
if (parentProvider == null)
{
throw new NullPointerException("parentProvider cannot be null");
}
this.provider = parentProvider;
this.params = new IrcStack.ServerParameters(nick, "", finger, null);
}
public boolean isConnected()
{
return (this.connectionState != null && this.connectionState.isConnected());
}
public void connect(String host, int port, String password, boolean autoNickChange)
{
this.params.setServer(new IRCServer(host, port, password, false));
synchronized(this.irc)
{
// start connecting to the specified server ...
this.irc.connect(this.params, new Callback<IIRCState>()
{
@Override
public void onSuccess(IIRCState state)
{
synchronized(IrcStack.this.irc)
{
System.out.println("IRC connected successfully!");
IrcStack.this.connectionState = state;
IrcStack.this.irc.notifyAll();
}
}
@Override
public void onFailure(Exception e)
{
synchronized(IrcStack.this.irc)
{
System.out.println("IRC connection FAILED!");
e.printStackTrace();
IrcStack.this.connectionState = null;
IrcStack.this.irc.notifyAll();
}
}
});
// wait while the irc connection is being established ...
try
{
System.out.println("Waiting for a connection ...");
this.irc.wait();
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
//TODO do something on connection fail!
}
}
public void disconnect()
{
if(this.connectionState != null)
{
this.irc.disconnect();
this.connectionState = null;
}
}
public void dispose()
{
disconnect();
}
public String getNick()
{
return (this.connectionState == null) ? this.params.getNickname() : this.connectionState.getNickname();
}
public void setUserNickname(String nick)
{
if (this.connectionState == null)
{
this.params.setNickname(nick);
}
else
{
this.irc.changeNick(nick);
}
}
public void setSubject(ChatRoomIrcImpl chatroom, String subject)
{
if (this.connectionState == null)
throw new IllegalStateException("Please connect to an IRC server first.");
if (chatroom == null)
throw new IllegalArgumentException("Cannot have a null chatroom");
this.irc.changeTopic(chatroom.getName(), subject == null ? "" : subject);
}
public boolean isJoined(ChatRoomIrcImpl chatroom)
{
//TODO Implement this.
return false;
}
public List<String> getServerChatRoomList()
{
//TODO Implement this.
return new ArrayList<String>();
}
public void join(ChatRoomIrcImpl chatroom)
{
join(chatroom, "".getBytes());
}
public void join(ChatRoomIrcImpl chatroom, byte[] password)
{
//TODO password as String
if (chatroom == null)
throw new IllegalArgumentException("chatroom cannot be null");
if (password == null)
throw new IllegalArgumentException("password cannot be null");
//TODO add chatroom listener
this.irc.joinChannel(chatroom.getName(), password.toString());
}
public void leave(ChatRoomIrcImpl chatroom)
{
//TODO Implement this.
}
public void banParticipant(ChatRoomIrcImpl chatroom, ChatRoomMember member, String reason)
{
//TODO Implement this.
}
public void kickParticipant(ChatRoomIrcImpl chatroom, ChatRoomMember member, String reason)
{
//TODO Implement this.
}
public void invite(String memberId, ChatRoomIrcImpl chatroom)
{
//TODO Implement this.
}
public void command(ChatRoomIrcImpl chatroom, String command)
{
//TODO Implement this.
}
public void message(ChatRoomIrcImpl chatroom, String message)
{
//TODO Implement this.
}
private static class ServerParameters implements IServerParameters {
private String nick;
private List<String> alternativeNicks = new ArrayList<String>();
private String real;
private String ident;
private IRCServer server;
private ServerParameters(String nickName, String realName, String ident, IRCServer server)
{
this.nick = nickName;
this.alternativeNicks.add(nickName+"_");
this.real = realName;
this.ident = ident;
this.server = server;
}
@Override
public String getNickname()
{
return this.nick;
}
public void setNickname(String nick)
{
this.nick = nick;
}
@Override
public List<String> getAlternativeNicknames()
{
return this.alternativeNicks;
}
public void setAlternativeNicknames(List<String> names)
{
this.alternativeNicks = names;
}
@Override
public String getIdent()
{
return this.ident;
}
public void setIdent(String ident)
{
this.ident = ident;
}
@Override
public String getRealname()
{
return this.real;
}
public void setRealname(String realname)
{
this.real = realname;
}
@Override
public IRCServer getServer()
{
return this.server;
}
public void setServer(IRCServer server)
{
this.server = server;
}
}
}
|