summaryrefslogtreecommitdiff
path: root/mBot/src/mbot/functions/mb_menu.cpp
blob: 09420811fbfb9717be463c2b545de7b4c1d35b4b (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
/*

Miranda Scripting Plugin for Miranda-IM
Copyright 2004-2006 Piotr Pawluczuk (www.pawluczuk.info)

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 "../functions.h"

ZEND_FUNCTION(mb_MenuAdd)
{
	zval*	cb = NULL;
	zval*	pb = NULL;
	char*	name = NULL;
	char*	proto = NULL;
	char	fname[24];
	char	root = 0;
	void*	temp = NULL;
	long	nl = 0;
	long	pl = 0;
	long	param = 0;
	long	icon = 0;
	long	flags = 0;
	long	position = 0x7ffffff;
	char	cm = 0;
	CLISTMENUITEM mi={0};
	sMFSync* mfs;

	mb_event* mbe = (mb_event*)(((sPHPENV*)SG(server_context))->c_param);

	if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "slz|llbslz!b",&name,&nl,&param,&cb,&icon,
		&flags,&cm,&proto,&pl,&position,&pb,&root)==FAILURE 
		|| !nl ||  cb->type != IS_STRING){
		PHP_FALSE_AND_WARNS(PHP_WARN_INVALID_PARAMS);
	}

	if(!zend_is_callable(cb,0,NULL)){
		PHP_FALSE_AND_WARNS(PHP_WARN_INVALID_PARAMS);
	}

	if(pb && (!cm || !zend_is_callable(pb,0,NULL))){
		PHP_FALSE_AND_WARNS(PHP_WARN_INVALID_PARAMS);
	}

	if(mbe->event == MBT_AUTOLOAD){
		mbe->php = sman_getbyfile((const char*)mbe->p3);
	}
	if(!mbe->php){
		PHP_FALSE_AND_WARNS(PHP_WARN_INVALID_PARAMS);
	}

	mfs = (sMFSync*)my_malloc(sizeof(sMFSync));
	if(!mfs){RETURN_FALSE;}
	memset(mfs,0,sizeof(sMFSync));

	mfs->php = mbe->php;

	strncpy(mfs->pszFunction,cb->value.str.val,sizeof(mfs->pszFunction));
	_snprintf(fname,sizeof(fname),"MBot/Menu/%.8x",mfs);

	temp = help_makefunct(mfs,(void*)help_callmenu);
	if(!temp){
		goto Error;
	}
	if(!CreateServiceFunction(fname,(MIRANDASERVICE)temp)){
		goto Error;
	}
	mi.cbSize = sizeof(mi);
	mi.position = position;
	mi.hIcon = (icon)?((HICON)icon):(hMBotIcon);
	mi.pszName = name;
	mi.pszService = fname;
	mi.flags = flags;

	if(cm){
		if(pl){
			mi.pszContactOwner = proto;
		}
		if(pb){
			strncpy(mfs->pszPrebuild,pb->value.str.val,sizeof(mfs->pszPrebuild));
			lm_flags |= MBOT_FLAG_WANTPREBUILD;
		}
	}else{
		mi.pszPopupName = (!root)?"MBot":NULL;
	}

	mfs->hMenu = CallService((cm)?(MS_CLIST_ADDCONTACTMENUITEM):(MS_CLIST_ADDMAINMENUITEM),0,(LPARAM)&mi);
	if(mfs->hMenu==NULL){
		DestroyServiceFunction(fname);
		goto Error;
	}
	g_mlist.Add(mfs);
	RETURN_LONG(mfs->hMenu);
Error:
	if(mbe->php){sman_decref(mbe->php);}
	my_memfree(mfs);
	if(temp){my_memfree(temp);}
	RETURN_FALSE;
}

ZEND_FUNCTION(mb_MenuModify)
{
	long  mid = 0;
	long  flags = 0xffffffff;
	char* name = 0;
	long  nl = 0;
	long  ico = 0;
	zval* cb = NULL;
	CLISTMENUITEM mi = {0};

	if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|sllz",&mid,&name,&nl,&ico,&flags,&cb)==FAILURE){
		PHP_FALSE_AND_WARNS(PHP_WARN_INVALID_PARAMS);
	}

	if(!mid){
		RETURN_NULL();
	}

	if(cb)
	{
		sMFSync* mfs;
		if(!zend_is_callable(cb,0,NULL)){
			PHP_FALSE_AND_ERRORS("$cb is expected to be a valid callback function!");
		}

		g_mlist.Lock();
		mfs = (sMFSync*)g_mlist.m_head;
		while(mfs)
		{
			if(mfs->hMenu == mid){
				strncpy(mfs->pszFunction,cb->value.str.val,sizeof(mfs->pszFunction)-1);
				break;
			}
			mfs = (sMFSync*)mfs->next;
		}
		g_mlist.Unlock();
	}

	mi.cbSize = sizeof(mi);

	if(ico){
		mi.hIcon = (HICON)ico;
		mi.flags |= CMIM_ICON;
	}
	if(nl){
		mi.pszName = name;
		mi.flags |= CMIM_NAME;
	}
	if(flags != 0xffffffff){
		mi.flags |= flags | CMIM_FLAGS;
	}

	RETURN_LONG(CallService(MS_CLIST_MODIFYMENUITEM,(WPARAM)mid,(LPARAM)&mi));
}