Skip to main content

E2eClient

Struct E2eClient 

Source
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>

Source

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.

Source

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.

Source

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.

Source

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).

Source

pub fn client(&self) -> &T

The underlying transport (client) this E2E client sends through.

Source

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.

Source

pub async fn accept_secret_chat( &self, chat: &EncryptedChatRequested, ) -> Result<(), E2eError>

Accept an incoming secret chat request (User B side).

Source

pub async fn finish_secret_chat( &self, chat: &EncryptedChat, ) -> Result<(), E2eError>

Complete chat creation on the initiator side (User A) after peer accepts.

Source

pub async fn send_text( &self, chat_id: i32, text: &str, ttl: i32, ) -> Result<(), E2eError>

Send a text message to an established secret chat.

Source

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.

Source

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.

Source

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.

Source

pub async fn send_service( &self, chat_id: i32, action: DecryptedAction, ) -> Result<(), E2eError>

Send a service message to an established secret chat.

Source

pub async fn discard( &self, chat_id: i32, delete_history: bool, ) -> Result<(), E2eError>

Discard (close) a secret chat.

Source

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.

Source

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.

Source

pub async fn set_encrypted_typing( &self, chat_id: i32, typing: bool, ) -> Result<(), E2eError>

Send a typing indicator to a secret chat.

Source

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).

Source

pub async fn received_queue(&self, max_qts: i32) -> Result<(), E2eError>

Acknowledge the secret chat message queue up to max_qts.

Source

pub async fn initiate_rekey(&self, chat_id: i32) -> Result<(), E2eError>

Initiate a re-key exchange for a chat.

Source

pub async fn handle_rekey_request( &self, chat_id: i32, action: &DecryptedAction, ) -> Result<(), E2eError>

Handle an incoming RequestKey rekey action.

Source

pub async fn handle_rekey_accept( &self, chat_id: i32, action: &DecryptedAction, ) -> Result<(), E2eError>

Handle an incoming AcceptKey rekey action.

Source

pub async fn handle_rekey_commit( &self, chat_id: i32, action: &DecryptedAction, ) -> Result<(), E2eError>

Handle an incoming CommitKey rekey action.

Source

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>

Source

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.

Trait Implementations§

Source§

impl<T: Clone + E2eTransport> Clone for E2eClient<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for E2eClient<T>

§

impl<T> !UnwindSafe for E2eClient<T>

§

impl<T> Forget for E2eClient<T>

§

impl<T> Freeze for E2eClient<T>
where T: Freeze,

§

impl<T> Send for E2eClient<T>

§

impl<T> Sync for E2eClient<T>

§

impl<T> Unpin for E2eClient<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for E2eClient<T>
where T: UnsafeUnpin,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more