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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
use std::collections::HashSet;
use tokio::io::{AsyncRead, AsyncWrite};
use tokio_util::codec::Framed;
use crate::conn::sendrecv;
use crate::types::ObjRef;
use crate::err::Error;
#[derive(Debug)]
pub struct Account {
pub id: i64,
pub name: String,
pub lock: bool,
pub perms: HashSet<String>
}
pub async fn rd<T: AsyncRead + AsyncWrite + Unpin>(
conn: &mut Framed<T, blather::Codec>,
acc: Option<ObjRef>
) -> Result<Account, Error> {
let mut tg = blather::Telegram::new_topic("RdAcc")?;
if let Some(acc) = acc {
match acc {
ObjRef::Id(id) => {
tg.add_param("Id", id)?;
}
ObjRef::Name(nm) => {
tg.add_str("Name", &nm)?;
}
}
}
let params = sendrecv(conn, &tg).await?;
let id = params.get_int::<i64>("Id")?;
let name = params.get_param::<String>("Name")?;
let lock = params.get_bool("Lock")?;
let perms = params.get_hashset("Perms")?;
let acc = Account {
id,
name,
lock,
perms
};
Ok(acc)
}
#[derive(Debug)]
pub struct LsEntry {
pub id: i64,
pub name: String
}
pub async fn ls<T: AsyncRead + AsyncWrite + Unpin>(
conn: &mut Framed<T, blather::Codec>,
inclock: bool
) -> Result<Vec<LsEntry>, Error> {
let mut tg = blather::Telegram::new_topic("LsAcc")?;
match inclock {
true => {
tg.add_bool("All", true)?;
}
_ => {}
}
let params = sendrecv(conn, &tg).await?;
let num_entries = params.get_int::<usize>("#")?;
let mut acclist = Vec::with_capacity(num_entries);
for i in 0..num_entries {
let id = format!("{}.Id", i);
let name = format!("{}.Name", i);
acclist.push(LsEntry {
id: params.get_int::<i64>(&id).unwrap(),
name: params.get_str(&name).unwrap().to_string()
});
}
Ok(acclist)
}
pub enum ModPerms {
Set(HashSet<String>),
Grant(HashSet<String>),
Revoke(HashSet<String>),
GrantRevoke(HashSet<String>, HashSet<String>)
}
pub struct WrAccount {
pub name: Option<String>,
pub username: Option<String>,
pub lock: Option<bool>,
pub perms: Option<ModPerms>
}
pub async fn wr<T: AsyncRead + AsyncWrite + Unpin>(
conn: &mut Framed<T, blather::Codec>,
acc: ObjRef,
ai: WrAccount
) -> Result<(), Error> {
let mut tg = blather::Telegram::new_topic("WrAcc")?;
match acc {
ObjRef::Id(id) => {
tg.add_param("Id", id)?;
}
ObjRef::Name(nm) => {
tg.add_str("Name", &nm)?;
}
}
if let Some(name) = ai.name {
tg.add_str("NewName", &name)?;
}
if let Some(username) = ai.username {
tg.add_str("UserName", &username)?;
}
if let Some(lck) = ai.lock {
tg.add_bool("Lock", lck)?;
}
if let Some(perms) = ai.perms {
match perms {
ModPerms::Set(set) => {
tg.add_strit("Perms", set.iter())?;
}
ModPerms::Grant(set) => {
tg.add_strit("Grant", set.iter())?;
}
ModPerms::Revoke(set) => {
tg.add_strit("Revoke", set.iter())?;
}
ModPerms::GrantRevoke(grant, revoke) => {
tg.add_strit("Grant", grant.iter())?;
tg.add_strit("Revoke", revoke.iter())?;
}
}
}
sendrecv(conn, &tg).await?;
Ok(())
}
pub async fn rm<T: AsyncRead + AsyncWrite + Unpin>(
conn: &mut Framed<T, blather::Codec>,
acc: ObjRef
) -> Result<(), Error> {
let mut tg = blather::Telegram::new_topic("RmAcc")?;
match acc {
ObjRef::Id(id) => {
tg.add_param("Id", id)?;
}
ObjRef::Name(nm) => {
tg.add_str("Name", &nm)?;
}
}
sendrecv(conn, &tg).await?;
Ok(())
}