Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Changeset 1649

Show
Ignore:
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
  • trunk/patches/proposals/Pop3Client.d

    r1631 r1649  
    55private import tango.text.convert.Integer; 
    66private import tango.text.Util; 
    7  
     7private import tango.io.Stdout;  
    88debug ( Pop3Debug ) { private import tango.io.Stdout; } 
    99 
    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/** 
     11Example: 
     12 
     13
     14POP3Connection pop3 = new POP3Connection("mail.server.com","username","password"); 
     15 
     16int messageCount = pop3.messageCount(); 
     17int totalMessageSize = pop3.totalSize(); // size of all messages 
     18int messageSize = pop3.size(1); // size of individual message 
     19 
     20POP3Response resp  = pop3.retrieve(1); // get a message 
     21 
     22char [] [] to = extractField("To:",resp ); // get all To fields 
     23char [] [] from = extractField("From:",resp ); // all From fields 
     24char [] [] subject = extractField("Subject:",resp ); // all Subject fields 
     25 
     26char [] messageBody = extractBody(resp); 
     27 
     28pop3.remove(1); // remove message 
     29 
     30foreach ( POP3Response message; pop3 ) 
     31
     32 
     33  char [] [] to = extractField("To:",message ); 
     34 
     35
     36 
     37foreach_reverse ( POP3Response message; pop3 ) 
     38
     39 
     40  char [] [] to = extractField("To:",message ); 
     41 
     42
     43 
     44*/ 
     45 
     46 
     47 
     48 
     49 
     50/* 
     51This pop3 client supports all standard pop3 commands, with method names matching the pop3 commands. 
     52It also has common methods and aliases for easy manipulation. 
     53 
     54The POP3Response structure represents the response from the pop3 server, with single line responses in the 'resp' field , 
     55and multiline responses in the 'lines' field. 
     56 
     57The common name functions should be enough for most usage: 
     58 
     59retrieve 
     60remove 
     61size 
     62totalSize 
     63messageCount 
     64 
     65*/ 
     66 
     67 
    1368 
    1469/// Response from POP3 server 
     
    52107    body 
    53108    { 
    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  
    65109    scope (failure) 
    66110        { 
     
    69113        } 
    70114 
     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 
    71127    getShortResponse(); // get welcome response 
    72128    shortCmd("USER " ~ username);  
     
    77133 
    78134    } 
     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 
    79172 
    80173 
     
    134227      POP3Response r; 
    135228      r.resp = shortCmd("STAT"); 
    136       char [] [] totalAndSize = delimit(r.resp," " ); 
     229      char [] [] totalAndSize = split(r.resp," " ); 
    137230 
    138231      assert(totalAndSize.length == 3 ); 
     
    163256    } 
    164257 
    165  
     258  /// Make it foreachable 
    166259  int opApply( int delegate (inout POP3Response resp ) dg ) 
    167260  { 
     
    181274  } 
    182275 
     276  /// Make it foreachable_reverse 
    183277  int opApplyReverse( int delegate (inout POP3Response resp ) dg ) 
    184278  { 
  • trunk/patches/proposals/Util.d

    r1591 r1649  
    114114 
    115115 
    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 
     119char [] [] extractField(char [] headerName, POP3Response resp ) 
    117120{ 
    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; 
    132122  bool wrappedField = false; 
    133123   
     
    145135          continue; 
    146136        } 
    147           else break
     137          else wrappedField = false
    148138        } 
    149139    } 
     
    155145        { 
    156146 
    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 
    165149 
    166150        }