[milter-manager-commit] milter-manager/milter-manager at ac99ef5 [master] Add a sample Ruby milter that replaces content

Back to archive index
Sutou Kouhei null+****@clear*****
Thu Jun 20 12:00:35 JST 2019


Sutou Kouhei	2019-06-20 12:00:35 +0900 (Thu, 20 Jun 2019)

  Revision: ac99ef5019f83b4836fd07d74b1e752843c51e69
  https://github.com/milter-manager/milter-manager/commit/ac99ef5019f83b4836fd07d74b1e752843c51e69

  Message:
    Add a sample Ruby milter that replaces content

  Added files:
    binding/ruby/sample/milter-replace.rb
  Modified files:
    binding/ruby/sample/Makefile.am

  Modified: binding/ruby/sample/Makefile.am (+2 -1)
===================================================================
--- binding/ruby/sample/Makefile.am    2019-05-13 15:12:26 +0900 (f6534350)
+++ binding/ruby/sample/Makefile.am    2019-06-20 12:00:35 +0900 (cc962450)
@@ -4,4 +4,5 @@ dist_sample_ruby_DATA =				\
 	milter-regexp.rb			\
 	milter-tarpit.rb			\
 	milter-reject-empty-body.rb		\
-	milter-reject-nil-sender.rb
+	milter-reject-nil-sender.rb		\
+	milter-replace.rb

  Added: binding/ruby/sample/milter-replace.rb (+68 -0) 100755
===================================================================
--- /dev/null
+++ binding/ruby/sample/milter-replace.rb    2019-06-20 12:00:35 +0900 (745cbce9)
@@ -0,0 +1,68 @@
+#!/usr/bin/env ruby
+#
+# Copyright (C) 2019  Sutou Kouhei <kou****@clear*****>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+require "milter"
+
+class MilterReplace < Milter::ClientSession
+  def initialize(context, patterns)
+    super(context)
+    @patterns = patterns
+    reset
+  end
+
+  def header(name, value)
+    @headers << [name, value]
+  end
+
+  def body(chunk)
+    @body << chunk
+  end
+
+  def end_of_message
+    header_indexes = {}
+    @headers.each do |name, value|
+      header_indexes[name] ||= 0
+      header_indexes[name] += 1
+      @patterns.each do |pattern, replaced|
+        replaced_value = value.gsub(pattern, replaced)
+        if value != replaced_value
+          change_header(name, header_indexes[name], replaced_value)
+          break
+        end
+      end
+    end
+
+    @patterns.each do |pattern, replaced|
+      replaced_body =****@body*****(pattern, replaced)
+      if @body != replaced_body
+        replace_body(replaced_body)
+      end
+    end
+  end
+
+  def reset
+    @headers = []
+    @body = ""
+  end
+end
+
+command_line = Milter::Client::CommandLine.new
+command_line.run do |client, _options|
+  client.register(MilterReplace, {
+                    /viagra/i => "XXX",
+                  })
+end
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.osdn.me/mailman/archives/milter-manager-commit/attachments/20190620/f39d0d17/attachment-0001.html>


More information about the milter-manager-commit mailing list
Back to archive index