summaryrefslogtreecommitdiff
path: root/protocols/EmLanProto/src/mlan.h
blob: 21471cdc2f390ab4e38b0d328ad7d1aad66b8abb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
//////////////////////////////////////////////////////////////////////////
// Miranda lan functions

#ifndef __mlan_h__
#define __mlan_h__

#include "lan.h"

#define MLAN_TIMEOUT 5
#define MLAN_CHECK 50
#define MLAN_SLEEP 400
#define MAX_HOSTNAME_LEN 128

#define FILE_SEND_BLOCK 4096
//#define FILE_SEND_BLOCK 32768
#define FILE_INFO_REFRESH 131072
#define FILE_MIN_BLOCK 1024

class CMLan;

typedef struct 
{
	PROTOSEARCHRESULT hdr;
	u_long ipaddr;
	WORD stat;
	u_long ver;
} EMPSEARCHRESULT;

struct TDataHolder
{
public:
	long id;
	long op;

	HANDLE hContact;
	char* const msg;
	CMLan* lan;

	explicit TDataHolder(const CCSDATA* cc,unsigned long _id, long _op, CMLan* _lan):
	msg(_strdup((char*)cc->lParam)),hContact(cc->hContact),id(_id),op(_op),lan(_lan)
	{}
	explicit TDataHolder(const char* str,unsigned long _id, long _op, CMLan* _lan):
	msg(_strdup(str)),hContact(0), id(_id), op(_op), lan(_lan)
	{}
	~TDataHolder(){delete[] msg;}
};

class CMLan : public CLan
{
public:
	CMLan();
	~CMLan();

	int GetMirandaStatus();
	void SetMirandaStatus(u_int status);
	void SetAllOffline();
	void RecvMessageUrl(CCSDATA* ccs);
	int SendMessageUrl(CCSDATA* ccs, bool isUrl);
	int GetAwayMsg(CCSDATA* ccs);
	int RecvAwayMsg(CCSDATA* ccs);
	int SetAwayMsg(u_int status, char* msg);

	int AddToContactList(u_int flags, EMPSEARCHRESULT* psr);
	int Search(const char* name);
	void LoadSettings();
	void SaveSettings();

	char* GetName() { return m_name; }
	bool GetUseHostName() { return m_UseHostName; }
	void SetUseHostName(bool val) { m_UseHostName = val; }
	void SetRequiredIp(u_long ip) { m_RequiredIp = ip; }

	int SendFile(CCSDATA* ccs);
	void RecvFile(CCSDATA* ccs);
	int FileAllow(CCSDATA* ccs);
	int FileDeny(CCSDATA* ccs);
	int FileCancel(CCSDATA* ccs);
	int FileResume(int cid, PROTOFILERESUME* pfr);

protected:
	virtual void OnRecvPacket(u_char* mes, int len, in_addr from);
	virtual void OnInTCPConnection(u_long addr, SOCKET in_socket);
	virtual void OnOutTCPConnection(u_long addr, SOCKET out_socket, LPVOID lpParameter);
private:
	struct TContact
	{
		in_addr m_addr;
		u_int m_status;
		int m_time;
		u_long m_ver;
		char* m_nick;
		TContact* m_prev;
	};
	u_int m_mirStatus;
	TContact* m_pRootContact;
	HANDLE m_hCheckThread;

	char m_name[MAX_HOSTNAME_LEN];
	int m_nameLen;

	CRITICAL_SECTION m_csAccessClass;
	CRITICAL_SECTION m_csReceiveThreadLock;
	CRITICAL_SECTION m_csAccessAwayMes;

	void RequestStatus(bool answer=false, u_long m_addr=INADDR_BROADCAST);
	HANDLE FindContact(in_addr addr, const char* nick, bool add_to_list, bool make_permanent, bool make_visible, u_int status = ID_STATUS_ONLINE);
	void DeleteCache();

	void StartChecking();
	void StopChecking();
	static DWORD WINAPI CheckProc(LPVOID lpParameter);
	void Check();

	int m_handleId;
	int GetRandomProcId() { return m_handleId++; } // TODO: must create propper CRITICAL SECTION, cause there may be collisions

	static DWORD WINAPI LaunchExt(LPVOID lpParameter);
	void SearchExt(TDataHolder* hold);
	void SendMessageExt(TDataHolder* hold);
	void GetAwayMsgExt(TDataHolder* hold);

	struct TPacket
	{
		u_int idVersion; // -1 means version is not sent
		u_int idStatus; // -1 means status is not sent
		char* strName; // NULL means no name
		bool flReqStatus; // false means no request
		char* strMessage; // NULL means no message
		int idMessage;
		bool flIsUrl; // true if message is an URL
		int idAckMessage; // 0 means no ack
		int idReqAwayMessage; // 0 means no request
		char* strAwayMessage; // NULL means no away message
		int idAckAwayMessage;
	};
	u_char* CreatePacket(TPacket& pak, int* pBufLen=NULL);
	void ParsePacket(TPacket& pak, u_char* buf, int len=65536);
	void SendPacketExt(TPacket& pak, u_long addr);

	bool m_UseHostName;
	u_long m_RequiredIp;

	HANDLE m_hookIcqMsgReq;
	char* m_amesAway;
	char* m_amesNa;
	char* m_amesOccupied;
	char* m_amesDnd;
	char* m_amesFfc;


	struct TFileConnection
	{
		enum enumFileConnectionStates
		{
			FCS_OK = 0,
			FCS_TERMINATE,
			FCS_ALLOW,

			FCS_OVERWRITE,
			FCS_RESUME,
			FCS_RENAME,
			FCS_SKIP,
		};

		TFileConnection();
		~TFileConnection();
		void Lock() { EnterCriticalSection(&m_csAccess); }
		void Unlock() { LeaveCriticalSection(&m_csAccess); }
		void Terminate() { Lock(); m_state = FCS_TERMINATE; Unlock(); }
		int Recv(bool halt=true);
		int Send(u_char* buf, int size);
		int SendRaw(u_char* buf, int size);

		CMLan* m_pLan;
		TFileConnection* m_pPrev;
		TFileConnection* m_pNext;
		int m_state;
		int m_cid;
		SOCKET m_socket;
		u_long m_addr;
		HANDLE m_hContact;

		char* m_szDescription;
		char** m_szFiles;
		char* m_szDir;
		char* m_szRenamedFile;

		u_char* m_buf;
		int m_recSize;

		CRITICAL_SECTION m_csAccess;
	};
	void FileAddToList(TFileConnection* conn);
	void FileRemoveFromList(TFileConnection* conn);

	CRITICAL_SECTION m_csFileConnectionList;
	TFileConnection* m_pFileConnectionList;
};

#endif //__mlan_h__