-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.rs
109 lines (97 loc) · 4.74 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#![allow(non_snake_case)]
#![allow(non_camel_case_types)]
#![allow(unused_assignments)]
#[macro_use]
extern crate clone_all;
use std::{net::TcpStream, io::{prelude::*, BufReader}, sync::{Arc,Mutex}, fs::File, str, env, usize, path::PathBuf};
use aho_corasick::AhoCorasick;
pub mod structs;
pub mod messages;
pub mod loops;
use loops::event;
pub fn connect_server(server: &structs::Server, handler: u32) -> (Arc<Mutex<TcpStream>>,Arc<Mutex<u32>>) {
let stream = Arc::new(Mutex::new(TcpStream::connect(format!("{}:{}",&server.host,&server.port)).unwrap()));
let mut handler = Arc::new(Mutex::new(handler));
let mut cloned_stream = stream.lock().unwrap();
cloned_stream.read(&mut [0;4]).unwrap();
let mut ver = [0;12];
cloned_stream.read(&mut ver).unwrap();
drop(cloned_stream);
assert_eq!(str::from_utf8(&ver).unwrap().trim_matches(char::from(0)),"GBXRemote 2");
println!("Connection acquired to server.");
handler = messages::send_message(messages::transform_xmlrpc(format!(r#"mc
mn Authenticate
pa
paa va str SuperAdmin
paa va str {}"#,&server.password)),Arc::clone(&stream),Arc::clone(&handler)).unwrap().1;
handler = messages::send_message(messages::transform_xmlrpc(String::from(r#"mc
mn EnableCallbacks
pa
paa va bool true"#)),Arc::clone(&stream),Arc::clone(&handler)).unwrap().1;
(stream,handler)
}
fn parse_xrd(parse: PathBuf) -> structs::Config {
let fr = BufReader::new(File::open(&parse).expect("Couldn't open config"));
let ac_findvh = AhoCorasick::new_auto_configured(&["^^"]);
let ac_verbs = AhoCorasick::new_auto_configured(&["version","prefix","host","port","password","trackspath","songspath","admins","gametype"]);
let mut cnfroot = structs::Config::default();
let mut srvvec = Vec::new();
let mut srvcount: u16 = 0;
for line in fr.lines() {
let lineu = line.unwrap();
if ac_findvh.is_match(&lineu) {
srvvec.push(structs::Server::default());
srvcount+=1;
continue;
} else if ac_verbs.is_match(&lineu) {
let mat = ac_verbs.find(&lineu).unwrap();
match mat {
_ if mat.pattern() == 0 => cnfroot.main.version = (&lineu[..][&lineu.find('|').unwrap()+1..].to_string()).to_owned(),
_ if mat.pattern() == 1 => cnfroot.main.prefix = (&lineu[..][&lineu.find('|').unwrap()+1..].to_string()).to_owned(),
_ if mat.pattern() == 2 => if srvcount > 0 { srvvec[usize::from(srvcount-1)].host = (&lineu[..][&lineu.find('|').unwrap()+1..].to_string()).to_owned() } else { continue; },
_ if mat.pattern() == 3 => if srvcount > 0 { srvvec[usize::from(srvcount-1)].port = (&lineu[..][&lineu.find('|').unwrap()+1..].to_string()).to_owned() } else { continue; },
_ if mat.pattern() == 4 => if srvcount > 0 { srvvec[usize::from(srvcount-1)].password = (&lineu[..][&lineu.find('|').unwrap()+1..].to_string()).to_owned() } else { continue; },
_ if mat.pattern() == 5 => if srvcount > 0 { srvvec[usize::from(srvcount-1)].trackspath = (&lineu[..][&lineu.find('|').unwrap()+1..].to_string()).to_owned() } else { continue; },
_ if mat.pattern() == 6 => if srvcount > 0 { srvvec[usize::from(srvcount-1)].songspath = (&lineu[..][&lineu.find('|').unwrap()+1..].to_string()).to_owned() } else { continue; },
_ if mat.pattern() == 7 => if srvcount > 0 { srvvec[usize::from(srvcount-1)].admins = (&lineu[..][&lineu.find('|').unwrap()+1..].to_string()).to_owned() } else { continue; },
_ if mat.pattern() == 8 => if srvcount > 0 { srvvec[usize::from(srvcount-1)].gametype = (&lineu[..][&lineu.find('|').unwrap()+1..].to_string()).to_owned() } else { continue; },
_ => structs::nop(),
};
}
} cnfroot.servers = srvvec;
cnfroot
}
fn main() {
let mut path = PathBuf::new();
path.push(env::current_dir().expect("No cwd?"));
path.push("cnf.xrd");
let cnfroot = parse_xrd(path);
let version = format!("xrd v{}",&cnfroot.main.version);
let mut handles = Vec::new();
println!(r#"
.......... ..........
,*//////////,. .,//////////*,
.*//////////*, .**/////////*.
,*//////////,. ,*/////////*,
.,*////////**,*//////////*.
.,//////////////(##%%(,
,/(((((///((##%%%#*.
,(%%%%%%%%%%%%#/,
*/##%%%%%%%%#(**,,.
.,//////((((//*,,,,,,,.
,/#%&&%#(*,,,,,,,*/(#%(/,
.*#%%%%%%%%#*. .*(#%%%%%%%(*.
.,*(%%%%%%%%(*. .*(%%%%%%%%#/*.
.,*(#%%%%%%%%/. ,/%%%%%%%%%%#(,
./((((((((((/, ,/((((((((((/,
{}"#,&version);
for x in &cnfroot.servers {
handles.push(std::thread::spawn({clone_all!(cnfroot, x); move || {
let sockarray = connect_server(&x,0x80000000);
event(sockarray,(&cnfroot.main,&x))
}}));
}
for x in handles {
x.join().unwrap();
}
}