#include "config.h"
#include <sys/stat.h>
#include <errno.h>
#include <stdio.h>
#include <limits.h>
#if defined(STDC_HEADERS)
#include <stdlib.h>
#include <string.h>
#endif
#if defined(HAVE_UNISTD_H)
#include <unistd.h>
#endif
#include "fetchmail.h"
#include "i18n.h"
static struct idlist *scratchlist;
#ifdef POP3_ENABLE
void initialize_saved_lists(struct query *hostlist, const char *idfile)
{
struct stat statbuf;
FILE *tmpfp;
struct query *ctl;
for (ctl = hostlist; ctl; ctl = ctl->next)
ctl->skipped = ctl->oldsaved = ctl->newsaved = (struct idlist *)NULL;
errno = 0;
if (lstat(idfile, &statbuf) < 0) {
if (errno == ENOTDIR)
{
report(stderr, GT_("lstat: %s: %s\n"), idfile, strerror(errno));
exit(PS_IOERR);
}
}
if ((tmpfp = fopen(idfile, "r")) != (FILE *)NULL)
{
char buf[POPBUFSIZE+1];
char *host = NULL;
char *user;
char *id;
char *atsign;
char *delimp1;
char saveddelim1;
char *delimp2;
char saveddelim2 = '\0';
while (fgets(buf, POPBUFSIZE, tmpfp) != (char *)NULL)
{
user = buf + strspn(buf, " \t");
if ((id = strchr(user, ' ')) != NULL )
{
delimp1 = strchr(user, '@');
id = strchr(delimp1,' ');
for (delimp1 = id; delimp1 >= user; delimp1--)
if ((*delimp1 != ' ') && (*delimp1 != '\t'))
break;
id = id + strspn(id, " ");
delimp1++;
saveddelim1 = *delimp1;
*delimp1 = '\0';
if (id != NULL)
{
if ((delimp2 = strpbrk(id, " \t\n")) != NULL ) {
saveddelim2 = *delimp2;
*delimp2 = '\0';
}
atsign = strrchr(user, '@');
if (atsign) {
*atsign = '\0';
host = atsign + 1;
}
for (ctl = hostlist; ctl; ctl = ctl->next) {
if (strcasecmp(host, ctl->server.queryname) == 0
&& strcasecmp(user, ctl->remotename) == 0) {
save_str(&ctl->oldsaved, id, UID_SEEN);
break;
}
}
if (ctl == (struct query *)NULL) {
*delimp1 = saveddelim1;
*atsign = '@';
if (delimp2 != NULL) {
*delimp2 = saveddelim2;
}
save_str(&scratchlist, buf, UID_SEEN);
}
}
}
}
fclose(tmpfp);
}
if (outlevel >= O_DEBUG)
{
struct idlist *idp;
int uidlcount = 0;
for (ctl = hostlist; ctl; ctl = ctl->next)
if (ctl->server.uidl)
{
report_build(stdout, GT_("Old UID list from %s:"),
ctl->server.pollname);
for (idp = ctl->oldsaved; idp; idp = idp->next)
report_build(stdout, " %s", idp->id);
if (!idp)
report_build(stdout, GT_(" <empty>"));
report_complete(stdout, "\n");
uidlcount++;
}
if (uidlcount)
{
report_build(stdout, GT_("Scratch list of UIDs:"));
for (idp = scratchlist; idp; idp = idp->next)
report_build(stdout, " %s", idp->id);
if (!idp)
report_build(stdout, GT_(" <empty>"));
report_complete(stdout, "\n");
}
}
}
#endif
struct idlist *save_str(struct idlist **idl, const char *str, flag status)
{
struct idlist **end;
for (end = idl; *end; end = &(*end)->next)
continue;
*end = (struct idlist *)xmalloc(sizeof(struct idlist));
(*end)->val.status.mark = status;
(*end)->id = str ? xstrdup(str) : (char *)NULL;
(*end)->next = NULL;
return(*end);
}
void free_str_list(struct idlist **idl)
{
if (*idl == (struct idlist *)NULL)
return;
free_str_list(&(*idl)->next);
free ((*idl)->id);
free(*idl);
*idl = (struct idlist *)NULL;
}
void save_str_pair(struct idlist **idl, const char *str1, const char *str2)
{
struct idlist **end;
for (end = idl; *end; end = &(*end)->next)
continue;
*end = (struct idlist *)xmalloc(sizeof(struct idlist));
(*end)->id = str1 ? xstrdup(str1) : (char *)NULL;
if (str2)
(*end)->val.id2 = xstrdup(str2);
else
(*end)->val.id2 = (char *)NULL;
(*end)->next = (struct idlist *)NULL;
}
#ifdef __UNUSED__
void free_str_pair_list(struct idlist **idl)
{
if (*idl == (struct idlist *)NULL)
return;
free_idpair_list(&(*idl)->next);
free ((*idl)->id);
free ((*idl)->val.id2);
free(*idl);
*idl = (struct idlist *)NULL;
}
#endif
int str_in_list(struct idlist **idl, const char *str, const flag caseblind)
{
if (*idl == (struct idlist *)NULL || str == (char *) NULL)
return(0);
else if (!caseblind && strcmp(str, (*idl)->id) == 0)
return(1);
else if (caseblind && strcasecmp(str, (*idl)->id) == 0)
return(1);
else
return(str_in_list(&(*idl)->next, str, caseblind));
}
int str_nr_in_list( struct idlist **idl, const char *str )
{
int nr;
struct idlist *walk;
if ( !str )
return -1;
for( walk = *idl, nr = 0; walk; nr ++, walk = walk->next )
if( strcmp( str, walk->id) == 0 )
return nr;
return -1;
}
int str_nr_last_in_list( struct idlist **idl, const char *str)
{
int nr, ret = -1;
struct idlist *walk;
if ( !str )
return -1;
for( walk = *idl, nr = 0; walk; nr ++, walk = walk->next )
if( strcmp( str, walk->id) == 0 )
ret = nr;
return ret;
}
void str_set_mark( struct idlist **idl, const char *str, const flag val)
{
int nr;
struct idlist *walk;
if (!str)
return;
for(walk = *idl, nr = 0; walk; nr ++, walk = walk->next)
if (strcmp(str, walk->id) == 0)
walk->val.status.mark = val;
}
int count_list( struct idlist **idl)
{
if( !*idl )
return 0;
return 1 + count_list( &(*idl)->next );
}
char *str_from_nr_list(struct idlist **idl, int number)
{
if( !*idl || number < 0)
return 0;
if( number == 0 )
return (*idl)->id;
return str_from_nr_list(&(*idl)->next, number-1);
}
char *str_find(struct idlist **idl, int number)
{
if (*idl == (struct idlist *) 0)
return((char *) 0);
else if (number == (*idl)->val.status.num)
return((*idl)->id);
else
return(str_find(&(*idl)->next, number));
}
char *idpair_find(struct idlist **idl, const char *id)
{
if (*idl == (struct idlist *) 0)
return((char *) 0);
else if (strcasecmp(id, (*idl)->id) == 0)
return((*idl)->val.id2 ? (*idl)->val.id2 : (*idl)->id);
else
return(idpair_find(&(*idl)->next, id));
}
int delete_str(struct idlist **idl, int num)
{
struct idlist *idp;
for (idp = *idl; idp; idp = idp->next)
if (idp->val.status.num == num)
{
idp->val.status.mark = UID_DELETED;
return(1);
}
return(0);
}
struct idlist *copy_str_list(struct idlist *idl)
{
struct idlist *newnode ;
if (idl == (struct idlist *)NULL)
return(NULL);
else
{
newnode = (struct idlist *)xmalloc(sizeof(struct idlist));
memcpy(newnode, idl, sizeof(struct idlist));
newnode->next = copy_str_list(idl->next);
return(newnode);
}
}
void append_str_list(struct idlist **idl, struct idlist **nidl)
{
if ((*nidl) == (struct idlist *)NULL || *nidl == *idl)
return;
else if ((*idl) == (struct idlist *)NULL)
*idl = *nidl;
else if ((*idl)->next == (struct idlist *)NULL)
(*idl)->next = *nidl;
else if ((*idl)->next != *nidl)
append_str_list(&(*idl)->next, nidl);
}
#ifdef POP3_ENABLE
void expunge_uids(struct query *ctl)
{
struct idlist *idl;
for (idl = ctl->newsaved; idl; idl = idl->next)
if (idl->val.status.mark == UID_DELETED)
idl->val.status.mark = UID_EXPUNGED;
}
void uid_swap_lists(struct query *ctl)
{
if (ctl->server.uidl && outlevel >= O_DEBUG)
{
struct idlist *idp;
report_build(stdout, GT_("New UID list from %s:"), ctl->server.pollname);
for (idp = ctl->newsaved; idp; idp = idp->next)
report_build(stdout, " %s = %d", idp->id, idp->val.status.mark);
if (!idp)
report_build(stdout, GT_(" <empty>"));
report_complete(stdout, "\n");
}
if (ctl->newsaved)
{
if (outlevel >= O_DEBUG)
report(stdout, GT_("swapping UID lists\n"));
free_str_list(&ctl->oldsaved);
ctl->oldsaved = ctl->newsaved;
ctl->newsaved = (struct idlist *) NULL;
}
else if (outlevel >= O_DEBUG)
report(stdout, GT_("not swapping UID lists, no UIDs seen this query\n"));
}
void write_saved_lists(struct query *hostlist, const char *idfile)
{
int idcount;
FILE *tmpfp;
struct query *ctl;
struct idlist *idp;
idcount = 0;
for (ctl = hostlist; ctl; ctl = ctl->next) {
for (idp = ctl->oldsaved; idp; idp = idp->next)
if (idp->val.status.mark == UID_SEEN
|| idp->val.status.mark == UID_DELETED)
idcount++;
}
if (!idcount && !scratchlist)
{
if (outlevel >= O_DEBUG)
report(stdout, GT_("Deleting fetchids file.\n"));
unlink(idfile);
}
else
{
if (outlevel >= O_DEBUG)
report(stdout, GT_("Writing fetchids file.\n"));
if ((tmpfp = fopen(idfile, "w")) != (FILE *)NULL) {
for (ctl = hostlist; ctl; ctl = ctl->next) {
for (idp = ctl->oldsaved; idp; idp = idp->next)
if (idp->val.status.mark == UID_SEEN
|| idp->val.status.mark == UID_DELETED)
fprintf(tmpfp, "%s@%s %s\n",
ctl->remotename, ctl->server.queryname, idp->id);
}
for (idp = scratchlist; idp; idp = idp->next)
fputs(idp->id, tmpfp);
fclose(tmpfp);
}
}
}
#endif