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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
|
use std::sync::{Arc, RwLock};
use futures::compat::Future01CompatExt;
use futures01::{future::Future, sink::Sink};
use tokio02::sync::mpsc::UnboundedSender;
use tsclientlib::Event::ConEvents;
use tsclientlib::{
events::Event, ChannelId, ClientId, ConnectOptions, Connection, DisconnectOptions,
MessageTarget, Reason,
};
use log::error;
use crate::bot::{Message, MusicBotMessage};
mod bbcode;
pub use bbcode::*;
pub struct TeamSpeakConnection {
conn: Connection,
}
fn get_message<'a>(event: &Event) -> Option<MusicBotMessage> {
use tsclientlib::events::{PropertyId, PropertyValue};
match event {
Event::Message {
from: target,
invoker: sender,
message: msg,
} => Some(MusicBotMessage::TextMessage(Message {
target: *target,
invoker: sender.clone(),
text: msg.clone(),
})),
Event::PropertyAdded {
id: property,
invoker: _,
} => match property {
PropertyId::Channel(id) => {
Some(MusicBotMessage::ChannelCreated(*id))
}
_ => None,
},
Event::PropertyChanged {
id: property,
old: from,
invoker: _,
} => match property {
PropertyId::ClientChannel(client) => {
if let PropertyValue::ChannelId(from) = from {
Some(MusicBotMessage::ClientChannel {
client: *client,
old_channel: *from,
})
} else {
None
}
}
_ => None,
},
Event::PropertyRemoved {
id: property,
old: client,
invoker: _,
} => match property {
PropertyId::Client(id) => {
if let PropertyValue::Client(client) = client {
Some(MusicBotMessage::ClientDisconnected {
id: *id,
client: client.clone(),
})
} else {
None
}
}
_ => None,
},
_ => None,
}
}
impl TeamSpeakConnection {
pub async fn new(
tx: Arc<RwLock<UnboundedSender<MusicBotMessage>>>,
options: ConnectOptions,
) -> Result<TeamSpeakConnection, tsclientlib::Error> {
let conn = Connection::new(options).compat().await?;
let packet = conn.lock().server.set_subscribed(true);
conn.send_packet(packet).compat().await.unwrap();
conn.add_event_listener(
String::from("listener"),
Box::new(move |e| {
if let ConEvents(_conn, events) = e {
for event in *events {
if let Some(msg) = get_message(event) {
let tx = tx.read().expect("RwLock was not poisoned");
// Ignore the result because the receiver might get dropped first.
let _ = tx.send(msg);
}
}
}
}),
);
Ok(TeamSpeakConnection { conn })
}
pub fn send_audio_packet(&self, samples: &[u8]) {
let packet =
tsproto_packets::packets::OutAudio::new(&tsproto_packets::packets::AudioData::C2S {
id: 0,
codec: tsproto_packets::packets::CodecType::OpusMusic,
data: samples,
});
let send_packet = self
.conn
.get_packet_sink()
.send(packet)
.map(|_| ())
.map_err(|_| error!("Failed to send voice packet"));
tokio::run(send_packet);
}
pub fn channel_of_user(&self, id: ClientId) -> Option<ChannelId> {
Some(self.conn.lock().clients.get(&id)?.channel)
}
pub fn channel_path_of_user(&self, id: ClientId) -> Option<String> {
let conn = self.conn.lock();
let channel_id = conn.clients.get(&id)?.channel;
let mut channel = conn
.channels
.get(&channel_id)
.expect("can find user channel");
let mut names = vec![&channel.name[..]];
// Channel 0 is the root channel
while channel.parent != ChannelId(0) {
names.push("/");
channel = conn
.channels
.get(&channel.parent)
.expect("can find user channel");
names.push(&channel.name);
}
let mut path = String::new();
while let Some(name) = names.pop() {
path.push_str(name);
}
Some(path)
}
pub fn my_channel(&self) -> ChannelId {
let conn = self.conn.lock();
conn.clients
.get(&conn.own_client)
.expect("can find myself")
.channel
}
pub fn user_count(&self, channel: ChannelId) -> u32 {
let conn = self.conn.lock();
let mut count = 0;
for (_, client) in &conn.clients {
if client.channel == channel {
count += 1;
}
}
count
}
pub fn set_nickname(&self, name: &str) {
tokio::spawn(
self.conn
.lock()
.to_mut()
.set_name(name)
.map_err(|e| error!("Failed to set nickname: {}", e)),
);
}
pub fn set_description(&self, desc: &str) {
tokio::spawn(
self.conn
.lock()
.to_mut()
.get_client(&self.conn.lock().own_client)
.expect("can get myself")
.set_description(desc)
.map_err(|e| error!("Failed to change description: {}", e)),
);
}
pub fn send_message_to_channel(&self, text: &str) {
tokio::spawn(
self.conn
.lock()
.to_mut()
.send_message(MessageTarget::Channel, text)
.map_err(|e| error!("Failed to send message: {}", e)),
);
}
pub fn send_message_to_user(&self, client: ClientId, text: &str) {
tokio::spawn(
self.conn
.lock()
.to_mut()
.send_message(MessageTarget::Client(client), text)
.map_err(|e| error!("Failed to send message: {}", e)),
);
}
pub fn subscribe_all(&self) {
let packet = self.conn.lock().to_mut().server.set_subscribed(true);
tokio::spawn(
self.conn
.send_packet(packet)
.map_err(|e| error!("Failed to send subscribe packet: {}", e)),
);
}
pub fn disconnect(&self, reason: &str) {
let opt = DisconnectOptions::new()
.reason(Reason::Clientdisconnect)
.message(reason);
tokio::spawn(
self.conn
.disconnect(opt)
.map_err(|e| error!("Failed to send message: {}", e)),
);
}
}
|