mailcvt 0.2.20 → 0.2.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/encoded_word.rb CHANGED
@@ -16,14 +16,14 @@ class EncodedWord
16
16
  puts f
17
17
  File.open(f + '.plain', 'w') do |out|
18
18
  File.readlines(f).each do |line|
19
- parts = line.split("\t")
19
+ parts = mysplit(line)
20
20
  newparts = []
21
21
  newparts << format_date(parts[0]) #date
22
22
  newparts << parts[1] #message-id
23
23
  newparts << decode_subject(parts[2]) #subject
24
- newparts << trim_email(parts[3]) #from
25
- newparts << trim_email(parts[4]) #to
26
- newparts << trim_email(parts[5]) #cc
24
+ newparts << trim_emails(parts[3]) #from
25
+ newparts << trim_emails(parts[4]) #to
26
+ newparts << trim_emails(parts[5]) #cc
27
27
  newparts << decode_attachments(parts[6]) unless parts.length == 6 #attachment
28
28
  out.puts newparts.join("\t")
29
29
  end
@@ -31,38 +31,72 @@ class EncodedWord
31
31
  end
32
32
  end
33
33
 
34
+ def mysplit(line, sep = "\t")
35
+ return [] unless line
36
+ return [] unless line.length > 0
37
+
38
+ parts = []
39
+ pos1 = -1
40
+ while true do
41
+ pos1 += 1
42
+ pos2 = line.index("\t", pos1)
43
+ if pos2
44
+ parts << line[pos1...pos2]
45
+ pos1 = pos2
46
+ else
47
+ parts << line[pos1..-1]
48
+ break
49
+ end
50
+ end
51
+ return parts
52
+ end
53
+
34
54
  def format_date(engdate)
55
+ return '' unless engdate and engdate.length > 0
35
56
  dt = DateTime.parse(engdate)
36
57
  dt = dt.new_offset('+0900')
37
58
  dt.strftime("%Y/%m/%d %H:%M:%S")
38
59
  end
39
60
 
40
61
  def decode_subject(sub)
62
+ return '' unless sub and sub.length > 0
41
63
  one = sub.gsub("\a", '')
42
64
  mime_decode(one)
43
65
  end
44
66
 
45
67
  def trim_emails(emails)
46
- parts = emails.split("\a")
68
+ return '' unless emails and emails.length > 0
69
+ pos1 = -1
47
70
  newparts = []
48
- parts.each do |p|
49
- newparts << trim_email(p)
71
+ while true do
72
+ pos1 += 1
73
+ pos2 = emails.index('@', pos1)
74
+ break unless pos2
75
+ newparts << getmail(emails, pos2)
76
+ pos1 = pos2
50
77
  end
51
78
  newparts.join("\a")
52
79
  end
53
80
 
54
- def trim_email(email)
55
- pos1 = email.index('<')
56
- if pos1
57
- pos1 += 1
58
- pos2 = email.index('>', pos1)
59
- return email[pos1...pos2]
81
+ def getmail(line, at)
82
+ pos1 = 0
83
+ pos2 = line.length - 1
84
+ (at-1).step(0, -1) do |i|
85
+ next if line[i] =~ /[\._a-zA-Z0-9-]/
86
+ pos1 = i + 1
87
+ break
88
+ end
89
+ (at+1).upto(line.length) do |i|
90
+ next if line[i] =~ /[\.a-zA-Z0-9-]/
91
+ pos2 = i - 1
92
+ break
60
93
  end
61
- email.strip
94
+ line[pos1..pos2]
62
95
  end
63
96
 
64
97
  def decode_attachments(attachments)
65
- parts = attachments.split("\a")
98
+ return '' unless attachments and attachments.length > 0
99
+ parts = mysplit(attachments, "\a")
66
100
  newparts = []
67
101
  parts.each do |p|
68
102
  newparts << mime_decode(p)
@@ -71,6 +105,7 @@ class EncodedWord
71
105
  end
72
106
 
73
107
  def mime_decode(input, out_charset = 'utf-8')
108
+ return '' unless input and input.length > 0
74
109
  ret = input.sub!(/=\?([A-Za-z0-9_-]+)\?([BQbq])\?([^\?]+)\?=/) {
75
110
  charset = $1
76
111
  enc = $2.upcase
@@ -1,3 +1,3 @@
1
1
  module Mailcvt
2
- VERSION = '0.2.20'
2
+ VERSION = '0.2.21'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mailcvt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.20
4
+ version: 0.2.21
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-10-03 00:00:00.000000000 Z
12
+ date: 2013-10-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake