pub struct E2eClient<T: E2eTransport> { /* private fields */ }Expand description
High-level secret chat client.
Generic over the E2eTransport that sends RPCs; with ferogram enabled,
use ferogram::e2e::E2eClient, which fixes T to ferogram::Client.
Cheap to clone: the chat store, DH cache and event channel are shared behind Arc.
Implementations§
Source§impl<T: E2eTransport> E2eClient<T>
impl<T: E2eTransport> E2eClient<T>
Sourcepub fn new(client: T) -> Self
pub fn new(client: T) -> Self
Create a client that keeps secret chat state in memory only.
Chats are lost when the process exits; use Self::with_persistence to keep them.
Sourcepub fn for_session(client: T, session: impl AsRef<Path>) -> Result<Self>
pub fn for_session(client: T, session: impl AsRef<Path>) -> Result<Self>
Create a client that keeps secret chat state next to a session file.
The store is <session_name>.e2e: the session path with its extension
replaced (mybot.session becomes mybot.e2e). See
with_persistence for the details.
Sourcepub fn with_persistence(client: T, path: impl Into<PathBuf>) -> Result<Self>
pub fn with_persistence(client: T, path: impl Into<PathBuf>) -> Result<Self>
Create a client that persists secret chat state to the binary file at path.
Previously saved chats are loaded on startup, and every change is written back
atomically. Loading fails with InvalidData if the file is corrupt or from an
unsupported version. The file holds each chat’s auth_key unencrypted (it is
created with mode 0600 on Unix), so protect it accordingly.
Sourcepub fn subscribe(&self) -> Receiver<E2eEvent>
pub fn subscribe(&self) -> Receiver<E2eEvent>
Subscribe to asynchronous events that are not tied to an incoming update
(currently only E2eEvent::MessageExpired for self-destruct timers).
Sourcepub async fn request_secret_chat_raw(
&self,
user_id: i64,
access_hash: i64,
) -> Result<i32, E2eError>
pub async fn request_secret_chat_raw( &self, user_id: i64, access_hash: i64, ) -> Result<i32, E2eError>
Initiate a secret chat with user_id / access_hash.
This is the low-level form for clients that already hold the access hash.
With ferogram, prefer E2eClientExt::request_secret_chat, which looks it up.
Sourcepub async fn accept_secret_chat(
&self,
chat: &EncryptedChatRequested,
) -> Result<(), E2eError>
pub async fn accept_secret_chat( &self, chat: &EncryptedChatRequested, ) -> Result<(), E2eError>
Accept an incoming secret chat request (User B side).
Sourcepub async fn finish_secret_chat(
&self,
chat: &EncryptedChat,
) -> Result<(), E2eError>
pub async fn finish_secret_chat( &self, chat: &EncryptedChat, ) -> Result<(), E2eError>
Complete chat creation on the initiator side (User A) after peer accepts.
Sourcepub async fn send_text(
&self,
chat_id: i32,
text: &str,
ttl: i32,
) -> Result<(), E2eError>
pub async fn send_text( &self, chat_id: i32, text: &str, ttl: i32, ) -> Result<(), E2eError>
Send a text message to an established secret chat.
Sourcepub async fn send_file(
&self,
chat_id: i32,
file_bytes: &[u8],
mime_type: &str,
file_name: &str,
caption: &str,
ttl: i32,
) -> Result<i64, E2eError>
pub async fn send_file( &self, chat_id: i32, file_bytes: &[u8], mime_type: &str, file_name: &str, caption: &str, ttl: i32, ) -> Result<i64, E2eError>
Encrypt and send a file as a secret chat document message.
file_bytes is the raw plaintext content.
mime_type e.g. “image/jpeg”, “application/pdf”.
file_name is the suggested filename shown to the recipient.
caption is optional (empty string = no caption).
ttl is self-destruct seconds (0 = no TTL).
Returns the random_id of the sent message.
Sourcepub async fn send_photo(
&self,
chat_id: i32,
photo_bytes: &[u8],
w: i32,
h: i32,
caption: &str,
ttl: i32,
) -> Result<i64, E2eError>
pub async fn send_photo( &self, chat_id: i32, photo_bytes: &[u8], w: i32, h: i32, caption: &str, ttl: i32, ) -> Result<i64, E2eError>
Send a photo to a secret chat.
Uses DecryptedMedia::Photo (constructor 0x32798a8c), which is what
official clients send for inline photos. w and h are the image
dimensions in pixels; pass 0 if unknown. ttl is self-destruct seconds
(0 = no TTL). Returns the random_id of the sent message.
Sourcepub async fn download_decrypted_file(
&self,
enc_file: &EncryptedFile,
key: &[u8],
iv: &[u8],
size: usize,
) -> Result<Vec<u8>, E2eError>
pub async fn download_decrypted_file( &self, enc_file: &EncryptedFile, key: &[u8], iv: &[u8], size: usize, ) -> Result<Vec<u8>, E2eError>
Download and decrypt an encrypted attachment received in a secret chat.
enc_file comes from E2eEvent::Message { file: Some(enc_file), .. }.
key and iv come from the DecryptedMedia inside the same message.
size is the original plaintext length from the DecryptedMedia size field.
Returns the decrypted plaintext bytes ready for forwarding or processing.
Sourcepub async fn send_service(
&self,
chat_id: i32,
action: DecryptedAction,
) -> Result<(), E2eError>
pub async fn send_service( &self, chat_id: i32, action: DecryptedAction, ) -> Result<(), E2eError>
Send a service message to an established secret chat.
Sourcepub async fn discard(
&self,
chat_id: i32,
delete_history: bool,
) -> Result<(), E2eError>
pub async fn discard( &self, chat_id: i32, delete_history: bool, ) -> Result<(), E2eError>
Discard (close) a secret chat.
Sourcepub fn fingerprint(&self, chat_id: i32) -> Result<i64, E2eError>
pub fn fingerprint(&self, chat_id: i32) -> Result<i64, E2eError>
Return the 64-bit key fingerprint of a chat’s auth_key.
A sanity check for the key exchange, not a user-facing verification code.
Sourcepub fn emoji_fingerprint(
&self,
chat_id: i32,
) -> Result<[&'static str; 4], E2eError>
pub fn emoji_fingerprint( &self, chat_id: i32, ) -> Result<[&'static str; 4], E2eError>
Return 4 emoji derived from the chat key, for comparing out of band.
Both sides of the chat see the same 4 emoji (see
crate::crypto::emoji_fingerprint_indices); display them side by side and
let the users compare. A mismatch means the key exchange was tampered with.
Sourcepub async fn set_encrypted_typing(
&self,
chat_id: i32,
typing: bool,
) -> Result<(), E2eError>
pub async fn set_encrypted_typing( &self, chat_id: i32, typing: bool, ) -> Result<(), E2eError>
Send a typing indicator to a secret chat.
Sourcepub async fn read_encrypted_history(
&self,
chat_id: i32,
max_date: i32,
) -> Result<(), E2eError>
pub async fn read_encrypted_history( &self, chat_id: i32, max_date: i32, ) -> Result<(), E2eError>
Mark secret chat messages as read up to max_date (Unix timestamp).
Sourcepub async fn received_queue(&self, max_qts: i32) -> Result<(), E2eError>
pub async fn received_queue(&self, max_qts: i32) -> Result<(), E2eError>
Acknowledge the secret chat message queue up to max_qts.
Sourcepub async fn initiate_rekey(&self, chat_id: i32) -> Result<(), E2eError>
pub async fn initiate_rekey(&self, chat_id: i32) -> Result<(), E2eError>
Initiate a re-key exchange for a chat.
Sourcepub async fn handle_rekey_request(
&self,
chat_id: i32,
action: &DecryptedAction,
) -> Result<(), E2eError>
pub async fn handle_rekey_request( &self, chat_id: i32, action: &DecryptedAction, ) -> Result<(), E2eError>
Handle an incoming RequestKey rekey action.
Sourcepub async fn handle_rekey_accept(
&self,
chat_id: i32,
action: &DecryptedAction,
) -> Result<(), E2eError>
pub async fn handle_rekey_accept( &self, chat_id: i32, action: &DecryptedAction, ) -> Result<(), E2eError>
Handle an incoming AcceptKey rekey action.
Sourcepub async fn handle_rekey_commit(
&self,
chat_id: i32,
action: &DecryptedAction,
) -> Result<(), E2eError>
pub async fn handle_rekey_commit( &self, chat_id: i32, action: &DecryptedAction, ) -> Result<(), E2eError>
Handle an incoming CommitKey rekey action.
Sourcepub async fn receive(
&self,
chat_id: i32,
data: &[u8],
) -> Result<Option<DecryptedMessage>, E2eError>
pub async fn receive( &self, chat_id: i32, data: &[u8], ) -> Result<Option<DecryptedMessage>, E2eError>
Decrypt an incoming encrypted message blob and return the inner message.
Validates the incoming seq_no and sends a Resend action if a gap is
detected. Handles service actions internally; returns None for them.
Source§impl<T: E2eTransport> E2eClient<T>
impl<T: E2eTransport> E2eClient<T>
Sourcepub async fn handle_update(
&self,
update: &Update,
) -> Result<Option<E2eEvent>, E2eError>
pub async fn handle_update( &self, update: &Update, ) -> Result<Option<E2eEvent>, E2eError>
Feed a raw Telegram update into the E2E layer.
Returns Some(E2eEvent) for events the caller needs to act on, and
None for updates that are not secret-chat related or that produce no
caller-visible event (service messages are handled internally).
With ferogram, use ferogram::e2e::E2eClientExt::handle_client_update
to pass an Update straight from the update stream.