From 9bb76c43ed7e5302ca60dbe4913dd3af207abd54 Mon Sep 17 00:00:00 2001 From: Matthieu Casanova Date: Mon, 2 May 2011 08:31:24 +0000 Subject: [PATCH] Use a precompiled pattern to escape chars in messages made some fields final replaced Vector by ArrayList Added some @Override annotations Fixed some indentation Made some private methods static Replaced http://sip-communicator.org by http://jitsi.org --- resources/install/doc/readme.txt | 2 +- ...tionSetBasicInstantMessagingYahooImpl.java | 148 +++++++++--------- 2 files changed, 79 insertions(+), 71 deletions(-) diff --git a/resources/install/doc/readme.txt b/resources/install/doc/readme.txt index 84f941de4..199d6aa8b 100644 --- a/resources/install/doc/readme.txt +++ b/resources/install/doc/readme.txt @@ -1,5 +1,5 @@ The SIP Communicator is currently under active development. The version you are running is only experimental and WILL NOT work as expected. Please refer to -http://sip-communicator.org +http://jitsi.org/ for more information. diff --git a/src/net/java/sip/communicator/impl/protocol/yahoo/OperationSetBasicInstantMessagingYahooImpl.java b/src/net/java/sip/communicator/impl/protocol/yahoo/OperationSetBasicInstantMessagingYahooImpl.java index f05f5f2eb..40c63f48a 100644 --- a/src/net/java/sip/communicator/impl/protocol/yahoo/OperationSetBasicInstantMessagingYahooImpl.java +++ b/src/net/java/sip/communicator/impl/protocol/yahoo/OperationSetBasicInstantMessagingYahooImpl.java @@ -38,12 +38,17 @@ public class OperationSetBasicInstantMessagingYahooImpl * 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 + private static final int MAX_MESSAGE_LENGTH = 800; // 949 + + /** + * A regexp that is used to escape some chars in messages. + */ + private static final Pattern MESSAGE_CHARS_ESCAPE = Pattern.compile("([.()^&$*|])"); /** * A list of filters registered for message events. */ - private Vector eventFilters = new Vector(); + private final List eventFilters = new ArrayList(); /** * The provider that created us. @@ -54,7 +59,7 @@ public class OperationSetBasicInstantMessagingYahooImpl * 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(); + private final MessageDecoder messageDecoder = new MessageDecoder(); /** * A reference to the persistent presence operation set that we use @@ -106,13 +111,14 @@ public boolean isOfflineMessagingSupported() */ public boolean isContentTypeSupported(String contentType) { - if(contentType.equals(DEFAULT_MIME_TYPE) || + if(contentType.equals(DEFAULT_MIME_TYPE) || contentType.equals(HTML_MIME_TYPE)) return true; else return false; } + @Override public Message createMessage(String content, String contentType, String encoding, String subject) { @@ -208,7 +214,6 @@ public void sendInstantMessage(Contact to, Message message) if (evt != null) fireMessageEvent(evt); - return; } } @@ -267,6 +272,7 @@ public void registrationStateChanged(RegistrationStateChangeEvent evt) * @param evt the EventObject that we'd like delivered to all * registered message listerners. */ + @Override protected void fireMessageEvent(EventObject evt) { // check if this event should be filtered out @@ -315,6 +321,7 @@ private class YahooMessageListener * * @param ev Event with information on the received message */ + @Override public void messageReceived(SessionEvent ev) { handleNewMessage(ev); @@ -327,6 +334,7 @@ public void messageReceived(SessionEvent ev) * * @param ev Event with information on the received message */ + @Override public void offlineMessageReceived(SessionEvent ev) { handleNewMessage(ev); @@ -342,62 +350,63 @@ public void offlineMessageReceived(SessionEvent ev) * * @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 = "    " - + yahooMailLogon + ""; - - String newMail = YahooActivator.getResources().getI18NString( - "service.gui.NEW_MAIL", - new String[]{ev.getFrom(), - "<" + ev.getEmailAddress() + ">", - ev.getSubject(), - "    "+yahooMailLogon}) ; - - Message newMailMessage = new MessageYahooImpl( - newMail, - HTML_MIME_TYPE, - DEFAULT_MIME_ENCODING, - null); - - Contact sourceContact = opSetPersPresence. - findContactByID(ev.getEmailAddress()); - - if (sourceContact == null) - { - if (logger.isDebugEnabled()) - logger.debug("received a new mail from an unknown contact: " - + ev.getFrom() - + " <" + ev.getEmailAddress() + ">"); - //create the volatile contact - sourceContact = opSetPersPresence - .createVolatileContact(ev.getEmailAddress()); - } - MessageReceivedEvent msgReceivedEvt - = new MessageReceivedEvent( - newMailMessage, sourceContact, System.currentTimeMillis(), - MessageReceivedEvent.SYSTEM_MESSAGE_RECEIVED); + @Override + 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 = "    " + + yahooMailLogon + ""; + + String newMail = YahooActivator.getResources().getI18NString( + "service.gui.NEW_MAIL", + new String[]{ev.getFrom(), + "<" + ev.getEmailAddress() + ">", + ev.getSubject(), + "    "+yahooMailLogon}) ; - fireMessageEvent(msgReceivedEvt); - } + Message newMailMessage = new MessageYahooImpl( + newMail, + HTML_MIME_TYPE, + DEFAULT_MIME_ENCODING, + null); + + Contact sourceContact = opSetPersPresence. + findContactByID(ev.getEmailAddress()); + + if (sourceContact == null) + { + if (logger.isDebugEnabled()) + logger.debug("received a new mail from an unknown contact: " + + ev.getFrom() + + " <" + ev.getEmailAddress() + ">"); + //create the volatile contact + sourceContact = opSetPersPresence + .createVolatileContact(ev.getEmailAddress()); + } + MessageReceivedEvent msgReceivedEvt + = new MessageReceivedEvent( + newMailMessage, sourceContact, System.currentTimeMillis(), + MessageReceivedEvent.SYSTEM_MESSAGE_RECEIVED); + + fireMessageEvent(msgReceivedEvt); + } /** * Handle incoming message by creating an appropriate Sip Communicator @@ -503,7 +512,7 @@ public String processLinks(String message) String matchGroup1 = m.group(1); String matchGroup2 = m.group(2); - String formattedString = this.formatLinksToHTML(matchGroup1); + String formattedString = formatLinksToHTML(matchGroup1); m.appendReplacement(msgBuffer, replaceSpecialRegExpChars(formattedString) + matchGroup2); @@ -524,23 +533,23 @@ public String processLinks(String message) * @param text The initial text. * @return the formatted text */ - private String replaceSpecialRegExpChars(String text) + private static String replaceSpecialRegExpChars(String text) { - return text.replaceAll("([.()^&$*|])", "\\\\$1"); + return MESSAGE_CHARS_ESCAPE.matcher(text).replaceAll("\\\\$1"); } /** * Goes through the given text and converts all links to HTML links. *

- * For example all occurrences of http://sip-communicator.org will be - * replaced by - * http://sip-communicator.org. The same is true for all strings + * For example all occurrences of http://jitsi.org/ will be + * replaced by + * http://jitsi.org/. 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) + private static String formatLinksToHTML(String text) { String wwwURL = "(www\\." + // Matches the "www" string. "[^/?#<\"'\\s]+" + // Matches at least one char of @@ -565,7 +574,7 @@ private String formatLinksToHTML(String text) "(\\?[^#<\"'\\s]*)?" + "(#.*)?)"; - String url = "(" + wwwURL + "|" + protocolURL + ")"; + String url = '(' + wwwURL + '|' + protocolURL + ')'; Pattern p = Pattern.compile(url, Pattern.CASE_INSENSITIVE); @@ -573,12 +582,11 @@ private String formatLinksToHTML(String text) StringBuffer linkBuffer = new StringBuffer(); - String replacement; - while (m.find()) { String linkGroup = m.group(); + String replacement; if (linkGroup.startsWith("www")) { replacement = "