user_management.rs 20.4 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
use serenity::{
Ash's avatar
Ash committed
4
    model::{channel::Message, guild::Member, id::RoleId},
tec's avatar
tec committed
5
6
7
    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
use crate::database;
Ash's avatar
Ash committed
13
use crate::ldap::{ldap_exists, ldap_search};
tec's avatar
tec committed
14
use crate::token_management::*;
tec's avatar
tec committed
15
16

pub fn new_member(ctx: &Context, mut new_member: Member) {
tec's avatar
tec committed
17
    // TODO see if it's an old (registered) user re-joining, and act accordingly
tec's avatar
tec committed
18
19
20
21
22
    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
23
    message.push_line("You should also know that we follow the Freenode Channel Guidelines: https://freenode.net/changuide, and try to avoid defamatory content.");
24
    message.push("Make sure to check out ");
tec's avatar
tec committed
25
    message.mention(&CONFIG.readme_channel);
26
    message.push(" to get yourself some roles for directed pings 😊, and ");
tec's avatar
tec committed
27
    message.push_mono(format!("{}register username", CONFIG.command_prefix));
28
    message.push(" to link to your UCC account.");
29
    send_message!(CONFIG.welcome_channel, &ctx, message.build());
tec's avatar
tec committed
30
31
32

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

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

tec's avatar
tec committed
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
60
61
];

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

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

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

        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
132
            Ok(_) => info!("Email sent to {}", account_name),
tec's avatar
tec committed
133
134
            Err(why) => error!("Unable to send message with mutt {:?}", why),
        };
tec's avatar
tec committed
135
    }
Ash's avatar
Ash committed
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

    pub fn get_registered_role(name: String) -> Option<u64> {
        guard!(let Some(result) = ldap_search(&name) else {
            return None
        });
        if result.login_shell.contains("locked")
            && CONFIG.expired_member_role > 0 {
            return Some(CONFIG.expired_member_role)
        }
        Some(CONFIG.registered_member_role)
    }

    // TODO: make this return a result
    // NOTE: don't make this directly send messages, so it can be used for mass updates
    pub fn update_registered_role(ctx: Context, msg: Message) {
        guard!(let Ok(member_info) = database::get_member_info(&msg.author.id.0) else {
            return // Err()
        });
        guard!(let Some(registered_role) = Commands::get_registered_role(member_info.username) else {
            return // Err()
        });
        guard!(let Ok(mut discord_member) = serenity::model::id::GuildId(CONFIG.server_id)
            .member(ctx.http.clone(), msg.author.id) else {
            return // Err()
        });

        let roles_to_remove = vec![
            CONFIG.registered_member_role,
            CONFIG.unregistered_member_role,
            CONFIG.expired_member_role];

        for role in roles_to_remove {
            if role == registered_role { // remove when vec.remove_item is stable
                continue
            }
            if discord_member.roles.contains(&RoleId::from(role))
                && discord_member.remove_role(&ctx.http, role).is_err() {
                return // Err()
            }
        }

        if !discord_member.roles.contains(&RoleId::from(registered_role))
            && discord_member.add_role(&ctx.http, registered_role).is_err() {
            return // Err()
        }

        // Ok()
    }

tec's avatar
tec committed
185
186
    pub fn verify(ctx: Context, msg: Message, token: &str) {
        match parse_token(&msg.author, token) {
tec's avatar
tec committed
187
188
189
190
191
192
            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
193
                            let full_member = database::add_member(&msg.author.id.0, &name);
tec's avatar
tec committed
194
195
196
197
                            e!(
                                "Unable to remove role: {:?}",
                                member.remove_role(&ctx.http, CONFIG.unregistered_member_role)
                            );
Ash's avatar
Ash committed
198
199
200
201
                            guard!(let Some(member_role) = Commands::get_registered_role(name) else {
                                send_message!(msg.channel_id, ctx.http.clone(), "Couldn't find you in LDAP!");
                                return
                            });
tec's avatar
tec committed
202
203
                            e!(
                                "Unable to add role: {:?}",
Ash's avatar
Ash committed
204
                                member.add_role(&ctx.http, member_role)
tec's avatar
tec committed
205
                            );
tec's avatar
tec committed
206
207
208
                            e!(
                                "Unable to edit nickname: {:?}",
                                member.edit(&ctx.http, |m| {
tec's avatar
tec committed
209
                                    m.nickname(member_nickname(&full_member));
tec's avatar
tec committed
210
211
212
                                    m
                                })
                            );
tec's avatar
tec committed
213
                            let mut verification_message = MessageBuilder::new();
214
                            verification_message.push(format!("Great, {}! Verification was successful. To provide a friendly introduction to yourself, consider doing ", &full_member.username));
tec's avatar
tec committed
215
216
                            verification_message.push_mono(format!("{}set bio <info>", CONFIG.command_prefix));
                            send_message!(
tec's avatar
tec committed
217
218
                                msg.channel_id,
                                ctx.http.clone(),
tec's avatar
tec committed
219
                                verification_message.build()
tec's avatar
tec committed
220
221
222
223
                            );
                        })
                );
            }
tec's avatar
tec committed
224
225
226
227
228
            Err(reason) => send_message!(
                msg.channel_id,
                &ctx.http,
                format!("Verification error: {:?}", reason)
            ),
tec's avatar
tec committed
229
230
        }
        e!("Error deleting register message: {:?}", msg.delete(&ctx));
tec's avatar
tec committed
231
    }
tec's avatar
tec committed
232
233
    pub fn profile(ctx: Context, msg: Message, name: &str) {
        let possible_member: Option<database::Member> = match if name.trim().is_empty() {
tec's avatar
tec committed
234
235
236
237
            info!(
                "{} (discord name) wants to look at their own profile",
                &msg.author.name
            );
tec's avatar
tec committed
238
239
            database::get_member_info(&msg.author.id.0)
        } else {
tec's avatar
tec committed
240
            info!("Searching for a profile for {}", &name);
tec's avatar
tec committed
241
242
243
244
            database::get_member_info_from_username(&name)
        } {
            Ok(member) => Some(member),
            Err(why) => {
245
                warn!("Could not find member {}, {:?}", &name, why);
tec's avatar
tec committed
246
247
248
                if name.len() != 3 {
                    None
                } else {
tec's avatar
tec committed
249
                    info!(
250
                        "Searching for a profile for the TLA '{}'",
tec's avatar
tec committed
251
252
                        &name.to_uppercase()
                    );
tec's avatar
tec committed
253
                    match database::get_member_info_from_tla(&name.to_uppercase()) {
tec's avatar
tec committed
254
255
256
257
258
259
260
261
262
263
264
265
                        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)"
            );
266
            return;
tec's avatar
tec committed
267
268
        }
        let member = possible_member.unwrap();
269
        info!("Found matching profile, UCC username: {}", &member.username);
tec's avatar
tec committed
270
271
272
273
274
275
276
277
278
279
280
        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()
281
                            .unwrap_or(String::from("https://www.ucc.asn.au/logos/ucc-logo.png")),
tec's avatar
tec committed
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
                    );
                    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);
                }
298
299
300
                if let Some(study) = member.study.clone() {
                    embed.field("Area of study", study, false);
                }
tec's avatar
tec committed
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
                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() {
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
            msg.channel_id
                .send_message(&ctx.http, |m| {
                    m.embed(|embed| {
                        embed.colour(serenity::utils::Colour::LIGHT_GREY);
                        embed.title("Usage");
                        embed.description(
                            format!(
                                "`{}set <field> <info>` or `{}clear <field>`",
                                CONFIG.command_prefix,
                                CONFIG.command_prefix,
                            )
                        );
                        embed.field("Biography", format!("`{}set bio <info>`\nBe friendly! Provide a little introduction to yourself.", CONFIG.command_prefix), false);
                        embed.field("Git", format!("`{}set git <url>`\nA link to your git forge profile. Also takes a github username for convinience", CONFIG.command_prefix), false);
                        embed.field("Photo", format!("`{}set photo <url>`\nPut a face to a name! Provide a profile photo.", CONFIG.command_prefix), false);
                        embed.field("Website", format!("`{}set web <info>`\nGot a personal website? Share it here :)", CONFIG.command_prefix), false);
                        embed.field("Studying", format!("`{}set study <info>`\nYou're (probably) a Uni student, what's your major?", CONFIG.command_prefix), false);
                        embed
                    });
                    m
                })
                .expect("Failed to send usage help embed");
339
            return;
tec's avatar
tec committed
340
341
342
343
        }
        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
344
        if info_content.len() == 1
345
            || !vec!["bio", "git", "web", "photo", "study"].contains(&property.as_str())
tec's avatar
tec committed
346
        {
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
            msg.channel_id
                .send_message(&ctx.http, |m| {
                    m.embed(|embed| {
                        embed.colour(serenity::utils::Colour::LIGHT_GREY);
                        embed.title("Usage");
                        embed.field(
                            match property.as_str() {
                                "bio" => "Biography",
                                "git" => "Git Forge Profile",
                                "photo" => "Profile Photo",
                                "web" => "Personal Website",
                                "study" => "Area of study",
                                _ => "???",
                            },
                            format!(
                                "`{}set {} <info>` or `{}clear {}`\n{}",
                                CONFIG.command_prefix,
                                property,
                                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",
                                    "study" => "Your degree title",
                                    _ => "Whatever you want, because this does absolutely nothing.",
                                }
                            ),
                            false,
                        );
                        embed
                    });
                    m
                })
                .expect("Failed to send usage embed");
383
            return;
tec's avatar
tec committed
384
385
386
387
        }
        let mut value = info_content[1].to_string();

        if vec!["git", "photo", "web"].contains(&property.as_str()) {
Ash's avatar
fixes    
Ash committed
388
            if Url::parse(&value).is_err() {
tec's avatar
tec committed
389
390
391
392
393
                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
394
                if Url::parse(&value).is_err() {
tec's avatar
tec committed
395
396
397
398
399
                    send_message!(
                        msg.channel_id,
                        &ctx.http,
                        "That ain't a URL where I come from..."
                    );
400
                    return;
tec's avatar
tec committed
401
402
403
                }
            }
        }
Ash's avatar
fixes    
Ash committed
404
        guard!(let Ok(member) = database::get_member_info(&msg.author.id.0) else {
tec's avatar
tec committed
405
406
407
408
409
410
411
            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
412
413
414
415
            );
            return
        });
        let set_property = match property.as_str() {
416
417
418
419
420
            "bio" => database::set_member_bio(&msg.author.id.0, Some(&value)),
            "git" => database::set_member_git(&msg.author.id.0, Some(&value)),
            "photo" => database::set_member_photo(&msg.author.id.0, Some(&value)),
            "web" => database::set_member_website(&msg.author.id.0, Some(&value)),
            "study" => database::set_member_study(&msg.author.id.0, Some(&value)),
Ash's avatar
fixes    
Ash committed
421
422
423
            _ => Err(diesel::result::Error::NotFound),
        };
        match set_property {
424
            Ok(_) => {
425
426
427
428
429
430
431
                info!(
                    "Set {}'s {} in profile to {}",
                    &msg.author_nick(ctx.http.clone())
                        .unwrap_or(String::from("?")),
                    property,
                    value
                );
432
433
434
435
436
437
438
439
440
441
442
443
444
445
                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,
                                    Some(
                                        format!(
                                            "https://github.com/{}.png",
                                            path_segments.next().expect("URL doesn't have a path")
                                        )
                                        .as_str(),
                                    ),
Ash's avatar
fixes    
Ash committed
446
                                )
447
                                .expect("Attempt to set member photo failed");
448
                                info!(" ... and set profile photo to github photo")
449
                            }
Ash's avatar
fixes    
Ash committed
450
                        }
451
                        _ => {}
Ash's avatar
fixes    
Ash committed
452
453
454
455
456
457
458
459
460
461
462
463
464
                    }
                }
            }
            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
465
466
        }
    }
467
468
469
470
471
472
    pub fn clear_info(ctx: Context, msg: Message, field: &str) {
        if field.trim().is_empty() {
            // just show the help page from set_info
            Commands::set_info(ctx, msg, "");
            return;
        }
473
        match field {
474
475
476
477
478
479
            "bio" => database::set_member_bio(&msg.author.id.0, None),
            "git" => database::set_member_git(&msg.author.id.0, None),
            "photo" => database::set_member_photo(&msg.author.id.0, None),
            "web" => database::set_member_website(&msg.author.id.0, None),
            "study" => database::set_member_study(&msg.author.id.0, None),
            _ => Err(diesel::result::Error::NotFound),
480
481
        }
        .expect("Unable to clear profile field");
482
483
484
485
486
        info!(
            "Cleared {}'s {} in profile",
            &msg.author_nick(ctx.http).unwrap_or(String::from("?")),
            field,
        );
487
    }
tec's avatar
tec committed
488
}