16 #include "kmime_parsers.h"
20 using namespace KMime::Parser;
26 MultiPart::MultiPart(
const TQCString &src,
const TQCString &boundary)
33 bool MultiPart::parse()
35 TQCString b=
"--"+b_oundary, part;
36 int pos1=0, pos2=0, blen=b.length();
42 if( (pos1=s_rc.find(b.data(), pos1))==-1 || pos1==0 || s_rc[pos1-1]==
'\n' )
49 if(s_rc[pos1]==
'-' && s_rc[pos1+1]==
'-')
51 else if( (pos1-blen)>1 )
52 p_reamble=s_rc.left(pos1-blen);
56 while(pos1>-1 && pos2>-1) {
59 if( (pos1=s_rc.find(
'\n', pos1))>-1 ) {
63 if( (pos2=s_rc.find(b.data(), pos2))==-1 || s_rc[pos2-1]==
'\n' )
69 part=s_rc.mid(pos1, s_rc.length()-pos1);
75 part=s_rc.mid(pos1, pos2-pos1 - 1 );
78 if(s_rc[pos2]==
'-' && s_rc[pos2+1]==
'-') {
80 if( (pos1=s_rc.find(
'\n', pos1))>-1 )
81 e_pilouge=s_rc.mid(pos1+1, s_rc.length()-pos1-1);
92 return (!p_arts.isEmpty());
98 NonMimeParser::NonMimeParser(
const TQCString &src) :
99 s_rc(src), p_artNr(-1), t_otalNr(-1)
105 TQCString NonMimeParser::guessMimeType(
const TQCString& fileName)
107 TQCString tmp, mimeType;
110 if(!fileName.isEmpty()) {
111 pos=fileName.findRev(
'.');
113 tmp=fileName.mid(pos, fileName.length()-pos).upper();
114 if(tmp==
"JPG" || tmp==
"JPEG") mimeType=
"image/jpeg";
115 else if(tmp==
"GIF") mimeType=
"image/gif";
116 else if(tmp==
"PNG") mimeType=
"image/png";
117 else if(tmp==
"TIFF" || tmp==
"TIF") mimeType=
"image/tiff";
118 else if(tmp==
"XPM") mimeType=
"image/x-xpm";
119 else if(tmp==
"XBM") mimeType=
"image/x-xbm";
120 else if(tmp==
"BMP") mimeType=
"image/x-bmp";
121 else if(tmp==
"TXT" ||
126 tmp==
"CPP") mimeType=
"text/plain";
127 else if(tmp==
"HTML" || tmp==
"HTM") mimeType=
"text/html";
128 else mimeType=
"application/octet-stream";
130 else mimeType=
"application/octet-stream";
132 else mimeType=
"application/octet-stream";
140 UUEncoded::UUEncoded(
const TQCString &src,
const TQCString &subject) :
145 bool UUEncoded::parse()
148 bool success=
true, firstIteration=
true;
151 int beginPos=currentPos, uuStart=currentPos, endPos=0, lineCount=0, MCount=0, pos=0, len=0;
152 bool containsBegin=
false, containsEnd=
false;
153 TQCString tmp,fileName;
155 if( (beginPos=s_rc.find(TQRegExp(
"begin [0-9][0-9][0-9]"),currentPos))>-1 && (beginPos==0 || s_rc.at(beginPos-1)==
'\n') ) {
157 uuStart=s_rc.find(
'\n', beginPos);
164 else beginPos=currentPos;
166 if ( (endPos=s_rc.find(
"\nend",(uuStart>0)? uuStart-1:0))==-1 )
167 endPos=s_rc.length();
171 if ((containsBegin && containsEnd) || firstIteration) {
175 for(
int idx=uuStart; idx<endPos; idx++)
176 if(s_rc[idx]==
'\n') {
178 if(idx+1<endPos && s_rc[idx+1]==
'M') {
185 if( MCount==0 || (lineCount-MCount)>10 ||
186 ((!containsBegin || !containsEnd) && (MCount<15)) ) {
191 if( (!containsBegin || !containsEnd) && !s_ubject.isNull()) {
192 TQRegExp rx(
"[0-9]+/[0-9]+");
193 pos=rx.search(TQString(s_ubject), 0);
194 len=rx.matchedLength();
196 tmp=s_ubject.mid(pos, len);
198 p_artNr=tmp.left(pos).toInt();
199 t_otalNr=tmp.right(tmp.length()-pos-1).toInt();
208 t_ext.append(s_rc.mid(currentPos,beginPos-currentPos));
211 fileName = s_rc.mid(beginPos+10, uuStart-beginPos-11);
214 f_ilenames.append(fileName);
215 b_ins.append(s_rc.mid(uuStart, endPos-uuStart+1));
217 firstIteration=
false;
219 int next = s_rc.find(
'\n', endPos+1);
233 t_ext.append(s_rc.right(s_rc.length()-currentPos));
235 return ((b_ins.count()>0) || isPartial());
242 YENCEncoded::YENCEncoded(
const TQCString &src) :
247 bool YENCEncoded::yencMeta(TQCString& src,
const TQCString& name,
int* value)
250 TQCString sought=name +
"=";
252 int iPos=src.find( sought.data() );
254 int pos1=src.find(
' ', iPos);
255 int pos2=src.find(
'\r', iPos);
256 int pos3=src.find(
'\t', iPos);
257 int pos4=src.find(
'\n', iPos);
258 if (pos2>=0 && (pos1<0 || pos1>pos2))
260 if (pos3>=0 && (pos1<0 || pos1>pos3))
262 if (pos4>=0 && (pos1<0 || pos1>pos4))
264 iPos=src.findRev(
'=', pos1)+1;
266 char c=src.at( iPos);
267 if ( c>=
'0' && c<=
'9') {
269 *value=src.mid( iPos, pos1-iPos).toInt();
277 bool YENCEncoded::parse()
283 int beginPos=currentPos, yencStart=currentPos;
284 bool containsPart=
false;
285 TQCString fileName,mimeType;
287 if ((beginPos=s_rc.find(
"=ybegin ", currentPos))>-1 && ( beginPos==0 || s_rc.at( beginPos-1)==
'\n') ) {
288 yencStart=s_rc.find(
'\n', beginPos);
294 if (s_rc.find(
"=ypart", yencStart)==yencStart) {
296 yencStart=s_rc.find(
'\n', yencStart);
297 if ( yencStart== -1) {
307 TQCString meta=s_rc.mid(beginPos, yencStart-beginPos);
308 int namePos=meta.find(
"name=");
313 int eolPos=meta.find(
'\r', namePos);
315 eolPos=meta.find(
'\n', namePos);
320 fileName=meta.mid(namePos+5, eolPos-(namePos+5));
324 if (!yencMeta(meta,
"line", ¥cLine)) {
329 if (!yencMeta( meta,
"size", ¥cSize)) {
334 int partBegin, partEnd;
336 if (!yencMeta(meta,
"part", &p_artNr)) {
340 if (!yencMeta(meta,
"begin", &partBegin) || !
341 yencMeta(meta,
"end", &partEnd)) {
345 if (!yencMeta(meta,
"total", &t_otalNr))
347 if (yencSize==partEnd-partBegin+1)
349 yencSize=partEnd-partBegin+1;
355 int len=s_rc.length();
358 bool containsEnd=
false;
359 TQByteArray binary = TQByteArray(yencSize);
366 if (lineLength!=yencLine && totalSize!=yencSize)
383 if (lineStart && ch==
'y')
392 if (totalSize>=yencSize)
394 binary.at(totalSize++)=ch;
405 if (totalSize>=yencSize)
407 binary.at(totalSize++)=ch;
420 if (totalSize!=yencSize)
427 eolPos=s_rc.find(
'\n', pos);
433 meta=s_rc.mid(pos, eolPos-pos);
434 if (!yencMeta(meta,
"size", &totalSize))
439 if (totalSize!=yencSize)
445 f_ilenames.append(fileName);
447 b_ins.append(binary);
451 t_ext.append(s_rc.mid(currentPos,beginPos-currentPos));
452 currentPos = eolPos+1;
460 t_ext.append(s_rc.right(s_rc.length()-currentPos));
462 return b_ins.count()>0;
Helper-class: abstract base class of all parsers for non-mime binary data (uuencoded,...
static TQCString guessMimeType(const TQCString &fileName)
try to guess the mimetype from the file-extension