/**
   * 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 IllegalStateException if the underlying ICQ stack is not registered and initialized.
   * @throws IllegalArgumentException if <tt>to</tt> is not an instance belonging to the underlying
   *     implementation.
   */
  public void sendInstantMessage(Contact to, Message message)
      throws IllegalStateException, IllegalArgumentException {
    if (!(to instanceof ContactDictImpl)) {
      throw new IllegalArgumentException("The specified contact is not a Dict contact." + to);
    }

    // Remove all html tags from the message
    message = createMessage(Html2Text.extractText(message.getContent()));

    // Display the queried word
    fireMessageDelivered(message, to);

    this.submitDictQuery((ContactDictImpl) to, message);
  }
  /**
   * Create, execute and display a query to a dictionary (ContactDictImpl)
   *
   * @param dictContact the contact containing the database name
   * @param message the message containing the word
   */
  private void submitDictQuery(ContactDictImpl dictContact, Message message) {
    Message msg = this.createMessage("");

    String database = dictContact.getContactID();
    DictConnection conn = this.parentProvider.getConnection();
    boolean doMatch = false;

    String word;

    // Formatting the query message, if the word as one or more spaces we
    // put it between quotes to prevent errors
    word = message.getContent().replace("\"", "").trim();
    if (word.indexOf(' ') > 0) {
      word = "\"" + word + "\"";
    }

    // Try to get the definition of the work
    try {
      List<Definition> definitions = conn.define(database, word);
      msg = this.createMessage(retrieveDefine(definitions, word));
    } catch (DictException dx) {
      if (dx.getErrorCode()
          == DictReturnCode.NO_MATCH) { // No word found, we are going to try the match command
        doMatch = true;
      } else { // Otherwise we display the error returned by the server
        msg = this.createMessage(manageException(dx, database));
      }
    }

    if (doMatch) {
      // Trying the match command
      try {
        List<MatchWord> matchWords = conn.match(database, word, this.accountID.getStrategy());
        msg = this.createMessage(retrieveMatch(matchWords, word));
      } catch (DictException dx) {
        msg = this.createMessage(manageException(dx, database));
      }
    }

    // Send message
    fireMessageReceived(msg, dictContact);
  }