summaryrefslogtreecommitdiff
path: root/plugins/CmdLine/src/services.cpp
blob: 56c768878a91a94a4fdf1f246cfe3715485b7cb5 (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
/*
CmdLine plugin for Miranda IM

Copyright © 2007 Cristian Libotean

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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#include "stdafx.h"

int bWaitForUnload = 0;

int InitServices()
{
	return 0;
}

int DestroyServices()
{
	return 0;
}

extern "C" __declspec(dllexport) void ProcessConsoleCommand(PCommand command, TArgument *arguments, int count, PReply reply)
{
	HandleCommand(command, arguments, count, reply);
}

void __cdecl ServerWorkerThread(void*)
{
	int done = FALSE;
	const HANDLE events[] = {heServerExec, heServerClose};
	const int cEvents = sizeof(events) / sizeof(events[0]);

	while (!done)
	{
		switch (WaitForMultipleObjects(cEvents, events, FALSE, INFINITE))
		{
			case WAIT_OBJECT_0: //need to process something
			{
				ProcessConsoleCommand(&sdCmdLine->command, sdCmdLine->arguments, sdCmdLine->cArguments, &sdCmdLine->reply);
				SetEvent(heServerDone); //notify the client we've finished

				break;
			}

			case WAIT_OBJECT_0 + 1: //server is closing
			{
				done = TRUE; //stop the thread

				break;
			}
		}
	}
}

int StartServer()
{
	int failure = 1;
	if (sdCmdLine)
	{
		if (sdCmdLine->instances == 0)
		{
			HANDLE server = mir_forkthread(ServerWorkerThread, nullptr);
			if (server)
			{
				char path[MIMFOLDER_SIZE];
				GetModuleFileNameA(GetModuleHandle(nullptr), path, sizeof(path));
				char *p = strrchr(path, '\\');
				if (p) { *p = 0; }
				strncpy_s(sdCmdLine->mimFolder, path, _TRUNCATE);
				sdCmdLine->instances++;

				failure = 0;
			}
			else PUShowMessageT(TranslateT("Could not create CommandLine listening server!"), SM_WARNING);
		}
		else MessageBox(nullptr, TranslateT("You can only run one instance of CmdLine plugin."), TranslateT("Error"), MB_ICONERROR | MB_OK);
	}

	return failure;
}

extern "C" __declspec(dllexport) void ListCommands(PCommand * commands, int *count)
{
	*commands = mimCommands;
	*count = cMimCommands;
}