summaryrefslogtreecommitdiff
path: root/plugins/MirLua/src/m_schedule.cpp
blob: ce4e2ea8f934c6a1102aac7f0a1a98dcf8d1e8c3 (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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
#include "stdafx.h"

static mir_cs threadLock;
static HANDLE hScheduleEvent = NULL;
static HANDLE hScheduleThread = NULL;

struct ScheduleTask
{
	time_t startTime;
	time_t endTime;
	time_t interval;

	lua_State *L;
	lua_State *T;
	int threadRef;
	int callbackRef;
};

static int TaskCompare(const ScheduleTask *p1, const ScheduleTask *p2)
{
	return p1->startTime - p2->startTime;
}

static LIST<ScheduleTask> tasks(1, TaskCompare);

void DestroyTask(ScheduleTask *task)
{
	luaL_unref(task->L, LUA_REGISTRYINDEX, task->threadRef);
	delete task;
}

void ExecuteTaskThread(void *arg)
{
	ScheduleTask *task = (ScheduleTask*)arg;

	lua_rawgeti(task->T, LUA_REGISTRYINDEX, task->callbackRef);
	if (lua_pcall(task->T, 0, 2, 0))
		CallService(MS_NETLIB_LOG, (WPARAM)hNetlib, (LPARAM)lua_tostring(task->T, -1));

	if (task->interval == 0)
	{
		DestroyTask(task);
		return;
	}

	{
		mir_cslock lock(threadLock);

		time_t timestamp = time(NULL);
		if(task->startTime + task->interval >= timestamp)
			task->startTime += task->interval;
		else
			task->startTime = timestamp + task->interval;
		tasks.insert(task);
	}
	SetEvent(hScheduleEvent);
}

void ScheduleThread(void*)
{
	time_t waitTime = INFINITE;

	while (true)
	{
wait:	WaitForSingleObject(hScheduleEvent, waitTime);

		while (ScheduleTask *task = tasks[0])
		{
			mir_cslock lock(threadLock);

			time_t timestamp = time(NULL);
			if (task->startTime > timestamp)
			{
				waitTime = (task->startTime - timestamp - 1) * 1000;
				goto wait;
			}

			tasks.remove(task);

			if (task->endTime > 0 && task->endTime < timestamp)
			{
				DestroyTask(task);
				continue;
			}

			mir_forkthread(ExecuteTaskThread, task);
		}

		waitTime = INFINITE;
	}
}

void KillModuleScheduleTasks()
{
	mir_cslock lock(threadLock);

	while (ScheduleTask *task = tasks[0])
	{
		tasks.remove(task);
		DestroyTask(task);
	}
}

/***********************************************/

enum DayOfWeek
{
	None = -1,
	Sunday = 0,
	Monday = 1,
	Tuesday = 2,
	Wednesday = 3,
	Thursday = 4,
	Friday = 5,
	Saturday = 6
};

static int lua__Second(lua_State *L)
{
	luaL_checktype(L, 1, LUA_TTABLE);

	lua_pushinteger(L, 1);
	lua_setfield(L, -2, "Interval");
	lua_pushinteger(L, time(NULL));
	lua_setfield(L, -2, "Timestamp");
	lua_pushinteger(L, DayOfWeek::None);
	lua_setfield(L, -2, "DayOfWeek");

	return 1;
}

static int lua__Seconds(lua_State *L)
{
	luaL_checktype(L, 1, LUA_TTABLE);

	lua_pushliteral(L, "Interval");
	lua_gettable(L, -2);
	int seconds = luaL_optinteger(L, -1, 1);
	lua_pop(L, 1);
	lua_pushinteger(L, seconds);
	lua_setfield(L, -2, "Interval");
	lua_pushinteger(L, DayOfWeek::None);
	lua_setfield(L, -2, "DayOfWeek");

	return 1;
}

static int lua__Minute(lua_State *L)
{
	luaL_checktype(L, 1, LUA_TTABLE);

	lua_pushinteger(L, 60);
	lua_setfield(L, -2, "Interval");
	lua_pushinteger(L, DayOfWeek::None);
	lua_setfield(L, -2, "DayOfWeek");

	return 1;
}

static int lua__Minutes(lua_State *L)
{
	luaL_checktype(L, 1, LUA_TTABLE);

	lua_pushliteral(L, "Interval");
	lua_gettable(L, -2);
	int interval = luaL_optinteger(L, -1, 1);
	lua_pop(L, 1);
	lua_pushinteger(L, interval * 60);
	lua_setfield(L, -2, "Interval");
	lua_pushinteger(L, DayOfWeek::None);
	lua_setfield(L, -2, "DayOfWeek");

	return 1;
}

static int lua__Hour(lua_State *L)
{
	luaL_checktype(L, 1, LUA_TTABLE);

	lua_pushinteger(L, 60 * 60);
	lua_setfield(L, -2, "Interval");
	lua_pushinteger(L, DayOfWeek::None);
	lua_setfield(L, -2, "DayOfWeek");

	return 1;
}

static int lua__Hours(lua_State *L)
{
	luaL_checktype(L, 1, LUA_TTABLE);

	lua_pushliteral(L, "Interval");
	lua_gettable(L, -2);
	int interval = luaL_optinteger(L, -1, 1);
	lua_pop(L, 1);
	lua_pushinteger(L, interval * 60 * 60);
	lua_setfield(L, -2, "Interval");
	lua_pushinteger(L, DayOfWeek::None);
	lua_setfield(L, -2, "DayOfWeek");

	return 1;
}

static int lua__Day(lua_State *L)
{
	luaL_checktype(L, 1, LUA_TTABLE);

	lua_pushinteger(L, 60 * 60 * 24);
	lua_setfield(L, -2, "Interval");
	lua_pushinteger(L, DayOfWeek::None);
	lua_setfield(L, -2, "DayOfWeek");

	return 1;
}

static int lua__Days(lua_State *L)
{
	luaL_checktype(L, 1, LUA_TTABLE);

	lua_pushliteral(L, "Interval");
	lua_gettable(L, -2);
	int interval = luaL_optinteger(L, -1, 1);
	lua_pop(L, 1);
	lua_pushinteger(L, interval * 60 * 60 * 24);
	lua_setfield(L, -2, "Interval");
	lua_pushinteger(L, DayOfWeek::None);
	lua_setfield(L, -2, "DayOfWeek");

	return 1;
}

static int lua__Week(lua_State *L)
{
	luaL_checktype(L, 1, LUA_TTABLE);

	lua_pushinteger(L, 60 * 60 * 24 * 7);
	lua_setfield(L, -2, "Interval");
	lua_pushinteger(L, DayOfWeek::None);
	lua_setfield(L, -2, "DayOfWeek");

	return 1;
}

static int lua__Weeks(lua_State *L)
{
	luaL_checktype(L, 1, LUA_TTABLE);

	lua_pushliteral(L, "Interval");
	lua_gettable(L, -2);
	int interval = luaL_optinteger(L, -1, 1);
	lua_pop(L, 1);
	lua_pushinteger(L, interval * 60 * 60 * 24 * 7);
	lua_setfield(L, -2, "Interval");
	lua_pushinteger(L, DayOfWeek::None);
	lua_setfield(L, -2, "DayOfWeek");

	return 1;
}

static int lua__Monday(lua_State *L)
{
	luaL_checktype(L, 1, LUA_TTABLE);

	lua_pushinteger(L, 60 * 60 * 24 * 7);
	lua_setfield(L, -2, "Interval");
	lua_pushinteger(L, Monday);
	lua_setfield(L, -2, "DayOfWeek");

	return 1;
}

static int lua__Tuesday(lua_State *L)
{
	luaL_checktype(L, 1, LUA_TTABLE);

	lua_pushinteger(L, 60 * 60 * 24 * 7);
	lua_setfield(L, -2, "Interval");
	lua_pushinteger(L, Tuesday);
	lua_setfield(L, -2, "DayOfWeek");

	return 1;
}

static int lua__Wednesday(lua_State *L)
{
	luaL_checktype(L, 1, LUA_TTABLE);

	lua_pushinteger(L, 60 * 60 * 24 * 7);
	lua_setfield(L, -2, "Interval");
	lua_pushinteger(L, Wednesday);
	lua_setfield(L, -2, "DayOfWeek");

	return 1;
}

static int lua__Thursday(lua_State *L)
{
	luaL_checktype(L, 1, LUA_TTABLE);

	lua_pushinteger(L, 60 * 60 * 24 * 7);
	lua_setfield(L, -2, "Interval");
	lua_pushinteger(L, Thursday);
	lua_setfield(L, -2, "DayOfWeek");

	return 1;
}

static int lua__Friday(lua_State *L)
{
	luaL_checktype(L, 1, LUA_TTABLE);

	lua_pushinteger(L, 60 * 60 * 24 * 7);
	lua_setfield(L, -2, "Interval");
	lua_pushinteger(L, Friday);
	lua_setfield(L, -2, "DayOfWeek");

	return 1;
}

static int lua__Saturday(lua_State *L)
{
	luaL_checktype(L, 1, LUA_TTABLE);

	lua_pushinteger(L, 60 * 60 * 24 * 7);
	lua_setfield(L, -2, "Interval");
	lua_pushinteger(L, Saturday);
	lua_setfield(L, -2, "DayOfWeek");

	return 1;
}

static int lua__Sunday(lua_State *L)
{
	luaL_checktype(L, 1, LUA_TTABLE);

	lua_pushinteger(L, 60 * 60 * 24 * 7);
	lua_setfield(L, -2, "Interval");
	lua_pushinteger(L, Sunday);
	lua_setfield(L, -2, "DayOfWeek");

	return 1;
}

static int lua__From(lua_State *L)
{
	luaL_checktype(L, 1, LUA_TTABLE);

	time_t startTime, timestamp = time(NULL);
	switch (lua_type(L, 2))
	{
	case LUA_TNUMBER:
		startTime = luaL_optinteger(L, 2, timestamp);
		break;

	case LUA_TSTRING:
	{
		const char *strtime = luaL_optstring(L, 2, "00:00:00");

		int hour = 0, min = 0, sec = 0;
		sscanf_s(strtime, "%02d:%02d:%02d", &hour, &min, &sec);
		struct tm *ti = localtime(&timestamp);
		ti->tm_hour = hour;
		ti->tm_min = min;
		ti->tm_sec = sec;
		startTime = mktime(ti);
		break;
	}

	default:
		startTime = timestamp;
		break;
	}

	if (startTime < timestamp)
	{
		lua_pushliteral(L, "Interval");
		lua_gettable(L, 1);
		int interval = luaL_optinteger(L, -1, 1);
		lua_pop(L, 1);
		if (interval > 0)
			startTime += (((timestamp - startTime) / interval) + 1) * interval;
	}

	lua_pushvalue(L, 1);
	lua_pushinteger(L, startTime);
	lua_setfield(L, -2, "StartTime");

	return 1;
}

static int lua__Untill(lua_State *L)
{
	luaL_checktype(L, 1, LUA_TTABLE);

	time_t endTime = 0;
	switch (lua_type(L, 2))
	{
	case LUA_TNUMBER:
		endTime = luaL_optinteger(L, 2, 0);
		break;

	case LUA_TSTRING:
	{
		const char *strtime = luaL_optstring(L, 2, "00:00:00");

		int hour = 0, min = 0, sec = 0;
		sscanf_s(strtime, "%02d:%02d:%02d", &hour, &min, &sec);
		time_t timestamp = time(NULL);
		struct tm *ti = localtime(&timestamp);
		ti->tm_hour = hour;
		ti->tm_min = min;
		ti->tm_sec = sec;
		endTime = mktime(ti);
		break;
	}
	}

	lua_pushvalue(L, 1);
	lua_pushinteger(L, endTime);
	lua_setfield(L, -2, "EndTime");

	return 1;
}

static int lua__Do(lua_State *L)
{
	luaL_checktype(L, 1, LUA_TTABLE);
	luaL_checktype(L, 2, LUA_TFUNCTION);

	lua_pushliteral(L, "Interval");
	lua_gettable(L, 1);
	int interval = luaL_optinteger(L, -1, 0);
	lua_pop(L, 1);

	time_t timestamp = time(NULL);

	lua_pushliteral(L, "StartTime");
	lua_gettable(L, 1);
	time_t startTime = luaL_optinteger(L, -1, timestamp);
	lua_pop(L, 1);

	if (startTime < timestamp && interval == 0)
		return 0;

	lua_pushliteral(L, "DayOfWeek");
	lua_gettable(L, 1);
	DayOfWeek dayOfWeek = (DayOfWeek)luaL_optinteger(L, -1, DayOfWeek::None);
	lua_pop(L, 1);

	if (dayOfWeek > DayOfWeek::None && dayOfWeek <= DayOfWeek::Saturday)
	{
		struct tm *ti = localtime(&startTime);
		ti->tm_mday += abs((int)dayOfWeek - ti->tm_wday);
		startTime = mktime(ti);
	}

	lua_pushliteral(L, "EndTime");
	lua_gettable(L, 1);
	time_t endTime = luaL_optinteger(L, -1, 0);
	lua_pop(L, 1);

	if (endTime > 0 && endTime < timestamp)
		return 0;

	ScheduleTask *task = new ScheduleTask();
	task->startTime = startTime;
	task->endTime = endTime;
	task->interval = interval;
	task->L = L;
	lua_pushvalue(L, 2);
	task->callbackRef = luaL_ref(L, LUA_REGISTRYINDEX);
	task->T = lua_newthread(L);
	task->threadRef = luaL_ref(L, LUA_REGISTRYINDEX);
	{
		mir_cslock lock(threadLock);
		tasks.insert(task);
	}
	SetEvent(hScheduleEvent);

	return 0;
}

static const luaL_Reg schedule[] =
{
	{ "Second", lua__Second },
	{ "Seconds", lua__Seconds },
	{ "Minute", lua__Minute },
	{ "Minutes", lua__Minutes },
	{ "Hour", lua__Hour },
	{ "Hours", lua__Hours },
	{ "Day", lua__Day },
	{ "Days", lua__Days },
	{ "Week", lua__Week },
	{ "Weeks", lua__Weeks },
	{ "Monday", lua__Week },
	{ "Tuesday", lua__Tuesday },
	{ "Wednesday", lua__Wednesday },
	{ "Thursday", lua__Thursday },
	{ "Friday", lua__Friday },
	{ "Saturday", lua__Saturday },
	{ "Sunday", lua__Sunday },
	{ "From", lua__From },
	{ "Untill", lua__Untill },
	{ "Do", lua__Do },

	{ NULL, NULL }
};

/***********************************************/

static int lua__At(lua_State *L)
{
	time_t startTime, timestamp = time(NULL);
	switch (lua_type(L, 1))
	{
	case LUA_TNUMBER:
		startTime = luaL_optinteger(L, 1, timestamp);
		break;

	case LUA_TSTRING:
	{
		const char *strtime = luaL_optstring(L, 1, "00:00:00");

		int hour = 0, min = 0, sec = 0;
		sscanf_s(strtime, "%02d:%02d:%02d", &hour, &min, &sec);
		struct tm *ti = localtime(&timestamp);
		ti->tm_hour = hour;
		ti->tm_min = min;
		ti->tm_sec = sec;
		startTime = mktime(ti);
		break;
	}

	default:
		startTime = timestamp;
		break;
	}

	lua_newtable(L);
	lua_pushcclosure(L, lua__Untill, 0);
	lua_setfield(L, -2, "Untill");
	lua_pushcclosure(L, lua__Do, 0);
	lua_setfield(L, -2, "Do");
	lua_pushinteger(L, startTime);
	lua_setfield(L, -2, "StartTime");

	return 1;
}

static int lua__Every(lua_State *L)
{
	int interval = luaL_optinteger(L, 1, 0);

	luaL_newlib(L, schedule);
	lua_pushinteger(L, interval);
	lua_setfield(L, -2, "Interval");

	return 1;
}

static const luaL_Reg scheduleApi[] =
{
	{ "At", lua__At },
	{ "Every", lua__Every },

	{ NULL, NULL }
};

LUAMOD_API int luaopen_m_schedule(lua_State *L)
{
	luaL_newlib(L, scheduleApi);

	if (hScheduleEvent == NULL)
		hScheduleEvent = CreateEvent(NULL, FALSE, FALSE, NULL);

	if (hScheduleThread == NULL)
		hScheduleThread = mir_forkthread(ScheduleThread);

	return 1;
}