1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
use std::fmt::{Debug, Display, Error, Formatter};

use crate::{
    api::{delete_msg, Convert, Result},
    targets::{cqcode, cqcode::CQCode},
};

#[derive(Debug, Default, Clone)]
pub struct Message {
    pub msg: String,
    msg_id: i32,
    pub raw_msg: String,
    pub cqcodes: Vec<CQCode>,
}

impl Message {
    pub fn new(msg: impl Into<Convert<String>>, msg_id: i32) -> Self {
        let msg = msg.into().to_string();
        Message {
            msg: Self::escape(cqcode::clean(msg.as_ref())),
            cqcodes: cqcode::parse(msg.as_ref()),
            raw_msg: msg,
            msg_id,
        }
    }

    /// 撤回消息
    pub fn delete(&self) -> bool {
        delete_msg(self.msg_id).is_ok()
    }

    pub fn has_cqcode(&self) -> bool {
        self.cqcodes.iter().count() != 0
    }

    // 将因为防止与cq码混淆而转义的字符还原
    fn escape(s: String) -> String {
        s.replace("&amp;", "&")
            .replace("&#91;", "[")
            .replace("&#93;", "]")
            .replace("&#44;", ",")
    }
}

/// Examples:
/// ```
/// use coolq_sdk_rust::targets::message::MessageSegment;
///
/// let msg = MessageSegment::new()
///             .add("hi")
///             .newline()
///             .face(10);
///
/// // xx.send_message(msg);
/// // api::send_private_msg(qq, msg);
/// // event.reply(msg);
/// ```
#[derive(Clone)]
pub struct MessageSegment(String);

impl MessageSegment {
    pub fn new() -> Self {
        MessageSegment(String::new())
    }

    pub fn add(&mut self, msg: impl ToString) -> &mut Self {
        self.0.push_str(msg.to_string().as_ref());
        self
    }

    pub fn at_all(&mut self) -> &mut Self {
        self.add(CQCode::AtAll())
    }

    pub fn at(&mut self, user_id: i64) -> &mut Self {
        self.add(CQCode::At(user_id))
    }

    pub fn face(&mut self, face_id: i32) -> &mut Self {
        self.add(CQCode::Face(face_id))
    }

    pub fn bface(&mut self, bface_id: i32) -> &mut Self {
        self.add(CQCode::Bface(bface_id))
    }

    pub fn sface(&mut self, sface_id: i32) -> &mut Self {
        self.add(CQCode::Sface(sface_id))
    }

    pub fn emoji(&mut self, emoji_id: i32) -> &mut Self {
        self.add(CQCode::Emoji(emoji_id))
    }

    pub fn newline(&mut self) -> &mut Self {
        self.add("\n")
    }

    pub fn newlines(&mut self, count: usize) -> &mut Self {
        self.add("\n".repeat(count))
    }
}

impl Display for MessageSegment {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::result::Result<(), Error> {
        write!(f, "{}", self.0)
    }
}

impl Debug for MessageSegment {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::result::Result<(), Error> {
        write!(f, "{}", self.0)
    }
}

pub trait SendMessage {
    /// `@return` msg id
    fn send_message(&self, msg: impl ToString) -> Result<i32> {
        match self.send(msg) {
            Ok(msg_id) => Ok(msg_id.into()),
            Err(err) => Err(err),
        }
    }

    fn send(&self, msg: impl ToString) -> crate::api::Result<Convert<i32>>;

    /// type参数暂不支持
    fn send_rps(&self) -> Result<i32> {
        self.send_message(CQCode::Rps(0))
    }

    /// type参数暂不支持
    fn send_dice(&self) -> Result<i32> {
        self.send_message(CQCode::Dice(0))
    }

    fn send_shake(&self) -> Result<i32> {
        self.send_message(CQCode::Shake())
    }

    fn send_anonymous(&self, ignore: bool, msg: impl ToString) -> Result<i32> {
        self.send_message(
            MessageSegment::new()
                .add(CQCode::Anonymous(ignore))
                .add(msg),
        )
    }

    fn send_location(
        &self, latitude: f32, longitude: f32, title: &str, content: &str,
    ) -> Result<i32> {
        self.send_message(CQCode::Location(
            latitude,
            longitude,
            title.to_owned(),
            content.to_owned(),
        ))
    }

    fn send_music(&self, _type: &str, id: i32, style: i32) -> Result<i32> {
        self.send_message(CQCode::Music(_type.to_owned(), id, style))
    }

    fn send_music_custom(
        &self, url: &str, audio: &str, title: &str, content: &str, image: &str,
    ) -> Result<i32> {
        self.send_message(CQCode::MusicCustom(
            url.to_owned(),
            audio.to_owned(),
            title.to_owned(),
            content.to_owned(),
            image.to_owned(),
        ))
    }

    fn send_share(&self, url: &str, title: &str, content: &str, image: &str) -> Result<i32> {
        self.send_message(CQCode::Share(
            url.to_owned(),
            title.to_owned(),
            content.to_owned(),
            image.to_owned(),
        ))
    }

    fn at(&self, user_id: i64, msg: impl ToString) -> Result<i32> {
        self.send_message(MessageSegment::new().add(CQCode::At(user_id)).add(msg))
    }
}