 |
Changeset 1649
- Timestamp:
- 02/10/07 18:27:21
(2 years ago)
- Author:
- qbert
- Message:
Updated extractField to extract all fields.
Common names for methods in POP3Connection
-
Files:
-
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
| r1631 |
r1649 |
|
| 5 | 5 | private import tango.text.convert.Integer; |
|---|
| 6 | 6 | private import tango.text.Util; |
|---|
| 7 | | |
|---|
| | 7 | private import tango.io.Stdout; |
|---|
| 8 | 8 | debug ( Pop3Debug ) { private import tango.io.Stdout; } |
|---|
| 9 | 9 | |
|---|
| 10 | | // The sendData, sendLine, readData, readLine functions were all ripped from Telnet.d |
|---|
| 11 | | // these have a good potentila for reuse, perhaps move them into their own class ? TextNetworkClient or something |
|---|
| 12 | | // for now just inherits from Telnet |
|---|
| | 10 | /** |
|---|
| | 11 | Example: |
|---|
| | 12 | |
|---|
| | 13 | { |
|---|
| | 14 | POP3Connection pop3 = new POP3Connection("mail.server.com","username","password"); |
|---|
| | 15 | |
|---|
| | 16 | int messageCount = pop3.messageCount(); |
|---|
| | 17 | int totalMessageSize = pop3.totalSize(); // size of all messages |
|---|
| | 18 | int messageSize = pop3.size(1); // size of individual message |
|---|
| | 19 | |
|---|
| | 20 | POP3Response resp = pop3.retrieve(1); // get a message |
|---|
| | 21 | |
|---|
| | 22 | char [] [] to = extractField("To:",resp ); // get all To fields |
|---|
| | 23 | char [] [] from = extractField("From:",resp ); // all From fields |
|---|
| | 24 | char [] [] subject = extractField("Subject:",resp ); // all Subject fields |
|---|
| | 25 | |
|---|
| | 26 | char [] messageBody = extractBody(resp); |
|---|
| | 27 | |
|---|
| | 28 | pop3.remove(1); // remove message |
|---|
| | 29 | |
|---|
| | 30 | foreach ( POP3Response message; pop3 ) |
|---|
| | 31 | { |
|---|
| | 32 | |
|---|
| | 33 | char [] [] to = extractField("To:",message ); |
|---|
| | 34 | |
|---|
| | 35 | } |
|---|
| | 36 | |
|---|
| | 37 | foreach_reverse ( POP3Response message; pop3 ) |
|---|
| | 38 | { |
|---|
| | 39 | |
|---|
| | 40 | char [] [] to = extractField("To:",message ); |
|---|
| | 41 | |
|---|
| | 42 | } |
|---|
| | 43 | |
|---|
| | 44 | */ |
|---|
| | 45 | |
|---|
| | 46 | |
|---|
| | 47 | |
|---|
| | 48 | |
|---|
| | 49 | |
|---|
| | 50 | /* |
|---|
| | 51 | This pop3 client supports all standard pop3 commands, with method names matching the pop3 commands. |
|---|
| | 52 | It also has common methods and aliases for easy manipulation. |
|---|
| | 53 | |
|---|
| | 54 | The POP3Response structure represents the response from the pop3 server, with single line responses in the 'resp' field , |
|---|
| | 55 | and multiline responses in the 'lines' field. |
|---|
| | 56 | |
|---|
| | 57 | The common name functions should be enough for most usage: |
|---|
| | 58 | |
|---|
| | 59 | retrieve |
|---|
| | 60 | remove |
|---|
| | 61 | size |
|---|
| | 62 | totalSize |
|---|
| | 63 | messageCount |
|---|
| | 64 | |
|---|
| | 65 | */ |
|---|
| | 66 | |
|---|
| | 67 | |
|---|
| 13 | 68 | |
|---|
| 14 | 69 | /// Response from POP3 server |
|---|
| … | … | |
| 52 | 107 | body |
|---|
| 53 | 108 | { |
|---|
| 54 | | // Close any active connection. |
|---|
| 55 | | |
|---|
| 56 | | if (this.socket !is null) |
|---|
| 57 | | this.close(); |
|---|
| 58 | | |
|---|
| 59 | | |
|---|
| 60 | | // Connect to whichever FTP server responds first. |
|---|
| 61 | | this.findAvailableServer(hostname, port); |
|---|
| 62 | | |
|---|
| 63 | | this.socket.blocking = false; |
|---|
| 64 | | |
|---|
| 65 | 109 | scope (failure) |
|---|
| 66 | 110 | { |
|---|
| … | … | |
| 69 | 113 | } |
|---|
| 70 | 114 | |
|---|
| | 115 | // Close any active connection. |
|---|
| | 116 | |
|---|
| | 117 | if (this.socket !is null) |
|---|
| | 118 | this.close(); |
|---|
| | 119 | |
|---|
| | 120 | |
|---|
| | 121 | // Connect to whichever pop3 server responds first. |
|---|
| | 122 | this.findAvailableServer(hostname, port); |
|---|
| | 123 | |
|---|
| | 124 | this.socket.blocking = false; |
|---|
| | 125 | |
|---|
| | 126 | |
|---|
| 71 | 127 | getShortResponse(); // get welcome response |
|---|
| 72 | 128 | shortCmd("USER " ~ username); |
|---|
| … | … | |
| 77 | 133 | |
|---|
| 78 | 134 | } |
|---|
| | 135 | |
|---|
| | 136 | /* Aliases */ |
|---|
| | 137 | alias dele remove; /// remove a message |
|---|
| | 138 | alias retr retrieve; /// retrieve a message |
|---|
| | 139 | alias rset reset; /// reset all messages marked for deleton |
|---|
| | 140 | |
|---|
| | 141 | /// size of particular message |
|---|
| | 142 | int messageSize( int messageNumber ) |
|---|
| | 143 | { |
|---|
| | 144 | POP3Response resp = list(messageNumber ); |
|---|
| | 145 | int spacePos = locatePrior(resp.resp,' '); // locate the last space |
|---|
| | 146 | char [] size = resp.resp[spacePos+1 .. $]; // extract the size |
|---|
| | 147 | |
|---|
| | 148 | return atoi(size); |
|---|
| | 149 | } |
|---|
| | 150 | |
|---|
| | 151 | /// total message count |
|---|
| | 152 | int messageCount() |
|---|
| | 153 | { |
|---|
| | 154 | |
|---|
| | 155 | uint count, dummy; |
|---|
| | 156 | POP3Response resp = stat(count,dummy ); |
|---|
| | 157 | |
|---|
| | 158 | return count; |
|---|
| | 159 | |
|---|
| | 160 | } |
|---|
| | 161 | |
|---|
| | 162 | /// size of all messages on server |
|---|
| | 163 | uint totalSize ( ) |
|---|
| | 164 | { |
|---|
| | 165 | uint dummy, size; |
|---|
| | 166 | POP3Response resp = stat(dummy,size ); |
|---|
| | 167 | |
|---|
| | 168 | return size; |
|---|
| | 169 | |
|---|
| | 170 | } |
|---|
| | 171 | |
|---|
| 79 | 172 | |
|---|
| 80 | 173 | |
|---|
| … | … | |
| 134 | 227 | POP3Response r; |
|---|
| 135 | 228 | r.resp = shortCmd("STAT"); |
|---|
| 136 | | char [] [] totalAndSize = delimit(r.resp," " ); |
|---|
| | 229 | char [] [] totalAndSize = split(r.resp," " ); |
|---|
| 137 | 230 | |
|---|
| 138 | 231 | assert(totalAndSize.length == 3 ); |
|---|
| … | … | |
| 163 | 256 | } |
|---|
| 164 | 257 | |
|---|
| 165 | | |
|---|
| | 258 | /// Make it foreachable |
|---|
| 166 | 259 | int opApply( int delegate (inout POP3Response resp ) dg ) |
|---|
| 167 | 260 | { |
|---|
| … | … | |
| 181 | 274 | } |
|---|
| 182 | 275 | |
|---|
| | 276 | /// Make it foreachable_reverse |
|---|
| 183 | 277 | int opApplyReverse( int delegate (inout POP3Response resp ) dg ) |
|---|
| 184 | 278 | { |
|---|
| r1591 |
r1649 |
|
| 114 | 114 | |
|---|
| 115 | 115 | |
|---|
| 116 | | T[] join(T)(T[][] items,T[] glue = "" ) |
|---|
| | 116 | |
|---|
| | 117 | // some fields are repeated more than once , so we loop through the whole message till we find an empty line, |
|---|
| | 118 | // which represents the break for the body |
|---|
| | 119 | char [] [] extractField(char [] headerName, POP3Response resp ) |
|---|
| 117 | 120 | { |
|---|
| 118 | | |
|---|
| 119 | | T[] result; |
|---|
| 120 | | foreach ( item;items ) |
|---|
| 121 | | { |
|---|
| 122 | | result ~= item ~ glue; |
|---|
| 123 | | } |
|---|
| 124 | | return result; |
|---|
| 125 | | |
|---|
| 126 | | } |
|---|
| 127 | | |
|---|
| 128 | | |
|---|
| 129 | | char [] extractField(char [] headerName, POP3Response resp ) |
|---|
| 130 | | { |
|---|
| 131 | | char [] result; |
|---|
| | 121 | char [] [] result; |
|---|
| 132 | 122 | bool wrappedField = false; |
|---|
| 133 | 123 | |
|---|
| … | … | |
| 145 | 135 | continue; |
|---|
| 146 | 136 | } |
|---|
| 147 | | else break; |
|---|
| | 137 | else wrappedField = false; |
|---|
| 148 | 138 | } |
|---|
| 149 | 139 | } |
|---|
| … | … | |
| 155 | 145 | { |
|---|
| 156 | 146 | |
|---|
| 157 | | result = line[headerName.length+1 .. $]; |
|---|
| 158 | | |
|---|
| 159 | | if ( containsPattern(line,"\r\n" )) // these are for wrapped header fields, the next line must be followed bya |
|---|
| 160 | | // linear white space char , \s\t etc |
|---|
| 161 | | { |
|---|
| 162 | | wrappedField = true; |
|---|
| 163 | | } |
|---|
| 164 | | else break; |
|---|
| | 147 | result ~= line[headerName.length+1 .. $]; |
|---|
| | 148 | wrappedField = true; // we check for wrappedFields |
|---|
| 165 | 149 | |
|---|
| 166 | 150 | } |
|---|
Download in other formats:
|
 |
 |
|
 |
Copyright © 2006-2008 Tango. All Rights Reserved. | Page Width:
Static or
Dynamic