Commit 706aed07 authored by vysheng's avatar vysheng

Merge pull request #603 from datamachine/master

Python version support fixes
parents 7600da74 4c5f9b79
To use python with client you should write python script. You can specify it from config ("python_script" option) or from command_line [-Z].
You should set the following callbacks in your script:
tgl.on_binlog_replay_end() - it is called when replay of old events end. Any updates prior this call were already received by this client
some time ago.
tgl.on_get_difference_end() - it is called after first get_difference call. So we received all updates after last client execute.
tgl.on_our_id(our_id) - Informs about id of currently logged in user.
tgl.on_msg_receive(msg) - it is called when we receive new msg (!! may be called before on_binlog_replay_end, than it is old msg).
tgl.on_user_update(peer, what_changed) - updated info about user. what_changed is array of strings.
tgl.on_chat_update(peer, what_changed) - updated info about user. what_changed is array of strings.
tgl.on_secret_chat_update(peer, what_changed) - updated info about user. what_changed is array of strings.
Also, you can call several functions. Each this function last two arguments, are callback, which is a python function implementing the callback from the function.
These functions may return false immidiately if something is bad with args, or return true and call cb_function in future.
The callback function should have one arguments: first success (True or False), and the rest depends on the call.
Functions that require a peer type other than what is passed will raise tgl.PeerError.
Function_list (arguments are listed aside from callback, import tgl for access) :
tgl.get_contact_list ()
tgl.get_dialog_list ()
tgl.rename_chat (peer, new_name)
tgl.chat_set_photo (peer, file)
tgl.send_typing (peer)
tgl.send_typing_abort (peer)
tgl.send_msg (peer, text)
tgl.fwd_msg (peer, msg)
tgl.send_photo (peer, file)
tgl.send_video (peer, file)
tgl.send_audio (peer, file)
tgl.send_document (peer, file)
tgl.send_text (peer, file)
tgl.load_photo(msg)
tgl.load_video(msg)
tgl.load_video_thumb(msg)
tgl.load_audio(msg)
tgl.load_document(msg)
tgl.load_document_thumb(msg)
tgl.info (peer)
tgl.get_history (peer, limit)
tgl.chat_add_user (peer, user)
tgl.chat_del_user (peer, user)
tgl.add_contact (phone, first_name, last_name)
tgl.rename_contact (phone, first_name, last_name)
tgl.msg_search (peer, text)
tgl.msg_global_search (text)
tgl.mark_read (peer)
tgl.set_profile_photo (file)
tgl.create_secret_chat (user)
tgl.create_group_chat (peer, name)
tgl.delete_msg (msg)
tgl.restore_msg (msg_id)
tgl.status_online ()
tgl.status_offline ()
tgl.send_location (peer, latitude, longitude)
Additionally, the tgl.Peer object has the following direct methods:
peer.rename_chat(new_name)
peer.chat_set_photo(file)
peer.send_typing()
peer.send_typing_abort()
peer.send_msg(text)
peer.send_photo(file)
peer.send_video(file)
peer.send_document(file)
peer.send_text(file)
peer.info()
peer.history(limit, offset)
peer.add_user(peer)
peer.del_user(peer)
peer.search(text)
peer.mark_read()
peer.send_location(latitude, longitude)
This diff is collapsed.
...@@ -64,6 +64,9 @@ if test x$ax_python_bin != x; then ...@@ -64,6 +64,9 @@ if test x$ax_python_bin != x; then
if test x$ax_python_lib == xno; then if test x$ax_python_lib == xno; then
AC_CHECK_LIB(${ax_python_bin}m, main, ax_python_lib=${ax_python_bin}m, ax_python_lib=no) AC_CHECK_LIB(${ax_python_bin}m, main, ax_python_lib=${ax_python_bin}m, ax_python_lib=no)
fi fi
if test x$ax_python_lib == xno; then
AC_CHECK_LIB(${ax_python_bin}mu, main, ax_python_lib=${ax_python_bin}mu, ax_python_lib=no)
fi
if test x$ax_python_lib != xno; then if test x$ax_python_lib != xno; then
ax_python_header=`$ax_python_bin -c "from distutils.sysconfig import *; print(get_config_var('CONFINCLUDEPY'))"` ax_python_header=`$ax_python_bin -c "from distutils.sysconfig import *; print(get_config_var('CONFINCLUDEPY'))"`
if test x$ax_python_header != x; then if test x$ax_python_header != x; then
......
...@@ -6069,6 +6069,46 @@ else ...@@ -6069,6 +6069,46 @@ else
ax_python_lib=no ax_python_lib=no
fi fi
fi
if test x$ax_python_lib == xno; then
as_ac_Lib=`$as_echo "ac_cv_lib_${ax_python_bin}mu''_main" | $as_tr_sh`
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for main in -l${ax_python_bin}mu" >&5
$as_echo_n "checking for main in -l${ax_python_bin}mu... " >&6; }
if eval \${$as_ac_Lib+:} false; then :
$as_echo_n "(cached) " >&6
else
ac_check_lib_save_LIBS=$LIBS
LIBS="-l${ax_python_bin}mu $LIBS"
cat confdefs.h - <<_ACEOF >conftest.$ac_ext
/* end confdefs.h. */
int
main ()
{
return main ();
;
return 0;
}
_ACEOF
if ac_fn_c_try_link "$LINENO"; then :
eval "$as_ac_Lib=yes"
else
eval "$as_ac_Lib=no"
fi
rm -f core conftest.err conftest.$ac_objext \
conftest$ac_exeext conftest.$ac_ext
LIBS=$ac_check_lib_save_LIBS
fi
eval ac_res=\$$as_ac_Lib
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_res" >&5
$as_echo "$ac_res" >&6; }
if eval test \"x\$"$as_ac_Lib"\" = x"yes"; then :
ax_python_lib=${ax_python_bin}mu
else
ax_python_lib=no
fi
fi fi
if test x$ax_python_lib != xno; then if test x$ax_python_lib != xno; then
ax_python_header=`$ax_python_bin -c "from distutils.sysconfig import *; print(get_config_var('CONFINCLUDEPY'))"` ax_python_header=`$ax_python_bin -c "from distutils.sysconfig import *; print(get_config_var('CONFINCLUDEPY'))"`
...@@ -6262,6 +6302,46 @@ else ...@@ -6262,6 +6302,46 @@ else
ax_python_lib=no ax_python_lib=no
fi fi
fi
if test x$ax_python_lib == xno; then
as_ac_Lib=`$as_echo "ac_cv_lib_${ax_python_bin}mu''_main" | $as_tr_sh`
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for main in -l${ax_python_bin}mu" >&5
$as_echo_n "checking for main in -l${ax_python_bin}mu... " >&6; }
if eval \${$as_ac_Lib+:} false; then :
$as_echo_n "(cached) " >&6
else
ac_check_lib_save_LIBS=$LIBS
LIBS="-l${ax_python_bin}mu $LIBS"
cat confdefs.h - <<_ACEOF >conftest.$ac_ext
/* end confdefs.h. */
int
main ()
{
return main ();
;
return 0;
}
_ACEOF
if ac_fn_c_try_link "$LINENO"; then :
eval "$as_ac_Lib=yes"
else
eval "$as_ac_Lib=no"
fi
rm -f core conftest.err conftest.$ac_objext \
conftest$ac_exeext conftest.$ac_ext
LIBS=$ac_check_lib_save_LIBS
fi
eval ac_res=\$$as_ac_Lib
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_res" >&5
$as_echo "$ac_res" >&6; }
if eval test \"x\$"$as_ac_Lib"\" = x"yes"; then :
ax_python_lib=${ax_python_bin}mu
else
ax_python_lib=no
fi
fi fi
if test x$ax_python_lib != xno; then if test x$ax_python_lib != xno; then
ax_python_header=`$ax_python_bin -c "from distutils.sysconfig import *; print(get_config_var('CONFINCLUDEPY'))"` ax_python_header=`$ax_python_bin -c "from distutils.sysconfig import *; print(get_config_var('CONFINCLUDEPY'))"`
......
...@@ -467,7 +467,8 @@ enum py_query_type { ...@@ -467,7 +467,8 @@ enum py_query_type {
pq_status_online, pq_status_online,
pq_status_offline, pq_status_offline,
pq_send_location, pq_send_location,
pq_extf pq_extf,
pq_import_chat_link
}; };
void py_empty_cb (struct tgl_state *TLSR, void *cb_extra, int success) { void py_empty_cb (struct tgl_state *TLSR, void *cb_extra, int success) {
...@@ -807,7 +808,7 @@ void py_do_all (void) { ...@@ -807,7 +808,7 @@ void py_do_all (void) {
break; break;
case pq_msg: case pq_msg:
if(PyArg_ParseTuple(args, "O!s#|OO", &tgl_PeerType, &peer, &str, &len, &cb_extra, &pyObj1)) { if(PyArg_ParseTuple(args, "O!s#|OO", &tgl_PeerType, &peer, &str, &len, &cb_extra, &pyObj1)) {
if(PyArg_ParseTuple(pyObj1, "ii", &preview, &reply_id)) { if(pyObj1 && PyArg_ParseTuple(pyObj1, "ii", &preview, &reply_id)) {
if(preview != -1) { if(preview != -1) {
if(preview) if(preview)
flags |= TGL_SEND_MSG_FLAG_ENABLE_PREVIEW; flags |= TGL_SEND_MSG_FLAG_ENABLE_PREVIEW;
...@@ -1062,7 +1063,13 @@ void py_do_all (void) { ...@@ -1062,7 +1063,13 @@ void py_do_all (void) {
break; break;
case pq_extf: case pq_extf:
if(PyArg_ParseTuple(args, "s#|O", &str, &len, &cb_extra)) if(PyArg_ParseTuple(args, "s#|O", &str, &len, &cb_extra))
tgl_do_send_extf (TLS, str, len, py_str_cb, &cb_extra); tgl_do_send_extf (TLS, str, len, py_str_cb, cb_extra);
else
PyErr_Print();
break;
case pq_import_chat_link:
if(PyArg_ParseTuple(args, "s#|O", &str, &len, &cb_extra))
tgl_do_import_chat_link (TLS, str, len, py_empty_cb, cb_extra);
else else
PyErr_Print(); PyErr_Print();
break; break;
...@@ -1146,6 +1153,7 @@ PyObject* py_status_online(PyObject *self, PyObject *args) { return push_py_func ...@@ -1146,6 +1153,7 @@ PyObject* py_status_online(PyObject *self, PyObject *args) { return push_py_func
PyObject* py_status_offline(PyObject *self, PyObject *args) { return push_py_func(pq_status_offline, args); } PyObject* py_status_offline(PyObject *self, PyObject *args) { return push_py_func(pq_status_offline, args); }
PyObject* py_send_location(PyObject *self, PyObject *args) { return push_py_func(pq_send_location, args); } PyObject* py_send_location(PyObject *self, PyObject *args) { return push_py_func(pq_send_location, args); }
PyObject* py_extf(PyObject *self, PyObject *args) { return push_py_func(pq_extf, args); } PyObject* py_extf(PyObject *self, PyObject *args) { return push_py_func(pq_extf, args); }
PyObject* py_import_chat_link(PyObject *self, PyObject *args) { return push_py_func(pq_import_chat_link, args); }
extern int safe_quit; extern int safe_quit;
extern int exit_code; extern int exit_code;
...@@ -1232,6 +1240,7 @@ static PyMethodDef py_tgl_methods[] = { ...@@ -1232,6 +1240,7 @@ static PyMethodDef py_tgl_methods[] = {
{"status_offline", py_status_offline, METH_VARARGS, ""}, {"status_offline", py_status_offline, METH_VARARGS, ""},
{"send_location", py_send_location, METH_VARARGS, ""}, {"send_location", py_send_location, METH_VARARGS, ""},
{"ext_function", py_extf, METH_VARARGS, ""}, {"ext_function", py_extf, METH_VARARGS, ""},
{"import_chat_link", py_import_chat_link, METH_VARARGS, ""},
{"set_on_binlog_replay_end", set_py_binlog_end, METH_VARARGS, ""}, {"set_on_binlog_replay_end", set_py_binlog_end, METH_VARARGS, ""},
{"set_on_get_difference_end", set_py_diff_end, METH_VARARGS, ""}, {"set_on_get_difference_end", set_py_diff_end, METH_VARARGS, ""},
{"set_on_our_id", set_py_our_id, METH_VARARGS, ""}, {"set_on_our_id", set_py_our_id, METH_VARARGS, ""},
......
...@@ -19,7 +19,6 @@ ...@@ -19,7 +19,6 @@
#include "python-tg.h" #include "python-tg.h"
extern struct tgl_state *TLS; extern struct tgl_state *TLS;
// TGL Python Exceptions // TGL Python Exceptions
extern PyObject *TglError; extern PyObject *TglError;
extern PyObject *PeerError; extern PyObject *PeerError;
...@@ -944,7 +943,10 @@ tgl_Peer_repr(tgl_Peer *self) ...@@ -944,7 +943,10 @@ tgl_Peer_repr(tgl_Peer *self)
switch(self->peer->id.type) { switch(self->peer->id.type) {
case TGL_PEER_USER: case TGL_PEER_USER:
ret = PyUnicode_FromFormat("<tgl.Peer: type=user, id=%ld, username=%R, name=%R, first_name=%R, last_name=%R, phone=%R>", #if PY_VERSION_HEX < 0x02070900
ret = PyUnicode_FromFormat("<tgl.Peer: id=%ld>", self->peer->id.id);
#else
ret = PyUnicode_FromFormat("<tgl.Peer: type=user, id=%ld, username=%R, name=%R, first_name=%R, last_name=%R, phone=%R>",
self->peer->id.id, self->peer->id.id,
PyObject_GetAttrString((PyObject*)self, "username"), PyObject_GetAttrString((PyObject*)self, "username"),
PyObject_GetAttrString((PyObject*)self, "name"), PyObject_GetAttrString((PyObject*)self, "name"),
...@@ -952,6 +954,7 @@ tgl_Peer_repr(tgl_Peer *self) ...@@ -952,6 +954,7 @@ tgl_Peer_repr(tgl_Peer *self)
PyObject_GetAttrString((PyObject*)self, "last_name"), PyObject_GetAttrString((PyObject*)self, "last_name"),
PyObject_GetAttrString((PyObject*)self, "phone") PyObject_GetAttrString((PyObject*)self, "phone")
); );
#endif
break; break;
case TGL_PEER_CHAT: case TGL_PEER_CHAT:
ret = PyUnicode_FromFormat("<tgl.Peer: type=chat, id=%ld, name=%s>", ret = PyUnicode_FromFormat("<tgl.Peer: type=chat, id=%ld, name=%s>",
...@@ -1423,7 +1426,9 @@ static PyObject * ...@@ -1423,7 +1426,9 @@ static PyObject *
tgl_Msg_repr(tgl_Msg *self) tgl_Msg_repr(tgl_Msg *self)
{ {
PyObject *ret; PyObject *ret;
#if PY_VERSION_HEX < 0x02070900
ret = PyUnicode_FromFormat("<tgl.Msg id=%ld>", self->msg->id);
#else
ret = PyUnicode_FromFormat("<tgl.Msg id=%ld, flags=%d, mention=%R, out=%R, unread=%R, service=%R, src=%R, " ret = PyUnicode_FromFormat("<tgl.Msg id=%ld, flags=%d, mention=%R, out=%R, unread=%R, service=%R, src=%R, "
"dest=%R, text=%R, media=%R, date=%R, fwd_src=%R, fwd_date=%R, reply_id=%R, reply=%R>", "dest=%R, text=%R, media=%R, date=%R, fwd_src=%R, fwd_date=%R, reply_id=%R, reply=%R>",
self->msg->id, self->msg->flags, self->msg->id, self->msg->flags,
...@@ -1441,7 +1446,7 @@ tgl_Msg_repr(tgl_Msg *self) ...@@ -1441,7 +1446,7 @@ tgl_Msg_repr(tgl_Msg *self)
PyObject_GetAttrString((PyObject*)self, "reply_id"), PyObject_GetAttrString((PyObject*)self, "reply_id"),
PyObject_GetAttrString((PyObject*)self, "reply") PyObject_GetAttrString((PyObject*)self, "reply")
); );
#endif
return ret; return ret;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment