/*

MAgen.dll: Miranda plugin for mail.ru message exchange.

Copyright 2005-2010 Lenik.
Project home at http://www.lenik.ru/win32/magent
Author contact: lenik@lenik.ru

This program is free software.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

*/

unsigned long MRIM_do_login(unsigned char *user,unsigned char *password,unsigned long status)
{
//      LPS ## login ## email авторизующегося пользователя
//      LPS ## password ## пароль
//      UL ## status ## статус (см. MRIM_CS_CHANGE_STATUS)
//      LPS ## user_agent ## текстовое описание клиента пользователя, например "Mail.Ru Miranda Plugin v 1.0"
unsigned long seq;
static struct
    {
    mrim_packet_header_t hdr;
    unsigned char parameters[4096];
    }packet;

#ifdef MRIM_DEBUG
    PrintDebug("mrim_do_login: started\n");
#endif
packet.hdr.magic = CS_MAGIC;
packet.hdr.proto = PROTO_VERSION;
packet.hdr.seq = seq = GetSequenceNext();
packet.hdr.msg = MRIM_CS_LOGIN2;
packet.hdr.dlen = 0;
packet.hdr.from = 0;
packet.hdr.fromport = 0;

MRIM_Packet_AddString(&packet,user);
MRIM_Packet_AddString(&packet,password);
MRIM_Packet_AddLong(&packet,status);
MRIM_Packet_AddString(&packet,ModuleName_Text);

if(MRIM_sendpacket(&packet) == 0)
    {
    #ifdef MRIM_DEBUG
        PrintDebug("mrim_do_login: MRIM_CS_LOGIN2(%s,<pass>,%lu,%s) sended\n",user,status,ModuleName_Text);
    #endif
    //MessageBox(NULL,"sendpacket","MRIM_do_login",MB_OK);
    if(MRIM_receivepacket(&packet,sizeof(packet),seq,MRIM_GetTimeout()) == 0)
        {
        #ifdef MRIM_DEBUG
            PrintDebug("mrim_do_login: packet received\n");
        #endif
        //MessageBox(NULL,"receivepacket ok","MRIM_do_login",MB_OK);
        if(packet.hdr.msg == MRIM_CS_LOGIN_ACK)
            {
            #ifdef MRIM_DEBUG
                PrintDebug("mrim_do_login: MRIM_CS_LOGIN_ACK received\n");
            #endif
            //MessageBox(NULL,"MRIM_CS_LOGIN_ACK received","MRIM_do_login",MB_OK);
            #ifdef MRIM_DEBUG
                PrintDebug("mrim_do_login: ended ok\n");
            #endif
            return(0);
            }
        }
    }
#ifdef MRIM_DEBUG
    PrintDebug("mrim_do_login: ended with error\n");
#endif
return(1);
}
