/*

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.

*/

struct MRIM_SendMessageAckThreadParm
    {
    HANDLE hContact;
    unsigned long Seq;
    unsigned long thid;
    DBCONTACTGETSETTING cgs;
    DBVARIANT dbv;
    unsigned char Text[];
    };

void MRIM_SendMessageAckThread(void *parm)
{
struct MRIM_SendMessageAckThreadParm *p = parm;

#ifdef MRIM_DEBUG
    PrintDebug("MRIM_SendMessageAckThread: started\n");
#endif
if(MRIM_do_sendmessage(p->dbv.pszVal,p->Text) == 0)
    {
    #ifdef MRIM_DEBUG
        PrintDebug("MRIM_SendMessageAckThread: mrim_do_sendmessage(%s,%s) ok\n",p->dbv.pszVal,p->Text);
    #endif
    ProtoBroadcastAck(ProtoName,p->hContact,ACKTYPE_MESSAGE,ACKRESULT_SUCCESS,(HANDLE)(p->Seq),(LPARAM)"Ok.");
    }
else{
    //ProtoBroadcastAck(ProtoName,p->hContact,ACKTYPE_MESSAGE,ACKRESULT_FAILED,(HANDLE)(p->Seq),(LPARAM)Translate("Send Failed."));
    }
#ifdef MRIM_DEBUG
    PrintDebug("MRIM_SendMessageAckThread: returning memory\n");
#endif
DBFreeVariant(&(p->dbv));
free(p);
#ifdef MRIM_DEBUG
    PrintDebug("MRIM_SendMessageAckThread: ended\n");
#endif
}

int MRIM_SendMessage(WPARAM wParam, LPARAM lParam)
{
struct MRIM_SendMessageAckThreadParm *p;
CCSDATA* ccs = (CCSDATA*)lParam;
unsigned long Sequence = 0;

#ifdef MRIM_DEBUG
    PrintDebug("MRIM_SendMessage: started\n");
#endif
if((ccs->hContact)&&(ccs->lParam))
    {
    if((p=((struct MRIM_SendMessageAckThreadParm*)malloc(sizeof(struct MRIM_SendMessageAckThreadParm)+strlen((char*)ccs->lParam)+4))) != NULL)
        {
        #ifdef MRIM_DEBUG
            PrintDebug("MRIM_SendMessage: contact ok, malloc() ok\n");
        #endif
        p->cgs.szModule = ProtoName;
        p->cgs.szSetting = DB_SETTING_EMAIL;
        p->cgs.pValue = &(p->dbv);
        if(CallService(MS_DB_CONTACT_GETSETTING,(WPARAM)NULL,(LPARAM)&(p->cgs)) == 0)
            {
            DBWriteContactSettingString(NULL,ProtoName,"Nick",p->dbv.pszVal);
            #ifdef MRIM_DEBUG
                PrintDebug("MRIM_SendMessage: \"Nick\" (%s) is set.\n",p->dbv.pszVal);
            #endif
            DBFreeVariant(&(p->dbv));
            }
        p->cgs.szModule = ProtoName;
        p->cgs.szSetting = DB_SETTING_EMAIL;
        p->cgs.pValue = &(p->dbv);
        if(CallService(MS_DB_CONTACT_GETSETTING,(WPARAM)ccs->hContact,(LPARAM)&(p->cgs)) == 0)
            {
            p->hContact = ccs->hContact;
            p->Seq = Sequence = GetSequenceNext();
            strcpy(p->Text,(char*)ccs->lParam);
            #ifdef MRIM_DEBUG
                PrintDebug("MRIM_SendMessage: Creating send Thread\n");
            #endif
            CreateThread(NULL,0,(unsigned long (__stdcall *)(void *))MRIM_SendMessageAckThread,p,0,&(p->thid));
            }
        else{
            DBFreeVariant(&(p->dbv));
            free(p);
            }
        }
    }
#ifdef MRIM_DEBUG
    PrintDebug("MRIM_SendMessage: returning sequence %8.08X\n",Sequence);
#endif
return(Sequence);
}
