Class: Mailbox
- Inherits:
-
Object
- Object
- Mailbox
- Defined in:
- lib/rmbox.rb
Overview
handle mailbox files as object
Instance Method Summary collapse
-
#append(mail) ⇒ Object
append a mail to the mailbox file argument must be RFC822 email as string.
-
#delete(num) ⇒ Object
remove the mail number num from the mailbox.
-
#get(num) ⇒ Object
return mail number num.
-
#initialize(fichero = false) ⇒ Mailbox
constructor
read or create a mailbox file.
-
#mails ⇒ Object
return the number of email in the mbox.
-
#reread ⇒ Object
read again the email.
-
#to_a ⇒ Object
return an array with all the mails.
-
#writable? ⇒ Boolean
not move if file is not writable.
Constructor Details
#initialize(fichero = false) ⇒ Mailbox
read or create a mailbox file
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 |
# File 'lib/rmbox.rb', line 7 def initialize fichero=false if fichero==false # if not mailbox was especified, not argument gived raise ArgumentError, 'argument must be a valid absolute path file' end # we'll store ALL the mails here # TERRIBLE ram usage if the mbox is a big file @@correos={} # take file's path @@fichero=fichero # file must exist an be readable or creations enabled if File.exist?(fichero) # can I read it? if File.readable? fichero # open and read the file @@mbox=File.read fichero unless @@mbox.length==0 # file must not empty # must be a valid mailbox file unless @@mbox.lines.to_a[0].downcase.start_with? 'from ' raise ArgumentError, 'argument must be a valid mailbox file' end # unless # split the file by the 'From ', first object is a "" skip it @@mbox.lines.each do |li| # take the 'From ' where split each mail at the mailbox file @key=li if li.downcase.start_with? 'from ' and /from .* \d\d:\d\d:\d\d \d\d\d\d\n/===li.downcase # fill the value with the email unless @@correos.keys.include? @key @@correos[@key]='' else @@correos[@key]+=li end # unless end # .each do end # unless empty else # some mailbox can be listed but not readed raise ArgumentError, "Can't read the file #{fichero}. Permission?" end # if File.readable? else # try to write it begin # create the file File.write @@fichero,'' rescue # warn it if something get wrong raise Exception, "can't write the file #{@@fichero}" end # begin end # if File.exist? end |
Instance Method Details
#append(mail) ⇒ Object
append a mail to the mailbox file argument must be RFC822 email as string
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/rmbox.rb', line 112 def append mail raise Exception, "Fichero #{@@fichero} is not writable" unless writable? # read again the email reread # generate a from based in the local user @from=Time.now.strftime "From #{ENV['USER']} %a %b %d %T %Y\n" # we'll append the email in the file @fichero=File.open(@@fichero,'a') @fichero.write @from+mail @fichero.close # read again the email reread end |
#delete(num) ⇒ Object
remove the mail number num from the mailbox
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 162 |
# File 'lib/rmbox.rb', line 134 def delete num # can't delete emails in a not writable mailbox file raise Exception, "Fichero #{@@fichero} is not writable" unless writable? reread # sync! # can't parse a email out of range, and out or range is NOT the last email if num.to_i==0 or num>mails raise ArgumentError, "argument must be a valid mail's number from 1 to #{mails}" end # remove the email from the block @@correos.delete(@@correos.keys[num-1]) # each From plus their mail @mailbox=String.new @@correos.each do|from,mail| @mailbox<<from+mail end # do # write in to the disk File.write @@fichero,@mailbox # and again reread end |
#get(num) ⇒ Object
return mail number num
86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/rmbox.rb', line 86 def get num if mails==0 # if has not mails, tell it raise ArgumentError, 'Mailbox is empty' elsif num.to_i==0 or num.to_i>mails raise ArgumentError, "Give me a valid mail number from 1 to #{mails}" end # if # took the mail number "num" from mailbox @@correos.values[num-1] end |
#mails ⇒ Object
return the number of email in the mbox
73 74 75 |
# File 'lib/rmbox.rb', line 73 def mails @@correos.values.length end |
#reread ⇒ Object
read again the email
78 79 80 81 82 |
# File 'lib/rmbox.rb', line 78 def reread # reaload everything # this is a very dirty operation initialize @@fichero end |
#to_a ⇒ Object
return an array with all the mails
100 101 102 |
# File 'lib/rmbox.rb', line 100 def to_a return @@correos.values end |
#writable? ⇒ Boolean
not move if file is not writable
105 106 107 108 |
# File 'lib/rmbox.rb', line 105 def writable? return true if File.writable?(@@fichero) return false end |