Migrate API to completely use new python objects.

This commit is contained in:
Vincent Castellano 2015-05-19 23:09:46 -07:00
parent fe030a69e2
commit 7f2fc476eb
4 changed files with 334 additions and 118 deletions

View File

@ -735,6 +735,8 @@ void py_str_cb (struct tgl_state *TLSR, void *cb_extra, int success, const char
Py_XDECREF(callable);
}
#define PY_PEER_ID(x) (tgl_peer_id_t)((tgl_Peer*)x)->peer->id
void py_do_all (void) {
int p = 0;
while (p < pos) {
@ -744,6 +746,10 @@ void py_do_all (void) {
PyObject *args = (PyObject *)py_ptr[p ++];
const char *str, *str1, *str2, *str3;
Py_ssize_t i;
tgl_user_id_t *ids;
int len, len1, len2, len3;
int limit, offset;
long msg_id;
@ -751,61 +757,87 @@ void py_do_all (void) {
PyObject *pyObj2 = NULL;
PyObject *cb_extra = NULL;
//struct tgl_message *M;
tgl_peer_id_t peer, peer1;
PyObject *peer = NULL;
PyObject *peer1 = NULL;
switch (f) {
case pq_contact_list:
PyArg_ParseTuple(args, "|O", &cb_extra);
tgl_do_update_contact_list (TLS, py_contact_list_cb, cb_extra);
if(PyArg_ParseTuple(args, "|O", &cb_extra))
tgl_do_update_contact_list (TLS, py_contact_list_cb, cb_extra);
else
PyErr_Print();
break;
case pq_dialog_list:
PyArg_ParseTuple(args, "|O", &cb_extra);
tgl_do_get_dialog_list (TLS, 100, 0, py_dialog_list_cb, cb_extra);
if(PyArg_ParseTuple(args, "|O", &cb_extra))
tgl_do_get_dialog_list (TLS, 100, 0, py_dialog_list_cb, cb_extra);
else
PyErr_Print();
break;
case pq_msg:
PyArg_ParseTuple(args, "iis#|O", &peer.type, &peer.id, &str, &len, &cb_extra);
tgl_do_send_message (TLS, peer, str, len, 0, py_msg_cb, cb_extra);
if(PyArg_ParseTuple(args, "O!s#|O", &tgl_PeerType, &peer, &str, &len, &cb_extra))
tgl_do_send_message (TLS, PY_PEER_ID(peer), str, len, 0, py_msg_cb, cb_extra);
else
PyErr_Print();
break;
case pq_send_typing:
PyArg_ParseTuple(args, "ii|O", &peer.type, &peer.id, &cb_extra);
tgl_do_send_typing (TLS, peer, tgl_typing_typing, py_empty_cb, cb_extra);
if(PyArg_ParseTuple(args, "O!|O", &tgl_PeerType, &peer, &cb_extra))
tgl_do_send_typing (TLS, PY_PEER_ID(peer), tgl_typing_typing, py_empty_cb, cb_extra);
else
PyErr_Print();
break;
case pq_send_typing_abort:
PyArg_ParseTuple(args, "ii|O", &peer.type, &peer.id, &cb_extra);
tgl_do_send_typing (TLS, peer, tgl_typing_cancel, py_empty_cb, cb_extra);
if(PyArg_ParseTuple(args, "O!|O", &tgl_PeerType, &peer, &cb_extra))
tgl_do_send_typing (TLS, PY_PEER_ID(peer), tgl_typing_cancel, py_empty_cb, cb_extra);
else
PyErr_Print();
break;
case pq_rename_chat:
PyArg_ParseTuple(args, "iis#|O", &peer.type, &peer.id, &str, &len, &cb_extra);
tgl_do_rename_chat (TLS, peer, str, len, py_empty_cb, cb_extra);
if(PyArg_ParseTuple(args, "O!s#|O", &tgl_PeerType, &peer, &str, &len, &cb_extra))
tgl_do_rename_chat (TLS, PY_PEER_ID(peer), str, len, py_empty_cb, cb_extra);
else
PyErr_Print();
break;
case pq_send_photo:
PyArg_ParseTuple(args, "iis|O", &peer.type, &peer.id, &str, &cb_extra);
tgl_do_send_document (TLS, peer, str, NULL, 0, TGL_SEND_MSG_FLAG_DOCUMENT_PHOTO, py_msg_cb, cb_extra);
if(PyArg_ParseTuple(args, "O!s|O", &tgl_PeerType, &peer, &str, &cb_extra))
tgl_do_send_document (TLS, PY_PEER_ID(peer), str, NULL, 0, TGL_SEND_MSG_FLAG_DOCUMENT_PHOTO, py_msg_cb, cb_extra);
else
PyErr_Print();
break;
case pq_send_video:
PyArg_ParseTuple(args, "iis|O", &peer.type, &peer.id, &str, &cb_extra);
tgl_do_send_document (TLS, peer, str, NULL, 0, TGL_SEND_MSG_FLAG_DOCUMENT_VIDEO, py_msg_cb, cb_extra);
if(PyArg_ParseTuple(args, "O!s|O", &tgl_PeerType, &peer, &str, &cb_extra))
tgl_do_send_document (TLS, PY_PEER_ID(peer), str, NULL, 0, TGL_SEND_MSG_FLAG_DOCUMENT_VIDEO, py_msg_cb, cb_extra);
else
PyErr_Print();
break;
case pq_send_audio:
PyArg_ParseTuple(args, "iis|O", &peer.type, &peer.id, &str, &cb_extra);
tgl_do_send_document (TLS, peer, str, NULL, 0, TGL_SEND_MSG_FLAG_DOCUMENT_AUDIO, py_msg_cb, cb_extra);
if(PyArg_ParseTuple(args, "O!s|O", &tgl_PeerType, &peer, &str, &cb_extra))
tgl_do_send_document (TLS, PY_PEER_ID(peer), str, NULL, 0, TGL_SEND_MSG_FLAG_DOCUMENT_AUDIO, py_msg_cb, cb_extra);
else
PyErr_Print();
break;
case pq_send_document:
PyArg_ParseTuple(args, "iis|O", &peer.type, &peer.id, &str, &cb_extra);
tgl_do_send_document (TLS, peer, str, NULL, 0, 0, py_msg_cb, cb_extra);
if(PyArg_ParseTuple(args, "O!s|O", &tgl_PeerType, &peer, &str, &cb_extra))
tgl_do_send_document (TLS, PY_PEER_ID(peer), str, NULL, 0, 0, py_msg_cb, cb_extra);
else
PyErr_Print();
break;
case pq_send_file:
PyArg_ParseTuple(args, "iis|O", &peer.type, &peer.id, &str, &cb_extra);
tgl_do_send_document (TLS, peer, str, NULL, 0, TGL_SEND_MSG_FLAG_DOCUMENT_AUTO, py_msg_cb, cb_extra);
if(PyArg_ParseTuple(args, "O!s|O", &tgl_PeerType, &peer, &str, &cb_extra))
tgl_do_send_document (TLS, PY_PEER_ID(peer), str, NULL, 0, TGL_SEND_MSG_FLAG_DOCUMENT_AUTO, py_msg_cb, cb_extra);
else
PyErr_Print();
break;
case pq_send_text:
PyArg_ParseTuple(args, "iis|O", &peer.type, &peer.id, &str, &cb_extra);
tgl_do_send_text (TLS, peer, str, 0, py_msg_cb, cb_extra);
if(PyArg_ParseTuple(args, "O!s|O", &tgl_PeerType, &peer, &str, &cb_extra))
tgl_do_send_text (TLS, PY_PEER_ID(peer), str, 0, py_msg_cb, cb_extra);
else
PyErr_Print();
break;
case pq_chat_set_photo:
PyArg_ParseTuple(args, "iis|O", &peer.type, &peer.id, &str, &cb_extra);
tgl_do_set_chat_photo (TLS, peer, str, py_empty_cb, cb_extra);
if(PyArg_ParseTuple(args, "O!s|O", &tgl_PeerType, &peer, &str, &cb_extra))
tgl_do_set_chat_photo (TLS, PY_PEER_ID(peer), str, py_empty_cb, cb_extra);
else
PyErr_Print();
break;
/* case pq_load_photo:
case pq_load_video:
@ -835,83 +867,124 @@ void py_do_all (void) {
break;
*/
case pq_fwd:
PyArg_ParseTuple(args, "iil|O", &peer.type, &peer.id, &msg_id, &cb_extra);
tgl_do_forward_message (TLS, peer, msg_id, 0, py_msg_cb, cb_extra);
if(PyArg_ParseTuple(args, "O!l|O", &tgl_PeerType, &peer, &msg_id, &cb_extra))
tgl_do_forward_message (TLS, PY_PEER_ID(peer), msg_id, 0, py_msg_cb, cb_extra);
else
PyErr_Print();
break;
case pq_fwd_media:
PyArg_ParseTuple(args, "iil|O", &peer.type, &peer.id, &msg_id, &cb_extra);
tgl_do_forward_media (TLS, peer, msg_id, 0, py_msg_cb, cb_extra);
if(PyArg_ParseTuple(args, "O!l|O", &tgl_PeerType, &peer, &msg_id, &cb_extra))
tgl_do_forward_media (TLS, PY_PEER_ID(peer), msg_id, 0, py_msg_cb, cb_extra);
else
PyErr_Print();
break;
case pq_chat_info:
PyArg_ParseTuple(args, "ii|O", &peer.type, &peer.id, &cb_extra);
tgl_do_get_chat_info (TLS, peer, 0, py_chat_cb, cb_extra);
if(PyArg_ParseTuple(args, "O!|O", &tgl_PeerType, &peer, &cb_extra))
tgl_do_get_chat_info (TLS, PY_PEER_ID(peer), 0, py_chat_cb, cb_extra);
else
PyErr_Print();
break;
case pq_user_info:
PyArg_ParseTuple(args, "ii|O", &peer.type, &peer.id, &cb_extra);
tgl_do_get_user_info (TLS, peer, 0, py_user_cb, cb_extra);
if(PyArg_ParseTuple(args, "O!|O", &tgl_PeerType, &peer, &cb_extra))
tgl_do_get_user_info (TLS, PY_PEER_ID(peer), 0, py_user_cb, cb_extra);
else
PyErr_Print();
break;
case pq_history:
PyArg_ParseTuple(args, "iiii|O", &peer.type, &peer.id, &offset, &limit, &cb_extra);
tgl_do_get_history (TLS, peer, offset, limit, 0, py_msg_list_cb, cb_extra);
if(PyArg_ParseTuple(args, "O!ii|O", &tgl_PeerType, &peer, &offset, &limit, &cb_extra))
tgl_do_get_history (TLS, PY_PEER_ID(peer), offset, limit, 0, py_msg_list_cb, cb_extra);
else
PyErr_Print();
break;
case pq_chat_add_user:
PyArg_ParseTuple(args, "iiii|O", &peer.type, &peer.id, &peer1.type, &peer1.id, &cb_extra);
tgl_do_add_user_to_chat (TLS, peer, peer1, 100, py_empty_cb, cb_extra);
if(PyArg_ParseTuple(args, "O!O!|O", &tgl_PeerType, &peer, &tgl_PeerType, &peer1, &cb_extra))
tgl_do_add_user_to_chat (TLS, PY_PEER_ID(peer), PY_PEER_ID(peer1), 100, py_empty_cb, cb_extra);
else
PyErr_Print();
break;
case pq_chat_del_user:
PyArg_ParseTuple(args, "iiii|O", &peer.type, &peer.id, &peer.type, &peer.id, &cb_extra);
tgl_do_del_user_from_chat (TLS, peer, peer1, py_empty_cb, cb_extra);
if(PyArg_ParseTuple(args, "O!O!|O", &tgl_PeerType, &peer, &tgl_PeerType, &peer1, &cb_extra))
tgl_do_del_user_from_chat (TLS, PY_PEER_ID(peer), PY_PEER_ID(peer1), py_empty_cb, cb_extra);
else
PyErr_Print();
break;
case pq_add_contact:
PyArg_ParseTuple(args, "s#s#s#|O", &str1, &len1, &str2, &len2, &str3, &len3, &cb_extra);
tgl_do_add_contact (TLS, str1, len1, str2, len2, str3, len3, 0, py_contact_list_cb, cb_extra);
if(PyArg_ParseTuple(args, "s#s#s#|O", &str1, &len1, &str2, &len2, &str3, &len3, &cb_extra))
tgl_do_add_contact (TLS, str1, len1, str2, len2, str3, len3, 0, py_contact_list_cb, cb_extra);
else
PyErr_Print();
break;
case pq_del_contact:
PyArg_ParseTuple(args, "ii|O", &peer.type, &peer.id, &cb_extra);
tgl_do_del_contact (TLS, peer, py_empty_cb, cb_extra);
if(PyArg_ParseTuple(args, "O!|O", &tgl_PeerType, &peer, &cb_extra))
tgl_do_del_contact (TLS, PY_PEER_ID(peer), py_empty_cb, cb_extra);
else
PyErr_Print();
break;
case pq_rename_contact:
PyArg_ParseTuple(args, "s#s#s#|O", &str1, &len1, &str2, &len2, &str3, &len3, &cb_extra);
tgl_do_add_contact (TLS, str1, len1, str2, len2, str3, len3, 1, py_contact_list_cb, cb_extra);
if(PyArg_ParseTuple(args, "s#s#s#|O", &str1, &len1, &str2, &len2, &str3, &len3, &cb_extra))
tgl_do_add_contact (TLS, str1, len1, str2, len2, str3, len3, 1, py_contact_list_cb, cb_extra);
else
PyErr_Print();
break;
case pq_search:
PyArg_ParseTuple(args, "iis#|O", &peer.type, &peer.id, &str, &len, &cb_extra);
tgl_do_msg_search (TLS, peer, 0, 0, 40, 0, str, len, py_msg_list_cb, cb_extra);
if(PyArg_ParseTuple(args, "O!s#|O", &tgl_PeerType, &peer, &str, &len, &cb_extra))
tgl_do_msg_search (TLS, PY_PEER_ID(peer), 0, 0, 40, 0, str, len, py_msg_list_cb, cb_extra);
else
PyErr_Print();
break;
case pq_global_search:
PyArg_ParseTuple(args, "s#|O", &str, &len, &cb_extra);
tgl_do_msg_search (TLS, tgl_set_peer_id (TGL_PEER_UNKNOWN, 0), 0, 0, 40, 0, str, len, py_msg_list_cb, cb_extra);
if(PyArg_ParseTuple(args, "s#|O", &str, &len, &cb_extra))
tgl_do_msg_search (TLS, tgl_set_peer_id (TGL_PEER_UNKNOWN, 0), 0, 0, 40, 0, str, len, py_msg_list_cb, cb_extra);
else
PyErr_Print();
break;
case pq_mark_read:
PyArg_ParseTuple(args, "ii|O", &peer.type, &peer.id, &cb_extra);
tgl_do_mark_read (TLS, peer, py_empty_cb, cb_extra);
if(PyArg_ParseTuple(args, "O!|O", &tgl_PeerType, &peer, &cb_extra))
tgl_do_mark_read (TLS, PY_PEER_ID(peer), py_empty_cb, cb_extra);
else
PyErr_Print();
break;
case pq_set_profile_photo:
PyArg_ParseTuple(args, "s|O", &str, &cb_extra);
tgl_do_set_profile_photo (TLS, str, py_empty_cb, cb_extra);
if(PyArg_ParseTuple(args, "s|O", &str, &cb_extra))
tgl_do_set_profile_photo (TLS, str, py_empty_cb, cb_extra);
else
PyErr_Print();
break;
case pq_set_profile_name:
PyArg_ParseTuple(args, "s#s#|O", &str1, &len1, &str2, len2, &cb_extra);
tgl_do_set_profile_name (TLS, str1, len1, str2, len2, py_user_cb, cb_extra);
if(PyArg_ParseTuple(args, "s#s#|O", &str1, &len1, &str2, len2, &cb_extra))
tgl_do_set_profile_name (TLS, str1, len1, str2, len2, py_user_cb, cb_extra);
else
PyErr_Print();
break;
case pq_create_secret_chat:
PyArg_ParseTuple(args, "ii|O", &peer.type, &peer.id, &cb_extra);
tgl_do_create_secret_chat (TLS, peer, py_secret_chat_cb, cb_extra);
if(PyArg_ParseTuple(args, "O!|O", &tgl_PeerType, &peer, &cb_extra))
tgl_do_create_secret_chat (TLS, PY_PEER_ID(peer), py_secret_chat_cb, cb_extra);
else
PyErr_Print();
break;
/*
case pq_create_group_chat:
PyArg_ParseTuple(args, "Os|O", &pyObj1, str, &cb_extra);
if(PyList_Check(pyObj1) {
tgl_do_create_group_chat (TLS, peer, str, py_msg_cb, cb_extra);
} else {
logprintf("create_group_chat: Argument 1 must be a list of peers"
if(PyArg_ParseTuple(args, "O!s#|O", &PyList_Type, &pyObj1, &str, &len, &cb_extra)) {
if(PyList_GET_SIZE(pyObj1) > 2) {
ids = (tgl_user_id_t *)malloc(PyList_GET_SIZE(pyObj1) * sizeof(tgl_user_id_t));
for(i = 0; i < PyList_GET_SIZE(pyObj1); i++) {
peer = PyList_GetItem(pyObj1, i);
*(ids+i) = PY_PEER_ID(peer);
}
tgl_do_create_group_chat (TLS, PyList_GET_SIZE(pyObj1), ids, str, len, py_empty_cb, cb_extra);
tfree(ids, PyList_GET_SIZE(pyObj1) * sizeof(tgl_user_id_t));
} else {
logprintf("create_group_chat: Argument 1 must be a list of at least 3 peers");
}
}
Py_XDECREF(pyObj1);
break;
*/
case pq_delete_msg:
case pq_restore_msg:
PyArg_ParseTuple(args, "l|O", msg_id, &cb_extra);
tgl_do_delete_msg (TLS, msg_id, py_empty_cb, cb_extra);
if(PyArg_ParseTuple(args, "l|O", msg_id, &cb_extra))
tgl_do_delete_msg (TLS, msg_id, py_empty_cb, cb_extra);
else
PyErr_Print();
break;
/*
case pq_accept_secret_chat:
@ -919,26 +992,38 @@ void py_do_all (void) {
break;
*/
case pq_send_contact:
PyArg_ParseTuple(args, "iis#s#s#|O", &peer.type, &peer.id, &str1, &len1, &str2, &len2, &str3, &len3, &cb_extra);
tgl_do_send_contact (TLS, peer, str1, len1, str2, len2, str3, len3, 0, py_msg_cb, cb_extra);
if(PyArg_ParseTuple(args, "O!s#s#s#|O", &tgl_PeerType, &peer, &str1, &len1, &str2, &len2,
&str3, &len3, &cb_extra))
tgl_do_send_contact (TLS, PY_PEER_ID(peer), str1, len1, str2, len2, str3, len3, 0, py_msg_cb, cb_extra);
else
PyErr_Print();
break;
case pq_status_online:
PyArg_ParseTuple(args, "|O", &cb_extra);
tgl_do_update_status (TLS, 1, py_empty_cb, cb_extra);
if(PyArg_ParseTuple(args, "|O", &cb_extra))
tgl_do_update_status (TLS, 1, py_empty_cb, cb_extra);
else
PyErr_Print();
break;
case pq_status_offline:
PyArg_ParseTuple(args, "|O", &cb_extra);
tgl_do_update_status (TLS, 0, py_empty_cb, cb_extra);
if(PyArg_ParseTuple(args, "|O", &cb_extra))
tgl_do_update_status (TLS, 0, py_empty_cb, cb_extra);
else
PyErr_Print();
break;
case pq_extf:
PyArg_ParseTuple(args, "s#|O", &str, &len, &cb_extra);
tgl_do_send_extf (TLS, str, len, py_str_cb, &cb_extra);
if(PyArg_ParseTuple(args, "s#|O", &str, &len, &cb_extra))
tgl_do_send_extf (TLS, str, len, py_str_cb, &cb_extra);
else
PyErr_Print();
break;
case pq_send_location:
PyArg_ParseTuple(args, "iiOO|O", &peer.type, &peer.id, &pyObj1, &pyObj2, &cb_extra);
tgl_do_send_location (TLS, peer, PyFloat_AsDouble(pyObj1), PyFloat_AsDouble(pyObj2), 0, py_msg_cb, cb_extra);
Py_XDECREF(pyObj1);
Py_XDECREF(pyObj2);
if(PyArg_ParseTuple(args, "O!O!O!|O", &tgl_PeerType, &peer, &PyFloat_Type, &pyObj1, &PyFloat_Type, &pyObj2, &cb_extra)){
tgl_do_send_location (TLS, PY_PEER_ID(peer),
PyFloat_AsDouble(pyObj1), PyFloat_AsDouble(pyObj2), 0, py_msg_cb, cb_extra);
Py_XDECREF(pyObj1);
Py_XDECREF(pyObj2);
} else
PyErr_Print();
break;
default:
assert (0);
@ -946,9 +1031,11 @@ void py_do_all (void) {
// Increment reference on cb_extra as it is passed on to the callback to use
Py_XINCREF(cb_extra);
// Clean up any arg variables we could have used.
//Py_XDECREF(args); // TODO: this is going negative ref and causing segfaults
Py_XDECREF(peer);
Py_XDECREF(peer1);
}
pos = 0;

View File

@ -24,6 +24,7 @@
#include <Python.h>
#include <tgl/tgl.h>
// Python functions
void py_init (const char *file);
void py_new_msg (struct tgl_message *M);
void py_our_id (int id);
@ -34,7 +35,54 @@ void py_binlog_end (void);
void py_diff_end (void);
void py_do_all (void);
// Binding functions
PyObject* py_contact_list(PyObject *self, PyObject *args);
PyObject* py_dialog_list(PyObject *self, PyObject *args);
PyObject* py_rename_chat(PyObject *self, PyObject *args);
PyObject* py_send_msg(PyObject *self, PyObject *args);
PyObject* py_send_typing(PyObject *self, PyObject *args);
PyObject* py_send_typing_abort(PyObject *self, PyObject *args);
PyObject* py_send_photo(PyObject *self, PyObject *args);
PyObject* py_send_video(PyObject *self, PyObject *args);
PyObject* py_send_audio(PyObject *self, PyObject *args);
PyObject* py_send_document(PyObject *self, PyObject *args);
PyObject* py_send_file(PyObject *self, PyObject *args);
PyObject* py_send_text(PyObject *self, PyObject *args);
PyObject* py_chat_set_photo(PyObject *self, PyObject *args);
PyObject* py_load_photo(PyObject *self, PyObject *args);
PyObject* py_load_video(PyObject *self, PyObject *args);
PyObject* py_load_video_thumb(PyObject *self, PyObject *args);
PyObject* py_load_audio(PyObject *self, PyObject *args);
PyObject* py_load_document(PyObject *self, PyObject *args);
PyObject* py_load_document_thumb(PyObject *self, PyObject *args);
PyObject* py_fwd(PyObject *self, PyObject *args);
PyObject* py_fwd_media(PyObject *self, PyObject *args);
PyObject* py_chat_info(PyObject *self, PyObject *args);
PyObject* py_user_info(PyObject *self, PyObject *args);
PyObject* py_history(PyObject *self, PyObject *args);
PyObject* py_chat_add_user(PyObject *self, PyObject *args);
PyObject* py_chat_del_user(PyObject *self, PyObject *args);
PyObject* py_add_contact(PyObject *self, PyObject *args);
PyObject* py_del_contact(PyObject *self, PyObject *args);
PyObject* py_rename_contact(PyObject *self, PyObject *args);
PyObject* py_search(PyObject *self, PyObject *args);
PyObject* py_global_search(PyObject *self, PyObject *args);
PyObject* py_mark_read(PyObject *self, PyObject *args);
PyObject* py_set_profile_photo(PyObject *self, PyObject *args);
PyObject* py_set_profile_name(PyObject *self, PyObject *args);
PyObject* py_create_secret_chat(PyObject *self, PyObject *args);
PyObject* py_create_group_chat(PyObject *self, PyObject *args);
PyObject* py_delete_msg(PyObject *self, PyObject *args);
PyObject* py_restore_msg(PyObject *self, PyObject *args);
PyObject* py_accept_secret_chat(PyObject *self, PyObject *args);
PyObject* py_send_contact(PyObject *self, PyObject *args);
PyObject* py_status_online(PyObject *self, PyObject *args);
PyObject* py_status_offline(PyObject *self, PyObject *args);
PyObject* py_send_location(PyObject *self, PyObject *args);
PyObject* py_extf(PyObject *self, PyObject *args);
// Util Functions
void py_add_string_field (PyObject* dict, char *name, const char *value);
void py_add_string_field_arr (PyObject* list, int num, const char *value);
void py_add_num_field (PyObject* dict, const char *name, double value);

View File

@ -38,11 +38,13 @@ tgl_Peer_init(tgl_Peer *self, PyObject *args, PyObject *kwds)
{
static char *kwlist[] = {"type", "id", NULL};
tgl_peer_id_t peer_id;
if(!PyArg_ParseTupleAndKeywords(args, kwds, "|ii", kwlist,
if(!PyArg_ParseTupleAndKeywords(args, kwds, "ii", kwlist,
&peer_id.type,
&peer_id.id))
{
PyErr_Format(PyErr_NewException("tgl.PeerInvalid", NULL, NULL), "Peer must specify type and id");
return -1;
}
self->peer = tgl_peer_get(TLS, peer_id);
if(self->peer == NULL)
return -1;
@ -66,7 +68,7 @@ tgl_Peer_getname (tgl_Peer *self, void *closure)
ret = PyUnicode_FromString(self->peer->encr_chat.print_name);
break;
default:
PyErr_SetString(PyExc_TypeError, "peer.type not supported!");
PyErr_SetString(PyExc_TypeError, "peer.type_name not supported!");
Py_RETURN_NONE;
}
@ -85,7 +87,7 @@ tgl_Peer_getuser_id (tgl_Peer *self, void *closure)
ret = PyLong_FromLong(self->peer->id.id);
break;
case TGL_PEER_CHAT:
PyErr_SetString(PyExc_TypeError, "peer.type == TGL_PEER_CHAT has no user_id");
PyErr_SetString(PyExc_TypeError, "peer.type_name == 'chat' has no user_id");
Py_RETURN_NONE;
break;
@ -93,7 +95,7 @@ tgl_Peer_getuser_id (tgl_Peer *self, void *closure)
ret = PyLong_FromLong(self->peer->encr_chat.user_id);
break;
default:
PyErr_SetString(PyExc_TypeError, "peer.type not supported!");
PyErr_SetString(PyExc_TypeError, "peer.type_name not supported!");
Py_RETURN_NONE;
}
@ -120,11 +122,11 @@ tgl_Peer_getuser_list (tgl_Peer *self, void *closure)
break;
case TGL_PEER_ENCR_CHAT:
case TGL_PEER_USER:
PyErr_SetString(PyExc_TypeError, "Only peer.type == TGL_PEER_CHAT has user_list");
PyErr_SetString(PyExc_TypeError, "Only peer.type_name == 'chat' has user_list");
Py_RETURN_NONE;
break;
default:
PyErr_SetString(PyExc_TypeError, "peer.type not supported!");
PyErr_SetString(PyExc_TypeError, "peer.type_name not supported!");
Py_RETURN_NONE;
}
@ -147,11 +149,11 @@ tgl_Peer_getuser_status(tgl_Peer *self, void *closure)
break;
case TGL_PEER_CHAT:
case TGL_PEER_ENCR_CHAT:
PyErr_SetString(PyExc_TypeError, "Only peer.type == TGL_PEER_USER has user_status");
PyErr_SetString(PyExc_TypeError, "Only peer.type_name == 'user' has user_status");
Py_RETURN_NONE;
break;
default:
PyErr_SetString(PyExc_TypeError, "peer.type not supported!");
PyErr_SetString(PyExc_TypeError, "peer.type_name not supported!");
Py_RETURN_NONE;
}
@ -170,11 +172,11 @@ tgl_Peer_getphone (tgl_Peer *self, void *closure)
break;
case TGL_PEER_CHAT:
case TGL_PEER_ENCR_CHAT:
PyErr_SetString(PyExc_TypeError, "Only peer.type == TGL_PEER_USER has phone");
PyErr_SetString(PyExc_TypeError, "Only peer.type_name == 'user' has phone");
Py_RETURN_NONE;
break;
default:
PyErr_SetString(PyExc_TypeError, "peer.type not supported!");
PyErr_SetString(PyExc_TypeError, "peer.type_name not supported!");
Py_RETURN_NONE;
}
@ -193,11 +195,11 @@ tgl_Peer_getusername (tgl_Peer *self, void *closure)
break;
case TGL_PEER_CHAT:
case TGL_PEER_ENCR_CHAT:
PyErr_SetString(PyExc_TypeError, "Only peer.type == TGL_PEER_USER has username");
PyErr_SetString(PyExc_TypeError, "Only peer.type_name == 'user' has username");
Py_RETURN_NONE;
break;
default:
PyErr_SetString(PyExc_TypeError, "peer.type not supported!");
PyErr_SetString(PyExc_TypeError, "peer.type_name not supported!");
Py_RETURN_NONE;
}
@ -205,6 +207,51 @@ tgl_Peer_getusername (tgl_Peer *self, void *closure)
return ret;
}
static PyObject *
tgl_Peer_getfirst_name (tgl_Peer *self, void *closure)
{
PyObject *ret;
switch(self->peer->id.type) {
case TGL_PEER_USER:
ret = PyUnicode_FromString(self->peer->user.first_name);
break;
case TGL_PEER_CHAT:
case TGL_PEER_ENCR_CHAT:
PyErr_SetString(PyExc_TypeError, "Only peer.type_name == 'user' has first_name");
Py_RETURN_NONE;
break;
default:
PyErr_SetString(PyExc_TypeError, "peer.type_name not supported!");
Py_RETURN_NONE;
}
Py_XINCREF(ret);
return ret;
}
static PyObject *
tgl_Peer_getlast_name (tgl_Peer *self, void *closure)
{
PyObject *ret;
switch(self->peer->id.type) {
case TGL_PEER_USER:
ret = PyUnicode_FromString(self->peer->user.username);
break;
case TGL_PEER_CHAT:
case TGL_PEER_ENCR_CHAT:
PyErr_SetString(PyExc_TypeError, "Only peer.type_name == 'user' has last_name");
Py_RETURN_NONE;
break;
default:
PyErr_SetString(PyExc_TypeError, "peer.type_name not supported!");
Py_RETURN_NONE;
}
Py_XINCREF(ret);
return ret;
}
static PyObject *
tgl_Peer_getuser (tgl_Peer *self, void *closure)
@ -216,12 +263,14 @@ tgl_Peer_getuser (tgl_Peer *self, void *closure)
ret = tgl_Peer_FromTglPeer(tgl_peer_get(TLS, TGL_MK_USER (self->peer->encr_chat.user_id)));
break;
case TGL_PEER_USER:
ret = (PyObject*)self;
break;
case TGL_PEER_CHAT:
PyErr_SetString(PyExc_TypeError, "Only peer.type == TGL_PEER_ENCR_CHAT has user");
PyErr_SetString(PyExc_TypeError, "Only peer.type_name == 'chat' does not have user");
Py_RETURN_NONE;
break;
default:
PyErr_SetString(PyExc_TypeError, "peer.type not supported!");
PyErr_SetString(PyExc_TypeError, "peer.type_name not supported!");
Py_RETURN_NONE;
}
@ -229,6 +278,27 @@ tgl_Peer_getuser (tgl_Peer *self, void *closure)
return ret;
}
static PyObject *
tgl_Peer_gettype_name(tgl_Peer* self)
{
PyObject *name;
switch(self->peer->id.type) {
case TGL_PEER_USER:
name = PyUnicode_FromString("user");
break;
case TGL_PEER_CHAT:
name = PyUnicode_FromString("chat");
break;
case TGL_PEER_ENCR_CHAT:
name = PyUnicode_FromString("secret_chat");
break;
default:
name = PyUnicode_FromString("unknown");
}
return name;
}
static PyObject *
tgl_Peer_getid (tgl_Peer *self, void *closure)
{
@ -254,6 +324,7 @@ tgl_Peer_gettype (tgl_Peer *self, void *closure)
static PyGetSetDef tgl_Peer_getseters[] = {
{"id", (getter)tgl_Peer_getid, NULL, "", NULL},
{"type", (getter)tgl_Peer_gettype, NULL, "", NULL},
{"type_name", (getter)tgl_Peer_gettype_name, NULL, "", NULL},
{"name", (getter)tgl_Peer_getname, NULL, "", NULL},
{"user_id", (getter)tgl_Peer_getuser_id, NULL, "", NULL},
{"user", (getter)tgl_Peer_getuser, NULL, "", NULL},
@ -261,6 +332,8 @@ static PyGetSetDef tgl_Peer_getseters[] = {
{"user_status", (getter)tgl_Peer_getuser_status, NULL, "", NULL},
{"phone", (getter)tgl_Peer_getphone, NULL, "", NULL},
{"username", (getter)tgl_Peer_getusername, NULL, "", NULL},
{"first_name", (getter)tgl_Peer_getfirst_name, NULL, "", NULL},
{"last_name", (getter)tgl_Peer_getlast_name, NULL, "", NULL},
{NULL} /* Sentinel */
};
@ -269,30 +342,36 @@ static PyMemberDef tgl_Peer_members[] = {
};
static PyObject *
tgl_Peer_type_name(tgl_Peer* self)
tgl_Peer_send_msg (tgl_Peer *self, PyObject *args, PyObject *kwargs)
{
PyObject *name;
static char *kwlist[] = {"message", "callback", NULL};
char *message;
PyObject *callback = NULL;
if(PyArg_ParseTupleAndKeywords(args, kwargs, "s|O", kwlist, &message, &callback)) {
PyObject *api_call;
if(callback)
api_call = Py_BuildValue("OsO", (PyObject*) self, message, callback);
else
api_call = Py_BuildValue("Os", (PyObject*) self, message);
Py_XINCREF(Py_None);
Py_XINCREF(api_call);
return py_send_msg(Py_None, api_call);
} else {
Py_XINCREF(Py_False);
return Py_False;
}
switch(self->peer->id.type) {
case TGL_PEER_USER:
name = PyUnicode_FromString("user");
break;
case TGL_PEER_CHAT:
name = PyUnicode_FromString("chat");
break;
case TGL_PEER_ENCR_CHAT:
name = PyUnicode_FromString("encr_chat");
break;
default:
name = PyUnicode_FromString("unknown");
}
return name;
}
static PyMethodDef tgl_Peer_methods[] = {
{"type_name", (PyCFunction)tgl_Peer_type_name, METH_NOARGS,
"Return the string representation of the peer type."
},
{"send_msg", (PyCFunction)tgl_Peer_send_msg, METH_VARARGS | METH_KEYWORDS,
"Send a message to peer object"},
{NULL} /* Sentinel */
};

View File

@ -31,11 +31,13 @@ def history_cb(msg_list, ptype, pid, success, msgs):
if len(msgs) == HISTORY_QUERY_SIZE:
tgl.get_history(ptype, pid, len(msg_list), HISTORY_QUERY_SIZE, partial(history_cb, msg_list, ptype, pid));
def on_msg_receive(msg):
if msg.out and not binlog_done:
return;
print("From: {0}, To: {1}".format(msg.src.id, msg.dest.user))
tgl.send_msg(tgl.Peer(97704886), "Test")
print("Peers {0}".format(msg.src.id))
"""
def on_msg_receive(msg):