⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 imapprotocol.cpp

📁 Qtopia下的邮件处理程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    }        incomingDataTimer.stop();    if (status == IMAP_Init) {	operationState = OpOk;	emit finished(status, operationState);	response = "";	read = 0;	return;    }    if (status == IMAP_Logout) {        socket->close();        _connected = false;        d->reset();	operationState = OpOk;	_name = "";	emit finished(status, operationState);	response = "";	read = 0;	return;    }    if ( (status == IMAP_UIDFetch) && (dataItems & F_RFC822)  )	emit downloadSize( mailDropSize );//    qDebug("imapincomingdata status = %i", status);    /* Applies to all functions below	*/    if ( !response.startsWith( commandId(requests.at(requests.count() - 1) ) ) ) {	response = "";	read = 0;        return;    }    if ( ( operationState = commandResponse(response)) != OpOk ) {	// The client decides whether the error is critical or not    qWarning( response.toAscii() );	_lastError = response;	if (status == IMAP_UIDSearch)	    _lastError += "\nThis server does not provide a complete IMAP4rev1 implementation.";	emit finished(status, operationState);	response = "";	read = 0;	return;    }    if (status == IMAP_Login) {	emit finished(status, operationState);	response = "";	read = 0;	return;    }    if (status == IMAP_List) {	for ( QString str = d->first(); str != QString::null; str = d->next() ) {	    if ( str.startsWith("* LIST") )		parseList( str.mid(7) );	}		emit finished(status, operationState);	response = "";	read = 0;	return;    }    if (status == IMAP_Select) {	parseSelect();		emit finished(status, operationState);	response = "";	read = 0;	return;    }    if (status == IMAP_UIDSearch) {        parseUid();	emit finished(status, operationState);	response = "";	read = 0;	return;    }    if ( status == IMAP_UIDFetch ) {	//temporary for now	if ( dataItems & F_RFC822_HEADER ) {	    parseFetch();	} else {	    parseFetchAll();	    emit finished(status, operationState);	    response = "";	    read = 0;	}	return;    }    if ( status == IMAP_UIDStore ) {	emit finished(status, operationState);	response = "";	read = 0;	return;    }    if ( status == IMAP_Expunge ) {	emit finished(status, operationState);	response = "";	read = 0;	return;    }/*    if (status == Exit) {	    socket->close();	    receiving = false;	    serverData.clear();	    emit updateStatus(tr("Communication finished"));//		if (unresolvedUidl.count() > 0 && !preview) {//			emit unresolvedUidlList(unresolvedUidl);//		}	    emit mailTransferred(0);	    if (errorList.count() > 0)		    emit failedList(errorList);    }*/    response = "";    read = 0;}QString ImapProtocol::newCommandId(){    QString id, out;    requestCount++;    id.setNum(requestCount);    out = "a";    out = out.leftJustified(4 - id.length(), '0');    out += id;    return out;}QString ImapProtocol::commandId(QString in){    int pos = in.indexOf(' ');    if (pos == -1)        return "";    return in.left(pos).trimmed();}OperationState ImapProtocol::commandResponse(QString in){    QString old = in;    int start = in.indexOf(' ');    start = in.indexOf(' ', start);    int stop = in.indexOf(' ', start + 1);    if (start == -1 || stop == -1) {      qWarning(("could not parse command response: " + in).toAscii());	return OpFailed;    }    in = in.mid(start, stop - start).trimmed().toUpper();    OperationState state = OpFailed;    if ( in == "OK")	state = OpOk;    if ( in == "NO")	state = OpNo;    if ( in == "BAD")	state = OpBad;        return state;}void ImapProtocol::parseList(QString in){    QString flags, name, delimiter;    int pos, index = 0;    bool noSelect;    flags = token(in, '(', ')', &index);    noSelect = ( flags.indexOf("noselect", 0, Qt::CaseInsensitive) != -1 );    if (noSelect)        return;    delimiter = token(in, ' ', ' ', &index);    pos = 0;    if ( token(delimiter, '"', '"', &pos) != QString::null ) {        pos = 0;        delimiter = token(delimiter, '"', '"', &pos);    }    index--;	//to point back to previous => () NIL "INBOX"    name = token(in, ' ', '\r', &index).trimmed();    pos = 0;    if ( token(name, '"', '"', &pos) != QString::null ) {        pos = 0;        name = token(name, '"', '"', &pos);    }    emit mailboxListed(flags, delimiter, name);}void ImapProtocol::parseSelect(){    int start;    bool result;    QString str, temp;    // reset all attributes in case some are not reported    _exists = 0;    _recent = 0;    _flags = "";    _mailboxUid = "";    for ( str = d->first(); str != QString::null; str = d->next() ) {	if ( str.indexOf("EXISTS", 0) != -1) {	    start = 0;	    temp = token(str, ' ', ' ', &start);	    _exists =  temp.toInt(&result);	    if ( !result )		_exists = 0;	} else if ( str.indexOf("RECENT", 0) != -1) {	    start = 0;	    temp = token(str, ' ', ' ', &start);	    _recent = temp.toInt(&result);	    if (!result)		_recent = 0;	} else if ( str.startsWith("* FLAGS") ) {	    start = 0;	    _flags = token(str, '(', ')', &start);	} else if ( str.indexOf("UIDVALIDITY", 0) != -1) {	    start = 0;	    temp = token(str, '[', ']', &start);	    _mailboxUid = temp.mid(12);	}    }}void ImapProtocol::parseFetch(){    int start, endMsg;    QString str, uid, msg, size;    MessageFlags flags = 0;        if ( firstParseFetch ) {	firstParseFetch = false;	str = d->first();    } else {	str = d->current();    }    if ( str != QString::null ) {	if ( str.startsWith("* ") ) {	    if (  (start = str.indexOf("UID", 0)) != -1) {		uid = token(str, ' ', ' ', &start);		//get flags as well	    }	    if (  (start = str.indexOf("RFC822.SIZE", 0)) != -1) {		size = token(str, ' ', ' ', &start);	    }		    if ( str.indexOf("\\Seen") > -1 )		flags |= MFlag_Seen;        if ( str.indexOf("\\Answered") > -1 )            flags |= MFlag_Answered;        if ( str.indexOf("\\Flagged") > -1 )            flags |= MFlag_Flagged;        if ( str.indexOf("\\Deleted") > -1 )            flags |= MFlag_Deleted;        if ( str.indexOf("\\Draft") > -1 )            flags |= MFlag_Draft;        if ( str.indexOf("\\Recent") > -1 )            flags |= MFlag_Recent;		    msg = "";	    str = d->next();	    while ( !str.startsWith("* ") && str != QString::null ) {		msg += str;		str = d->next();	    }		    if ( str.toUpper().startsWith("* NO") ) {        qWarning(("fetch failed: " + str).toAscii());		flags = 0;	    } else {		endMsg = msg.indexOf("\r\n\r\n");		if (endMsg != -1)		    msg.truncate( (uint) endMsg);		//to work with pop standard		endMsg = msg.indexOf("\r\n.\r\n", -5);		if (endMsg == -1)		    msg += "\r\n.\r\n";		emit messageFetched(msg, uid, size.toInt(), flags);		flags = 0;		// don't freeze up the pda when we're very busy		parseFetchTimer.start(0);		return;	    }	} else {	    str = d->next();	}    } else {	firstParseFetch = true;	parseFetchTimer.stop();	ImapCommand status = IMAP_UIDFetch;	emit finished(status, operationState);	response = "";	read = 0;    }}void ImapProtocol::parseFetchAll(){    int start, msgSize=0;    QString uid, msg, size;    bool result;    MessageFlags flags = 0;    QString str = d->first();    if ( str == QString::null ) {	qWarning("not a valid message");	return;    }    if ( str.startsWith("* ") ) {	//get flags as well	if (  (start = str.indexOf("UID", 0)) != -1) {	    uid = token(str, ' ', ' ', &start);	}	if (  (start = str.indexOf("RFC822.SIZE", 0)) != -1) {	    size = token(str, ' ', ' ', &start);	    msgSize = size.toUInt(&result);	    if ( !result)		msgSize = 0;	}      if ( str.indexOf("\\Seen") > -1 )              flags |= MFlag_Seen;      if ( str.indexOf("\\Answered") > -1 )              flags |= MFlag_Answered;      if ( str.indexOf("\\Flagged") > -1 )              flags |= MFlag_Flagged;      if ( str.indexOf("\\Deleted") > -1 )              flags |= MFlag_Deleted;      if ( str.indexOf("\\Draft") > -1 )              flags |= MFlag_Draft;      if ( str.indexOf("\\Recent") > -1 )	    flags |= MFlag_Recent;    }    str = d->next();    while ( str != QString::null ) {	msg += str;	str = d->next();    }    // remove "junk" lines:   )\r\n* Ax FETCH ...    int pos = msg.lastIndexOf("\r\n)\r\n", -1);    if ( pos > 0 )	msg.truncate( pos + 2 );	    msg += "\r\n.\r\n";	//to work with pop    emit messageFetched(msg, uid, msgSize, flags);}void ImapProtocol::parseUid(){    int index;    QString str, temp;    uidList.clear();    for ( str = d->first(); str != QString::null; str = d->next() ) {	if ( str.startsWith("* SEARCH") ) {	    index = 7;	    while ( (temp = token(str, ' ', ' ', &index)) != QString::null) {		uidList.append(temp);		index--;	    }	    temp = token(str, ' ', '\r', &index);	    if ( temp != QString::null )		uidList.append(temp);	}    }}QString ImapProtocol::token(QString str, QChar c1, QChar c2, int *index){    int start, stop;    start = str.indexOf(c1, *index, Qt::CaseInsensitive);    if (start == -1)	return QString::null;    stop = str.indexOf(c2, ++start, Qt::CaseInsensitive);    if (stop == -1)	return QString::null;    *index = stop + 1;    return str.mid(start, stop - start);}/* Adds "" to mailbox names with spaces and not to mailboxes without.   Some IMAP servers require this methology.  */QString ImapProtocol::quoteString(QString name){    if ( name.indexOf(' ', 0) != -1 )	return "\"" + name + "\"";    return name;}QString ImapProtocol::unquoteString(QString name){    QString str = name.simplified();    if ( str[0] == '\"' )	str = str.right( str.length() - 1 );    if ( str[(int)str.length() - 1] == '\"' )        str = str.left( str.length() - 1 );    return str.trimmed();}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -