Skip to main content

freeswitch_log_parser/message/
classify.rs

1//! The classification dispatcher — the ordered prefix checks that map a
2//! message to its [`MessageKind`], and the shape-specific constructors.
3
4use crate::codec::CodecMedia;
5
6use super::dtmf::parse_dtmf;
7use super::kind::MessageKind;
8use super::lifecycle::{classify_channel_prefixed, detect_channel_lifecycle};
9use super::media::{detect_media, detect_sdp_direction};
10use super::parts::{
11    dialplan_parts, execute_parts, parse_bracketed_value, set_export_parts, strip_channel_prefix,
12};
13
14fn parse_execute(msg: &str) -> MessageKind {
15    let parts = execute_parts(msg);
16    MessageKind::Execute {
17        depth: parts.depth,
18        channel: parts.channel.to_string(),
19        application: parts.application.to_string(),
20        arguments: parts.arguments.to_string(),
21    }
22}
23fn parse_dialplan(msg: &str) -> MessageKind {
24    let (channel, detail) = dialplan_parts(msg);
25    MessageKind::Dialplan {
26        channel: channel.to_string(),
27        detail: detail.to_string(),
28    }
29}
30/// Classify a log message's text into a [`MessageKind`].
31///
32/// Pure function — no state, no allocation beyond the returned enum. Works on
33/// the `message` field from [`RawLine`](crate::RawLine) or any raw message string.
34pub fn classify_message(msg: &str) -> MessageKind {
35    if msg.starts_with("EXECUTE ") || msg.starts_with("Execute ") {
36        return parse_execute(msg);
37    }
38
39    if msg.starts_with("RECV DTMF ")
40        || msg.starts_with("RTP RECV DTMF ")
41        || msg.starts_with("INFO DTMF(")
42    {
43        if let Some(dtmf) = parse_dtmf(msg) {
44            return dtmf;
45        }
46    }
47
48    if msg.starts_with("Dialplan: ") || msg.starts_with("Chatplan: ") {
49        return parse_dialplan(msg);
50    }
51
52    if msg.starts_with("Processing ")
53        && (msg.contains(" in context ") || msg.contains("recursive conditions"))
54    {
55        return parse_dialplan_processing(msg);
56    }
57
58    if msg.contains("CHANNEL_DATA") {
59        return MessageKind::ChannelData;
60    }
61
62    if msg.starts_with("variable_") {
63        if let Some((name, value)) = parse_bracketed_value(msg, 0) {
64            return MessageKind::Variable {
65                name: name.to_string(),
66                value: value.to_string(),
67            };
68        }
69    }
70
71    if let Some(direction) = detect_sdp_direction(msg) {
72        return MessageKind::SdpMarker { direction };
73    }
74
75    if msg.contains("State Change") || msg.contains("Callstate Change") {
76        return MessageKind::StateChange {
77            detail: msg.to_string(),
78        };
79    }
80
81    if msg.starts_with("SET ") || msg.starts_with("EXPORT ") {
82        if let Some(sv) = parse_set_or_export(msg) {
83            return sv;
84        }
85    }
86
87    if msg.starts_with("Audio Codec Compare ") {
88        return MessageKind::CodecNegotiation {
89            media: CodecMedia::Audio,
90        };
91    }
92
93    if msg.starts_with("Video Codec Compare ") {
94        return MessageKind::CodecNegotiation {
95            media: CodecMedia::Video,
96        };
97    }
98
99    if msg.starts_with("CoreSession::setVariable(") {
100        return parse_core_session_set_variable(msg);
101    }
102
103    if msg.starts_with("UNSET ") {
104        return parse_unset(msg);
105    }
106
107    // Pre-dialplan set action: "set variable name=value"
108    if let Some(rest) = msg.strip_prefix("set variable ") {
109        if let Some((name, value)) = rest.split_once('=') {
110            return MessageKind::Variable {
111                name: format!("variable_{name}"),
112                value: value.to_string(),
113            };
114        }
115    }
116
117    if msg.starts_with("Transfer ") {
118        return MessageKind::Dialplan {
119            channel: String::new(),
120            detail: msg.to_string(),
121        };
122    }
123
124    // (channel) State STATE — parenthesized channel state
125    if msg.starts_with('(') {
126        if msg.contains(") State ") {
127            return MessageKind::StateChange {
128                detail: msg.to_string(),
129            };
130        }
131        return MessageKind::ChannelLifecycle {
132            detail: msg.to_string(),
133        };
134    }
135
136    // SOFIA STATE (no channel prefix) — e.g. "SOFIA EXCHANGE_MEDIA"
137    if msg.starts_with("SOFIA ") {
138        return MessageKind::StateChange {
139            detail: msg.to_string(),
140        };
141    }
142
143    // Pre-dialplan: checking condition / action results from sofia_pre_dialplan.c
144    if msg.starts_with("checking condition") || msg.starts_with("action(") {
145        return MessageKind::ChannelLifecycle {
146            detail: msg.to_string(),
147        };
148    }
149
150    if msg.starts_with("Event Socket Command") {
151        return MessageKind::EventSocket {
152            detail: msg.to_string(),
153        };
154    }
155
156    // Media patterns (no channel prefix)
157    if let Some(kind) = detect_media(msg) {
158        return kind;
159    }
160
161    // Channel lifecycle patterns (no channel prefix)
162    if let Some(kind) = detect_channel_lifecycle(msg) {
163        return kind;
164    }
165
166    // Channel-prefixed messages: sofia/..., loopback/... prefix
167    if let Some((channel_part, rest)) = strip_channel_prefix(msg) {
168        return classify_channel_prefixed(channel_part, rest);
169    }
170
171    // Channel-* fields and other Key: [value] patterns from CHANNEL_DATA dumps
172    // Must come after more specific checks to avoid false positives
173    if let Some((name, value)) = parse_bracketed_value(msg, 0) {
174        let name_bytes = name.as_bytes();
175        if !name_bytes.is_empty()
176            && !name.contains(' ')
177            && name_bytes[0].is_ascii_alphabetic()
178            && (name.contains('-') || name.starts_with("Channel-"))
179        {
180            return MessageKind::ChannelField {
181                name: name.to_string(),
182                value: value.to_string(),
183            };
184        }
185    }
186
187    MessageKind::General
188}
189fn parse_core_session_set_variable(msg: &str) -> MessageKind {
190    let rest = &msg["CoreSession::setVariable(".len()..];
191    if let Some(end) = rest.strip_suffix(')') {
192        if let Some(comma) = end.find(", ") {
193            return MessageKind::Variable {
194                name: format!("variable_{}", &end[..comma]),
195                value: end[comma + 2..].to_string(),
196            };
197        }
198    }
199    MessageKind::Variable {
200        name: String::new(),
201        value: msg.to_string(),
202    }
203}
204
205fn parse_unset(msg: &str) -> MessageKind {
206    let rest = &msg["UNSET ".len()..];
207    let name = if let Some(inner) = rest.strip_prefix('[') {
208        inner.strip_suffix(']').unwrap_or(inner)
209    } else {
210        rest
211    };
212    MessageKind::Variable {
213        name: format!("variable_{name}"),
214        value: String::new(),
215    }
216}
217
218fn parse_dialplan_processing(msg: &str) -> MessageKind {
219    let rest = &msg["Processing ".len()..];
220    MessageKind::Dialplan {
221        channel: String::new(),
222        detail: rest.to_string(),
223    }
224}
225fn parse_set_or_export(msg: &str) -> Option<MessageKind> {
226    let parts = set_export_parts(msg)?;
227    Some(MessageKind::Variable {
228        name: format!("variable_{}", parts.name),
229        value: parts.value.to_string(),
230    })
231}