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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
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
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
#![deny(
    unused_qualifications,
    unused_qualifications,
    unused_import_braces,
    unused_lifetimes,
    unreachable_pub,
    trivial_numeric_casts,
    missing_debug_implementations,
    missing_copy_implementations,
    deprecated_in_future,
    meta_variable_misuse,
    non_ascii_idents,
    rust_2018_compatibility,
    rust_2018_idioms,
    future_incompatible,
    nonstandard_style,
    clippy::all
)]
#![warn(variant_size_differences, let_underscore_drop)]
// TODO: Document everything properly
// clippy::default_trait_access
// clippy::use_self
// clippy::undocumented_unsafe_blocks
// clippy::allow_attributes_without_reason
// clippy::as_underscore
// clippy::cast_possible_truncation
// clippy::cast_possible_wrap
// clippy::cast_sign_loss
// clippy::fn_to_numeric_cast_any
// clippy::redundant_closure_for_method_calls
// clippy::too_many_lines

//! # Pikadick

pub mod checks;
pub mod cli_options;
pub mod client_data;
pub mod commands;
pub mod config;
pub mod database;
pub mod logger;
pub mod setup;
pub mod util;

use crate::{
    cli_options::CliOptions,
    client_data::ClientData,
    commands::*,
    config::{
        ActivityKind,
        Config,
    },
    database::{
        model::TikTokEmbedFlags,
        Database,
    },
    util::LoadingReaction,
};
use anyhow::{
    bail,
    ensure,
    Context as _,
};
use pikadick_util::AsyncLockFile;
use serenity::{
    framework::standard::{
        buckets::BucketBuilder,
        help_commands,
        macros::{
            group,
            help,
        },
        Args,
        CommandGroup,
        CommandResult,
        Configuration as StandardFrameworkConfiguration,
        DispatchError,
        HelpOptions,
        Reason,
        StandardFramework,
    },
    futures::future::BoxFuture,
    gateway::{
        ActivityData,
        ShardManager,
    },
    model::{
        application::Interaction,
        prelude::*,
    },
    prelude::*,
    FutureExt,
};
use songbird::SerenityInit;
use std::{
    collections::HashSet,
    sync::Arc,
    time::{
        Duration,
        Instant,
    },
};
use tokio::runtime::Builder as RuntimeBuilder;
use tracing::{
    error,
    info,
    warn,
};
use tracing_appender::non_blocking::WorkerGuard;
use url::Url;

const TOKIO_RT_SHUTDOWN_TIMEOUT: Duration = Duration::from_secs(10);

struct Handler;

#[serenity::async_trait]
impl EventHandler for Handler {
    async fn ready(&self, ctx: Context, ready: Ready) {
        let data_lock = ctx.data.read().await;
        let client_data = data_lock
            .get::<ClientDataKey>()
            .expect("missing client data");
        let slash_framework = data_lock
            .get::<SlashFrameworkKey>()
            .expect("missing slash framework")
            .clone();
        let config = client_data.config.clone();
        drop(data_lock);

        if let (Some(status), Some(kind)) = (config.status_name(), config.status_type()) {
            match kind {
                ActivityKind::Listening => {
                    ctx.set_activity(Some(ActivityData::listening(status)));
                }
                ActivityKind::Streaming => {
                    let result: Result<_, anyhow::Error> = async {
                        let activity = ActivityData::streaming(
                            status,
                            config.status_url().context("failed to get status url")?,
                        )?;

                        ctx.set_activity(Some(activity));

                        Ok(())
                    }
                    .await;

                    if let Err(error) = result.context("failed to set activity") {
                        error!("{error:?}");
                    }
                }
                ActivityKind::Playing => {
                    ctx.set_activity(Some(ActivityData::playing(status)));
                }
            }
        }

        info!("logged in as \"{}\"", ready.user.name);

        // TODO: Consider shutting down the bot. It might be possible to use old data though.
        if let Err(error) = slash_framework
            .register(ctx.clone(), config.test_guild)
            .await
            .context("failed to register slash commands")
        {
            error!("{error:?}");
        }

        info!("registered slash commands");
    }

    async fn resume(&self, _ctx: Context, _resumed: ResumedEvent) {
        warn!("resumed connection");
    }

    #[tracing::instrument(skip(self, ctx, msg), fields(author = %msg.author.id, guild = ?msg.guild_id, content = %msg.content))]
    async fn message(&self, ctx: Context, msg: Message) {
        let data_lock = ctx.data.read().await;
        let client_data = data_lock
            .get::<ClientDataKey>()
            .expect("missing client data");
        let reddit_embed_data = client_data.reddit_embed_data.clone();
        let tiktok_data = client_data.tiktok_data.clone();
        let db = client_data.db.clone();
        drop(data_lock);

        // Process URL Embeds
        {
            // Only embed guild links
            let guild_id = match msg.guild_id {
                Some(id) => id,
                None => {
                    return;
                }
            };

            // No Bots
            if msg.author.bot {
                return;
            }

            // Get enabled data for embeds
            let reddit_embed_is_enabled_for_guild = db
                .get_reddit_embed_enabled(guild_id)
                .await
                .with_context(|| format!("failed to get reddit-embed server data for {guild_id}"))
                .unwrap_or_else(|error| {
                    error!("{error:?}");
                    false
                });
            let tiktok_embed_flags = db
                .get_tiktok_embed_flags(guild_id)
                .await
                .with_context(|| format!("failed to get tiktok-embed server data for {guild_id}"))
                .unwrap_or_else(|error| {
                    error!("{error:?}");
                    TikTokEmbedFlags::empty()
                });

            // Extract urls.
            // We collect into a `Vec` as the regex iterator is not Sync and cannot be held across await points.
            let urls: Vec<Url> = crate::util::extract_urls(&msg.content).collect();

            // Check to see if it we will even try to embed
            let will_try_embedding = urls.iter().any(|url| {
                let url_host = match url.host() {
                    Some(host) => host,
                    None => return false,
                };

                let reddit_url =
                    matches!(url_host, url::Host::Domain("www.reddit.com" | "reddit.com"));

                let tiktok_url = matches!(
                    url_host,
                    url::Host::Domain("vm.tiktok.com" | "tiktok.com" | "www.tiktok.com")
                );

                (reddit_url && reddit_embed_is_enabled_for_guild)
                    || (tiktok_url && tiktok_embed_flags.contains(TikTokEmbedFlags::ENABLED))
            });

            // Return if we won't try embedding
            if !will_try_embedding {
                return;
            }

            let mut loading_reaction = Some(LoadingReaction::new(ctx.http.clone(), &msg));

            // Embed for each url
            // NOTE: we short circuit on failure since sending a msg to a channel and failing is most likely a permissions problem,
            // especially since serenity retries each req once
            for url in urls.iter() {
                match url.host() {
                    Some(url::Host::Domain("www.reddit.com" | "reddit.com")) => {
                        // Don't process if it isn't enabled
                        if reddit_embed_is_enabled_for_guild {
                            if let Err(error) = reddit_embed_data
                                .try_embed_url(&ctx, &msg, url, &mut loading_reaction)
                                .await
                                .context("failed to generate reddit embed")
                            {
                                error!("{error:?}");
                            }
                        }
                    }
                    Some(url::Host::Domain("vm.tiktok.com" | "tiktok.com" | "www.tiktok.com")) => {
                        if tiktok_embed_flags.contains(TikTokEmbedFlags::ENABLED) {
                            if let Err(error) = tiktok_data
                                .try_embed_url(
                                    &ctx,
                                    &msg,
                                    url,
                                    &mut loading_reaction,
                                    tiktok_embed_flags.contains(TikTokEmbedFlags::DELETE_LINK),
                                )
                                .await
                                .context("failed to generate tiktok embed")
                            {
                                error!("{error:?}");
                            }
                        }
                    }
                    _ => {}
                }
            }

            // Trim caches
            reddit_embed_data.cache.trim();
            reddit_embed_data.video_data_cache.trim();
            tiktok_data.post_page_cache.trim();
        }
    }

    async fn interaction_create(&self, ctx: Context, interaction: Interaction) {
        let data_lock = ctx.data.read().await;
        let framework = data_lock
            .get::<SlashFrameworkKey>()
            .expect("missing slash framework")
            .clone();
        drop(data_lock);

        framework.process_interaction_create(ctx, interaction).await;
    }
}

#[derive(Debug, Clone, Copy)]
pub struct ClientDataKey;

impl TypeMapKey for ClientDataKey {
    type Value = ClientData;
}

#[derive(Debug, Clone, Copy)]
pub struct SlashFrameworkKey;

impl TypeMapKey for SlashFrameworkKey {
    type Value = pikadick_slash_framework::Framework;
}

#[help]
async fn help(
    ctx: &Context,
    msg: &Message,
    args: Args,
    help_options: &'static HelpOptions,
    groups: &[&'static CommandGroup],
    owners: HashSet<UserId>,
) -> CommandResult {
    match help_commands::with_embeds(ctx, msg, args, help_options, groups, owners)
        .await
        .context("failed to send help")
    {
        Ok(_) => {}
        Err(error) => {
            error!("{error:?}");
        }
    }
    Ok(())
}

#[group]
#[commands(
    system,
    quizizz,
    fml,
    zalgo,
    shift,
    reddit_embed,
    invite,
    vaporwave,
    cmd,
    latency,
    uwuify,
    cache_stats,
    insta_dl,
    deviantart,
    urban,
    xkcd,
    tic_tac_toe,
    iqdb,
    reddit,
    leave,
    stop,
    sauce_nao
)]
struct General;

async fn handle_ctrl_c(shard_manager: Arc<ShardManager>) {
    match tokio::signal::ctrl_c()
        .await
        .context("failed to set ctrl-c handler")
    {
        Ok(_) => {
            info!("shutting down...");
            info!("stopping client...");
            shard_manager.shutdown_all().await;
        }
        Err(error) => {
            warn!("{error}");
            // The default "kill everything" handler is probably still installed, so this isn't a problem?
        }
    };
}

#[tracing::instrument(skip(_ctx, msg), fields(author = %msg.author.id, guild = ?msg.guild_id, content = %msg.content))]
fn before_handler<'fut>(
    _ctx: &'fut Context,
    msg: &'fut Message,
    cmd_name: &'fut str,
) -> BoxFuture<'fut, bool> {
    info!("allowing command to process");
    async move { true }.boxed()
}

fn after_handler<'fut>(
    _ctx: &'fut Context,
    _msg: &'fut Message,
    command_name: &'fut str,
    command_result: CommandResult,
) -> BoxFuture<'fut, ()> {
    async move {
        if let Err(error) = command_result {
            error!("failed to process command \"{command_name}\": {error}");
        }
    }
    .boxed()
}

fn unrecognised_command_handler<'fut>(
    ctx: &'fut Context,
    msg: &'fut Message,
    command_name: &'fut str,
) -> BoxFuture<'fut, ()> {
    async move {
        error!("unrecognized command \"{command_name}\"");

        let _ = msg
            .channel_id
            .say(
                &ctx.http,
                format!("Could not find command \"{command_name}\""),
            )
            .await
            .is_ok();
    }
    .boxed()
}

fn process_dispatch_error<'fut>(
    ctx: &'fut Context,
    msg: &'fut Message,
    error: DispatchError,
    cmd_name: &'fut str,
) -> BoxFuture<'fut, ()> {
    process_dispatch_error_future(ctx, msg, error, cmd_name).boxed()
}

async fn process_dispatch_error_future<'fut>(
    ctx: &'fut Context,
    msg: &'fut Message,
    error: DispatchError,
    _cmd_name: &'fut str,
) {
    match error {
        DispatchError::Ratelimited(duration) => {
            let seconds = duration.as_secs();
            let _ = msg
                .channel_id
                .say(
                    &ctx.http,
                    format!("Wait {seconds} seconds to use that command again"),
                )
                .await
                .is_ok();
        }
        DispatchError::NotEnoughArguments { min, given } => {
            let _ = msg
                .channel_id
                .say(
                    &ctx.http,
                    format!(
                        "Expected at least {min} argument(s) for this command, but only got {given}",
                    ),
                )
                .await
                .is_ok();
        }
        DispatchError::TooManyArguments { max, given } => {
            let response_str = format!("Expected no more than {max} argument(s) for this command, but got {given}. Try using quotation marks if your argument has spaces.");
            let _ = msg.channel_id.say(&ctx.http, response_str).await.is_ok();
        }
        DispatchError::CheckFailed(check_name, reason) => match reason {
            Reason::User(user_reason_str) => {
                let _ = msg.channel_id.say(&ctx.http, user_reason_str).await.is_ok();
            }
            _ => {
                let _ = msg
                    .channel_id
                    .say(
                        &ctx.http,
                        format!("\"{check_name}\" check failed: {reason:#?}"),
                    )
                    .await
                    .is_ok();
            }
        },
        error => {
            let _ = msg
                .channel_id
                .say(&ctx.http, format!("Unhandled Dispatch Error: {error:?}"))
                .await
                .is_ok();
        }
    };
}

/// Set up a serenity client
async fn setup_client(config: Arc<Config>) -> anyhow::Result<Client> {
    // Setup slash framework
    let slash_framework = pikadick_slash_framework::FrameworkBuilder::new()
        .check(self::checks::enabled::create_slash_check)
        .help_command(create_slash_help_command()?)
        .command(self::commands::nekos::create_slash_command()?)
        .command(self::commands::ping::create_slash_command()?)
        .command(self::commands::r6stats::create_slash_command()?)
        .command(self::commands::r6tracker::create_slash_command()?)
        .command(self::commands::rule34::create_slash_command()?)
        .command(self::commands::tiktok_embed::create_slash_command()?)
        .command(self::commands::chat::create_slash_command()?)
        .command(self::commands::yodaspeak::create_slash_command()?)
        .build()?;

    // Create second prefix that is uppercase so we are case-insensitive
    let config_prefix = config.prefix.clone();
    let uppercase_prefix = config_prefix.to_uppercase();

    // Build the standard framework
    info!("using prefix \"{config_prefix}\"");
    let framework_config = StandardFrameworkConfiguration::new()
        .prefixes([config_prefix, uppercase_prefix])
        .case_insensitivity(true);
    let framework = StandardFramework::new();
    framework.configure(framework_config);
    let framework = framework
        .help(&HELP)
        .group(&GENERAL_GROUP)
        .bucket("r6stats", BucketBuilder::new_channel().delay(7))
        .await
        .bucket("r6tracker", BucketBuilder::new_channel().delay(7))
        .await
        .bucket("system", BucketBuilder::new_channel().delay(30))
        .await
        .bucket("quizizz", BucketBuilder::new_channel().delay(10))
        .await
        .bucket("insta-dl", BucketBuilder::new_channel().delay(10))
        .await
        .bucket("ttt-board", BucketBuilder::new_channel().delay(1))
        .await
        .bucket("default", BucketBuilder::new_channel().delay(1))
        .await
        .before(before_handler)
        .after(after_handler)
        .unrecognised_command(unrecognised_command_handler)
        .on_dispatch_error(process_dispatch_error);

    // Build the client
    let config_token = config.token.clone();
    let client = Client::builder(
        config_token,
        GatewayIntents::non_privileged() | GatewayIntents::MESSAGE_CONTENT,
    )
    .event_handler(Handler)
    .application_id(ApplicationId::new(config.application_id))
    .framework(framework)
    .register_songbird()
    .await
    .context("failed to create client")?;

    {
        client
            .data
            .write()
            .await
            .insert::<SlashFrameworkKey>(slash_framework);
    }

    // TODO: Spawn a task for this earlier?
    // Spawn the ctrl-c handler
    tokio::spawn(handle_ctrl_c(client.shard_manager.clone()));

    Ok(client)
}

/// Data from the setup function
struct SetupData {
    tokio_rt: tokio::runtime::Runtime,
    config: Arc<Config>,
    database: Database,
    lock_file: AsyncLockFile,
    worker_guard: WorkerGuard,
}

/// Pre-main setup
fn setup(cli_options: CliOptions) -> anyhow::Result<SetupData> {
    eprintln!("starting tokio runtime...");
    let tokio_rt = RuntimeBuilder::new_multi_thread()
        .enable_all()
        .thread_name("pikadick-tokio-worker")
        .build()
        .context("failed to start tokio runtime")?;

    let config = crate::setup::load_config(&cli_options.config)
        .map(Arc::new)
        .context("failed to load config")?;

    eprintln!("opening data directory...");
    let data_dir_metadata = match std::fs::metadata(&config.data_dir) {
        Ok(metadata) => Some(metadata),
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => None,
        Err(e) => {
            return Err(e).context("failed to get metadata for the data dir");
        }
    };

    let _missing_data_dir = data_dir_metadata.is_none();
    match data_dir_metadata.as_ref() {
        Some(metadata) => {
            if metadata.is_dir() {
                eprintln!("data directory already exists.");
            } else if metadata.is_file() {
                bail!("failed to create or open data directory, the path is a file");
            }
        }
        None => {
            eprintln!("data directory does not exist. creating...");
            std::fs::create_dir_all(&config.data_dir).context("failed to create data directory")?;
        }
    }

    eprintln!("creating lockfile...");
    let lock_file_path = config.data_dir.join("pikadick.lock");
    let lock_file = AsyncLockFile::blocking_open(lock_file_path.as_std_path())
        .context("failed to open lockfile")?;
    let lock_file_locked = lock_file
        .try_lock_with_pid_blocking()
        .context("failed to try to lock the lockfile")?;
    ensure!(lock_file_locked, "another process has locked the lockfile");

    std::fs::create_dir_all(config.log_file_dir()).context("failed to create log file dir")?;
    std::fs::create_dir_all(config.cache_dir()).context("failed to create cache dir")?;

    // TODO: Init db
    eprintln!("opening database...");
    let database_path = config.data_dir.join("pikadick.sqlite");

    // Safety: This is called before any other sqlite functions.
    // TODO: Is there a good reason to not remake the db if it is missing?
    let database = unsafe {
        Database::blocking_new(database_path, true) // missing_data_dir
            .context("failed to open database")?
    };

    // Everything past here is assumed to need tokio
    let _enter_guard = tokio_rt.handle().enter();

    eprintln!("setting up logger...");
    let worker_guard = crate::logger::setup(&config).context("failed to initialize logger")?;

    eprintln!();
    Ok(SetupData {
        tokio_rt,
        config,
        database,
        lock_file,
        worker_guard,
    })
}

/// The main entry.
///
/// Sets up the program and calls `real_main`.
/// This allows more things to drop correctly.
/// This also calls setup operations like loading config and setting up the tokio runtime,
/// logging errors to the stderr instead of the loggers, which are not initialized yet.
fn main() -> anyhow::Result<()> {
    // This line MUST run first.
    // It is needed to exit early if the options are invalid,
    // and this will NOT run destructors if it does so.
    let cli_options = argh::from_env();

    let setup_data = setup(cli_options)?;
    real_main(setup_data)?;
    Ok(())
}

/// The actual entry point
fn real_main(setup_data: SetupData) -> anyhow::Result<()> {
    // We spawn this is a seperate thread/task as the main thread does not have enough stack space
    let _enter_guard = setup_data.tokio_rt.enter();
    let ret = setup_data.tokio_rt.block_on(tokio::spawn(async_main(
        setup_data.config,
        setup_data.database,
    )));

    let shutdown_start = Instant::now();
    info!(
        "shutting down tokio runtime (shutdown timeout is {:?})...",
        TOKIO_RT_SHUTDOWN_TIMEOUT
    );
    setup_data
        .tokio_rt
        .shutdown_timeout(TOKIO_RT_SHUTDOWN_TIMEOUT);
    info!("shutdown tokio runtime in {:?}", shutdown_start.elapsed());

    info!("unlocking lockfile...");
    setup_data
        .lock_file
        .blocking_unlock()
        .context("failed to unlock lockfile")?;

    info!("successful shutdown");

    // Logging no longer reliable past this point
    drop(setup_data.worker_guard);

    ret?
}

/// The async entry
async fn async_main(config: Arc<Config>, database: Database) -> anyhow::Result<()> {
    // TODO: See if it is possible to start serenity without a network
    info!("setting up client...");
    let mut client = setup_client(config.clone())
        .await
        .context("failed to set up client")?;

    let client_data = ClientData::init(client.shard_manager.clone(), config, database.clone())
        .await
        .context("client data initialization failed")?;

    // Add all post-init client data changes here
    {
        client_data.enabled_check_data.add_groups(&[&GENERAL_GROUP]);
    }

    {
        let mut data = client.data.write().await;
        data.insert::<ClientDataKey>(client_data);
    }

    info!("logging in...");
    client.start().await.context("failed to run client")?;
    let client_data = {
        let mut data = client.data.write().await;
        data.remove::<ClientDataKey>().expect("missing client data")
    };
    drop(client);

    info!("running shutdown routine for client data");
    client_data.shutdown().await;
    drop(client_data);

    info!("closing database...");
    database.close().await.context("failed to close database")?;

    Ok(())
}