tools/ubot/src/main.rs
changeset 15814 d9db7b763bd1
parent 15792 191e51179d1b
equal deleted inserted replaced
15813:ad79e5c0885c 15814:d9db7b763bd1
    39         .map(char::from)
    39         .map(char::from)
    40         .collect()
    40         .collect()
    41 }
    41 }
    42 
    42 
    43 async fn handle_irc(pub_channel: &lapin::Channel, irc_message: &Message) -> AHResult<()> {
    43 async fn handle_irc(pub_channel: &lapin::Channel, irc_message: &Message) -> AHResult<()> {
    44     if let Command::PRIVMSG(msgtarget, message) = &irc_message.command {
    44     match &irc_message.command {
    45         let target = irc_message
    45         Command::PRIVMSG(msgtarget, message) => {
    46             .response_target()
    46             let target = irc_message
    47             .expect("Really expected PRIVMSG would have a source");
    47                 .response_target()
    48         let target = if target.starts_with('#') {
    48                 .expect("Really expected PRIVMSG would have a source");
    49             &target[1..]
    49             let target = if target.starts_with('#') {
    50         } else {
    50                 &target[1..]
    51             &target
    51             } else {
    52         };
    52                 &target
       
    53             };
    53 
    54 
    54         let who = irc_message.source_nickname().unwrap_or(msgtarget);
    55             let who = irc_message.source_nickname().unwrap_or(msgtarget);
    55 
    56 
    56         if message.starts_with("!") {
    57             if message.starts_with("!") {
    57             if let Some((cmd, param)) = message.split_once(' ') {
    58                 if let Some((cmd, param)) = message.split_once(' ') {
    58                 pub_channel
    59                     pub_channel
    59                     .basic_publish(
    60                         .basic_publish(
    60                         "irc",
    61                             "irc",
    61                         &format!("cmd.{}.{}", &cmd[1..], target),
    62                             &format!("cmd.{}.{}", &cmd[1..], target),
    62                         BasicPublishOptions::default(),
    63                             BasicPublishOptions::default(),
    63                         format!("{}\n{}", who, param).as_bytes().to_vec(),
    64                             format!("{}\n{}", who, param).as_bytes().to_vec(),
    64                         BasicProperties::default(),
    65                             BasicProperties::default(),
    65                     )
    66                         )
    66                     .await?;
    67                         .await?;
       
    68                 } else {
       
    69                     pub_channel
       
    70                         .basic_publish(
       
    71                             "irc",
       
    72                             &format!("cmd.{}.{}", &message[1..], target),
       
    73                             BasicPublishOptions::default(),
       
    74                             who.as_bytes().to_vec(),
       
    75                             BasicProperties::default(),
       
    76                         )
       
    77                         .await?;
       
    78                 }
    67             } else {
    79             } else {
    68                 pub_channel
    80                 pub_channel
    69                     .basic_publish(
    81                     .basic_publish(
    70                         "irc",
    82                         "irc",
    71                         &format!("cmd.{}.{}", &message[1..], target),
    83                         &format!("msg.{}", target),
    72                         BasicPublishOptions::default(),
    84                         BasicPublishOptions::default(),
    73                         who.as_bytes().to_vec(),
    85                         format!("{}\n{}", who, message).as_bytes().to_vec(),
    74                         BasicProperties::default(),
    86                         BasicProperties::default(),
    75                     )
    87                     )
    76                     .await?;
    88                     .await?;
    77             }
    89             }
    78         } else {
    90         }
       
    91         Command::JOIN(channel, _, _) => {
    79             pub_channel
    92             pub_channel
    80                 .basic_publish(
    93                 .basic_publish(
    81                     "irc",
    94                     "irc",
    82                     &format!("msg.{}", target),
    95                     &format!("join.{}", &channel[1..]),
    83                     BasicPublishOptions::default(),
    96                     BasicPublishOptions::default(),
    84                     format!("{}\n{}", who, message).as_bytes().to_vec(),
    97                     irc_message
       
    98                         .source_nickname()
       
    99                         .expect("Hey, who joined?")
       
   100                         .as_bytes()
       
   101                         .to_vec(),
    85                     BasicProperties::default(),
   102                     BasicProperties::default(),
    86                 )
   103                 )
    87                 .await?;
   104                 .await?;
    88         }
   105         }
       
   106         Command::PART(channel, _) => {
       
   107             pub_channel
       
   108                 .basic_publish(
       
   109                     "irc",
       
   110                     &format!("part.{}", &channel[1..]),
       
   111                     BasicPublishOptions::default(),
       
   112                     irc_message
       
   113                         .source_nickname()
       
   114                         .expect("Hey, who left?")
       
   115                         .as_bytes()
       
   116                         .to_vec(),
       
   117                     BasicProperties::default(),
       
   118                 )
       
   119                 .await?;
       
   120         }
       
   121         _ => (),
    89     }
   122     }
    90 
   123 
    91     Ok(())
   124     Ok(())
    92 }
   125 }
    93 
   126