Top Level Namespace

Constant Summary collapse

BCBIFF_VERSION =
'0.3.0'
CERTS_PATHS =
[
  '/etc/ssl/cert.pem',		# FreeBSD
  '/etc/ssl/certs',		# Ubuntu
  '/usr/share/ssl/certs',	# RHEL4
  '/etc/pki/tls/certs',		# RHEL5
  '/opt/local/share/curl/curl-ca-bundle.crt',	# OS X; MacPorts
]
CERTS_PATH =
CERTS_PATHS.find { |f| File.exist?(f) }
IDCACHE_FILE =
'~/Maildir/idcache.%s.yml'
IDCACHE_SIZE =
100
BCBIFF_FILE =
'~/.bcbiff'
ADDRESSES_FILE =
'~/.addresses'

Instance Method Summary collapse

Instance Method Details

#certs_pathObject



129
130
131
# File 'lib/bcbiff.rb', line 129

def certs_path
  $certs_path ||= config[:certs_path] || CERTS_PATH
end

#check_mails(options) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/bcbiff.rb', line 161

def check_mails(options)
  #Net::IMAP.debug = true
  mailto = options[:mailto]
  msgids_file = File.expand_path(IDCACHE_FILE % mailto)

  File.open(msgids_file, File::RDWR | File::CREAT, 0600) {|f|
    f.flock(File::LOCK_EX | File::LOCK_NB) or break

    msgids = YAML.load(f) || []

    imap = Net::IMAP.new(options[:host], options[:port], options[:ssl], certs_path, true)
    imap.(options[:username], options[:password])
    folders = options[:folders] || ['Inbox']

    folders.each { |folder|
      imap.select(folder)
      unseen = imap.search('UNSEEN')
      next if unseen.empty?

      imap.fetch(unseen, item = 'RFC822.HEADER').each { |data|
        mail = Mail.read_from_string(data.attr[item])
        msgid = mail.message_id
        next if msgids.include?(msgid)
        msgids << msgid

        (header = mail.header).fields.map(&:name).each { |name|
          case name
          when /\A(From|Subject|Date)\z/i
            # preserve
          else
            header[name] = nil
          end
        }

        mail.from = mail[:from].field.addrs.map { |addr|
          $display_address[addr.address.downcase] || addr.to_s
        }.join(', ')

        begin
          encoded = mail.encoded
          open("| sendmail #{mailto.shellescape}", 'w') { |sendmail|
            sendmail.print encoded
          }
        rescue => e
          STDERR.puts "%s: %s" % [msgid, e.message]
        end
      }
    }
    msgids.slice!(0...-IDCACHE_SIZE) if msgids.size > IDCACHE_SIZE

    f.rewind
    f.print YAML.dump(msgids)
    f.truncate(f.pos)
  }
end

#configObject



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
# File 'lib/bcbiff.rb', line 96

def config
  $config ||=
    begin
      value = YAML.load_file(File.expand_path(BCBIFF_FILE))
      raise unless value.is_a?(Hash) && value.key?(:accounts)
      value
    end
rescue
  STDERR.puts "Put your configuration in #{BCBIFF_FILE} that looks like below.", ''
  STDERR.print YAML.dump({
    :accounts => [
        {
          :host => 'imap.gmail.com',
          :port => 993,
          :ssl  => true,
          :username => 'your.account',
          :password => 'password1',
          :mailto => '[email protected]',
        },
        {
          :host => 'imap.gmail.com',
          :port => 993,
          :ssl  => true,
          :username => '[email protected]',
          :password => 'password2',
          :mailto => '[email protected]',
          :folders => %w[Inbox work/important],
        }
    ]
  })
  exit 1
end

#main(argv) ⇒ Object



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
# File 'lib/bcbiff.rb', line 58

def main(argv)
  accounts = config[:accounts]

  maildir = File.dirname(File.expand_path(IDCACHE_FILE))
  Dir.mkdir(maildir, 0700) unless File.directory?(maildir)

  if accounts.any? { || [:ssl] } && !certs_path
    STDERR.print <<-EOS
The system path for SSL certificates is not found.
Install SSL certificates in one of the following locations:
    EOS
    CERTS_PATHS.each { |path|
      STDERR.puts "\t#{path}"
    }
    STDERR.print <<-EOS if RUBY_PLATFORM =~ /darwin/

If you are on OS X and have MacPorts installed, running the
following command is an easy way to have one installed:
\tport install curl-ca-bundle
    EOS
    STDERR.print <<-EOS

Otherwise, refer to the following site and place the pem
file somewhere:
\thttp://curl.haxx.se/docs/caextract.html

Then add `:certs_path: /path/to/pem` to #{BCBIFF_FILE}.
    EOS
    exit 1
  end

  read_addresses

  accounts.each { |options|
    check_mails(options)
  }
end

#read_addressesObject



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
# File 'lib/bcbiff.rb', line 133

def read_addresses
  $display_address = {}

  begin
    content = File.read(ADDRESSES_FILE)
  rescue
    return
  end

  content.each_line { |line|
    line.chomp!
    address, nickname, fullname = line.split("\t").map { |field|
      if m = field.match(/\A"(.*)"\z/)
        m[1].gsub(/\\(.)/, "\\1")
      else
        field
      end
    }
    begin
      addr = Mail::Address.new(address)
      addr.display_name = nickname
      $display_address[address.downcase] = addr.to_s
    rescue
    end
  }
rescue
end