public void signOut() {
    try {
      sipRC.register(this.getLocalURI(), true);
    } catch (Exception e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
    }

    Element[] elms = new Element[3];

    elms[0] = new Element(MessageCons.IM_TYPE, MessageCons.GATEWAY, Message.JXTA_NAME_SPACE);
    elms[1] = new Element(MessageCons.FROM_HEADER, this.getLocalURI(), Message.JXTA_NAME_SPACE);
    elms[2] =
        new Element(
            MessageCons.IM_STATUS,
            Integer.toString(MessageCons.IM_STATUS_OFFLINE),
            Message.JXTA_NAME_SPACE);

    try {
      groupsvc.send(impipe, new Message(elms));
    } catch (IOException e) {
      e.printStackTrace();
      //            LOG.error("could not subscribe, no other IM running");
      //            throw new IMException("could not subscribe, no other IM running");
    }
  }
  public void signIn() {
    try {
      sipRC.register(this.getLocalURI(), false);
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }

    sendGatewayMessage();
  }
  public void handleMessage(Message message, String arg) {
    logger.debug("got message:" + message.toXMLString());

    String fromaddress = new String(message.getElement(MessageCons.FROM_HEADER).getData());

    String type = new String(message.getElement(MessageCons.IM_TYPE).getData());

    // IM_TYPE_MSG
    if (type.equals(MessageCons.MESSAGE))
    //                (addressto.equals(sipaddress) || address.equals("any")))
    {

      String toaddress = new String(message.getElement(MessageCons.TO_HEADER).getData());
      String msg = new String(message.getElement(MessageCons.MESSAGE_VALUE).getData());

      if (!registeredUserList.hasUser(toaddress)) {
        try {
          sipMC.sendMessage("sip:" + fromaddress, "sip:" + toaddress, msg);
        } catch (Exception e) {
          e.printStackTrace();
        }
      }

      //            if (toaddress.equals(sipaddress) ||
      //            if (toaddress.equals(MessageCons.SIP_ADDRESS_ANY))
      //            {
      //                String msg = new
      // String(message.getElement(MessageCons.MESSAGE_VALUE).getData());
      //                imlistener.process(fromaddress, msg);
      //            }
    }
    // IM_TYPE_REG
    else if (type.equals(MessageCons.REGISTER)) {
      int status =
          Integer.parseInt(new String(message.getElement(MessageCons.IM_STATUS).getData()));
      Element element = message.getElement(MessageCons.PASSWORD);
      String password = null;
      if (element != null) {
        password = new String(element.getData());
      } else {
        logger.error("PLEASE PROVIDE A PASSWORD !");
      }
      String username = fromaddress.substring(0, fromaddress.indexOf("@"));
      String realm = fromaddress.substring(fromaddress.indexOf("@") + 1);

      logger.debug("Adding user to registereduser list: " + fromaddress);
      RegisteredUser user =
          new RegisteredUser(
              username,
              realm,
              password,
              new AuthenticationProcess(this, username, realm, password));
      registeredUserList.addUser(user);

      try {
        sipRC.register("sip:" + fromaddress, false);
      } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }

      sendGatewayMessage();

      //            try{
      //                processMessage(fromaddress,"sip:gw@localhost","test");
      //            }
      //            catch (IMException e) {
      //            	e.printStackTrace();
      //            }
      //            NeighbourTuple ntuple = new NeighbourTuple(fromaddress, status);
      //            neighbours.put(fromaddress,ntuple);
      //
      //            imlistener.imRegistered(fromaddress, status);
    }
    // IM_TYPE_UNREG
    else if (type.equals(MessageCons.BYE)) {
      //            imlistener.imUnregistered(fromaddress);
      //
      //            neighbours.remove(fromaddress);
    }
    // IM_TYPE_NACK
    //        else if (type.equals(IM_TYPE_ALIVE))
    //        {
    //            int status = Integer.parseInt(new
    // String(message.getElement(IM_STATUS).getData()));
    //
    //            if (!neighbours.containsKey(fromaddress))
    //            {
    //                NeighbourTuple ntuple = new NeighbourTuple(fromaddress,status);
    //                neighbours.put(fromaddress, ntuple);
    //            }
    //        }
    // not known...
    else if (type.equals(MessageCons.PUBLISH)) {
      if (registeredUserList.hasUser(fromaddress)) {
        String status = new String(message.getElement(MessageCons.IM_STATUS).getData());

        sipPC.sendPublish("sip:" + fromaddress, status);
      }
    } else logger.debug("message type not known: " + type);
  }
  public void processResponse(ResponseEvent responseEvent) {
    Response response = responseEvent.getResponse();
    ClientTransaction clientTransaction = responseEvent.getClientTransaction();
    CSeqHeader cseqHeader = (CSeqHeader) response.getHeader(CSeqHeader.NAME);

    logger.debug("***** Response: " + response.getStatusCode() + " received by UA *****");
    logger.debug(response);
    logger.debug("***** Response dispatched *****");

    if (response.getStatusCode() == Response.PROXY_AUTHENTICATION_REQUIRED
        || response.getStatusCode() == Response.UNAUTHORIZED) {
      if (clientTransaction == null) {
        logger.info("Bad username ?");
      } else {
        try {
          logger.debug("IMUserAgent, processResponse(), Credentials to provide!");
          // WE start the authentication process!!!
          // Let's get the Request related to this response:
          Request clonedRequest = (Request) clientTransaction.getRequest().clone();
          if (clonedRequest == null) {
            logger.debug(
                "IMUserAgent, processResponse(), the request "
                    + " that caused the 407 has not been retrieved!!! Return cancelled!");
          } else {
            // Let's increase the Cseq:
            cseqHeader = (CSeqHeader) clonedRequest.getHeader(CSeqHeader.NAME);
            cseqHeader.setSequenceNumber(cseqHeader.getSequenceNumber() + 1);
            // Let's add a Proxy-Authorization header:
            // We send the informations stored:
            FromHeader fromHeader = (FromHeader) clonedRequest.getHeader(FromHeader.NAME);
            String fromURI = fromHeader.getAddress().getURI().toString();
            Header header;
            if (fromURI.equals(getLocalURI())) {
              header = authenticationProcess.getHeader(response);
            } else {
              String fromAddress = IMUtilities.getUsernameFromURI(fromURI);
              logger.info("Fetching credentials dor user: "******"ERROR, user not found");
                header = null;
              }
            }

            if (header == null) {
              logger.debug(
                  "IMUserAgent, processResponse(), Proxy-Authorization "
                      + " header is null, the request is not resent");
            } else {
              clonedRequest.setHeader(header);

              ClientTransaction newClientTransaction =
                  getSipProvider().getNewClientTransaction(clonedRequest);

              newClientTransaction.sendRequest();
              logger.debug(
                  "IMUserAgent, processResponse(), REGISTER "
                      + "with credentials sent:\n"
                      + clonedRequest);
            }
          }
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    } else {
      if (cseqHeader.getMethod().equals("REGISTER")) {
        //	logger.info("REGISTER Response received");
        sipRC.processResponse(responseEvent);
      } else if (cseqHeader.getMethod().equals("ACK")) {

      } else if (cseqHeader.getMethod().equals("MESSAGE")) {
        sipMC.processResponse(responseEvent);
      }
    }
  }