From f37588b1b9db7891ce32dbfc232c20fa15316c57 Mon Sep 17 00:00:00 2001 From: Vincent Castellano Date: Tue, 2 Jun 2015 01:02:18 -0700 Subject: [PATCH 1/4] Add constants for peer types and action types. --- python-tg.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/python-tg.c b/python-tg.c index e1835fe..3d07cee 100644 --- a/python-tg.c +++ b/python-tg.c @@ -1234,6 +1234,41 @@ static PyMethodDef py_tgl_methods[] = { { NULL, NULL, 0, NULL } }; +void py_add_action_enums(PyObject *m) +{ + PyModule_AddIntConstant(m, "ACTION_NONE", tgl_message_action_none); + PyModule_AddIntConstant(m, "ACTION_GEO_CHAT_CREATE", tgl_message_action_geo_chat_create); + PyModule_AddIntConstant(m, "ACTION_GEO_CHAT_CHECKIN", tgl_message_action_geo_chat_checkin); + PyModule_AddIntConstant(m, "ACTION_CHAT_CREATE", tgl_message_action_chat_create); + PyModule_AddIntConstant(m, "ACTION_CHAT_EDIT_TITLE", tgl_message_action_chat_edit_title); + PyModule_AddIntConstant(m, "ACTION_CHAT_EDIT_PHOTO", tgl_message_action_chat_edit_photo); + PyModule_AddIntConstant(m, "ACTION_CHAT_DELETE_PHOTO", tgl_message_action_chat_delete_photo); + PyModule_AddIntConstant(m, "ACTION_CHAT_ADD_USER", tgl_message_action_chat_add_user); + PyModule_AddIntConstant(m, "ACTION_CHAT_ADD_USER_BY_LINK", tgl_message_action_chat_add_user_by_link); + PyModule_AddIntConstant(m, "ACTION_CHAT_DELETE_USER", tgl_message_action_chat_delete_user); + PyModule_AddIntConstant(m, "ACTION_SET_MESSAGE_TTL", tgl_message_action_set_message_ttl); + PyModule_AddIntConstant(m, "ACTION_READ_MESSAGES", tgl_message_action_read_messages); + PyModule_AddIntConstant(m, "ACTION_DELETE_MESSAGES", tgl_message_action_delete_messages); + PyModule_AddIntConstant(m, "ACTION_SCREENSHOT_MESSAGES", tgl_message_action_screenshot_messages); + PyModule_AddIntConstant(m, "ACTION_FLUSH_HISTORY", tgl_message_action_flush_history); + PyModule_AddIntConstant(m, "ACTION_RESEND", tgl_message_action_resend); + PyModule_AddIntConstant(m, "ACTION_NOTIFY_LAYER", tgl_message_action_notify_layer); + PyModule_AddIntConstant(m, "ACTION_TYPING", tgl_message_action_typing); + PyModule_AddIntConstant(m, "ACTION_NOOP", tgl_message_action_noop); + PyModule_AddIntConstant(m, "ACTION_COMMIT_KEY", tgl_message_action_commit_key); + PyModule_AddIntConstant(m, "ACTION_ABORT_KEY", tgl_message_action_abort_key); + PyModule_AddIntConstant(m, "ACTION_REQUEST_KEY", tgl_message_action_request_key); + PyModule_AddIntConstant(m, "ACTION_ACCEPT_KEY", tgl_message_action_accept_key); +} + +void py_add_peer_type_enums(PyObject *m) +{ + PyModule_AddIntConstant(m, "PEER_USER", TGL_PEER_USER); + PyModule_AddIntConstant(m, "PEER_USER", TGL_PEER_CHAT); + PyModule_AddIntConstant(m, "PEER_ENCR_CHAT", TGL_PEER_ENCR_CHAT); +} + + MOD_INIT(tgl) { PyObject *m; @@ -1243,6 +1278,9 @@ MOD_INIT(tgl) if (m == NULL) return MOD_ERROR_VAL; + py_add_action_enums(m); + py_add_peer_type_enums(m); + if (PyType_Ready(&tgl_PeerType) < 0) return MOD_ERROR_VAL; From a515e0c9b49f315167d9d5c36996825af25ae71d Mon Sep 17 00:00:00 2001 From: Vincent Castellano Date: Tue, 2 Jun 2015 01:20:25 -0700 Subject: [PATCH 2/4] Implement load_ functions --- python-tg.c | 44 ++++++++++++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 16 deletions(-) diff --git a/python-tg.c b/python-tg.c index 3d07cee..63bd2b6 100644 --- a/python-tg.c +++ b/python-tg.c @@ -774,16 +774,21 @@ void py_do_all (void) { int preview = 0; int reply_id = 0; unsigned long long flags = 0; + Py_ssize_t i; tgl_user_id_t *ids; + struct tgl_message *M; + int len, len1, len2, len3; int limit, offset; long msg_id = 0; + PyObject *pyObj1 = NULL; PyObject *pyObj2 = NULL; PyObject *cb_extra = NULL; + PyObject *msg = NULL; PyObject *peer = NULL; PyObject *peer1 = NULL; @@ -877,33 +882,40 @@ void py_do_all (void) { else PyErr_Print(); break; -/* case pq_load_photo: + case pq_load_photo: case pq_load_video: case pq_load_audio: case pq_load_document: - M = py_ptr[p + 1]; - if (!M || (M->media.type != tgl_message_media_photo && M->media.type != tgl_message_media_photo_encr && M->media.type != tgl_message_media_document && M->media.type != tgl_message_media_document_encr)) { - py_file_cb (TLS, py_ptr[p], 0, 0); - } else { - , limit, offse, limit, offsettif (M->media.type == tgl_message_media_photo) { - tgl_do_load_photo (TLS, &M->media.photo, py_file_cb, py_ptr[p]); - } else if (M->media.type == tgl_message_media_document) { - tgl_do_load_document (TLS, &M->media.document, py_file_cb, py_ptr[p]); + if(PyArg_ParseTuple(args, "O!O", &tgl_MsgType, &msg, &cb_extra)) + { + M = ((tgl_Msg*)msg)->msg; + if (!M || (M->media.type != tgl_message_media_photo && M->media.type != tgl_message_media_document && M->media.type != tgl_message_media_document_encr)) { + py_file_cb (TLS, cb_extra, 0, 0); } else { - tgl_do_load_encr_document (TLS, &M->media.encr_document, py_file_cb, py_ptr[p]); + if (M->media.type == tgl_message_media_photo) { + assert (M->media.photo); + tgl_do_load_photo (TLS, M->media.photo, py_file_cb, cb_extra); + } else if (M->media.type == tgl_message_media_document) { + tgl_do_load_document (TLS, M->media.document, py_file_cb, cb_extra); + } else { + tgl_do_load_encr_document (TLS, M->media.encr_document, py_file_cb, cb_extra); + } } } break; case pq_load_video_thumb: case pq_load_document_thumb: - M = py_ptr[p + 1]; - if (!M || (M->media.type != tgl_message_media_document)) { - py_file_cb (TLS, py_ptr[p], 0, 0); - } else { - tgl_do_load_document_thumb (TLS, &M->media.document, py_file_cb, py_ptr[p]); + if(PyArg_ParseTuple(args, "O!O", &tgl_MsgType, &msg, &cb_extra)) + { + M = ((tgl_Msg*)msg)->msg; + if (!M || (M->media.type != tgl_message_media_document)) { + py_file_cb (TLS, cb_extra, 0, 0); + } else { + tgl_do_load_document_thumb (TLS, M->media.document, py_file_cb, cb_extra); + } } break; -*/ + case pq_fwd: 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); From 49951819c59d02e7e7062f814a59ac554c222792 Mon Sep 17 00:00:00 2001 From: Vincent Castellano Date: Tue, 2 Jun 2015 01:24:17 -0700 Subject: [PATCH 3/4] Add msg action type enum --- python-types.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/python-types.c b/python-types.c index 3dbdf4b..f2fdaac 100644 --- a/python-types.c +++ b/python-types.c @@ -1151,6 +1151,17 @@ tgl_Msg_getservice (tgl_Msg *self, void *closure) return ret; } +static PyObject * +tgl_Msg_getaction (tgl_Msg *self, void *closure) +{ + PyObject *ret; + + ret = PyLong_FromLong(self->msg->action.type); + + Py_XINCREF(ret); + return ret; +} + static PyObject * tgl_Msg_getsrc (tgl_Msg *self, void *closure) @@ -1400,6 +1411,7 @@ static PyGetSetDef tgl_Msg_getseters[] = { {"fwd_date", (getter)tgl_Msg_getfwd_date, NULL, "", NULL}, {"reply", (getter)tgl_Msg_getreply, NULL, "", NULL}, {"reply_id", (getter)tgl_Msg_getreply_id, NULL, "", NULL}, + {"action", (getter)tgl_Msg_getaction, NULL, "", NULL}, {NULL} /* Sentinel */ }; From ff704275c3efab2fb0c5a7a491688ddea8dff005 Mon Sep 17 00:00:00 2001 From: Vincent Castellano Date: Tue, 2 Jun 2015 01:30:21 -0700 Subject: [PATCH 4/4] Implement msg.load_* --- python-types.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/python-types.c b/python-types.c index f2fdaac..6d4413c 100644 --- a/python-types.c +++ b/python-types.c @@ -1221,6 +1221,8 @@ tgl_Msg_gettext (tgl_Msg *self, void *closure) return ret; } + + static PyObject * tgl_Msg_getmedia (tgl_Msg *self, void *closure) { @@ -1368,6 +1370,55 @@ tgl_Msg_getreply_id (tgl_Msg *self, void *closure) return ret; } +// All load methods are implemented the same, just alias load_document +static PyObject * +tgl_Msg_load_document (tgl_Msg *self, PyObject *args, PyObject *kwargs) +{ + static char *kwlist[] = {"callback", NULL}; + + PyObject *callback = NULL; + + if(PyArg_ParseTupleAndKeywords(args, kwargs, "O", kwlist, &callback)) { + PyObject *api_call; + + api_call = Py_BuildValue("OO", (PyObject*) self, callback); + + Py_INCREF(Py_None); + Py_XINCREF(api_call); + + return py_load_document(Py_None, api_call); + } else { + PyErr_Print(); + Py_XINCREF(Py_False); + return Py_False; + } + +} + +static PyObject * +tgl_Msg_load_document_thumb (tgl_Msg *self, PyObject *args, PyObject *kwargs) +{ + static char *kwlist[] = {"callback", NULL}; + + PyObject *callback = NULL; + + if(PyArg_ParseTupleAndKeywords(args, kwargs, "O", kwlist, &callback)) { + PyObject *api_call; + + api_call = Py_BuildValue("OO", (PyObject*) self, callback); + + Py_INCREF(Py_None); + Py_XINCREF(api_call); + + return py_load_document_thumb(Py_None, api_call); + } else { + PyErr_Print(); + Py_XINCREF(Py_False); + return Py_False; + } + +} + static PyObject * tgl_Msg_repr(tgl_Msg *self) { @@ -1422,6 +1473,12 @@ static PyMemberDef tgl_Msg_members[] = { static PyMethodDef tgl_Msg_methods[] = { + {"load_document", (PyCFunction)tgl_Msg_load_document, METH_VARARGS | METH_KEYWORDS, ""}, + {"load_photo", (PyCFunction)tgl_Msg_load_document, METH_VARARGS | METH_KEYWORDS, ""}, + {"load_audio", (PyCFunction)tgl_Msg_load_document, METH_VARARGS | METH_KEYWORDS, ""}, + {"load_video", (PyCFunction)tgl_Msg_load_document, METH_VARARGS | METH_KEYWORDS, ""}, + {"load_document_thumb", (PyCFunction)tgl_Msg_load_document_thumb, METH_VARARGS | METH_KEYWORDS, ""}, + {"load_video_thumb", (PyCFunction)tgl_Msg_load_document_thumb, METH_VARARGS | METH_KEYWORDS, ""}, {NULL} /* Sentinel */ };