Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

imap4.py 207KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233
  1. # -*- test-case-name: twisted.mail.test.test_imap.IMAP4HelperTests -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. An IMAP4 protocol implementation
  6. @author: Jp Calderone
  7. To do::
  8. Suspend idle timeout while server is processing
  9. Use an async message parser instead of buffering in memory
  10. Figure out a way to not queue multi-message client requests (Flow? A simple callback?)
  11. Clarify some API docs (Query, etc)
  12. Make APPEND recognize (again) non-existent mailboxes before accepting the literal
  13. """
  14. import binascii
  15. import codecs
  16. import copy
  17. import email.utils
  18. import functools
  19. import re
  20. import string
  21. import tempfile
  22. import time
  23. import uuid
  24. from base64 import decodebytes, encodebytes
  25. from io import BytesIO
  26. from itertools import chain
  27. from typing import Any, List, cast
  28. from zope.interface import implementer
  29. from twisted.cred import credentials
  30. from twisted.cred.error import UnauthorizedLogin, UnhandledCredentials
  31. from twisted.internet import defer, error, interfaces
  32. from twisted.internet.defer import maybeDeferred
  33. from twisted.mail._cred import (
  34. CramMD5ClientAuthenticator,
  35. LOGINAuthenticator,
  36. LOGINCredentials,
  37. PLAINAuthenticator,
  38. PLAINCredentials,
  39. )
  40. from twisted.mail._except import (
  41. IllegalClientResponse,
  42. IllegalIdentifierError,
  43. IllegalMailboxEncoding,
  44. IllegalOperation,
  45. IllegalQueryError,
  46. IllegalServerResponse,
  47. IMAP4Exception,
  48. MailboxCollision,
  49. MailboxException,
  50. MismatchedNesting,
  51. MismatchedQuoting,
  52. NegativeResponse,
  53. NoSuchMailbox,
  54. NoSupportedAuthentication,
  55. ReadOnlyMailbox,
  56. UnhandledResponse,
  57. )
  58. # Re-exported for compatibility reasons
  59. from twisted.mail.interfaces import (
  60. IAccountIMAP as IAccount,
  61. IClientAuthentication,
  62. ICloseableMailboxIMAP as ICloseableMailbox,
  63. IMailboxIMAP as IMailbox,
  64. IMailboxIMAPInfo as IMailboxInfo,
  65. IMailboxIMAPListener as IMailboxListener,
  66. IMessageIMAP as IMessage,
  67. IMessageIMAPCopier as IMessageCopier,
  68. IMessageIMAPFile as IMessageFile,
  69. IMessageIMAPPart as IMessagePart,
  70. INamespacePresenter,
  71. ISearchableIMAPMailbox as ISearchableMailbox,
  72. )
  73. from twisted.protocols import basic, policies
  74. from twisted.python import log, text
  75. from twisted.python.compat import (
  76. _get_async_param,
  77. _matchingString,
  78. iterbytes,
  79. nativeString,
  80. networkString,
  81. )
  82. # locale-independent month names to use instead of strftime's
  83. _MONTH_NAMES = dict(
  84. zip(range(1, 13), "Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec".split())
  85. )
  86. def _swap(this, that, ifIs):
  87. """
  88. Swap C{this} with C{that} if C{this} is C{ifIs}.
  89. @param this: The object that may be replaced.
  90. @param that: The object that may replace C{this}.
  91. @param ifIs: An object whose identity will be compared to
  92. C{this}.
  93. """
  94. return that if this is ifIs else this
  95. def _swapAllPairs(of, that, ifIs):
  96. """
  97. Swap each element in each pair in C{of} with C{that} it is
  98. C{ifIs}.
  99. @param of: A list of 2-L{tuple}s, whose members may be the object
  100. C{that}
  101. @type of: L{list} of 2-L{tuple}s
  102. @param ifIs: An object whose identity will be compared to members
  103. of each pair in C{of}
  104. @return: A L{list} of 2-L{tuple}s with all occurences of C{ifIs}
  105. replaced with C{that}
  106. """
  107. return [
  108. (_swap(first, that, ifIs), _swap(second, that, ifIs)) for first, second in of
  109. ]
  110. class MessageSet:
  111. """
  112. A set of message identifiers usable by both L{IMAP4Client} and
  113. L{IMAP4Server} via L{IMailboxIMAP.store} and
  114. L{IMailboxIMAP.fetch}.
  115. These identifiers can be either message sequence numbers or unique
  116. identifiers. See Section 2.3.1, "Message Numbers", RFC 3501.
  117. This represents the C{sequence-set} described in Section 9,
  118. "Formal Syntax" of RFC 3501:
  119. - A L{MessageSet} can describe a single identifier, e.g.
  120. C{MessageSet(1)}
  121. - A L{MessageSet} can describe C{*} via L{None}, e.g.
  122. C{MessageSet(None)}
  123. - A L{MessageSet} can describe a range of identifiers, e.g.
  124. C{MessageSet(1, 2)}. The range is inclusive and unordered
  125. (see C{seq-range} in RFC 3501, Section 9), so that
  126. C{Message(2, 1)} is equivalent to C{MessageSet(1, 2)}, and
  127. both describe messages 1 and 2. Ranges can include C{*} by
  128. specifying L{None}, e.g. C{MessageSet(None, 1)}. In all
  129. cases ranges are normalized so that the smallest identifier
  130. comes first, and L{None} always comes last; C{Message(2, 1)}
  131. becomes C{MessageSet(1, 2)} and C{MessageSet(None, 1)}
  132. becomes C{MessageSet(1, None)}
  133. - A L{MessageSet} can describe a sequence of single
  134. identifiers and ranges, constructed by addition.
  135. C{MessageSet(1) + MessageSet(5, 10)} refers the message
  136. identified by C{1} and the messages identified by C{5}
  137. through C{10}.
  138. B{NB: The meaning of * varies, but it always represents the
  139. largest number in use}.
  140. B{For servers}: Your L{IMailboxIMAP} provider must set
  141. L{MessageSet.last} to the highest-valued identifier (unique or
  142. message sequence) before iterating over it.
  143. B{For clients}: C{*} consumes ranges smaller than it, e.g.
  144. C{MessageSet(1, 100) + MessageSet(50, None)} is equivalent to
  145. C{1:*}.
  146. @type getnext: Function taking L{int} returning L{int}
  147. @ivar getnext: A function that returns the next message number,
  148. used when iterating through the L{MessageSet}. By default, a
  149. function returning the next integer is supplied, but as this
  150. can be rather inefficient for sparse UID iterations, it is
  151. recommended to supply one when messages are requested by UID.
  152. The argument is provided as a hint to the implementation and
  153. may be ignored if it makes sense to do so (eg, if an iterator
  154. is being used that maintains its own state, it is guaranteed
  155. that it will not be called out-of-order).
  156. """
  157. _empty: List[Any] = []
  158. _infinity = float("inf")
  159. def __init__(self, start=_empty, end=_empty):
  160. """
  161. Create a new MessageSet()
  162. @type start: Optional L{int}
  163. @param start: Start of range, or only message number
  164. @type end: Optional L{int}
  165. @param end: End of range.
  166. """
  167. self._last = self._empty # Last message/UID in use
  168. self.ranges = [] # List of ranges included
  169. self.getnext = lambda x: x + 1 # A function which will return the next
  170. # message id. Handy for UID requests.
  171. if start is self._empty:
  172. return
  173. if isinstance(start, list):
  174. self.ranges = start[:]
  175. self.clean()
  176. else:
  177. self.add(start, end)
  178. @property
  179. def last(self):
  180. """
  181. The largest number in use.
  182. This is undefined until it has been set by assigning to this property.
  183. """
  184. return self._last
  185. @last.setter
  186. def last(self, value):
  187. """
  188. Replaces all occurrences of "*". This should be the
  189. largest number in use. Must be set before attempting to
  190. use the MessageSet as a container.
  191. @raises ValueError: if a largest value has already been set.
  192. """
  193. if self._last is not self._empty:
  194. raise ValueError("last already set")
  195. self._last = value
  196. for i, (low, high) in enumerate(self.ranges):
  197. if low is None:
  198. low = value
  199. if high is None:
  200. high = value
  201. if low > high:
  202. low, high = high, low
  203. self.ranges[i] = (low, high)
  204. self.clean()
  205. def add(self, start, end=_empty):
  206. """
  207. Add another range
  208. @type start: L{int}
  209. @param start: Start of range, or only message number
  210. @type end: Optional L{int}
  211. @param end: End of range.
  212. """
  213. if end is self._empty:
  214. end = start
  215. if self._last is not self._empty:
  216. if start is None:
  217. start = self.last
  218. if end is None:
  219. end = self.last
  220. start, end = sorted(
  221. [start, end], key=functools.partial(_swap, that=self._infinity, ifIs=None)
  222. )
  223. self.ranges.append((start, end))
  224. self.clean()
  225. def __add__(self, other):
  226. if isinstance(other, MessageSet):
  227. ranges = self.ranges + other.ranges
  228. return MessageSet(ranges)
  229. else:
  230. res = MessageSet(self.ranges)
  231. if self.last is not self._empty:
  232. res.last = self.last
  233. try:
  234. res.add(*other)
  235. except TypeError:
  236. res.add(other)
  237. return res
  238. def extend(self, other):
  239. """
  240. Extend our messages with another message or set of messages.
  241. @param other: The messages to include.
  242. @type other: L{MessageSet}, L{tuple} of two L{int}s, or a
  243. single L{int}
  244. """
  245. if isinstance(other, MessageSet):
  246. self.ranges.extend(other.ranges)
  247. self.clean()
  248. else:
  249. try:
  250. self.add(*other)
  251. except TypeError:
  252. self.add(other)
  253. return self
  254. def clean(self):
  255. """
  256. Clean ranges list, combining adjacent ranges
  257. """
  258. ranges = sorted(_swapAllPairs(self.ranges, that=self._infinity, ifIs=None))
  259. mergedRanges = [(float("-inf"), float("-inf"))]
  260. for low, high in ranges:
  261. previousLow, previousHigh = mergedRanges[-1]
  262. if previousHigh < low - 1:
  263. mergedRanges.append((low, high))
  264. continue
  265. mergedRanges[-1] = (min(previousLow, low), max(previousHigh, high))
  266. self.ranges = _swapAllPairs(mergedRanges[1:], that=None, ifIs=self._infinity)
  267. def _noneInRanges(self):
  268. """
  269. Is there a L{None} in our ranges?
  270. L{MessageSet.clean} merges overlapping or consecutive ranges.
  271. None is represents a value larger than any number. There are
  272. thus two cases:
  273. 1. C{(x, *) + (y, z)} such that C{x} is smaller than C{y}
  274. 2. C{(z, *) + (x, y)} such that C{z} is larger than C{y}
  275. (Other cases, such as C{y < x < z}, can be split into these
  276. two cases; for example C{(y - 1, y)} + C{(x, x) + (z, z + 1)})
  277. In case 1, C{* > y} and C{* > z}, so C{(x, *) + (y, z) = (x,
  278. *)}
  279. In case 2, C{z > x and z > y}, so the intervals do not merge,
  280. and the ranges are sorted as C{[(x, y), (z, *)]}. C{*} is
  281. represented as C{(*, *)}, so this is the same as 2. but with
  282. a C{z} that is greater than everything.
  283. The result is that there is a maximum of two L{None}s, and one
  284. of them has to be the high element in the last tuple in
  285. C{self.ranges}. That means checking if C{self.ranges[-1][-1]}
  286. is L{None} suffices to check if I{any} element is L{None}.
  287. @return: L{True} if L{None} is in some range in ranges and
  288. L{False} if otherwise.
  289. """
  290. return self.ranges[-1][-1] is None
  291. def __contains__(self, value):
  292. """
  293. May raise TypeError if we encounter an open-ended range
  294. @param value: Is this in our ranges?
  295. @type value: L{int}
  296. """
  297. if self._noneInRanges():
  298. raise TypeError("Can't determine membership; last value not set")
  299. for low, high in self.ranges:
  300. if low <= value <= high:
  301. return True
  302. return False
  303. def _iterator(self):
  304. for l, h in self.ranges:
  305. l = self.getnext(l - 1)
  306. while l <= h:
  307. yield l
  308. l = self.getnext(l)
  309. def __iter__(self):
  310. if self._noneInRanges():
  311. raise TypeError("Can't iterate; last value not set")
  312. return self._iterator()
  313. def __len__(self):
  314. res = 0
  315. for l, h in self.ranges:
  316. if l is None:
  317. res += 1
  318. elif h is None:
  319. raise TypeError("Can't size object; last value not set")
  320. else:
  321. res += (h - l) + 1
  322. return res
  323. def __str__(self) -> str:
  324. p = []
  325. for low, high in self.ranges:
  326. if low == high:
  327. if low is None:
  328. p.append("*")
  329. else:
  330. p.append(str(low))
  331. elif high is None:
  332. p.append("%d:*" % (low,))
  333. else:
  334. p.append("%d:%d" % (low, high))
  335. return ",".join(p)
  336. def __repr__(self) -> str:
  337. return f"<MessageSet {str(self)}>"
  338. def __eq__(self, other: object) -> bool:
  339. if isinstance(other, MessageSet):
  340. return cast(bool, self.ranges == other.ranges)
  341. return NotImplemented
  342. class LiteralString:
  343. def __init__(self, size, defered):
  344. self.size = size
  345. self.data = []
  346. self.defer = defered
  347. def write(self, data):
  348. self.size -= len(data)
  349. passon = None
  350. if self.size > 0:
  351. self.data.append(data)
  352. else:
  353. if self.size:
  354. data, passon = data[: self.size], data[self.size :]
  355. else:
  356. passon = b""
  357. if data:
  358. self.data.append(data)
  359. return passon
  360. def callback(self, line):
  361. """
  362. Call deferred with data and rest of line
  363. """
  364. self.defer.callback((b"".join(self.data), line))
  365. class LiteralFile:
  366. _memoryFileLimit = 1024 * 1024 * 10
  367. def __init__(self, size, defered):
  368. self.size = size
  369. self.defer = defered
  370. if size > self._memoryFileLimit:
  371. self.data = tempfile.TemporaryFile()
  372. else:
  373. self.data = BytesIO()
  374. def write(self, data):
  375. self.size -= len(data)
  376. passon = None
  377. if self.size > 0:
  378. self.data.write(data)
  379. else:
  380. if self.size:
  381. data, passon = data[: self.size], data[self.size :]
  382. else:
  383. passon = b""
  384. if data:
  385. self.data.write(data)
  386. return passon
  387. def callback(self, line):
  388. """
  389. Call deferred with data and rest of line
  390. """
  391. self.data.seek(0, 0)
  392. self.defer.callback((self.data, line))
  393. class WriteBuffer:
  394. """
  395. Buffer up a bunch of writes before sending them all to a transport at once.
  396. """
  397. def __init__(self, transport, size=8192):
  398. self.bufferSize = size
  399. self.transport = transport
  400. self._length = 0
  401. self._writes = []
  402. def write(self, s):
  403. self._length += len(s)
  404. self._writes.append(s)
  405. if self._length > self.bufferSize:
  406. self.flush()
  407. def flush(self):
  408. if self._writes:
  409. self.transport.writeSequence(self._writes)
  410. self._writes = []
  411. self._length = 0
  412. class Command:
  413. _1_RESPONSES = (
  414. b"CAPABILITY",
  415. b"FLAGS",
  416. b"LIST",
  417. b"LSUB",
  418. b"STATUS",
  419. b"SEARCH",
  420. b"NAMESPACE",
  421. )
  422. _2_RESPONSES = (b"EXISTS", b"EXPUNGE", b"FETCH", b"RECENT")
  423. _OK_RESPONSES = (
  424. b"UIDVALIDITY",
  425. b"UNSEEN",
  426. b"READ-WRITE",
  427. b"READ-ONLY",
  428. b"UIDNEXT",
  429. b"PERMANENTFLAGS",
  430. )
  431. defer = None
  432. def __init__(
  433. self,
  434. command,
  435. args=None,
  436. wantResponse=(),
  437. continuation=None,
  438. *contArgs,
  439. **contKw,
  440. ):
  441. self.command = command
  442. self.args = args
  443. self.wantResponse = wantResponse
  444. self.continuation = lambda x: continuation(x, *contArgs, **contKw)
  445. self.lines = []
  446. def __repr__(self) -> str:
  447. return "<imap4.Command {!r} {!r} {!r} {!r} {!r}>".format(
  448. self.command, self.args, self.wantResponse, self.continuation, self.lines
  449. )
  450. def format(self, tag):
  451. if self.args is None:
  452. return b" ".join((tag, self.command))
  453. return b" ".join((tag, self.command, self.args))
  454. def finish(self, lastLine, unusedCallback):
  455. send = []
  456. unuse = []
  457. for L in self.lines:
  458. names = parseNestedParens(L)
  459. N = len(names)
  460. if (
  461. N >= 1
  462. and names[0] in self._1_RESPONSES
  463. or N >= 2
  464. and names[1] in self._2_RESPONSES
  465. or N >= 2
  466. and names[0] == b"OK"
  467. and isinstance(names[1], list)
  468. and names[1][0] in self._OK_RESPONSES
  469. ):
  470. send.append(names)
  471. else:
  472. unuse.append(names)
  473. d, self.defer = self.defer, None
  474. d.callback((send, lastLine))
  475. if unuse:
  476. unusedCallback(unuse)
  477. # Some constants to help define what an atom is and is not - see the grammar
  478. # section of the IMAP4 RFC - <https://tools.ietf.org/html/rfc3501#section-9>.
  479. # Some definitions (SP, CTL, DQUOTE) are also from the ABNF RFC -
  480. # <https://tools.ietf.org/html/rfc2234>.
  481. _SP = b" "
  482. _CTL = bytes(chain(range(0x21), range(0x80, 0x100)))
  483. # It is easier to define ATOM-CHAR in terms of what it does not match than in
  484. # terms of what it does match.
  485. _nonAtomChars = b']\\\\(){%*"' + _SP + _CTL
  486. # _nonAtomRE is only used in Query, so it uses native strings.
  487. _nativeNonAtomChars = _nonAtomChars.decode("charmap")
  488. _nonAtomRE = re.compile("[" + _nativeNonAtomChars + "]")
  489. # This is all the bytes that match the ATOM-CHAR from the grammar in the RFC.
  490. _atomChars = bytes(ch for ch in range(0x100) if ch not in _nonAtomChars)
  491. @implementer(IMailboxListener)
  492. class IMAP4Server(basic.LineReceiver, policies.TimeoutMixin):
  493. """
  494. Protocol implementation for an IMAP4rev1 server.
  495. The server can be in any of four states:
  496. - Non-authenticated
  497. - Authenticated
  498. - Selected
  499. - Logout
  500. """
  501. # Identifier for this server software
  502. IDENT = b"Twisted IMAP4rev1 Ready"
  503. # Number of seconds before idle timeout
  504. # Initially 1 minute. Raised to 30 minutes after login.
  505. timeOut = 60
  506. POSTAUTH_TIMEOUT = 60 * 30
  507. # Whether STARTTLS has been issued successfully yet or not.
  508. startedTLS = False
  509. # Whether our transport supports TLS
  510. canStartTLS = False
  511. # Mapping of tags to commands we have received
  512. tags = None
  513. # The object which will handle logins for us
  514. portal = None
  515. # The account object for this connection
  516. account = None
  517. # Logout callback
  518. _onLogout = None
  519. # The currently selected mailbox
  520. mbox = None
  521. # Command data to be processed when literal data is received
  522. _pendingLiteral = None
  523. # Maximum length to accept for a "short" string literal
  524. _literalStringLimit = 4096
  525. # IChallengeResponse factories for AUTHENTICATE command
  526. challengers = None
  527. # Search terms the implementation of which needs to be passed both the last
  528. # message identifier (UID) and the last sequence id.
  529. _requiresLastMessageInfo = {b"OR", b"NOT", b"UID"}
  530. state = "unauth"
  531. parseState = "command"
  532. def __init__(self, chal=None, contextFactory=None, scheduler=None):
  533. if chal is None:
  534. chal = {}
  535. self.challengers = chal
  536. self.ctx = contextFactory
  537. if scheduler is None:
  538. scheduler = iterateInReactor
  539. self._scheduler = scheduler
  540. self._queuedAsync = []
  541. def capabilities(self):
  542. cap = {b"AUTH": list(self.challengers.keys())}
  543. if self.ctx and self.canStartTLS:
  544. if (
  545. not self.startedTLS
  546. and interfaces.ISSLTransport(self.transport, None) is None
  547. ):
  548. cap[b"LOGINDISABLED"] = None
  549. cap[b"STARTTLS"] = None
  550. cap[b"NAMESPACE"] = None
  551. cap[b"IDLE"] = None
  552. return cap
  553. def connectionMade(self):
  554. self.tags = {}
  555. self.canStartTLS = interfaces.ITLSTransport(self.transport, None) is not None
  556. self.setTimeout(self.timeOut)
  557. self.sendServerGreeting()
  558. def connectionLost(self, reason):
  559. self.setTimeout(None)
  560. if self._onLogout:
  561. self._onLogout()
  562. self._onLogout = None
  563. def timeoutConnection(self):
  564. self.sendLine(b"* BYE Autologout; connection idle too long")
  565. self.transport.loseConnection()
  566. if self.mbox:
  567. self.mbox.removeListener(self)
  568. cmbx = ICloseableMailbox(self.mbox, None)
  569. if cmbx is not None:
  570. maybeDeferred(cmbx.close).addErrback(log.err)
  571. self.mbox = None
  572. self.state = "timeout"
  573. def rawDataReceived(self, data):
  574. self.resetTimeout()
  575. passon = self._pendingLiteral.write(data)
  576. if passon is not None:
  577. self.setLineMode(passon)
  578. # Avoid processing commands while buffers are being dumped to
  579. # our transport
  580. blocked = None
  581. def _unblock(self):
  582. commands = self.blocked
  583. self.blocked = None
  584. while commands and self.blocked is None:
  585. self.lineReceived(commands.pop(0))
  586. if self.blocked is not None:
  587. self.blocked.extend(commands)
  588. def lineReceived(self, line):
  589. if self.blocked is not None:
  590. self.blocked.append(line)
  591. return
  592. self.resetTimeout()
  593. f = getattr(self, "parse_" + self.parseState)
  594. try:
  595. f(line)
  596. except Exception as e:
  597. self.sendUntaggedResponse(b"BAD Server error: " + networkString(str(e)))
  598. log.err()
  599. def parse_command(self, line):
  600. args = line.split(None, 2)
  601. rest = None
  602. if len(args) == 3:
  603. tag, cmd, rest = args
  604. elif len(args) == 2:
  605. tag, cmd = args
  606. elif len(args) == 1:
  607. tag = args[0]
  608. self.sendBadResponse(tag, b"Missing command")
  609. return None
  610. else:
  611. self.sendBadResponse(None, b"Null command")
  612. return None
  613. cmd = cmd.upper()
  614. try:
  615. return self.dispatchCommand(tag, cmd, rest)
  616. except IllegalClientResponse as e:
  617. self.sendBadResponse(tag, b"Illegal syntax: " + networkString(str(e)))
  618. except IllegalOperation as e:
  619. self.sendNegativeResponse(
  620. tag, b"Illegal operation: " + networkString(str(e))
  621. )
  622. except IllegalMailboxEncoding as e:
  623. self.sendNegativeResponse(
  624. tag, b"Illegal mailbox name: " + networkString(str(e))
  625. )
  626. def parse_pending(self, line):
  627. d = self._pendingLiteral
  628. self._pendingLiteral = None
  629. self.parseState = "command"
  630. d.callback(line)
  631. def dispatchCommand(self, tag, cmd, rest, uid=None):
  632. f = self.lookupCommand(cmd)
  633. if f:
  634. fn = f[0]
  635. parseargs = f[1:]
  636. self.__doCommand(tag, fn, [self, tag], parseargs, rest, uid)
  637. else:
  638. self.sendBadResponse(tag, b"Unsupported command")
  639. def lookupCommand(self, cmd):
  640. return getattr(self, "_".join((self.state, nativeString(cmd.upper()))), None)
  641. def __doCommand(self, tag, handler, args, parseargs, line, uid):
  642. for (i, arg) in enumerate(parseargs):
  643. if callable(arg):
  644. parseargs = parseargs[i + 1 :]
  645. maybeDeferred(arg, self, line).addCallback(
  646. self.__cbDispatch, tag, handler, args, parseargs, uid
  647. ).addErrback(self.__ebDispatch, tag)
  648. return
  649. else:
  650. args.append(arg)
  651. if line:
  652. # Too many arguments
  653. raise IllegalClientResponse("Too many arguments for command: " + repr(line))
  654. if uid is not None:
  655. handler(uid=uid, *args)
  656. else:
  657. handler(*args)
  658. def __cbDispatch(self, result, tag, fn, args, parseargs, uid):
  659. (arg, rest) = result
  660. args.append(arg)
  661. self.__doCommand(tag, fn, args, parseargs, rest, uid)
  662. def __ebDispatch(self, failure, tag):
  663. if failure.check(IllegalClientResponse):
  664. self.sendBadResponse(
  665. tag, b"Illegal syntax: " + networkString(str(failure.value))
  666. )
  667. elif failure.check(IllegalOperation):
  668. self.sendNegativeResponse(
  669. tag, b"Illegal operation: " + networkString(str(failure.value))
  670. )
  671. elif failure.check(IllegalMailboxEncoding):
  672. self.sendNegativeResponse(
  673. tag, b"Illegal mailbox name: " + networkString(str(failure.value))
  674. )
  675. else:
  676. self.sendBadResponse(
  677. tag, b"Server error: " + networkString(str(failure.value))
  678. )
  679. log.err(failure)
  680. def _stringLiteral(self, size):
  681. if size > self._literalStringLimit:
  682. raise IllegalClientResponse(
  683. "Literal too long! I accept at most %d octets"
  684. % (self._literalStringLimit,)
  685. )
  686. d = defer.Deferred()
  687. self.parseState = "pending"
  688. self._pendingLiteral = LiteralString(size, d)
  689. self.sendContinuationRequest(
  690. networkString("Ready for %d octets of text" % size)
  691. )
  692. self.setRawMode()
  693. return d
  694. def _fileLiteral(self, size):
  695. d = defer.Deferred()
  696. self.parseState = "pending"
  697. self._pendingLiteral = LiteralFile(size, d)
  698. self.sendContinuationRequest(
  699. networkString("Ready for %d octets of data" % size)
  700. )
  701. self.setRawMode()
  702. return d
  703. def arg_finalastring(self, line):
  704. """
  705. Parse an astring from line that represents a command's final
  706. argument. This special case exists to enable parsing empty
  707. string literals.
  708. @param line: A line that contains a string literal.
  709. @type line: L{bytes}
  710. @return: A 2-tuple containing the parsed argument and any
  711. trailing data, or a L{Deferred} that fires with that
  712. 2-tuple
  713. @rtype: L{tuple} of (L{bytes}, L{bytes}) or a L{Deferred}
  714. @see: https://twistedmatrix.com/trac/ticket/9207
  715. """
  716. return self.arg_astring(line, final=True)
  717. def arg_astring(self, line, final=False):
  718. """
  719. Parse an astring from the line, return (arg, rest), possibly
  720. via a deferred (to handle literals)
  721. @param line: A line that contains a string literal.
  722. @type line: L{bytes}
  723. @param final: Is this the final argument?
  724. @type final L{bool}
  725. @return: A 2-tuple containing the parsed argument and any
  726. trailing data, or a L{Deferred} that fires with that
  727. 2-tuple
  728. @rtype: L{tuple} of (L{bytes}, L{bytes}) or a L{Deferred}
  729. """
  730. line = line.strip()
  731. if not line:
  732. raise IllegalClientResponse("Missing argument")
  733. d = None
  734. arg, rest = None, None
  735. if line[0:1] == b'"':
  736. try:
  737. spam, arg, rest = line.split(b'"', 2)
  738. rest = rest[1:] # Strip space
  739. except ValueError:
  740. raise IllegalClientResponse("Unmatched quotes")
  741. elif line[0:1] == b"{":
  742. # literal
  743. if line[-1:] != b"}":
  744. raise IllegalClientResponse("Malformed literal")
  745. try:
  746. size = int(line[1:-1])
  747. except ValueError:
  748. raise IllegalClientResponse("Bad literal size: " + repr(line[1:-1]))
  749. if final and not size:
  750. return (b"", b"")
  751. d = self._stringLiteral(size)
  752. else:
  753. arg = line.split(b" ", 1)
  754. if len(arg) == 1:
  755. arg.append(b"")
  756. arg, rest = arg
  757. return d or (arg, rest)
  758. # ATOM: Any CHAR except ( ) { % * " \ ] CTL SP (CHAR is 7bit)
  759. atomre = re.compile(
  760. b"(?P<atom>[" + re.escape(_atomChars) + b"]+)( (?P<rest>.*$)|$)"
  761. )
  762. def arg_atom(self, line):
  763. """
  764. Parse an atom from the line
  765. """
  766. if not line:
  767. raise IllegalClientResponse("Missing argument")
  768. m = self.atomre.match(line)
  769. if m:
  770. return m.group("atom"), m.group("rest")
  771. else:
  772. raise IllegalClientResponse("Malformed ATOM")
  773. def arg_plist(self, line):
  774. """
  775. Parse a (non-nested) parenthesised list from the line
  776. """
  777. if not line:
  778. raise IllegalClientResponse("Missing argument")
  779. if line[:1] != b"(":
  780. raise IllegalClientResponse("Missing parenthesis")
  781. i = line.find(b")")
  782. if i == -1:
  783. raise IllegalClientResponse("Mismatched parenthesis")
  784. return (parseNestedParens(line[1:i], 0), line[i + 2 :])
  785. def arg_literal(self, line):
  786. """
  787. Parse a literal from the line
  788. """
  789. if not line:
  790. raise IllegalClientResponse("Missing argument")
  791. if line[:1] != b"{":
  792. raise IllegalClientResponse("Missing literal")
  793. if line[-1:] != b"}":
  794. raise IllegalClientResponse("Malformed literal")
  795. try:
  796. size = int(line[1:-1])
  797. except ValueError:
  798. raise IllegalClientResponse(f"Bad literal size: {line[1:-1]!r}")
  799. return self._fileLiteral(size)
  800. def arg_searchkeys(self, line):
  801. """
  802. searchkeys
  803. """
  804. query = parseNestedParens(line)
  805. # XXX Should really use list of search terms and parse into
  806. # a proper tree
  807. return (query, b"")
  808. def arg_seqset(self, line):
  809. """
  810. sequence-set
  811. """
  812. rest = b""
  813. arg = line.split(b" ", 1)
  814. if len(arg) == 2:
  815. rest = arg[1]
  816. arg = arg[0]
  817. try:
  818. return (parseIdList(arg), rest)
  819. except IllegalIdentifierError as e:
  820. raise IllegalClientResponse("Bad message number " + str(e))
  821. def arg_fetchatt(self, line):
  822. """
  823. fetch-att
  824. """
  825. p = _FetchParser()
  826. p.parseString(line)
  827. return (p.result, b"")
  828. def arg_flaglist(self, line):
  829. """
  830. Flag part of store-att-flag
  831. """
  832. flags = []
  833. if line[0:1] == b"(":
  834. if line[-1:] != b")":
  835. raise IllegalClientResponse("Mismatched parenthesis")
  836. line = line[1:-1]
  837. while line:
  838. m = self.atomre.search(line)
  839. if not m:
  840. raise IllegalClientResponse("Malformed flag")
  841. if line[0:1] == b"\\" and m.start() == 1:
  842. flags.append(b"\\" + m.group("atom"))
  843. elif m.start() == 0:
  844. flags.append(m.group("atom"))
  845. else:
  846. raise IllegalClientResponse("Malformed flag")
  847. line = m.group("rest")
  848. return (flags, b"")
  849. def arg_line(self, line):
  850. """
  851. Command line of UID command
  852. """
  853. return (line, b"")
  854. def opt_plist(self, line):
  855. """
  856. Optional parenthesised list
  857. """
  858. if line.startswith(b"("):
  859. return self.arg_plist(line)
  860. else:
  861. return (None, line)
  862. def opt_datetime(self, line):
  863. """
  864. Optional date-time string
  865. """
  866. if line.startswith(b'"'):
  867. try:
  868. spam, date, rest = line.split(b'"', 2)
  869. except ValueError:
  870. raise IllegalClientResponse("Malformed date-time")
  871. return (date, rest[1:])
  872. else:
  873. return (None, line)
  874. def opt_charset(self, line):
  875. """
  876. Optional charset of SEARCH command
  877. """
  878. if line[:7].upper() == b"CHARSET":
  879. arg = line.split(b" ", 2)
  880. if len(arg) == 1:
  881. raise IllegalClientResponse("Missing charset identifier")
  882. if len(arg) == 2:
  883. arg.append(b"")
  884. spam, arg, rest = arg
  885. return (arg, rest)
  886. else:
  887. return (None, line)
  888. def sendServerGreeting(self):
  889. msg = b"[CAPABILITY " + b" ".join(self.listCapabilities()) + b"] " + self.IDENT
  890. self.sendPositiveResponse(message=msg)
  891. def sendBadResponse(self, tag=None, message=b""):
  892. self._respond(b"BAD", tag, message)
  893. def sendPositiveResponse(self, tag=None, message=b""):
  894. self._respond(b"OK", tag, message)
  895. def sendNegativeResponse(self, tag=None, message=b""):
  896. self._respond(b"NO", tag, message)
  897. def sendUntaggedResponse(self, message, isAsync=None, **kwargs):
  898. isAsync = _get_async_param(isAsync, **kwargs)
  899. if not isAsync or (self.blocked is None):
  900. self._respond(message, None, None)
  901. else:
  902. self._queuedAsync.append(message)
  903. def sendContinuationRequest(self, msg=b"Ready for additional command text"):
  904. if msg:
  905. self.sendLine(b"+ " + msg)
  906. else:
  907. self.sendLine(b"+")
  908. def _respond(self, state, tag, message):
  909. if state in (b"OK", b"NO", b"BAD") and self._queuedAsync:
  910. lines = self._queuedAsync
  911. self._queuedAsync = []
  912. for msg in lines:
  913. self._respond(msg, None, None)
  914. if not tag:
  915. tag = b"*"
  916. if message:
  917. self.sendLine(b" ".join((tag, state, message)))
  918. else:
  919. self.sendLine(b" ".join((tag, state)))
  920. def listCapabilities(self):
  921. caps = [b"IMAP4rev1"]
  922. for c, v in self.capabilities().items():
  923. if v is None:
  924. caps.append(c)
  925. elif len(v):
  926. caps.extend([(c + b"=" + cap) for cap in v])
  927. return caps
  928. def do_CAPABILITY(self, tag):
  929. self.sendUntaggedResponse(b"CAPABILITY " + b" ".join(self.listCapabilities()))
  930. self.sendPositiveResponse(tag, b"CAPABILITY completed")
  931. unauth_CAPABILITY = (do_CAPABILITY,)
  932. auth_CAPABILITY = unauth_CAPABILITY
  933. select_CAPABILITY = unauth_CAPABILITY
  934. logout_CAPABILITY = unauth_CAPABILITY
  935. def do_LOGOUT(self, tag):
  936. self.sendUntaggedResponse(b"BYE Nice talking to you")
  937. self.sendPositiveResponse(tag, b"LOGOUT successful")
  938. self.transport.loseConnection()
  939. unauth_LOGOUT = (do_LOGOUT,)
  940. auth_LOGOUT = unauth_LOGOUT
  941. select_LOGOUT = unauth_LOGOUT
  942. logout_LOGOUT = unauth_LOGOUT
  943. def do_NOOP(self, tag):
  944. self.sendPositiveResponse(tag, b"NOOP No operation performed")
  945. unauth_NOOP = (do_NOOP,)
  946. auth_NOOP = unauth_NOOP
  947. select_NOOP = unauth_NOOP
  948. logout_NOOP = unauth_NOOP
  949. def do_AUTHENTICATE(self, tag, args):
  950. args = args.upper().strip()
  951. if args not in self.challengers:
  952. self.sendNegativeResponse(tag, b"AUTHENTICATE method unsupported")
  953. else:
  954. self.authenticate(self.challengers[args](), tag)
  955. unauth_AUTHENTICATE = (do_AUTHENTICATE, arg_atom)
  956. def authenticate(self, chal, tag):
  957. if self.portal is None:
  958. self.sendNegativeResponse(tag, b"Temporary authentication failure")
  959. return
  960. self._setupChallenge(chal, tag)
  961. def _setupChallenge(self, chal, tag):
  962. try:
  963. challenge = chal.getChallenge()
  964. except Exception as e:
  965. self.sendBadResponse(tag, b"Server error: " + networkString(str(e)))
  966. else:
  967. coded = encodebytes(challenge)[:-1]
  968. self.parseState = "pending"
  969. self._pendingLiteral = defer.Deferred()
  970. self.sendContinuationRequest(coded)
  971. self._pendingLiteral.addCallback(self.__cbAuthChunk, chal, tag)
  972. self._pendingLiteral.addErrback(self.__ebAuthChunk, tag)
  973. def __cbAuthChunk(self, result, chal, tag):
  974. try:
  975. uncoded = decodebytes(result)
  976. except binascii.Error:
  977. raise IllegalClientResponse("Malformed Response - not base64")
  978. chal.setResponse(uncoded)
  979. if chal.moreChallenges():
  980. self._setupChallenge(chal, tag)
  981. else:
  982. self.portal.login(chal, None, IAccount).addCallbacks(
  983. self.__cbAuthResp, self.__ebAuthResp, (tag,), None, (tag,), None
  984. )
  985. def __cbAuthResp(self, result, tag):
  986. (iface, avatar, logout) = result
  987. assert iface is IAccount, "IAccount is the only supported interface"
  988. self.account = avatar
  989. self.state = "auth"
  990. self._onLogout = logout
  991. self.sendPositiveResponse(tag, b"Authentication successful")
  992. self.setTimeout(self.POSTAUTH_TIMEOUT)
  993. def __ebAuthResp(self, failure, tag):
  994. if failure.check(UnauthorizedLogin):
  995. self.sendNegativeResponse(tag, b"Authentication failed: unauthorized")
  996. elif failure.check(UnhandledCredentials):
  997. self.sendNegativeResponse(
  998. tag, b"Authentication failed: server misconfigured"
  999. )
  1000. else:
  1001. self.sendBadResponse(tag, b"Server error: login failed unexpectedly")
  1002. log.err(failure)
  1003. def __ebAuthChunk(self, failure, tag):
  1004. self.sendNegativeResponse(
  1005. tag, b"Authentication failed: " + networkString(str(failure.value))
  1006. )
  1007. def do_STARTTLS(self, tag):
  1008. if self.startedTLS:
  1009. self.sendNegativeResponse(tag, b"TLS already negotiated")
  1010. elif self.ctx and self.canStartTLS:
  1011. self.sendPositiveResponse(tag, b"Begin TLS negotiation now")
  1012. self.transport.startTLS(self.ctx)
  1013. self.startedTLS = True
  1014. self.challengers = self.challengers.copy()
  1015. if b"LOGIN" not in self.challengers:
  1016. self.challengers[b"LOGIN"] = LOGINCredentials
  1017. if b"PLAIN" not in self.challengers:
  1018. self.challengers[b"PLAIN"] = PLAINCredentials
  1019. else:
  1020. self.sendNegativeResponse(tag, b"TLS not available")
  1021. unauth_STARTTLS = (do_STARTTLS,)
  1022. def do_LOGIN(self, tag, user, passwd):
  1023. if b"LOGINDISABLED" in self.capabilities():
  1024. self.sendBadResponse(tag, b"LOGIN is disabled before STARTTLS")
  1025. return
  1026. maybeDeferred(self.authenticateLogin, user, passwd).addCallback(
  1027. self.__cbLogin, tag
  1028. ).addErrback(self.__ebLogin, tag)
  1029. unauth_LOGIN = (do_LOGIN, arg_astring, arg_finalastring)
  1030. def authenticateLogin(self, user, passwd):
  1031. """
  1032. Lookup the account associated with the given parameters
  1033. Override this method to define the desired authentication behavior.
  1034. The default behavior is to defer authentication to C{self.portal}
  1035. if it is not None, or to deny the login otherwise.
  1036. @type user: L{str}
  1037. @param user: The username to lookup
  1038. @type passwd: L{str}
  1039. @param passwd: The password to login with
  1040. """
  1041. if self.portal:
  1042. return self.portal.login(
  1043. credentials.UsernamePassword(user, passwd), None, IAccount
  1044. )
  1045. raise UnauthorizedLogin()
  1046. def __cbLogin(self, result, tag):
  1047. (iface, avatar, logout) = result
  1048. if iface is not IAccount:
  1049. self.sendBadResponse(tag, b"Server error: login returned unexpected value")
  1050. log.err(f"__cbLogin called with {iface!r}, IAccount expected")
  1051. else:
  1052. self.account = avatar
  1053. self._onLogout = logout
  1054. self.sendPositiveResponse(tag, b"LOGIN succeeded")
  1055. self.state = "auth"
  1056. self.setTimeout(self.POSTAUTH_TIMEOUT)
  1057. def __ebLogin(self, failure, tag):
  1058. if failure.check(UnauthorizedLogin):
  1059. self.sendNegativeResponse(tag, b"LOGIN failed")
  1060. else:
  1061. self.sendBadResponse(
  1062. tag, b"Server error: " + networkString(str(failure.value))
  1063. )
  1064. log.err(failure)
  1065. def do_NAMESPACE(self, tag):
  1066. personal = public = shared = None
  1067. np = INamespacePresenter(self.account, None)
  1068. if np is not None:
  1069. personal = np.getPersonalNamespaces()
  1070. public = np.getSharedNamespaces()
  1071. shared = np.getSharedNamespaces()
  1072. self.sendUntaggedResponse(
  1073. b"NAMESPACE " + collapseNestedLists([personal, public, shared])
  1074. )
  1075. self.sendPositiveResponse(tag, b"NAMESPACE command completed")
  1076. auth_NAMESPACE = (do_NAMESPACE,)
  1077. select_NAMESPACE = auth_NAMESPACE
  1078. def _selectWork(self, tag, name, rw, cmdName):
  1079. if self.mbox:
  1080. self.mbox.removeListener(self)
  1081. cmbx = ICloseableMailbox(self.mbox, None)
  1082. if cmbx is not None:
  1083. maybeDeferred(cmbx.close).addErrback(log.err)
  1084. self.mbox = None
  1085. self.state = "auth"
  1086. name = _parseMbox(name)
  1087. maybeDeferred(self.account.select, _parseMbox(name), rw).addCallback(
  1088. self._cbSelectWork, cmdName, tag
  1089. ).addErrback(self._ebSelectWork, cmdName, tag)
  1090. def _ebSelectWork(self, failure, cmdName, tag):
  1091. self.sendBadResponse(tag, cmdName + b" failed: Server error")
  1092. log.err(failure)
  1093. def _cbSelectWork(self, mbox, cmdName, tag):
  1094. if mbox is None:
  1095. self.sendNegativeResponse(tag, b"No such mailbox")
  1096. return
  1097. if "\\noselect" in [s.lower() for s in mbox.getFlags()]:
  1098. self.sendNegativeResponse(tag, "Mailbox cannot be selected")
  1099. return
  1100. flags = [networkString(flag) for flag in mbox.getFlags()]
  1101. self.sendUntaggedResponse(b"%d EXISTS" % (mbox.getMessageCount(),))
  1102. self.sendUntaggedResponse(b"%d RECENT" % (mbox.getRecentCount(),))
  1103. self.sendUntaggedResponse(b"FLAGS (" + b" ".join(flags) + b")")
  1104. self.sendPositiveResponse(None, b"[UIDVALIDITY %d]" % (mbox.getUIDValidity(),))
  1105. s = mbox.isWriteable() and b"READ-WRITE" or b"READ-ONLY"
  1106. mbox.addListener(self)
  1107. self.sendPositiveResponse(tag, b"[" + s + b"] " + cmdName + b" successful")
  1108. self.state = "select"
  1109. self.mbox = mbox
  1110. auth_SELECT = (_selectWork, arg_astring, 1, b"SELECT")
  1111. select_SELECT = auth_SELECT
  1112. auth_EXAMINE = (_selectWork, arg_astring, 0, b"EXAMINE")
  1113. select_EXAMINE = auth_EXAMINE
  1114. def do_IDLE(self, tag):
  1115. self.sendContinuationRequest(None)
  1116. self.parseTag = tag
  1117. self.lastState = self.parseState
  1118. self.parseState = "idle"
  1119. def parse_idle(self, *args):
  1120. self.parseState = self.lastState
  1121. del self.lastState
  1122. self.sendPositiveResponse(self.parseTag, b"IDLE terminated")
  1123. del self.parseTag
  1124. select_IDLE = (do_IDLE,)
  1125. auth_IDLE = select_IDLE
  1126. def do_CREATE(self, tag, name):
  1127. name = _parseMbox(name)
  1128. try:
  1129. result = self.account.create(name)
  1130. except MailboxException as c:
  1131. self.sendNegativeResponse(tag, networkString(str(c)))
  1132. except BaseException:
  1133. self.sendBadResponse(
  1134. tag, b"Server error encountered while creating mailbox"
  1135. )
  1136. log.err()
  1137. else:
  1138. if result:
  1139. self.sendPositiveResponse(tag, b"Mailbox created")
  1140. else:
  1141. self.sendNegativeResponse(tag, b"Mailbox not created")
  1142. auth_CREATE = (do_CREATE, arg_finalastring)
  1143. select_CREATE = auth_CREATE
  1144. def do_DELETE(self, tag, name):
  1145. name = _parseMbox(name)
  1146. if name.lower() == "inbox":
  1147. self.sendNegativeResponse(tag, b"You cannot delete the inbox")
  1148. return
  1149. try:
  1150. self.account.delete(name)
  1151. except MailboxException as m:
  1152. self.sendNegativeResponse(tag, str(m).encode("imap4-utf-7"))
  1153. except BaseException:
  1154. self.sendBadResponse(
  1155. tag, b"Server error encountered while deleting mailbox"
  1156. )
  1157. log.err()
  1158. else:
  1159. self.sendPositiveResponse(tag, b"Mailbox deleted")
  1160. auth_DELETE = (do_DELETE, arg_finalastring)
  1161. select_DELETE = auth_DELETE
  1162. def do_RENAME(self, tag, oldname, newname):
  1163. oldname, newname = (_parseMbox(n) for n in (oldname, newname))
  1164. if oldname.lower() == "inbox" or newname.lower() == "inbox":
  1165. self.sendNegativeResponse(
  1166. tag, b"You cannot rename the inbox, or rename another mailbox to inbox."
  1167. )
  1168. return
  1169. try:
  1170. self.account.rename(oldname, newname)
  1171. except TypeError:
  1172. self.sendBadResponse(tag, b"Invalid command syntax")
  1173. except MailboxException as m:
  1174. self.sendNegativeResponse(tag, networkString(str(m)))
  1175. except BaseException:
  1176. self.sendBadResponse(
  1177. tag, b"Server error encountered while renaming mailbox"
  1178. )
  1179. log.err()
  1180. else:
  1181. self.sendPositiveResponse(tag, b"Mailbox renamed")
  1182. auth_RENAME = (do_RENAME, arg_astring, arg_finalastring)
  1183. select_RENAME = auth_RENAME
  1184. def do_SUBSCRIBE(self, tag, name):
  1185. name = _parseMbox(name)
  1186. try:
  1187. self.account.subscribe(name)
  1188. except MailboxException as m:
  1189. self.sendNegativeResponse(tag, networkString(str(m)))
  1190. except BaseException:
  1191. self.sendBadResponse(
  1192. tag, b"Server error encountered while subscribing to mailbox"
  1193. )
  1194. log.err()
  1195. else:
  1196. self.sendPositiveResponse(tag, b"Subscribed")
  1197. auth_SUBSCRIBE = (do_SUBSCRIBE, arg_finalastring)
  1198. select_SUBSCRIBE = auth_SUBSCRIBE
  1199. def do_UNSUBSCRIBE(self, tag, name):
  1200. name = _parseMbox(name)
  1201. try:
  1202. self.account.unsubscribe(name)
  1203. except MailboxException as m:
  1204. self.sendNegativeResponse(tag, networkString(str(m)))
  1205. except BaseException:
  1206. self.sendBadResponse(
  1207. tag, b"Server error encountered while unsubscribing from mailbox"
  1208. )
  1209. log.err()
  1210. else:
  1211. self.sendPositiveResponse(tag, b"Unsubscribed")
  1212. auth_UNSUBSCRIBE = (do_UNSUBSCRIBE, arg_finalastring)
  1213. select_UNSUBSCRIBE = auth_UNSUBSCRIBE
  1214. def _listWork(self, tag, ref, mbox, sub, cmdName):
  1215. mbox = _parseMbox(mbox)
  1216. ref = _parseMbox(ref)
  1217. maybeDeferred(self.account.listMailboxes, ref, mbox).addCallback(
  1218. self._cbListWork, tag, sub, cmdName
  1219. ).addErrback(self._ebListWork, tag)
  1220. def _cbListWork(self, mailboxes, tag, sub, cmdName):
  1221. for (name, box) in mailboxes:
  1222. if not sub or self.account.isSubscribed(name):
  1223. flags = [networkString(flag) for flag in box.getFlags()]
  1224. delim = box.getHierarchicalDelimiter().encode("imap4-utf-7")
  1225. resp = (
  1226. DontQuoteMe(cmdName),
  1227. map(DontQuoteMe, flags),
  1228. delim,
  1229. name.encode("imap4-utf-7"),
  1230. )
  1231. self.sendUntaggedResponse(collapseNestedLists(resp))
  1232. self.sendPositiveResponse(tag, cmdName + b" completed")
  1233. def _ebListWork(self, failure, tag):
  1234. self.sendBadResponse(tag, b"Server error encountered while listing mailboxes.")
  1235. log.err(failure)
  1236. auth_LIST = (_listWork, arg_astring, arg_astring, 0, b"LIST")
  1237. select_LIST = auth_LIST
  1238. auth_LSUB = (_listWork, arg_astring, arg_astring, 1, b"LSUB")
  1239. select_LSUB = auth_LSUB
  1240. def do_STATUS(self, tag, mailbox, names):
  1241. nativeNames = []
  1242. for name in names:
  1243. nativeNames.append(nativeString(name))
  1244. mailbox = _parseMbox(mailbox)
  1245. maybeDeferred(self.account.select, mailbox, 0).addCallback(
  1246. self._cbStatusGotMailbox, tag, mailbox, nativeNames
  1247. ).addErrback(self._ebStatusGotMailbox, tag)
  1248. def _cbStatusGotMailbox(self, mbox, tag, mailbox, names):
  1249. if mbox:
  1250. maybeDeferred(mbox.requestStatus, names).addCallbacks(
  1251. self.__cbStatus,
  1252. self.__ebStatus,
  1253. (tag, mailbox),
  1254. None,
  1255. (tag, mailbox),
  1256. None,
  1257. )
  1258. else:
  1259. self.sendNegativeResponse(tag, b"Could not open mailbox")
  1260. def _ebStatusGotMailbox(self, failure, tag):
  1261. self.sendBadResponse(tag, b"Server error encountered while opening mailbox.")
  1262. log.err(failure)
  1263. auth_STATUS = (do_STATUS, arg_astring, arg_plist)
  1264. select_STATUS = auth_STATUS
  1265. def __cbStatus(self, status, tag, box):
  1266. # STATUS names should only be ASCII
  1267. line = networkString(" ".join(["%s %s" % x for x in status.items()]))
  1268. self.sendUntaggedResponse(
  1269. b"STATUS " + box.encode("imap4-utf-7") + b" (" + line + b")"
  1270. )
  1271. self.sendPositiveResponse(tag, b"STATUS complete")
  1272. def __ebStatus(self, failure, tag, box):
  1273. self.sendBadResponse(
  1274. tag, b"STATUS " + box + b" failed: " + networkString(str(failure.value))
  1275. )
  1276. def do_APPEND(self, tag, mailbox, flags, date, message):
  1277. mailbox = _parseMbox(mailbox)
  1278. maybeDeferred(self.account.select, mailbox).addCallback(
  1279. self._cbAppendGotMailbox, tag, flags, date, message
  1280. ).addErrback(self._ebAppendGotMailbox, tag)
  1281. def _cbAppendGotMailbox(self, mbox, tag, flags, date, message):
  1282. if not mbox:
  1283. self.sendNegativeResponse(tag, "[TRYCREATE] No such mailbox")
  1284. return
  1285. decodedFlags = [nativeString(flag) for flag in flags]
  1286. d = mbox.addMessage(message, decodedFlags, date)
  1287. d.addCallback(self.__cbAppend, tag, mbox)
  1288. d.addErrback(self.__ebAppend, tag)
  1289. def _ebAppendGotMailbox(self, failure, tag):
  1290. self.sendBadResponse(tag, b"Server error encountered while opening mailbox.")
  1291. log.err(failure)
  1292. auth_APPEND = (do_APPEND, arg_astring, opt_plist, opt_datetime, arg_literal)
  1293. select_APPEND = auth_APPEND
  1294. def __cbAppend(self, result, tag, mbox):
  1295. self.sendUntaggedResponse(b"%d EXISTS" % (mbox.getMessageCount(),))
  1296. self.sendPositiveResponse(tag, b"APPEND complete")
  1297. def __ebAppend(self, failure, tag):
  1298. self.sendBadResponse(
  1299. tag, b"APPEND failed: " + networkString(str(failure.value))
  1300. )
  1301. def do_CHECK(self, tag):
  1302. d = self.checkpoint()
  1303. if d is None:
  1304. self.__cbCheck(None, tag)
  1305. else:
  1306. d.addCallbacks(
  1307. self.__cbCheck, self.__ebCheck, callbackArgs=(tag,), errbackArgs=(tag,)
  1308. )
  1309. select_CHECK = (do_CHECK,)
  1310. def __cbCheck(self, result, tag):
  1311. self.sendPositiveResponse(tag, b"CHECK completed")
  1312. def __ebCheck(self, failure, tag):
  1313. self.sendBadResponse(tag, b"CHECK failed: " + networkString(str(failure.value)))
  1314. def checkpoint(self):
  1315. """
  1316. Called when the client issues a CHECK command.
  1317. This should perform any checkpoint operations required by the server.
  1318. It may be a long running operation, but may not block. If it returns
  1319. a deferred, the client will only be informed of success (or failure)
  1320. when the deferred's callback (or errback) is invoked.
  1321. """
  1322. return None
  1323. def do_CLOSE(self, tag):
  1324. d = None
  1325. if self.mbox.isWriteable():
  1326. d = maybeDeferred(self.mbox.expunge)
  1327. cmbx = ICloseableMailbox(self.mbox, None)
  1328. if cmbx is not None:
  1329. if d is not None:
  1330. d.addCallback(lambda result: cmbx.close())
  1331. else:
  1332. d = maybeDeferred(cmbx.close)
  1333. if d is not None:
  1334. d.addCallbacks(self.__cbClose, self.__ebClose, (tag,), None, (tag,), None)
  1335. else:
  1336. self.__cbClose(None, tag)
  1337. select_CLOSE = (do_CLOSE,)
  1338. def __cbClose(self, result, tag):
  1339. self.sendPositiveResponse(tag, b"CLOSE completed")
  1340. self.mbox.removeListener(self)
  1341. self.mbox = None
  1342. self.state = "auth"
  1343. def __ebClose(self, failure, tag):
  1344. self.sendBadResponse(tag, b"CLOSE failed: " + networkString(str(failure.value)))
  1345. def do_EXPUNGE(self, tag):
  1346. if self.mbox.isWriteable():
  1347. maybeDeferred(self.mbox.expunge).addCallbacks(
  1348. self.__cbExpunge, self.__ebExpunge, (tag,), None, (tag,), None
  1349. )
  1350. else:
  1351. self.sendNegativeResponse(tag, b"EXPUNGE ignored on read-only mailbox")
  1352. select_EXPUNGE = (do_EXPUNGE,)
  1353. def __cbExpunge(self, result, tag):
  1354. for e in result:
  1355. self.sendUntaggedResponse(b"%d EXPUNGE" % (e,))
  1356. self.sendPositiveResponse(tag, b"EXPUNGE completed")
  1357. def __ebExpunge(self, failure, tag):
  1358. self.sendBadResponse(
  1359. tag, b"EXPUNGE failed: " + networkString(str(failure.value))
  1360. )
  1361. log.err(failure)
  1362. def do_SEARCH(self, tag, charset, query, uid=0):
  1363. sm = ISearchableMailbox(self.mbox, None)
  1364. if sm is not None:
  1365. maybeDeferred(sm.search, query, uid=uid).addCallback(
  1366. self.__cbSearch, tag, self.mbox, uid
  1367. ).addErrback(self.__ebSearch, tag)
  1368. else:
  1369. # that's not the ideal way to get all messages, there should be a
  1370. # method on mailboxes that gives you all of them
  1371. s = parseIdList(b"1:*")
  1372. maybeDeferred(self.mbox.fetch, s, uid=uid).addCallback(
  1373. self.__cbManualSearch, tag, self.mbox, query, uid
  1374. ).addErrback(self.__ebSearch, tag)
  1375. select_SEARCH = (do_SEARCH, opt_charset, arg_searchkeys)
  1376. def __cbSearch(self, result, tag, mbox, uid):
  1377. if uid:
  1378. result = map(mbox.getUID, result)
  1379. ids = networkString(" ".join([str(i) for i in result]))
  1380. self.sendUntaggedResponse(b"SEARCH " + ids)
  1381. self.sendPositiveResponse(tag, b"SEARCH completed")
  1382. def __cbManualSearch(self, result, tag, mbox, query, uid, searchResults=None):
  1383. """
  1384. Apply the search filter to a set of messages. Send the response to the
  1385. client.
  1386. @type result: L{list} of L{tuple} of (L{int}, provider of
  1387. L{imap4.IMessage})
  1388. @param result: A list two tuples of messages with their sequence ids,
  1389. sorted by the ids in descending order.
  1390. @type tag: L{str}
  1391. @param tag: A command tag.
  1392. @type mbox: Provider of L{imap4.IMailbox}
  1393. @param mbox: The searched mailbox.
  1394. @type query: L{list}
  1395. @param query: A list representing the parsed form of the search query.
  1396. @param uid: A flag indicating whether the search is over message
  1397. sequence numbers or UIDs.
  1398. @type searchResults: L{list}
  1399. @param searchResults: The search results so far or L{None} if no
  1400. results yet.
  1401. """
  1402. if searchResults is None:
  1403. searchResults = []
  1404. i = 0
  1405. # result is a list of tuples (sequenceId, Message)
  1406. lastSequenceId = result and result[-1][0]
  1407. lastMessageId = result and result[-1][1].getUID()
  1408. for (i, (msgId, msg)) in list(zip(range(5), result)):
  1409. # searchFilter and singleSearchStep will mutate the query. Dang.
  1410. # Copy it here or else things will go poorly for subsequent
  1411. # messages.
  1412. if self._searchFilter(
  1413. copy.deepcopy(query), msgId, msg, lastSequenceId, lastMessageId
  1414. ):
  1415. searchResults.append(b"%d" % (msg.getUID() if uid else msgId,))
  1416. if i == 4:
  1417. from twisted.internet import reactor
  1418. reactor.callLater(
  1419. 0,
  1420. self.__cbManualSearch,
  1421. list(result[5:]),
  1422. tag,
  1423. mbox,
  1424. query,
  1425. uid,
  1426. searchResults,
  1427. )
  1428. else:
  1429. if searchResults:
  1430. self.sendUntaggedResponse(b"SEARCH " + b" ".join(searchResults))
  1431. self.sendPositiveResponse(tag, b"SEARCH completed")
  1432. def _searchFilter(self, query, id, msg, lastSequenceId, lastMessageId):
  1433. """
  1434. Pop search terms from the beginning of C{query} until there are none
  1435. left and apply them to the given message.
  1436. @param query: A list representing the parsed form of the search query.
  1437. @param id: The sequence number of the message being checked.
  1438. @param msg: The message being checked.
  1439. @type lastSequenceId: L{int}
  1440. @param lastSequenceId: The highest sequence number of any message in
  1441. the mailbox being searched.
  1442. @type lastMessageId: L{int}
  1443. @param lastMessageId: The highest UID of any message in the mailbox
  1444. being searched.
  1445. @return: Boolean indicating whether all of the query terms match the
  1446. message.
  1447. """
  1448. while query:
  1449. if not self._singleSearchStep(
  1450. query, id, msg, lastSequenceId, lastMessageId
  1451. ):
  1452. return False
  1453. return True
  1454. def _singleSearchStep(self, query, msgId, msg, lastSequenceId, lastMessageId):
  1455. """
  1456. Pop one search term from the beginning of C{query} (possibly more than
  1457. one element) and return whether it matches the given message.
  1458. @param query: A list representing the parsed form of the search query.
  1459. @param msgId: The sequence number of the message being checked.
  1460. @param msg: The message being checked.
  1461. @param lastSequenceId: The highest sequence number of any message in
  1462. the mailbox being searched.
  1463. @param lastMessageId: The highest UID of any message in the mailbox
  1464. being searched.
  1465. @return: Boolean indicating whether the query term matched the message.
  1466. """
  1467. q = query.pop(0)
  1468. if isinstance(q, list):
  1469. if not self._searchFilter(q, msgId, msg, lastSequenceId, lastMessageId):
  1470. return False
  1471. else:
  1472. c = q.upper()
  1473. if not c[:1].isalpha():
  1474. # A search term may be a word like ALL, ANSWERED, BCC, etc (see
  1475. # below) or it may be a message sequence set. Here we
  1476. # recognize a message sequence set "N:M".
  1477. messageSet = parseIdList(c, lastSequenceId)
  1478. return msgId in messageSet
  1479. else:
  1480. f = getattr(self, "search_" + nativeString(c), None)
  1481. if f is None:
  1482. raise IllegalQueryError(
  1483. "Invalid search command %s" % nativeString(c)
  1484. )
  1485. if c in self._requiresLastMessageInfo:
  1486. result = f(query, msgId, msg, (lastSequenceId, lastMessageId))
  1487. else:
  1488. result = f(query, msgId, msg)
  1489. if not result:
  1490. return False
  1491. return True
  1492. def search_ALL(self, query, id, msg):
  1493. """
  1494. Returns C{True} if the message matches the ALL search key (always).
  1495. @type query: A L{list} of L{str}
  1496. @param query: A list representing the parsed query string.
  1497. @type id: L{int}
  1498. @param id: The sequence number of the message being checked.
  1499. @type msg: Provider of L{imap4.IMessage}
  1500. """
  1501. return True
  1502. def search_ANSWERED(self, query, id, msg):
  1503. """
  1504. Returns C{True} if the message has been answered.
  1505. @type query: A L{list} of L{str}
  1506. @param query: A list representing the parsed query string.
  1507. @type id: L{int}
  1508. @param id: The sequence number of the message being checked.
  1509. @type msg: Provider of L{imap4.IMessage}
  1510. """
  1511. return "\\Answered" in msg.getFlags()
  1512. def search_BCC(self, query, id, msg):
  1513. """
  1514. Returns C{True} if the message has a BCC address matching the query.
  1515. @type query: A L{list} of L{str}
  1516. @param query: A list whose first element is a BCC L{str}
  1517. @type id: L{int}
  1518. @param id: The sequence number of the message being checked.
  1519. @type msg: Provider of L{imap4.IMessage}
  1520. """
  1521. bcc = msg.getHeaders(False, "bcc").get("bcc", "")
  1522. return bcc.lower().find(query.pop(0).lower()) != -1
  1523. def search_BEFORE(self, query, id, msg):
  1524. date = parseTime(query.pop(0))
  1525. return email.utils.parsedate(nativeString(msg.getInternalDate())) < date
  1526. def search_BODY(self, query, id, msg):
  1527. body = query.pop(0).lower()
  1528. return text.strFile(body, msg.getBodyFile(), False)
  1529. def search_CC(self, query, id, msg):
  1530. cc = msg.getHeaders(False, "cc").get("cc", "")
  1531. return cc.lower().find(query.pop(0).lower()) != -1
  1532. def search_DELETED(self, query, id, msg):
  1533. return "\\Deleted" in msg.getFlags()
  1534. def search_DRAFT(self, query, id, msg):
  1535. return "\\Draft" in msg.getFlags()
  1536. def search_FLAGGED(self, query, id, msg):
  1537. return "\\Flagged" in msg.getFlags()
  1538. def search_FROM(self, query, id, msg):
  1539. fm = msg.getHeaders(False, "from").get("from", "")
  1540. return fm.lower().find(query.pop(0).lower()) != -1
  1541. def search_HEADER(self, query, id, msg):
  1542. hdr = query.pop(0).lower()
  1543. hdr = msg.getHeaders(False, hdr).get(hdr, "")
  1544. return hdr.lower().find(query.pop(0).lower()) != -1
  1545. def search_KEYWORD(self, query, id, msg):
  1546. query.pop(0)
  1547. return False
  1548. def search_LARGER(self, query, id, msg):
  1549. return int(query.pop(0)) < msg.getSize()
  1550. def search_NEW(self, query, id, msg):
  1551. return "\\Recent" in msg.getFlags() and "\\Seen" not in msg.getFlags()
  1552. def search_NOT(self, query, id, msg, lastIDs):
  1553. """
  1554. Returns C{True} if the message does not match the query.
  1555. @type query: A L{list} of L{str}
  1556. @param query: A list representing the parsed form of the search query.
  1557. @type id: L{int}
  1558. @param id: The sequence number of the message being checked.
  1559. @type msg: Provider of L{imap4.IMessage}
  1560. @param msg: The message being checked.
  1561. @type lastIDs: L{tuple}
  1562. @param lastIDs: A tuple of (last sequence id, last message id).
  1563. The I{last sequence id} is an L{int} containing the highest sequence
  1564. number of a message in the mailbox. The I{last message id} is an
  1565. L{int} containing the highest UID of a message in the mailbox.
  1566. """
  1567. (lastSequenceId, lastMessageId) = lastIDs
  1568. return not self._singleSearchStep(query, id, msg, lastSequenceId, lastMessageId)
  1569. def search_OLD(self, query, id, msg):
  1570. return "\\Recent" not in msg.getFlags()
  1571. def search_ON(self, query, id, msg):
  1572. date = parseTime(query.pop(0))
  1573. return email.utils.parsedate(msg.getInternalDate()) == date
  1574. def search_OR(self, query, id, msg, lastIDs):
  1575. """
  1576. Returns C{True} if the message matches any of the first two query
  1577. items.
  1578. @type query: A L{list} of L{str}
  1579. @param query: A list representing the parsed form of the search query.
  1580. @type id: L{int}
  1581. @param id: The sequence number of the message being checked.
  1582. @type msg: Provider of L{imap4.IMessage}
  1583. @param msg: The message being checked.
  1584. @type lastIDs: L{tuple}
  1585. @param lastIDs: A tuple of (last sequence id, last message id).
  1586. The I{last sequence id} is an L{int} containing the highest sequence
  1587. number of a message in the mailbox. The I{last message id} is an
  1588. L{int} containing the highest UID of a message in the mailbox.
  1589. """
  1590. (lastSequenceId, lastMessageId) = lastIDs
  1591. a = self._singleSearchStep(query, id, msg, lastSequenceId, lastMessageId)
  1592. b = self._singleSearchStep(query, id, msg, lastSequenceId, lastMessageId)
  1593. return a or b
  1594. def search_RECENT(self, query, id, msg):
  1595. return "\\Recent" in msg.getFlags()
  1596. def search_SEEN(self, query, id, msg):
  1597. return "\\Seen" in msg.getFlags()
  1598. def search_SENTBEFORE(self, query, id, msg):
  1599. """
  1600. Returns C{True} if the message date is earlier than the query date.
  1601. @type query: A L{list} of L{str}
  1602. @param query: A list whose first element starts with a stringified date
  1603. that is a fragment of an L{imap4.Query()}. The date must be in the
  1604. format 'DD-Mon-YYYY', for example '03-March-2003' or '03-Mar-2003'.
  1605. @type id: L{int}
  1606. @param id: The sequence number of the message being checked.
  1607. @type msg: Provider of L{imap4.IMessage}
  1608. """
  1609. date = msg.getHeaders(False, "date").get("date", "")
  1610. date = email.utils.parsedate(date)
  1611. return date < parseTime(query.pop(0))
  1612. def search_SENTON(self, query, id, msg):
  1613. """
  1614. Returns C{True} if the message date is the same as the query date.
  1615. @type query: A L{list} of L{str}
  1616. @param query: A list whose first element starts with a stringified date
  1617. that is a fragment of an L{imap4.Query()}. The date must be in the
  1618. format 'DD-Mon-YYYY', for example '03-March-2003' or '03-Mar-2003'.
  1619. @type msg: Provider of L{imap4.IMessage}
  1620. """
  1621. date = msg.getHeaders(False, "date").get("date", "")
  1622. date = email.utils.parsedate(date)
  1623. return date[:3] == parseTime(query.pop(0))[:3]
  1624. def search_SENTSINCE(self, query, id, msg):
  1625. """
  1626. Returns C{True} if the message date is later than the query date.
  1627. @type query: A L{list} of L{str}
  1628. @param query: A list whose first element starts with a stringified date
  1629. that is a fragment of an L{imap4.Query()}. The date must be in the
  1630. format 'DD-Mon-YYYY', for example '03-March-2003' or '03-Mar-2003'.
  1631. @type msg: Provider of L{imap4.IMessage}
  1632. """
  1633. date = msg.getHeaders(False, "date").get("date", "")
  1634. date = email.utils.parsedate(date)
  1635. return date > parseTime(query.pop(0))
  1636. def search_SINCE(self, query, id, msg):
  1637. date = parseTime(query.pop(0))
  1638. return email.utils.parsedate(msg.getInternalDate()) > date
  1639. def search_SMALLER(self, query, id, msg):
  1640. return int(query.pop(0)) > msg.getSize()
  1641. def search_SUBJECT(self, query, id, msg):
  1642. subj = msg.getHeaders(False, "subject").get("subject", "")
  1643. return subj.lower().find(query.pop(0).lower()) != -1
  1644. def search_TEXT(self, query, id, msg):
  1645. # XXX - This must search headers too
  1646. body = query.pop(0).lower()
  1647. return text.strFile(body, msg.getBodyFile(), False)
  1648. def search_TO(self, query, id, msg):
  1649. to = msg.getHeaders(False, "to").get("to", "")
  1650. return to.lower().find(query.pop(0).lower()) != -1
  1651. def search_UID(self, query, id, msg, lastIDs):
  1652. """
  1653. Returns C{True} if the message UID is in the range defined by the
  1654. search query.
  1655. @type query: A L{list} of L{bytes}
  1656. @param query: A list representing the parsed form of the search
  1657. query. Its first element should be a L{str} that can be interpreted
  1658. as a sequence range, for example '2:4,5:*'.
  1659. @type id: L{int}
  1660. @param id: The sequence number of the message being checked.
  1661. @type msg: Provider of L{imap4.IMessage}
  1662. @param msg: The message being checked.
  1663. @type lastIDs: L{tuple}
  1664. @param lastIDs: A tuple of (last sequence id, last message id).
  1665. The I{last sequence id} is an L{int} containing the highest sequence
  1666. number of a message in the mailbox. The I{last message id} is an
  1667. L{int} containing the highest UID of a message in the mailbox.
  1668. """
  1669. (lastSequenceId, lastMessageId) = lastIDs
  1670. c = query.pop(0)
  1671. m = parseIdList(c, lastMessageId)
  1672. return msg.getUID() in m
  1673. def search_UNANSWERED(self, query, id, msg):
  1674. return "\\Answered" not in msg.getFlags()
  1675. def search_UNDELETED(self, query, id, msg):
  1676. return "\\Deleted" not in msg.getFlags()
  1677. def search_UNDRAFT(self, query, id, msg):
  1678. return "\\Draft" not in msg.getFlags()
  1679. def search_UNFLAGGED(self, query, id, msg):
  1680. return "\\Flagged" not in msg.getFlags()
  1681. def search_UNKEYWORD(self, query, id, msg):
  1682. query.pop(0)
  1683. return False
  1684. def search_UNSEEN(self, query, id, msg):
  1685. return "\\Seen" not in msg.getFlags()
  1686. def __ebSearch(self, failure, tag):
  1687. self.sendBadResponse(
  1688. tag, b"SEARCH failed: " + networkString(str(failure.value))
  1689. )
  1690. log.err(failure)
  1691. def do_FETCH(self, tag, messages, query, uid=0):
  1692. if query:
  1693. self._oldTimeout = self.setTimeout(None)
  1694. maybeDeferred(self.mbox.fetch, messages, uid=uid).addCallback(
  1695. iter
  1696. ).addCallback(self.__cbFetch, tag, query, uid).addErrback(
  1697. self.__ebFetch, tag
  1698. )
  1699. else:
  1700. self.sendPositiveResponse(tag, b"FETCH complete")
  1701. select_FETCH = (do_FETCH, arg_seqset, arg_fetchatt)
  1702. def __cbFetch(self, results, tag, query, uid):
  1703. if self.blocked is None:
  1704. self.blocked = []
  1705. try:
  1706. id, msg = next(results)
  1707. except StopIteration:
  1708. # The idle timeout was suspended while we delivered results,
  1709. # restore it now.
  1710. self.setTimeout(self._oldTimeout)
  1711. del self._oldTimeout
  1712. # All results have been processed, deliver completion notification.
  1713. # It's important to run this *after* resetting the timeout to "rig
  1714. # a race" in some test code. writing to the transport will
  1715. # synchronously call test code, which synchronously loses the
  1716. # connection, calling our connectionLost method, which cancels the
  1717. # timeout. We want to make sure that timeout is cancelled *after*
  1718. # we reset it above, so that the final state is no timed
  1719. # calls. This avoids reactor uncleanliness errors in the test
  1720. # suite.
  1721. # XXX: Perhaps loopback should be fixed to not call the user code
  1722. # synchronously in transport.write?
  1723. self.sendPositiveResponse(tag, b"FETCH completed")
  1724. # Instance state is now consistent again (ie, it is as though
  1725. # the fetch command never ran), so allow any pending blocked
  1726. # commands to execute.
  1727. self._unblock()
  1728. else:
  1729. self.spewMessage(id, msg, query, uid).addCallback(
  1730. lambda _: self.__cbFetch(results, tag, query, uid)
  1731. ).addErrback(self.__ebSpewMessage)
  1732. def __ebSpewMessage(self, failure):
  1733. # This indicates a programming error.
  1734. # There's no reliable way to indicate anything to the client, since we
  1735. # may have already written an arbitrary amount of data in response to
  1736. # the command.
  1737. log.err(failure)
  1738. self.transport.loseConnection()
  1739. def spew_envelope(self, id, msg, _w=None, _f=None):
  1740. if _w is None:
  1741. _w = self.transport.write
  1742. _w(b"ENVELOPE " + collapseNestedLists([getEnvelope(msg)]))
  1743. def spew_flags(self, id, msg, _w=None, _f=None):
  1744. if _w is None:
  1745. _w = self.transport.writen
  1746. encodedFlags = [networkString(flag) for flag in msg.getFlags()]
  1747. _w(b"FLAGS " + b"(" + b" ".join(encodedFlags) + b")")
  1748. def spew_internaldate(self, id, msg, _w=None, _f=None):
  1749. if _w is None:
  1750. _w = self.transport.write
  1751. idate = msg.getInternalDate()
  1752. ttup = email.utils.parsedate_tz(nativeString(idate))
  1753. if ttup is None:
  1754. log.msg("%d:%r: unpareseable internaldate: %r" % (id, msg, idate))
  1755. raise IMAP4Exception("Internal failure generating INTERNALDATE")
  1756. # need to specify the month manually, as strftime depends on locale
  1757. strdate = time.strftime("%d-%%s-%Y %H:%M:%S ", ttup[:9])
  1758. odate = networkString(strdate % (_MONTH_NAMES[ttup[1]],))
  1759. if ttup[9] is None:
  1760. odate = odate + b"+0000"
  1761. else:
  1762. if ttup[9] >= 0:
  1763. sign = b"+"
  1764. else:
  1765. sign = b"-"
  1766. odate = (
  1767. odate
  1768. + sign
  1769. + b"%04d"
  1770. % ((abs(ttup[9]) // 3600) * 100 + (abs(ttup[9]) % 3600) // 60,)
  1771. )
  1772. _w(b"INTERNALDATE " + _quote(odate))
  1773. def spew_rfc822header(self, id, msg, _w=None, _f=None):
  1774. if _w is None:
  1775. _w = self.transport.write
  1776. hdrs = _formatHeaders(msg.getHeaders(True))
  1777. _w(b"RFC822.HEADER " + _literal(hdrs))
  1778. def spew_rfc822text(self, id, msg, _w=None, _f=None):
  1779. if _w is None:
  1780. _w = self.transport.write
  1781. _w(b"RFC822.TEXT ")
  1782. _f()
  1783. return FileProducer(msg.getBodyFile()).beginProducing(self.transport)
  1784. def spew_rfc822size(self, id, msg, _w=None, _f=None):
  1785. if _w is None:
  1786. _w = self.transport.write
  1787. _w(b"RFC822.SIZE %d" % (msg.getSize(),))
  1788. def spew_rfc822(self, id, msg, _w=None, _f=None):
  1789. if _w is None:
  1790. _w = self.transport.write
  1791. _w(b"RFC822 ")
  1792. _f()
  1793. mf = IMessageFile(msg, None)
  1794. if mf is not None:
  1795. return FileProducer(mf.open()).beginProducing(self.transport)
  1796. return MessageProducer(msg, None, self._scheduler).beginProducing(
  1797. self.transport
  1798. )
  1799. def spew_uid(self, id, msg, _w=None, _f=None):
  1800. if _w is None:
  1801. _w = self.transport.write
  1802. _w(b"UID %d" % (msg.getUID(),))
  1803. def spew_bodystructure(self, id, msg, _w=None, _f=None):
  1804. _w(b"BODYSTRUCTURE " + collapseNestedLists([getBodyStructure(msg, True)]))
  1805. def spew_body(self, part, id, msg, _w=None, _f=None):
  1806. if _w is None:
  1807. _w = self.transport.write
  1808. for p in part.part:
  1809. if msg.isMultipart():
  1810. msg = msg.getSubPart(p)
  1811. elif p > 0:
  1812. # Non-multipart messages have an implicit first part but no
  1813. # other parts - reject any request for any other part.
  1814. raise TypeError("Requested subpart of non-multipart message")
  1815. if part.header:
  1816. hdrs = msg.getHeaders(part.header.negate, *part.header.fields)
  1817. hdrs = _formatHeaders(hdrs)
  1818. _w(part.__bytes__() + b" " + _literal(hdrs))
  1819. elif part.text:
  1820. _w(part.__bytes__() + b" ")
  1821. _f()
  1822. return FileProducer(msg.getBodyFile()).beginProducing(self.transport)
  1823. elif part.mime:
  1824. hdrs = _formatHeaders(msg.getHeaders(True))
  1825. _w(part.__bytes__() + b" " + _literal(hdrs))
  1826. elif part.empty:
  1827. _w(part.__bytes__() + b" ")
  1828. _f()
  1829. if part.part:
  1830. return FileProducer(msg.getBodyFile()).beginProducing(self.transport)
  1831. else:
  1832. mf = IMessageFile(msg, None)
  1833. if mf is not None:
  1834. return FileProducer(mf.open()).beginProducing(self.transport)
  1835. return MessageProducer(msg, None, self._scheduler).beginProducing(
  1836. self.transport
  1837. )
  1838. else:
  1839. _w(b"BODY " + collapseNestedLists([getBodyStructure(msg)]))
  1840. def spewMessage(self, id, msg, query, uid):
  1841. wbuf = WriteBuffer(self.transport)
  1842. write = wbuf.write
  1843. flush = wbuf.flush
  1844. def start():
  1845. write(b"* %d FETCH (" % (id,))
  1846. def finish():
  1847. write(b")\r\n")
  1848. def space():
  1849. write(b" ")
  1850. def spew():
  1851. seenUID = False
  1852. start()
  1853. for part in query:
  1854. if part.type == "uid":
  1855. seenUID = True
  1856. if part.type == "body":
  1857. yield self.spew_body(part, id, msg, write, flush)
  1858. else:
  1859. f = getattr(self, "spew_" + part.type)
  1860. yield f(id, msg, write, flush)
  1861. if part is not query[-1]:
  1862. space()
  1863. if uid and not seenUID:
  1864. space()
  1865. yield self.spew_uid(id, msg, write, flush)
  1866. finish()
  1867. flush()
  1868. return self._scheduler(spew())
  1869. def __ebFetch(self, failure, tag):
  1870. self.setTimeout(self._oldTimeout)
  1871. del self._oldTimeout
  1872. log.err(failure)
  1873. self.sendBadResponse(tag, b"FETCH failed: " + networkString(str(failure.value)))
  1874. def do_STORE(self, tag, messages, mode, flags, uid=0):
  1875. mode = mode.upper()
  1876. silent = mode.endswith(b"SILENT")
  1877. if mode.startswith(b"+"):
  1878. mode = 1
  1879. elif mode.startswith(b"-"):
  1880. mode = -1
  1881. else:
  1882. mode = 0
  1883. flags = [nativeString(flag) for flag in flags]
  1884. maybeDeferred(self.mbox.store, messages, flags, mode, uid=uid).addCallbacks(
  1885. self.__cbStore,
  1886. self.__ebStore,
  1887. (tag, self.mbox, uid, silent),
  1888. None,
  1889. (tag,),
  1890. None,
  1891. )
  1892. select_STORE = (do_STORE, arg_seqset, arg_atom, arg_flaglist)
  1893. def __cbStore(self, result, tag, mbox, uid, silent):
  1894. if result and not silent:
  1895. for (k, v) in result.items():
  1896. if uid:
  1897. uidstr = b" UID %d" % (mbox.getUID(k),)
  1898. else:
  1899. uidstr = b""
  1900. flags = [networkString(flag) for flag in v]
  1901. self.sendUntaggedResponse(
  1902. b"%d FETCH (FLAGS (%b)%b)" % (k, b" ".join(flags), uidstr)
  1903. )
  1904. self.sendPositiveResponse(tag, b"STORE completed")
  1905. def __ebStore(self, failure, tag):
  1906. self.sendBadResponse(tag, b"Server error: " + networkString(str(failure.value)))
  1907. def do_COPY(self, tag, messages, mailbox, uid=0):
  1908. mailbox = _parseMbox(mailbox)
  1909. maybeDeferred(self.account.select, mailbox).addCallback(
  1910. self._cbCopySelectedMailbox, tag, messages, mailbox, uid
  1911. ).addErrback(self._ebCopySelectedMailbox, tag)
  1912. select_COPY = (do_COPY, arg_seqset, arg_finalastring)
  1913. def _cbCopySelectedMailbox(self, mbox, tag, messages, mailbox, uid):
  1914. if not mbox:
  1915. self.sendNegativeResponse(tag, "No such mailbox: " + mailbox)
  1916. else:
  1917. maybeDeferred(self.mbox.fetch, messages, uid).addCallback(
  1918. self.__cbCopy, tag, mbox
  1919. ).addCallback(self.__cbCopied, tag, mbox).addErrback(self.__ebCopy, tag)
  1920. def _ebCopySelectedMailbox(self, failure, tag):
  1921. self.sendBadResponse(tag, b"Server error: " + networkString(str(failure.value)))
  1922. def __cbCopy(self, messages, tag, mbox):
  1923. # XXX - This should handle failures with a rollback or something
  1924. addedDeferreds = []
  1925. fastCopyMbox = IMessageCopier(mbox, None)
  1926. for (id, msg) in messages:
  1927. if fastCopyMbox is not None:
  1928. d = maybeDeferred(fastCopyMbox.copy, msg)
  1929. addedDeferreds.append(d)
  1930. continue
  1931. # XXX - The following should be an implementation of IMessageCopier.copy
  1932. # on an IMailbox->IMessageCopier adapter.
  1933. flags = msg.getFlags()
  1934. date = msg.getInternalDate()
  1935. body = IMessageFile(msg, None)
  1936. if body is not None:
  1937. bodyFile = body.open()
  1938. d = maybeDeferred(mbox.addMessage, bodyFile, flags, date)
  1939. else:
  1940. def rewind(f):
  1941. f.seek(0)
  1942. return f
  1943. buffer = tempfile.TemporaryFile()
  1944. d = (
  1945. MessageProducer(msg, buffer, self._scheduler)
  1946. .beginProducing(None)
  1947. .addCallback(
  1948. lambda _, b=buffer, f=flags, d=date: mbox.addMessage(
  1949. rewind(b), f, d
  1950. )
  1951. )
  1952. )
  1953. addedDeferreds.append(d)
  1954. return defer.DeferredList(addedDeferreds)
  1955. def __cbCopied(self, deferredIds, tag, mbox):
  1956. ids = []
  1957. failures = []
  1958. for (status, result) in deferredIds:
  1959. if status:
  1960. ids.append(result)
  1961. else:
  1962. failures.append(result.value)
  1963. if failures:
  1964. self.sendNegativeResponse(tag, "[ALERT] Some messages were not copied")
  1965. else:
  1966. self.sendPositiveResponse(tag, b"COPY completed")
  1967. def __ebCopy(self, failure, tag):
  1968. self.sendBadResponse(tag, b"COPY failed:" + networkString(str(failure.value)))
  1969. log.err(failure)
  1970. def do_UID(self, tag, command, line):
  1971. command = command.upper()
  1972. if command not in (b"COPY", b"FETCH", b"STORE", b"SEARCH"):
  1973. raise IllegalClientResponse(command)
  1974. self.dispatchCommand(tag, command, line, uid=1)
  1975. select_UID = (do_UID, arg_atom, arg_line)
  1976. #
  1977. # IMailboxListener implementation
  1978. #
  1979. def modeChanged(self, writeable):
  1980. if writeable:
  1981. self.sendUntaggedResponse(message=b"[READ-WRITE]", isAsync=True)
  1982. else:
  1983. self.sendUntaggedResponse(message=b"[READ-ONLY]", isAsync=True)
  1984. def flagsChanged(self, newFlags):
  1985. for (mId, flags) in newFlags.items():
  1986. encodedFlags = [networkString(flag) for flag in flags]
  1987. msg = b"%d FETCH (FLAGS (%b))" % (mId, b" ".join(encodedFlags))
  1988. self.sendUntaggedResponse(msg, isAsync=True)
  1989. def newMessages(self, exists, recent):
  1990. if exists is not None:
  1991. self.sendUntaggedResponse(b"%d EXISTS" % (exists,), isAsync=True)
  1992. if recent is not None:
  1993. self.sendUntaggedResponse(b"%d RECENT" % (recent,), isAsync=True)
  1994. TIMEOUT_ERROR = error.TimeoutError()
  1995. @implementer(IMailboxListener)
  1996. class IMAP4Client(basic.LineReceiver, policies.TimeoutMixin):
  1997. """IMAP4 client protocol implementation
  1998. @ivar state: A string representing the state the connection is currently
  1999. in.
  2000. """
  2001. tags = None
  2002. waiting = None
  2003. queued = None
  2004. tagID = 1
  2005. state = None
  2006. startedTLS = False
  2007. # Number of seconds to wait before timing out a connection.
  2008. # If the number is <= 0 no timeout checking will be performed.
  2009. timeout = 0
  2010. # Capabilities are not allowed to change during the session
  2011. # So cache the first response and use that for all later
  2012. # lookups
  2013. _capCache = None
  2014. _memoryFileLimit = 1024 * 1024 * 10
  2015. # Authentication is pluggable. This maps names to IClientAuthentication
  2016. # objects.
  2017. authenticators = None
  2018. STATUS_CODES = ("OK", "NO", "BAD", "PREAUTH", "BYE")
  2019. STATUS_TRANSFORMATIONS = {"MESSAGES": int, "RECENT": int, "UNSEEN": int}
  2020. context = None
  2021. def __init__(self, contextFactory=None):
  2022. self.tags = {}
  2023. self.queued = []
  2024. self.authenticators = {}
  2025. self.context = contextFactory
  2026. self._tag = None
  2027. self._parts = None
  2028. self._lastCmd = None
  2029. def registerAuthenticator(self, auth):
  2030. """
  2031. Register a new form of authentication
  2032. When invoking the authenticate() method of IMAP4Client, the first
  2033. matching authentication scheme found will be used. The ordering is
  2034. that in which the server lists support authentication schemes.
  2035. @type auth: Implementor of C{IClientAuthentication}
  2036. @param auth: The object to use to perform the client
  2037. side of this authentication scheme.
  2038. """
  2039. self.authenticators[auth.getName().upper()] = auth
  2040. def rawDataReceived(self, data):
  2041. if self.timeout > 0:
  2042. self.resetTimeout()
  2043. self._pendingSize -= len(data)
  2044. if self._pendingSize > 0:
  2045. self._pendingBuffer.write(data)
  2046. else:
  2047. passon = b""
  2048. if self._pendingSize < 0:
  2049. data, passon = data[: self._pendingSize], data[self._pendingSize :]
  2050. self._pendingBuffer.write(data)
  2051. rest = self._pendingBuffer
  2052. self._pendingBuffer = None
  2053. self._pendingSize = None
  2054. rest.seek(0, 0)
  2055. self._parts.append(rest.read())
  2056. self.setLineMode(passon.lstrip(b"\r\n"))
  2057. # def sendLine(self, line):
  2058. # print 'S:', repr(line)
  2059. # return basic.LineReceiver.sendLine(self, line)
  2060. def _setupForLiteral(self, rest, octets):
  2061. self._pendingBuffer = self.messageFile(octets)
  2062. self._pendingSize = octets
  2063. if self._parts is None:
  2064. self._parts = [rest, b"\r\n"]
  2065. else:
  2066. self._parts.extend([rest, b"\r\n"])
  2067. self.setRawMode()
  2068. def connectionMade(self):
  2069. if self.timeout > 0:
  2070. self.setTimeout(self.timeout)
  2071. def connectionLost(self, reason):
  2072. """
  2073. We are no longer connected
  2074. """
  2075. if self.timeout > 0:
  2076. self.setTimeout(None)
  2077. if self.queued is not None:
  2078. queued = self.queued
  2079. self.queued = None
  2080. for cmd in queued:
  2081. cmd.defer.errback(reason)
  2082. if self.tags is not None:
  2083. tags = self.tags
  2084. self.tags = None
  2085. for cmd in tags.values():
  2086. if cmd is not None and cmd.defer is not None:
  2087. cmd.defer.errback(reason)
  2088. def lineReceived(self, line):
  2089. """
  2090. Attempt to parse a single line from the server.
  2091. @type line: L{bytes}
  2092. @param line: The line from the server, without the line delimiter.
  2093. @raise IllegalServerResponse: If the line or some part of the line
  2094. does not represent an allowed message from the server at this time.
  2095. """
  2096. # print('C: ' + repr(line))
  2097. if self.timeout > 0:
  2098. self.resetTimeout()
  2099. lastPart = line.rfind(b"{")
  2100. if lastPart != -1:
  2101. lastPart = line[lastPart + 1 :]
  2102. if lastPart.endswith(b"}"):
  2103. # It's a literal a-comin' in
  2104. try:
  2105. octets = int(lastPart[:-1])
  2106. except ValueError:
  2107. raise IllegalServerResponse(line)
  2108. if self._parts is None:
  2109. self._tag, parts = line.split(None, 1)
  2110. else:
  2111. parts = line
  2112. self._setupForLiteral(parts, octets)
  2113. return
  2114. if self._parts is None:
  2115. # It isn't a literal at all
  2116. self._regularDispatch(line)
  2117. else:
  2118. # If an expression is in progress, no tag is required here
  2119. # Since we didn't find a literal indicator, this expression
  2120. # is done.
  2121. self._parts.append(line)
  2122. tag, rest = self._tag, b"".join(self._parts)
  2123. self._tag = self._parts = None
  2124. self.dispatchCommand(tag, rest)
  2125. def timeoutConnection(self):
  2126. if self._lastCmd and self._lastCmd.defer is not None:
  2127. d, self._lastCmd.defer = self._lastCmd.defer, None
  2128. d.errback(TIMEOUT_ERROR)
  2129. if self.queued:
  2130. for cmd in self.queued:
  2131. if cmd.defer is not None:
  2132. d, cmd.defer = cmd.defer, d
  2133. d.errback(TIMEOUT_ERROR)
  2134. self.transport.loseConnection()
  2135. def _regularDispatch(self, line):
  2136. parts = line.split(None, 1)
  2137. if len(parts) != 2:
  2138. parts.append(b"")
  2139. tag, rest = parts
  2140. self.dispatchCommand(tag, rest)
  2141. def messageFile(self, octets):
  2142. """
  2143. Create a file to which an incoming message may be written.
  2144. @type octets: L{int}
  2145. @param octets: The number of octets which will be written to the file
  2146. @rtype: Any object which implements C{write(string)} and
  2147. C{seek(int, int)}
  2148. @return: A file-like object
  2149. """
  2150. if octets > self._memoryFileLimit:
  2151. return tempfile.TemporaryFile()
  2152. else:
  2153. return BytesIO()
  2154. def makeTag(self):
  2155. tag = ("%0.4X" % self.tagID).encode("ascii")
  2156. self.tagID += 1
  2157. return tag
  2158. def dispatchCommand(self, tag, rest):
  2159. if self.state is None:
  2160. f = self.response_UNAUTH
  2161. else:
  2162. f = getattr(self, "response_" + self.state.upper(), None)
  2163. if f:
  2164. try:
  2165. f(tag, rest)
  2166. except BaseException:
  2167. log.err()
  2168. self.transport.loseConnection()
  2169. else:
  2170. log.err(f"Cannot dispatch: {self.state}, {tag!r}, {rest!r}")
  2171. self.transport.loseConnection()
  2172. def response_UNAUTH(self, tag, rest):
  2173. if self.state is None:
  2174. # Server greeting, this is
  2175. status, rest = rest.split(None, 1)
  2176. if status.upper() == b"OK":
  2177. self.state = "unauth"
  2178. elif status.upper() == b"PREAUTH":
  2179. self.state = "auth"
  2180. else:
  2181. # XXX - This is rude.
  2182. self.transport.loseConnection()
  2183. raise IllegalServerResponse(tag + b" " + rest)
  2184. b, e = rest.find(b"["), rest.find(b"]")
  2185. if b != -1 and e != -1:
  2186. self.serverGreeting(
  2187. self.__cbCapabilities(([parseNestedParens(rest[b + 1 : e])], None))
  2188. )
  2189. else:
  2190. self.serverGreeting(None)
  2191. else:
  2192. self._defaultHandler(tag, rest)
  2193. def response_AUTH(self, tag, rest):
  2194. self._defaultHandler(tag, rest)
  2195. def _defaultHandler(self, tag, rest):
  2196. if tag == b"*" or tag == b"+":
  2197. if not self.waiting:
  2198. self._extraInfo([parseNestedParens(rest)])
  2199. else:
  2200. cmd = self.tags[self.waiting]
  2201. if tag == b"+":
  2202. cmd.continuation(rest)
  2203. else:
  2204. cmd.lines.append(rest)
  2205. else:
  2206. try:
  2207. cmd = self.tags[tag]
  2208. except KeyError:
  2209. # XXX - This is rude.
  2210. self.transport.loseConnection()
  2211. raise IllegalServerResponse(tag + b" " + rest)
  2212. else:
  2213. status, line = rest.split(None, 1)
  2214. if status == b"OK":
  2215. # Give them this last line, too
  2216. cmd.finish(rest, self._extraInfo)
  2217. else:
  2218. cmd.defer.errback(IMAP4Exception(line))
  2219. del self.tags[tag]
  2220. self.waiting = None
  2221. self._flushQueue()
  2222. def _flushQueue(self):
  2223. if self.queued:
  2224. cmd = self.queued.pop(0)
  2225. t = self.makeTag()
  2226. self.tags[t] = cmd
  2227. self.sendLine(cmd.format(t))
  2228. self.waiting = t
  2229. def _extraInfo(self, lines):
  2230. # XXX - This is terrible.
  2231. # XXX - Also, this should collapse temporally proximate calls into single
  2232. # invocations of IMailboxListener methods, where possible.
  2233. flags = {}
  2234. recent = exists = None
  2235. for response in lines:
  2236. elements = len(response)
  2237. if elements == 1 and response[0] == [b"READ-ONLY"]:
  2238. self.modeChanged(False)
  2239. elif elements == 1 and response[0] == [b"READ-WRITE"]:
  2240. self.modeChanged(True)
  2241. elif elements == 2 and response[1] == b"EXISTS":
  2242. exists = int(response[0])
  2243. elif elements == 2 and response[1] == b"RECENT":
  2244. recent = int(response[0])
  2245. elif elements == 3 and response[1] == b"FETCH":
  2246. mId = int(response[0])
  2247. values, _ = self._parseFetchPairs(response[2])
  2248. flags.setdefault(mId, []).extend(values.get("FLAGS", ()))
  2249. else:
  2250. log.msg(f"Unhandled unsolicited response: {response}")
  2251. if flags:
  2252. self.flagsChanged(flags)
  2253. if recent is not None or exists is not None:
  2254. self.newMessages(exists, recent)
  2255. def sendCommand(self, cmd):
  2256. cmd.defer = defer.Deferred()
  2257. if self.waiting:
  2258. self.queued.append(cmd)
  2259. return cmd.defer
  2260. t = self.makeTag()
  2261. self.tags[t] = cmd
  2262. self.sendLine(cmd.format(t))
  2263. self.waiting = t
  2264. self._lastCmd = cmd
  2265. return cmd.defer
  2266. def getCapabilities(self, useCache=1):
  2267. """
  2268. Request the capabilities available on this server.
  2269. This command is allowed in any state of connection.
  2270. @type useCache: C{bool}
  2271. @param useCache: Specify whether to use the capability-cache or to
  2272. re-retrieve the capabilities from the server. Server capabilities
  2273. should never change, so for normal use, this flag should never be
  2274. false.
  2275. @rtype: L{Deferred}
  2276. @return: A deferred whose callback will be invoked with a
  2277. dictionary mapping capability types to lists of supported
  2278. mechanisms, or to None if a support list is not applicable.
  2279. """
  2280. if useCache and self._capCache is not None:
  2281. return defer.succeed(self._capCache)
  2282. cmd = b"CAPABILITY"
  2283. resp = (b"CAPABILITY",)
  2284. d = self.sendCommand(Command(cmd, wantResponse=resp))
  2285. d.addCallback(self.__cbCapabilities)
  2286. return d
  2287. def __cbCapabilities(self, result):
  2288. (lines, tagline) = result
  2289. caps = {}
  2290. for rest in lines:
  2291. for cap in rest[1:]:
  2292. parts = cap.split(b"=", 1)
  2293. if len(parts) == 1:
  2294. category, value = parts[0], None
  2295. else:
  2296. category, value = parts
  2297. caps.setdefault(category, []).append(value)
  2298. # Preserve a non-ideal API for backwards compatibility. It would
  2299. # probably be entirely sensible to have an object with a wider API than
  2300. # dict here so this could be presented less insanely.
  2301. for category in caps:
  2302. if caps[category] == [None]:
  2303. caps[category] = None
  2304. self._capCache = caps
  2305. return caps
  2306. def logout(self):
  2307. """
  2308. Inform the server that we are done with the connection.
  2309. This command is allowed in any state of connection.
  2310. @rtype: L{Deferred}
  2311. @return: A deferred whose callback will be invoked with None
  2312. when the proper server acknowledgement has been received.
  2313. """
  2314. d = self.sendCommand(Command(b"LOGOUT", wantResponse=(b"BYE",)))
  2315. d.addCallback(self.__cbLogout)
  2316. return d
  2317. def __cbLogout(self, result):
  2318. (lines, tagline) = result
  2319. self.transport.loseConnection()
  2320. # We don't particularly care what the server said
  2321. return None
  2322. def noop(self):
  2323. """
  2324. Perform no operation.
  2325. This command is allowed in any state of connection.
  2326. @rtype: L{Deferred}
  2327. @return: A deferred whose callback will be invoked with a list
  2328. of untagged status updates the server responds with.
  2329. """
  2330. d = self.sendCommand(Command(b"NOOP"))
  2331. d.addCallback(self.__cbNoop)
  2332. return d
  2333. def __cbNoop(self, result):
  2334. # Conceivable, this is elidable.
  2335. # It is, afterall, a no-op.
  2336. (lines, tagline) = result
  2337. return lines
  2338. def startTLS(self, contextFactory=None):
  2339. """
  2340. Initiates a 'STARTTLS' request and negotiates the TLS / SSL
  2341. Handshake.
  2342. @param contextFactory: The TLS / SSL Context Factory to
  2343. leverage. If the contextFactory is None the IMAP4Client will
  2344. either use the current TLS / SSL Context Factory or attempt to
  2345. create a new one.
  2346. @type contextFactory: C{ssl.ClientContextFactory}
  2347. @return: A Deferred which fires when the transport has been
  2348. secured according to the given contextFactory, or which fails
  2349. if the transport cannot be secured.
  2350. """
  2351. assert (
  2352. not self.startedTLS
  2353. ), "Client and Server are currently communicating via TLS"
  2354. if contextFactory is None:
  2355. contextFactory = self._getContextFactory()
  2356. if contextFactory is None:
  2357. return defer.fail(
  2358. IMAP4Exception(
  2359. "IMAP4Client requires a TLS context to "
  2360. "initiate the STARTTLS handshake"
  2361. )
  2362. )
  2363. if b"STARTTLS" not in self._capCache:
  2364. return defer.fail(
  2365. IMAP4Exception(
  2366. "Server does not support secure communication " "via TLS / SSL"
  2367. )
  2368. )
  2369. tls = interfaces.ITLSTransport(self.transport, None)
  2370. if tls is None:
  2371. return defer.fail(
  2372. IMAP4Exception(
  2373. "IMAP4Client transport does not implement "
  2374. "interfaces.ITLSTransport"
  2375. )
  2376. )
  2377. d = self.sendCommand(Command(b"STARTTLS"))
  2378. d.addCallback(self._startedTLS, contextFactory)
  2379. d.addCallback(lambda _: self.getCapabilities())
  2380. return d
  2381. def authenticate(self, secret):
  2382. """
  2383. Attempt to enter the authenticated state with the server
  2384. This command is allowed in the Non-Authenticated state.
  2385. @rtype: L{Deferred}
  2386. @return: A deferred whose callback is invoked if the authentication
  2387. succeeds and whose errback will be invoked otherwise.
  2388. """
  2389. if self._capCache is None:
  2390. d = self.getCapabilities()
  2391. else:
  2392. d = defer.succeed(self._capCache)
  2393. d.addCallback(self.__cbAuthenticate, secret)
  2394. return d
  2395. def __cbAuthenticate(self, caps, secret):
  2396. auths = caps.get(b"AUTH", ())
  2397. for scheme in auths:
  2398. if scheme.upper() in self.authenticators:
  2399. cmd = Command(
  2400. b"AUTHENTICATE", scheme, (), self.__cbContinueAuth, scheme, secret
  2401. )
  2402. return self.sendCommand(cmd)
  2403. if self.startedTLS:
  2404. return defer.fail(
  2405. NoSupportedAuthentication(auths, self.authenticators.keys())
  2406. )
  2407. else:
  2408. def ebStartTLS(err):
  2409. err.trap(IMAP4Exception)
  2410. # We couldn't negotiate TLS for some reason
  2411. return defer.fail(
  2412. NoSupportedAuthentication(auths, self.authenticators.keys())
  2413. )
  2414. d = self.startTLS()
  2415. d.addErrback(ebStartTLS)
  2416. d.addCallback(lambda _: self.getCapabilities())
  2417. d.addCallback(self.__cbAuthTLS, secret)
  2418. return d
  2419. def __cbContinueAuth(self, rest, scheme, secret):
  2420. try:
  2421. chal = decodebytes(rest + b"\n")
  2422. except binascii.Error:
  2423. self.sendLine(b"*")
  2424. raise IllegalServerResponse(rest)
  2425. else:
  2426. auth = self.authenticators[scheme]
  2427. chal = auth.challengeResponse(secret, chal)
  2428. self.sendLine(encodebytes(chal).strip())
  2429. def __cbAuthTLS(self, caps, secret):
  2430. auths = caps.get(b"AUTH", ())
  2431. for scheme in auths:
  2432. if scheme.upper() in self.authenticators:
  2433. cmd = Command(
  2434. b"AUTHENTICATE", scheme, (), self.__cbContinueAuth, scheme, secret
  2435. )
  2436. return self.sendCommand(cmd)
  2437. raise NoSupportedAuthentication(auths, self.authenticators.keys())
  2438. def login(self, username, password):
  2439. """
  2440. Authenticate with the server using a username and password
  2441. This command is allowed in the Non-Authenticated state. If the
  2442. server supports the STARTTLS capability and our transport supports
  2443. TLS, TLS is negotiated before the login command is issued.
  2444. A more secure way to log in is to use C{startTLS} or
  2445. C{authenticate} or both.
  2446. @type username: L{str}
  2447. @param username: The username to log in with
  2448. @type password: L{str}
  2449. @param password: The password to log in with
  2450. @rtype: L{Deferred}
  2451. @return: A deferred whose callback is invoked if login is successful
  2452. and whose errback is invoked otherwise.
  2453. """
  2454. d = maybeDeferred(self.getCapabilities)
  2455. d.addCallback(self.__cbLoginCaps, username, password)
  2456. return d
  2457. def serverGreeting(self, caps):
  2458. """
  2459. Called when the server has sent us a greeting.
  2460. @type caps: C{dict}
  2461. @param caps: Capabilities the server advertised in its greeting.
  2462. """
  2463. def _getContextFactory(self):
  2464. if self.context is not None:
  2465. return self.context
  2466. try:
  2467. from twisted.internet import ssl
  2468. except ImportError:
  2469. return None
  2470. else:
  2471. return ssl.ClientContextFactory()
  2472. def __cbLoginCaps(self, capabilities, username, password):
  2473. # If the server advertises STARTTLS, we might want to try to switch to TLS
  2474. tryTLS = b"STARTTLS" in capabilities
  2475. # If our transport supports switching to TLS, we might want to try to switch to TLS.
  2476. tlsableTransport = interfaces.ITLSTransport(self.transport, None) is not None
  2477. # If our transport is not already using TLS, we might want to try to switch to TLS.
  2478. nontlsTransport = interfaces.ISSLTransport(self.transport, None) is None
  2479. if not self.startedTLS and tryTLS and tlsableTransport and nontlsTransport:
  2480. d = self.startTLS()
  2481. d.addCallbacks(
  2482. self.__cbLoginTLS,
  2483. self.__ebLoginTLS,
  2484. callbackArgs=(username, password),
  2485. )
  2486. return d
  2487. else:
  2488. if nontlsTransport:
  2489. log.msg("Server has no TLS support. logging in over cleartext!")
  2490. args = b" ".join((_quote(username), _quote(password)))
  2491. return self.sendCommand(Command(b"LOGIN", args))
  2492. def _startedTLS(self, result, context):
  2493. self.transport.startTLS(context)
  2494. self._capCache = None
  2495. self.startedTLS = True
  2496. return result
  2497. def __cbLoginTLS(self, result, username, password):
  2498. args = b" ".join((_quote(username), _quote(password)))
  2499. return self.sendCommand(Command(b"LOGIN", args))
  2500. def __ebLoginTLS(self, failure):
  2501. log.err(failure)
  2502. return failure
  2503. def namespace(self):
  2504. """
  2505. Retrieve information about the namespaces available to this account
  2506. This command is allowed in the Authenticated and Selected states.
  2507. @rtype: L{Deferred}
  2508. @return: A deferred whose callback is invoked with namespace
  2509. information. An example of this information is::
  2510. [[['', '/']], [], []]
  2511. which indicates a single personal namespace called '' with '/'
  2512. as its hierarchical delimiter, and no shared or user namespaces.
  2513. """
  2514. cmd = b"NAMESPACE"
  2515. resp = (b"NAMESPACE",)
  2516. d = self.sendCommand(Command(cmd, wantResponse=resp))
  2517. d.addCallback(self.__cbNamespace)
  2518. return d
  2519. def __cbNamespace(self, result):
  2520. (lines, last) = result
  2521. # Namespaces and their delimiters qualify and delimit
  2522. # mailboxes, so they should be native strings
  2523. #
  2524. # On Python 2, no decoding is necessary to maintain
  2525. # the API contract.
  2526. #
  2527. # On Python 3, users specify mailboxes with native strings, so
  2528. # they should receive namespaces and delimiters as native
  2529. # strings. Both cases are possible because of the imap4-utf-7
  2530. # encoding.
  2531. def _prepareNamespaceOrDelimiter(namespaceList):
  2532. return [element.decode("imap4-utf-7") for element in namespaceList]
  2533. for parts in lines:
  2534. if len(parts) == 4 and parts[0] == b"NAMESPACE":
  2535. return [
  2536. []
  2537. if pairOrNone is None
  2538. else [_prepareNamespaceOrDelimiter(value) for value in pairOrNone]
  2539. for pairOrNone in parts[1:]
  2540. ]
  2541. log.err("No NAMESPACE response to NAMESPACE command")
  2542. return [[], [], []]
  2543. def select(self, mailbox):
  2544. """
  2545. Select a mailbox
  2546. This command is allowed in the Authenticated and Selected states.
  2547. @type mailbox: L{str}
  2548. @param mailbox: The name of the mailbox to select
  2549. @rtype: L{Deferred}
  2550. @return: A deferred whose callback is invoked with mailbox
  2551. information if the select is successful and whose errback is
  2552. invoked otherwise. Mailbox information consists of a dictionary
  2553. with the following L{str} keys and values::
  2554. FLAGS: A list of strings containing the flags settable on
  2555. messages in this mailbox.
  2556. EXISTS: An integer indicating the number of messages in this
  2557. mailbox.
  2558. RECENT: An integer indicating the number of "recent"
  2559. messages in this mailbox.
  2560. UNSEEN: The message sequence number (an integer) of the
  2561. first unseen message in the mailbox.
  2562. PERMANENTFLAGS: A list of strings containing the flags that
  2563. can be permanently set on messages in this mailbox.
  2564. UIDVALIDITY: An integer uniquely identifying this mailbox.
  2565. """
  2566. cmd = b"SELECT"
  2567. args = _prepareMailboxName(mailbox)
  2568. # This appears not to be used, so we can use native strings to
  2569. # indicate that the return type is native strings.
  2570. resp = ("FLAGS", "EXISTS", "RECENT", "UNSEEN", "PERMANENTFLAGS", "UIDVALIDITY")
  2571. d = self.sendCommand(Command(cmd, args, wantResponse=resp))
  2572. d.addCallback(self.__cbSelect, 1)
  2573. return d
  2574. def examine(self, mailbox):
  2575. """
  2576. Select a mailbox in read-only mode
  2577. This command is allowed in the Authenticated and Selected states.
  2578. @type mailbox: L{str}
  2579. @param mailbox: The name of the mailbox to examine
  2580. @rtype: L{Deferred}
  2581. @return: A deferred whose callback is invoked with mailbox
  2582. information if the examine is successful and whose errback
  2583. is invoked otherwise. Mailbox information consists of a dictionary
  2584. with the following keys and values::
  2585. 'FLAGS': A list of strings containing the flags settable on
  2586. messages in this mailbox.
  2587. 'EXISTS': An integer indicating the number of messages in this
  2588. mailbox.
  2589. 'RECENT': An integer indicating the number of \"recent\"
  2590. messages in this mailbox.
  2591. 'UNSEEN': An integer indicating the number of messages not
  2592. flagged \\Seen in this mailbox.
  2593. 'PERMANENTFLAGS': A list of strings containing the flags that
  2594. can be permanently set on messages in this mailbox.
  2595. 'UIDVALIDITY': An integer uniquely identifying this mailbox.
  2596. """
  2597. cmd = b"EXAMINE"
  2598. args = _prepareMailboxName(mailbox)
  2599. resp = (
  2600. b"FLAGS",
  2601. b"EXISTS",
  2602. b"RECENT",
  2603. b"UNSEEN",
  2604. b"PERMANENTFLAGS",
  2605. b"UIDVALIDITY",
  2606. )
  2607. d = self.sendCommand(Command(cmd, args, wantResponse=resp))
  2608. d.addCallback(self.__cbSelect, 0)
  2609. return d
  2610. def _intOrRaise(self, value, phrase):
  2611. """
  2612. Parse C{value} as an integer and return the result or raise
  2613. L{IllegalServerResponse} with C{phrase} as an argument if C{value}
  2614. cannot be parsed as an integer.
  2615. """
  2616. try:
  2617. return int(value)
  2618. except ValueError:
  2619. raise IllegalServerResponse(phrase)
  2620. def __cbSelect(self, result, rw):
  2621. """
  2622. Handle lines received in response to a SELECT or EXAMINE command.
  2623. See RFC 3501, section 6.3.1.
  2624. """
  2625. (lines, tagline) = result
  2626. # In the absence of specification, we are free to assume:
  2627. # READ-WRITE access
  2628. datum = {"READ-WRITE": rw}
  2629. lines.append(parseNestedParens(tagline))
  2630. for split in lines:
  2631. if len(split) > 0 and split[0].upper() == b"OK":
  2632. # Handle all the kinds of OK response.
  2633. content = split[1]
  2634. if isinstance(content, list):
  2635. key = content[0]
  2636. else:
  2637. # not multi-valued, like OK LOGIN
  2638. key = content
  2639. key = key.upper()
  2640. if key == b"READ-ONLY":
  2641. datum["READ-WRITE"] = False
  2642. elif key == b"READ-WRITE":
  2643. datum["READ-WRITE"] = True
  2644. elif key == b"UIDVALIDITY":
  2645. datum["UIDVALIDITY"] = self._intOrRaise(content[1], split)
  2646. elif key == b"UNSEEN":
  2647. datum["UNSEEN"] = self._intOrRaise(content[1], split)
  2648. elif key == b"UIDNEXT":
  2649. datum["UIDNEXT"] = self._intOrRaise(content[1], split)
  2650. elif key == b"PERMANENTFLAGS":
  2651. datum["PERMANENTFLAGS"] = tuple(
  2652. nativeString(flag) for flag in content[1]
  2653. )
  2654. else:
  2655. log.err(f"Unhandled SELECT response (2): {split}")
  2656. elif len(split) == 2:
  2657. # Handle FLAGS, EXISTS, and RECENT
  2658. if split[0].upper() == b"FLAGS":
  2659. datum["FLAGS"] = tuple(nativeString(flag) for flag in split[1])
  2660. elif isinstance(split[1], bytes):
  2661. # Must make sure things are strings before treating them as
  2662. # strings since some other forms of response have nesting in
  2663. # places which results in lists instead.
  2664. if split[1].upper() == b"EXISTS":
  2665. datum["EXISTS"] = self._intOrRaise(split[0], split)
  2666. elif split[1].upper() == b"RECENT":
  2667. datum["RECENT"] = self._intOrRaise(split[0], split)
  2668. else:
  2669. log.err(f"Unhandled SELECT response (0): {split}")
  2670. else:
  2671. log.err(f"Unhandled SELECT response (1): {split}")
  2672. else:
  2673. log.err(f"Unhandled SELECT response (4): {split}")
  2674. return datum
  2675. def create(self, name):
  2676. """
  2677. Create a new mailbox on the server
  2678. This command is allowed in the Authenticated and Selected states.
  2679. @type name: L{str}
  2680. @param name: The name of the mailbox to create.
  2681. @rtype: L{Deferred}
  2682. @return: A deferred whose callback is invoked if the mailbox creation
  2683. is successful and whose errback is invoked otherwise.
  2684. """
  2685. return self.sendCommand(Command(b"CREATE", _prepareMailboxName(name)))
  2686. def delete(self, name):
  2687. """
  2688. Delete a mailbox
  2689. This command is allowed in the Authenticated and Selected states.
  2690. @type name: L{str}
  2691. @param name: The name of the mailbox to delete.
  2692. @rtype: L{Deferred}
  2693. @return: A deferred whose calblack is invoked if the mailbox is
  2694. deleted successfully and whose errback is invoked otherwise.
  2695. """
  2696. return self.sendCommand(Command(b"DELETE", _prepareMailboxName(name)))
  2697. def rename(self, oldname, newname):
  2698. """
  2699. Rename a mailbox
  2700. This command is allowed in the Authenticated and Selected states.
  2701. @type oldname: L{str}
  2702. @param oldname: The current name of the mailbox to rename.
  2703. @type newname: L{str}
  2704. @param newname: The new name to give the mailbox.
  2705. @rtype: L{Deferred}
  2706. @return: A deferred whose callback is invoked if the rename is
  2707. successful and whose errback is invoked otherwise.
  2708. """
  2709. oldname = _prepareMailboxName(oldname)
  2710. newname = _prepareMailboxName(newname)
  2711. return self.sendCommand(Command(b"RENAME", b" ".join((oldname, newname))))
  2712. def subscribe(self, name):
  2713. """
  2714. Add a mailbox to the subscription list
  2715. This command is allowed in the Authenticated and Selected states.
  2716. @type name: L{str}
  2717. @param name: The mailbox to mark as 'active' or 'subscribed'
  2718. @rtype: L{Deferred}
  2719. @return: A deferred whose callback is invoked if the subscription
  2720. is successful and whose errback is invoked otherwise.
  2721. """
  2722. return self.sendCommand(Command(b"SUBSCRIBE", _prepareMailboxName(name)))
  2723. def unsubscribe(self, name):
  2724. """
  2725. Remove a mailbox from the subscription list
  2726. This command is allowed in the Authenticated and Selected states.
  2727. @type name: L{str}
  2728. @param name: The mailbox to unsubscribe
  2729. @rtype: L{Deferred}
  2730. @return: A deferred whose callback is invoked if the unsubscription
  2731. is successful and whose errback is invoked otherwise.
  2732. """
  2733. return self.sendCommand(Command(b"UNSUBSCRIBE", _prepareMailboxName(name)))
  2734. def list(self, reference, wildcard):
  2735. """
  2736. List a subset of the available mailboxes
  2737. This command is allowed in the Authenticated and Selected
  2738. states.
  2739. @type reference: L{str}
  2740. @param reference: The context in which to interpret
  2741. C{wildcard}
  2742. @type wildcard: L{str}
  2743. @param wildcard: The pattern of mailbox names to match,
  2744. optionally including either or both of the '*' and '%'
  2745. wildcards. '*' will match zero or more characters and
  2746. cross hierarchical boundaries. '%' will also match zero
  2747. or more characters, but is limited to a single
  2748. hierarchical level.
  2749. @rtype: L{Deferred}
  2750. @return: A deferred whose callback is invoked with a list of
  2751. L{tuple}s, the first element of which is a L{tuple} of
  2752. mailbox flags, the second element of which is the
  2753. hierarchy delimiter for this mailbox, and the third of
  2754. which is the mailbox name; if the command is unsuccessful,
  2755. the deferred's errback is invoked instead. B{NB}: the
  2756. delimiter and the mailbox name are L{str}s.
  2757. """
  2758. cmd = b"LIST"
  2759. args = (f'"{reference}" "{wildcard}"').encode("imap4-utf-7")
  2760. resp = (b"LIST",)
  2761. d = self.sendCommand(Command(cmd, args, wantResponse=resp))
  2762. d.addCallback(self.__cbList, b"LIST")
  2763. return d
  2764. def lsub(self, reference, wildcard):
  2765. """
  2766. List a subset of the subscribed available mailboxes
  2767. This command is allowed in the Authenticated and Selected states.
  2768. The parameters and returned object are the same as for the L{list}
  2769. method, with one slight difference: Only mailboxes which have been
  2770. subscribed can be included in the resulting list.
  2771. """
  2772. cmd = b"LSUB"
  2773. encodedReference = reference.encode("ascii")
  2774. encodedWildcard = wildcard.encode("imap4-utf-7")
  2775. args = b"".join(
  2776. [
  2777. b'"',
  2778. encodedReference,
  2779. b'"' b' "',
  2780. encodedWildcard,
  2781. b'"',
  2782. ]
  2783. )
  2784. resp = (b"LSUB",)
  2785. d = self.sendCommand(Command(cmd, args, wantResponse=resp))
  2786. d.addCallback(self.__cbList, b"LSUB")
  2787. return d
  2788. def __cbList(self, result, command):
  2789. (lines, last) = result
  2790. results = []
  2791. for parts in lines:
  2792. if len(parts) == 4 and parts[0] == command:
  2793. # flags
  2794. parts[1] = tuple(nativeString(flag) for flag in parts[1])
  2795. # The mailbox should be a native string.
  2796. # On Python 2, this maintains the API's contract.
  2797. #
  2798. # On Python 3, users specify mailboxes with native
  2799. # strings, so they should receive mailboxes as native
  2800. # strings. Both cases are possible because of the
  2801. # imap4-utf-7 encoding.
  2802. #
  2803. # Mailbox names contain the hierarchical delimiter, so
  2804. # it too should be a native string.
  2805. # delimiter
  2806. parts[2] = parts[2].decode("imap4-utf-7")
  2807. # mailbox
  2808. parts[3] = parts[3].decode("imap4-utf-7")
  2809. results.append(tuple(parts[1:]))
  2810. return results
  2811. _statusNames = {
  2812. name: name.encode("ascii")
  2813. for name in (
  2814. "MESSAGES",
  2815. "RECENT",
  2816. "UIDNEXT",
  2817. "UIDVALIDITY",
  2818. "UNSEEN",
  2819. )
  2820. }
  2821. def status(self, mailbox, *names):
  2822. """
  2823. Retrieve the status of the given mailbox
  2824. This command is allowed in the Authenticated and Selected states.
  2825. @type mailbox: L{str}
  2826. @param mailbox: The name of the mailbox to query
  2827. @type names: L{bytes}
  2828. @param names: The status names to query. These may be any number of:
  2829. C{'MESSAGES'}, C{'RECENT'}, C{'UIDNEXT'}, C{'UIDVALIDITY'}, and
  2830. C{'UNSEEN'}.
  2831. @rtype: L{Deferred}
  2832. @return: A deferred which fires with the status information if the
  2833. command is successful and whose errback is invoked otherwise. The
  2834. status information is in the form of a C{dict}. Each element of
  2835. C{names} is a key in the dictionary. The value for each key is the
  2836. corresponding response from the server.
  2837. """
  2838. cmd = b"STATUS"
  2839. preparedMailbox = _prepareMailboxName(mailbox)
  2840. try:
  2841. names = b" ".join(self._statusNames[name] for name in names)
  2842. except KeyError:
  2843. raise ValueError(f"Unknown names: {set(names) - set(self._statusNames)!r}")
  2844. args = b"".join([preparedMailbox, b" (", names, b")"])
  2845. resp = (b"STATUS",)
  2846. d = self.sendCommand(Command(cmd, args, wantResponse=resp))
  2847. d.addCallback(self.__cbStatus)
  2848. return d
  2849. def __cbStatus(self, result):
  2850. (lines, last) = result
  2851. status = {}
  2852. for parts in lines:
  2853. if parts[0] == b"STATUS":
  2854. items = parts[2]
  2855. items = [items[i : i + 2] for i in range(0, len(items), 2)]
  2856. for k, v in items:
  2857. try:
  2858. status[nativeString(k)] = v
  2859. except UnicodeDecodeError:
  2860. raise IllegalServerResponse(repr(items))
  2861. for k in status.keys():
  2862. t = self.STATUS_TRANSFORMATIONS.get(k)
  2863. if t:
  2864. try:
  2865. status[k] = t(status[k])
  2866. except Exception as e:
  2867. raise IllegalServerResponse(
  2868. "(" + k + " " + status[k] + "): " + str(e)
  2869. )
  2870. return status
  2871. def append(self, mailbox, message, flags=(), date=None):
  2872. """
  2873. Add the given message to the given mailbox.
  2874. This command is allowed in the Authenticated and Selected states.
  2875. @type mailbox: L{str}
  2876. @param mailbox: The mailbox to which to add this message.
  2877. @type message: Any file-like object opened in B{binary mode}.
  2878. @param message: The message to add, in RFC822 format. Newlines
  2879. in this file should be \\r\\n-style.
  2880. @type flags: Any iterable of L{str}
  2881. @param flags: The flags to associated with this message.
  2882. @type date: L{str}
  2883. @param date: The date to associate with this message. This should
  2884. be of the format DD-MM-YYYY HH:MM:SS +/-HHMM. For example, in
  2885. Eastern Standard Time, on July 1st 2004 at half past 1 PM,
  2886. \"01-07-2004 13:30:00 -0500\".
  2887. @rtype: L{Deferred}
  2888. @return: A deferred whose callback is invoked when this command
  2889. succeeds or whose errback is invoked if it fails.
  2890. """
  2891. message.seek(0, 2)
  2892. L = message.tell()
  2893. message.seek(0, 0)
  2894. if date:
  2895. date = networkString(' "%s"' % nativeString(date))
  2896. else:
  2897. date = b""
  2898. encodedFlags = [networkString(flag) for flag in flags]
  2899. cmd = b"%b (%b)%b {%d}" % (
  2900. _prepareMailboxName(mailbox),
  2901. b" ".join(encodedFlags),
  2902. date,
  2903. L,
  2904. )
  2905. d = self.sendCommand(
  2906. Command(b"APPEND", cmd, (), self.__cbContinueAppend, message)
  2907. )
  2908. return d
  2909. def __cbContinueAppend(self, lines, message):
  2910. s = basic.FileSender()
  2911. return s.beginFileTransfer(message, self.transport, None).addCallback(
  2912. self.__cbFinishAppend
  2913. )
  2914. def __cbFinishAppend(self, foo):
  2915. self.sendLine(b"")
  2916. def check(self):
  2917. """
  2918. Tell the server to perform a checkpoint
  2919. This command is allowed in the Selected state.
  2920. @rtype: L{Deferred}
  2921. @return: A deferred whose callback is invoked when this command
  2922. succeeds or whose errback is invoked if it fails.
  2923. """
  2924. return self.sendCommand(Command(b"CHECK"))
  2925. def close(self):
  2926. """
  2927. Return the connection to the Authenticated state.
  2928. This command is allowed in the Selected state.
  2929. Issuing this command will also remove all messages flagged \\Deleted
  2930. from the selected mailbox if it is opened in read-write mode,
  2931. otherwise it indicates success by no messages are removed.
  2932. @rtype: L{Deferred}
  2933. @return: A deferred whose callback is invoked when the command
  2934. completes successfully or whose errback is invoked if it fails.
  2935. """
  2936. return self.sendCommand(Command(b"CLOSE"))
  2937. def expunge(self):
  2938. """
  2939. Return the connection to the Authenticate state.
  2940. This command is allowed in the Selected state.
  2941. Issuing this command will perform the same actions as issuing the
  2942. close command, but will also generate an 'expunge' response for
  2943. every message deleted.
  2944. @rtype: L{Deferred}
  2945. @return: A deferred whose callback is invoked with a list of the
  2946. 'expunge' responses when this command is successful or whose errback
  2947. is invoked otherwise.
  2948. """
  2949. cmd = b"EXPUNGE"
  2950. resp = (b"EXPUNGE",)
  2951. d = self.sendCommand(Command(cmd, wantResponse=resp))
  2952. d.addCallback(self.__cbExpunge)
  2953. return d
  2954. def __cbExpunge(self, result):
  2955. (lines, last) = result
  2956. ids = []
  2957. for parts in lines:
  2958. if len(parts) == 2 and parts[1] == b"EXPUNGE":
  2959. ids.append(self._intOrRaise(parts[0], parts))
  2960. return ids
  2961. def search(self, *queries, uid=False):
  2962. """
  2963. Search messages in the currently selected mailbox
  2964. This command is allowed in the Selected state.
  2965. Any non-zero number of queries are accepted by this method, as returned
  2966. by the C{Query}, C{Or}, and C{Not} functions.
  2967. @param uid: if true, the server is asked to return message UIDs instead
  2968. of message sequence numbers.
  2969. @type uid: L{bool}
  2970. @rtype: L{Deferred}
  2971. @return: A deferred whose callback will be invoked with a list of all
  2972. the message sequence numbers return by the search, or whose errback
  2973. will be invoked if there is an error.
  2974. """
  2975. # Queries should be encoded as ASCII unless a charset
  2976. # identifier is provided. See #9201.
  2977. queries = [query.encode("charmap") for query in queries]
  2978. cmd = b"UID SEARCH" if uid else b"SEARCH"
  2979. args = b" ".join(queries)
  2980. d = self.sendCommand(Command(cmd, args, wantResponse=(cmd,)))
  2981. d.addCallback(self.__cbSearch)
  2982. return d
  2983. def __cbSearch(self, result):
  2984. (lines, end) = result
  2985. ids = []
  2986. for parts in lines:
  2987. if len(parts) > 0 and parts[0] == b"SEARCH":
  2988. ids.extend([self._intOrRaise(p, parts) for p in parts[1:]])
  2989. return ids
  2990. def fetchUID(self, messages, uid=0):
  2991. """
  2992. Retrieve the unique identifier for one or more messages
  2993. This command is allowed in the Selected state.
  2994. @type messages: L{MessageSet} or L{str}
  2995. @param messages: A message sequence set
  2996. @type uid: L{bool}
  2997. @param uid: Indicates whether the message sequence set is of message
  2998. numbers or of unique message IDs.
  2999. @rtype: L{Deferred}
  3000. @return: A deferred whose callback is invoked with a dict mapping
  3001. message sequence numbers to unique message identifiers, or whose
  3002. errback is invoked if there is an error.
  3003. """
  3004. return self._fetch(messages, useUID=uid, uid=1)
  3005. def fetchFlags(self, messages, uid=0):
  3006. """
  3007. Retrieve the flags for one or more messages
  3008. This command is allowed in the Selected state.
  3009. @type messages: L{MessageSet} or L{str}
  3010. @param messages: The messages for which to retrieve flags.
  3011. @type uid: L{bool}
  3012. @param uid: Indicates whether the message sequence set is of message
  3013. numbers or of unique message IDs.
  3014. @rtype: L{Deferred}
  3015. @return: A deferred whose callback is invoked with a dict mapping
  3016. message numbers to lists of flags, or whose errback is invoked if
  3017. there is an error.
  3018. """
  3019. return self._fetch(messages, useUID=uid, flags=1)
  3020. def fetchInternalDate(self, messages, uid=0):
  3021. """
  3022. Retrieve the internal date associated with one or more messages
  3023. This command is allowed in the Selected state.
  3024. @type messages: L{MessageSet} or L{str}
  3025. @param messages: The messages for which to retrieve the internal date.
  3026. @type uid: L{bool}
  3027. @param uid: Indicates whether the message sequence set is of message
  3028. numbers or of unique message IDs.
  3029. @rtype: L{Deferred}
  3030. @return: A deferred whose callback is invoked with a dict mapping
  3031. message numbers to date strings, or whose errback is invoked
  3032. if there is an error. Date strings take the format of
  3033. \"day-month-year time timezone\".
  3034. """
  3035. return self._fetch(messages, useUID=uid, internaldate=1)
  3036. def fetchEnvelope(self, messages, uid=0):
  3037. """
  3038. Retrieve the envelope data for one or more messages
  3039. This command is allowed in the Selected state.
  3040. @type messages: L{MessageSet} or L{str}
  3041. @param messages: The messages for which to retrieve envelope
  3042. data.
  3043. @type uid: L{bool}
  3044. @param uid: Indicates whether the message sequence set is of
  3045. message numbers or of unique message IDs.
  3046. @rtype: L{Deferred}
  3047. @return: A deferred whose callback is invoked with a dict
  3048. mapping message numbers to envelope data, or whose errback
  3049. is invoked if there is an error. Envelope data consists
  3050. of a sequence of the date, subject, from, sender,
  3051. reply-to, to, cc, bcc, in-reply-to, and message-id header
  3052. fields. The date, subject, in-reply-to, and message-id
  3053. fields are L{str}, while the from, sender, reply-to, to,
  3054. cc, and bcc fields contain address data as L{str}s.
  3055. Address data consists of a sequence of name, source route,
  3056. mailbox name, and hostname. Fields which are not present
  3057. for a particular address may be L{None}.
  3058. """
  3059. return self._fetch(messages, useUID=uid, envelope=1)
  3060. def fetchBodyStructure(self, messages, uid=0):
  3061. """
  3062. Retrieve the structure of the body of one or more messages
  3063. This command is allowed in the Selected state.
  3064. @type messages: L{MessageSet} or L{str}
  3065. @param messages: The messages for which to retrieve body structure
  3066. data.
  3067. @type uid: L{bool}
  3068. @param uid: Indicates whether the message sequence set is of message
  3069. numbers or of unique message IDs.
  3070. @rtype: L{Deferred}
  3071. @return: A deferred whose callback is invoked with a dict mapping
  3072. message numbers to body structure data, or whose errback is invoked
  3073. if there is an error. Body structure data describes the MIME-IMB
  3074. format of a message and consists of a sequence of mime type, mime
  3075. subtype, parameters, content id, description, encoding, and size.
  3076. The fields following the size field are variable: if the mime
  3077. type/subtype is message/rfc822, the contained message's envelope
  3078. information, body structure data, and number of lines of text; if
  3079. the mime type is text, the number of lines of text. Extension fields
  3080. may also be included; if present, they are: the MD5 hash of the body,
  3081. body disposition, body language.
  3082. """
  3083. return self._fetch(messages, useUID=uid, bodystructure=1)
  3084. def fetchSimplifiedBody(self, messages, uid=0):
  3085. """
  3086. Retrieve the simplified body structure of one or more messages
  3087. This command is allowed in the Selected state.
  3088. @type messages: L{MessageSet} or L{str}
  3089. @param messages: A message sequence set
  3090. @type uid: C{bool}
  3091. @param uid: Indicates whether the message sequence set is of message
  3092. numbers or of unique message IDs.
  3093. @rtype: L{Deferred}
  3094. @return: A deferred whose callback is invoked with a dict mapping
  3095. message numbers to body data, or whose errback is invoked
  3096. if there is an error. The simplified body structure is the same
  3097. as the body structure, except that extension fields will never be
  3098. present.
  3099. """
  3100. return self._fetch(messages, useUID=uid, body=1)
  3101. def fetchMessage(self, messages, uid=0):
  3102. """
  3103. Retrieve one or more entire messages
  3104. This command is allowed in the Selected state.
  3105. @type messages: L{MessageSet} or L{str}
  3106. @param messages: A message sequence set
  3107. @type uid: C{bool}
  3108. @param uid: Indicates whether the message sequence set is of message
  3109. numbers or of unique message IDs.
  3110. @rtype: L{Deferred}
  3111. @return: A L{Deferred} which will fire with a C{dict} mapping message
  3112. sequence numbers to C{dict}s giving message data for the
  3113. corresponding message. If C{uid} is true, the inner dictionaries
  3114. have a C{'UID'} key mapped to a L{str} giving the UID for the
  3115. message. The text of the message is a L{str} associated with the
  3116. C{'RFC822'} key in each dictionary.
  3117. """
  3118. return self._fetch(messages, useUID=uid, rfc822=1)
  3119. def fetchHeaders(self, messages, uid=0):
  3120. """
  3121. Retrieve headers of one or more messages
  3122. This command is allowed in the Selected state.
  3123. @type messages: L{MessageSet} or L{str}
  3124. @param messages: A message sequence set
  3125. @type uid: L{bool}
  3126. @param uid: Indicates whether the message sequence set is of message
  3127. numbers or of unique message IDs.
  3128. @rtype: L{Deferred}
  3129. @return: A deferred whose callback is invoked with a dict mapping
  3130. message numbers to dicts of message headers, or whose errback is
  3131. invoked if there is an error.
  3132. """
  3133. return self._fetch(messages, useUID=uid, rfc822header=1)
  3134. def fetchBody(self, messages, uid=0):
  3135. """
  3136. Retrieve body text of one or more messages
  3137. This command is allowed in the Selected state.
  3138. @type messages: L{MessageSet} or L{str}
  3139. @param messages: A message sequence set
  3140. @type uid: L{bool}
  3141. @param uid: Indicates whether the message sequence set is of message
  3142. numbers or of unique message IDs.
  3143. @rtype: L{Deferred}
  3144. @return: A deferred whose callback is invoked with a dict mapping
  3145. message numbers to file-like objects containing body text, or whose
  3146. errback is invoked if there is an error.
  3147. """
  3148. return self._fetch(messages, useUID=uid, rfc822text=1)
  3149. def fetchSize(self, messages, uid=0):
  3150. """
  3151. Retrieve the size, in octets, of one or more messages
  3152. This command is allowed in the Selected state.
  3153. @type messages: L{MessageSet} or L{str}
  3154. @param messages: A message sequence set
  3155. @type uid: L{bool}
  3156. @param uid: Indicates whether the message sequence set is of message
  3157. numbers or of unique message IDs.
  3158. @rtype: L{Deferred}
  3159. @return: A deferred whose callback is invoked with a dict mapping
  3160. message numbers to sizes, or whose errback is invoked if there is
  3161. an error.
  3162. """
  3163. return self._fetch(messages, useUID=uid, rfc822size=1)
  3164. def fetchFull(self, messages, uid=0):
  3165. """
  3166. Retrieve several different fields of one or more messages
  3167. This command is allowed in the Selected state. This is equivalent
  3168. to issuing all of the C{fetchFlags}, C{fetchInternalDate},
  3169. C{fetchSize}, C{fetchEnvelope}, and C{fetchSimplifiedBody}
  3170. functions.
  3171. @type messages: L{MessageSet} or L{str}
  3172. @param messages: A message sequence set
  3173. @type uid: L{bool}
  3174. @param uid: Indicates whether the message sequence set is of message
  3175. numbers or of unique message IDs.
  3176. @rtype: L{Deferred}
  3177. @return: A deferred whose callback is invoked with a dict mapping
  3178. message numbers to dict of the retrieved data values, or whose
  3179. errback is invoked if there is an error. They dictionary keys
  3180. are "flags", "date", "size", "envelope", and "body".
  3181. """
  3182. return self._fetch(
  3183. messages,
  3184. useUID=uid,
  3185. flags=1,
  3186. internaldate=1,
  3187. rfc822size=1,
  3188. envelope=1,
  3189. body=1,
  3190. )
  3191. def fetchAll(self, messages, uid=0):
  3192. """
  3193. Retrieve several different fields of one or more messages
  3194. This command is allowed in the Selected state. This is equivalent
  3195. to issuing all of the C{fetchFlags}, C{fetchInternalDate},
  3196. C{fetchSize}, and C{fetchEnvelope} functions.
  3197. @type messages: L{MessageSet} or L{str}
  3198. @param messages: A message sequence set
  3199. @type uid: L{bool}
  3200. @param uid: Indicates whether the message sequence set is of message
  3201. numbers or of unique message IDs.
  3202. @rtype: L{Deferred}
  3203. @return: A deferred whose callback is invoked with a dict mapping
  3204. message numbers to dict of the retrieved data values, or whose
  3205. errback is invoked if there is an error. They dictionary keys
  3206. are "flags", "date", "size", and "envelope".
  3207. """
  3208. return self._fetch(
  3209. messages, useUID=uid, flags=1, internaldate=1, rfc822size=1, envelope=1
  3210. )
  3211. def fetchFast(self, messages, uid=0):
  3212. """
  3213. Retrieve several different fields of one or more messages
  3214. This command is allowed in the Selected state. This is equivalent
  3215. to issuing all of the C{fetchFlags}, C{fetchInternalDate}, and
  3216. C{fetchSize} functions.
  3217. @type messages: L{MessageSet} or L{str}
  3218. @param messages: A message sequence set
  3219. @type uid: L{bool}
  3220. @param uid: Indicates whether the message sequence set is of message
  3221. numbers or of unique message IDs.
  3222. @rtype: L{Deferred}
  3223. @return: A deferred whose callback is invoked with a dict mapping
  3224. message numbers to dict of the retrieved data values, or whose
  3225. errback is invoked if there is an error. They dictionary keys are
  3226. "flags", "date", and "size".
  3227. """
  3228. return self._fetch(messages, useUID=uid, flags=1, internaldate=1, rfc822size=1)
  3229. def _parseFetchPairs(self, fetchResponseList):
  3230. """
  3231. Given the result of parsing a single I{FETCH} response, construct a
  3232. L{dict} mapping response keys to response values.
  3233. @param fetchResponseList: The result of parsing a I{FETCH} response
  3234. with L{parseNestedParens} and extracting just the response data
  3235. (that is, just the part that comes after C{"FETCH"}). The form
  3236. of this input (and therefore the output of this method) is very
  3237. disagreeable. A valuable improvement would be to enumerate the
  3238. possible keys (representing them as structured objects of some
  3239. sort) rather than using strings and tuples of tuples of strings
  3240. and so forth. This would allow the keys to be documented more
  3241. easily and would allow for a much simpler application-facing API
  3242. (one not based on looking up somewhat hard to predict keys in a
  3243. dict). Since C{fetchResponseList} notionally represents a
  3244. flattened sequence of pairs (identifying keys followed by their
  3245. associated values), collapsing such complex elements of this
  3246. list as C{["BODY", ["HEADER.FIELDS", ["SUBJECT"]]]} into a
  3247. single object would also greatly simplify the implementation of
  3248. this method.
  3249. @return: A C{dict} of the response data represented by C{pairs}. Keys
  3250. in this dictionary are things like C{"RFC822.TEXT"}, C{"FLAGS"}, or
  3251. C{("BODY", ("HEADER.FIELDS", ("SUBJECT",)))}. Values are entirely
  3252. dependent on the key with which they are associated, but retain the
  3253. same structured as produced by L{parseNestedParens}.
  3254. """
  3255. # TODO: RFC 3501 Section 7.4.2, "FETCH Response", says for
  3256. # BODY responses that "8-bit textual data is permitted if a
  3257. # charset identifier is part of the body parameter
  3258. # parenthesized list". Every other component is 7-bit. This
  3259. # should parse out the charset identifier and use it to decode
  3260. # 8-bit bodies. Until then, on Python 2 it should continue to
  3261. # return native (byte) strings, while on Python 3 it should
  3262. # decode bytes to native strings via charmap, ensuring data
  3263. # fidelity at the cost of mojibake.
  3264. def nativeStringResponse(thing):
  3265. if isinstance(thing, bytes):
  3266. return thing.decode("charmap")
  3267. elif isinstance(thing, list):
  3268. return [nativeStringResponse(subthing) for subthing in thing]
  3269. values = {}
  3270. unstructured = []
  3271. responseParts = iter(fetchResponseList)
  3272. while True:
  3273. try:
  3274. key = next(responseParts)
  3275. except StopIteration:
  3276. break
  3277. try:
  3278. value = next(responseParts)
  3279. except StopIteration:
  3280. raise IllegalServerResponse(b"Not enough arguments", fetchResponseList)
  3281. # The parsed forms of responses like:
  3282. #
  3283. # BODY[] VALUE
  3284. # BODY[TEXT] VALUE
  3285. # BODY[HEADER.FIELDS (SUBJECT)] VALUE
  3286. # BODY[HEADER.FIELDS (SUBJECT)]<N.M> VALUE
  3287. #
  3288. # are:
  3289. #
  3290. # ["BODY", [], VALUE]
  3291. # ["BODY", ["TEXT"], VALUE]
  3292. # ["BODY", ["HEADER.FIELDS", ["SUBJECT"]], VALUE]
  3293. # ["BODY", ["HEADER.FIELDS", ["SUBJECT"]], "<N.M>", VALUE]
  3294. #
  3295. # Additionally, BODY responses for multipart messages are
  3296. # represented as:
  3297. #
  3298. # ["BODY", VALUE]
  3299. #
  3300. # with list as the type of VALUE and the type of VALUE[0].
  3301. #
  3302. # See #6281 for ideas on how this might be improved.
  3303. if key not in (b"BODY", b"BODY.PEEK"):
  3304. # Only BODY (and by extension, BODY.PEEK) responses can have
  3305. # body sections.
  3306. hasSection = False
  3307. elif not isinstance(value, list):
  3308. # A BODY section is always represented as a list. Any non-list
  3309. # is not a BODY section.
  3310. hasSection = False
  3311. elif len(value) > 2:
  3312. # The list representing a BODY section has at most two elements.
  3313. hasSection = False
  3314. elif value and isinstance(value[0], list):
  3315. # A list containing a list represents the body structure of a
  3316. # multipart message, instead.
  3317. hasSection = False
  3318. else:
  3319. # Otherwise it must have a BODY section to examine.
  3320. hasSection = True
  3321. # If it has a BODY section, grab some extra elements and shuffle
  3322. # around the shape of the key a little bit.
  3323. key = nativeString(key)
  3324. unstructured.append(key)
  3325. if hasSection:
  3326. if len(value) < 2:
  3327. value = [nativeString(v) for v in value]
  3328. unstructured.append(value)
  3329. key = (key, tuple(value))
  3330. else:
  3331. valueHead = nativeString(value[0])
  3332. valueTail = [nativeString(v) for v in value[1]]
  3333. unstructured.append([valueHead, valueTail])
  3334. key = (key, (valueHead, tuple(valueTail)))
  3335. try:
  3336. value = next(responseParts)
  3337. except StopIteration:
  3338. raise IllegalServerResponse(
  3339. b"Not enough arguments", fetchResponseList
  3340. )
  3341. # Handle partial ranges
  3342. if value.startswith(b"<") and value.endswith(b">"):
  3343. try:
  3344. int(value[1:-1])
  3345. except ValueError:
  3346. # This isn't really a range, it's some content.
  3347. pass
  3348. else:
  3349. value = nativeString(value)
  3350. unstructured.append(value)
  3351. key = key + (value,)
  3352. try:
  3353. value = next(responseParts)
  3354. except StopIteration:
  3355. raise IllegalServerResponse(
  3356. b"Not enough arguments", fetchResponseList
  3357. )
  3358. value = nativeStringResponse(value)
  3359. unstructured.append(value)
  3360. values[key] = value
  3361. return values, unstructured
  3362. def _cbFetch(self, result, requestedParts, structured):
  3363. (lines, last) = result
  3364. info = {}
  3365. for parts in lines:
  3366. if len(parts) == 3 and parts[1] == b"FETCH":
  3367. id = self._intOrRaise(parts[0], parts)
  3368. if id not in info:
  3369. info[id] = [parts[2]]
  3370. else:
  3371. info[id][0].extend(parts[2])
  3372. results = {}
  3373. decodedInfo = {}
  3374. for (messageId, values) in info.items():
  3375. structuredMap, unstructuredList = self._parseFetchPairs(values[0])
  3376. decodedInfo.setdefault(messageId, [[]])[0].extend(unstructuredList)
  3377. results.setdefault(messageId, {}).update(structuredMap)
  3378. info = decodedInfo
  3379. flagChanges = {}
  3380. for messageId in list(results.keys()):
  3381. values = results[messageId]
  3382. for part in list(values.keys()):
  3383. if part not in requestedParts and part == "FLAGS":
  3384. flagChanges[messageId] = values["FLAGS"]
  3385. # Find flags in the result and get rid of them.
  3386. for i in range(len(info[messageId][0])):
  3387. if info[messageId][0][i] == "FLAGS":
  3388. del info[messageId][0][i : i + 2]
  3389. break
  3390. del values["FLAGS"]
  3391. if not values:
  3392. del results[messageId]
  3393. if flagChanges:
  3394. self.flagsChanged(flagChanges)
  3395. if structured:
  3396. return results
  3397. else:
  3398. return info
  3399. def fetchSpecific(
  3400. self,
  3401. messages,
  3402. uid=0,
  3403. headerType=None,
  3404. headerNumber=None,
  3405. headerArgs=None,
  3406. peek=None,
  3407. offset=None,
  3408. length=None,
  3409. ):
  3410. """
  3411. Retrieve a specific section of one or more messages
  3412. @type messages: L{MessageSet} or L{str}
  3413. @param messages: A message sequence set
  3414. @type uid: L{bool}
  3415. @param uid: Indicates whether the message sequence set is of message
  3416. numbers or of unique message IDs.
  3417. @type headerType: L{str}
  3418. @param headerType: If specified, must be one of HEADER, HEADER.FIELDS,
  3419. HEADER.FIELDS.NOT, MIME, or TEXT, and will determine which part of
  3420. the message is retrieved. For HEADER.FIELDS and HEADER.FIELDS.NOT,
  3421. C{headerArgs} must be a sequence of header names. For MIME,
  3422. C{headerNumber} must be specified.
  3423. @type headerNumber: L{int} or L{int} sequence
  3424. @param headerNumber: The nested rfc822 index specifying the entity to
  3425. retrieve. For example, C{1} retrieves the first entity of the
  3426. message, and C{(2, 1, 3}) retrieves the 3rd entity inside the first
  3427. entity inside the second entity of the message.
  3428. @type headerArgs: A sequence of L{str}
  3429. @param headerArgs: If C{headerType} is HEADER.FIELDS, these are the
  3430. headers to retrieve. If it is HEADER.FIELDS.NOT, these are the
  3431. headers to exclude from retrieval.
  3432. @type peek: C{bool}
  3433. @param peek: If true, cause the server to not set the \\Seen flag on
  3434. this message as a result of this command.
  3435. @type offset: L{int}
  3436. @param offset: The number of octets at the beginning of the result to
  3437. skip.
  3438. @type length: L{int}
  3439. @param length: The number of octets to retrieve.
  3440. @rtype: L{Deferred}
  3441. @return: A deferred whose callback is invoked with a mapping of message
  3442. numbers to retrieved data, or whose errback is invoked if there is
  3443. an error.
  3444. """
  3445. fmt = "%s BODY%s[%s%s%s]%s"
  3446. if headerNumber is None:
  3447. number = ""
  3448. elif isinstance(headerNumber, int):
  3449. number = str(headerNumber)
  3450. else:
  3451. number = ".".join(map(str, headerNumber))
  3452. if headerType is None:
  3453. header = ""
  3454. elif number:
  3455. header = "." + headerType
  3456. else:
  3457. header = headerType
  3458. if header and headerType in ("HEADER.FIELDS", "HEADER.FIELDS.NOT"):
  3459. if headerArgs is not None:
  3460. payload = " (%s)" % " ".join(headerArgs)
  3461. else:
  3462. payload = " ()"
  3463. else:
  3464. payload = ""
  3465. if offset is None:
  3466. extra = ""
  3467. else:
  3468. extra = "<%d.%d>" % (offset, length)
  3469. fetch = uid and b"UID FETCH" or b"FETCH"
  3470. cmd = fmt % (messages, peek and ".PEEK" or "", number, header, payload, extra)
  3471. # APPEND components should be encoded as ASCII unless a
  3472. # charset identifier is provided. See #9201.
  3473. cmd = cmd.encode("charmap")
  3474. d = self.sendCommand(Command(fetch, cmd, wantResponse=(b"FETCH",)))
  3475. d.addCallback(self._cbFetch, (), False)
  3476. return d
  3477. def _fetch(self, messages, useUID=0, **terms):
  3478. messages = str(messages).encode("ascii")
  3479. fetch = useUID and b"UID FETCH" or b"FETCH"
  3480. if "rfc822text" in terms:
  3481. del terms["rfc822text"]
  3482. terms["rfc822.text"] = True
  3483. if "rfc822size" in terms:
  3484. del terms["rfc822size"]
  3485. terms["rfc822.size"] = True
  3486. if "rfc822header" in terms:
  3487. del terms["rfc822header"]
  3488. terms["rfc822.header"] = True
  3489. # The terms in 6.4.5 are all ASCII congruent, so wing it.
  3490. # Note that this isn't a public API, so terms in responses
  3491. # should not be decoded to native strings.
  3492. encodedTerms = [networkString(s) for s in terms]
  3493. cmd = messages + b" (" + b" ".join([s.upper() for s in encodedTerms]) + b")"
  3494. d = self.sendCommand(Command(fetch, cmd, wantResponse=(b"FETCH",)))
  3495. d.addCallback(self._cbFetch, [t.upper() for t in terms.keys()], True)
  3496. return d
  3497. def setFlags(self, messages, flags, silent=1, uid=0):
  3498. """
  3499. Set the flags for one or more messages.
  3500. This command is allowed in the Selected state.
  3501. @type messages: L{MessageSet} or L{str}
  3502. @param messages: A message sequence set
  3503. @type flags: Any iterable of L{str}
  3504. @param flags: The flags to set
  3505. @type silent: L{bool}
  3506. @param silent: If true, cause the server to suppress its verbose
  3507. response.
  3508. @type uid: L{bool}
  3509. @param uid: Indicates whether the message sequence set is of message
  3510. numbers or of unique message IDs.
  3511. @rtype: L{Deferred}
  3512. @return: A deferred whose callback is invoked with a list of the
  3513. server's responses (C{[]} if C{silent} is true) or whose
  3514. errback is invoked if there is an error.
  3515. """
  3516. return self._store(messages, b"FLAGS", silent, flags, uid)
  3517. def addFlags(self, messages, flags, silent=1, uid=0):
  3518. """
  3519. Add to the set flags for one or more messages.
  3520. This command is allowed in the Selected state.
  3521. @type messages: C{MessageSet} or L{str}
  3522. @param messages: A message sequence set
  3523. @type flags: Any iterable of L{str}
  3524. @param flags: The flags to set
  3525. @type silent: C{bool}
  3526. @param silent: If true, cause the server to suppress its verbose
  3527. response.
  3528. @type uid: C{bool}
  3529. @param uid: Indicates whether the message sequence set is of message
  3530. numbers or of unique message IDs.
  3531. @rtype: L{Deferred}
  3532. @return: A deferred whose callback is invoked with a list of the
  3533. server's responses (C{[]} if C{silent} is true) or whose
  3534. errback is invoked if there is an error.
  3535. """
  3536. return self._store(messages, b"+FLAGS", silent, flags, uid)
  3537. def removeFlags(self, messages, flags, silent=1, uid=0):
  3538. """
  3539. Remove from the set flags for one or more messages.
  3540. This command is allowed in the Selected state.
  3541. @type messages: L{MessageSet} or L{str}
  3542. @param messages: A message sequence set
  3543. @type flags: Any iterable of L{str}
  3544. @param flags: The flags to set
  3545. @type silent: L{bool}
  3546. @param silent: If true, cause the server to suppress its verbose
  3547. response.
  3548. @type uid: L{bool}
  3549. @param uid: Indicates whether the message sequence set is of message
  3550. numbers or of unique message IDs.
  3551. @rtype: L{Deferred}
  3552. @return: A deferred whose callback is invoked with a list of the
  3553. server's responses (C{[]} if C{silent} is true) or whose
  3554. errback is invoked if there is an error.
  3555. """
  3556. return self._store(messages, b"-FLAGS", silent, flags, uid)
  3557. def _store(self, messages, cmd, silent, flags, uid):
  3558. messages = str(messages).encode("ascii")
  3559. encodedFlags = [networkString(flag) for flag in flags]
  3560. if silent:
  3561. cmd = cmd + b".SILENT"
  3562. store = uid and b"UID STORE" or b"STORE"
  3563. args = b" ".join((messages, cmd, b"(" + b" ".join(encodedFlags) + b")"))
  3564. d = self.sendCommand(Command(store, args, wantResponse=(b"FETCH",)))
  3565. expected = ()
  3566. if not silent:
  3567. expected = ("FLAGS",)
  3568. d.addCallback(self._cbFetch, expected, True)
  3569. return d
  3570. def copy(self, messages, mailbox, uid):
  3571. """
  3572. Copy the specified messages to the specified mailbox.
  3573. This command is allowed in the Selected state.
  3574. @type messages: L{MessageSet} or L{str}
  3575. @param messages: A message sequence set
  3576. @type mailbox: L{str}
  3577. @param mailbox: The mailbox to which to copy the messages
  3578. @type uid: C{bool}
  3579. @param uid: If true, the C{messages} refers to message UIDs, rather
  3580. than message sequence numbers.
  3581. @rtype: L{Deferred}
  3582. @return: A deferred whose callback is invoked with a true value
  3583. when the copy is successful, or whose errback is invoked if there
  3584. is an error.
  3585. """
  3586. messages = str(messages).encode("ascii")
  3587. if uid:
  3588. cmd = b"UID COPY"
  3589. else:
  3590. cmd = b"COPY"
  3591. args = b" ".join([messages, _prepareMailboxName(mailbox)])
  3592. return self.sendCommand(Command(cmd, args))
  3593. #
  3594. # IMailboxListener methods
  3595. #
  3596. def modeChanged(self, writeable):
  3597. """Override me"""
  3598. def flagsChanged(self, newFlags):
  3599. """Override me"""
  3600. def newMessages(self, exists, recent):
  3601. """Override me"""
  3602. def parseIdList(s, lastMessageId=None):
  3603. """
  3604. Parse a message set search key into a C{MessageSet}.
  3605. @type s: L{bytes}
  3606. @param s: A string description of an id list, for example "1:3, 4:*"
  3607. @type lastMessageId: L{int}
  3608. @param lastMessageId: The last message sequence id or UID, depending on
  3609. whether we are parsing the list in UID or sequence id context. The
  3610. caller should pass in the correct value.
  3611. @rtype: C{MessageSet}
  3612. @return: A C{MessageSet} that contains the ids defined in the list
  3613. """
  3614. res = MessageSet()
  3615. parts = s.split(b",")
  3616. for p in parts:
  3617. if b":" in p:
  3618. low, high = p.split(b":", 1)
  3619. try:
  3620. if low == b"*":
  3621. low = None
  3622. else:
  3623. low = int(low)
  3624. if high == b"*":
  3625. high = None
  3626. else:
  3627. high = int(high)
  3628. if low is high is None:
  3629. # *:* does not make sense
  3630. raise IllegalIdentifierError(p)
  3631. # non-positive values are illegal according to RFC 3501
  3632. if (low is not None and low <= 0) or (high is not None and high <= 0):
  3633. raise IllegalIdentifierError(p)
  3634. # star means "highest value of an id in the mailbox"
  3635. high = high or lastMessageId
  3636. low = low or lastMessageId
  3637. res.add(low, high)
  3638. except ValueError:
  3639. raise IllegalIdentifierError(p)
  3640. else:
  3641. try:
  3642. if p == b"*":
  3643. p = None
  3644. else:
  3645. p = int(p)
  3646. if p is not None and p <= 0:
  3647. raise IllegalIdentifierError(p)
  3648. except ValueError:
  3649. raise IllegalIdentifierError(p)
  3650. else:
  3651. res.extend(p or lastMessageId)
  3652. return res
  3653. _SIMPLE_BOOL = (
  3654. "ALL",
  3655. "ANSWERED",
  3656. "DELETED",
  3657. "DRAFT",
  3658. "FLAGGED",
  3659. "NEW",
  3660. "OLD",
  3661. "RECENT",
  3662. "SEEN",
  3663. "UNANSWERED",
  3664. "UNDELETED",
  3665. "UNDRAFT",
  3666. "UNFLAGGED",
  3667. "UNSEEN",
  3668. )
  3669. _NO_QUOTES = ("LARGER", "SMALLER", "UID")
  3670. _sorted = sorted
  3671. def Query(sorted=0, **kwarg):
  3672. """
  3673. Create a query string
  3674. Among the accepted keywords are::
  3675. all : If set to a true value, search all messages in the
  3676. current mailbox
  3677. answered : If set to a true value, search messages flagged with
  3678. \\Answered
  3679. bcc : A substring to search the BCC header field for
  3680. before : Search messages with an internal date before this
  3681. value. The given date should be a string in the format
  3682. of 'DD-Mon-YYYY'. For example, '03-Mar-2003'.
  3683. body : A substring to search the body of the messages for
  3684. cc : A substring to search the CC header field for
  3685. deleted : If set to a true value, search messages flagged with
  3686. \\Deleted
  3687. draft : If set to a true value, search messages flagged with
  3688. \\Draft
  3689. flagged : If set to a true value, search messages flagged with
  3690. \\Flagged
  3691. from : A substring to search the From header field for
  3692. header : A two-tuple of a header name and substring to search
  3693. for in that header
  3694. keyword : Search for messages with the given keyword set
  3695. larger : Search for messages larger than this number of octets
  3696. messages : Search only the given message sequence set.
  3697. new : If set to a true value, search messages flagged with
  3698. \\Recent but not \\Seen
  3699. old : If set to a true value, search messages not flagged with
  3700. \\Recent
  3701. on : Search messages with an internal date which is on this
  3702. date. The given date should be a string in the format
  3703. of 'DD-Mon-YYYY'. For example, '03-Mar-2003'.
  3704. recent : If set to a true value, search for messages flagged with
  3705. \\Recent
  3706. seen : If set to a true value, search for messages flagged with
  3707. \\Seen
  3708. sentbefore : Search for messages with an RFC822 'Date' header before
  3709. this date. The given date should be a string in the format
  3710. of 'DD-Mon-YYYY'. For example, '03-Mar-2003'.
  3711. senton : Search for messages with an RFC822 'Date' header which is
  3712. on this date The given date should be a string in the format
  3713. of 'DD-Mon-YYYY'. For example, '03-Mar-2003'.
  3714. sentsince : Search for messages with an RFC822 'Date' header which is
  3715. after this date. The given date should be a string in the format
  3716. of 'DD-Mon-YYYY'. For example, '03-Mar-2003'.
  3717. since : Search for messages with an internal date that is after
  3718. this date.. The given date should be a string in the format
  3719. of 'DD-Mon-YYYY'. For example, '03-Mar-2003'.
  3720. smaller : Search for messages smaller than this number of octets
  3721. subject : A substring to search the 'subject' header for
  3722. text : A substring to search the entire message for
  3723. to : A substring to search the 'to' header for
  3724. uid : Search only the messages in the given message set
  3725. unanswered : If set to a true value, search for messages not
  3726. flagged with \\Answered
  3727. undeleted : If set to a true value, search for messages not
  3728. flagged with \\Deleted
  3729. undraft : If set to a true value, search for messages not
  3730. flagged with \\Draft
  3731. unflagged : If set to a true value, search for messages not
  3732. flagged with \\Flagged
  3733. unkeyword : Search for messages without the given keyword set
  3734. unseen : If set to a true value, search for messages not
  3735. flagged with \\Seen
  3736. @type sorted: C{bool}
  3737. @param sorted: If true, the output will be sorted, alphabetically.
  3738. The standard does not require it, but it makes testing this function
  3739. easier. The default is zero, and this should be acceptable for any
  3740. application.
  3741. @rtype: L{str}
  3742. @return: The formatted query string
  3743. """
  3744. cmd = []
  3745. keys = kwarg.keys()
  3746. if sorted:
  3747. keys = _sorted(keys)
  3748. for k in keys:
  3749. v = kwarg[k]
  3750. k = k.upper()
  3751. if k in _SIMPLE_BOOL and v:
  3752. cmd.append(k)
  3753. elif k == "HEADER":
  3754. cmd.extend([k, str(v[0]), str(v[1])])
  3755. elif k == "KEYWORD" or k == "UNKEYWORD":
  3756. # Discard anything that does not fit into an "atom". Perhaps turn
  3757. # the case where this actually removes bytes from the value into a
  3758. # warning and then an error, eventually. See #6277.
  3759. v = _nonAtomRE.sub("", v)
  3760. cmd.extend([k, v])
  3761. elif k not in _NO_QUOTES:
  3762. if isinstance(v, MessageSet):
  3763. fmt = '"%s"'
  3764. elif isinstance(v, str):
  3765. fmt = '"%s"'
  3766. else:
  3767. fmt = '"%d"'
  3768. cmd.extend([k, fmt % (v,)])
  3769. elif isinstance(v, int):
  3770. cmd.extend([k, "%d" % (v,)])
  3771. else:
  3772. cmd.extend([k, f"{v}"])
  3773. if len(cmd) > 1:
  3774. return "(" + " ".join(cmd) + ")"
  3775. else:
  3776. return " ".join(cmd)
  3777. def Or(*args):
  3778. """
  3779. The disjunction of two or more queries
  3780. """
  3781. if len(args) < 2:
  3782. raise IllegalQueryError(args)
  3783. elif len(args) == 2:
  3784. return "(OR %s %s)" % args
  3785. else:
  3786. return f"(OR {args[0]} {Or(*args[1:])})"
  3787. def Not(query):
  3788. """The negation of a query"""
  3789. return f"(NOT {query})"
  3790. def wildcardToRegexp(wildcard, delim=None):
  3791. wildcard = wildcard.replace("*", "(?:.*?)")
  3792. if delim is None:
  3793. wildcard = wildcard.replace("%", "(?:.*?)")
  3794. else:
  3795. wildcard = wildcard.replace("%", "(?:(?:[^%s])*?)" % re.escape(delim))
  3796. return re.compile(wildcard, re.I)
  3797. def splitQuoted(s):
  3798. """
  3799. Split a string into whitespace delimited tokens
  3800. Tokens that would otherwise be separated but are surrounded by \"
  3801. remain as a single token. Any token that is not quoted and is
  3802. equal to \"NIL\" is tokenized as L{None}.
  3803. @type s: L{bytes}
  3804. @param s: The string to be split
  3805. @rtype: L{list} of L{bytes}
  3806. @return: A list of the resulting tokens
  3807. @raise MismatchedQuoting: Raised if an odd number of quotes are present
  3808. """
  3809. s = s.strip()
  3810. result = []
  3811. word = []
  3812. inQuote = inWord = False
  3813. qu = _matchingString('"', s)
  3814. esc = _matchingString("\x5c", s)
  3815. empty = _matchingString("", s)
  3816. nil = _matchingString("NIL", s)
  3817. for i, c in enumerate(iterbytes(s)):
  3818. if c == qu:
  3819. if i and s[i - 1 : i] == esc:
  3820. word.pop()
  3821. word.append(qu)
  3822. elif not inQuote:
  3823. inQuote = True
  3824. else:
  3825. inQuote = False
  3826. result.append(empty.join(word))
  3827. word = []
  3828. elif (
  3829. not inWord
  3830. and not inQuote
  3831. and c not in (qu + (string.whitespace.encode("ascii")))
  3832. ):
  3833. inWord = True
  3834. word.append(c)
  3835. elif inWord and not inQuote and c in string.whitespace.encode("ascii"):
  3836. w = empty.join(word)
  3837. if w == nil:
  3838. result.append(None)
  3839. else:
  3840. result.append(w)
  3841. word = []
  3842. inWord = False
  3843. elif inWord or inQuote:
  3844. word.append(c)
  3845. if inQuote:
  3846. raise MismatchedQuoting(s)
  3847. if inWord:
  3848. w = empty.join(word)
  3849. if w == nil:
  3850. result.append(None)
  3851. else:
  3852. result.append(w)
  3853. return result
  3854. def splitOn(sequence, predicate, transformers):
  3855. result = []
  3856. mode = predicate(sequence[0])
  3857. tmp = [sequence[0]]
  3858. for e in sequence[1:]:
  3859. p = predicate(e)
  3860. if p != mode:
  3861. result.extend(transformers[mode](tmp))
  3862. tmp = [e]
  3863. mode = p
  3864. else:
  3865. tmp.append(e)
  3866. result.extend(transformers[mode](tmp))
  3867. return result
  3868. def collapseStrings(results):
  3869. """
  3870. Turns a list of length-one strings and lists into a list of longer
  3871. strings and lists. For example,
  3872. ['a', 'b', ['c', 'd']] is returned as ['ab', ['cd']]
  3873. @type results: L{list} of L{bytes} and L{list}
  3874. @param results: The list to be collapsed
  3875. @rtype: L{list} of L{bytes} and L{list}
  3876. @return: A new list which is the collapsed form of C{results}
  3877. """
  3878. copy = []
  3879. begun = None
  3880. pred = lambda e: isinstance(e, tuple)
  3881. tran = {
  3882. 0: lambda e: splitQuoted(b"".join(e)),
  3883. 1: lambda e: [b"".join([i[0] for i in e])],
  3884. }
  3885. for i, c in enumerate(results):
  3886. if isinstance(c, list):
  3887. if begun is not None:
  3888. copy.extend(splitOn(results[begun:i], pred, tran))
  3889. begun = None
  3890. copy.append(collapseStrings(c))
  3891. elif begun is None:
  3892. begun = i
  3893. if begun is not None:
  3894. copy.extend(splitOn(results[begun:], pred, tran))
  3895. return copy
  3896. def parseNestedParens(s, handleLiteral=1):
  3897. """
  3898. Parse an s-exp-like string into a more useful data structure.
  3899. @type s: L{bytes}
  3900. @param s: The s-exp-like string to parse
  3901. @rtype: L{list} of L{bytes} and L{list}
  3902. @return: A list containing the tokens present in the input.
  3903. @raise MismatchedNesting: Raised if the number or placement
  3904. of opening or closing parenthesis is invalid.
  3905. """
  3906. s = s.strip()
  3907. inQuote = 0
  3908. contentStack = [[]]
  3909. try:
  3910. i = 0
  3911. L = len(s)
  3912. while i < L:
  3913. c = s[i : i + 1]
  3914. if inQuote:
  3915. if c == b"\\":
  3916. contentStack[-1].append(s[i : i + 2])
  3917. i += 2
  3918. continue
  3919. elif c == b'"':
  3920. inQuote = not inQuote
  3921. contentStack[-1].append(c)
  3922. i += 1
  3923. else:
  3924. if c == b'"':
  3925. contentStack[-1].append(c)
  3926. inQuote = not inQuote
  3927. i += 1
  3928. elif handleLiteral and c == b"{":
  3929. end = s.find(b"}", i)
  3930. if end == -1:
  3931. raise ValueError("Malformed literal")
  3932. literalSize = int(s[i + 1 : end])
  3933. contentStack[-1].append((s[end + 3 : end + 3 + literalSize],))
  3934. i = end + 3 + literalSize
  3935. elif c == b"(" or c == b"[":
  3936. contentStack.append([])
  3937. i += 1
  3938. elif c == b")" or c == b"]":
  3939. contentStack[-2].append(contentStack.pop())
  3940. i += 1
  3941. else:
  3942. contentStack[-1].append(c)
  3943. i += 1
  3944. except IndexError:
  3945. raise MismatchedNesting(s)
  3946. if len(contentStack) != 1:
  3947. raise MismatchedNesting(s)
  3948. return collapseStrings(contentStack[0])
  3949. def _quote(s):
  3950. qu = _matchingString('"', s)
  3951. esc = _matchingString("\x5c", s)
  3952. return qu + s.replace(esc, esc + esc).replace(qu, esc + qu) + qu
  3953. def _literal(s: bytes) -> bytes:
  3954. return b"{%d}\r\n%b" % (len(s), s)
  3955. class DontQuoteMe:
  3956. def __init__(self, value):
  3957. self.value = value
  3958. def __str__(self) -> str:
  3959. return str(self.value)
  3960. _ATOM_SPECIALS = b'(){ %*"'
  3961. def _needsQuote(s):
  3962. if s == b"":
  3963. return 1
  3964. for c in iterbytes(s):
  3965. if c < b"\x20" or c > b"\x7f":
  3966. return 1
  3967. if c in _ATOM_SPECIALS:
  3968. return 1
  3969. return 0
  3970. def _parseMbox(name):
  3971. if isinstance(name, str):
  3972. return name
  3973. try:
  3974. return name.decode("imap4-utf-7")
  3975. except BaseException:
  3976. log.err()
  3977. raise IllegalMailboxEncoding(name)
  3978. def _prepareMailboxName(name):
  3979. if not isinstance(name, str):
  3980. name = name.decode("charmap")
  3981. name = name.encode("imap4-utf-7")
  3982. if _needsQuote(name):
  3983. return _quote(name)
  3984. return name
  3985. def _needsLiteral(s):
  3986. # change this to "return 1" to wig out stupid clients
  3987. cr = _matchingString("\n", s)
  3988. lf = _matchingString("\r", s)
  3989. return cr in s or lf in s or len(s) > 1000
  3990. def collapseNestedLists(items):
  3991. """
  3992. Turn a nested list structure into an s-exp-like string.
  3993. Strings in C{items} will be sent as literals if they contain CR or LF,
  3994. otherwise they will be quoted. References to None in C{items} will be
  3995. translated to the atom NIL. Objects with a 'read' attribute will have
  3996. it called on them with no arguments and the returned string will be
  3997. inserted into the output as a literal. Integers will be converted to
  3998. strings and inserted into the output unquoted. Instances of
  3999. C{DontQuoteMe} will be converted to strings and inserted into the output
  4000. unquoted.
  4001. This function used to be much nicer, and only quote things that really
  4002. needed to be quoted (and C{DontQuoteMe} did not exist), however, many
  4003. broken IMAP4 clients were unable to deal with this level of sophistication,
  4004. forcing the current behavior to be adopted for practical reasons.
  4005. @type items: Any iterable
  4006. @rtype: L{str}
  4007. """
  4008. pieces = []
  4009. for i in items:
  4010. if isinstance(i, str):
  4011. # anything besides ASCII will have to wait for an RFC 5738
  4012. # implementation. See
  4013. # https://twistedmatrix.com/trac/ticket/9258
  4014. i = i.encode("ascii")
  4015. if i is None:
  4016. pieces.extend([b" ", b"NIL"])
  4017. elif isinstance(i, int):
  4018. pieces.extend([b" ", networkString(str(i))])
  4019. elif isinstance(i, DontQuoteMe):
  4020. pieces.extend([b" ", i.value])
  4021. elif isinstance(i, bytes):
  4022. # XXX warning
  4023. if _needsLiteral(i):
  4024. pieces.extend([b" ", b"{%d}" % (len(i),), IMAP4Server.delimiter, i])
  4025. else:
  4026. pieces.extend([b" ", _quote(i)])
  4027. elif hasattr(i, "read"):
  4028. d = i.read()
  4029. pieces.extend([b" ", b"{%d}" % (len(d),), IMAP4Server.delimiter, d])
  4030. else:
  4031. pieces.extend([b" ", b"(" + collapseNestedLists(i) + b")"])
  4032. return b"".join(pieces[1:])
  4033. @implementer(IAccount)
  4034. class MemoryAccountWithoutNamespaces:
  4035. mailboxes = None
  4036. subscriptions = None
  4037. top_id = 0
  4038. def __init__(self, name):
  4039. self.name = name
  4040. self.mailboxes = {}
  4041. self.subscriptions = []
  4042. def allocateID(self):
  4043. id = self.top_id
  4044. self.top_id += 1
  4045. return id
  4046. ##
  4047. ## IAccount
  4048. ##
  4049. def addMailbox(self, name, mbox=None):
  4050. name = _parseMbox(name.upper())
  4051. if name in self.mailboxes:
  4052. raise MailboxCollision(name)
  4053. if mbox is None:
  4054. mbox = self._emptyMailbox(name, self.allocateID())
  4055. self.mailboxes[name] = mbox
  4056. return 1
  4057. def create(self, pathspec):
  4058. paths = [path for path in pathspec.split("/") if path]
  4059. for accum in range(1, len(paths)):
  4060. try:
  4061. self.addMailbox("/".join(paths[:accum]))
  4062. except MailboxCollision:
  4063. pass
  4064. try:
  4065. self.addMailbox("/".join(paths))
  4066. except MailboxCollision:
  4067. if not pathspec.endswith("/"):
  4068. return False
  4069. return True
  4070. def _emptyMailbox(self, name, id):
  4071. raise NotImplementedError
  4072. def select(self, name, readwrite=1):
  4073. return self.mailboxes.get(_parseMbox(name.upper()))
  4074. def delete(self, name):
  4075. name = _parseMbox(name.upper())
  4076. # See if this mailbox exists at all
  4077. mbox = self.mailboxes.get(name)
  4078. if not mbox:
  4079. raise MailboxException("No such mailbox")
  4080. # See if this box is flagged \Noselect
  4081. if r"\Noselect" in mbox.getFlags():
  4082. # Check for hierarchically inferior mailboxes with this one
  4083. # as part of their root.
  4084. for others in self.mailboxes.keys():
  4085. if others != name and others.startswith(name):
  4086. raise MailboxException(
  4087. "Hierarchically inferior mailboxes exist and \\Noselect is set"
  4088. )
  4089. mbox.destroy()
  4090. # iff there are no hierarchically inferior names, we will
  4091. # delete it from our ken.
  4092. if len(self._inferiorNames(name)) > 1:
  4093. raise MailboxException(f'Name "{name}" has inferior hierarchical names')
  4094. del self.mailboxes[name]
  4095. def rename(self, oldname, newname):
  4096. oldname = _parseMbox(oldname.upper())
  4097. newname = _parseMbox(newname.upper())
  4098. if oldname not in self.mailboxes:
  4099. raise NoSuchMailbox(oldname)
  4100. inferiors = self._inferiorNames(oldname)
  4101. inferiors = [(o, o.replace(oldname, newname, 1)) for o in inferiors]
  4102. for (old, new) in inferiors:
  4103. if new in self.mailboxes:
  4104. raise MailboxCollision(new)
  4105. for (old, new) in inferiors:
  4106. self.mailboxes[new] = self.mailboxes[old]
  4107. del self.mailboxes[old]
  4108. def _inferiorNames(self, name):
  4109. inferiors = []
  4110. for infname in self.mailboxes.keys():
  4111. if infname.startswith(name):
  4112. inferiors.append(infname)
  4113. return inferiors
  4114. def isSubscribed(self, name):
  4115. return _parseMbox(name.upper()) in self.subscriptions
  4116. def subscribe(self, name):
  4117. name = _parseMbox(name.upper())
  4118. if name not in self.subscriptions:
  4119. self.subscriptions.append(name)
  4120. def unsubscribe(self, name):
  4121. name = _parseMbox(name.upper())
  4122. if name not in self.subscriptions:
  4123. raise MailboxException(f"Not currently subscribed to {name}")
  4124. self.subscriptions.remove(name)
  4125. def listMailboxes(self, ref, wildcard):
  4126. ref = self._inferiorNames(_parseMbox(ref.upper()))
  4127. wildcard = wildcardToRegexp(wildcard, "/")
  4128. return [(i, self.mailboxes[i]) for i in ref if wildcard.match(i)]
  4129. @implementer(INamespacePresenter)
  4130. class MemoryAccount(MemoryAccountWithoutNamespaces):
  4131. ##
  4132. ## INamespacePresenter
  4133. ##
  4134. def getPersonalNamespaces(self):
  4135. return [[b"", b"/"]]
  4136. def getSharedNamespaces(self):
  4137. return None
  4138. def getOtherNamespaces(self):
  4139. return None
  4140. def getUserNamespaces(self):
  4141. # INamespacePresenter.getUserNamespaces
  4142. return None
  4143. _statusRequestDict = {
  4144. "MESSAGES": "getMessageCount",
  4145. "RECENT": "getRecentCount",
  4146. "UIDNEXT": "getUIDNext",
  4147. "UIDVALIDITY": "getUIDValidity",
  4148. "UNSEEN": "getUnseenCount",
  4149. }
  4150. def statusRequestHelper(mbox, names):
  4151. r = {}
  4152. for n in names:
  4153. r[n] = getattr(mbox, _statusRequestDict[n.upper()])()
  4154. return r
  4155. def parseAddr(addr):
  4156. if addr is None:
  4157. return [
  4158. (None, None, None),
  4159. ]
  4160. addr = email.utils.getaddresses([addr])
  4161. return [[fn or None, None] + address.split("@") for fn, address in addr]
  4162. def getEnvelope(msg):
  4163. headers = msg.getHeaders(True)
  4164. date = headers.get("date")
  4165. subject = headers.get("subject")
  4166. from_ = headers.get("from")
  4167. sender = headers.get("sender", from_)
  4168. reply_to = headers.get("reply-to", from_)
  4169. to = headers.get("to")
  4170. cc = headers.get("cc")
  4171. bcc = headers.get("bcc")
  4172. in_reply_to = headers.get("in-reply-to")
  4173. mid = headers.get("message-id")
  4174. return (
  4175. date,
  4176. subject,
  4177. parseAddr(from_),
  4178. parseAddr(sender),
  4179. reply_to and parseAddr(reply_to),
  4180. to and parseAddr(to),
  4181. cc and parseAddr(cc),
  4182. bcc and parseAddr(bcc),
  4183. in_reply_to,
  4184. mid,
  4185. )
  4186. def getLineCount(msg):
  4187. # XXX - Super expensive, CACHE THIS VALUE FOR LATER RE-USE
  4188. # XXX - This must be the number of lines in the ENCODED version
  4189. lines = 0
  4190. for _ in msg.getBodyFile():
  4191. lines += 1
  4192. return lines
  4193. def unquote(s):
  4194. if s[0] == s[-1] == '"':
  4195. return s[1:-1]
  4196. return s
  4197. def _getContentType(msg):
  4198. """
  4199. Return a two-tuple of the main and subtype of the given message.
  4200. """
  4201. attrs = None
  4202. mm = msg.getHeaders(False, "content-type").get("content-type", "")
  4203. mm = "".join(mm.splitlines())
  4204. if mm:
  4205. mimetype = mm.split(";")
  4206. type = mimetype[0].split("/", 1)
  4207. if len(type) == 1:
  4208. major = type[0]
  4209. minor = None
  4210. else:
  4211. # length must be 2, because of split('/', 1)
  4212. major, minor = type
  4213. attrs = dict(x.strip().lower().split("=", 1) for x in mimetype[1:])
  4214. else:
  4215. major = minor = None
  4216. return major, minor, attrs
  4217. def _getMessageStructure(message):
  4218. """
  4219. Construct an appropriate type of message structure object for the given
  4220. message object.
  4221. @param message: A L{IMessagePart} provider
  4222. @return: A L{_MessageStructure} instance of the most specific type available
  4223. for the given message, determined by inspecting the MIME type of the
  4224. message.
  4225. """
  4226. main, subtype, attrs = _getContentType(message)
  4227. if main is not None:
  4228. main = main.lower()
  4229. if subtype is not None:
  4230. subtype = subtype.lower()
  4231. if main == "multipart":
  4232. return _MultipartMessageStructure(message, subtype, attrs)
  4233. elif (main, subtype) == ("message", "rfc822"):
  4234. return _RFC822MessageStructure(message, main, subtype, attrs)
  4235. elif main == "text":
  4236. return _TextMessageStructure(message, main, subtype, attrs)
  4237. else:
  4238. return _SinglepartMessageStructure(message, main, subtype, attrs)
  4239. class _MessageStructure:
  4240. """
  4241. L{_MessageStructure} is a helper base class for message structure classes
  4242. representing the structure of particular kinds of messages, as defined by
  4243. their MIME type.
  4244. """
  4245. def __init__(self, message, attrs):
  4246. """
  4247. @param message: An L{IMessagePart} provider which this structure object
  4248. reports on.
  4249. @param attrs: A C{dict} giving the parameters of the I{Content-Type}
  4250. header of the message.
  4251. """
  4252. self.message = message
  4253. self.attrs = attrs
  4254. def _disposition(self, disp):
  4255. """
  4256. Parse a I{Content-Disposition} header into a two-sequence of the
  4257. disposition and a flattened list of its parameters.
  4258. @return: L{None} if there is no disposition header value, a L{list} with
  4259. two elements otherwise.
  4260. """
  4261. if disp:
  4262. disp = disp.split("; ")
  4263. if len(disp) == 1:
  4264. disp = (disp[0].lower(), None)
  4265. elif len(disp) > 1:
  4266. # XXX Poorly tested parser
  4267. params = [x for param in disp[1:] for x in param.split("=", 1)]
  4268. disp = [disp[0].lower(), params]
  4269. return disp
  4270. else:
  4271. return None
  4272. def _unquotedAttrs(self):
  4273. """
  4274. @return: The I{Content-Type} parameters, unquoted, as a flat list with
  4275. each Nth element giving a parameter name and N+1th element giving
  4276. the corresponding parameter value.
  4277. """
  4278. if self.attrs:
  4279. unquoted = [(k, unquote(v)) for (k, v) in self.attrs.items()]
  4280. return [y for x in sorted(unquoted) for y in x]
  4281. return None
  4282. class _SinglepartMessageStructure(_MessageStructure):
  4283. """
  4284. L{_SinglepartMessageStructure} represents the message structure of a
  4285. non-I{multipart/*} message.
  4286. """
  4287. _HEADERS = ["content-id", "content-description", "content-transfer-encoding"]
  4288. def __init__(self, message, main, subtype, attrs):
  4289. """
  4290. @param message: An L{IMessagePart} provider which this structure object
  4291. reports on.
  4292. @param main: A L{str} giving the main MIME type of the message (for
  4293. example, C{"text"}).
  4294. @param subtype: A L{str} giving the MIME subtype of the message (for
  4295. example, C{"plain"}).
  4296. @param attrs: A C{dict} giving the parameters of the I{Content-Type}
  4297. header of the message.
  4298. """
  4299. _MessageStructure.__init__(self, message, attrs)
  4300. self.main = main
  4301. self.subtype = subtype
  4302. self.attrs = attrs
  4303. def _basicFields(self):
  4304. """
  4305. Return a list of the basic fields for a single-part message.
  4306. """
  4307. headers = self.message.getHeaders(False, *self._HEADERS)
  4308. # Number of octets total
  4309. size = self.message.getSize()
  4310. major, minor = self.main, self.subtype
  4311. # content-type parameter list
  4312. unquotedAttrs = self._unquotedAttrs()
  4313. return [
  4314. major,
  4315. minor,
  4316. unquotedAttrs,
  4317. headers.get("content-id"),
  4318. headers.get("content-description"),
  4319. headers.get("content-transfer-encoding"),
  4320. size,
  4321. ]
  4322. def encode(self, extended):
  4323. """
  4324. Construct and return a list of the basic and extended fields for a
  4325. single-part message. The list suitable to be encoded into a BODY or
  4326. BODYSTRUCTURE response.
  4327. """
  4328. result = self._basicFields()
  4329. if extended:
  4330. result.extend(self._extended())
  4331. return result
  4332. def _extended(self):
  4333. """
  4334. The extension data of a non-multipart body part are in the
  4335. following order:
  4336. 1. body MD5
  4337. A string giving the body MD5 value as defined in [MD5].
  4338. 2. body disposition
  4339. A parenthesized list with the same content and function as
  4340. the body disposition for a multipart body part.
  4341. 3. body language
  4342. A string or parenthesized list giving the body language
  4343. value as defined in [LANGUAGE-TAGS].
  4344. 4. body location
  4345. A string list giving the body content URI as defined in
  4346. [LOCATION].
  4347. """
  4348. result = []
  4349. headers = self.message.getHeaders(
  4350. False,
  4351. "content-md5",
  4352. "content-disposition",
  4353. "content-language",
  4354. "content-language",
  4355. )
  4356. result.append(headers.get("content-md5"))
  4357. result.append(self._disposition(headers.get("content-disposition")))
  4358. result.append(headers.get("content-language"))
  4359. result.append(headers.get("content-location"))
  4360. return result
  4361. class _TextMessageStructure(_SinglepartMessageStructure):
  4362. """
  4363. L{_TextMessageStructure} represents the message structure of a I{text/*}
  4364. message.
  4365. """
  4366. def encode(self, extended):
  4367. """
  4368. A body type of type TEXT contains, immediately after the basic
  4369. fields, the size of the body in text lines. Note that this
  4370. size is the size in its content transfer encoding and not the
  4371. resulting size after any decoding.
  4372. """
  4373. result = _SinglepartMessageStructure._basicFields(self)
  4374. result.append(getLineCount(self.message))
  4375. if extended:
  4376. result.extend(self._extended())
  4377. return result
  4378. class _RFC822MessageStructure(_SinglepartMessageStructure):
  4379. """
  4380. L{_RFC822MessageStructure} represents the message structure of a
  4381. I{message/rfc822} message.
  4382. """
  4383. def encode(self, extended):
  4384. """
  4385. A body type of type MESSAGE and subtype RFC822 contains,
  4386. immediately after the basic fields, the envelope structure,
  4387. body structure, and size in text lines of the encapsulated
  4388. message.
  4389. """
  4390. result = _SinglepartMessageStructure.encode(self, extended)
  4391. contained = self.message.getSubPart(0)
  4392. result.append(getEnvelope(contained))
  4393. result.append(getBodyStructure(contained, False))
  4394. result.append(getLineCount(contained))
  4395. return result
  4396. class _MultipartMessageStructure(_MessageStructure):
  4397. """
  4398. L{_MultipartMessageStructure} represents the message structure of a
  4399. I{multipart/*} message.
  4400. """
  4401. def __init__(self, message, subtype, attrs):
  4402. """
  4403. @param message: An L{IMessagePart} provider which this structure object
  4404. reports on.
  4405. @param subtype: A L{str} giving the MIME subtype of the message (for
  4406. example, C{"plain"}).
  4407. @param attrs: A C{dict} giving the parameters of the I{Content-Type}
  4408. header of the message.
  4409. """
  4410. _MessageStructure.__init__(self, message, attrs)
  4411. self.subtype = subtype
  4412. def _getParts(self):
  4413. """
  4414. Return an iterator over all of the sub-messages of this message.
  4415. """
  4416. i = 0
  4417. while True:
  4418. try:
  4419. part = self.message.getSubPart(i)
  4420. except IndexError:
  4421. break
  4422. else:
  4423. yield part
  4424. i += 1
  4425. def encode(self, extended):
  4426. """
  4427. Encode each sub-message and added the additional I{multipart} fields.
  4428. """
  4429. result = [_getMessageStructure(p).encode(extended) for p in self._getParts()]
  4430. result.append(self.subtype)
  4431. if extended:
  4432. result.extend(self._extended())
  4433. return result
  4434. def _extended(self):
  4435. """
  4436. The extension data of a multipart body part are in the following order:
  4437. 1. body parameter parenthesized list
  4438. A parenthesized list of attribute/value pairs [e.g., ("foo"
  4439. "bar" "baz" "rag") where "bar" is the value of "foo", and
  4440. "rag" is the value of "baz"] as defined in [MIME-IMB].
  4441. 2. body disposition
  4442. A parenthesized list, consisting of a disposition type
  4443. string, followed by a parenthesized list of disposition
  4444. attribute/value pairs as defined in [DISPOSITION].
  4445. 3. body language
  4446. A string or parenthesized list giving the body language
  4447. value as defined in [LANGUAGE-TAGS].
  4448. 4. body location
  4449. A string list giving the body content URI as defined in
  4450. [LOCATION].
  4451. """
  4452. result = []
  4453. headers = self.message.getHeaders(
  4454. False, "content-language", "content-location", "content-disposition"
  4455. )
  4456. result.append(self._unquotedAttrs())
  4457. result.append(self._disposition(headers.get("content-disposition")))
  4458. result.append(headers.get("content-language", None))
  4459. result.append(headers.get("content-location", None))
  4460. return result
  4461. def getBodyStructure(msg, extended=False):
  4462. """
  4463. RFC 3501, 7.4.2, BODYSTRUCTURE::
  4464. A parenthesized list that describes the [MIME-IMB] body structure of a
  4465. message. This is computed by the server by parsing the [MIME-IMB] header
  4466. fields, defaulting various fields as necessary.
  4467. For example, a simple text message of 48 lines and 2279 octets can have
  4468. a body structure of: ("TEXT" "PLAIN" ("CHARSET" "US-ASCII") NIL NIL
  4469. "7BIT" 2279 48)
  4470. This is represented as::
  4471. ["TEXT", "PLAIN", ["CHARSET", "US-ASCII"], None, None, "7BIT", 2279, 48]
  4472. These basic fields are documented in the RFC as:
  4473. 1. body type
  4474. A string giving the content media type name as defined in
  4475. [MIME-IMB].
  4476. 2. body subtype
  4477. A string giving the content subtype name as defined in
  4478. [MIME-IMB].
  4479. 3. body parameter parenthesized list
  4480. A parenthesized list of attribute/value pairs [e.g., ("foo"
  4481. "bar" "baz" "rag") where "bar" is the value of "foo" and
  4482. "rag" is the value of "baz"] as defined in [MIME-IMB].
  4483. 4. body id
  4484. A string giving the content id as defined in [MIME-IMB].
  4485. 5. body description
  4486. A string giving the content description as defined in
  4487. [MIME-IMB].
  4488. 6. body encoding
  4489. A string giving the content transfer encoding as defined in
  4490. [MIME-IMB].
  4491. 7. body size
  4492. A number giving the size of the body in octets. Note that this size is
  4493. the size in its transfer encoding and not the resulting size after any
  4494. decoding.
  4495. Put another way, the body structure is a list of seven elements. The
  4496. semantics of the elements of this list are:
  4497. 1. Byte string giving the major MIME type
  4498. 2. Byte string giving the minor MIME type
  4499. 3. A list giving the Content-Type parameters of the message
  4500. 4. A byte string giving the content identifier for the message part, or
  4501. None if it has no content identifier.
  4502. 5. A byte string giving the content description for the message part, or
  4503. None if it has no content description.
  4504. 6. A byte string giving the Content-Encoding of the message body
  4505. 7. An integer giving the number of octets in the message body
  4506. The RFC goes on::
  4507. Multiple parts are indicated by parenthesis nesting. Instead of a body
  4508. type as the first element of the parenthesized list, there is a sequence
  4509. of one or more nested body structures. The second element of the
  4510. parenthesized list is the multipart subtype (mixed, digest, parallel,
  4511. alternative, etc.).
  4512. For example, a two part message consisting of a text and a
  4513. BASE64-encoded text attachment can have a body structure of: (("TEXT"
  4514. "PLAIN" ("CHARSET" "US-ASCII") NIL NIL "7BIT" 1152 23)("TEXT" "PLAIN"
  4515. ("CHARSET" "US-ASCII" "NAME" "cc.diff")
  4516. "<960723163407.20117h@cac.washington.edu>" "Compiler diff" "BASE64" 4554
  4517. 73) "MIXED")
  4518. This is represented as::
  4519. [["TEXT", "PLAIN", ["CHARSET", "US-ASCII"], None, None, "7BIT", 1152,
  4520. 23],
  4521. ["TEXT", "PLAIN", ["CHARSET", "US-ASCII", "NAME", "cc.diff"],
  4522. "<960723163407.20117h@cac.washington.edu>", "Compiler diff",
  4523. "BASE64", 4554, 73],
  4524. "MIXED"]
  4525. In other words, a list of N + 1 elements, where N is the number of parts in
  4526. the message. The first N elements are structures as defined by the previous
  4527. section. The last element is the minor MIME subtype of the multipart
  4528. message.
  4529. Additionally, the RFC describes extension data::
  4530. Extension data follows the multipart subtype. Extension data is never
  4531. returned with the BODY fetch, but can be returned with a BODYSTRUCTURE
  4532. fetch. Extension data, if present, MUST be in the defined order.
  4533. The C{extended} flag controls whether extension data might be returned with
  4534. the normal data.
  4535. """
  4536. return _getMessageStructure(msg).encode(extended)
  4537. def _formatHeaders(headers):
  4538. # TODO: This should use email.header.Header, which handles encoding
  4539. hdrs = [
  4540. ": ".join((k.title(), "\r\n".join(v.splitlines())))
  4541. for (k, v) in headers.items()
  4542. ]
  4543. hdrs = "\r\n".join(hdrs) + "\r\n"
  4544. return networkString(hdrs)
  4545. def subparts(m):
  4546. i = 0
  4547. try:
  4548. while True:
  4549. yield m.getSubPart(i)
  4550. i += 1
  4551. except IndexError:
  4552. pass
  4553. def iterateInReactor(i):
  4554. """
  4555. Consume an interator at most a single iteration per reactor iteration.
  4556. If the iterator produces a Deferred, the next iteration will not occur
  4557. until the Deferred fires, otherwise the next iteration will be taken
  4558. in the next reactor iteration.
  4559. @rtype: C{Deferred}
  4560. @return: A deferred which fires (with None) when the iterator is
  4561. exhausted or whose errback is called if there is an exception.
  4562. """
  4563. from twisted.internet import reactor
  4564. d = defer.Deferred()
  4565. def go(last):
  4566. try:
  4567. r = next(i)
  4568. except StopIteration:
  4569. d.callback(last)
  4570. except BaseException:
  4571. d.errback()
  4572. else:
  4573. if isinstance(r, defer.Deferred):
  4574. r.addCallback(go)
  4575. else:
  4576. reactor.callLater(0, go, r)
  4577. go(None)
  4578. return d
  4579. class MessageProducer:
  4580. CHUNK_SIZE = 2 ** 2 ** 2 ** 2
  4581. _uuid4 = staticmethod(uuid.uuid4)
  4582. def __init__(self, msg, buffer=None, scheduler=None):
  4583. """
  4584. Produce this message.
  4585. @param msg: The message I am to produce.
  4586. @type msg: L{IMessage}
  4587. @param buffer: A buffer to hold the message in. If None, I will
  4588. use a L{tempfile.TemporaryFile}.
  4589. @type buffer: file-like
  4590. """
  4591. self.msg = msg
  4592. if buffer is None:
  4593. buffer = tempfile.TemporaryFile()
  4594. self.buffer = buffer
  4595. if scheduler is None:
  4596. scheduler = iterateInReactor
  4597. self.scheduler = scheduler
  4598. self.write = self.buffer.write
  4599. def beginProducing(self, consumer):
  4600. self.consumer = consumer
  4601. return self.scheduler(self._produce())
  4602. def _produce(self):
  4603. headers = self.msg.getHeaders(True)
  4604. boundary = None
  4605. if self.msg.isMultipart():
  4606. content = headers.get("content-type")
  4607. parts = [x.split("=", 1) for x in content.split(";")[1:]]
  4608. parts = {k.lower().strip(): v for (k, v) in parts}
  4609. boundary = parts.get("boundary")
  4610. if boundary is None:
  4611. # Bastards
  4612. boundary = f"----={self._uuid4().hex}"
  4613. headers["content-type"] += f'; boundary="{boundary}"'
  4614. else:
  4615. if boundary.startswith('"') and boundary.endswith('"'):
  4616. boundary = boundary[1:-1]
  4617. boundary = networkString(boundary)
  4618. self.write(_formatHeaders(headers))
  4619. self.write(b"\r\n")
  4620. if self.msg.isMultipart():
  4621. for p in subparts(self.msg):
  4622. self.write(b"\r\n--" + boundary + b"\r\n")
  4623. yield MessageProducer(p, self.buffer, self.scheduler).beginProducing(
  4624. None
  4625. )
  4626. self.write(b"\r\n--" + boundary + b"--\r\n")
  4627. else:
  4628. f = self.msg.getBodyFile()
  4629. while True:
  4630. b = f.read(self.CHUNK_SIZE)
  4631. if b:
  4632. self.buffer.write(b)
  4633. yield None
  4634. else:
  4635. break
  4636. if self.consumer:
  4637. self.buffer.seek(0, 0)
  4638. yield FileProducer(self.buffer).beginProducing(self.consumer).addCallback(
  4639. lambda _: self
  4640. )
  4641. class _FetchParser:
  4642. class Envelope:
  4643. # Response should be a list of fields from the message:
  4644. # date, subject, from, sender, reply-to, to, cc, bcc, in-reply-to,
  4645. # and message-id.
  4646. #
  4647. # from, sender, reply-to, to, cc, and bcc are themselves lists of
  4648. # address information:
  4649. # personal name, source route, mailbox name, host name
  4650. #
  4651. # reply-to and sender must not be None. If not present in a message
  4652. # they should be defaulted to the value of the from field.
  4653. type = "envelope"
  4654. __str__ = lambda self: "envelope"
  4655. class Flags:
  4656. type = "flags"
  4657. __str__ = lambda self: "flags"
  4658. class InternalDate:
  4659. type = "internaldate"
  4660. __str__ = lambda self: "internaldate"
  4661. class RFC822Header:
  4662. type = "rfc822header"
  4663. __str__ = lambda self: "rfc822.header"
  4664. class RFC822Text:
  4665. type = "rfc822text"
  4666. __str__ = lambda self: "rfc822.text"
  4667. class RFC822Size:
  4668. type = "rfc822size"
  4669. __str__ = lambda self: "rfc822.size"
  4670. class RFC822:
  4671. type = "rfc822"
  4672. __str__ = lambda self: "rfc822"
  4673. class UID:
  4674. type = "uid"
  4675. __str__ = lambda self: "uid"
  4676. class Body:
  4677. type = "body"
  4678. peek = False
  4679. header = None
  4680. mime = None
  4681. text = None
  4682. part = ()
  4683. empty = False
  4684. partialBegin = None
  4685. partialLength = None
  4686. def __str__(self) -> str:
  4687. return self.__bytes__().decode("ascii")
  4688. def __bytes__(self) -> bytes:
  4689. base = b"BODY"
  4690. part = b""
  4691. separator = b""
  4692. if self.part:
  4693. part = b".".join([str(x + 1).encode("ascii") for x in self.part]) # type: ignore[unreachable]
  4694. separator = b"."
  4695. # if self.peek:
  4696. # base += '.PEEK'
  4697. if self.header:
  4698. base += ( # type: ignore[unreachable]
  4699. b"[" + part + separator + str(self.header).encode("ascii") + b"]"
  4700. )
  4701. elif self.text:
  4702. base += b"[" + part + separator + b"TEXT]" # type: ignore[unreachable]
  4703. elif self.mime:
  4704. base += b"[" + part + separator + b"MIME]" # type: ignore[unreachable]
  4705. elif self.empty:
  4706. base += b"[" + part + b"]"
  4707. if self.partialBegin is not None:
  4708. base += b"<%d.%d>" % (self.partialBegin, self.partialLength) # type: ignore[unreachable]
  4709. return base
  4710. class BodyStructure:
  4711. type = "bodystructure"
  4712. __str__ = lambda self: "bodystructure"
  4713. # These three aren't top-level, they don't need type indicators
  4714. class Header:
  4715. negate = False
  4716. fields = None
  4717. part = None
  4718. def __str__(self) -> str:
  4719. return self.__bytes__().decode("ascii")
  4720. def __bytes__(self) -> bytes:
  4721. base = b"HEADER"
  4722. if self.fields:
  4723. base += b".FIELDS" # type: ignore[unreachable]
  4724. if self.negate:
  4725. base += b".NOT"
  4726. fields = []
  4727. for f in self.fields:
  4728. f = f.title()
  4729. if _needsQuote(f):
  4730. f = _quote(f)
  4731. fields.append(f)
  4732. base += b" (" + b" ".join(fields) + b")"
  4733. if self.part:
  4734. # TODO: _FetchParser never assigns Header.part - dead
  4735. # code?
  4736. base = b".".join([(x + 1).__bytes__() for x in self.part]) + b"." + base # type: ignore[unreachable]
  4737. return base
  4738. class Text:
  4739. pass
  4740. class MIME:
  4741. pass
  4742. parts = None
  4743. _simple_fetch_att = [
  4744. (b"envelope", Envelope),
  4745. (b"flags", Flags),
  4746. (b"internaldate", InternalDate),
  4747. (b"rfc822.header", RFC822Header),
  4748. (b"rfc822.text", RFC822Text),
  4749. (b"rfc822.size", RFC822Size),
  4750. (b"rfc822", RFC822),
  4751. (b"uid", UID),
  4752. (b"bodystructure", BodyStructure),
  4753. ]
  4754. def __init__(self):
  4755. self.state = ["initial"]
  4756. self.result = []
  4757. self.remaining = b""
  4758. def parseString(self, s):
  4759. s = self.remaining + s
  4760. try:
  4761. while s or self.state:
  4762. if not self.state:
  4763. raise IllegalClientResponse("Invalid Argument")
  4764. # print 'Entering state_' + self.state[-1] + ' with', repr(s)
  4765. state = self.state.pop()
  4766. try:
  4767. used = getattr(self, "state_" + state)(s)
  4768. except BaseException:
  4769. self.state.append(state)
  4770. raise
  4771. else:
  4772. # print state, 'consumed', repr(s[:used])
  4773. s = s[used:]
  4774. finally:
  4775. self.remaining = s
  4776. def state_initial(self, s):
  4777. # In the initial state, the literals "ALL", "FULL", and "FAST"
  4778. # are accepted, as is a ( indicating the beginning of a fetch_att
  4779. # token, as is the beginning of a fetch_att token.
  4780. if s == b"":
  4781. return 0
  4782. l = s.lower()
  4783. if l.startswith(b"all"):
  4784. self.result.extend(
  4785. (self.Flags(), self.InternalDate(), self.RFC822Size(), self.Envelope())
  4786. )
  4787. return 3
  4788. if l.startswith(b"full"):
  4789. self.result.extend(
  4790. (
  4791. self.Flags(),
  4792. self.InternalDate(),
  4793. self.RFC822Size(),
  4794. self.Envelope(),
  4795. self.Body(),
  4796. )
  4797. )
  4798. return 4
  4799. if l.startswith(b"fast"):
  4800. self.result.extend(
  4801. (
  4802. self.Flags(),
  4803. self.InternalDate(),
  4804. self.RFC822Size(),
  4805. )
  4806. )
  4807. return 4
  4808. if l.startswith(b"("):
  4809. self.state.extend(("close_paren", "maybe_fetch_att", "fetch_att"))
  4810. return 1
  4811. self.state.append("fetch_att")
  4812. return 0
  4813. def state_close_paren(self, s):
  4814. if s.startswith(b")"):
  4815. return 1
  4816. # TODO: does maybe_fetch_att's startswith(b')') make this dead
  4817. # code?
  4818. raise Exception("Missing )")
  4819. def state_whitespace(self, s):
  4820. # Eat up all the leading whitespace
  4821. if not s or not s[0:1].isspace():
  4822. raise Exception("Whitespace expected, none found")
  4823. i = 0
  4824. for i in range(len(s)):
  4825. if not s[i : i + 1].isspace():
  4826. break
  4827. return i
  4828. def state_maybe_fetch_att(self, s):
  4829. if not s.startswith(b")"):
  4830. self.state.extend(("maybe_fetch_att", "fetch_att", "whitespace"))
  4831. return 0
  4832. def state_fetch_att(self, s):
  4833. # Allowed fetch_att tokens are "ENVELOPE", "FLAGS", "INTERNALDATE",
  4834. # "RFC822", "RFC822.HEADER", "RFC822.SIZE", "RFC822.TEXT", "BODY",
  4835. # "BODYSTRUCTURE", "UID",
  4836. # "BODY [".PEEK"] [<section>] ["<" <number> "." <nz_number> ">"]
  4837. l = s.lower()
  4838. for (name, cls) in self._simple_fetch_att:
  4839. if l.startswith(name):
  4840. self.result.append(cls())
  4841. return len(name)
  4842. b = self.Body()
  4843. if l.startswith(b"body.peek"):
  4844. b.peek = True
  4845. used = 9
  4846. elif l.startswith(b"body"):
  4847. used = 4
  4848. else:
  4849. raise Exception(f"Nothing recognized in fetch_att: {l}")
  4850. self.pending_body = b
  4851. self.state.extend(("got_body", "maybe_partial", "maybe_section"))
  4852. return used
  4853. def state_got_body(self, s):
  4854. self.result.append(self.pending_body)
  4855. del self.pending_body
  4856. return 0
  4857. def state_maybe_section(self, s):
  4858. if not s.startswith(b"["):
  4859. return 0
  4860. self.state.extend(("section", "part_number"))
  4861. return 1
  4862. _partExpr = re.compile(br"(\d+(?:\.\d+)*)\.?")
  4863. def state_part_number(self, s):
  4864. m = self._partExpr.match(s)
  4865. if m is not None:
  4866. self.parts = [int(p) - 1 for p in m.groups()[0].split(b".")]
  4867. return m.end()
  4868. else:
  4869. self.parts = []
  4870. return 0
  4871. def state_section(self, s):
  4872. # Grab "HEADER]" or "HEADER.FIELDS (Header list)]" or
  4873. # "HEADER.FIELDS.NOT (Header list)]" or "TEXT]" or "MIME]" or
  4874. # just "]".
  4875. l = s.lower()
  4876. used = 0
  4877. if l.startswith(b"]"):
  4878. self.pending_body.empty = True
  4879. used += 1
  4880. elif l.startswith(b"header]"):
  4881. h = self.pending_body.header = self.Header()
  4882. h.negate = True
  4883. h.fields = ()
  4884. used += 7
  4885. elif l.startswith(b"text]"):
  4886. self.pending_body.text = self.Text()
  4887. used += 5
  4888. elif l.startswith(b"mime]"):
  4889. self.pending_body.mime = self.MIME()
  4890. used += 5
  4891. else:
  4892. h = self.Header()
  4893. if l.startswith(b"header.fields.not"):
  4894. h.negate = True
  4895. used += 17
  4896. elif l.startswith(b"header.fields"):
  4897. used += 13
  4898. else:
  4899. raise Exception(f"Unhandled section contents: {l!r}")
  4900. self.pending_body.header = h
  4901. self.state.extend(("finish_section", "header_list", "whitespace"))
  4902. self.pending_body.part = tuple(self.parts)
  4903. self.parts = None
  4904. return used
  4905. def state_finish_section(self, s):
  4906. if not s.startswith(b"]"):
  4907. raise Exception("section must end with ]")
  4908. return 1
  4909. def state_header_list(self, s):
  4910. if not s.startswith(b"("):
  4911. raise Exception("Header list must begin with (")
  4912. end = s.find(b")")
  4913. if end == -1:
  4914. raise Exception("Header list must end with )")
  4915. headers = s[1:end].split()
  4916. self.pending_body.header.fields = [h.upper() for h in headers]
  4917. return end + 1
  4918. def state_maybe_partial(self, s):
  4919. # Grab <number.number> or nothing at all
  4920. if not s.startswith(b"<"):
  4921. return 0
  4922. end = s.find(b">")
  4923. if end == -1:
  4924. raise Exception("Found < but not >")
  4925. partial = s[1:end]
  4926. parts = partial.split(b".", 1)
  4927. if len(parts) != 2:
  4928. raise Exception(
  4929. "Partial specification did not include two .-delimited integers"
  4930. )
  4931. begin, length = map(int, parts)
  4932. self.pending_body.partialBegin = begin
  4933. self.pending_body.partialLength = length
  4934. return end + 1
  4935. class FileProducer:
  4936. CHUNK_SIZE = 2 ** 2 ** 2 ** 2
  4937. firstWrite = True
  4938. def __init__(self, f):
  4939. self.f = f
  4940. def beginProducing(self, consumer):
  4941. self.consumer = consumer
  4942. self.produce = consumer.write
  4943. d = self._onDone = defer.Deferred()
  4944. self.consumer.registerProducer(self, False)
  4945. return d
  4946. def resumeProducing(self):
  4947. b = b""
  4948. if self.firstWrite:
  4949. b = b"{%d}\r\n" % (self._size(),)
  4950. self.firstWrite = False
  4951. if not self.f:
  4952. return
  4953. b = b + self.f.read(self.CHUNK_SIZE)
  4954. if not b:
  4955. self.consumer.unregisterProducer()
  4956. self._onDone.callback(self)
  4957. self._onDone = self.f = self.consumer = None
  4958. else:
  4959. self.produce(b)
  4960. def pauseProducing(self):
  4961. """
  4962. Pause the producer. This does nothing.
  4963. """
  4964. def stopProducing(self):
  4965. """
  4966. Stop the producer. This does nothing.
  4967. """
  4968. def _size(self):
  4969. b = self.f.tell()
  4970. self.f.seek(0, 2)
  4971. e = self.f.tell()
  4972. self.f.seek(b, 0)
  4973. return e - b
  4974. def parseTime(s):
  4975. # XXX - This may require localization :(
  4976. months = [
  4977. "jan",
  4978. "feb",
  4979. "mar",
  4980. "apr",
  4981. "may",
  4982. "jun",
  4983. "jul",
  4984. "aug",
  4985. "sep",
  4986. "oct",
  4987. "nov",
  4988. "dec",
  4989. "january",
  4990. "february",
  4991. "march",
  4992. "april",
  4993. "may",
  4994. "june",
  4995. "july",
  4996. "august",
  4997. "september",
  4998. "october",
  4999. "november",
  5000. "december",
  5001. ]
  5002. expr = {
  5003. "day": r"(?P<day>3[0-1]|[1-2]\d|0[1-9]|[1-9]| [1-9])",
  5004. "mon": r"(?P<mon>\w+)",
  5005. "year": r"(?P<year>\d\d\d\d)",
  5006. }
  5007. m = re.match("%(day)s-%(mon)s-%(year)s" % expr, s)
  5008. if not m:
  5009. raise ValueError(f"Cannot parse time string {s!r}")
  5010. d = m.groupdict()
  5011. try:
  5012. d["mon"] = 1 + (months.index(d["mon"].lower()) % 12)
  5013. d["year"] = int(d["year"])
  5014. d["day"] = int(d["day"])
  5015. except ValueError:
  5016. raise ValueError(f"Cannot parse time string {s!r}")
  5017. else:
  5018. return time.struct_time((d["year"], d["mon"], d["day"], 0, 0, 0, -1, -1, -1))
  5019. # we need to cast Python >=3.3 memoryview to chars (from unsigned bytes), but
  5020. # cast is absent in previous versions: thus, the lambda returns the
  5021. # memoryview instance while ignoring the format
  5022. memory_cast = getattr(memoryview, "cast", lambda *x: x[0])
  5023. def modified_base64(s):
  5024. s_utf7 = s.encode("utf-7")
  5025. return s_utf7[1:-1].replace(b"/", b",")
  5026. def modified_unbase64(s):
  5027. s_utf7 = b"+" + s.replace(b",", b"/") + b"-"
  5028. return s_utf7.decode("utf-7")
  5029. def encoder(s, errors=None):
  5030. """
  5031. Encode the given C{unicode} string using the IMAP4 specific variation of
  5032. UTF-7.
  5033. @type s: C{unicode}
  5034. @param s: The text to encode.
  5035. @param errors: Policy for handling encoding errors. Currently ignored.
  5036. @return: L{tuple} of a L{str} giving the encoded bytes and an L{int}
  5037. giving the number of code units consumed from the input.
  5038. """
  5039. r = bytearray()
  5040. _in = []
  5041. valid_chars = set(map(chr, range(0x20, 0x7F))) - {"&"}
  5042. for c in s:
  5043. if c in valid_chars:
  5044. if _in:
  5045. r += b"&" + modified_base64("".join(_in)) + b"-"
  5046. del _in[:]
  5047. r.append(ord(c))
  5048. elif c == "&":
  5049. if _in:
  5050. r += b"&" + modified_base64("".join(_in)) + b"-"
  5051. del _in[:]
  5052. r += b"&-"
  5053. else:
  5054. _in.append(c)
  5055. if _in:
  5056. r.extend(b"&" + modified_base64("".join(_in)) + b"-")
  5057. return (bytes(r), len(s))
  5058. def decoder(s, errors=None):
  5059. """
  5060. Decode the given L{str} using the IMAP4 specific variation of UTF-7.
  5061. @type s: L{str}
  5062. @param s: The bytes to decode.
  5063. @param errors: Policy for handling decoding errors. Currently ignored.
  5064. @return: a L{tuple} of a C{unicode} string giving the text which was
  5065. decoded and an L{int} giving the number of bytes consumed from the
  5066. input.
  5067. """
  5068. r = []
  5069. decode = []
  5070. s = memory_cast(memoryview(s), "c")
  5071. for c in s:
  5072. if c == b"&" and not decode:
  5073. decode.append(b"&")
  5074. elif c == b"-" and decode:
  5075. if len(decode) == 1:
  5076. r.append("&")
  5077. else:
  5078. r.append(modified_unbase64(b"".join(decode[1:])))
  5079. decode = []
  5080. elif decode:
  5081. decode.append(c)
  5082. else:
  5083. r.append(c.decode())
  5084. if decode:
  5085. r.append(modified_unbase64(b"".join(decode[1:])))
  5086. return ("".join(r), len(s))
  5087. class StreamReader(codecs.StreamReader):
  5088. def decode(self, s, errors="strict"):
  5089. return decoder(s)
  5090. class StreamWriter(codecs.StreamWriter):
  5091. def encode(self, s, errors="strict"):
  5092. return encoder(s)
  5093. _codecInfo = codecs.CodecInfo(encoder, decoder, StreamReader, StreamWriter)
  5094. def imap4_utf_7(name):
  5095. # In Python 3.9, codecs.lookup() was changed to normalize the codec name
  5096. # in the same way as encodings.normalize_encoding(). The docstring
  5097. # for encodings.normalize_encoding() describes how the codec name is
  5098. # normalized. We need to replace '-' with '_' to be compatible with
  5099. # older Python versions.
  5100. # See: https://bugs.python.org/issue37751
  5101. # https://github.com/python/cpython/pull/17997
  5102. if name.replace("-", "_") == "imap4_utf_7":
  5103. return _codecInfo
  5104. codecs.register(imap4_utf_7)
  5105. __all__ = [
  5106. # Protocol classes
  5107. "IMAP4Server",
  5108. "IMAP4Client",
  5109. # Interfaces
  5110. "IMailboxListener",
  5111. "IClientAuthentication",
  5112. "IAccount",
  5113. "IMailbox",
  5114. "INamespacePresenter",
  5115. "ICloseableMailbox",
  5116. "IMailboxInfo",
  5117. "IMessage",
  5118. "IMessageCopier",
  5119. "IMessageFile",
  5120. "ISearchableMailbox",
  5121. "IMessagePart",
  5122. # Exceptions
  5123. "IMAP4Exception",
  5124. "IllegalClientResponse",
  5125. "IllegalOperation",
  5126. "IllegalMailboxEncoding",
  5127. "UnhandledResponse",
  5128. "NegativeResponse",
  5129. "NoSupportedAuthentication",
  5130. "IllegalServerResponse",
  5131. "IllegalIdentifierError",
  5132. "IllegalQueryError",
  5133. "MismatchedNesting",
  5134. "MismatchedQuoting",
  5135. "MailboxException",
  5136. "MailboxCollision",
  5137. "NoSuchMailbox",
  5138. "ReadOnlyMailbox",
  5139. # Auth objects
  5140. "CramMD5ClientAuthenticator",
  5141. "PLAINAuthenticator",
  5142. "LOGINAuthenticator",
  5143. "PLAINCredentials",
  5144. "LOGINCredentials",
  5145. # Simple query interface
  5146. "Query",
  5147. "Not",
  5148. "Or",
  5149. # Miscellaneous
  5150. "MemoryAccount",
  5151. "statusRequestHelper",
  5152. ]