// This file is a part of the xMule Project.
//
// Copyright (c) 2004 Theodore R. Smith (hopeseekr@xmule.ws / http://www.xmule.ws/)
// DSA-1024 Fingerprint: 10A0 6372 9092 85A2 BB7F 907B CB8B 654B E33B F1ED
//
// Copyright (c) 2002 Merkur (merkur-@users.sourceforge.net / http://www.xmule-project.net)
// 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.
//
// 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.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

#ifdef PRECOMP
#	include "xmule-headers.h"
#else
#	include "Friend.h"
#	include "packets.h"
#	include "xmule.h"
#endif

#include "CFile.h"                          // CFile
#include "wintypes.h"
#include "updownclient.h"

CFriend::CFriend(void)
{
    m_dwLastSeen = 0;
    m_dwLastUsedIP = 0;
    m_nLastUsedPort = 0;
    m_dwLastChatted = 0;
    m_strName = "";
    m_LinkedClient = 0;
    m_dwHasHash = 0;
    memset(m_abyUserhash, 0, 16);
}

//Added this to work with the IRC.. Probably a better way to do it.. But wanted this in the release..
CFriend::CFriend(unsigned char tm_abyUserhash[16], uint32_t tm_dwLastSeen, uint32_t tm_dwLastUsedIP, uint32_t tm_nLastUsedPort, uint32_t tm_dwLastChatted, wxString tm_strName, uint32_t tm_dwHasHash)
{
    m_dwLastSeen = tm_dwLastSeen;
    m_dwLastUsedIP = tm_dwLastUsedIP;
    m_nLastUsedPort = tm_nLastUsedPort;
    m_dwLastChatted = tm_dwLastChatted;
    if (tm_dwHasHash)
    {
        memcpy(m_abyUserhash, tm_abyUserhash, 16);
        m_dwHasHash = 1;
    }
    else
    m_dwHasHash = 0;
    m_strName = tm_strName;
    m_LinkedClient = 0;
}

CFriend::CFriend(CUpDownClient *client)
{
    assert(client);
    //CTime lwtime;
    //mktime(lwtime.GetLocalTm());:
    m_dwLastSeen = time(NULL);
    m_dwLastUsedIP = client->GetIP();
    m_nLastUsedPort = client->GetUserPort();
    m_dwLastChatted = 0;
    if (client->GetUserName())
    m_strName = client->GetUserName();
    else
    m_strName = "";
    memcpy(m_abyUserhash, client->GetUserHash(), 16);
    m_LinkedClient = client;
    m_dwHasHash = 1;
}

CFriend::~CFriend(void)
{
}

void CFriend::LoadFromFile(CFile *file)
{
    try
    {
        if (16 != file->Read(m_abyUserhash, 16))
        throw CInvalidPacket();
        if (4 != file->Read( &m_dwLastUsedIP, 4))
        throw CInvalidPacket();
        if (2 != file->Read( &m_nLastUsedPort, 2))
        throw CInvalidPacket();
        if (4 != file->Read( &m_dwLastSeen, 4))
        throw CInvalidPacket();
        if (4 != file->Read( &m_dwLastChatted, 4))
        throw CInvalidPacket();
        m_dwHasHash = 1;
        uint32_t tagcount;
        if (4 != file->Read( &tagcount, 4))
        throw CInvalidPacket();
        for (uint32_t j = 0 ; j < tagcount ; j++)
        {
            try
            {
                CTag *newtag = new CTag(file);
                switch (newtag->tag->specialtag)
                {
                case FF_NAME:
                    if (newtag->GetType() != TAG_STRING)
                    throw CInvalidPacket("Invalid data type for friend name");
                    m_strName = newtag->tag->stringvalue;
                    delete newtag;
                    break;
                }
            }
            catch(CStrangePacket)
            {
            }
        }
    }
    catch(CInvalidPacket)
    {
        m_dwLastSeen = 0;
        m_dwLastUsedIP = 0;
        m_nLastUsedPort = 0;
        m_dwLastChatted = 0;
        m_strName = "";
        m_LinkedClient = 0;
        m_dwHasHash = 0;
        memset(m_abyUserhash, 0, 16);
        throw;
    }
}

void CFriend::WriteToFile(CFile *file)
{
    //	if (!m_dwHasHash)
    //		for( int i = 0; i < 16; i++ )
    //			m_abyUserhash[i] = 1;
    file->Write(m_abyUserhash, 16);
    file->Write( &m_dwLastUsedIP, 4);
    file->Write( &m_nLastUsedPort, 2);
    file->Write( &m_dwLastSeen, 4);
    file->Write( &m_dwLastChatted, 4);
    uint32_t tagcount = 0;
    if (!m_strName.IsEmpty())
    tagcount++;
    file->Write( &tagcount, 4);
    if (!m_strName.IsEmpty())
    {
        CTag nametag(FF_NAME, (char *) m_strName.GetData());
        nametag.WriteTagToFile(file);
    }
}

