commit 51469240eeb1114c58cd746c6f6188b035897c6b
parent 821d890948e6ea062c42cadf5a01fa6222e09c6c
Author: thomas.vigouroux@univ-grenoble-alpes.fr <thomas.vigouroux@univ-grenoble-alpes.fr>
Date: Wed, 15 Jun 2022 05:58:49 +0000
Try reconnecting
Diffstat:
M | src/main.rs | | | 137 | ++++++++++++++++++++++++++++++++++++++++++++++++------------------------------- |
1 file changed, 83 insertions(+), 54 deletions(-)
diff --git a/src/main.rs b/src/main.rs
@@ -2,8 +2,10 @@ use chrono::{prelude::*, Duration};
use futures::prelude::*;
use irc::client::prelude::*;
use std::{collections::HashMap, str::FromStr};
+use std::thread::sleep;
+use std::time::Duration as StdDuration;
-use log::{debug, info};
+use log::{debug, info, error};
use simple_logger::SimpleLogger;
#[macro_use]
@@ -21,6 +23,7 @@ use cmds::stats::{add_words, stats};
const NICK: &'static str = "vortaroboto";
const LINEO_SEP: &'static str = "\r\n";
+const DORM_TEMPO: u64 = 500;
enum NumerSelektilo {
Cxiuj,
@@ -59,73 +62,99 @@ async fn main() -> irc::error::Result<()> {
channels: vec!["##esperanto".to_owned()],
max_messages_in_burst: Some(5),
burst_window_length: Some(1),
- ping_timeout: Some(600),
+ // ping_timeout: Some(600),
..Config::default()
};
- let mut client = Client::from_config(config).await?;
- client.identify()?;
+ loop {
- let mut stream = client.stream()?;
- let sender = client.sender();
+ let mut client = Client::from_config(config.clone()).await;
+ while let Err(_) = client {
+ client = Client::from_config(config.clone()).await;
+ error!("Could not connect the client.");
+ sleep(StdDuration::from_millis(1000));
+ }
- loop {
- match stream.next().await.transpose() {
- Ok(Some(message)) => {
- debug!("{}", message);
- match message.command {
- Command::PRIVMSG(ref target, ref msg) => {
- let is_channel = target.starts_with('#');
- let target = if is_channel {
- target
- } else {
- message.source_nickname().unwrap_or(target)
- };
-
- if let Some(cmd) = msg.strip_prefix('!') {
- if let Some(answer) = handle_command(target, cmd).await {
- let r = match (is_channel, message.source_nickname()) {
- (true, Some(n)) => format!("{}:\r\n{}", n, answer),
- _ => answer,
- };
- sender.send_privmsg(target, r)?;
+ let mut client = if let Ok(c) = client {
+ c
+ } else {
+ unreachable!();
+ };
+
+ while let Err(_) = client.identify() {
+ error!("Could not identify.");
+ sleep(StdDuration::from_millis(1000));
+ };
+
+ let mut stream = client.stream()?;
+ let sender = client.sender();
+
+ loop {
+ match stream.next().await.transpose() {
+ Ok(Some(message)) => {
+ debug!("{}", message);
+ match message.command {
+ Command::PRIVMSG(ref target, ref msg) => {
+ let is_channel = target.starts_with('#');
+ let target = if is_channel {
+ target
+ } else {
+ message.source_nickname().unwrap_or(target)
+ };
+
+ if let Some(cmd) = msg.strip_prefix('!') {
+ if let Some(answer) = handle_command(target, cmd).await {
+ let r = match (is_channel, message.source_nickname()) {
+ (true, Some(n)) => format!("{}:\r\n{}", n, answer),
+ _ => answer,
+ };
+ sender.send_privmsg(target, r)?;
+ }
+ continue;
}
- continue;
- }
- // TODO: make this local to a channel / query
- tokio::task::spawn(add_words(msg.to_string()));
- if let Some(nomo) = message.source_nickname() {
- vidita_update(target, nomo, msg);
- }
+ // TODO: make this local to a channel / query
+ tokio::task::spawn(add_words(msg.to_string()));
+ if let Some(nomo) = message.source_nickname() {
+ vidita_update(target, nomo, msg);
+ }
- // Donu bonaj respondoj ofte
- if msg.contains(client.current_nickname()) {
- let msg = match msg.split_ascii_whitespace().next() {
- Some("saluton" | "Saluton") => {
- format!(
+ // Donu bonaj respondoj ofte
+ if msg.contains(client.current_nickname()) {
+ let msg = match msg.split_ascii_whitespace().next() {
+ Some("saluton" | "Saluton") => {
+ format!(
"Saluton ! Mi estas {} la roboto kiun vi povas demandi pri vortoj kaj tradukoj ! Tajpu !helpu por pli'sciiĝi !",
irc_fmt!(FormattingKind::Bold => client.current_nickname())
)
- }
- Some("dankon" | "Dankon") => {
- format!(
- "Nedankinde {} !",
- message.source_nickname().unwrap_or("vi")
- )
- }
- _ => {
- continue;
- }
- };
- sender.send_privmsg(target, msg)?;
+ }
+ Some("dankon" | "Dankon") => {
+ format!(
+ "Nedankinde {} !",
+ message.source_nickname().unwrap_or("vi")
+ )
+ }
+ _ => {
+ continue;
+ }
+ };
+ sender.send_privmsg(target, msg)?;
+ }
}
+ _ => (),
}
- _ => (),
}
- },
- Ok(None) | Err(irc::error::Error::PingTimeout) => {},
- Err(e) => { return Err(e) }
+ Ok(None) => {},
+ Err(irc::error::Error::PingTimeout) => {
+ info!("Ping timeout, reconnecting...")
+ sleep(StdDuration::from_millis(DORM_TEMPO));
+ break;
+ }
+ Err(e) => {
+ error!("{}", e.to_string());
+ break;
+ }
+ }
}
}
}