Exemplo n.º 1
0
void tgp_msg_recv (struct tgl_state *TLS, struct tgl_message *M) {
  connection_data *conn = TLS->ev_base;
  struct tgp_msg_loading *C = tgp_msg_loading_init (TRUE, M);

  if (M->date != 0 && M->date < tgp_msg_oldest_relevant_ts (TLS)) {
    debug ("Message from %d on %d too old, ignored.", tgl_get_peer_id (M->from_id), M->date);
    return;
  }
  if (M->media.type == tgl_message_media_photo) {
    C->done = FALSE;
    tgl_do_load_photo (TLS, M->media.photo, tgp_msg_on_loaded_document, C);
  }
  if (M->media.type == tgl_message_media_document_encr &&
      M->media.encr_document->flags & TGLDF_IMAGE &&
    !(M->media.encr_document->flags & TGLDF_STICKER)) {
    C->done = FALSE;
    tgl_do_load_encr_document (TLS, M->media.encr_document, tgp_msg_on_loaded_document, C);
  }
  #ifdef HAVE_LIBWEBP
  if (M->media.type == tgl_message_media_document && M->media.document->flags & TGLDF_STICKER) {
    C->done = FALSE;
    tgl_do_load_document (TLS, M->media.document, tgp_msg_on_loaded_document, C);
  }
  #endif

  if (M->media.type == tgl_message_media_geo) {
    // TODO: load geo thumbnail
  }
  g_queue_push_tail (conn->new_messages, C);
  tgp_msg_process_in_ready (TLS);
}
Exemplo n.º 2
0
void
tw_do_load_photo (struct tgl_state* TLS, struct tgl_photo* photo, tw_callback_load_photo callback, long long peer_id, void* callback_extra)
{
	callback_data* data = (callback_data*) malloc (sizeof (callback_data));
	assert (data);
	data->callback = callback;
	data->data = callback_extra;
	data->extra = peer_id;
	tgl_do_load_photo (TLS, photo, tw_load_photo_cb, data);
}
Exemplo n.º 3
0
void on_user_get_info (struct tgl_state *TLS, void *show_info, int success, struct tgl_user *U)
{
  assert (success);
  
  if (U->photo.sizes_num == 0) {
    if (show_info) {
      PurpleNotifyUserInfo *info = create_user_notify_info(U);
      p2tgl_notify_userinfo(TLS, U->id, info, NULL, NULL);
    }
  } else {
    struct download_desc *dld = malloc (sizeof(struct download_desc));
    dld->data = U;
    dld->type = show_info ? 1 : 2;
    tgl_do_load_photo (TLS, &U->photo, on_userpic_loaded, dld);
  }
}
Exemplo n.º 4
0
void tgp_msg_recv (struct tgl_state *TLS, struct tgl_message *M)
{
  connection_data *conn = TLS->ev_base;
  struct tgp_msg_loading *C = tgp_msg_loading_init (TRUE, M);
  
  if (M->date != 0 && M->date < tgp_msg_oldest_relevant_ts (TLS)) {
    debug ("Message from %d on %d too old, ignored.", tgl_get_peer_id (M->from_id), M->date);
    return;
  }
  
  if (M->media.type == tgl_message_media_photo) {
    C->done = FALSE;
    tgl_do_load_photo (TLS, &M->media.photo, tgp_msg_on_loaded_photo, C);
  }
  
  if (M->media.type == tgl_message_media_geo) {
    // TODO: load geo thumbnail
  }
  
  g_queue_push_tail (conn->new_messages, C);
  tgp_msg_process_ready (TLS);
}
Exemplo n.º 5
0
static void update_message_received (struct tgl_state *TLS, struct tgl_message *M) {
  debug ("received message\n");
  telegram_conn *conn = TLS->ev_base;
  conn->updated = 1;

  if (M->service) {
    debug ("service message, skipping...\n");
    char *text = format_service_msg (TLS, M);
    if (text) {
      switch (tgl_get_peer_type (M->to_id)) {
        case TGL_PEER_CHAT:
          chat_add_message (TLS, M, text);
          break;
          
        case TGL_PEER_USER:
          p2tgl_got_im (TLS, M->from_id, text, PURPLE_MESSAGE_SYSTEM, M->date);
          break;
      }
      g_free (text);
    }
    conn->updated = 1;
    return;
  }
  
  if ((M->flags & (FLAG_MESSAGE_EMPTY | FLAG_DELETED)) || !(M->flags & FLAG_CREATED)) {
    return;
  }
  if (!tgl_get_peer_type (M->to_id)) {
    warning ("Bad msg\n");
    return;
  }

  if (M->media.type == tgl_message_media_photo) {
    tgl_do_load_photo (TLS, &M->media.photo, on_message_load_photo, M);
    return;
  }

  if (!M->message) {
    return;
  }

  char *text = purple_markup_escape_text (M->message, strlen (M->message));
  switch (tgl_get_peer_type (M->to_id)) {
    case TGL_PEER_CHAT:
      debug ("PEER_CHAT\n");
      if (!our_msg(TLS, M)) {
        chat_add_message (TLS, M, text);
      }
      break;
      
    case TGL_PEER_USER:
      debug ("PEER_USER\n");
      
      // p2tgl_got_im (TLS, M->to_id, text, PURPLE_MESSAGE_SEND, M->date);
      // :TODO: figure out how to add messages from different devices to history
      if (!our_msg(TLS, M)) {
        if (out_msg(TLS, M)) {
          p2tgl_got_im (TLS, M->to_id, text, PURPLE_MESSAGE_SEND, M->date);
        } else {
          p2tgl_got_im (TLS, M->from_id, text, PURPLE_MESSAGE_RECV, M->date);
        }
      }
      break;
      
    case TGL_PEER_ENCR_CHAT:
      break;
      
    case TGL_PEER_GEO_CHAT:
      break;
  }
  
  g_free (text);
}
Exemplo n.º 6
0
void tgp_msg_recv (struct tgl_state *TLS, struct tgl_message *M) {
  connection_data *conn = TLS->ev_base;
  if (M->flags & (TGLMF_EMPTY | TGLMF_DELETED)) {
    return;
  }
  if (!(M->flags & TGLMF_CREATED)) {
    return;
  }
  if (!(M->flags | TGLMF_UNREAD) && M->date != 0 && M->date < tgp_msg_oldest_relevant_ts (TLS)) {
    debug ("Message from %d on %d too old, ignored.", tgl_get_peer_id (M->from_id), M->date);
    return;
  }
  
  struct tgp_msg_loading *C = tgp_msg_loading_init (M);
  
  if (! (M->flags & TGLMF_SERVICE)) {
    
    // handle all messages that need to load content before they can be displayed
    if (M->media.type != tgl_message_media_none) {
      switch (M->media.type) {
        case tgl_message_media_photo: {
          
          // include the "bad photo" check from telegram-cli interface.c:3287 to avoid crashes when fetching history
          // TODO: find out the reason for this behavior
          if (M->media.photo) {
            ++ C->pending;
            tgl_do_load_photo (TLS, M->media.photo, tgp_msg_on_loaded_document, C);
          }
          break;
        }
          
        // documents that are stickers or images will be displayed just like regular photo messages
        // and need to be lodaed beforehand
        case tgl_message_media_document:
        case tgl_message_media_video:
        case tgl_message_media_audio:
          if (M->media.document->flags & TGLDF_STICKER || M->media.document->flags & TGLDF_IMAGE) {
            ++ C->pending;
            tgl_do_load_document (TLS, M->media.document, tgp_msg_on_loaded_document, C);
          }
          break;
        case tgl_message_media_document_encr:
          if (M->media.encr_document->flags & TGLDF_STICKER || M->media.encr_document->flags & TGLDF_IMAGE) {
            ++ C->pending;
            tgl_do_load_encr_document (TLS, M->media.encr_document, tgp_msg_on_loaded_document, C);
          }
          break;
          
        case tgl_message_media_geo:
          // TODO: load geo thumbnail
          break;
          
        default: // prevent Clang warnings ...
          break;
      }
    }
  }
  
  if (tgl_get_peer_type (M->to_id) == TGL_PEER_CHAT) {
    
    tgl_peer_t *P = tgl_peer_get (TLS, M->to_id);
    g_warn_if_fail(P);
    if (P && ! P->chat.user_list_size) {
      // To display a chat the full name of every single user is needed, but the updates received from the server only
      // contain the names of users mentioned in the events. In order to display a messages we always need to fetch the
      // full chat info first. If the user list is empty, this means that we still haven't fetched the full chat information.
      
      // assure that there is only one chat info request for every
      // chat to avoid causing FLOOD_WAIT_X errors that will lead to delays or dropped messages
      gpointer to_ptr = GINT_TO_POINTER(tgl_get_peer_id (M->to_id));
      
      if (! g_hash_table_lookup (conn->pending_chat_info, to_ptr)) {
        ++ C->pending;
        
        tgl_do_get_chat_info (TLS, M->to_id, FALSE, tgp_msg_on_loaded_chat_full, C);
        g_hash_table_replace (conn->pending_chat_info, to_ptr, to_ptr);
      }
    }
  }
  
  g_queue_push_tail (conn->new_messages, C);
  tgp_msg_process_in_ready (TLS);
}