Skip to content

Commit fcce85c

Browse files
importossobomax
authored andcommitted
Add get_pseudoVar() and set_pseudoVar() to msg object.
Those functions allow Python code to access and manipulate pseudo-variables. PR #2317 +Documentation by: @sobomax
1 parent bdaaf60 commit fcce85c

File tree

2 files changed

+128
-28
lines changed

2 files changed

+128
-28
lines changed

modules/python/doc/python_admin.xml

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,19 @@
113113
</para></listitem>
114114
<listitem><para>
115115
<emphasis>getHeader()</emphasis> - returns the header of a message
116-
the destination address (&osips; address) of the message
117116
</para></listitem>
118117
<listitem><para>
119-
<emphasis>call_function()</emphasis> - calls a function from the script
118+
<emphasis>call_function()</emphasis> - calls built-in script function
119+
or function exported by other module
120+
</para></listitem>
121+
<listitem><para>
122+
<emphasis>get_pseudoVar(name)</emphasis> - returns the value of the
123+
the pseudo-variable specified by the <emphasis>name</emphasis> as
124+
Unicode string.
125+
</para></listitem>
126+
<listitem><para>
127+
<emphasis>set_pseudoVar(name, value)</emphasis> - sets pseudo-variable
128+
using Unicode string <emphasis>value</emphasis>.
120129
</para></listitem>
121130
</itemizedlist>
122131
</para>
@@ -218,36 +227,35 @@ modparam("python", "child_init_method", "child_initializer")
218227

219228
</section>
220229

221-
222230
<section id="exported_functions" xreflabel="exported_functions">
223-
<title>Exported Functions</title>
224-
<section id="func_python_exec" xreflabel="python_exec()">
231+
<title>Exported Functions</title>
232+
<section id="func_python_exec" xreflabel="python_exec()">
225233
<title>
226234
<function moreinfo="none">python_exec(method_name [, extra_args])</function>
227235
</title>
228-
<para>
229-
Thhis function is used to execute a method from the Python module
230-
loaded.
231-
</para>
232-
<para>
233-
This function can be used from REQUEST_ROUTE, ONREPLY_ROUTE,
234-
FAILURE_ROUTE and BRANCH_ROUTE.
235-
</para>
236-
<para>Meaning of the parameters is as follows:</para>
237-
<itemizedlist>
238-
<listitem>
239-
<para>
240-
<emphasis>method_name</emphasis> (string) - name of the method called
241-
</para>
242-
</listitem>
243-
<listitem>
244-
<para>
245-
<emphasis>extra_args</emphasis> (string, optional) - extra arguments that can
246-
be passed from the script to the python function.
247-
</para>
248-
</listitem>
249-
</itemizedlist>
250-
</section>
236+
<para>
237+
This function is used to execute a method from the Python module
238+
loaded.
239+
</para>
240+
<para>
241+
This function can be used from REQUEST_ROUTE, ONREPLY_ROUTE,
242+
FAILURE_ROUTE and BRANCH_ROUTE.
243+
</para>
244+
<para>Meaning of the parameters is as follows:</para>
245+
<itemizedlist>
246+
<listitem>
247+
<para>
248+
<emphasis>method_name</emphasis> (string) - name of the method called
249+
</para>
250+
</listitem>
251+
<listitem>
252+
<para>
253+
<emphasis>extra_args</emphasis> (string, optional) - extra arguments that can
254+
be passed from the script to the python function.
255+
</para>
256+
</listitem>
257+
</itemizedlist>
258+
</section>
251259
</section>
252260

253261
</chapter>

modules/python/python_msgobj.c

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,94 @@ msg_call_function(msgobject *self, PyObject *args)
365365
return PyLong_FromLong(rval);
366366
}
367367

368+
static PyObject *
369+
msg_get_pseudoVar(msgobject *self, PyObject *args)
370+
{
371+
str hmodel,rval;
372+
pv_elem_t *model;
373+
int buf_size = 4096;
374+
char *out;
375+
PyObject * res;
376+
377+
if (self->msg == NULL) {
378+
PyErr_SetString(PyExc_RuntimeError, "self->msg is NULL");
379+
Py_INCREF(Py_None);
380+
return Py_None;
381+
}
382+
383+
if(!PyArg_ParseTuple(args, "s", &hmodel.s)){
384+
Py_INCREF(Py_None);
385+
return Py_None;
386+
}
387+
388+
hmodel.len = strlen(hmodel.s);
389+
if (pv_parse_format(&hmodel, &model) < 0)
390+
{
391+
Py_INCREF(Py_None);
392+
return Py_None;
393+
}
394+
395+
out = pkg_malloc(buf_size);
396+
if (!out)
397+
{
398+
PyErr_SetString(PyExc_RuntimeError, "Not enough memor");
399+
Py_INCREF(Py_None);
400+
return Py_None;
401+
}
402+
403+
rval.len = pv_printf(self->msg, model, out, &buf_size) ;
404+
rval.s = out;
405+
rval.len = strlen(rval.s);
406+
res = PyUnicode_FromStringAndSize(rval.s, rval.len);
407+
pkg_free(out);
408+
pv_elem_free_all(model);
409+
return res;
410+
}
411+
412+
static PyObject *
413+
msg_set_pseudoVar(msgobject *self, PyObject *args)
414+
{
415+
str hmodel;
416+
pv_spec_t model;
417+
int retval;
418+
pv_value_t val;
419+
420+
if (self->msg == NULL) {
421+
PyErr_SetString(PyExc_RuntimeError, "self->msg is NULL");
422+
Py_INCREF(Py_None);
423+
return Py_None;
424+
}
425+
if(!PyArg_ParseTuple(args, "ss", &hmodel.s,&val.rs.s)){
426+
PyErr_SetString(PyExc_RuntimeError, "args is not valid");
427+
Py_INCREF(Py_None);
428+
return Py_None;
429+
}
430+
431+
hmodel.len = strlen(hmodel.s);
432+
val.rs.len = strlen(val.rs.s);
433+
if (pv_parse_spec(&hmodel, &model) < 0)
434+
{
435+
Py_INCREF(Py_None);
436+
return Py_None;
437+
}
438+
if(!pv_is_w(&model)){
439+
Py_INCREF(Py_None);
440+
return Py_None;
441+
}
442+
val.flags = PV_VAL_STR;
443+
retval = pv_set_value(self->msg, &model, EQ_T, &val);
444+
if (retval >= 0){
445+
Py_INCREF(Py_None);
446+
return Py_None;
447+
}else{
448+
Py_INCREF(Py_None);
449+
return Py_None;
450+
}
451+
Py_INCREF(Py_None);
452+
return Py_None;
453+
454+
}
455+
368456
PyDoc_STRVAR(copy_doc,
369457
"copy() -> msg object\n\
370458
\n\
@@ -380,6 +468,10 @@ static PyMethodDef msg_methods[] = {
380468
"Get SIP header field by name."},
381469
{"call_function", (PyCFunction)msg_call_function, METH_VARARGS,
382470
"Invoke function exported by the other module."},
471+
{"get_pseudoVar", (PyCFunction)msg_get_pseudoVar, METH_VARARGS,
472+
"calc variable "},
473+
{"set_pseudoVar", (PyCFunction)msg_set_pseudoVar, METH_VARARGS,
474+
"set variable "},
383475
{NULL, NULL, 0, NULL} /* sentinel */
384476
};
385477

0 commit comments

Comments
 (0)