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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
|
/*
* SIP Communicator, 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.msn;
import java.util.*;
import java.text.*;
import net.java.sip.communicator.service.protocol.*;
import net.java.sip.communicator.service.protocol.event.*;
import net.java.sip.communicator.service.protocol.msnconstants.*;
import net.java.sip.communicator.util.*;
import net.sf.jml.*;
import net.sf.jml.event.*;
import net.sf.jml.message.*;
import net.java.sip.communicator.impl.protocol.msn.mail.utils.*;
/**
* A straightforward implementation of the basic instant messaging operation
* set.
*
* @author Damian Minkov
*/
public class OperationSetBasicInstantMessagingMsnImpl
implements OperationSetBasicInstantMessaging
{
private static final Logger logger =
Logger.getLogger(OperationSetBasicInstantMessagingMsnImpl.class);
/**
* A list of listeneres registered for message events.
*/
private Vector messageListeners = new Vector();
/**
* The provider that created us.
*/
private ProtocolProviderServiceMsnImpl msnProvider = null;
/**
* A reference to the persistent presence operation set that we use
* to match incoming messages to <tt>Contact</tt>s and vice versa.
*/
private OperationSetPersistentPresenceMsnImpl opSetPersPresence = null;
/**
* Creates an instance of this operation set.
* @param provider a ref to the <tt>ProtocolProviderServiceImpl</tt>
* that created us and that we'll use for retrieving the underlying aim
* connection.
*/
OperationSetBasicInstantMessagingMsnImpl(
ProtocolProviderServiceMsnImpl provider)
{
this.msnProvider = provider;
provider.addRegistrationStateChangeListener(new RegistrationStateListener());
}
/**
* Registeres a MessageListener with this operation set so that it gets
* notifications of successful message delivery, failure or reception of
* incoming messages..
*
* @param listener the <tt>MessageListener</tt> to register.
*/
public void addMessageListener(MessageListener listener)
{
synchronized(messageListeners)
{
if(!messageListeners.contains(listener))
{
this.messageListeners.add(listener);
}
}
}
/**
* Unregisteres <tt>listener</tt> so that it won't receive any further
* notifications upon successful message delivery, failure or reception of
* incoming messages..
*
* @param listener the <tt>MessageListener</tt> to unregister.
*/
public void removeMessageListener(MessageListener listener)
{
synchronized(messageListeners)
{
this.messageListeners.remove(listener);
}
}
/**
* Determines wheter the protocol provider (or the protocol itself) support
* sending and receiving offline messages. Most often this method would
* return true for protocols that support offline messages and false for
* those that don't. It is however possible for a protocol to support these
* messages and yet have a particular account that does not (i.e. feature
* not enabled on the protocol server). In cases like this it is possible
* for this method to return true even when offline messaging is not
* supported, and then have the sendMessage method throw an
* OperationFailedException with code - OFFLINE_MESSAGES_NOT_SUPPORTED.
*
* @return <tt>true</tt> if the protocol supports offline messages and
* <tt>false</tt> otherwise.
*/
public boolean isOfflineMessagingSupported()
{
return false;
}
/**
* Determines wheter the protocol supports the supplied content type
*
* @param contentType the type we want to check
* @return <tt>true</tt> if the protocol supports it and
* <tt>false</tt> otherwise.
*/
public boolean isContentTypeSupported(String contentType)
{
if(contentType.equals(DEFAULT_MIME_TYPE))
return true;
else
return false;
}
/**
* Create a Message instance for sending arbitrary MIME-encoding content.
*
* @param content content value
* @param contentType the MIME-type for <tt>content</tt>
* @param contentEncoding encoding used for <tt>content</tt>
* @param subject a <tt>String</tt> subject or <tt>null</tt> for now subject.
* @return the newly created message.
*/
public Message createMessage(byte[] content, String contentType,
String contentEncoding, String subject)
{
return new MessageMsnImpl(new String(content), contentType
, contentEncoding, subject);
}
/**
* Create a Message instance for sending a simple text messages with
* default (text/plain) content type and encoding.
*
* @param messageText the string content of the message.
* @return Message the newly created message
*/
public Message createMessage(String messageText)
{
return new MessageMsnImpl(messageText, DEFAULT_MIME_TYPE
, DEFAULT_MIME_ENCODING, null);
}
/**
* Sends the <tt>message</tt> to the destination indicated by the
* <tt>to</tt> contact.
*
* @param to the <tt>Contact</tt> to send <tt>message</tt> to
* @param message the <tt>Message</tt> to send.
* @throws java.lang.IllegalStateException if the underlying stack is
* not registered and initialized.
* @throws java.lang.IllegalArgumentException if <tt>to</tt> is not an
* instance of ContactImpl.
*/
public void sendInstantMessage(Contact to, Message message)
throws IllegalStateException, IllegalArgumentException
{
assertConnected();
if( !(to instanceof ContactMsnImpl) )
throw new IllegalArgumentException(
"The specified contact is not an MSN contact."
+ to);
if(to.getPresenceStatus().equals(MsnStatusEnum.OFFLINE))
{
MessageDeliveryFailedEvent evt =
new MessageDeliveryFailedEvent(
message,
to,
MessageDeliveryFailedEvent.OFFLINE_MESSAGES_NOT_SUPPORTED,
new Date());
fireMessageEvent(evt);
return;
}
msnProvider.getMessenger().
sendText(
((ContactMsnImpl)to).getSourceContact().getEmail(),
message.getContent()
);
MessageDeliveredEvent msgDeliveredEvt
= new MessageDeliveredEvent(
message, to, new Date());
fireMessageEvent(msgDeliveredEvt);
}
/**
* Utility method throwing an exception if the stack is not properly
* initialized.
* @throws java.lang.IllegalStateException if the underlying stack is
* not registered and initialized.
*/
private void assertConnected() throws IllegalStateException
{
if (msnProvider == null)
throw new IllegalStateException(
"The provider must be non-null and signed on the "
+"service before being able to communicate.");
if (!msnProvider.isRegistered())
throw new IllegalStateException(
"The provider must be signed on the service before "
+"being able to communicate.");
}
/**
* Our listener that will tell us when we're registered to
*/
private class RegistrationStateListener
implements RegistrationStateChangeListener
{
/**
* The method is called by a ProtocolProvider implementation whenver
* a change in the registration state of the corresponding provider had
* occurred.
* @param evt ProviderStatusChangeEvent the event describing the status
* change.
*/
public void registrationStateChanged(RegistrationStateChangeEvent evt)
{
logger.debug("The provider changed state from: "
+ evt.getOldState()
+ " to: " + evt.getNewState());
if (evt.getNewState() == RegistrationState.REGISTERED)
{
opSetPersPresence = (OperationSetPersistentPresenceMsnImpl)
msnProvider.getSupportedOperationSets()
.get(OperationSetPersistentPresence.class.getName());
msnProvider.getMessenger().
addMessageListener(new MsnMessageListener());
msnProvider.getMessenger().
addEmailListener(new MsnMessageListener());
}
}
}
/**
* Delivers the specified event to all registered message listeners.
* @param evt the <tt>EventObject</tt> that we'd like delivered to all
* registered message listerners.
*/
private void fireMessageEvent(EventObject evt)
{
Iterator listeners = null;
synchronized (messageListeners)
{
listeners = new ArrayList(messageListeners).iterator();
}
while (listeners.hasNext())
{
MessageListener listener
= (MessageListener) listeners.next();
if (evt instanceof MessageDeliveredEvent)
{
listener.messageDelivered( (MessageDeliveredEvent) evt);
}
else if (evt instanceof MessageReceivedEvent)
{
listener.messageReceived( (MessageReceivedEvent) evt);
}
else if (evt instanceof MessageDeliveryFailedEvent)
{
listener.messageDeliveryFailed(
(MessageDeliveryFailedEvent) evt);
}
}
}
private class MsnMessageListener
extends MsnMessageAdapter
implements MsnEmailListener
{
public void instantMessageReceived(MsnSwitchboard switchboard,
MsnInstantMessage message,
MsnContact contact)
{
Message newMessage = createMessage(message.getContent());
Contact sourceContact = opSetPersPresence.
findContactByID(contact.getEmail().getEmailAddress());
if(sourceContact == null)
{
logger.debug("received a message from an unknown contact: "
+ contact);
//create the volatile contact
sourceContact = opSetPersPresence
.createVolatileContact(contact.getEmail().getEmailAddress());
}
MessageReceivedEvent msgReceivedEvt
= new MessageReceivedEvent(
newMessage, sourceContact , new Date() );
fireMessageEvent(msgReceivedEvt);
}
public void initialEmailNotificationReceived(MsnSwitchboard switchboard,
MsnEmailInitMessage message,
MsnContact contact)
{
}
public void initialEmailDataReceived(MsnSwitchboard switchboard,
MsnEmailInitEmailData message,
MsnContact contact)
{
}
public void newEmailNotificationReceived(MsnSwitchboard switchboard,
MsnEmailNotifyMessage message,
MsnContact contact)
{
// we don't process incoming event without email.
if ((message.getFromAddr() == null)
|| (message.getFromAddr().indexOf('@') < 0))
{
return;
}
String subject = message.getSubject();
try
{
subject = MimeUtility.decodeText(subject);
}
catch (Exception exception)
{
exception.printStackTrace();
}
Message newMailMessage = new MessageMsnImpl(
MessageFormat.format(Resources.getString("newMail"),
new Object[]{message.getFrom(),
message.getFromAddr(),
subject}),
DEFAULT_MIME_TYPE,
DEFAULT_MIME_ENCODING,
subject);
Contact sourceContact = opSetPersPresence.
findContactByID(message.getFromAddr());
if (sourceContact == null)
{
logger.debug("received a new mail from an unknown contact: "
+ message.getFrom()
+ " <" + message.getFromAddr() + ">");
//create the volatile contact
sourceContact = opSetPersPresence
.createVolatileContact(message.getFromAddr());
}
MessageReceivedEvent msgReceivedEvt
= new MessageReceivedEvent(
newMailMessage, sourceContact, new Date(),
MessageReceivedEvent.SYSTEM_MESSAGE_RECEIVED);
fireMessageEvent(msgReceivedEvt);
}
public void activityEmailNotificationReceived(MsnSwitchboard switchboard,
MsnEmailActivityMessage message,
MsnContact contact)
{
}
}
}
|