Skip to content

Commit

Permalink
增加一些匹配选项,默认忽略大小写
Browse files Browse the repository at this point in the history
"case_insensitive": true,          // [所有命令] 是否忽略大小写(默认:true)
"multi_line": true,                // [所有命令] 是否开启逐行匹配(默认:true,When enabled, ^ matches the beginning of lines and $ matches the end of lines.)
"unicode": true,                   // [所有命令] 是否开启unicode支持(默认:true,When disabled, character classes such as \w only match ASCII word characters instead of all Unicode word characters)
"octal": true,                     // [所有命令] 是否支持octal语法(默认:false)
"dot_matches_new_line": false      // [所有命令] .是否匹配换行符(默认:true)
  • Loading branch information
owent committed Feb 27, 2019
1 parent 03aa793 commit 839193b
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "wxwork_robotd"
version = "0.3.9"
version = "0.5.0"
authors = ["owent <[email protected]>"]

[dependencies]
Expand Down
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,13 @@
"(help)|(帮助)|(指令列表)": {
"type": "help", // 帮助类型的命令
"description": "help|帮助|指令列表", // 描述,所有的命令都有这个选项,用于help类型命令的输出,如果没有这一项,则会直接输出命令的key(匹配式)
"prefix": "### 可用指令列表\r\n" // 帮助信息前缀
"prefix": "### 可用指令列表\r\n" // 帮助信息前缀
"suffix": "" // 帮助信息后缀
"case_insensitive": true, // [所有命令] 是否忽略大小写(默认:true)
"multi_line": true, // [所有命令] 是否开启逐行匹配(默认:true,When enabled, ^ matches the beginning of lines and $ matches the end of lines.)
"unicode": true, // [所有命令] 是否开启unicode支持(默认:true,When disabled, character classes such as \w only match ASCII word characters instead of all Unicode word characters)
"octal": true, // [所有命令] 是否支持octal语法(默认:false)
"dot_matches_new_line": false // [所有命令] .是否匹配换行符(默认:true)
},
"\\s*(?P<MSG>[^\\r\\n]+)": {
"type": "echo",
Expand Down
1 change: 1 addition & 0 deletions enc.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
��N�~ۀ�g�w{�p,}
7 changes: 6 additions & 1 deletion etc/conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@
"headers": {
"X-TEST": "value"
},
"echo": "已发起HTTP请求,回包内容\r\n{{WXWORK_ROBOT_HTTP_RESPONSE}}"
"echo": "已发起HTTP请求,回包内容\r\n{{WXWORK_ROBOT_HTTP_RESPONSE}}",
"case_insensitive": true,
"multi_line": true,
"unicode": true,
"octal": true,
"dot_matches_new_line": false
},
"访问\\s*(?P<URL>[^\\r\\n]+)": {
"type": "http",
Expand Down
3 changes: 3 additions & 0 deletions input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
! TRAVIS input file
! Created with TRAVIS version compiled at Dec 20 2018 01:24:43
! Input file written at Thu Dec 20 09:42:01 2018.
71 changes: 65 additions & 6 deletions src/wxwork_robot/command.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::HashMap;
use std::sync::Arc;

use regex::Regex;
use regex::{Regex, RegexBuilder};
use serde_json;

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -104,10 +104,34 @@ pub fn read_object_from_json_object<'a>(

pub fn read_bool_from_json_object<'a>(json: &'a serde_json::Value, name: &str) -> Option<bool> {
if let Some(ref x) = json.as_object() {
if let Some(ref v) = x.get(name) {
if let Some(r) = v.as_bool() {
return Some(r);
}
if let Some(v) = x.get(name) {
return match v {
serde_json::Value::Null => None,
serde_json::Value::Bool(r) => Some(*r),
serde_json::Value::Number(r) => {
if let Some(rv) = r.as_i64() {
Some(rv != 0)
} else if let Some(rv) = r.as_u64() {
Some(rv != 0)
} else if let Some(rv) = r.as_f64() {
Some(rv != 0.0)
} else {
Some(false)
}
}
serde_json::Value::String(r) => {
let lc_name = r.to_lowercase();
Some(
lc_name.len() > 0
&& lc_name.as_str() != "false"
&& lc_name.as_str() != "no"
&& lc_name.as_str() != "disable"
&& lc_name.as_str() != "disabled",
)
}
serde_json::Value::Array(r) => Some(r.len() > 0),
serde_json::Value::Object(r) => Some(r.len() > 0),
};
}
}

Expand Down Expand Up @@ -180,7 +204,42 @@ impl WXWorkCommand {
pub fn new(cmd_name: &str, json: &serde_json::Value) -> Option<WXWorkCommand> {
let cmd_data: WXWorkCommandData;
let mut envs_obj = json!({});
let rule_obj = match Regex::new(cmd_name) {
// read_bool_from_json_object
let mut reg_builder = RegexBuilder::new(cmd_name);
reg_builder.case_insensitive(if let Some(v) =
read_bool_from_json_object(json, "case_insensitive")
{
v
} else {
true
});
reg_builder.multi_line(
if let Some(v) = read_bool_from_json_object(json, "multi_line") {
v
} else {
true
},
);
reg_builder.unicode(
if let Some(v) = read_bool_from_json_object(json, "unicode") {
v
} else {
true
},
);
reg_builder.octal(if let Some(v) = read_bool_from_json_object(json, "octal") {
v
} else {
false
});
reg_builder.octal(
if let Some(v) = read_bool_from_json_object(json, "dot_matches_new_line") {
v
} else {
false
},
);
let rule_obj = match reg_builder.build() {
Ok(x) => x,
Err(e) => {
error!("command {} regex invalid: {}\n{}", cmd_name, json, e);
Expand Down
7 changes: 5 additions & 2 deletions src/wxwork_robot/command_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::process::{Command, Stdio};
use std::sync::Arc;
use std::time::{Duration, Instant};

use regex::Regex;
use regex::{Regex, RegexBuilder};

use tokio::util::FutureExt;
use tokio_process::CommandExt;
Expand All @@ -33,7 +33,10 @@ pub struct WXWorkCommandRuntime {
}

lazy_static! {
static ref PICK_AT_RULE: Regex = Regex::new("\\@(?P<AT>[\\B]+)").unwrap();
static ref PICK_AT_RULE: Regex = RegexBuilder::new("\\@(?P<AT>[\\B]+)")
.case_insensitive(false)
.build()
.unwrap();
}

pub fn get_project_name_from_runtime(runtime: &Arc<WXWorkCommandRuntime>) -> Arc<String> {
Expand Down
7 changes: 5 additions & 2 deletions src/wxwork_robot/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::io::Cursor;

use actix_web::HttpResponse;

use regex::Regex;
use regex::{Regex, RegexBuilder};

#[derive(Debug, Clone)]
pub struct WXWorkMessageDec {
Expand Down Expand Up @@ -56,7 +56,10 @@ pub struct WXWorkMessageImageRsp {
}

lazy_static! {
static ref PICK_WEBHOOK_KEY_RULE: Regex = Regex::new("key=(?P<KEY>[\\d\\w\\-_]+)").unwrap();
static ref PICK_WEBHOOK_KEY_RULE: Regex = RegexBuilder::new("key=(?P<KEY>[\\d\\w\\-_]+)")
.case_insensitive(false)
.build()
.unwrap();
}

pub fn get_msg_encrypt_from_bytes(bytes: Bytes) -> Option<String> {
Expand Down

0 comments on commit 839193b

Please sign in to comment.