/
elm2trojita.rb
executable file
·160 lines (125 loc) · 3.59 KB
/
elm2trojita.rb
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
#!/usr/bin/ruby
###========================================================================###
#
# Author: David Billsbrough <billsbrough@gmail.com>
# Created: Thursday, May 07, 2015 at 13:09:46 PM (EDT)
# License: GNU General Public License -- version 2
# Warranty: None
# Version: $Revision: 0.2 $
#
# Purpose: Convert an Elm address book for use with the Trojita imap client
#
# $Id: elm2trojita.rb,v 0.2 2015/05/07 17:29:05 kc4zvw Exp kc4zvw $
#
###========================================================================###
# vim: tabstop=4:expandtab:shiftwidth=4:syntax=ruby:
def get_home_dir
myHOME = ENV["HOME"]
print "My $HOME directory is #{myHOME}.\n"
return myHOME
end
def get_timestamp
""" Date Format: 04-16-2006 at 17:57:11 """
tm = Time.now
return sprintf("%02d-%02d-%04d at %02d:%02d:%02d",
tm.mon, tm.mday, tm.year, tm.hour, tm.min, tm.sec)
end
###------------------------------------------------------------------------###
def delete_comment (name, pos)
return name[0, pos]
end
def get_firstname (name, pos)
return name[pos + 2, 99]
end
def get_lastname (name, pos)
return name[0, pos]
end
def get_fullname (first_name, last_name)
return "#{first_name} #{last_name}"
end
###------------------------------------------------------------------------###
def display_entry(full_name)
puts "Converting '#{full_name}' (#{$.})"
end
def write_header(i, output)
index = i - 1
output.print "[#{index}]\n"
end
def write_trojita(myalias, first, last, email, output)
fullname = get_fullname(first, last)
output.print "name=#{fullname}\n"
output.print "email=#{email}\n"
output.print "address=[address]\n"
output.print "city=[city]\n"
output.print "state=[state]\n"
output.print "zip=[zip]\n"
output.print "country=USA\n"
output.print "phone=n/a\n"
output.print "workphone=\n"
output.print "fax=\n"
output.print "mobile=\n"
output.print "nick=#{myalias}\n"
output.print "url=\n"
output.print "anniversary=none\n"
output.print "\n"
end
###------------------------------------------------------------------------###
def process_line(output, aline, index)
pos1 = pos2 = nil
myalias = name = email = ""
first = last = ""
line = aline.chomp
myalias, name, email = line.split(' = ')
pos1 = name.index(',') # search for a comma
pos2 = name.index(';') # search for a semicolon
if pos1 != nil then
#print "Comma at #{pos1}.\n"
name = delete_comment(name, pos1)
end
if pos2 != nil then
#print "Semicolon at #{pos2}.\n"
first = get_firstname(name, pos2)
last = get_lastname(name, pos2)
name = get_fullname(first, last)
end
display_entry(name) # Display progress
write_header(index, output)
write_trojita(myalias, first, last, email, output) # Write single entry
end
###---------------------------- Main Routine ------------------------------###
home = get_home_dir
elm_path = [ home, ".elm", "aliases.text" ]
output_path = [ home, ".abook", "address_book_2" ]
elm = elm_path.join("/")
addressbook = output_path.join("/")
puts
puts "The Elm mail alias file is #{elm}."
puts "The trojita address book file is #{addressbook}."
puts
begin
input = File.open(elm, mode='r')
rescue
puts "Can not read input mail aliases."
puts "#{$!}"
exit 1
end
begin
output = File.open(addressbook, mode='w')
rescue
puts "Can not write output address book."
puts "#{$!}"
exit 2
end
until input.eof()
line = input.readline
process_line(output, line, $.)
end
input.close
output.close
datestamp = get_timestamp
record_count = $.
puts
print "processed #{record_count} records on #{datestamp}.\n"
puts
puts "Finished."
# End of Program