+eNextState POP3_FetchNetworkUsetableEntry(AsyncIO *IO)
+{
+ long HKLen;
+ const char *HKey;
+ void *vData;
+ struct cdbdata *cdbut;
+ pop3aggr *RecvMsg = (pop3aggr *) IO->Data;
+
+ if(GetNextHashPos(RecvMsg->MsgNumbers, RecvMsg->Pos, &HKLen, &HKey, &vData))
+ {
+ struct UseTable ut;
+ if (server_shutting_down)
+ return eAbort;
+
+ RecvMsg->CurrMsg = (FetchItem*) vData;
+ syslog(LOG_DEBUG, "CHECKING: whether %s has already been seen: ", ChrPtr(RecvMsg->CurrMsg->MsgUID));
+ /* Find out if we've already seen this item */
+ safestrncpy(ut.ut_msgid,
+ ChrPtr(RecvMsg->CurrMsg->MsgUID),
+ sizeof(ut.ut_msgid));
+ ut.ut_timestamp = time(NULL);/// TODO: libev timestamp!
+
+ cdbut = cdb_fetch(CDB_USETABLE, SKEY(RecvMsg->CurrMsg->MsgUID));
+ if (cdbut != NULL) {
+ /* Item has already been seen */
+ syslog(LOG_DEBUG, "YES\n");
+ cdb_free(cdbut);
+
+ /* rewrite the record anyway, to update the timestamp */
+ cdb_store(CDB_USETABLE,
+ SKEY(RecvMsg->CurrMsg->MsgUID),
+ &ut, sizeof(struct UseTable) );
+ RecvMsg->CurrMsg->NeedFetch = 0; ////TODO0;
+ }
+ else
+ {
+ syslog(LOG_DEBUG, "NO\n");
+ RecvMsg->CurrMsg->NeedFetch = 1;
+ }
+ return NextDBOperation(&RecvMsg->IO, POP3_FetchNetworkUsetableEntry);
+ }
+ else
+ {
+ /* ok, now we know them all, continue with reading the actual messages. */
+ DeleteHashPos(&RecvMsg->Pos);
+
+ return QueueEventContext(IO, POP3_C_ReAttachToFetchMessages);
+ }
+}
+
+eNextState POP3C_GetOneMessagID(pop3aggr *RecvMsg)
+{
+ long HKLen;
+ const char *HKey;
+ void *vData;
+
+#if 0
+ int rc;
+ rc = TestValidateHash(RecvMsg->MsgNumbers);
+ if (rc != 0)
+ syslog(LOG_DEBUG, "Hash Invalid: %d\n", rc);
+#endif
+ if(GetNextHashPos(RecvMsg->MsgNumbers, RecvMsg->Pos, &HKLen, &HKey, &vData))
+ {
+ RecvMsg->CurrMsg = (FetchItem*) vData;
+ /* Find out the UIDL of the message, to determine whether we've already downloaded it */
+ StrBufPrintf(RecvMsg->IO.SendBuf.Buf,
+ "UIDL %ld\r\n", RecvMsg->CurrMsg->MSGID);
+ POP3C_DBG_SEND();
+ }
+ else
+ {
+ RecvMsg->State++;
+ DeleteHashPos(&RecvMsg->Pos);
+ /// done receiving uidls.. start looking them up now.
+ RecvMsg->Pos = GetNewHashPos(RecvMsg->MsgNumbers, 0);
+ return QueueDBOperation(&RecvMsg->IO, POP3_FetchNetworkUsetableEntry);
+ }
+ return eReadMore; /* TODO */
+}
+
+eNextState POP3C_GetOneMessageIDState(pop3aggr *RecvMsg)
+{
+#if 0
+ int rc;
+ rc = TestValidateHash(RecvMsg->MsgNumbers);
+ if (rc != 0)
+ syslog(LOG_DEBUG, "Hash Invalid: %d\n", rc);
+#endif
+
+ POP3C_DBG_READ();
+ if (!POP3C_OK) return eTerminateConnection;
+ RecvMsg->CurrMsg->MsgUIDL = NewStrBufPlain(NULL, StrLength(RecvMsg->IO.IOBuf));
+ RecvMsg->CurrMsg->MsgUID = NewStrBufPlain(NULL, StrLength(RecvMsg->IO.IOBuf) * 2);
+
+ StrBufExtract_token(RecvMsg->CurrMsg->MsgUIDL, RecvMsg->IO.IOBuf, 2, ' ');
+ StrBufPrintf(RecvMsg->CurrMsg->MsgUID,
+ "pop3/%s/%s:%s@%s",
+ ChrPtr(RecvMsg->RoomName),
+ ChrPtr(RecvMsg->CurrMsg->MsgUIDL),
+ RecvMsg->IO.ConnectMe->User,
+ RecvMsg->IO.ConnectMe->Host);
+ RecvMsg->State --;
+ return eSendReply;
+}
+
+
+eNextState POP3C_SendGetOneMsg(pop3aggr *RecvMsg)
+{
+ long HKLen;
+ const char *HKey;
+ void *vData;
+
+ RecvMsg->CurrMsg = NULL;
+ while (GetNextHashPos(RecvMsg->MsgNumbers, RecvMsg->Pos, &HKLen, &HKey, &vData) &&
+ (RecvMsg->CurrMsg = (FetchItem*) vData, RecvMsg->CurrMsg->NeedFetch == 0))
+ {}
+
+ if ((RecvMsg->CurrMsg != NULL ) && (RecvMsg->CurrMsg->NeedFetch == 1))
+ {
+ /* Message has not been seen. Tell the server to fetch the message... */
+ StrBufPrintf(RecvMsg->IO.SendBuf.Buf,
+ "RETR %ld\r\n", RecvMsg->CurrMsg->MSGID);
+ POP3C_DBG_SEND();
+ return eReadMessage;
+ }
+ else {
+ RecvMsg->State = ReadQuitState;
+ return POP3_C_DispatchWriteDone(&RecvMsg->IO);
+ }
+}
+
+
+eNextState POP3C_ReadMessageBodyFollowing(pop3aggr *RecvMsg)
+{
+ POP3C_DBG_READ();
+ if (!POP3C_OK) return eTerminateConnection;
+ RecvMsg->IO.ReadMsg = NewAsyncMsg(HKEY("."),
+ RecvMsg->CurrMsg->MSGSize,
+ config.c_maxmsglen,
+ NULL, -1,
+ 1);
+
+ return eReadPayload;
+}
+
+
+eNextState POP3C_StoreMsgRead(AsyncIO *IO)
+{
+ pop3aggr *RecvMsg = (pop3aggr *) IO->Data;
+ struct UseTable ut;
+
+ syslog(LOG_DEBUG, "MARKING: %s as seen: ", ChrPtr(RecvMsg->CurrMsg->MsgUID));
+
+ safestrncpy(ut.ut_msgid,
+ ChrPtr(RecvMsg->CurrMsg->MsgUID),
+ sizeof(ut.ut_msgid));
+ ut.ut_timestamp = time(NULL); /* TODO: use libev time */
+ cdb_store(CDB_USETABLE,
+ ChrPtr(RecvMsg->CurrMsg->MsgUID),
+ StrLength(RecvMsg->CurrMsg->MsgUID),
+ &ut,
+ sizeof(struct UseTable) );
+
+ return QueueEventContext(&RecvMsg->IO, POP3_C_ReAttachToFetchMessages);
+}
+eNextState POP3C_SaveMsg(AsyncIO *IO)
+{
+ long msgnum;
+ pop3aggr *RecvMsg = (pop3aggr *) IO->Data;
+
+ /* Do Something With It (tm) */
+ msgnum = CtdlSubmitMsg(RecvMsg->CurrMsg->Msg,
+ NULL,
+ ChrPtr(RecvMsg->RoomName),
+ 0);
+ if (msgnum > 0L) {
+ /* Message has been committed to the store */
+ /* write the uidl to the use table so we don't fetch this message again */
+ }
+ CtdlFreeMessage(RecvMsg->CurrMsg->Msg);
+
+ return NextDBOperation(&RecvMsg->IO, POP3C_StoreMsgRead);
+}
+
+eNextState POP3C_ReadMessageBody(pop3aggr *RecvMsg)
+{
+ syslog(LOG_DEBUG, "Converting message...\n");
+ RecvMsg->CurrMsg->Msg = convert_internet_message_buf(&RecvMsg->IO.ReadMsg->MsgBuf);
+
+ return QueueDBOperation(&RecvMsg->IO, POP3C_SaveMsg);
+}
+
+eNextState POP3C_SendDelete(pop3aggr *RecvMsg)
+{
+ if (!RecvMsg->keep) {
+ StrBufPrintf(RecvMsg->IO.SendBuf.Buf,
+ "DELE %ld\r\n", RecvMsg->CurrMsg->MSGID);
+ POP3C_DBG_SEND();
+ return eReadMessage;
+ }
+ else {
+ RecvMsg->State = ReadMessageBodyFollowing;
+ return POP3_C_DispatchWriteDone(&RecvMsg->IO);
+ }
+}
+eNextState POP3C_ReadDeleteState(pop3aggr *RecvMsg)
+{
+ POP3C_DBG_READ();
+ RecvMsg->State = GetOneMessageIDState;
+ return eReadMessage;
+}
+
+eNextState POP3C_SendQuit(pop3aggr *RecvMsg)
+{
+ /* Log out */
+ StrBufPlain(RecvMsg->IO.SendBuf.Buf,
+ HKEY("QUIT\r\n3)"));
+ POP3C_DBG_SEND();
+ return eReadMessage;
+}
+
+
+eNextState POP3C_ReadQuitState(pop3aggr *RecvMsg)
+{
+ POP3C_DBG_READ();
+ return eTerminateConnection;
+}
+
+const long POP3_C_ConnTimeout = 1000;
+const long DefaultPOP3Port = 110;
+
+Pop3ClientHandler POP3C_ReadHandlers[] = {
+ POP3C_ReadGreeting,
+ POP3C_GetUserState,
+ POP3C_GetPassState,
+ POP3C_GetListCommandState,
+ POP3C_GetListOneLine,
+ POP3C_GetOneMessageIDState,
+ POP3C_ReadMessageBodyFollowing,
+ POP3C_ReadMessageBody,
+ POP3C_ReadDeleteState,
+ POP3C_ReadQuitState,
+};
+
+const long POP3_C_SendTimeouts[POP3C_MaxRead] = {
+ 100,
+ 100,
+ 100,
+ 100,
+ 100,
+ 100,
+ 100,
+ 100
+};
+const ConstStr POP3C_ReadErrors[POP3C_MaxRead] = {
+ {HKEY("Connection broken during ")},
+ {HKEY("Connection broken during ")},
+ {HKEY("Connection broken during ")},
+ {HKEY("Connection broken during ")},
+ {HKEY("Connection broken during ")},
+ {HKEY("Connection broken during ")},
+ {HKEY("Connection broken during ")},
+ {HKEY("Connection broken during ")}
+};
+
+Pop3ClientHandler POP3C_SendHandlers[] = {
+ NULL, /* we don't send a greeting */
+ POP3C_SendUser,
+ POP3C_SendPassword,
+ POP3C_SendListCommand,
+ NULL,
+ POP3C_GetOneMessagID,
+ POP3C_SendGetOneMsg,
+ NULL,
+ POP3C_SendDelete,
+ POP3C_SendQuit
+};
+
+const long POP3_C_ReadTimeouts[] = {
+ 100,
+ 100,
+ 100,
+ 100,
+ 100,
+ 100,
+ 100,
+ 100,
+ 100,
+ 100
+};
+/*****************************************************************************/
+/* POP3 CLIENT DISPATCHER */
+/*****************************************************************************/
+
+void POP3SetTimeout(eNextState NextTCPState, pop3aggr *pMsg)
+{
+ double Timeout = 0.0;
+
+ syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
+
+ switch (NextTCPState) {
+ case eSendFile:
+ case eSendReply:
+ case eSendMore:
+ Timeout = POP3_C_SendTimeouts[pMsg->State];
+/*
+ if (pMsg->State == eDATABody) {
+ / * if we're sending a huge message, we need more time. * /
+ Timeout += StrLength(pMsg->msgtext) / 1024;
+ }
+*/
+ break;
+ case eReadFile:
+ case eReadMessage:
+ Timeout = POP3_C_ReadTimeouts[pMsg->State];
+/*
+ if (pMsg->State == eDATATerminateBody) {
+ / *
+ * some mailservers take a nap before accepting the message
+ * content inspection and such.
+ * /
+ Timeout += StrLength(pMsg->msgtext) / 1024;
+ }
+*/
+ break;
+ case eReadPayload:
+ Timeout = 100000;
+ /* TODO!!! */
+ break;
+ case eSendDNSQuery:
+ case eReadDNSReply:
+ case eConnect:
+ case eTerminateConnection:
+ case eDBQuery:
+ case eAbort:
+ case eReadMore://// TODO
+ return;
+ }
+ SetNextTimeout(&pMsg->IO, Timeout);
+}
+eNextState POP3_C_DispatchReadDone(AsyncIO *IO)
+{
+ syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
+ pop3aggr *pMsg = IO->Data;
+ eNextState rc;
+
+ rc = POP3C_ReadHandlers[pMsg->State](pMsg);
+ if (rc != eReadMore)
+ pMsg->State++;
+ POP3SetTimeout(rc, pMsg);
+ return rc;
+}
+eNextState POP3_C_DispatchWriteDone(AsyncIO *IO)
+{
+ syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
+ pop3aggr *pMsg = IO->Data;
+ eNextState rc;
+
+ rc = POP3C_SendHandlers[pMsg->State](pMsg);
+ POP3SetTimeout(rc, pMsg);
+ return rc;
+}
+
+
+/*****************************************************************************/
+/* POP3 CLIENT ERROR CATCHERS */
+/*****************************************************************************/
+eNextState POP3_C_Terminate(AsyncIO *IO)
+{
+/// pop3aggr *pMsg = (pop3aggr *)IO->Data;
+
+ syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
+ FinalizePOP3AggrRun(IO);
+ return eAbort;
+}
+eNextState POP3_C_Timeout(AsyncIO *IO)
+{
+ pop3aggr *pMsg = IO->Data;
+
+ syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
+ StrBufPlain(IO->ErrMsg, CKEY(POP3C_ReadErrors[pMsg->State]));
+ return FailAggregationRun(IO);
+}
+eNextState POP3_C_ConnFail(AsyncIO *IO)
+{
+ pop3aggr *pMsg = (pop3aggr *)IO->Data;
+
+ syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
+ StrBufPlain(IO->ErrMsg, CKEY(POP3C_ReadErrors[pMsg->State]));
+ return FailAggregationRun(IO);
+}
+eNextState POP3_C_DNSFail(AsyncIO *IO)
+{
+ pop3aggr *pMsg = (pop3aggr *)IO->Data;
+
+ syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
+ StrBufPlain(IO->ErrMsg, CKEY(POP3C_ReadErrors[pMsg->State]));
+ return FailAggregationRun(IO);
+}
+eNextState POP3_C_Shutdown(AsyncIO *IO)
+{
+ syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
+//// pop3aggr *pMsg = IO->Data;
+
+ ////pMsg->MyQEntry->Status = 3;
+ ///StrBufPlain(pMsg->MyQEntry->StatusMessage, HKEY("server shutdown during message retrieval."));
+ FinalizePOP3AggrRun(IO);
+ return eAbort;
+}
+
+
+/**
+ * @brief lineread Handler; understands when to read more POP3 lines, and when this is a one-lined reply.
+ */
+eReadState POP3_C_ReadServerStatus(AsyncIO *IO)
+{
+ eReadState Finished = eBufferNotEmpty;
+
+ switch (IO->NextState) {
+ case eSendDNSQuery:
+ case eReadDNSReply:
+ case eDBQuery:
+ case eConnect:
+ case eTerminateConnection:
+ case eAbort:
+ Finished = eReadFail;
+ break;
+ case eSendFile:
+ case eSendReply:
+ case eSendMore:
+ case eReadMore:
+ case eReadMessage:
+ Finished = StrBufChunkSipLine(IO->IOBuf, &IO->RecvBuf);
+ break;
+ case eReadFile:
+ case eReadPayload:
+ Finished = CtdlReadMessageBodyAsync(IO);
+ break;
+ }
+ return Finished;
+}
+
+/*****************************************************************************
+ * So we connect our Server IP here. *
+ *****************************************************************************/
+eNextState POP3_C_ReAttachToFetchMessages(AsyncIO *IO)
+{
+ pop3aggr *cpptr = IO->Data;
+
+ syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
+////??? cpptr->State ++;
+ if (cpptr->Pos == NULL)
+ cpptr->Pos = GetNewHashPos(cpptr->MsgNumbers, 0);
+
+ POP3_C_DispatchWriteDone(IO);
+ ReAttachIO(IO, cpptr, 0);
+ IO->NextState = eReadMessage;
+ return IO->NextState;
+}
+
+eNextState pop3_connect_ip(AsyncIO *IO)
+{
+ syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
+
+ return EvConnectSock(IO,
+ POP3_C_ConnTimeout,
+ POP3_C_ReadTimeouts[0],
+ 1);
+}
+
+eNextState pop3_get_one_host_ip_done(AsyncIO *IO)
+{
+ pop3aggr *cpptr = IO->Data;
+ struct hostent *hostent;
+
+ QueryCbDone(IO);
+
+ hostent = cpptr->HostLookup.VParsedDNSReply;
+ if ((cpptr->HostLookup.DNSStatus == ARES_SUCCESS) &&
+ (hostent != NULL) ) {
+ memset(&cpptr->IO.ConnectMe->Addr, 0, sizeof(struct in6_addr));
+ if (cpptr->IO.ConnectMe->IPv6) {
+ memcpy(&cpptr->IO.ConnectMe->Addr.sin6_addr.s6_addr,
+ &hostent->h_addr_list[0],
+ sizeof(struct in6_addr));
+
+ cpptr->IO.ConnectMe->Addr.sin6_family = hostent->h_addrtype;
+ cpptr->IO.ConnectMe->Addr.sin6_port = htons(DefaultPOP3Port);
+ }
+ else {
+ struct sockaddr_in *addr = (struct sockaddr_in*) &cpptr->IO.ConnectMe->Addr;
+ /* Bypass the ns lookup result like this: IO->Addr.sin_addr.s_addr = inet_addr("127.0.0.1"); */
+// addr->sin_addr.s_addr = htonl((uint32_t)&hostent->h_addr_list[0]);
+ memcpy(&addr->sin_addr.s_addr,
+ hostent->h_addr_list[0],
+ sizeof(uint32_t));
+
+ addr->sin_family = hostent->h_addrtype;
+ addr->sin_port = htons(DefaultPOP3Port);
+
+ }
+ return pop3_connect_ip(IO);
+ }
+ else
+ return eAbort;
+}
+
+eNextState pop3_get_one_host_ip(AsyncIO *IO)
+{
+ pop3aggr *cpptr = IO->Data;
+ /*
+ * here we start with the lookup of one host. it might be...
+ * - the relay host *sigh*
+ * - the direct hostname if there was no mx record
+ * - one of the mx'es
+ */
+
+ InitC_ares_dns(IO);
+
+ syslog(LOG_DEBUG, "POP3: %s\n", __FUNCTION__);
+
+ syslog(LOG_DEBUG,
+ "POP3 client[%ld]: looking up %s-Record %s : %d ...\n",
+ cpptr->n,
+ (cpptr->IO.ConnectMe->IPv6)? "aaaa": "a",
+ cpptr->IO.ConnectMe->Host,
+ cpptr->IO.ConnectMe->Port);
+
+ QueueQuery((cpptr->IO.ConnectMe->IPv6)? ns_t_aaaa : ns_t_a,
+ cpptr->IO.ConnectMe->Host,
+ &cpptr->IO,
+ &cpptr->HostLookup,
+ pop3_get_one_host_ip_done);
+ IO->NextState = eReadDNSReply;
+ return IO->NextState;
+}
+
+
+
+int pop3_do_fetching(pop3aggr *cpptr)
+{
+ InitIOStruct(&cpptr->IO,
+ cpptr,
+ eReadMessage,
+ POP3_C_ReadServerStatus,
+ POP3_C_DNSFail,
+ POP3_C_DispatchWriteDone,
+ POP3_C_DispatchReadDone,
+ POP3_C_Terminate,
+ POP3_C_ConnFail,
+ POP3_C_Timeout,
+ POP3_C_Shutdown);
+
+ safestrncpy(((CitContext *)cpptr->IO.CitContext)->cs_host,
+ ChrPtr(cpptr->Url),
+ sizeof(((CitContext *)cpptr->IO.CitContext)->cs_host));
+
+ if (cpptr->IO.ConnectMe->IsIP) {
+ QueueEventContext(&cpptr->IO,
+ pop3_connect_ip);
+ }
+ else {
+ QueueEventContext(&cpptr->IO,
+ pop3_get_one_host_ip);
+ }
+ return 1;
+}