• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Blightmud / Blightmud / 14316676040

07 Apr 2025 06:27PM UTC coverage: 74.417% (-0.9%) from 75.283%
14316676040

push

github

web-flow
Merge pull request #1202 from Blightmud/dependabot/cargo/rustls-0.23.25

build(deps): bump rustls from 0.23.23 to 0.23.25

7089 of 9526 relevant lines covered (74.42%)

431.56 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

85.92
/src/lua/core.rs
1
use std::sync::mpsc::Sender;
2

3
use libmudtelnet::bytes::Bytes;
4
use log::debug;
5
use mlua::{AnyUserData, Table, UserData, UserDataMethods};
6

7
use crate::{event::Event, io::exec};
8

9
use super::{
10
    constants::{
11
        PROTO_DISABLED_LISTENERS_TABLE, PROTO_ENABLED_LISTENERS_TABLE, PROTO_SUBNEG_LISTENERS_TABLE,
12
    },
13
    exec_response::ExecResponse,
14
};
15

16
#[derive(Debug, Clone)]
17
pub struct Core {
18
    main_writer: Sender<Event>,
19
    next_id: u32,
20
}
21

22
impl Core {
23
    pub fn new(writer: Sender<Event>) -> Self {
165✔
24
        Self {
165✔
25
            main_writer: writer,
165✔
26
            next_id: 0,
165✔
27
        }
165✔
28
    }
165✔
29

30
    fn next_index(&mut self) -> u32 {
2,645✔
31
        self.next_id += 1;
2,645✔
32
        self.next_id
2,645✔
33
    }
2,645✔
34
}
35

36
impl UserData for Core {
37
    fn add_methods<'lua, M: UserDataMethods<'lua, Self>>(methods: &mut M) {
165✔
38
        methods.add_function("enable_protocol", |ctx, proto: u8| {
992✔
39
            let this_aux = ctx.globals().get::<_, AnyUserData>("core")?;
992✔
40
            let this = this_aux.borrow_mut::<Core>()?;
992✔
41
            this.main_writer.send(Event::EnableProto(proto)).unwrap();
992✔
42
            Ok(())
992✔
43
        });
992✔
44
        methods.add_function("disable_protocol", |ctx, proto: u8| {
165✔
45
            let this_aux = ctx.globals().get::<_, AnyUserData>("core")?;
×
46
            let this = this_aux.borrow_mut::<Core>()?;
×
47
            this.main_writer.send(Event::DisableProto(proto)).unwrap();
×
48
            Ok(())
×
49
        });
×
50
        methods.add_function_mut("on_protocol_enabled", |ctx, cb: mlua::Function| {
992✔
51
            let table: Table = ctx.named_registry_value(PROTO_ENABLED_LISTENERS_TABLE)?;
992✔
52
            let this_aux = ctx.globals().get::<_, AnyUserData>("core")?;
992✔
53
            let mut this = this_aux.borrow_mut::<Core>()?;
992✔
54
            table.set(this.next_index(), cb)?;
992✔
55
            ctx.set_named_registry_value(PROTO_ENABLED_LISTENERS_TABLE, table)?;
992✔
56
            Ok(())
992✔
57
        });
992✔
58
        methods.add_function_mut("on_protocol_disabled", |ctx, cb: mlua::Function| {
826✔
59
            let table: Table = ctx.named_registry_value(PROTO_DISABLED_LISTENERS_TABLE)?;
826✔
60
            let this_aux = ctx.globals().get::<_, AnyUserData>("core")?;
826✔
61
            let mut this = this_aux.borrow_mut::<Core>()?;
826✔
62
            table.set(this.next_index(), cb)?;
826✔
63
            ctx.set_named_registry_value(PROTO_DISABLED_LISTENERS_TABLE, table)?;
826✔
64
            Ok(())
826✔
65
        });
826✔
66
        methods.add_function_mut("subneg_recv", |ctx, cb: mlua::Function| {
827✔
67
            let table: Table = ctx.named_registry_value(PROTO_SUBNEG_LISTENERS_TABLE)?;
827✔
68
            let this_aux = ctx.globals().get::<_, AnyUserData>("core")?;
827✔
69
            let mut this = this_aux.borrow_mut::<Core>()?;
827✔
70
            table.set(this.next_index(), cb)?;
827✔
71
            ctx.set_named_registry_value(PROTO_SUBNEG_LISTENERS_TABLE, table)?;
827✔
72
            Ok(())
827✔
73
        });
827✔
74
        methods.add_function_mut("subneg_send", |ctx, (proto, bytes): (u8, Table)| {
165✔
75
            let this_aux = ctx.globals().get::<_, AnyUserData>("core")?;
30✔
76
            let this = this_aux.borrow_mut::<Core>()?;
30✔
77
            let data = bytes
30✔
78
                .pairs::<i32, u8>()
30✔
79
                .filter_map(Result::ok)
30✔
80
                .map(|pair| pair.1)
651✔
81
                .collect::<Bytes>();
30✔
82
            debug!("lua subneg: {}", String::from_utf8_lossy(&data).to_mut());
30✔
83
            this.main_writer
30✔
84
                .send(Event::ProtoSubnegSend(proto, data))
30✔
85
                .unwrap();
30✔
86
            Ok(())
30✔
87
        });
30✔
88
        methods.add_function(
165✔
89
            "exec",
90
            |_, cmd: String| -> Result<ExecResponse, mlua::Error> {
×
91
                match exec(&cmd) {
×
92
                    Ok(output) => Ok(ExecResponse::from(output)),
×
93
                    Err(err) => Err(mlua::Error::RuntimeError(err.to_string())),
×
94
                }
95
            },
×
96
        );
97
        methods.add_function("time", |_, ()| -> Result<i64, mlua::Error> {
165✔
98
            Ok(chrono::Local::now().timestamp_millis())
7✔
99
        });
7✔
100
    }
165✔
101
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc