📄 alias.c
字号:
/*--------------------------------------------------------------------*/
/* A l i a s B y N i c k */
/* */
/* Locate a mail address by search the alias table. Returns TRUE */
/* if alias found and has address, otherwise FALSE. */
/*--------------------------------------------------------------------*/
char *AliasByNick(const char *nick)
{
int upper;
int lower;
if (!AliasCount)
AliasCount = LoadAliases();
upper = AliasCount - 1;
lower = 0;
while (upper >= lower)
{
int midpoint;
int hit;
midpoint = ( upper + lower ) / 2;
hit = stricmp(nick,alias[midpoint].anick);
if (!hit)
return alias[midpoint].afull;
if ( hit > 0 )
lower = midpoint + 1;
else
upper = midpoint - 1;
}
return NULL;
}
/*--------------------------------------------------------------------*/
/* A l i a s B y A d d r */
/* */
/* Locate a mail address by search the alias table. Returns TRUE */
/* if alias found and has address, otherwise FALSE */
/*--------------------------------------------------------------------*/
char *AliasByAddr(const char *node, const char *user)
{
size_t current = 0;
if (!AliasCount)
AliasCount = LoadAliases();
while (current < AliasCount)
{
int hit;
hit = stricmp(node,alias[current].anode);
if (!hit)
{
hit = stricmp(user,alias[current].auser);
if (!hit)
return alias[current].afull;
}
current++;
}
return NULL;
}
/*--------------------------------------------------------------------*/
/* L o a d A l i a s e s */
/* */
/* Initializes the address alias table; returns number of aliases */
/* loaded */
/*--------------------------------------------------------------------*/
size_t LoadAliases(void)
{
FILE *ff;
char buf[BUFSIZ];
char *token;
size_t elements = 0;
size_t max_elements = UserElements + 20;
size_t subscript;
struct AliasTable *hit; /* temporary pointer for searching */
struct AliasTable target;
checkuser( E_mailbox ); /* Force the table to be loaded */
alias = calloc(max_elements, sizeof(*alias));
checkref(alias);
/*--------------------------------------------------------------------*/
/* Actually load the alias table */
/*--------------------------------------------------------------------*/
if (E_aliases != NULL ) /* Did the user specify aliases file? */
{
char fname[FILENAME_MAX];
strcpy( fname, E_aliases);
expand_path( fname, E_homedir, E_homedir , NULL );
ff = FOPEN(fname , "r",TEXT_MODE);
if (ff == NULL)
{
printerr(fname);
return elements;
} /* if */
while (! feof(ff))
{
if (fgets(buf,BUFSIZ,ff) == NULL) /* Try to read a line */
break; /* Exit if end of file */
token = strtok(buf," \t\n");
if (token == NULL) /* Any data? */
continue; /* No --> read another line */
if (token[0] == '#')
continue; /* Line is a comment; loop again */
/* Add the alias to the table. Note that we must add the nick */
/* to the table ourselves (rather than use lsearch) because */
/* we must make a copy of the string; the *token we use for */
/* the search is in the middle of our I/O buffer! */
/*
/* I was burned, _you_ have been warned. */
target.anick = token;
hit = (void *) lfind(&target, alias, &elements , sizeof(alias[0]),
nickcmp);
if (hit == NULL)
{
char node[MAXADDR];
char user[MAXADDR];
char path[MAXADDR];
char addr[MAXADDR];
char *eos;
if (elements == max_elements)
{
max_elements = max_elements * 2;
alias = realloc(alias, max_elements * sizeof(*alias));
checkref(alias);
}
alias[elements].anick = newstr(token);
token = strtok(NULL,""); /* Get rest of string */
while ( strlen(token) && isspace(*token))
token++;
eos = token + strlen(token) - 1;
while ( strlen(token) && isspace(*eos))
{
*eos = '\0';
eos--;
}
alias[elements].afull = newstr(token);
ExtractAddress(addr,alias[elements].afull,FALSE);
user_at_node(addr,path,node,user);
alias[elements].anode = newstr(node);
alias[elements].auser = newstr(user);
elements += 1;
}
else
printmsg(0,"LoadAliases: Duplicate alias '%s' in table",token);
}
fclose(ff);
} /* if (E_aliases != NULL ) */
/*--------------------------------------------------------------------*/
/* Add the local users as final aliases in table */
/*--------------------------------------------------------------------*/
alias = realloc(alias, (elements + UserElements) * sizeof(*alias));
/* Resize table to final known size */
checkref(alias);
for ( subscript = 0; subscript < UserElements; subscript++)
{
if ( equal(users[subscript].realname,EMPTY_GCOS) )
continue;
alias[elements].anick = ""; /* No nickname, only good for addr */
if (bflag[F_BANG])
sprintf(buf, "(%s) %s!%s",
users[subscript].realname, E_fdomain,
users[subscript].uid);
else
sprintf(buf, "\"%s\" <%s@%s>", users[subscript].realname,
users[subscript].uid, E_fdomain );
alias[elements].afull = newstr(buf);
alias[elements].anode = E_nodename;
alias[elements].auser = users[subscript].uid;
elements++;
} /* for */
/*--------------------------------------------------------------------*/
/* Now sort the table */
/*--------------------------------------------------------------------*/
qsort(alias, elements ,sizeof(alias[0]) , nickcmp);
return (elements) ;
} /*LoadAliases*/
/*--------------------------------------------------------------------*/
/* n i c k c m p */
/* */
/* Accepts indirect pointers to two strings and compares them using */
/* stricmp (case insensitive string compare) */
/*--------------------------------------------------------------------*/
int nickcmp( const void *a, const void *b )
{
return stricmp(((struct AliasTable *)a)->anick,
((struct AliasTable *)b)->anick);
} /*nickcmp*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -