Create basic IRC loop

This commit is contained in:
Yash Karandikar 2021-12-27 11:24:31 -06:00
parent 81c2129a21
commit 6f195f7611
Signed by: karx
GPG key ID: A794DA2529474BA5
3 changed files with 201 additions and 15 deletions

142
Cargo.lock generated
View file

@ -172,10 +172,75 @@ name = "dircord"
version = "0.1.0"
dependencies = [
"anyhow",
"irc",
"serenity",
"tokio",
]
[[package]]
name = "encoding"
version = "0.2.33"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6b0d943856b990d12d3b55b359144ff341533e516d94098b1d3fc1ac666d36ec"
dependencies = [
"encoding-index-japanese",
"encoding-index-korean",
"encoding-index-simpchinese",
"encoding-index-singlebyte",
"encoding-index-tradchinese",
]
[[package]]
name = "encoding-index-japanese"
version = "1.20141219.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "04e8b2ff42e9a05335dbf8b5c6f7567e5591d0d916ccef4e0b1710d32a0d0c91"
dependencies = [
"encoding_index_tests",
]
[[package]]
name = "encoding-index-korean"
version = "1.20141219.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4dc33fb8e6bcba213fe2f14275f0963fd16f0a02c878e3095ecfdf5bee529d81"
dependencies = [
"encoding_index_tests",
]
[[package]]
name = "encoding-index-simpchinese"
version = "1.20141219.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d87a7194909b9118fc707194baa434a4e3b0fb6a5a757c73c3adb07aa25031f7"
dependencies = [
"encoding_index_tests",
]
[[package]]
name = "encoding-index-singlebyte"
version = "1.20141219.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3351d5acffb224af9ca265f435b859c7c01537c0849754d3db3fdf2bfe2ae84a"
dependencies = [
"encoding_index_tests",
]
[[package]]
name = "encoding-index-tradchinese"
version = "1.20141219.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd0e20d5688ce3cab59eb3ef3a2083a5c77bf496cb798dc6fcdb75f323890c18"
dependencies = [
"encoding_index_tests",
]
[[package]]
name = "encoding_index_tests"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a246d82be1c9d791c5dfde9a2bd045fc3cbba3fa2b11ad558f27d01712f00569"
[[package]]
name = "encoding_rs"
version = "0.8.30"
@ -475,6 +540,43 @@ version = "2.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "68f2d64f2edebec4ce84ad108148e67e1064789bee435edc5b60ad398714a3a9"
[[package]]
name = "irc"
version = "0.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c5510c4c4631e53c57d6b05c44ab8447d1db6beef28fb9d12c4d6a46fad9dfcc"
dependencies = [
"chrono",
"encoding",
"futures-util",
"irc-proto",
"log",
"native-tls",
"parking_lot",
"pin-project",
"serde",
"serde_derive",
"thiserror",
"tokio",
"tokio-native-tls",
"tokio-stream",
"tokio-util",
"toml",
]
[[package]]
name = "irc-proto"
version = "0.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "55fa0a52d825e59ba8aea5b7503890245aea000f77e68d9b1903f3491fa33643"
dependencies = [
"bytes 1.1.0",
"encoding",
"thiserror",
"tokio",
"tokio-util",
]
[[package]]
name = "itoa"
version = "0.4.8"
@ -1097,6 +1199,26 @@ dependencies = [
"winapi",
]
[[package]]
name = "thiserror"
version = "1.0.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "854babe52e4df1653706b98fcfc05843010039b406875930a70e4d9644e5c417"
dependencies = [
"thiserror-impl",
]
[[package]]
name = "thiserror-impl"
version = "1.0.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aa32fd3f627f367fe16f893e2597ae3c05020f8bba2666a4e6ea73d377e5714b"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "time"
version = "0.1.44"
@ -1163,6 +1285,17 @@ dependencies = [
"tokio",
]
[[package]]
name = "tokio-stream"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "50145484efff8818b5ccd256697f36863f587da82cf8b409c53adf1e840798e3"
dependencies = [
"futures-core",
"pin-project-lite",
"tokio",
]
[[package]]
name = "tokio-util"
version = "0.6.9"
@ -1177,6 +1310,15 @@ dependencies = [
"tokio",
]
[[package]]
name = "toml"
version = "0.5.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a31142970826733df8241ef35dc040ef98c679ab14d7c3e54d827099b3acecaa"
dependencies = [
"serde",
]
[[package]]
name = "tower-service"
version = "0.3.1"

View file

@ -7,6 +7,7 @@ edition = "2021"
[dependencies]
anyhow = "1.0.52"
irc = "0.15"
[dependencies.tokio]
version = "1.15.0"

View file

@ -2,6 +2,7 @@ use std::{env, sync::Arc};
use serenity::{
async_trait,
futures::StreamExt,
http::Http,
model::{
channel::Message,
@ -9,6 +10,12 @@ use serenity::{
prelude::Ready,
},
prelude::*,
Client as DiscordClient,
};
use irc::{
client::{data::Config, Client as IrcClient, Sender},
proto::Command,
};
struct Handler;
@ -37,11 +44,11 @@ impl EventHandler for Handler {
(http, channel_id, user_id)
};
if user_id != msg.author.id && !msg.author.bot {
send_message(&http, &channel_id, &format!("{}: {}", nick, msg.content))
.await
.unwrap();
}
// if user_id != msg.author.id && !msg.author.bot {
// send_message(&http, &channel_id, &format!("{}: {}", nick, msg.content))
// .await
// .unwrap();
// }
}
async fn ready(&self, ctx: Context, info: Ready) {
@ -55,6 +62,7 @@ impl EventHandler for Handler {
struct HttpKey;
struct ChannelIdKey;
struct UserIdKey;
struct SenderKey;
impl TypeMapKey for HttpKey {
type Value = Arc<Http>;
@ -68,29 +76,64 @@ impl TypeMapKey for UserIdKey {
type Value = UserId;
}
impl TypeMapKey for SenderKey {
type Value = Sender;
}
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let token = env::var("DISCORD_TOKEN").expect("DISCORD_TOKEN not set");
let mut client = Client::builder(&token).event_handler(Handler).await?;
let mut discord_client = DiscordClient::builder(&token)
.event_handler(Handler)
.await?;
let channel_id = ChannelId(831255708875751477);
let config = Config {
nickname: Some("dircord".to_string()),
server: Some("192.168.1.28".to_owned()),
port: Some(6667),
channels: vec!["#no-normies".to_string()],
use_tls: Some(false),
umodes: Some("+B".to_string()),
..Config::default()
};
let mut irc_client = IrcClient::from_config(config).await?;
let http = discord_client.cache_and_http.http.clone();
{
let mut data = client.data.write().await;
data.insert::<HttpKey>(client.cache_and_http.http.clone());
let mut data = discord_client.data.write().await;
data.insert::<HttpKey>(http.clone());
data.insert::<ChannelIdKey>(channel_id);
data.insert::<SenderKey>(irc_client.sender());
}
client.start().await?;
tokio::spawn(async move {
irc_loop(irc_client, http, channel_id).await.unwrap();
});
discord_client.start().await?;
Ok(())
}
async fn send_message(
http: &Http,
channel_id: &ChannelId,
content: &str,
) -> anyhow::Result<Message> {
Ok(channel_id.say(http, content).await?)
async fn irc_loop(
mut client: IrcClient,
http: Arc<Http>,
channel_id: ChannelId,
) -> anyhow::Result<()> {
client.identify()?;
let mut stream = client.stream()?;
while let Some(orig_message) = stream.next().await.transpose()? {
print!("{}", orig_message);
if let Command::PRIVMSG(ref channel, ref message) = orig_message.command {
let nickname = orig_message.source_nickname().unwrap();
channel_id
.say(&http, format!("{}: {}", nickname, message))
.await?;
}
}
Ok(())
}