summaryrefslogtreecommitdiff
path: root/plugins/Boltun/src/actionQueue.cpp
blob: d0ba9adae80127c2b3479afbba58d55ad27d9b60 (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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
//***********************************************************
//	Copyright  2003-2008 Alexander S. Kiselev, Valentin Pavlyuchenko
//
//	This file is part of Boltun.
//
//    Boltun 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.
//
//    Boltun 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 Boltun. If not, see <http://www.gnu.org/licenses/>.
//
//***********************************************************

#include "actionQueue.h"
#include "config.h"
#include "boltun.h"
#include "Engine/tstring.h"
#include "Engine/TalkEngine.h"

#include <list>
#include <set>
#include <time.h>
#include "Engine/CriticalSection.h"
#ifdef _DEBUG
#include <assert.h>
#endif

#define MIRANDA_VER 0x0A00
#include "newpluginapi.h"
#include "m_database.h"
#include "m_system.h"
#include "m_protosvc.h"

using namespace std;

extern TalkBot* bot;

typedef void (*ActionHandler)(HANDLE hContact, const TalkBot::MessageInfo *inf);

typedef struct _QueueElement {
	HANDLE hContact;
	const TalkBot::MessageInfo *inf;
	ActionHandler Handler;
	bool Sticky;
	int TimeOffset;
	_QueueElement(HANDLE contact, ActionHandler handler, int timeOffset, const TalkBot::MessageInfo *info = NULL, bool sticky = false)
		:hContact(contact), Handler(handler), TimeOffset(timeOffset), inf(info), Sticky(sticky)
	{
	}
} QueueElement;

static list<QueueElement> actionQueue;
static set<HANDLE> typingContacts;
UINT_PTR timerID = 0;

CriticalSection cs;
CriticalSection typingContactsLock;

void UpdateTimer();

VOID CALLBACK TimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
{
	cs.Enter();
	QueueElement q = actionQueue.front();
	actionQueue.pop_front();
	UpdateTimer();
	cs.Leave();
	q.Handler(q.hContact, q.inf);
}

void UpdateTimer()
{
	if (timerID)
		KillTimer(NULL, timerID);
	if (actionQueue.size())
		timerID = SetTimer(NULL, 0, actionQueue.front().TimeOffset, TimerProc);	
	else
		timerID = 0;
}

static bool NotifyTyping(HANDLE hContact)
{
	int res = DBGetContactSettingByte(hContact, "SRMsg", "SupportTyping", 2);
	if (res == 2)
		res = DBGetContactSettingByte(NULL, "SRMsg", "DefaultTyping", 1);
	return res != 0;
}

static char *MsgServiceName(HANDLE hContact)
{

    char szServiceName[100];
    char *szProto = GetContactProto(hContact);
    if (szProto == NULL)
        return PSS_MESSAGE;

    mir_snprintf(szServiceName, sizeof(szServiceName), "%s%sW", szProto, PSS_MESSAGE);
    if (ServiceExists(szServiceName))
        return PSS_MESSAGE "W";

    return PSS_MESSAGE;
}

static void TimerAnswer(HANDLE hContact, const TalkBot::MessageInfo* info)
{
	DBEVENTINFO ldbei;
	int size = (int)info->Answer.length() + 1;
	int bufsize = size;
	char* msg;

	bufsize *= sizeof(TCHAR) + 1;
	msg = new char[bufsize];
	//msg[size - 1] = '\0';
	
	if (!WideCharToMultiByte(CP_ACP, 0, info->Answer.c_str(), -1, msg, size, 
		NULL, NULL))
		FillMemory(msg, size - 1, '-'); //In case of fault return "----" in ANSI part
	CopyMemory(msg + size, info->Answer.c_str(), size * 2);


	CallContactService(hContact, MsgServiceName(hContact), PREF_TCHAR, (LPARAM)msg);

	ZeroMemory(&ldbei, sizeof(ldbei));
	ldbei.cbSize    = sizeof(ldbei);
	//FIXME: Error may happen
	ldbei.cbBlob    = bufsize;
	ldbei.pBlob     = (PBYTE)(void*)msg;
	ldbei.eventType = EVENTTYPE_MESSAGE;
	ldbei.flags     = DBEF_SENT;
	ldbei.szModule  = BOLTUN_NAME;
	ldbei.timestamp = (DWORD)time(NULL);

	CallService(MS_DB_EVENT_ADD, (WPARAM)hContact, (LPARAM)&ldbei);
	bot->AnswerGiven(hContact, *info);
	delete info;

	delete msg;

	typingContactsLock.Enter();
	typingContacts.erase(hContact);
	typingContactsLock.Leave();
}

static void StartTyping(HANDLE hContact, const TalkBot::MessageInfo*)
{
	CallService(MS_PROTO_SELFISTYPING, (WPARAM)hContact, 
		(LPARAM)PROTOTYPE_SELFTYPING_ON);
	typingContactsLock.Enter();
	typingContacts.insert(hContact);
	typingContactsLock.Leave();
}

void DoAnswer(HANDLE hContact, const TalkBot::MessageInfo *info, bool sticky = false)
{
	if (info->Answer[0] == _T('\0'))
		return;
	int waitTime, thinkTime = 0;
	int defWaitTime = Config.AnswerPauseTime * 1000;
	if (Config.PauseDepends)
		waitTime = defWaitTime * (int)info->Answer.length() / 25;
	else
		waitTime = defWaitTime;
	if (Config.PauseRandom)
	{
		//Let it be up to 4 times longer.
		waitTime =  waitTime * (rand() % 300) / 100 + waitTime;
	}
	if (waitTime == 0)
		waitTime = 50; //it's essential, because otherwise message will be added later 
					   //then its response, that will cause incorrect ordering of 
					   //messages in the opened history (reopening will
					   //help, but anyway it's no good)
	if (NotifyTyping(hContact) && Config.AnswerThinkTime)
	{
		thinkTime = Config.AnswerThinkTime * 1000;
		if (Config.PauseRandom)
		{
			//Let it be up to 4 times longer.
			thinkTime = thinkTime * (rand() % 300) / 100 + thinkTime;
		}				
	}
	cs.Enter();
	//Check if this contact's timer handler is now waiting for a cs.
	bool needTimerRearrange = false;
	if (!actionQueue.empty() && actionQueue.front().hContact == hContact)
	{
		needTimerRearrange = true;
		KillTimer(NULL, timerID);
		cs.Leave();
		cs.Enter();
	}
	if (!actionQueue.empty())
	{
		list<QueueElement>::iterator it = actionQueue.end();
		it--;
		while (true)
		{
			if ((*it).hContact == hContact)
			{
				if ((*it).Sticky)
					break;
				list<QueueElement>::iterator tmp = it;
				if (tmp != actionQueue.begin())
					tmp--;
				actionQueue.erase(it);
				it = tmp;
				if (actionQueue.empty())
					break;
			}
			if (it == actionQueue.begin())
				break;
			it--;
		}
	}
	typingContactsLock.Enter();
	if (typingContacts.find(hContact) != typingContacts.end())
	{
		CallService(MS_PROTO_SELFISTYPING, (WPARAM)hContact, (LPARAM)PROTOTYPE_SELFTYPING_OFF);
		typingContacts.erase(hContact);
	}
	typingContactsLock.Leave();
	if (actionQueue.empty())
		needTimerRearrange = true;
	if (thinkTime)
		actionQueue.push_back(QueueElement(hContact, StartTyping, thinkTime, NULL, sticky));
	actionQueue.push_back(QueueElement(hContact, TimerAnswer, waitTime, info, sticky));
	if (needTimerRearrange)
		UpdateTimer();
	cs.Leave();
}

void AnswerToContact(HANDLE hContact, const TCHAR* messageToAnswer)
{
	if (Config.TalkWarnContacts && DBGetContactSettingByte(hContact, BOLTUN_KEY, 
		DB_CONTACT_WARNED, FALSE) == FALSE)
	{
		DoAnswer(hContact, new TalkBot::MessageInfo((const TCHAR*)Config.WarnText), true);
		DBWriteContactSettingByte(hContact, BOLTUN_KEY, DB_CONTACT_WARNED, TRUE);
	}
	else
		DoAnswer(hContact, bot->Reply(hContact, messageToAnswer, false));
}

void StartChatting(HANDLE hContact)
{
	DoAnswer(hContact, new TalkBot::MessageInfo(bot->GetInitMessage(hContact)), true);
}