censor_bear 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3b7bf815d7375b755966c23467029b2219e3a3c3645bc93d0796b9afb0720bdc
4
- data.tar.gz: 82a58299728acbeca22d1bf5ec0739ed2a07d4150e6c48639decc0a652f9480a
3
+ metadata.gz: 0cdef69a9d04136c6116ed8245122bda98d27849a91e313487282be5d0640c4d
4
+ data.tar.gz: 5088b2e4f868403f242b62344af06edbe47daf4e90bbbaa264848e7c982e574d
5
5
  SHA512:
6
- metadata.gz: c403de59e6da357406191a1cbbeecba4dc0b1fcadd5fca74ffdd6ba32a3fca4f63fdd0c0b32d02db6956916e28bb998ca180080eaa6c0296235fbf731d58b7de
7
- data.tar.gz: 84e32c7fc6e3d029829fcbc6bf65da0901a816e7a411a37cb09beb75cf2e0fecd2de9a929c708038f40b9476e5d725becc449539f737a8d734f168e7d8063eaa
6
+ metadata.gz: 3b9138dc7b8af468e6fb59960f1bdc6801927c986758c90d709f49ce408b4a6127bf520e5480198c667717f24a2bfc9cd657938e385a5440c3c811862f47f682
7
+ data.tar.gz: 80966dea001f69dd7db9a1a64d6c687a67062a39fd01b2809a3a1c5d9b028b2066526e69594730805f76c975c49255da8974e6063e2446e7136aa2cf8bd61d89
@@ -12,5 +12,7 @@ class CreateCensorBearStopWords < ActiveRecord::Migration[6.1]
12
12
 
13
13
  t.timestamps
14
14
  end
15
+
16
+ add_index :censor_bear_stop_words, :key, unique: true
15
17
  end
16
18
  end
@@ -10,6 +10,9 @@ module CensorBear
10
10
  end
11
11
 
12
12
  class Censor
13
+ QQ_REG = /(?:[加茄qQ企鹅号码\s]{1,}|[群号]{1,}|[叩叩]{1,}|[抠抠]{1,}|[扣扣]{1,})(?:[\u4e00-\u9eff]*)(?:[:,:]?)([\d\s]{6,})/
14
+ WX_REG = /(?:[加+微++➕薇?vV威卫星♥❤姓xX信]{2,}|weixin|weix)(?:[,❤️.\s]?)(?:[\u4e00-\u9eff]?)(?:[:,:]?)([\w\s]{6,})/
15
+
13
16
  def initialize(content, type = 'ugc')
14
17
  @is_mod = false
15
18
  @mod_words = []
@@ -21,6 +24,27 @@ module CensorBear
21
24
  return Result.new(@content) if @content.blank?
22
25
  return Result.new(@content) unless CensorBear::StopWord::FIELDS.include?(@type)
23
26
 
27
+ if @content.match(QQ_REG)
28
+ CensorBear::Log.create(
29
+ original_content: @content,
30
+ action: "#{@type}_regex_qq",
31
+ filtered_content: nil,
32
+ mod_words: @mod_words
33
+ )
34
+ raise NotPassedException
35
+ end
36
+
37
+ if @content.match(WX_REG)
38
+ @is_mod = true
39
+ CensorBear::Log.create(
40
+ original_content: @content,
41
+ action: "#{@type}_regex_wx",
42
+ filtered_content: nil,
43
+ mod_words: @mod_words
44
+ )
45
+ raise NotPassedException
46
+ end
47
+
24
48
  local_check
25
49
 
26
50
  # 用户相关类型直接封禁,速错
@@ -8,5 +8,10 @@ module CensorBear
8
8
 
9
9
  # user name method, default: "name"
10
10
  attr_accessor :user_name_method
11
+
12
+ # import task file locaation
13
+ attr_accessor :import_file_path
14
+ attr_accessor :format_from_file_path
15
+ attr_accessor :format_dest_file_path
11
16
  end
12
17
  end
@@ -1,3 +1,3 @@
1
1
  module CensorBear
2
- VERSION = '0.1.0'
2
+ VERSION = '0.1.1'
3
3
  end
data/lib/censor_bear.rb CHANGED
@@ -17,6 +17,9 @@ module CensorBear
17
17
  @config.user_class = 'User'
18
18
  @config.current_user_method = 'current_user'
19
19
  @config.user_name_method = 'name'
20
+ @config.import_file_path = "#{Rails.root}/tmp/dest_sensitives.txt"
21
+ @config.format_from_file_path = "#{Rails.root}/tmp/from_sensitives.txt"
22
+ @config.format_dest_file_path = "#{Rails.root}/tmp/dest_sensitives.txt"
20
23
  @config
21
24
  end
22
25
 
@@ -1,4 +1,58 @@
1
- # desc "Explaining what the task does"
2
- # task :censor_bear do
3
- # # Task goes here
4
- # end
1
+ namespace :censor_bear do
2
+ desc 'Format words'
3
+ task format: :environment do
4
+ from_file = File.read(CensorBear.config.format_from_file_path)
5
+ File.open(CensorBear.config.format_dest_file_path, 'w') do |f|
6
+ from_file.each_line do |word|
7
+ line = "#{word.strip!}={BANNED}|{BANNED}|{BANNED}|{BANNED}\r\n"
8
+ f.write(line)
9
+ f.flush
10
+ end
11
+ end
12
+ end
13
+
14
+ desc 'Import words'
15
+ task import: :environment do
16
+ is_overwrite = true
17
+ file = File.read(CensorBear.config.import_file_path)
18
+ # lines = doc.split(/\n/)
19
+ file.each_line do |line|
20
+ line.strip!
21
+ key, rule_str = line.split('=')
22
+ rules = rule_str.split('|')
23
+ ugc = parse(rules[0]).blank? ? 'replace' : parse(rules[0])
24
+ username = parse(rules[1]) || 'ignore'
25
+ signature = parse(rules[2]) || 'ignore'
26
+ dialog = parse(rules[3]) || 'ignore'
27
+ nickname = parse(rules[1]) || 'ignore'
28
+ replacement = ugc == 'replace' ? rules[0] : '**'
29
+
30
+ payload = {
31
+ key: key,
32
+ ugc: ugc,
33
+ username: username,
34
+ signature: signature,
35
+ dialog: dialog,
36
+ nickname: nickname,
37
+ replacement: replacement,
38
+ user_id: User.last&.id,
39
+ created_at: Time.now,
40
+ updated_at: Time.now
41
+ }
42
+
43
+ word = CensorBear::StopWord.find_by(key: key)
44
+ if word
45
+ word.update_columns(payload) if is_overwrite
46
+ else
47
+ CensorBear::StopWord.create(payload)
48
+ end
49
+ end
50
+ end
51
+
52
+ def parse(rule)
53
+ return nil if rule.blank?
54
+
55
+ r = rule.match(/^{(\w+)}$/)
56
+ r.blank? ? nil : r[1].downcase
57
+ end
58
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: censor_bear
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - 42up
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-11-30 00:00:00.000000000 Z
11
+ date: 2021-12-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails