/* GNU POP3 - a small, fast, and efficient POP3 daemon Copyright (C) 1999 Jakob 'sparky' Kaivo This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include "gnu-pop3d.h" int pop3_uidl (const char *arg) { /* UIDL code contributed by Jeffrey Stedfast * * Note: Current implementation uses the message's "Message-Id" * field as the unique identifier. While this is compliant * with the RFCs, as RFC2449 stated, a hash of the message * might be a better way to do this. */ int mesg = 0, done = 0; char buffer[512], *begin, *end; if (state != TRANSACTION) return ERR_WRONG_STATE; if (strlen (arg) == 0) { fprintf(ofile, "+OK\r\n"); for (mesg = 0; mesg < num_messages; mesg++) { if (pop3_mesg_exist(mesg) == OK) { mbox = freopen (mailbox, "r", mbox); if (mbox == NULL) return ERR_FILE; fsetpos (mbox, &(messages[mesg].header)); done = 0; while (!done && !feof(mbox)) { memset(buffer, 0, sizeof(buffer)); fgets(buffer, sizeof(buffer)-1, mbox); if (!strncmp(buffer, "Message-Id:", 11) || !strncmp(buffer, "Message-ID:", 11)) done = 1; } if (done) { for (begin = buffer + 11; *begin && *begin != '<'; begin++); begin++; for (end = begin; *end && *end != '@' && *end != '>'; end++); *end = '\0'; fprintf(ofile, "%d %s\r\n", mesg + 1, begin); } else break; /* end of spool reached - this should not happen */ } } fprintf (ofile, ".\r\n"); } else { mesg = atoi(arg) - 1; if (pop3_mesg_exist(mesg) != OK) return ERR_NO_MESG; mbox = freopen (mailbox, "r", mbox); if (mbox == NULL) return ERR_FILE; fsetpos (mbox, &(messages[mesg].header)); done = 0; while (!done && !feof(mbox)) { memset(buffer, 0, sizeof(buffer)); fgets(buffer, sizeof(buffer)-1, mbox); if (!strncmp(buffer, "Message-Id:", 11) || !strncmp(buffer, "Message-ID:", 11)) done = 1; } if (done) { for (begin = buffer + 11; *begin && *begin != '<'; begin++); begin++; for (end = begin; *end && *end != '@' && *end != '>'; end++); *end = '\0'; fprintf(ofile, "+OK %d %s\r\n", mesg + 1, begin); } else /* end of spool reached - this should not happen */ fprintf(ofile, "-ERR UIDL could not be found\r\n"); } return OK; }