tools/ubot-plugins/ubot-plugin-janitor/src/main.rs
author unc0rr
Wed, 14 Jul 2021 23:50:44 +0200
changeset 15815 a803bfa3f56c
permissions -rw-r--r--
Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15815
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
     1
use anyhow::Result as AHResult;
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
     2
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
     3
use futures::prelude::*;
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
     4
use lapin::{options::*, types::FieldTable, BasicProperties, Connection, ConnectionProperties};
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
     5
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
     6
use tokio_amqp::*;
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
     7
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
     8
#[tokio::main]
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
     9
async fn main() -> AHResult<()> {
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    10
    let amqp_url = std::env::var("AMQP_URL").expect("expected AMQP_URL env variabe");
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    11
    let conn = Connection::connect(&amqp_url, ConnectionProperties::default().with_tokio()).await?;
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    12
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    13
    let pub_channel = conn.create_channel().await?;
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    14
    let sub_channel = conn.create_channel().await?;
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    15
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    16
    let queue = sub_channel
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    17
        .queue_declare(
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    18
            &"",
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    19
            QueueDeclareOptions {
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    20
                exclusive: true,
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    21
                auto_delete: true,
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    22
                ..QueueDeclareOptions::default()
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    23
            },
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    24
            FieldTable::default(),
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    25
        )
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    26
        .await?;
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    27
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    28
    sub_channel
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    29
        .queue_bind(
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    30
            queue.name().as_str(),
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    31
            "irc",
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    32
            "*.hedgewars",
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    33
            QueueBindOptions::default(),
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    34
            FieldTable::default(),
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    35
        )
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    36
        .await?;
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    37
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    38
    let mut subscriber = sub_channel
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    39
        .basic_consume(
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    40
            queue.name().as_str(),
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    41
            &"",
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    42
            BasicConsumeOptions::default(),
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    43
            FieldTable::default(),
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    44
        )
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    45
        .await?;
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    46
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    47
    let mut last_joined = None;
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    48
    let mut talking_to = None;
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    49
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    50
    while let Some(amqp_message) = subscriber.next().await {
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    51
        let (_, delivery) = amqp_message.expect("error in consumer");
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    52
        delivery.ack(BasicAckOptions::default()).await?;
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    53
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    54
        match delivery.routing_key.as_str() {
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    55
            "msg.hedgewars" => {
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    56
                let chat_message = String::from_utf8_lossy(&delivery.data);
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    57
                if let Some((who, _)) = chat_message.split_once('\n') {
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    58
                    let who = Some(who.to_owned());
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    59
                    if talking_to == who || last_joined == who {
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    60
                        talking_to = who;
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    61
                        pub_channel
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    62
                            .basic_publish(
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    63
                                "irc",
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    64
                                "cmd.say.hedgewars",
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    65
                                BasicPublishOptions::default(),
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    66
                                vec![],
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    67
                                BasicProperties::default(),
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    68
                            )
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    69
                            .await?;
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    70
                    } else {
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    71
                        last_joined = None;
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    72
                        talking_to = None;
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    73
                    }
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    74
                }
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    75
            }
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    76
            "join.hedgewars" => {
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    77
                last_joined = Some(String::from_utf8_lossy(&delivery.data).to_string());
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    78
            }
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    79
            _ => (),
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    80
        }
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    81
    }
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    82
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    83
    Ok(())
a803bfa3f56c Add janitor plugin that would suggest mingpt plugin to reply to newcomers messages
unc0rr
parents:
diff changeset
    84
}