aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/impl/protocol/yahoo/OperationSetBasicInstantMessagingYahooImpl.java
blob: c61519ccb0b7b9164703e950319ab28b1890f15c (plain)
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
/*
 * 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.yahoo;

import java.io.*;
import java.util.*;
import java.util.regex.*;

import net.java.sip.communicator.service.protocol.*;
import net.java.sip.communicator.service.protocol.event.*;
import net.java.sip.communicator.util.*;
import ymsg.network.event.*;
import ymsg.support.*;

/**
 * A straightforward implementation of the basic instant messaging operation
 * set.
 *
 * @author Damian Minkov
 */
public class OperationSetBasicInstantMessagingYahooImpl
    implements OperationSetBasicInstantMessaging
{
    /**
     * Logger for this class
     */
    private static final Logger logger =
        Logger.getLogger(OperationSetBasicInstantMessagingYahooImpl.class);
    
    /**
     * HTML content type
     */
    private static final String CONTENT_TYPE_HTML = "text/html";

    /**
     * Yahoo has limit of message length. If exceeded 
     * message is not delivered and no notification is received for that.
     */
    private static int MAX_MESSAGE_LENGTH = 800; // 949
    
    /**
     * A list of listeneres registered for message events.
     */
    private Vector messageListeners = new Vector();

    /**
     * The provider that created us.
     */
    private ProtocolProviderServiceYahooImpl yahooProvider = null;

    /**
     * Message decoder allows to convert Yahoo formated messages, which can
     * contains some specials characters, to HTML or to plain text.
     */
     private MessageDecoder messageDecoder = new MessageDecoder();

    /**
     * A reference to the persistent presence operation set that we use
     * to match incoming messages to <tt>Contact</tt>s and vice versa.
     */
    private OperationSetPersistentPresenceYahooImpl 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.
     */
    OperationSetBasicInstantMessagingYahooImpl(
        ProtocolProviderServiceYahooImpl provider)
    {
        this.yahooProvider = 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 true;
    }

    /**
     * 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) || 
           contentType.equals(CONTENT_TYPE_HTML))
            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 MessageYahooImpl(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 MessageYahooImpl(
            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 ContactYahooImpl) )
           throw new IllegalArgumentException(
               "The specified contact is not a Yahoo contact."
               + to);

        try
        {
            String toUserID = ((ContactYahooImpl) to).getID();

            byte[] msgBytesToBeSent = message.getContent().getBytes();

            // split the message in parts with max allowed length
            // and send them all
            do
            {
                if(msgBytesToBeSent.length > MAX_MESSAGE_LENGTH)
                {
                    byte[] tmp1 = new byte[MAX_MESSAGE_LENGTH];
                    System.arraycopy(msgBytesToBeSent, 
                        0, tmp1, 0, MAX_MESSAGE_LENGTH);
                    
                    byte[] tmp2 = 
                        new byte[msgBytesToBeSent.length - MAX_MESSAGE_LENGTH];
                    System.arraycopy(msgBytesToBeSent, 
                        MAX_MESSAGE_LENGTH, tmp2, 0, tmp2.length);
                    
                    msgBytesToBeSent = tmp2;
                    
                    yahooProvider.getYahooSession().sendMessage(
                        toUserID,
                        new String(tmp1));
                }
                else
                {
                    yahooProvider.getYahooSession().sendMessage(
                        toUserID,
                        new String(msgBytesToBeSent));
                }
                
                MessageDeliveredEvent msgDeliveredEvt
                    = new MessageDeliveredEvent(
                        message, to, new Date());

                fireMessageEvent(msgDeliveredEvt);
            }
            while(msgBytesToBeSent.length > MAX_MESSAGE_LENGTH);
        }
        catch (IOException ex)
        {
            logger.fatal("Cannot Send Message! " + ex.getMessage());
            MessageDeliveryFailedEvent evt =
                new MessageDeliveryFailedEvent(
                    message,
                    to,
                    MessageDeliveryFailedEvent.NETWORK_FAILURE,
                    new Date());
            fireMessageEvent(evt);
            return;
        }
    }

    /**
     * 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 (yahooProvider == null)
            throw new IllegalStateException(
                "The provider must be non-null and signed on the "
                +"service before being able to communicate.");
        if (!yahooProvider.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 = (OperationSetPersistentPresenceYahooImpl)
                    yahooProvider.getSupportedOperationSets()
                        .get(OperationSetPersistentPresence.class.getName());

                yahooProvider.getYahooSession().
                    addSessionListener(new YahooMessageListener());
            }
        }
    }

    /**
     * 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();
        }

        logger.debug("Dispatching  msg evt. Listeners="
                     + messageListeners.size()
                     + " evt=" + evt);

        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);
            }
        }
    }

    /**
     * This class provides methods to listen for yahoo events which interest us.
     */
    private class YahooMessageListener
        extends SessionAdapter
    {
        /**
         * Overrides <tt>messageReceived</tt> from <tt>SessionAdapter</tt>,
         * called when we receive a new intant message.
         *
         * @param ev Event with information on the received message
         */
        public void messageReceived(SessionEvent ev)
        {
            handleNewMessage(ev);
        }

        /**
         * Overrides <tt>offlineMessageReceived</tt> from <tt>SessionAdapter</tt>,
         * called when we receive a message which has been sent to us
         * when we were offline.
         *
         * @param ev Event with information on the received message
         */
        public void offlineMessageReceived(SessionEvent ev)
        {
            handleNewMessage(ev);
        }
 
        /**
         * Overrides <tt>newMailReceived</tt> from <tt>SessionAdapter</tt>,
         * called when yahoo alert us that there is a new message in our mailbox.
         * There is two types of notification, the first one provides only
         * the number of unread mails and the second gives informations about
         * a precise new mail. Here, we care about only the second case in which
         * we should always have the email of the sender of the mail.
         *
         * @param ev Event with information on the received email
         */
         public void newMailReceived(SessionNewMailEvent ev)
         {
             // why, if I provide mail@yahoo.FR when registering my account,
             // SC later tells me that my email address is mail@yahoo.COM ??
             // because of this users will always be sent on yahoo.com mail
             // login page rather than their usual (yahoo.XXX) login page.
             String myEmail = yahooProvider.getAccountID().getAccountAddress();

             // we don't process incoming email event without source address.
             // it allows us to avoid some spams
             if ((ev.getEmailAddress() == null)
                    || (ev.getEmailAddress().indexOf('@') < 0))
             {
                 return;
             }

             String yahooMailLogon = "http://mail."
                     + myEmail.substring(myEmail.indexOf("@") + 1);

             yahooMailLogon = "<a href=\""
                     + yahooMailLogon + "\">"
                     + yahooMailLogon + "</a>";

             String newMail = "<b>" + Resources.getString("newMail") + " : </b> "
                     + ev.getSubject();

             newMail += "\n<br /><b>" + Resources.getString("from") + " : </b> "
                     + ev.getFrom() + " &lt;" + ev.getEmailAddress() + "&gt;";

             newMail += "\n<br />&nbsp;&nbsp;&nbsp;&nbsp;" + yahooMailLogon;

             Message newMailMessage = new MessageYahooImpl(
                     newMail,
                     CONTENT_TYPE_HTML,
                     DEFAULT_MIME_ENCODING,
                     null);

             Contact sourceContact = opSetPersPresence.
                 findContactByID(ev.getEmailAddress());

             if (sourceContact == null)
             {
                 logger.debug("received a new mail from an unknown contact: "
                                    + ev.getFrom()
                                    + " &lt;" + ev.getEmailAddress() + "&gt;");
                 //create the volatile contact
                 sourceContact = opSetPersPresence
                     .createVolatileContact(ev.getEmailAddress());
             }
             MessageReceivedEvent msgReceivedEvt
                 = new MessageReceivedEvent(
                     newMailMessage, sourceContact, new Date(),
                     MessageReceivedEvent.SYSTEM_MESSAGE_RECEIVED);

             fireMessageEvent(msgReceivedEvt);
         }

        /**
         * Handle incoming message by creating an appropriate Sip Communicator
         * <tt>Message</tt> and firing a <tt>MessageReceivedEvent</tt>
         * to interested listeners.
         *
         * @param ev The original <tt>SessionEvent</tt> which noticed us
         * of an incoming message.
         */
        private void handleNewMessage(SessionEvent ev)
        {
            logger.debug("Message received : " + ev);

            // to keep things simple, we can decodeToText()
            //String formattedMessage = processLinks(
            //        messageDecoder.decodeToText(ev.getMessage()));

            String formattedMessage = ev.getMessage();
            logger.debug("original message received : " + formattedMessage);

            // if the message is decorated by Yahoo, we try to "decode" it first.
            if (formattedMessage.startsWith("\u001b"))
            {
                formattedMessage = processLinks(
                        messageDecoder.decodeToHTML(formattedMessage));
            }
            else
            {
                formattedMessage = processLinks(formattedMessage);
            }

            // now, we try to fix a wrong usage of the size attribute in the
            // <font> HTML element
            // TODO : it will good to use a better regex which takes in account
            // the "<font" opening tag. So, we will not miss an important message
            // containing the string 'size="x">'...
            // here, the zero 0 correspond to 10px
            formattedMessage =
                    formattedMessage.replaceAll("(<font) (.*) size=\"0\">",
                    "$1 $2 size=\"10\">");
            formattedMessage = 
                    formattedMessage.replaceAll("(<font) (.*) size=\"(\\d+)\">",
                    "$1 $2 style=\"font-size: $3px;\">");

            logger.debug("formatted Message : " + formattedMessage);
            //As no indications in the protocol is it html or not. No harm
            //to set all messages html - doesn't affect the appearance of the gui
            Message newMessage = createMessage(
                formattedMessage.getBytes(),
                CONTENT_TYPE_HTML,
                DEFAULT_MIME_ENCODING,
                null);

            Contact sourceContact = opSetPersPresence.
                findContactByID(ev.getFrom());

             if(sourceContact == null)
            {
                logger.debug("received a message from an unknown contact: "
                                   + ev.getFrom());
                //create the volatile contact
                sourceContact = opSetPersPresence
                    .createVolatileContact(ev.getFrom());
            }

            MessageReceivedEvent msgReceivedEvt
                = new MessageReceivedEvent(
                    newMessage, sourceContact , new Date() );

            fireMessageEvent(msgReceivedEvt);
        }
    }

    /**
     * Format links in the given message. Skips all links, which are already in
     * HTML format and converts all other links.
     * 
     * @param message The source message string.
     * @return The message string with properly formatted links.
     */
    private String processLinks(String message)
    {
        StringBuffer msgBuffer = new StringBuffer();

        // We match two groups of Strings. The first group is the group of any
        // String. The second group is a well formatted HTML link.
        Pattern p = Pattern.compile("(.*?)(<a[\\s][^<]*(/>|</a>))",
                                    Pattern.CASE_INSENSITIVE);

        Matcher m = p.matcher(message);

        int lastMatchIndex = 0;
        while (m.find())
        {
            lastMatchIndex = m.end();

            String matchGroup1 = m.group(1);
            String matchGroup2 = m.group(2);

            String formattedString = this.formatLinksToHTML(matchGroup1);

            m.appendReplacement(msgBuffer,
                replaceSpecialRegExpChars(formattedString) + matchGroup2);
        }

        String tailString = message.substring(lastMatchIndex);

        String formattedTailString = formatLinksToHTML(tailString);

        msgBuffer.append(formattedTailString);

        return msgBuffer.toString();
    }

    /**
     * Replaces some chars that are special in a regular expression.
     * 
     * @param text The initial text.
     * @return the formatted text
     */
    private String replaceSpecialRegExpChars(String text)
    {
        return text.replaceAll("([.()^&$*|])", "\\\\$1");
    }

    /**
     * Goes through the given text and converts all links to HTML links.
     * <p>
     * For example all occurrences of http://sip-communicator.org will be
     * replaced by <a href="http://sip-communicator.org">
     * http://sip-communicator.org</a\>. The same is true for all strings
     * starting with "www".
     * 
     * @param text the text on which the regular expression would be performed 
     * @return the initial text containing only HTML links
     */
    private String formatLinksToHTML(String text)
    {
        String wwwURL = "(www\\." + // Matches the "www" string.
                        "[^/?#<\"'\\s]+" + // Matches at least one char of
                                           // any type except / ? # < " '
                                           // and space.
                        "[\\.]" + // Matches the second point of the link.
                        "[^?#<\"'\\s]+" +   // Matches at least one char of
                                            // any type except ? # < " '
                                            // and space.
                        "(\\?[^#<\"'\\s]*)?" +
                        "(#.*)?)";

        String protocolURL
                =   "([^\"'<>:/?#\\s]+" +   // Matches at least one char of
                                            // any type except " ' < > : / ? #
                                            // and space.
                    "://" + // Matches the :// delimiter in links
                    "[^/?#<\"'\\s]*" +  // Matches any number of times any char
                                        // except / ? # < " ' and space.
                    "[^?#<\"'\\s]*" +   // Matches any number of times any char
                                        // except ? # < " ' and space.
                    "(\\?[^#<\"'\\s]*)?" +
                    "(#.*)?)";

        String url = "(" + wwwURL + "|" + protocolURL + ")";

        Pattern p = Pattern.compile(url, Pattern.CASE_INSENSITIVE);

        Matcher m = p.matcher(text);

        StringBuffer linkBuffer = new StringBuffer();

        String replacement;

        while (m.find())
        {
            String linkGroup = m.group();

            if (linkGroup.startsWith("www"))
            {
                replacement = "<A href=\"" + "http://"
                    + linkGroup + "\">" + linkGroup + "</A>";
            }
            else
            {
                replacement = "<A href=\"" + linkGroup
                    + "\">" + linkGroup + "</A>";
            }

            m.appendReplacement(linkBuffer,
                replaceSpecialRegExpChars(replacement));
        }

        m.appendTail(linkBuffer);

        return linkBuffer.toString();
    }
}