rmbox 0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +56 -0
- data/lib/rmbox.rb +164 -0
- data/rmbox.gemspec +29 -0
- metadata +47 -0
data/README.md
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
#Ruby MailBox file management (rmbox)
|
2
|
+
|
3
|
+
Another library for manage a mailbox as object...
|
4
|
+
|
5
|
+
##reading a mailbox
|
6
|
+
|
7
|
+
The new method create a new instance, classic:
|
8
|
+
|
9
|
+
require 'rmbox'
|
10
|
+
mbox=Mailbox.new '/var/mail/fulano'
|
11
|
+
|
12
|
+
If the mailbox do now exit, will be created. I suggest to write some email or you'll have an empty file :D
|
13
|
+
|
14
|
+
##reading emails
|
15
|
+
|
16
|
+
Each mailbox has a mail number. Lets get the mail number 3 from the mailbox.
|
17
|
+
|
18
|
+
# return the amount of mails in the mailbox
|
19
|
+
mbox.mails
|
20
|
+
=> 18
|
21
|
+
|
22
|
+
# get the mail number 3 as is in the mailbox
|
23
|
+
@myemail=mbox.get 3
|
24
|
+
|
25
|
+
If you mailbox change and you wanna reflect thats changes in you mailbox object, use the reread method:
|
26
|
+
|
27
|
+
# read the mailbox file again
|
28
|
+
mbox.reread
|
29
|
+
|
30
|
+
Ask a mail number bigger than existent mails will raise an exception. If your mailbox is not to big, you could use ".to_a" method for get an array with all the emails:
|
31
|
+
|
32
|
+
# get all the emails as an array
|
33
|
+
mbox.to_a
|
34
|
+
|
35
|
+
Then you can use, last and/or [-1] etc... but, be careful with big mailboxes and remember that you having a common array.
|
36
|
+
|
37
|
+
##writing a mail to your mailbox
|
38
|
+
|
39
|
+
Write in the mailbox is append a mail to the mailbox.
|
40
|
+
|
41
|
+
# add a email to the Mailbox
|
42
|
+
mbox.append @myemail
|
43
|
+
|
44
|
+
# duplicate the first email
|
45
|
+
mbox.append(mbox.get 1)
|
46
|
+
|
47
|
+
Where @myemail is a rfc822 mail formated string. Mailbox will be "rereaded" after and before append any new mail. Remember use <b>.append</b> instead of write. Changes are updated (write) automatically.
|
48
|
+
|
49
|
+
##deleting mails
|
50
|
+
|
51
|
+
Is very easy, just like any email client do it. Which mail number shall deleted?
|
52
|
+
|
53
|
+
# delete mail number 2
|
54
|
+
mbox.delete 2
|
55
|
+
|
56
|
+
|
data/lib/rmbox.rb
ADDED
@@ -0,0 +1,164 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#encoding: utf-8
|
3
|
+
|
4
|
+
class Mailbox # handle mailbox files as object
|
5
|
+
|
6
|
+
# read or create a mailbox file
|
7
|
+
def initialize fichero=false
|
8
|
+
|
9
|
+
if fichero==false # if not mailbox was especified, not argument gived
|
10
|
+
raise ArgumentError, 'argument must be a valid absolute path file'
|
11
|
+
end
|
12
|
+
|
13
|
+
# we'll store ALL the mails here
|
14
|
+
# TERRIBLE ram usage if the mbox is a big file
|
15
|
+
@@correos={}
|
16
|
+
|
17
|
+
# take file's path
|
18
|
+
@@fichero=fichero
|
19
|
+
|
20
|
+
# file must exist an be readable or creations enabled
|
21
|
+
if File.exist?(fichero)
|
22
|
+
|
23
|
+
# can I read it?
|
24
|
+
if File.readable? fichero
|
25
|
+
|
26
|
+
# open and read the file
|
27
|
+
@@mbox=File.read fichero
|
28
|
+
|
29
|
+
unless @@mbox.length==0 # file must not empty
|
30
|
+
|
31
|
+
# must be a valid mailbox file
|
32
|
+
unless @@mbox.lines.to_a[0].downcase.start_with? 'from '
|
33
|
+
raise ArgumentError, 'argument must be a valid mailbox file'
|
34
|
+
end # unless
|
35
|
+
|
36
|
+
# split the file by the 'From ', first object is a "" skip it
|
37
|
+
@@mbox.lines.each do |li|
|
38
|
+
|
39
|
+
# take the 'From ' where split each mail at the mailbox file
|
40
|
+
@key=li if li.downcase.start_with? 'from ' and /from .* \d\d:\d\d:\d\d \d\d\d\d\n/===li.downcase
|
41
|
+
|
42
|
+
# fill the value with the email
|
43
|
+
unless @@correos.keys.include? @key
|
44
|
+
@@correos[@key]=''
|
45
|
+
else
|
46
|
+
@@correos[@key]+=li
|
47
|
+
end # unless
|
48
|
+
|
49
|
+
end # .each do
|
50
|
+
|
51
|
+
end # unless empty
|
52
|
+
|
53
|
+
else # some mailbox can be listed but not readed
|
54
|
+
raise ArgumentError, "Can't read the file #{fichero}. Permission?"
|
55
|
+
|
56
|
+
end # if File.readable?
|
57
|
+
|
58
|
+
else # try to write it
|
59
|
+
|
60
|
+
begin # create the file
|
61
|
+
File.write @@fichero,''
|
62
|
+
|
63
|
+
rescue # warn it if something get wrong
|
64
|
+
raise Exception, "can't write the file #{@@fichero}"
|
65
|
+
|
66
|
+
end # begin
|
67
|
+
|
68
|
+
end # if File.exist?
|
69
|
+
|
70
|
+
end # initialize
|
71
|
+
|
72
|
+
# return the number of email in the mbox
|
73
|
+
def mails
|
74
|
+
@@correos.values.length
|
75
|
+
end # mails
|
76
|
+
|
77
|
+
# read again the email
|
78
|
+
def reread
|
79
|
+
# reaload everything
|
80
|
+
# this is a very dirty operation
|
81
|
+
initialize @@fichero
|
82
|
+
end # reread
|
83
|
+
|
84
|
+
|
85
|
+
# return mail number num
|
86
|
+
def get num
|
87
|
+
|
88
|
+
if mails==0 # if has not mails, tell it
|
89
|
+
raise ArgumentError, 'Mailbox is empty'
|
90
|
+
elsif num.to_i==0 or num.to_i>mails
|
91
|
+
raise ArgumentError, "Give me a valid mail number from 1 to #{mails}"
|
92
|
+
end # if
|
93
|
+
|
94
|
+
# took the mail number "num" from mailbox
|
95
|
+
@@correos.values[num-1]
|
96
|
+
|
97
|
+
end # get
|
98
|
+
|
99
|
+
# return an array with all the mails
|
100
|
+
def to_a
|
101
|
+
return @@correos.values
|
102
|
+
end # to_a
|
103
|
+
|
104
|
+
# not move if file is not writable
|
105
|
+
def writable?
|
106
|
+
return true if File.writable?(@@fichero)
|
107
|
+
return false
|
108
|
+
end # writable?
|
109
|
+
|
110
|
+
# append a mail to the mailbox file
|
111
|
+
# argument must be RFC822 email as string
|
112
|
+
def append mail
|
113
|
+
|
114
|
+
raise Exception, "Fichero #{@@fichero} is not writable" unless writable?
|
115
|
+
|
116
|
+
# read again the email
|
117
|
+
reread
|
118
|
+
|
119
|
+
# generate a from based in the local user
|
120
|
+
@from=Time.now.strftime "From #{ENV['USER']} %a %b %d %T %Y\n"
|
121
|
+
|
122
|
+
|
123
|
+
# we'll append the email in the file
|
124
|
+
@fichero=File.open(@@fichero,'a')
|
125
|
+
@fichero.write @from+mail
|
126
|
+
@fichero.close
|
127
|
+
|
128
|
+
# read again the email
|
129
|
+
reread
|
130
|
+
|
131
|
+
end # append
|
132
|
+
|
133
|
+
# remove the mail number num from the mailbox
|
134
|
+
def delete num
|
135
|
+
|
136
|
+
# can't delete emails in a not writable mailbox file
|
137
|
+
raise Exception, "Fichero #{@@fichero} is not writable" unless writable?
|
138
|
+
|
139
|
+
reread # sync!
|
140
|
+
|
141
|
+
# can't parse a email out of range, and out or range is NOT the last email
|
142
|
+
if num.to_i==0 or num>mails
|
143
|
+
raise ArgumentError, "argument must be a valid mail's number from 1 to #{mails}"
|
144
|
+
end
|
145
|
+
|
146
|
+
|
147
|
+
# remove the email from the block
|
148
|
+
@@correos.delete(@@correos.keys[num-1])
|
149
|
+
|
150
|
+
# each From plus their mail
|
151
|
+
@mailbox=String.new
|
152
|
+
@@correos.each do|from,mail|
|
153
|
+
@mailbox<<from+mail
|
154
|
+
end # do
|
155
|
+
|
156
|
+
# write in to the disk
|
157
|
+
File.write @@fichero,@mailbox
|
158
|
+
|
159
|
+
# and again
|
160
|
+
reread
|
161
|
+
|
162
|
+
end # delete
|
163
|
+
|
164
|
+
end # class Mailbox
|
data/rmbox.gemspec
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# autogeneramos la version a patitir de los commit de git
|
2
|
+
commit=File.read('.git/logs/HEAD').lines.to_a.length.to_s
|
3
|
+
if commit.length == 1
|
4
|
+
version="0.#{commit}"
|
5
|
+
else
|
6
|
+
version=commit[0..-2]+'.'+commit[-1]
|
7
|
+
end
|
8
|
+
|
9
|
+
# los ficheros que le pondremos al s.files
|
10
|
+
ficheros=[]
|
11
|
+
Dir.glob('*').each {|fi| ficheros << fi} unless Dir.glob('*')==[]
|
12
|
+
Dir.glob('*/*').each {|fi| ficheros << fi} unless Dir.glob('*/*')==[]
|
13
|
+
|
14
|
+
|
15
|
+
|
16
|
+
# podemos hacer un iterador que busca la ceadena 'description' en comentarios
|
17
|
+
# y la tome como descripcion, de esa misma form con el sumario
|
18
|
+
|
19
|
+
Gem::Specification.new do |s|
|
20
|
+
s.name = 'rmbox'
|
21
|
+
s.version = version
|
22
|
+
s.date = Time.now.to_s.split(' ')[0]
|
23
|
+
s.description = 'Manage Mailbox files using ruby'
|
24
|
+
s.summary = 'Manage Mailbox files using ruby'
|
25
|
+
s.authors = 'Lázaro Armando'
|
26
|
+
s.email = 'uranio-235@myopera.com'
|
27
|
+
s.files = ficheros.uniq
|
28
|
+
s.homepage = 'http://github.com/uranio-235/dev/rmbox'
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rmbox
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.4'
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Lázaro Armando
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-09-16 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Manage Mailbox files using ruby
|
15
|
+
email: uranio-235@myopera.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- rmbox.gemspec
|
21
|
+
- README.md
|
22
|
+
- lib/rmbox.rb
|
23
|
+
homepage: http://github.com/uranio-235/dev/rmbox
|
24
|
+
licenses: []
|
25
|
+
post_install_message:
|
26
|
+
rdoc_options: []
|
27
|
+
require_paths:
|
28
|
+
- lib
|
29
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ! '>='
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
36
|
+
none: false
|
37
|
+
requirements:
|
38
|
+
- - ! '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
requirements: []
|
42
|
+
rubyforge_project:
|
43
|
+
rubygems_version: 1.8.23
|
44
|
+
signing_key:
|
45
|
+
specification_version: 3
|
46
|
+
summary: Manage Mailbox files using ruby
|
47
|
+
test_files: []
|