File: rsendmail.rb

package info (click to toggle)
rubyfilter 0.12-2.1
  • links: PTS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 688 kB
  • sloc: ruby: 3,228; makefile: 64
file content (161 lines) | stat: -rw-r--r-- 4,596 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/env ruby
#--
#   Copyright (C) 2002 Matt Armstrong.  All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. The name of the author may not be used to endorse or promote
#    products derived from this software without specific prior
#    written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
# GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
# IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
require 'rmail/parser'
require 'rmail/address'
require 'rmail/serialize'

def syslog(str)
  system('logger', '-p', 'mail.info', '-t', 'rsendmail', str.to_s)
end

extract_recipients = false
sender = nil
full_name = nil
single_dot_ends = true
end_of_options = false
recipients = []
while not ARGV.empty?
  arg = ARGV.shift
  if ! recipients.empty? || arg =~ /\A[^-]/ || end_of_options
    recipients.push(arg)
  else
    case arg
    when '-t'
      extract_recipients = true
    when '-f'
      sender = ARGV.shift
    when '-F'
      full_name = ARGV.shift
    when '-oi'
      single_dot_ends = false
    when '--'
      end_of_options = true
    else
      $stderr.puts("rsendmail: invalid option -- #{arg}")
      exit 1
    end
  end
end

# FIXME: parsing messages should be external to the RMail::Message
# class.  For example, we should differentiate here between -oi and
# not -oi.
message = RMail::Parser.new.parse($stdin)

def get_recipients(message, field_name, list)
  unless message.header[field_name].nil?
    RMail::Address.parse(message.header[field_name]).each do |address|
      # FIXME: need an "smtpaddress" method
      list.push(address.address)
    end
  end
end

if extract_recipients
  get_recipients(message, 'to',  recipients)
  get_recipients(message, 'cc',  recipients)
  get_recipients(message, 'bcc', recipients)
end

# FIXME: put this into some kind of library

# FIXME: simplify verp recipients?

# FIXME: share with .rdeliver
def with_db(name)
  require 'gdbm'
  begin
    db = nil
    begin
      db = GDBM::open(File.join("/home/matt/.rfilter/var", name), 0600)
    rescue Errno::EWOULDBLOCK
      # FIXME: only wait so long, then defer
      sleep(2)
      retry
    end
    yield db
  ensure
    db.close unless db.nil?
  end
end

# FIXME: share with .rdeliver
def record_string_in_db(db, address)
  record = db[address]
  count = record.split(/:/)[1] unless record.nil?
  count ||= '0'
  db[address] = Time.now.strftime("%Y%m%d") + ':' + count.succ
end

with_db('sent-recipient') do |db|
  dup = {}
  recipients.each do |r|
    address = r.downcase
    record_string_in_db(db, address) unless dup.key?(address)
    dup[address] ||= 1
  end
end

with_db "sent-subjects" do |db|
  if subject = message.header['subject']
    subject = subject.strip.downcase
    record_string_in_db(db, subject)
  end
end

with_db "sent-msgid" do |db|
  if msgid = message.header['message-id']
    msgid = msgid.strip.downcase
    record_string_in_db(db, msgid)
  end
end


# FIXME: delete any bcc headers

# FIXME: should be able to generate a default From: header
#raise 'no from header' if message.header['from'].nil?

# FIXME: more error checking here

IO.popen('-', 'w') do |child|
  if child.nil?
    # FIXME: instead of 'address' need a way to output the address for
    # SMTP purposes.
    command = ['/usr/sbin/sendmail', '-oi']
    command.concat(['-F', full_name]) if full_name
    command.concat(['-f', sender]) if sender
    command.concat(recipients)
    #syslog("args ouggoing: " + command.inspect)
    exec(*command)
  else
    RMail::Serialize.new(child).serialize(message)
  end
end