/*

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_hello(void)
{
unsigned long seq;
static struct
    {
    mrim_packet_header_t hdr;
    unsigned long wd;
    }packet;

#ifdef MRIM_DEBUG
    PrintDebug("mrim_do_hello: started\n");
#endif
packet.hdr.magic = CS_MAGIC;
packet.hdr.proto = PROTO_VERSION;
packet.hdr.seq = seq = GetSequenceNext();
packet.hdr.msg = MRIM_CS_HELLO;
packet.hdr.dlen = 0;
packet.hdr.from = 0;
packet.hdr.fromport = 0;

if(MRIM_sendpacket(&packet) == 0)
    {
    #ifdef MRIM_DEBUG
        PrintDebug("mrim_do_hello: sended MRIM_CS_HELLO\n");
    #endif
    //MessageBox(NULL,"Send packet ok","Info",MB_OK);
    if(MRIM_receivepacket(&packet,sizeof(packet),seq,MRIM_GetTimeout()) == 0)
        {
        #ifdef MRIM_DEBUG
            PrintDebug("mrim_do_hello: packet received\n");
        #endif
        //MessageBox(NULL,"receive packet ok","Info",MB_OK);
        if(packet.hdr.msg == MRIM_CS_HELLO_ACK)
            {
            #ifdef MRIM_DEBUG
                PrintDebug("mrim_do_hello: MRIM_CS_HELLO_ACK received\n");
            #endif
            //MessageBox(NULL,"hello ack ok","Info",MB_OK);
            MRIM_WatchDogInterval = packet.wd;
            #ifdef MRIM_DEBUG
                PrintDebug("mrim_do_hello: ended ok\n");
            #endif
            return(0);
            }
        }
    }
#ifdef MRIM_DEBUG
    PrintDebug("mrim_do_hello: ended with error\n");
#endif
return(1);
}
