1 /**
2  * Copyright (c) 2005 Kanru Chen  <koster@debian.org.tw>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18 
19 #include <Python.h>
20 #include <cstring>
21 #include <vector>
22 #include "script/api.h"
23 #include "telnetcon.h"
24 
25 static PyObject*
SendUnEscapedString(PyObject * self,PyObject * args)26 SendUnEscapedString(PyObject *self, PyObject *args)
27 {
28 	char *pstr;
29 	long lp;
30 
31 	if (!PyArg_ParseTuple(args, "ls", &lp, &pstr))
32 		return NULL;
33 
34 	string str(pstr);
35 
36 	((CTelnetCon*)lp)->SendUnEscapedString(str);
37 
38 	Py_INCREF(Py_None);
39 	return Py_None;
40 }
41 
42 static PyObject*
SendString(PyObject * self,PyObject * args)43 SendString(PyObject *self, PyObject *args)
44 {
45 	char *pstr;
46 	long lp;
47 
48 	if (!PyArg_ParseTuple(args, "ls", &lp, &pstr))
49 		return NULL;
50 
51 	string str(pstr);
52 
53 	((CTelnetCon*)lp)->SendString(str);
54 
55 	Py_INCREF(Py_None);
56 	return Py_None;
57 }
58 
59 static PyMethodDef PCManXMethods[] = {
60 	{"SendString", SendString, METH_VARARGS,
61 		"Send String to Instance Window."},
62 	{"SendUnEscapedString", SendUnEscapedString, METH_VARARGS,
63 		"Send un escaped string to Instance Window."},
64 	{NULL, NULL, 0, NULL}
65 };
66 
67 vector<PyObject*> pModules;
InitScriptInterface(const char * path)68 void InitScriptInterface(const char *path)
69 {
70 	char *cmd;
71 	PyObject *pName;
72 
73 	Py_Initialize();
74 	PyRun_SimpleString("import sys\n");
75 	cmd = (char*)malloc(sizeof(char)*(strlen(path) + strlen("sys.path.insert(0,'')")));
76 	sprintf(cmd, "sys.path.insert(0,'%s')", path);
77 	PyRun_SimpleString(cmd);
78 	free(cmd);
79 
80 	Py_InitModule("PCManX", PCManXMethods);
81 
82 	/* FIXME: Load Plugins from _path_ */
83 
84 	pName = PyString_FromString("orz");
85 	pModules.push_back(PyImport_Import(pName));
86 	Py_DECREF(pName);
87 	if (PyErr_Occurred())
88 		PyErr_Print();
89 }
90 
ScriptOnNewIncomingMessage(void * handle,const char * text)91 void ScriptOnNewIncomingMessage(void *handle, const char *text)
92 {
93 	PyObject *pHandle, *pFunc, *pArgs, *pMsg, *pDict;
94 
95 	vector<PyObject*>::iterator pModule;
96 	for( pModule = pModules.begin(); pModule != pModules.end(); ++pModule )
97 	{
98 		if (*pModule != NULL)
99 			pDict = PyModule_GetDict(*pModule);
100 		else
101 		{
102 			printf("Failed to PyImport_Import\n");
103 		}
104 
105 		if(pDict == NULL )
106 		{
107 			printf("Failed to PyModule_GetDict\n");
108 		}
109 
110 		if(!*pModule || !pDict)
111 		{
112 			printf("Failed to load script\n");
113 			return;
114 		}
115 		pFunc = PyDict_GetItemString(pDict, "OnNewIncomingMessage");
116 
117 		if (pFunc && PyCallable_Check(pFunc)) {
118 			pArgs = PyTuple_New(2);
119 			pHandle = PyInt_FromLong((long)handle);
120 			pMsg = PyString_FromString(text);
121 			if (!pMsg || !pHandle) {
122 				Py_DECREF(pArgs);
123 				fprintf(stderr, "Cannot convert argument\n");
124 				return;
125 			}
126 			PyTuple_SetItem(pArgs, 0, pHandle);
127 			PyTuple_SetItem(pArgs, 1, pMsg);
128 			PyObject_CallObject(pFunc, pArgs);
129 			Py_DECREF(pArgs);
130 			Py_DECREF(pMsg);
131 		}
132 		else {
133 			if (PyErr_Occurred())
134 				PyErr_Print();
135 			fprintf(stderr, "Cannot find function \"%s\"\n", "OnNewIncomingMessage");
136 		}
137 	}
138 }
139 
FinalizeScriptInterface()140 void FinalizeScriptInterface()
141 {
142 	vector<PyObject*>::iterator pModule;
143 	for( pModule = pModules.begin(); pModule != pModules.end(); ++pModule )
144 		if(*pModule)
145 			Py_DECREF(*pModule);
146 	Py_Finalize();
147 }
148