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
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
use std::sync::Arc;

use rstring_builder::StringBuilder;
use text_reader::TextReader;

use crate::api::BotApi;
use crate::listener::Lout;
use crate::tglog;
use crate::types::{MessageEntity, MessageEntityKind, RawMessage};
use crate::vision::{Message, VCommand, VTextMessage};

pub fn handle_text(api: BotApi, lout: &Arc<Lout>, raw: &RawMessage, message: Message) {
  let text = raw.text.clone().unwrap();
  let entities = raw.entities.clone().unwrap_or_else(|| Vec::with_capacity(0));

  debug!(tglog::advanced(), "entities is empty | {:?}", entities.is_empty());
  debug!(tglog::advanced(), "entities => {:?}", entities);
  if entities.is_empty() {
    if let Some(fnc) = lout.listen_text() {
      let obj = VTextMessage { message, text, entities };
      (*fnc)((api, obj));
      return;
    }
    return;
  }

  let first = entities.get(0).unwrap();
  if first.kind == MessageEntityKind::BotCommand {
    handle_command(api, lout, message, &text, entities);
  }
}

fn handle_command(api: BotApi, lout: &Arc<Lout>, message: Message, text: &String, entities: Vec<MessageEntity>) {
  let (command, args) = extra_command(text);
  debug!(tglog::advanced(), "COMMAND: {:?} => ARGS: {:?}", command, args);

  let vcmd = VCommand {
    message,
    text: text.clone(),
    entities,
    command: command.clone(),
    args,
  };

  let lcd = lout.listen_command();

  if let Some(fnc) = lcd.get(&command[..]) {
    if let Some(lp) = lout.listen_precommand() {
      (*lp)((api.clone(), vcmd.clone()));
    }

    (*fnc)((api, vcmd));
    return;
  }

  if let Some(fnc) = lout.listen_none_command() {
    (*fnc)((api, vcmd))
  }
}


fn extra_command(text: &String) -> (String, Vec<String>) {
  let mut reader = TextReader::new(text);
  let mut command = "".to_string();
  let mut builder = StringBuilder::new();
  let mut args = Vec::new();

  let mut entry_command = false;
//  let mut check_command = false;
  let mut quote_ch = None;
  let mut entry_quote = false;
  while reader.has_next() {
    match reader.next() {
      Some('/') => {
        if reader.position() == 1 {
          entry_command = true;
          continue;
        }
        builder.append('/');
      }
      Some('\'') => {
        if entry_command {
          builder.append('\'');
          continue;
        }
        if entry_quote {
          if quote_ch == Some('\'') {
            quote_ch = None;
            entry_quote = false;
            continue;
          }
          builder.append('\'');
          continue;
        }
        quote_ch = Some('\'');
        entry_quote = true;
        continue;
      }
      Some('"') => {
        if entry_command {
          builder.append('"');
          continue;
        }
        if entry_quote {
          if quote_ch == Some('"') {
            quote_ch = None;
            entry_quote = false;
            continue;
          }
          builder.append('"');
          continue;
        }
        quote_ch = Some('"');
        entry_quote = true;
        continue;
      }
      Some(' ') => {
        if entry_quote {
          builder.append(' ');
          continue;
        }
        if entry_command {
          entry_command = false;
//          check_command = true;
          command = builder.string();
          builder.clear();
          continue;
        }
        if !entry_command {
          if builder.is_empty() {
            continue;
          }
          args.push(builder.string());
          builder.clear();
          continue;
        }
        continue;
      }
      Some(ch) => {
        builder.append(ch);
      }
      _ => {}
    }
  }

  if entry_command {
    command = builder.string();
  } else {
    args.push(builder.string());
  }
  builder.clear();


  if command.contains("@") {
    let mut reader = TextReader::new(command);
    while reader.has_next() {
      match reader.next() {
        Some('@') => {
          if reader.next() == Some('@') {
            builder.append('@');
            reader.back();
            continue;
          }
          break;
        }
        Some(ch) => builder.append(ch),
        None => continue,
      };
    }
    let command = builder.string();
    builder.clear();
    return (command, args);
  }

  (command, args)
}