aboutsummaryrefslogtreecommitdiffstats
path: root/src/modules/pythoncore/kvircmodule.cpp
blob: da22310f9eeac69967de7bb50b0bfd4d9d46a010 (about) (plain) (blame)
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
//=============================================================================
//
//   File : kvircmodule.cpp
//   Creation date : Wed Nov 19 19:11:29 2008 GMT by Elvio Basello
//
//   This file is part of the KVirc irc client distribution
//   Copyright (C) 2008 Elvio Basello (hellvis69 at netsons dot org)
//
//   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 opinion) 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 "kvi_settings.h"

#ifdef COMPILE_PYTHON_SUPPORT

#include <Python.h>

#define KVIRC_MODULE

#include "kvircmodule.h"
#include "kvi_window.h"
#include "kvi_userinput.h"
#include "kvi_kvs_runtimecontext.h"
#include "kvi_kvs_script.h"


static KviKvsRunTimeContext * g_pCurrentKvsContext = 0;
static KviStr g_szLastReturnValue("");

/*
static int PyKVIrc_Echo(const char * pcCmd)
{
	return system(pcCmd);
}
*/

static PyObject * PyKVIrc_echo(PyObject * pSelf, PyObject * pArgs)
{
	const char * pcText;
	int iColorSet;
	const char * pcWinId;

	if(!PyArg_ParseTuple(pArgs,"s|is",&pcText,&iColorSet,&pcWinId))
		return 0;

	if(g_pCurrentKvsContext && pcText)
	{
		KviWindow * pWnd;
		if(pcWinId)
		{
			pWnd = g_pApp->findWindow(pcWinId);
			if(!pWnd)
				pWnd = g_pCurrentKvsContext->window();
		} else {
			pWnd = g_pCurrentKvsContext->window();
		}
		pWnd->outputNoFmt(iColorSet,QString::fromUtf8(pcText));
	}

	//sts = PyKVIrc_System(command);
	//return Py_BuildValue("i", sts);
	return Py_BuildValue("i",1);
}

static PyObject * PyKVIrc_say(PyObject * pSelf, PyObject * pArgs)
{
	const char * pcText;
	const char * pcWinId;

	if(!PyArg_ParseTuple(pArgs,"s|s",&pcText,&pcWinId))
		return 0;

	if(g_pCurrentKvsContext && pcText)
	{
		KviWindow * pWnd;
		if(pcWinId)
		{
			pWnd = g_pApp->findWindow(pcWinId);
			if(!pWnd)
				pWnd = g_pCurrentKvsContext->window();
		} else {
			pWnd = g_pCurrentKvsContext->window();
		}
		QString szTmp = QString::fromUtf8(pcText);
		KviUserInput::parse(szTmp,pWnd);
	}

	return Py_BuildValue("i",1);
}

static PyObject * PyKVIrc_warning(PyObject * pSelf, PyObject * pArgs)
{
}

static PyObject * PyKVIrc_getLocal(PyObject * pSelf, PyObject * pArgs)
{
}

static PyObject * PyKVIrc_setLocal(PyObject * pSelf, PyObject * pArgs)
{
}

static PyObject * PyKVIrc_getGlobal(PyObject * pSelf, PyObject * pArgs)
{
}

static PyObject * PyKVIrc_setGlobal(PyObject * pSelf, PyObject * pArgs)
{
}

static PyObject * PyKVIrc_eval(PyObject * pSelf, PyObject * pArgs)
{
	const char * pcCode;
	char * pcRetVal;

	if(!PyArg_ParseTuple(pArgs,"s",&pcCode))
		return 0;

	if(g_pCurrentKvsContext && pcCode)
	{
		KviKvsVariant ret;
		if(KviKvsScript::run(QString::fromUtf8(pcCode),g_pCurrentKvsContext->window(),0,&ret))
		{
			QString szTmp;
			ret.asString(szTmp);
			g_szLastReturnValue = szTmp;
		} else {
			g_szLastReturnValue = "";
		}
		pcRetVal = g_szLastReturnValue.ptr();
	} else {
		pcRetVal = "";
	}

	return Py_BuildValue("s",pcRetVal);
}

static PyObject * PyKVIrc_internalWarning(PyObject * pSelf, PyObject * pArgs)
{
}

static PyMethodDef KVIrcMethods[] = {
	{ "KVIrc::echo", PyKVIrc_echo, METH_VARARGS,
		"Outputs a text to a KVIrc window" },
	{ "KVIrc::say", PyKVIrc_say, METH_VARARGS,
		"Types text in a window" },
	{ "KVIrc::warning", PyKVIrc_warning, METH_VARARGS,
		"Prints a warning message" },
	{ "KVIrc::getLocal", PyKVIrc_getLocal, METH_VARARGS,
		"Gets a local variable" },
	{ "KVIrc::setLocal", PyKVIrc_setLocal, METH_VARARGS,
		"Sets a local variable" },
	{ "KVIrc::getGlobal", PyKVIrc_getGlobal, METH_VARARGS,
		"Gets a global variable" },
	{ "KVIrc::setGlobal", PyKVIrc_setGlobal, METH_VARARGS,
		"Sets a global variable" },
	{ "KVIrc::eval", PyKVIrc_eval, METH_VARARGS,
		"Change the behaviour of a set of commands" },
	{ "KVIrc::internalWarning", PyKVIrc_internalWarning, METH_VARARGS,
		"" },
	{ NULL, NULL, 0, NULL }
};

PyMODINIT_FUNC python_init()
{
	static void * PyKVIrc_API[PyKVIrc_API_NUM];
	PyObject * pModule;
	PyObject * pC_API_Object;

	pModule = Py_InitModule("kvirc",KVIrcMethods);
	if(!pModule) return;

	// Initialize the C API pointer array
	PyKVIrc_API[0] = (void *)PyKVIrc_echo;
	PyKVIrc_API[1] = (void *)PyKVIrc_say;
	PyKVIrc_API[2] = (void *)PyKVIrc_warning;
	PyKVIrc_API[3] = (void *)PyKVIrc_getLocal;
	PyKVIrc_API[4] = (void *)PyKVIrc_setLocal;
	PyKVIrc_API[5] = (void *)PyKVIrc_getGlobal;
	PyKVIrc_API[6] = (void *)PyKVIrc_setGlobal;
	PyKVIrc_API[7] = (void *)PyKVIrc_eval;
	PyKVIrc_API[8] = (void *)PyKVIrc_internalWarning;

	// Create a CObject containing the API pointer array's address
	pC_API_Object = PyCObject_FromVoidPtr((void *)PyKVIrc_API,NULL);

	if(pC_API_Object)
		PyModule_AddObject(pModule,"_C_API",pC_API_Object);
}

#endif