/***************************************************************************
                          authentication.cpp  -  description
                             -------------------
    begin                : Thu Nov 20 2003
    copyright            : (C) 2003 by Wei Zheng
    email                : v_zheng@yahoo.com
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "authentication.h"

authentication::authentication(){

  memset(challenge,0,sizeof(challenge));
  memset(username,0,sizeof(username));
  memset(password,0,sizeof(password));
  memset(addr,0,sizeof(addr)); port = 0;
  memset(host,0,sizeof(host)); strcat(host,HOST);
  memset(url,0,sizeof(url));   strcat(url,URL);

  strcat(addr,HOST);
  port = 80;
}
authentication::~authentication(){
}

/** No descriptions */
void authentication::get_namepass(char * un, char * pw)
{
  strcat(username,un);
  strcat(password,pw);
}

/** No descriptions */
void authentication::get_challenge(char * ch){
  strcat(challenge,ch);
}

/** No descriptions */
void authentication::renew(char * newaddr,int newport)
{
  memset(addr,0,sizeof(addr));
  strcat(addr,newaddr);
  port = newport;
}

/** No descriptions */
int authentication::do_auth(){
  int result;
  while(1)
  {
    if(port==80)
    {
      memset(addr,0,sizeof(addr));
      strcat(addr,host);
    }
    if(make_connect(addr,port)==-1)
      return 1;
    send_request();
    result = get_response();
    make_close();
    if(result != 0)
      break;
  }
  return result;
}

/** No descriptions */
int authentication::send_request(){
  char line[560];

  memset(line,0,sizeof(line));
  sprintf(line,"GET http://%s/%s HTTP/1.0\r\n",host,url);
  send_line(line);

  memset(line,0,sizeof(line));
  sprintf(line,"Accept: */*\r\n");
  send_line(line);

  memset(line,0,sizeof(line));
  sprintf(line,"HOST: %s\r\n",host);
  send_line(line);

  memset(line,0,sizeof(line));
  sprintf(line,"User-Agent: Tcl http client package 2.4.1\r\n");
  send_line(line);

  memset(line,0,sizeof(line));
  sprintf(line,"Authorization: Passport1.4 OrgVerb=GET,OrgURL=http://messenger.msn.com,sign-in=%s,pwd=%s,{%s {}}\r\n",username,password,challenge);
  send_line(line);

  send_line("\r\n");
  return 0;
}

/** No descriptions */
int authentication::get_response(){
  int result;
  char line[560];
  char * p;

  while(1)
  {
    memset(line,0,sizeof(line));
    get_line(fd,line,sizeof(line));
    if(strlen(line)==0)
      break;
    else
    {
      if(strncmp(line,"Location:",9)==0)
      {

        p = strstr(line,"://") + 3*sizeof(char);
        memset(host,0,sizeof(host));
        strncat(host,p,strstr(p,"/")-p);

        p = strstr(p,"/") + sizeof(char);
        memset(url,0,sizeof(url));
        strcat(url,p);
      }
      if(strncmp(line,"Authentication-Info:",20)==0  || strncmp(line,"WWW-Authenticate:",17)==0)
      {
        if(strstr(line,"da-status=success")!=NULL)
        {
          result = 1;
          p = strstr(line,"from-PP=");
          if(p!=NULL) p = p + 9 * sizeof(char);
          memset(challenge,0,sizeof(challenge));
          strncat(challenge,p,strstr(p,"'")-p);
        }
        if(strstr(line,"da-status=failed")!=NULL)
        {
           result = -1;
        }
        if(strstr(line,"da-status=redir")!=NULL)
        {
           result = 0;
        }
      }
    }
  }
  return result;
}
