user_management.rs 14 KB
Newer Older
Ash's avatar
Ash committed
1
use rand::seq::SliceRandom;
tec's avatar
tec committed
2
use regex::Regex;
tec's avatar
tec committed
3
4
5
6
7
use serenity::{
    model::{channel::Message, guild::Member},
    prelude::*,
    utils::MessageBuilder,
};
tec's avatar
tec committed
8
9
use std::process::{Command, Stdio};
use url::Url;
tec's avatar
tec committed
10

11
use crate::config::CONFIG;
tec's avatar
tec committed
12
13
use crate::database;
use crate::ldap::ldap_exists;
tec's avatar
tec committed
14
use crate::token_management::*;
tec's avatar
tec committed
15
16
17
18
19
20
21

pub fn new_member(ctx: &Context, mut new_member: Member) {
    let mut message = MessageBuilder::new();
    message.push("Nice to see you here ");
    message.mention(&new_member);
    message.push_line("! Would you care to introduce yourself?");
    message.push_line("If you're not sure where to start, perhaps you could tell us about your projects, your first computer…");
tec's avatar
tec committed
22
23
24
    message.push_line("You should also know that we follow the Freenode Channel Guidelines: https://freenode.net/changuide, and try to avoid defamatory content.");
    message.push_line("Make sure to check out ");
    message.mention(&CONFIG.readme_channel);
tec's avatar
tec committed
25
26
27
    message.push_line(" to get yourself some roles for directed pings 😊, and ");
    message.push_mono(format!("{}register username", CONFIG.command_prefix));
    message.push_line(" to link to your UCC account.");
28
    send_message!(CONFIG.welcome_channel, &ctx, message.build());
tec's avatar
tec committed
29
30
31

    let mut message = MessageBuilder::new();
    message.push(format!("Say hi to {} in ", new_member.display_name()));
32
    message.mention(&CONFIG.welcome_channel);
33
    send_message!(CONFIG.main_channel, &ctx, message.build());
tec's avatar
tec committed
34

35
    if let Err(why) = new_member.add_role(&ctx.http, CONFIG.unregistered_member_role) {
tec's avatar
tec committed
36
        error!("Error adding user role: {:?}", why);
tec's avatar
tec committed
37
38
39
    };
}

tec's avatar
tec committed
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
fn member_nickname(member: &database::Member) -> String {
    let username = member.username.clone();
    if let Some(tla) = member.tla.clone() {
        if username.to_uppercase() == tla {
            return format!("{}", username);
        } else {
            return format!("{} [{}]", username, tla);
        }
    } else {
        return format!("{}", username);
    }
}

pub const RANDOM_SASS: &[&str] = &[
    "Please. As if I'd fall for that.",
    "Did you really think a stunt like that would work?",
    "Nothing slips past me.",
    "Did you even read the first line of !help?",
    "I never treated you this badly.",
Ash's avatar
Ash committed
59
60
];

Ash's avatar
fixes    
Ash committed
61
62
63
64
65
66
67
68
69
70
71
pub const RESERVED_NAMES: &[&str] = &[
    "committee",
    "committee-only",
    "ucc",
    "ucc-announce",
    "tech",
    "wheel",
    "door",
    "coke",
];

tec's avatar
tec committed
72
73
pub struct Commands;
impl Commands {
tec's avatar
tec committed
74
    pub fn register(ctx: Context, msg: Message, account_name: &str) {
Timothy du Heaume's avatar
Timothy du Heaume committed
75
        if account_name.is_empty() {
tec's avatar
tec committed
76
77
78
79
80
81
82
            send_message!(
                msg.channel_id,
                &ctx.http,
                format!("Usage: {}register <username>", CONFIG.command_prefix)
            );
            return;
        }
Ash's avatar
fixes    
Ash committed
83
        if RESERVED_NAMES.contains(&account_name)
tec's avatar
tec committed
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
            || database::username_exists(account_name)
        {
            send_message!(
                msg.channel_id,
                &ctx.http,
                RANDOM_SASS
                    .choose(&mut rand::thread_rng())
                    .expect("We couldn't get any sass")
            );
            return;
        }
        if !ldap_exists(account_name) {
            send_message!(
                msg.channel_id,
                &ctx.http,
                format!(
                    "I couldn't find an account with the username '{}'",
                    account_name
                )
            );
tec's avatar
tec committed
104
            return;
tec's avatar
tec committed
105
        }
tec's avatar
tec committed
106
107
        send_message!(
            msg.channel_id,
tec's avatar
tec committed
108
            &ctx.http,
tec's avatar
tec committed
109
110
111
112
113
            format!(
                "Ok {}, see the email I've just sent you to complete the link",
                account_name
            )
        );
tec's avatar
tec committed
114

tec's avatar
tec committed
115
        e!("Error deleting register message: {:?}", msg.delete(ctx));
tec's avatar
tec committed
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132

        let message = Command::new("echo").arg(format!("<h3>Link your Discord account</h3>\
                                                        <p>Hi {}, to complete the link, go to the discord server and enter\
                                                        <pre>{}verify {}</pre>\
                                                        </p><sub>The UCC discord bot</sub>",
                                                        account_name, CONFIG.command_prefix, generate_token(&msg.author, account_name))).stdout(Stdio::piped()).spawn().expect("Unable to spawn echo command");
        match Command::new("mutt")
            .arg("-e")
            .arg("set content_type=text/html")
            .arg("-e")
            .arg("set realname=\"UCC Discord Bot\"")
            .arg("-s")
            .arg("Discord account link token")
            .arg(format!("{}@ucc.asn.au", account_name))
            .stdin(message.stdout.unwrap())
            .output()
        {
tec's avatar
tec committed
133
            Ok(_) => info!("Email sent to {}", account_name),
tec's avatar
tec committed
134
135
            Err(why) => error!("Unable to send message with mutt {:?}", why),
        };
tec's avatar
tec committed
136
    }
tec's avatar
tec committed
137
138
    pub fn verify(ctx: Context, msg: Message, token: &str) {
        match parse_token(&msg.author, token) {
tec's avatar
tec committed
139
140
141
142
143
144
            Ok(name) => {
                e!(
                    "Unable to get member: {:?}",
                    serenity::model::id::GuildId(CONFIG.server_id)
                        .member(ctx.http.clone(), msg.author.id)
                        .map(|mut member| {
tec's avatar
tec committed
145
                            let full_member = database::add_member(&msg.author.id.0, &name);
tec's avatar
tec committed
146
147
148
149
                            e!(
                                "Unable to remove role: {:?}",
                                member.remove_role(&ctx.http, CONFIG.unregistered_member_role)
                            );
tec's avatar
tec committed
150
151
152
153
                            e!(
                                "Unable to add role: {:?}",
                                member.add_role(&ctx.http, CONFIG.registered_member_role)
                            );
tec's avatar
tec committed
154
155
156
                            e!(
                                "Unable to edit nickname: {:?}",
                                member.edit(&ctx.http, |m| {
tec's avatar
tec committed
157
                                    m.nickname(member_nickname(&full_member));
tec's avatar
tec committed
158
159
160
                                    m
                                })
                            );
tec's avatar
tec committed
161
162
163
164
                            let mut verification_message = MessageBuilder::new();
                            verification_message.push(format!("Verification was sucessful {}. To proide a friendly introduction to yourself consider doing ", &full_member.username));
                            verification_message.push_mono(format!("{}set bio <info>", CONFIG.command_prefix));
                            send_message!(
tec's avatar
tec committed
165
166
                                msg.channel_id,
                                ctx.http.clone(),
tec's avatar
tec committed
167
                                verification_message.build()
tec's avatar
tec committed
168
169
170
171
                            );
                        })
                );
            }
tec's avatar
tec committed
172
173
174
175
176
            Err(reason) => send_message!(
                msg.channel_id,
                &ctx.http,
                format!("Verification error: {:?}", reason)
            ),
tec's avatar
tec committed
177
178
        }
        e!("Error deleting register message: {:?}", msg.delete(&ctx));
tec's avatar
tec committed
179
    }
tec's avatar
tec committed
180
181
182
183
184
185
186
187
188
189
190
191
    pub fn profile(ctx: Context, msg: Message, name: &str) {
        let possible_member: Option<database::Member> = match if name.trim().is_empty() {
            database::get_member_info(&msg.author.id.0)
        } else {
            database::get_member_info_from_username(&name)
        } {
            Ok(member) => Some(member),
            Err(why) => {
                warn!("Could not find member {:?}", why);
                if name.len() != 3 {
                    None
                } else {
tec's avatar
tec committed
192
                    match database::get_member_info_from_tla(&name.to_uppercase()) {
tec's avatar
tec committed
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
                        Ok(member) => Some(member),
                        Err(_) => None,
                    }
                }
            }
        };
        if possible_member.is_none() {
            send_message!(
                msg.channel_id,
                &ctx.http,
                "Sorry, I couldn't find that profile (you need to !register for a profile)"
            );
            return;
        }
        let member = possible_member.unwrap();
        let result = msg.channel_id.send_message(&ctx.http, |m| {
            m.embed(|embed| {
                embed.colour(serenity::utils::Colour::LIGHTER_GREY);
                embed.footer(|f| {
                    let user = &ctx
                        .http
                        .get_user(member.discord_id.clone() as u64)
                        .expect("We expected this user to exist... they didn't ;(");
                    f.text(&user.name);
                    f.icon_url(
                        user.static_avatar_url()
                            .expect("Expected user to have avatar"),
                    );
                    f
                });
                if let Some(name) = member.name.clone() {
                    embed.title(name);
                }
                if let Some(photo) = member.photo.clone() {
                    embed.thumbnail(photo);
                }
                embed.field("Username", &member.username, true);
                if let Some(tla) = member.tla.clone() {
                    embed.field("TLA", tla, true);
                }
                if let Some(bio) = member.biography.clone() {
                    embed.field("Bio", bio, false);
                }
                if let Some(git) = member.github.clone() {
                    embed.field("Git", git, false);
                }
                if let Some(web) = member.website.clone() {
                    embed.field("Website", web, false);
                }
                embed
            });
            m
        });
        if let Err(why) = result {
            error!("Error sending profile embed: {:?}", why);
        }
    }
    pub fn set_info(ctx: Context, msg: Message, info: &str) {
        if info.trim().is_empty() {
            send_message!(
                msg.channel_id,
                &ctx.http,
                format!(
                    "Usage: {}set <bio|git|web|photo> <value>",
                    CONFIG.command_prefix
                )
            );
            return;
        }
        let info_content: Vec<_> = info.splitn(2, ' ').collect();
        let mut property = String::from(info_content[0]);
        property = property.replace("github", "git");
tec's avatar
tec committed
265
266
267
        if info_content.len() == 1
            || !vec!["bio", "git", "web", "photo"].contains(&property.as_str())
        {
tec's avatar
tec committed
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
            send_message!(
                msg.channel_id,
                &ctx.http,
                format!(
                    "Usage: {}set {} {}",
                    CONFIG.command_prefix,
                    property,
                    match property.as_str() {
                        "bio" => "some information about yourself :)",
                        "git" => "a url to your git{hub,lab} account",
                        "photo" => "a url to a profile photo online",
                        "web" => "a url to your website/webpage",
                        _ => "whatever you want, because this does absolutely nothing. Try !set to see what you can do"
                    }
                )
            );
            return;
        }
        let mut value = info_content[1].to_string();

        if vec!["git", "photo", "web"].contains(&property.as_str()) {
Ash's avatar
fixes    
Ash committed
289
            if Url::parse(&value).is_err() {
tec's avatar
tec committed
290
291
292
293
294
                let user_regex = Regex::new(r"^\w+$").unwrap();
                if property == "git" && user_regex.is_match(&value) {
                    value = format!("github.com/{}", value);
                }
                value = format!("https://{}", value);
Ash's avatar
fixes    
Ash committed
295
                if Url::parse(&value).is_err() {
tec's avatar
tec committed
296
297
298
299
300
301
302
303
304
                    send_message!(
                        msg.channel_id,
                        &ctx.http,
                        "That ain't a URL where I come from..."
                    );
                    return;
                }
            }
        }
Ash's avatar
fixes    
Ash committed
305
        guard!(let Ok(member) = database::get_member_info(&msg.author.id.0) else {
tec's avatar
tec committed
306
307
308
309
310
311
312
            send_message!(
                msg.channel_id,
                &ctx.http,
                format!(
                    "You don't seem to have a profile. {}register to get one",
                    CONFIG.command_prefix
                )
Ash's avatar
fixes    
Ash committed
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
            );
            return
        });
        let set_property = match property.as_str() {
            "bio" => database::set_member_bio(&msg.author.id.0, &value),
            "git" => database::set_member_git(&msg.author.id.0, &value),
            "photo" => database::set_member_photo(&msg.author.id.0, &value),
            "web" => database::set_member_website(&msg.author.id.0, &value),
            _ => Err(diesel::result::Error::NotFound),
        };
        match set_property {
            Ok(_) => if property == "git" && member.photo == None {
                let git_url = Url::parse(&value).unwrap(); // we parsed this earlier and it was fine
                match git_url.host_str() {
                    Some("github.com") => {
                        if let Some(mut path_segments) = git_url.path_segments() {
                            database::set_member_photo(
                                &msg.author.id.0,
                                format!(
                                    "https://github.com/{}.png",
                                    path_segments.next().expect("URL doesn't have a path")
                                )
                                .as_str(),
                            )
                            .expect("Attempt to set member photo failed");
                        } else {
                            info!("Git path added (2), {}", git_url.path());
                        }
                    }
                    _ => info!("Git path added, {}", git_url.path()),
                }
            }
            Err(why) => {
                error!(
                    "Umable to set property {} to {} in DB {:?}",
                    property, value, why
                );
                send_message!(msg.channel_id, &ctx.http, "Failed to set property. Ooops.");
            }
        }
        if let Err(why) = msg.delete(&ctx) {
            error!("Error deleting set profile property: {:?}", why);
tec's avatar
tec committed
355
356
        }
    }
tec's avatar
tec committed
357
}