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
|
/*
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"
LISTCOMMANDS ListCommands = NULL;
PCommand knownCommands = NULL;
int cKnownCommands = 0;
HMODULE hCmdLineDLL = NULL;
char *GetMirandaFolder(char *mimFolder, int size)
{
strncpy_s(mimFolder, size, sdCmdLine->mimFolder, _TRUNCATE);
return mimFolder;
}
int ConnectToMiranda()
{
TCHAR tszPath[MAX_PATH];
GetModuleFileName(NULL, tszPath, _countof(tszPath));
TCHAR *p = _tcsrchr(tszPath, '\\');
if (p) p[1] = 0;
_tcsncat_s(tszPath, L"libs", _TRUNCATE);
DWORD cbPath = (DWORD)_tcslen(tszPath);
DWORD cbSize = GetEnvironmentVariable(L"PATH", NULL, 0);
TCHAR *ptszVal = new TCHAR[cbSize + MAX_PATH + 2];
_tcscpy(ptszVal, tszPath);
_tcscat(ptszVal, L";");
GetEnvironmentVariable(L"PATH", ptszVal + cbPath + 1, cbSize);
SetEnvironmentVariable(L"PATH", ptszVal);
delete[] ptszVal;
char pluginPath[1024];
GetMirandaFolder(pluginPath, sizeof(pluginPath));
mir_strcat(pluginPath, "\\plugins\\cmdline.dll");
ListCommands = NULL;
hCmdLineDLL = LoadLibraryA(pluginPath);
int failure = 1;
if (hCmdLineDLL)
ListCommands = (LISTCOMMANDS) GetProcAddress(hCmdLineDLL, "ListCommands");
if (ListCommands)
failure = 0;
return failure;
}
int DisconnectFromMiranda()
{
return FreeLibrary(hCmdLineDLL);
}
int GetKnownCommands()
{
ListCommands(&knownCommands, &cKnownCommands);
return (knownCommands == NULL);
}
int DestroyKnownCommands()
{
return 0;
}
PCommand GetCommand(char *command)
{
int i;
char lower[512];
strncpy_s(lower, command, _TRUNCATE);
_strlwr(lower);
for (i = 0; i < cKnownCommands; i++)
if (mir_strcmp(knownCommands[i].command, lower) == 0)
return &knownCommands[i];
//allow more parameters to trigger the help command - /h -h /? --help
if ((mir_strcmp(lower, "/h") == 0) || (mir_strcmp(lower, "-h") == 0) || (mir_strcmp(lower, "/?") == 0) || (mir_strcmp(lower, "--help") == 0))
for (i = 0; i < cKnownCommands; i++)
if (knownCommands[i].ID == MIMCMD_HELP)
return &knownCommands[i];
return NULL;
}
void HandleHelpCommand(PCommand, char *argv[], int argc, PReply reply)
{
CMStringA szReply;
if (argc >= 3) {
PCommand command = GetCommand(argv[2]);
if (command) {
reply->code = MIMRES_SUCCESS;
szReply.Append(Translate(command->help));
}
else {
reply->code = MIMRES_NOTFOUND;
szReply.AppendFormat(Translate("No help for '%s'."), argv[2]);
}
}
else {
reply->code = MIMRES_SUCCESS;
szReply.Append(Translate("Available commands: "));
for (int i = 0; i < cKnownCommands - 1; i++) {
szReply.Append(knownCommands[i].command);
szReply.Append(", ");
}
szReply.Append(knownCommands[cKnownCommands-1].command);
szReply.AppendChar('.');
}
strncpy_s(reply->message, szReply, _TRUNCATE);
}
PReply ParseCommand(char *argv[], int argc)
{
PCommand command = GetCommand(argv[1]);
if (!command)
return NULL;
PReply reply = &sdCmdLine->reply;
if (command->ID == MIMCMD_HELP)
HandleHelpCommand(command, argv, argc, reply);
else
ProcessConsoleCommand(command, argv, argc, reply);
return reply;
}
void FillSharedDataStruct(PCommand command, char *arguments[], int count)
{
for (int i = 0; i < count; i++)
strncpy_s(sdCmdLine->arguments[i], ARGUMENT_SIZE, arguments[i], _TRUNCATE);
sdCmdLine->cArguments = count;
sdCmdLine->command = *command;
*sdCmdLine->reply.message = 0;
sdCmdLine->reply.code =-1;
}
void ProcessConsoleCommand(PCommand command, char *arguments[], int count, PReply reply)
{
const HANDLE events[] = { heServerDone, heServerClose, heServerBufferFull };
const int cEvents = sizeof(events) / sizeof(events[0]);
if (WaitForSingleObject(hmClient, INFINITE) == WAIT_OBJECT_0) {//got the mutex, we're the only one who can talk to miranda now
FillSharedDataStruct(command, arguments, count);
SetEvent(heServerExec); //tell Miranda to process the request
int done = FALSE;
while (!done) {
// wait until server either finished processing or miranda was closed
switch (WaitForMultipleObjects(cEvents, events, FALSE, INFINITE)) {
case WAIT_OBJECT_0: //done event
done = TRUE;
break; //nothing to do
case WAIT_OBJECT_0 + 1: //close event
default:
mir_strcpy(sdCmdLine->reply.message, Translate("Miranda has been closed or an error has occurred while waiting for the result, could not process request."));
done = TRUE;
break;
case WAIT_OBJECT_0 + 2: //buffer full event
lpprintf("%s", reply->message);
break;
}
}
reply->code = sdCmdLine->reply.code;
strncpy_s(reply->message, sdCmdLine->reply.message, _TRUNCATE);
ReleaseMutex(hmClient); //let other possible clients talk to the server
}
else {
reply->code = -1;
*reply->message = 0;
}
}
|