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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
use crate::{
    md::structs::{MessDetectorChar, MessDetectorCharFlags},
    utils::{is_suspiciously_successive_range, remove_accent},
};

// Base abstract trait used for mess detection plugins.
// All detectors MUST extend and implement given methods.
pub(super) trait MessDetectorPlugin {
    // Name of plugin
    fn name(&self) -> &str {
        std::any::type_name::<Self>().split("::").last().unwrap()
    }

    // Determine if given character should be fed in
    fn eligible(&self, character: &MessDetectorChar) -> bool;

    // The main routine to be executed upon character.
    // Insert the logic in witch the text would be considered chaotic.
    fn feed(&mut self, character: &MessDetectorChar);

    // Compute the chaos ratio based on what your feed() has seen.
    // Must NOT be lower than 0.; No restriction gt 0.
    fn ratio(&self) -> f32;
}

//
// TooManySymbolOrPunctuationPlugin implementation
//
#[derive(Default)]
pub(super) struct TooManySymbolOrPunctuationPlugin {
    punctuation_count: u64,
    symbol_count: u64,
    character_count: u64,
    last_printable_char: Option<MessDetectorChar>,
}

impl MessDetectorPlugin for TooManySymbolOrPunctuationPlugin {
    fn eligible(&self, character: &MessDetectorChar) -> bool {
        !character.is(MessDetectorCharFlags::UNPRINTABLE)
    }
    fn feed(&mut self, character: &MessDetectorChar) {
        self.character_count += 1;
        if (self.last_printable_char.is_none() || *character != self.last_printable_char.unwrap())
            && !character.is(MessDetectorCharFlags::COMMON_SAFE)
        {
            if character.is(MessDetectorCharFlags::PUNCTUATION) {
                self.punctuation_count += 1;
            } else if !character.is(MessDetectorCharFlags::NUMERIC)
                && character.is(MessDetectorCharFlags::SYMBOL)
                && !character.is(MessDetectorCharFlags::EMOTICON)
            {
                self.symbol_count += 2;
            }
        }
        self.last_printable_char = Some(*character);
    }
    fn ratio(&self) -> f32 {
        if self.character_count == 0 {
            return 0.0;
        }
        let ratio_of_punctuation =
            (self.punctuation_count + self.symbol_count) as f32 / (self.character_count as f32);
        if ratio_of_punctuation >= 0.3 {
            ratio_of_punctuation
        } else {
            0.0
        }
    }
}

//
// TooManyAccentuatedPlugin implementation
//

#[derive(Default)]
pub(super) struct TooManyAccentuatedPlugin {
    character_count: u64,
    accentuated_count: u64,
}

impl MessDetectorPlugin for TooManyAccentuatedPlugin {
    fn eligible(&self, character: &MessDetectorChar) -> bool {
        character.is(MessDetectorCharFlags::ALPHABETIC)
    }
    fn feed(&mut self, character: &MessDetectorChar) {
        self.character_count += 1;
        if character.is(MessDetectorCharFlags::ACCENTUATED) {
            self.accentuated_count += 1;
        }
    }
    fn ratio(&self) -> f32 {
        (self.character_count >= 8)
            .then_some(self.accentuated_count as f32 / self.character_count as f32)
            .filter(|&ratio| ratio >= 0.35)
            .unwrap_or(0.0)
    }
}

//
// UnprintablePlugin implementation
//

#[derive(Default)]
pub(super) struct UnprintablePlugin {
    character_count: u64,
    unprintable_count: u64,
}

impl MessDetectorPlugin for UnprintablePlugin {
    fn eligible(&self, _character: &MessDetectorChar) -> bool {
        true
    }
    fn feed(&mut self, character: &MessDetectorChar) {
        if character.is(MessDetectorCharFlags::UNPRINTABLE) {
            self.unprintable_count += 1;
        }
        self.character_count += 1;
    }
    fn ratio(&self) -> f32 {
        if self.character_count == 0 {
            return 0.0;
        }
        (self.unprintable_count as f32 * 8.0) / self.character_count as f32
    }
}

//
// SuspiciousDuplicateAccentPlugin implementation
//
#[derive(Default)]
pub(super) struct SuspiciousDuplicateAccentPlugin {
    character_count: u64,
    successive_count: u64,
    last_latin_character: Option<MessDetectorChar>,
}

impl MessDetectorPlugin for SuspiciousDuplicateAccentPlugin {
    fn eligible(&self, character: &MessDetectorChar) -> bool {
        character.is(MessDetectorCharFlags::ALPHABETIC)
            && character.is(MessDetectorCharFlags::LATIN)
    }
    fn feed(&mut self, character: &MessDetectorChar) {
        self.character_count += 1;
        if self.last_latin_character.is_some()
            && character.is(MessDetectorCharFlags::ACCENTUATED)
            && self
                .last_latin_character
                .unwrap()
                .is(MessDetectorCharFlags::ACCENTUATED)
        {
            if character.is(MessDetectorCharFlags::UPPERCASE)
                && self
                    .last_latin_character
                    .unwrap()
                    .is(MessDetectorCharFlags::UPPERCASE)
            {
                self.successive_count += 1;
            }

            // Worse if its the same char duplicated with different accent.
            if remove_accent(character.character)
                == remove_accent(self.last_latin_character.unwrap().character)
            {
                self.successive_count += 1;
            }
        }
        self.last_latin_character = Some(*character);
    }
    fn ratio(&self) -> f32 {
        if self.character_count == 0 {
            return 0.0;
        }
        (self.successive_count as f32 * 2.0) / self.character_count as f32
    }
}

//
// SuspiciousRangePlugin implementation
//
#[derive(Default)]
pub(super) struct SuspiciousRangePlugin {
    character_count: u64,
    suspicious_successive_range_count: u64,
    last_printable_char: Option<MessDetectorChar>,
}

impl MessDetectorPlugin for SuspiciousRangePlugin {
    fn eligible(&self, character: &MessDetectorChar) -> bool {
        !character.is(MessDetectorCharFlags::UNPRINTABLE)
    }
    fn feed(&mut self, character: &MessDetectorChar) {
        self.character_count += 1;

        if character.is(MessDetectorCharFlags::WHITESPACE)
            || character.is(MessDetectorCharFlags::PUNCTUATION)
            || character.is(MessDetectorCharFlags::COMMON_SAFE)
        {
            self.last_printable_char = None;
            return;
        }

        if self.last_printable_char.is_none() {
            self.last_printable_char = Some(*character);
            return;
        }

        if is_suspiciously_successive_range(
            self.last_printable_char.unwrap().unicode_range,
            character.unicode_range,
        ) {
            self.suspicious_successive_range_count += 1;
        }

        self.last_printable_char = Some(*character);
    }
    fn ratio(&self) -> f32 {
        (self.character_count > 0)
            .then_some(
                ((self.suspicious_successive_range_count as f32) * 2.0)
                    / self.character_count as f32,
            )
            .filter(|&ratio| ratio >= 0.1)
            .unwrap_or(0.0)
    }
}

//
// SuperWeirdWordPlugin implementation
//

#[derive(Default)]
pub(super) struct SuperWeirdWordPlugin {
    character_count: u64,
    word_count: u64,
    bad_word_count: u64,
    foreign_long_count: u64,
    is_current_word_bad: bool,
    foreign_long_watch: bool,
    bad_character_count: u64,
    buffer_accent_count: u64,
    buffer: Vec<MessDetectorChar>,
}

impl MessDetectorPlugin for SuperWeirdWordPlugin {
    fn eligible(&self, _character: &MessDetectorChar) -> bool {
        true
    }
    fn feed(&mut self, character: &MessDetectorChar) {
        if character.is(MessDetectorCharFlags::ASCII_ALPHABETIC) {
            self.buffer.push(*character);
            if character.is(MessDetectorCharFlags::ACCENTUATED) {
                self.buffer_accent_count += 1;
            }
            self.foreign_long_watch |= (!character.is(MessDetectorCharFlags::LATIN)
                || character.is(MessDetectorCharFlags::ACCENTUATED))
                && !character.is(MessDetectorCharFlags::CJK)
                && !character.is(MessDetectorCharFlags::HANGUL)
                && !character.is(MessDetectorCharFlags::KATAKANA)
                && !character.is(MessDetectorCharFlags::HIRAGANA)
                && !character.is(MessDetectorCharFlags::THAI);
            return;
        }
        if self.buffer.is_empty() {
            return;
        }

        if character.is(MessDetectorCharFlags::WHITESPACE)
            || character.is(MessDetectorCharFlags::PUNCTUATION)
            || character.is(MessDetectorCharFlags::SEPARATOR)
        {
            self.word_count += 1;
            let buffer_length = self.buffer.len();
            self.character_count += buffer_length as u64;

            if buffer_length >= 4 {
                if (self.buffer_accent_count as f32 / buffer_length as f32) > 0.34 {
                    self.is_current_word_bad = true;
                }

                // Word/Buffer ending with an upper case accentuated letter are so rare,
                // that we will consider them all as suspicious. Same weight as foreign_long suspicious.
                let last_char = self.buffer.last().unwrap();
                if last_char.is(MessDetectorCharFlags::ACCENTUATED)
                    && last_char.is(MessDetectorCharFlags::UPPERCASE)
                {
                    self.foreign_long_count += 1;
                    self.is_current_word_bad = true;
                }
            }
            if buffer_length >= 24 && self.foreign_long_watch {
                let uppercase_count = self
                    .buffer
                    .iter()
                    .filter(|&c| c.is(MessDetectorCharFlags::UPPERCASE))
                    .count();
                let mut probable_camel_cased: bool = false;

                if uppercase_count > 0 && (uppercase_count as f32 / buffer_length as f32) <= 0.3 {
                    probable_camel_cased = true;
                }

                if !probable_camel_cased {
                    self.foreign_long_count += 1;
                    self.is_current_word_bad = true;
                }
            }

            if self.is_current_word_bad {
                self.bad_word_count += 1;
                self.bad_character_count += self.buffer.len() as u64;
                self.is_current_word_bad = false;
            }

            self.foreign_long_watch = false;
            self.buffer.clear();
            self.buffer_accent_count = 0;
        } else if !character.is(MessDetectorCharFlags::WEIRD_SAFE)
            && !character.is(MessDetectorCharFlags::ASCII_DIGIT)
            && character.is(MessDetectorCharFlags::SYMBOL)
        {
            self.is_current_word_bad = true;
            self.buffer.push(*character);
        }
    }
    fn ratio(&self) -> f32 {
        if self.word_count <= 10 && self.foreign_long_count == 0 {
            return 0.0;
        }
        self.bad_character_count as f32 / self.character_count as f32
    }
}

//
// CjkInvalidStopPlugin implementation
//
// GB(Chinese) based encoding often render the stop incorrectly when the content does not fit and
// can be easily detected. Searching for the overuse of '丅' and '丄'.
#[derive(Default)]
pub(super) struct CjkInvalidStopPlugin {
    wrong_stop_count: u64,
    cjk_character_count: u64,
}

impl MessDetectorPlugin for CjkInvalidStopPlugin {
    fn eligible(&self, _: &MessDetectorChar) -> bool {
        true
    }
    fn feed(&mut self, character: &MessDetectorChar) {
        if "丅丄".contains(character.character) {
            self.wrong_stop_count += 1;
            return;
        }
        if character.is(MessDetectorCharFlags::CJK) {
            self.cjk_character_count += 1;
        }
    }
    fn ratio(&self) -> f32 {
        if self.cjk_character_count < 16 {
            return 0.0;
        }
        self.wrong_stop_count as f32 / self.cjk_character_count as f32
    }
}

//
// ArchaicUpperLowerPlugin implementation
//

pub(super) struct ArchaicUpperLowerPlugin {
    buf: bool,
    current_ascii_only: bool,
    character_count_since_last_sep: u64,
    successive_upper_lower_count: u64,
    successive_upper_lower_count_final: u64,
    character_count: u64,
    last_alpha_seen: Option<MessDetectorChar>,
}

impl Default for ArchaicUpperLowerPlugin {
    fn default() -> Self {
        ArchaicUpperLowerPlugin {
            buf: false,
            current_ascii_only: true,
            character_count_since_last_sep: 0,
            successive_upper_lower_count: 0,
            successive_upper_lower_count_final: 0,
            character_count: 0,
            last_alpha_seen: None,
        }
    }
}

impl MessDetectorPlugin for ArchaicUpperLowerPlugin {
    fn eligible(&self, _: &MessDetectorChar) -> bool {
        true
    }
    fn feed(&mut self, character: &MessDetectorChar) {
        if !(character.is(MessDetectorCharFlags::ALPHABETIC)
            && character.is(MessDetectorCharFlags::CASE_VARIABLE))
            && self.character_count_since_last_sep > 0
        {
            if self.character_count_since_last_sep <= 64
                && !character.is(MessDetectorCharFlags::ASCII_DIGIT)
                && !self.current_ascii_only
            {
                self.successive_upper_lower_count_final += self.successive_upper_lower_count;
            }

            self.successive_upper_lower_count = 0;
            self.character_count_since_last_sep = 0;
            self.last_alpha_seen = None;
            self.buf = false;
            self.character_count += 1;
            self.current_ascii_only = true;

            return;
        }

        self.current_ascii_only &= character.is(MessDetectorCharFlags::ASCII);

        if let Some(tmp_last_alpha) = self.last_alpha_seen {
            if (character.is(MessDetectorCharFlags::UPPERCASE)
                && tmp_last_alpha.is(MessDetectorCharFlags::LOWERCASE))
                || (character.is(MessDetectorCharFlags::LOWERCASE)
                    && tmp_last_alpha.is(MessDetectorCharFlags::UPPERCASE))
            {
                if self.buf {
                    self.successive_upper_lower_count += 2;
                    self.buf = false;
                } else {
                    self.buf = true;
                }
            } else {
                self.buf = false;
            }
        }

        self.character_count += 1;
        self.character_count_since_last_sep += 1;
        self.last_alpha_seen = Some(*character);
    }
    fn ratio(&self) -> f32 {
        if self.character_count == 0 {
            return 0.0;
        }
        self.successive_upper_lower_count_final as f32 / self.character_count as f32
    }
}