Class: PopThis::POP3Server
- Inherits:
-
GServer
- Object
- GServer
- PopThis::POP3Server
- Defined in:
- lib/pop_this.rb
Defined Under Namespace
Classes: Email
Instance Method Summary collapse
- #dele(msgid) ⇒ Object
- #emails ⇒ Object
-
#initialize(options) ⇒ POP3Server
constructor
A new instance of POP3Server.
- #list(msgid = nil) ⇒ Object
- #process_line(line) ⇒ Object
- #quit ⇒ Object
- #retr(msgid) ⇒ Object
- #rset ⇒ Object
- #serve(io) ⇒ Object
- #stat ⇒ Object
Constructor Details
#initialize(options) ⇒ POP3Server
Returns a new instance of POP3Server.
6 7 8 9 10 |
# File 'lib/pop_this.rb', line 6 def initialize() @hostname = [:host] super([:port]) @dir = [:path] end |
Instance Method Details
#dele(msgid) ⇒ Object
93 94 95 96 97 |
# File 'lib/pop_this.rb', line 93 def dele(msgid) msgid = msgid.to_i return false if msgid > @emails.length @emails[msgid-1].deleted = true end |
#emails ⇒ Object
59 60 61 |
# File 'lib/pop_this.rb', line 59 def emails @emails = Email.all(@dir) end |
#list(msgid = nil) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/pop_this.rb', line 73 def list(msgid = nil) msgid = msgid.to_i if msgid if msgid return false if msgid > @emails.length or @emails[msgid-1].deleted? return [ [msgid, @emails[msgid].email.length] ] else msgs = [] @emails.each_with_index do |e,i| msgs << [ i + 1, e.email.length ] end msgs end end |
#process_line(line) ⇒ Object
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 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/pop_this.rb', line 113 def process_line(line) line.chomp! case @state when 'auth' case line when /^QUIT$/ return false, "+OK popthis POP3 server signing off\r\n" when /^USER (.+)$/ return true, "+OK #{$1} is most welcome here\r\n" when /^PASS (.+)$/ @state = 'trans' emails msgs, bytes = stat return true, "+OK #{msgs} messages (#{bytes} octets)\r\n" end when 'trans' case line when /^NOOP$/ return true, "+OK\r\n" when /^STAT$/ msgs, bytes = stat return true, "+OK #{msgs} #{bytes}\r\n" when /^LIST$/ msgs, bytes = stat msg = "+OK #{msgs} messages (#{bytes} octets)\r\n" list.each do |num, bytes| msg += "#{num} #{bytes}\r\n" end msg += ".\r\n" return true, msg when /^LIST (\d+)$/ msgs, bytes = stat num, bytes = list($1) if num return true, "+OK #{num} #{bytes}\r\n" else return true, "-ERR no such message, only #{msgs} messages in maildrop\r\n" end when /^RETR (\d+)$/ msg = retr($1) if msg msg = "+OK #{msg.length} octets\r\n" + msg msg += "\r\n.\r\n" else msg = "-ERR no such message\r\n" end return true, msg when /^DELE (\d+)$/ if dele($1) return true, "+OK message #{$1} deleted\r\n" else return true, "-ERR message #{$1} already deleted\r\n" end when /^RSET$/ rset msgs, bytes = stat return true, "+OK maildrop has #{msgs} messages (#{bytes} octets)\r\n" when /^QUIT$/ @state = 'update' quit msgs, bytes = stat if msgs > 0 return false, "+OK popthis server signing off (#{msgs} messages left)\r\n" else return false, "+OK popthis server signing off (folder empty)\r\n" end when /^TOP (\d+) (\d+)$/ lines = $2 msg = retr($1) unless msg return true, "-ERR no such message\r\n" end cnt = nil final = "" msg.split(/\n/).each do |l| final += l+"\n" if cnt cnt += 1 break if cnt > lines end if l !~ /\w/ cnt = 0 end end return true, "+OK\r\n"+final+".\r\n" when /^UIDL$/ msgid = 0 msg = '' @emails.each do |e| msgid += 1 next if e.deleted? msg += "#{msgid} #{Digest::MD5.hexdigest(e.email)}\r\n" end return true, "+OK\r\n#{msg}.\r\n"; end when 'update' case line when /^QUIT$/ return true, "+OK popthis server signing off\r\n" end end return true, "-ERR unknown command\r\n" end |
#quit ⇒ Object
105 106 107 108 109 110 111 |
# File 'lib/pop_this.rb', line 105 def quit @emails.each do |e| if e.deleted? Email.delete(e) end end end |
#retr(msgid) ⇒ Object
87 88 89 90 91 |
# File 'lib/pop_this.rb', line 87 def retr(msgid) msgid = msgid.to_i return false if msgid > @emails.length or @emails[msgid-1].deleted? @emails[msgid-1].email end |
#rset ⇒ Object
99 100 101 102 103 |
# File 'lib/pop_this.rb', line 99 def rset @emails.each do |e| e.deleted = false end end |
#serve(io) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/pop_this.rb', line 38 def serve(io) @state = 'auth' @failed = 0 io.print("+OK POP3 server ready\r\n") loop do if IO.select([io], nil, nil, 0.1) begin data = io.readpartial(4096) puts ">> #{data.chomp}" ok, op = process_line(data) puts "<< #{op.chomp}" io.print op break unless ok rescue Exception end end break if io.closed? end io.close unless io.closed? end |
#stat ⇒ Object
63 64 65 66 67 68 69 70 71 |
# File 'lib/pop_this.rb', line 63 def stat msgs = bytes = 0 @emails.each do |e| next if e.deleted? msgs += 1 bytes += e.email.length end return msgs, bytes end |