summaryrefslogtreecommitdiff
path: root/plugins/modernb/m_api/m_variables.h
blob: 73ab9a4f1734462111475dc6c6392bb38cb78113 (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
/*
    Variables Plugin for Miranda-IM (www.miranda-im.org)
    Copyright 2003-2005 P. Boon

    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
*/
#ifndef __M_VARS
#define __M_VARS

#define FIF_TCHAR	0x01 // expects and returns TCHAR*

typedef struct {
	int cbSize;
	int flags;
	union {
		char *szFormat;
		TCHAR *tszFormat;
	};
	union {
		char *szSource;
		TCHAR *tszSource;
	};
	HANDLE hContact;
	int pCount;  // number of succesful parses
	int eCount;	 // number of failures
} FORMATINFO;
#define MS_VARS_FORMATSTRING			"Vars/FormatString"

#define CI_PROTOID		0x00000001
#define CI_NICK			0x00000002
#define CI_LISTNAME		0x00000004
#define CI_FIRSTNAME	0x00000008
#define CI_LASTNAME		0x00000010
#define CI_EMAIL		0x00000020
#define CI_UNIQUEID		0x00000040
#define CI_TCHAR		0x80000000 // tszContact is of type TCHAR
typedef struct {
	int cbSize;
	union {
		char *szContact;
		TCHAR *tszContact;
	};
	HANDLE *hContacts;
	DWORD flags;
} CONTACTSINFO;

// wparam = (CONTACTSINFO *)
// lparam = 0
// returns number of contacts found, hContacts array contains these hContacts
#define MS_VARS_GETCONTACTFROMSTRING	"Vars/GetContactFromString"

#define AIF_DONTPARSE		0x01	// don't parse the result of this function
#define AIF_FALSE			0x02	// (logical) false
//#define AIF_ERR_ARGS		0x04	// invalid arguments

typedef struct {
	int cbSize;			// in
	FORMATINFO *fi;		// in
	unsigned int argc;	// in
	union {
		char **argv;		// in  (argv[0] will be the tokenstring)
		TCHAR **targv;
	};
	int flags;			// out (AIF_*)
} ARGUMENTSINFO;

#define TR_MEM_VARIABLES	1		// the memory will be allocated in Variable's memory space (not implemented)
#define TR_MEM_MIRANDA		2		// the memory will be allocated in Miranda's memory space, 
									// if TRF_FREEMEM is set, the memory will be freed by Variables.
#define TR_MEM_OWNER		3		// the memory is in the caller's memory space, Variables won't touch it,
									// even if TRF_FREEMEM is set

#define TRF_FREEMEM			0x01	// free the memory if possible
#define TRF_CALLSVC			0x02	// cal szCleanupService when Variables doesn't need the result anymore
#define TRF_UNPARSEDARGS	0x04	// give the arguments unparsed (if available)
#define TRF_FIELD			0x08	// the token can be used as a %field%
#define TRF_FUNCTION		0x10	// the token can be used as a ?function()
#define TRF_TCHAR			0x20	// the token is given as TCHAR* and the service returns TCHAR*

typedef struct {
	int cbSize;
	union {
		char *szTokenString;	// non-built-in variable WITHOUT '%' or '?' or '!'
		TCHAR *tszTokenString;
	};
	char *szService;		// service to call, must return a 0 terminating string or NULL on error, will be called
							// with wparam = 0 and lparam = ARGUMENTSINFO *ai
	char *szCleanupService;	// only if flag VRF_CALLSVC is set, will be called when variable copied the result 
							// in it's own memory. wParam = 0, lParam = result from szService call.
	char *szHelpText;		// shown in help dialog, maybe NULL or in format 
							// "subject\targuments\tdescription" ("math\t(x, y ,...)\tx + y + ...") or 
							// "subject\tdescription" ("miranda\tpath to the Miranda-IM executable")
							// subject and description are translated automatically
	int memType;			// set to TR_MEM_* if you use the flag VRF_FREEMEM
	int flags;				// one of TRF_*
} TOKENREGISTER;

// wparam = 0
// lparam = (LPARAM)&TOKENREGISTER
// returns 0 on success
#define MS_VARS_REGISTERTOKEN		"Vars/RegisterToken"

// wparam = (void *)pnt
// lparam = 0
// free the memory from variables' memory space pointed by pnt
#define MS_VARS_FREEMEMORY				"Vars/FreeMemory"

// Returns Variable's RTL/CRT function poiners to malloc() free() realloc()
// wParam=0, lParam = (LPARAM) &MM_INTERFACE (see m_system.h)
// copied from Miranda's core (miranda.c)
#define MS_VARS_GET_MMI			"Vars/GetMMI"

__inline static void variables_free(void *ptr) {

	if (ptr) {
		struct MM_INTERFACE mm;
		mm.cbSize=sizeof(struct MM_INTERFACE);
		CallService(MS_VARS_GET_MMI,0,(LPARAM)&mm);
		mm.mmi_free(ptr);
	}
}

// wparam = (HWND)hwnd (may be NULL)
// lparam = (char *)string (may be NULL)
// when [ok] is pressed in the help box, hwnd's text will be set to the text in the help box.
// the text of hwnd is set as initial text if string is NULL
// string is set as initial value of the text box and EN_CHANGE will be send
// returns the handle to the help dialog. Only one can be opened at a time.
// WARNING: This service has not yet been converted to UNICODE, it will certainly change in the near future
#define MS_VARS_SHOWHELP		"Vars/ShowHelp"


// don't use these yet, WIP
#define VARM_PARSE			WM_USER+1
#define VARM_SETSUBJECT		WM_USER+2
#define VARM_GETSUBJECT		WM_USER+3
#define VARM_SETEXTRATEXT	WM_USER+4
#define VARM_GETEXTRATEXT	WM_USER+5

/*** HELPERS ***/

__inline static char *variables_parseA(char *src, char *extra, HANDLE hContact) {

	FORMATINFO fi;

	ZeroMemory(&fi, sizeof(fi));
	fi.cbSize = sizeof(fi);
	fi.szFormat = src;
	fi.szSource = extra;
	fi.hContact = hContact;
	return (char *)CallService(MS_VARS_FORMATSTRING, (WPARAM)&fi, 0);
}

__inline static char *variables_parseT(TCHAR *src, TCHAR *extra, HANDLE hContact) {

	FORMATINFO fi;

	ZeroMemory(&fi, sizeof(fi));
	fi.cbSize = sizeof(fi);
	fi.tszFormat = src;
	fi.tszSource = extra;
	fi.hContact = hContact;
	fi.flags = FIF_TCHAR;
	return (char *)CallService(MS_VARS_FORMATSTRING, (WPARAM)&fi, 0);
}

#ifdef UNICODE
#define variables_parse(x,y,z) variables_parseT(x,y,z)
#else
#define variables_parse(x,y,z) variables_parseA(x,y,z)
#endif

/*
Returns copy of src if Variables is not installed, or a copy of the parsed string if it is installed.
If the returned value is not NULL, it must be free using your own free().
*/
__inline static char *variables_parsedupA(char *src, char *extra, HANDLE hContact) {

	FORMATINFO fi;
	char *parsed, *res;

	if (src == NULL) {
		return NULL;
	}
	if (!ServiceExists(MS_VARS_FORMATSTRING)) {
		return _strdup(src);
	}
	res = NULL;
	ZeroMemory(&fi, sizeof(fi));
	fi.cbSize = sizeof(fi);
	fi.szFormat = src;
	fi.szSource = extra;
	fi.hContact = hContact;
	parsed = (char *)CallService(MS_VARS_FORMATSTRING, (WPARAM)&fi, 0);
	if (parsed != NULL) {
		res = _strdup(parsed);
		CallService(MS_VARS_FREEMEMORY, (WPARAM)parsed, 0);
	}
	else {
		return _strdup(src);
	}
	return res;
}

/*
Returns copy of src if Variables is not installed, or a copy of the parsed string if it is installed.
If the returned value is not NULL, it must be free using your own free().
*/
__inline static TCHAR *variables_parsedupT(TCHAR *src, TCHAR *extra, HANDLE hContact) {

	FORMATINFO fi;
	TCHAR *parsed, *res;

	if (src == NULL) {
		return NULL;
	}
	if (!ServiceExists(MS_VARS_FORMATSTRING)) {
		return _tcsdup(src);
	}
	res = NULL;
	ZeroMemory(&fi, sizeof(fi));
	fi.cbSize = sizeof(fi);
	fi.tszFormat = src;
	fi.tszSource = extra;
	fi.hContact = hContact;
	fi.flags |= FIF_TCHAR;
	parsed = (TCHAR *)CallService(MS_VARS_FORMATSTRING, (WPARAM)&fi, 0);
	if (parsed != NULL) {
		res = _tcsdup(parsed);
		CallService(MS_VARS_FREEMEMORY, (WPARAM)parsed, 0);
	}
	else {
		return _tcsdup(src);
	}
	return res;
}

#ifdef UNICODE
#define variables_parsedup(x,y,z) variables_parsedupT(x,y,z)
#else
#define variables_parsedup(x,y,z) variables_parsedupA(x,y,z)
#endif

#endif