~ubuntu-branches/ubuntu/oneiric/haxe/oneiric

« back to all changes in this revision

Viewing changes to haxe/std/mtwin/mail/imap/Connection.hx

  • Committer: Bazaar Package Importer
  • Author(s): Jens Peter Secher
  • Date: 2008-06-15 11:04:09 UTC
  • mfrom: (2.1.6 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080615110409-7pyykgwmk5v0cues
Tags: 1:1.19-3
* Remove bashism in script.
  (Closes: #484390)
* Upgrade to Policy 3.8.0 by including a README.source explaining how to
  use dpatch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
 */
25
25
package mtwin.mail.imap;
26
26
 
27
 
import neko.io.Socket;
 
27
import neko.net.Socket;
28
28
import mtwin.mail.Exception;
29
29
import mtwin.mail.imap.Tools;
30
30
 
68
68
        static var REG_FETCH_END = ~/^([A0-9]{4}) (OK|BAD|NO)/;
69
69
        static var REG_STATUS = ~/STATUS .*? \(([^)]+)\)/;
70
70
        static var REG_STATUS_VAL = ~/^ ?([A-Z]+) (-?[0-9]+)/;
71
 
        static var REG_LIST_RESP = ~/LIST \(([ \\A-Za-z0-9]*)\) "\." "([^"]+)"/;
 
71
        static var REG_LIST_RESP = ~/LIST \(([ \\A-Za-z0-9]*)\) [A-z0-9".]* "?([^"]+)"?/;
72
72
        static var REG_CRLF = ~/\r?\n/g;
73
73
 
74
74
        static function rmCRLF(s){
95
95
                if( port == null ) port = 143;
96
96
                cnx = new Socket();
97
97
                try{
98
 
                        cnx.connect( Socket.resolve(host), port );
 
98
                        cnx.connect( new neko.net.Host(host), port );
99
99
                }catch( e : Dynamic ){
 
100
                        cnx.close();
100
101
                        throw ConnectionError(host,port);
101
102
                }
102
103
                debug("socket connected");
108
109
        /**
109
110
                Login to server
110
111
        **/
111
 
        public function login( user : String, pass : String ){  
 
112
        public function login( user : String, pass : String ){
112
113
                var r = command("LOGIN",Tools.quote(user)+" "+Tools.quote(pass));
113
114
                if( !r.success ){
114
115
                        throw BadResponse(r.response);
142
143
                if( pattern == null ) pattern = "*";
143
144
                if( flat == null ) flat = false;
144
145
 
145
 
                var r = command("LIST",Tools.quote(".")+" "+Tools.quote(pattern));
 
146
                var r = command("LIST",Tools.quote("")+" "+Tools.quote(pattern));
146
147
                if( !r.success ){
147
148
                        throw BadResponse(r.response);
148
149
                }
149
150
 
150
151
                var hash = new Hash();
151
152
                for( v in r.result ){
152
 
                        if( REG_LIST_RESP.match(v) ){   
 
153
                        if( REG_LIST_RESP.match(v) ){
153
154
                                var name = REG_LIST_RESP.matched(2);
154
155
                                var flags = REG_LIST_RESP.matched(1).split(" ");
155
156
 
178
179
        }
179
180
 
180
181
        public function getMailbox( name : String ){
181
 
                return mailboxes(name)[0];
 
182
                var m = mailboxes(name)[0];
 
183
                if( m == null )
 
184
                        throw "No such mailbox "+name;
 
185
                return m;
182
186
        }
183
187
 
184
188
        /**
188
192
                if( selected == mailbox ) return null;
189
193
 
190
194
                var r = command("SELECT",Tools.quote(mailbox));
191
 
                if( !r.success ) 
 
195
                if( !r.success )
192
196
                        throw BadResponse(r.response);
193
197
 
194
198
                selected = mailbox;
195
 
                
 
199
 
196
200
                var ret = {recent: 0,exists: 0,firstUnseen: null};
197
201
                for( v in r.result ){
198
202
                        if( REG_EXISTS.match(v) ){
210
214
        public function status( mailbox : String ){
211
215
                var r = command("STATUS",Tools.quote(mailbox)+" (MESSAGES RECENT UNSEEN)");
212
216
                if( !r.success ) throw BadResponse( r.response );
213
 
                
214
 
                var ret = new Hash<Int>();      
 
217
 
 
218
                var ret = new Hash<Int>();
215
219
                if( REG_STATUS.match( r.result.first() ) ){
216
220
                        var t = REG_STATUS.matched(1);
217
221
                        while( REG_STATUS_VAL.match(t) ){
296
300
                        var l = cnx.input.readLine();
297
301
                        if( REG_FETCH_MAIN.match(l) ){
298
302
                                var id = Std.parseInt(REG_FETCH_MAIN.matched(1));
299
 
                                
 
303
 
300
304
                                var o = if( tmp.exists(id) ){
301
 
                                        tmp.get(id); 
 
305
                                        tmp.get(id);
302
306
                                }else {
303
307
                                        var o = {bodyType: null,body: null,flags: null,uid: null,structure: null,internalDate: null,envelope: null,id: id};
304
308
                                        tmp.set(id,o);
320
324
                                        }else if( REG_FETCH_ENVELOPE.match( s ) ){
321
325
                                                var t = REG_FETCH_ENVELOPE.matchedRight();
322
326
                                                t = completeString(t);
323
 
                                                o.envelope = Envelope.parse( t );
 
327
                                                o.envelope = mtwin.mail.imap.Envelope.parse( t );
324
328
                                                s = StringTools.ltrim(t.substr(o.envelope.__length,t.length));
325
329
                                        }else if( REG_FETCH_BODYSTRUCTURE.match( s ) ){
326
330
                                                var t = REG_FETCH_BODYSTRUCTURE.matchedRight();
327
331
                                                t = completeString(t);
328
 
                                                o.structure = BodyStructure.parse( t );
 
332
                                                o.structure = mtwin.mail.imap.BodyStructure.parse( t );
329
333
                                                s = StringTools.ltrim(t.substr(o.structure.__length,t.length));
330
334
                                        }else if( REG_FETCH_PART.match( s ) ){
331
335
                                                var len = Std.parseInt(REG_FETCH_PART.matched(2));
332
 
                                                
 
336
 
333
337
                                                o.body = cnx.input.read( len );
334
338
                                                o.bodyType = REG_FETCH_PART.matched(1);
335
 
                                                
 
339
 
336
340
                                                cnx.input.readLine();
337
341
                                                break;
338
342
                                        }else{
339
343
                                                break;
340
344
                                        }
341
345
                                }
342
 
                                
 
346
 
343
347
                        }else if( REG_FETCH_END.match(l) ){
344
348
                                var resp = REG_FETCH_END.matched(2);
345
349
                                if( resp == "OK" ){
367
371
                if( !r.success )
368
372
                        throw BadResponse(r.response);
369
373
        }
370
 
        
 
374
 
371
375
        /**
372
376
                Remove permanently all messages flagged as \Deleted in the currently selected mailbox.
373
377
        **/
384
388
                if( mode == null ) mode = Add;
385
389
                if( fetchResult == null ) fetchResult = false;
386
390
                if( useUid == null ) useUid = false;
387
 
                
 
391
 
388
392
                var range = Tools.collString(iRange);
389
393
                var elem = switch( mode ){
390
394
                        case Add: "+FLAGS";
395
399
                        elem += ".SILENT";
396
400
                }
397
401
 
398
 
                var r = command( if( useUid ) "UID STORE" else "STORE", range + " " + elem + "("+flags.join(" ")+")");
 
402
                var r = command( if( useUid ) "UID STORE" else "STORE", range + " " + elem + " ("+flags.join(" ")+")");
399
403
                if( !r.success ) throw BadResponse( r.response );
400
404
                if( !fetchResult ) return null;
401
405
 
418
422
                var r = command( "CREATE", Tools.quote(mailbox) );
419
423
                if( !r.success ) throw BadResponse( r.response );
420
424
        }
421
 
        
 
425
 
422
426
        /**
423
427
                Delete a mailbox.
424
428
        **/
466
470
                        throw NotConnected;
467
471
                if( r == null ) r = true;
468
472
                if( args == null ) args = "" else args = " "+args;
469
 
                
 
473
 
470
474
                count++;
471
475
                var c = Std.string(count);
472
476
                c = StringTools.lpad(c,"A000",4);
479
483
                return read(c);
480
484
        }
481
485
 
482
 
        
 
486
 
483
487
        function read( c ){
484
488
                var resp = new List();
485
489
                var sb : StringBuf = null;
521
525
                        }
522
526
                }
523
527
                return null;
524
 
        }       
 
528
        }
525
529
}