Skip to content

Commit

Permalink
fix: group code in poke event (lz1998#106)
Browse files Browse the repository at this point in the history
* support group poke event

* cargo fmt

* add group_code for GroupPoke

* msf offline if decode error

* optimize zero-copy

* reduce memory allocation times
  • Loading branch information
LaoLittle authored Feb 16, 2023
1 parent c88d081 commit e7946cc
Show file tree
Hide file tree
Showing 15 changed files with 89 additions and 74 deletions.
2 changes: 1 addition & 1 deletion ricq-core/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub fn group_uin2code(uin: i64) -> i64 {
left * 1000000 + uin % 1000000
}

#[derive(Debug, Clone, Default)]
#[derive(Debug, Clone, Copy, Default)]
pub struct RQAddr(pub u32, pub u16);

impl From<RQAddr> for SocketAddr {
Expand Down
1 change: 0 additions & 1 deletion ricq-core/src/highway/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ pub struct Session {
pub struct BdhInput {
// 1-friend, 2-group, 299-groupPtt
pub command_id: i32,
pub body: Vec<u8>,
pub ticket: Vec<u8>,
pub ext: Vec<u8>,
pub encrypt: bool,
Expand Down
1 change: 1 addition & 0 deletions ricq-core/src/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ pub struct FriendPoke {

#[derive(Debug, Clone, Default)]
pub struct GroupPoke {
pub group_code: i64,
pub sender: i64,
pub receiver: i64,
}
Expand Down
24 changes: 12 additions & 12 deletions ricq-core/src/wtlogin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ use crate::utils::OptionSet;
use crate::Transport;

impl super::Engine {
pub fn process_qrcode_confirmed(&mut self, resp: QRCodeConfirmed) {
self.transport.sig.tgtgt_key = resp.tgtgt_key;
pub fn process_qrcode_confirmed(&mut self, resp: &QRCodeConfirmed) {
self.transport.sig.tgtgt_key = resp.tgtgt_key.clone();
self.uin.store(resp.uin, Ordering::Relaxed);
}

pub fn process_login_response(&mut self, login_response: LoginResponse) {
pub fn process_login_response(&mut self, login_response: &LoginResponse) {
match login_response {
LoginResponse::Success(resp) => self.process_login_success(resp),
LoginResponse::Success(resp) => self.process_login_success(resp.clone()),
LoginResponse::NeedCaptcha(resp) => self.process_need_captcha(resp),
LoginResponse::DeviceLocked(resp) => self.process_device_locked(resp),
LoginResponse::DeviceLockLogin(resp) => self.process_device_lock_login(resp),
LoginResponse::DeviceLockLogin(resp) => self.process_device_lock_login(resp.clone()),
_ => {}
}
}
Expand Down Expand Up @@ -61,16 +61,16 @@ impl super::Engine {
}
}

fn process_need_captcha(&mut self, resp: LoginNeedCaptcha) {
self.transport.sig.t104.option_set(resp.t104);
fn process_need_captcha(&mut self, resp: &LoginNeedCaptcha) {
self.transport.sig.t104.option_set(resp.t104.clone());
}

fn process_device_locked(&mut self, resp: LoginDeviceLocked) {
self.transport.sig.t104.option_set(resp.t104);
self.transport.sig.t174.option_set(resp.t174);
fn process_device_locked(&mut self, resp: &LoginDeviceLocked) {
self.transport.sig.t104.option_set(resp.t104.clone());
self.transport.sig.t174.option_set(resp.t174.clone());

if let Some(v) = resp.t402 {
set_t402(&mut self.transport, v)
if let Some(v) = &resp.t402 {
set_t402(&mut self.transport, v.clone())
}
}
fn process_device_lock_login(&mut self, resp: LoginDeviceLockLogin) {
Expand Down
4 changes: 2 additions & 2 deletions ricq-guild/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ impl GuildClient {
&self,
guild_id: u64,
channel_id: u64,
image: Vec<u8>,
image: &[u8],
) -> RQResult<GuildImage> {
let info = ImageInfo::try_new(&image)?;

Expand Down Expand Up @@ -202,7 +202,6 @@ impl GuildClient {
addr.clone().into(),
BdhInput {
command_id: 83,
body: image,
ticket: upload_key,
ext: dynamic_message! {
11 => guild_id,
Expand All @@ -214,6 +213,7 @@ impl GuildClient {
chunk_size: 256 * 1024,
send_echo: true,
},
image,
)
.await?;

Expand Down
14 changes: 7 additions & 7 deletions ricq/src/client/api/friend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ impl super::super::Client {
.await
}

pub async fn upload_friend_image(&self, target: i64, data: Vec<u8>) -> RQResult<FriendImage> {
pub async fn upload_friend_image(&self, target: i64, data: &[u8]) -> RQResult<FriendImage> {
let image_info = ImageInfo::try_new(&data)?;
let image_store = self.get_off_pic_store(target, &image_info).await?;

Expand All @@ -203,22 +203,22 @@ impl super::super::Client {
mut upload_addrs,
} => {
let addr = match self.highway_addrs.read().await.first() {
Some(addr) => addr.clone(),
Some(addr) => *addr,
None => upload_addrs
.pop()
.ok_or(RQError::EmptyField("upload_addrs"))?,
};
self.highway_upload_bdh(
addr.clone().into(),
addr.into(),
BdhInput {
command_id: 1,
body: data,
ticket: upload_key,
ext: vec![],
encrypt: false,
chunk_size: 256 * 1024,
send_echo: true,
},
data,
)
.await?;
image_info.into_friend_image(res_id, uuid)
Expand Down Expand Up @@ -295,7 +295,7 @@ impl super::super::Client {
pub async fn upload_friend_audio(
&self,
target: i64,
data: Vec<u8>,
data: &[u8],
audio_duration: Duration,
) -> RQResult<FriendAudio> {
let md5 = md5::compute(&data).to_vec();
Expand All @@ -311,7 +311,7 @@ impl super::super::Client {
.read()
.await
.first()
.cloned()
.copied()
.ok_or(RQError::EmptyField("highway_addrs"))?;
let ticket = self
.highway_session
Expand All @@ -325,13 +325,13 @@ impl super::super::Client {
addr.into(),
BdhInput {
command_id: 26,
body: data,
ticket,
ext: ext.to_vec(),
encrypt: false,
chunk_size: 256 * 1024,
send_echo: true,
},
data,
)
.await?;
let uuid = self
Expand Down
39 changes: 20 additions & 19 deletions ricq/src/client/api/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -516,15 +516,15 @@ impl super::super::Client {
}

/// 上传群图片
pub async fn upload_group_image(&self, group_code: i64, data: Vec<u8>) -> RQResult<GroupImage> {
pub async fn upload_group_image(&self, group_code: i64, data: &[u8]) -> RQResult<GroupImage> {
let image_info = ImageInfo::try_new(&data)?;

let image_store = self.get_group_image_store(group_code, &image_info).await?;
let signature = self.highway_session.read().await.session_key.to_vec();
let group_image = match image_store {
GroupImageStoreResp::Exist { file_id, addrs } => image_info.into_group_image(
file_id,
addrs.first().cloned().unwrap_or_default(),
addrs.first().copied().unwrap_or_default(),
signature,
),
GroupImageStoreResp::NotExist {
Expand All @@ -533,22 +533,22 @@ impl super::super::Client {
mut upload_addrs,
} => {
let addr = match self.highway_addrs.read().await.first() {
Some(addr) => addr.clone(),
Some(addr) => *addr,
None => upload_addrs
.pop()
.ok_or(RQError::EmptyField("upload_addrs"))?,
};
self.highway_upload_bdh(
addr.clone().into(),
addr.into(),
BdhInput {
command_id: 2,
body: data,
ticket: upload_key,
ext: vec![],
encrypt: false,
chunk_size: 256 * 1024,
send_echo: true,
},
data,
)
.await?;
image_info.into_group_image(file_id, addr, signature)
Expand All @@ -561,7 +561,7 @@ impl super::super::Client {
pub async fn upload_group_audio(
&self,
group_code: i64,
data: Vec<u8>,
data: &[u8],
codec: u32,
) -> RQResult<GroupAudio> {
let md5 = md5::compute(&data).to_vec();
Expand All @@ -578,7 +578,7 @@ impl super::super::Client {
.read()
.await
.first()
.cloned()
.copied()
.ok_or(RQError::EmptyField("highway_addrs"))?;
let ticket = self
.highway_session
Expand All @@ -592,13 +592,13 @@ impl super::super::Client {
addr.into(),
BdhInput {
command_id: 29,
body: data,
ticket,
ext: ext.to_vec(),
encrypt: false,
chunk_size: 256 * 1024,
send_echo: true,
},
data,
)
.await?;
let file_key = self
Expand Down Expand Up @@ -654,8 +654,8 @@ impl super::super::Client {
pub async fn upload_group_short_video(
&self,
group_code: i64,
video_data: Vec<u8>,
thumb_data: Vec<u8>,
video_data: &[u8],
thumb_data: &[u8],
) -> RQResult<VideoFile> {
let video_md5 = md5::compute(&video_data).to_vec();
let thumb_md5 = md5::compute(&thumb_data).to_vec();
Expand All @@ -668,9 +668,9 @@ impl super::super::Client {
video_size as i64,
thumb_size as i64,
);
let video_store = self
.get_group_short_video_store(short_video_up_req.clone())
.await?;
let ext = short_video_up_req.to_bytes().to_vec();

let video_store = self.get_group_short_video_store(short_video_up_req).await?;

if video_store.file_exists == 1 {
return Ok(VideoFile {
Expand All @@ -688,28 +688,29 @@ impl super::super::Client {
.read()
.await
.first()
.ok_or(RQError::EmptyField("highway_addrs"))?
.clone();
.copied()
.ok_or(RQError::EmptyField("highway_addrs"))?;

if self.highway_session.read().await.session_key.is_empty() {
return Err(RQError::EmptyField("highway_session_key"));
}
let ticket = self.highway_session.read().await.sig_session.to_vec();
let mut data = thumb_data;
data.extend(video_data);
let mut data = Vec::with_capacity(thumb_size + video_size);
data.copy_from_slice(thumb_data);
data[thumb_size..].copy_from_slice(video_data);

let rsp = self
.highway_upload_bdh(
addr.into(),
BdhInput {
command_id: 25,
body: data,
ticket,
ext: short_video_up_req.to_bytes().to_vec(),
ext,
encrypt: true,
chunk_size: 256 * 1024,
send_echo: true,
},
&data,
)
.await?;
let rsp = pb::short_video::ShortVideoUploadRsp::decode(&*rsp)
Expand Down
18 changes: 9 additions & 9 deletions ricq/src/client/api/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl super::super::Client {
.read()
.await
.decode_trans_emp_response(resp.body)?;
self.process_trans_emp_response(resp.clone()).await;
self.process_trans_emp_response(&resp).await;
Ok(resp)
}

Expand All @@ -33,7 +33,7 @@ impl super::super::Client {
.read()
.await
.decode_trans_emp_response(resp.body)?;
self.process_trans_emp_response(resp.clone()).await;
self.process_trans_emp_response(&resp).await;
Ok(resp)
}

Expand All @@ -51,7 +51,7 @@ impl super::super::Client {
.build_qrcode_login_packet(tmp_pwd, tmp_no_pic_sig, tgt_qr);
let resp = self.send_and_wait(req).await?;
let resp = self.engine.read().await.decode_login_response(resp.body)?;
self.process_login_response(resp.clone()).await;
self.process_login_response(&resp).await;
Ok(resp)
}

Expand All @@ -69,7 +69,7 @@ impl super::super::Client {
.build_login_packet(password_md5, true);
let resp = self.send_and_wait(req).await?;
let resp = self.engine.read().await.decode_login_response(resp.body)?;
self.process_login_response(resp.clone()).await;
self.process_login_response(&resp).await;
Ok(resp)
}

Expand All @@ -83,7 +83,7 @@ impl super::super::Client {
let req = self.engine.read().await.build_sms_request_packet();
let resp = self.send_and_wait(req).await?;
let resp = self.engine.read().await.decode_login_response(resp.body)?;
self.process_login_response(resp.clone()).await;
self.process_login_response(&resp).await;
Ok(resp)
}

Expand All @@ -96,7 +96,7 @@ impl super::super::Client {
.build_sms_code_submit_packet(code.trim());
let resp = self.send_and_wait(req).await?;
let resp = self.engine.read().await.decode_login_response(resp.body)?;
self.process_login_response(resp.clone()).await;
self.process_login_response(&resp).await;
Ok(resp)
}

Expand All @@ -105,7 +105,7 @@ impl super::super::Client {
let req = self.engine.read().await.build_ticket_submit_packet(ticket);
let resp = self.send_and_wait(req).await?;
let resp = self.engine.read().await.decode_login_response(resp.body)?;
self.process_login_response(resp.clone()).await;
self.process_login_response(&resp).await;
Ok(resp)
}

Expand All @@ -114,7 +114,7 @@ impl super::super::Client {
let req = self.engine.read().await.build_device_lock_login_packet();
let resp = self.send_and_wait(req).await?;
let resp = self.engine.read().await.decode_login_response(resp.body)?;
self.process_login_response(resp.clone()).await;
self.process_login_response(&resp).await;
Ok(resp)
}

Expand All @@ -137,7 +137,7 @@ impl super::super::Client {
.read()
.await
.decode_exchange_emp_response(resp.body)?;
self.process_login_response(resp.clone()).await;
self.process_login_response(&resp).await;
Ok(resp)
}

Expand Down
Loading

0 comments on commit e7946cc

Please sign in to comment.