static CURLcode smtp_authenticate(struct connectdata *conn)
{
  CURLcode result = CURLE_OK;
  struct smtp_conn *smtpc = &conn->proto.smtpc;
  char *initresp = NULL;
  const char *mech = NULL;
  size_t len = 0;
  smtpstate state1 = SMTP_STOP;
  smtpstate state2 = SMTP_STOP;

  /* Check we have a username and password to authenticate with and end the
     connect phase if we don't. */
  if(!conn->bits.user_passwd) {
    state(conn, SMTP_STOP);

    return result;
  }

  /* Check supported authentication mechanisms by decreasing order of
     security. */
#ifndef CURL_DISABLE_CRYPTO_AUTH
  if(smtpc->authmechs & SMTP_AUTH_CRAM_MD5) {
    mech = "CRAM-MD5";
    state1 = SMTP_AUTHCRAM;
    smtpc->authused = SMTP_AUTH_CRAM_MD5;
  }
  else
#endif
#ifdef USE_NTLM
  if(smtpc->authmechs & SMTP_AUTH_NTLM) {
    mech = "NTLM";
    state1 = SMTP_AUTHNTLM;
    state2 = SMTP_AUTHNTLM_TYPE2MSG;
    smtpc->authused = SMTP_AUTH_NTLM;
    result = smtp_auth_ntlm_type1_message(conn, &initresp, &len);
  }
  else
#endif
  if(smtpc->authmechs & SMTP_AUTH_LOGIN) {
    mech = "LOGIN";
    state1 = SMTP_AUTHLOGIN;
    state2 = SMTP_AUTHPASSWD;
    smtpc->authused = SMTP_AUTH_LOGIN;
    result = smtp_auth_login_user(conn, &initresp, &len);
  }
  else if(smtpc->authmechs & SMTP_AUTH_PLAIN) {
    mech = "PLAIN";
    state1 = SMTP_AUTHPLAIN;
    state2 = SMTP_AUTH;
    smtpc->authused = SMTP_AUTH_PLAIN;
    result = smtp_auth_plain_data(conn, &initresp, &len);
  }
  else {
    infof(conn->data, "No known auth mechanisms supported!\n");
    result = CURLE_LOGIN_DENIED;      /* Other mechanisms not supported. */
  }

  if(!result) {
    if(initresp &&
       strlen(mech) + len <= 512 - 8) { /* AUTH <mech> ...<crlf> */
       result = Curl_pp_sendf(&smtpc->pp, "AUTH %s %s", mech, initresp);

      if(!result)
        state(conn, state2);
    }
    else {
      result = Curl_pp_sendf(&smtpc->pp, "AUTH %s", mech);

      if(!result)
        state(conn, state1);
    }
    Curl_safefree(initresp);
  }

  return result;
}
static CURLcode smtp_authenticate(struct connectdata *conn)
{
  CURLcode result = CURLE_OK;
  struct smtp_conn *smtpc = &conn->proto.smtpc;
  char *initresp = NULL;
  const char *mech = NULL;
  size_t len = 0;
  smtpstate state1 = SMTP_STOP;
  smtpstate state2 = SMTP_STOP;

   
  if(!conn->bits.user_passwd) {
    state(conn, SMTP_STOP);

    return result;
  }

   
#ifndef CURL_DISABLE_CRYPTO_AUTH
  if(smtpc->authmechs & SMTP_AUTH_CRAM_MD5) {
    mech = "CRAM-MD5";
    state1 = SMTP_AUTHCRAM;
    smtpc->authused = SMTP_AUTH_CRAM_MD5;
  }
  else
#endif
#ifdef USE_NTLM
  if(smtpc->authmechs & SMTP_AUTH_NTLM) {
    mech = "NTLM";
    state1 = SMTP_AUTHNTLM;
    state2 = SMTP_AUTHNTLM_TYPE2MSG;
    smtpc->authused = SMTP_AUTH_NTLM;
    result = smtp_auth_ntlm_type1_message(conn, &initresp, &len);
  }
  else
#endif
  if(smtpc->authmechs & SMTP_AUTH_LOGIN) {
    mech = "LOGIN";
    state1 = SMTP_AUTHLOGIN;
    state2 = SMTP_AUTHPASSWD;
    smtpc->authused = SMTP_AUTH_LOGIN;
    result = smtp_auth_login_user(conn, &initresp, &len);
  }
  else if(smtpc->authmechs & SMTP_AUTH_PLAIN) {
    mech = "PLAIN";
    state1 = SMTP_AUTHPLAIN;
    state2 = SMTP_AUTH;
    smtpc->authused = SMTP_AUTH_PLAIN;
    result = smtp_auth_plain_data(conn, &initresp, &len);
  }
  else {
    infof(conn->data, "No known auth mechanisms supported!\n");
    result = CURLE_LOGIN_DENIED;       
  }

  if(!result) {
    if(initresp &&
       strlen(mech) + len <= 512 - 8) {  
       result = Curl_pp_sendf(&smtpc->pp, "AUTH %s %s", mech, initresp);

      if(!result)
        state(conn, state2);
    }
    else {
      result = Curl_pp_sendf(&smtpc->pp, "AUTH %s", mech);

      if(!result)
        state(conn, state1);
    }
    Curl_safefree(initresp);
  }

  return result;
}