initial commit

Signed-off-by: Harald Hoyer <harald@hoyer.xyz>
This commit is contained in:
Harald Hoyer 2024-03-21 12:48:09 +01:00
commit 3e3c4e4d6e
5 changed files with 122 additions and 0 deletions

1
.gitattributes vendored Normal file
View file

@ -0,0 +1 @@
src/main.rs text filter=rot8000

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/target

54
Cargo.lock generated Normal file
View file

@ -0,0 +1,54 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "aho-corasick"
version = "1.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916"
dependencies = [
"memchr",
]
[[package]]
name = "memchr"
version = "2.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "523dc4f511e55ab87b694dc30d0f820d60906ef06413f93d4d7a1385599cc149"
[[package]]
name = "regex"
version = "1.10.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b62dbe01f0b06f9d8dc7d49e05a0785f153b00b2c227856282f671e0318c9b15"
dependencies = [
"aho-corasick",
"memchr",
"regex-automata",
"regex-syntax",
]
[[package]]
name = "regex-automata"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "86b83b8b9847f9bf95ef68afb0b8e6cdb80f498442f5179a29fad448fcc1eaea"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f"
[[package]]
name = "rot8000"
version = "0.1.0"
dependencies = [
"regex",
]

9
Cargo.toml Normal file
View file

@ -0,0 +1,9 @@
[package]
name = "rot8000"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
regex = "1.10"

57
src/main.rs Normal file
View file

@ -0,0 +1,57 @@
/*!
To be used as a git smudge filter,
to replace email addresses in a file with a scrambled version of the email address
```toml
[filter "rot8000"]
smudge = /home/foo/.cargo/bin/rot8000
clean = /home/foo/.cargo/bin/rot8000
```
"grfg123@rknzcyr.bet"
"grfg456@rknzcyr.bet"
"grfg456@rknzcyr1.bet"
!*/
use regex::{Captures, Regex, Replacer};
use std::io::stdin;
struct Rot8000;
impl Replacer for Rot8000 {
/// Replace the matched text with a replacement.
///
/// Should have implemented a true unicode rot13, but I was lazy
fn replace_append(&mut self, caps: &Captures<'_>, dst: &mut String) {
for c in caps[0].chars() {
if c.is_ascii() {
// do rot13 for ascii alphanumeric characters
let mut c = c as u8;
if c.is_ascii_lowercase() {
c = (c - b'a' + 13) % 26 + b'a';
} else if c.is_ascii_uppercase() {
c = (c - b'A' + 13) % 26 + b'A';
}
dst.push(unsafe { char::from_u32_unchecked(c as u32) });
} else {
dst.push(c);
}
}
}
}
fn main() {
// match only ascii email addresses for now
let re =
Regex::new(r#""([[[:ascii:]]&&[^"\s\[\]\\]]*?@[[[:ascii:]]&&[^"\s]\[\]\\]*)""#).unwrap();
let mut line = String::new();
while let Ok(n) = stdin().read_line(&mut line) {
if n == 0 {
break;
}
let result = re.replace(&line, Rot8000);
print!("{}", result);
line.clear();
}
}