tools/ubot/src/main.rs
author unC0Rr
Wed, 18 Sep 2024 13:42:26 +0200
branchtransitional_engine
changeset 16035 0caa3dfb3ba2
parent 15814 d9db7b763bd1
permissions -rw-r--r--
Add templates for different maze styles
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15789
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
     1
use lapin::{
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
     2
    message::Delivery, options::*, types::FieldTable, BasicProperties, Connection,
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
     3
    ConnectionProperties,
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
     4
};
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
     5
use tokio_amqp::*;
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
     6
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
     7
use futures::prelude::*;
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
     8
use irc::client::prelude::*;
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
     9
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
    10
use anyhow::{bail, Result as AHResult};
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
    11
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
    12
use url::Url;
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
    13
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
    14
use rand::distributions::Alphanumeric;
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
    15
use rand::{thread_rng, Rng};
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
    16
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
    17
fn url2irc_config(url: &str) -> AHResult<Config> {
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
    18
    let url = Url::parse(url)?;
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
    19
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
    20
    if url.scheme() != "irc" {
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
    21
        bail!("Expected 'irc' scheme")
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
    22
    }
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
    23
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
    24
    Ok(Config {
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
    25
        nickname: Some(url.username().to_owned()),
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
    26
        nick_password: url.password().map(|s| s.to_owned()),
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
    27
        server: url.host_str().map(|s| s.to_owned()),
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
    28
        port: url.port(),
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
    29
        channels: vec![format!("#{}", &url.path()[1..])],
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
    30
        //use_mock_connection: true,
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
    31
        ..Config::default()
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
    32
    })
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
    33
}
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
    34
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
    35
fn random_string(size: usize) -> String {
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
    36
    thread_rng()
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
    37
        .sample_iter(&Alphanumeric)
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
    38
        .take(size)
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
    39
        .map(char::from)
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
    40
        .collect()
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
    41
}
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
    42
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
    43
async fn handle_irc(pub_channel: &lapin::Channel, irc_message: &Message) -> AHResult<()> {
15814
d9db7b763bd1 Publish join and part commands as well
unc0rr
parents: 15792
diff changeset
    44
    match &irc_message.command {
d9db7b763bd1 Publish join and part commands as well
unc0rr
parents: 15792
diff changeset
    45
        Command::PRIVMSG(msgtarget, message) => {
d9db7b763bd1 Publish join and part commands as well
unc0rr
parents: 15792
diff changeset
    46
            let target = irc_message
d9db7b763bd1 Publish join and part commands as well
unc0rr
parents: 15792
diff changeset
    47
                .response_target()
d9db7b763bd1 Publish join and part commands as well
unc0rr
parents: 15792
diff changeset
    48
                .expect("Really expected PRIVMSG would have a source");
d9db7b763bd1 Publish join and part commands as well
unc0rr
parents: 15792
diff changeset
    49
            let target = if target.starts_with('#') {
d9db7b763bd1 Publish join and part commands as well
unc0rr
parents: 15792
diff changeset
    50
                &target[1..]
d9db7b763bd1 Publish join and part commands as well
unc0rr
parents: 15792
diff changeset
    51
            } else {
d9db7b763bd1 Publish join and part commands as well
unc0rr
parents: 15792
diff changeset
    52
                &target
d9db7b763bd1 Publish join and part commands as well
unc0rr
parents: 15792
diff changeset
    53
            };
15789
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
    54
15814
d9db7b763bd1 Publish join and part commands as well
unc0rr
parents: 15792
diff changeset
    55
            let who = irc_message.source_nickname().unwrap_or(msgtarget);
15792
191e51179d1b Fix detection of message author in ubot core
unc0rr
parents: 15789
diff changeset
    56
15814
d9db7b763bd1 Publish join and part commands as well
unc0rr
parents: 15792
diff changeset
    57
            if message.starts_with("!") {
d9db7b763bd1 Publish join and part commands as well
unc0rr
parents: 15792
diff changeset
    58
                if let Some((cmd, param)) = message.split_once(' ') {
d9db7b763bd1 Publish join and part commands as well
unc0rr
parents: 15792
diff changeset
    59
                    pub_channel
d9db7b763bd1 Publish join and part commands as well
unc0rr
parents: 15792
diff changeset
    60
                        .basic_publish(
d9db7b763bd1 Publish join and part commands as well
unc0rr
parents: 15792
diff changeset
    61
                            "irc",
d9db7b763bd1 Publish join and part commands as well
unc0rr
parents: 15792
diff changeset
    62
                            &format!("cmd.{}.{}", &cmd[1..], target),
d9db7b763bd1 Publish join and part commands as well
unc0rr
parents: 15792
diff changeset
    63
                            BasicPublishOptions::default(),
d9db7b763bd1 Publish join and part commands as well
unc0rr
parents: 15792
diff changeset
    64
                            format!("{}\n{}", who, param).as_bytes().to_vec(),
d9db7b763bd1 Publish join and part commands as well
unc0rr
parents: 15792
diff changeset
    65
                            BasicProperties::default(),
d9db7b763bd1 Publish join and part commands as well
unc0rr
parents: 15792
diff changeset
    66
                        )
d9db7b763bd1 Publish join and part commands as well
unc0rr
parents: 15792
diff changeset
    67
                        .await?;
d9db7b763bd1 Publish join and part commands as well
unc0rr
parents: 15792
diff changeset
    68
                } else {
d9db7b763bd1 Publish join and part commands as well
unc0rr
parents: 15792
diff changeset
    69
                    pub_channel
d9db7b763bd1 Publish join and part commands as well
unc0rr
parents: 15792
diff changeset
    70
                        .basic_publish(
d9db7b763bd1 Publish join and part commands as well
unc0rr
parents: 15792
diff changeset
    71
                            "irc",
d9db7b763bd1 Publish join and part commands as well
unc0rr
parents: 15792
diff changeset
    72
                            &format!("cmd.{}.{}", &message[1..], target),
d9db7b763bd1 Publish join and part commands as well
unc0rr
parents: 15792
diff changeset
    73
                            BasicPublishOptions::default(),
d9db7b763bd1 Publish join and part commands as well
unc0rr
parents: 15792
diff changeset
    74
                            who.as_bytes().to_vec(),
d9db7b763bd1 Publish join and part commands as well
unc0rr
parents: 15792
diff changeset
    75
                            BasicProperties::default(),
d9db7b763bd1 Publish join and part commands as well
unc0rr
parents: 15792
diff changeset
    76
                        )
d9db7b763bd1 Publish join and part commands as well
unc0rr
parents: 15792
diff changeset
    77
                        .await?;
d9db7b763bd1 Publish join and part commands as well
unc0rr
parents: 15792
diff changeset
    78
                }
15789
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
    79
            } else {
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
    80
                pub_channel
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
    81
                    .basic_publish(
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
    82
                        "irc",
15814
d9db7b763bd1 Publish join and part commands as well
unc0rr
parents: 15792
diff changeset
    83
                        &format!("msg.{}", target),
15789
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
    84
                        BasicPublishOptions::default(),
15814
d9db7b763bd1 Publish join and part commands as well
unc0rr
parents: 15792
diff changeset
    85
                        format!("{}\n{}", who, message).as_bytes().to_vec(),
15789
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
    86
                        BasicProperties::default(),
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
    87
                    )
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
    88
                    .await?;
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
    89
            }
15814
d9db7b763bd1 Publish join and part commands as well
unc0rr
parents: 15792
diff changeset
    90
        }
d9db7b763bd1 Publish join and part commands as well
unc0rr
parents: 15792
diff changeset
    91
        Command::JOIN(channel, _, _) => {
15789
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
    92
            pub_channel
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
    93
                .basic_publish(
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
    94
                    "irc",
15814
d9db7b763bd1 Publish join and part commands as well
unc0rr
parents: 15792
diff changeset
    95
                    &format!("join.{}", &channel[1..]),
15789
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
    96
                    BasicPublishOptions::default(),
15814
d9db7b763bd1 Publish join and part commands as well
unc0rr
parents: 15792
diff changeset
    97
                    irc_message
d9db7b763bd1 Publish join and part commands as well
unc0rr
parents: 15792
diff changeset
    98
                        .source_nickname()
d9db7b763bd1 Publish join and part commands as well
unc0rr
parents: 15792
diff changeset
    99
                        .expect("Hey, who joined?")
d9db7b763bd1 Publish join and part commands as well
unc0rr
parents: 15792
diff changeset
   100
                        .as_bytes()
d9db7b763bd1 Publish join and part commands as well
unc0rr
parents: 15792
diff changeset
   101
                        .to_vec(),
15789
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   102
                    BasicProperties::default(),
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   103
                )
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   104
                .await?;
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   105
        }
15814
d9db7b763bd1 Publish join and part commands as well
unc0rr
parents: 15792
diff changeset
   106
        Command::PART(channel, _) => {
d9db7b763bd1 Publish join and part commands as well
unc0rr
parents: 15792
diff changeset
   107
            pub_channel
d9db7b763bd1 Publish join and part commands as well
unc0rr
parents: 15792
diff changeset
   108
                .basic_publish(
d9db7b763bd1 Publish join and part commands as well
unc0rr
parents: 15792
diff changeset
   109
                    "irc",
d9db7b763bd1 Publish join and part commands as well
unc0rr
parents: 15792
diff changeset
   110
                    &format!("part.{}", &channel[1..]),
d9db7b763bd1 Publish join and part commands as well
unc0rr
parents: 15792
diff changeset
   111
                    BasicPublishOptions::default(),
d9db7b763bd1 Publish join and part commands as well
unc0rr
parents: 15792
diff changeset
   112
                    irc_message
d9db7b763bd1 Publish join and part commands as well
unc0rr
parents: 15792
diff changeset
   113
                        .source_nickname()
d9db7b763bd1 Publish join and part commands as well
unc0rr
parents: 15792
diff changeset
   114
                        .expect("Hey, who left?")
d9db7b763bd1 Publish join and part commands as well
unc0rr
parents: 15792
diff changeset
   115
                        .as_bytes()
d9db7b763bd1 Publish join and part commands as well
unc0rr
parents: 15792
diff changeset
   116
                        .to_vec(),
d9db7b763bd1 Publish join and part commands as well
unc0rr
parents: 15792
diff changeset
   117
                    BasicProperties::default(),
d9db7b763bd1 Publish join and part commands as well
unc0rr
parents: 15792
diff changeset
   118
                )
d9db7b763bd1 Publish join and part commands as well
unc0rr
parents: 15792
diff changeset
   119
                .await?;
d9db7b763bd1 Publish join and part commands as well
unc0rr
parents: 15792
diff changeset
   120
        }
d9db7b763bd1 Publish join and part commands as well
unc0rr
parents: 15792
diff changeset
   121
        _ => (),
15789
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   122
    }
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   123
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   124
    Ok(())
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   125
}
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   126
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   127
async fn handle_amqp(
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   128
    irc_client: &mut Client,
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   129
    irc_channel: &str,
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   130
    delivery: Delivery,
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   131
) -> AHResult<()> {
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   132
    let message = String::from_utf8(delivery.data)?;
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   133
    Ok(irc_client.send_privmsg(irc_channel, message)?)
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   134
}
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   135
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   136
#[tokio::main]
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   137
async fn main() -> AHResult<()> {
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   138
    let amqp_url = std::env::var("AMQP_URL").expect("expected AMQP_URL env variabe");
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   139
    let irc_url = std::env::var("IRC_URL").expect("expected IRC_URL env variabe");
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   140
    let conn = Connection::connect(&amqp_url, ConnectionProperties::default().with_tokio()).await?;
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   141
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   142
    let pub_channel = conn.create_channel().await?;
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   143
    let sub_channel = conn.create_channel().await?;
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   144
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   145
    let irc_config = url2irc_config(&irc_url)?;
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   146
    let irc_channel = irc_config.channels[0].to_owned();
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   147
    let mut irc_client = Client::from_config(irc_config).await?;
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   148
    let mut irc_stream = irc_client.stream()?;
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   149
    irc_client.identify()?;
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   150
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   151
    let queue = sub_channel
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   152
        .queue_declare(
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   153
            &random_string(32),
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   154
            QueueDeclareOptions {
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   155
                exclusive: true,
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   156
                auto_delete: true,
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   157
                ..QueueDeclareOptions::default()
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   158
            },
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   159
            FieldTable::default(),
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   160
        )
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   161
        .await?;
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   162
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   163
    sub_channel
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   164
        .queue_bind(
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   165
            queue.name().as_str(),
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   166
            "irc",
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   167
            &format!("say.{}", &irc_channel[1..]),
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   168
            QueueBindOptions::default(),
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   169
            FieldTable::default(),
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   170
        )
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   171
        .await?;
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   172
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   173
    let mut subscriber = sub_channel
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   174
        .basic_consume(
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   175
            queue.name().as_str(),
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   176
            &random_string(32),
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   177
            BasicConsumeOptions::default(),
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   178
            FieldTable::default(),
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   179
        )
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   180
        .await?;
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   181
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   182
    loop {
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   183
        tokio::select! {
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   184
            Some(irc_message) = irc_stream.next() => handle_irc(&pub_channel, &irc_message?).await?,
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   185
            Some(amqp_message) = subscriber.next() => {
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   186
                let (_, delivery) = amqp_message.expect("error in consumer");
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   187
                delivery
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   188
                    .ack(BasicAckOptions::default())
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   189
                    .await?;
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   190
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   191
                    handle_amqp(&mut irc_client, &irc_channel, delivery).await?
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   192
            }
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   193
        }
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   194
    }
d97ea528ce95 Add main module for irc bot which serves as an irc<>rabbitmq relay
unc0rr
parents:
diff changeset
   195
}