Algorithm 版 (精华区)
发信人: Lerry (想不开·撞树), 信区: Algorithm
标 题: pmail.c
发信站: 哈工大紫丁香 (2002年06月09日21:30:28 星期天), 站内信件
/*
* NoseyParker, the search engine for FTP archives
* Copyright (C) 1993-96 by Jiri A. Randus
*
* 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 of the License, 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.
*
* The author can be reached as follows:
* Internet: <Jiri.Randus@vslib.cz>
* Phone: ++42 48 5227374
* SnailMail: Jiri Randus
* KIN HF TU v Liberci
* Halkova 6
* 46117 Liberec
* Czech Republic
*/
#define PMAIL_VERSION "2.3"
#include "parker.h"
/* This little fella was causing a lot of troubles both to NoseyParker
and EuNET so we will exclude him from using the service ;-) */
#define DENY_ACC "RSTA%platon@bb-prg.eunet.cz"
char ReplyTo[MAX];
char Line[MAX];
char Subject[MAX];
FILE *mailto;
char *mailline[]={ MAIL, ReplyTo, NULL};
char *GetFrom[]={"From: ", "Sender: ", "Reply-to:", NULL};
char *Ignore[]={"root", "Operator", "Postmaster", "mmdf", "parker", "Delivery",
"mail", "daemon", "SMTP", "nobody",
NULL};
int SearchType=0;
char *searchtype[]={ "substring", "wildcards", "egrep"};
struct comm { char *str; void (*fnc)(void); };
int quitok=0;
void Headers(void)
{
int fd[2];
pid_t child;
pipe(fd);
while((child=fork())==-1) sleep(1);
if(!child) {
setsid();
close(0);
dup(fd[0]);
close(fd[0]);
close(fd[1]);
execve(MAILPATH, mailline, NULL);
printf("Exec failed\n");
exit(1);
}
close(fd[0]);
if(!(mailto=fdopen(dup(fd[1]),"w"))) {perror("fdopen");exit(1);}
close(fd[1]);
fprintf(mailto,"%s\n",FROM_LINE);
fprintf(mailto,"To: %s\n",ReplyTo);
if(!*Subject) fprintf(mailto,"Subject: Answer to your search\n\n");
else fprintf(mailto,"%s\n\n",Subject);
}
void replytoset(void)
{
char *ptr;
if((ptr=strchr(Line,' '))!=NULL) {
while(*ptr==' ') ptr++;
strcpy(ReplyTo,ptr);
}
}
void substrset(void)
{
SearchType=0;
}
void wildset(void)
{
SearchType=1;
}
void egrepset(void)
{
SearchType=2;
}
void help(void)
{
FILE *help;
Headers();
if(!(help=fopen(MAILHLPTXT,"r"))) {
perror("Help fopen");
fprintf(mailto,"Cannot open MAILHLPTXT\n");
return;
}
while(1)
{
fgets(Line,MAX,help);
if(feof(help)) break;
fprintf(mailto,"%s",Line);
}
fclose(help);
fclose(mailto);
}
void dbstatus(void)
{
int fd[2];
pid_t child;
FILE *daemon;
Headers();
fprintf(mailto,"*** NoseyParker's Database Status ***\n");
pipe(fd);
fflush(mailto);
while((child=fork())==-1) sleep(1);
if(!child) {
close(fd[0]);
PStatus("status",fd[1]);
exit(0);
}
close(fd[1]);
if(!(daemon=fdopen(fd[0],"r"))) {
perror("fdopen in Search");
return;
}
while(1)
{
fgets(Line,MAX,daemon);
if(feof(daemon)) break;
fprintf(mailto,"%s",Line);
}
fclose(daemon);
fprintf(mailto,"*** End Of Status ***\n\n");
fclose(mailto);
}
void Search(char *p)
{
int fd[2];
FILE *daemon;
pid_t child;
fprintf(mailto,"*** %s search for `%s' ***\n",searchtype[SearchType],p);
pipe(fd);
fflush(mailto);
while((child=fork())==-1) sleep(1);
if(!child) {
close(fd[0]);
PQuery(searchtype[SearchType],p,fd[1],NULL,NULL,NULL);
exit(0);
}
close(fd[1]);
if(!(daemon=fdopen(fd[0],"r"))) {
perror("fdopen in Search");
return;
}
while(1)
{
fgets(Line,MAX,daemon);
if(feof(daemon)) break;
fprintf(mailto,"%s",Line);
}
fclose(daemon);
fprintf(mailto,"*** End Of Search ***\n\n");
}
void quit(void)
{
quitok=1;
}
struct comm Commands[]={"Reply-To", replytoset,
"Substring", substrset,
"Wildcards", wildset,
"Egrep", egrepset,
"Help", help,
"DB-Status", dbstatus,
"Quit", quit,
NULL, NULL
};
char *StrNStr(char *p1,char *p2)
{
int l;
l=strlen(p2);
while(*p1) if(!strncasecmp(p1,p2,l)) return(p1);
else p1++;
return(NULL);
}
void main(void)
{
char *ptr;
int i;
if(chdir(PARKER_HOME)) {
perror("chdir to PARKER_HOME");
exit(100);
}
nice(PMAIL_NICE);
while(1)
{
fgets(Line,MAX,stdin);
if((ptr=strchr(Line,CR))!=NULL) *ptr='\0';
if((ptr=strchr(Line,LF))!=NULL) *ptr='\0';
if(feof(stdin) || !strlen(Line)) break;
for(i=0;GetFrom[i];i++)
if(!strncasecmp(Line,GetFrom[i],strlen(GetFrom[i])))
strcpy(ReplyTo, &Line[strlen(GetFrom[i])]);
if(!strncasecmp(Line,SUBJ,strlen(SUBJ))) strcpy(Subject, Line);
}
if(!strlen(ReplyTo)) {printf("Can't find a sender\n"); exit(102);}
if(feof(stdin)) {printf("No text body\n"); exit(101);}
while(*ReplyTo==' ') strcpy(ReplyTo,&ReplyTo[1]);
for(i=0;Ignore[i];i++) if(StrNStr(ReplyTo,Ignore[i]))
{
Log("mailer: Ignoring mail from %s",ReplyTo,NULL);
return;
}
if(ptr=strchr(ReplyTo,'<')) {
strcpy(ReplyTo,ptr);
if(ptr=strchr(ReplyTo,'>')) *(ptr+1)='\0';
}
Log("mailer: Job from %s",ReplyTo,NULL);
/* Process the job */
while(1)
{
fgets(Line,MAX,stdin);
if((ptr=strchr(Line,CR))!=NULL) *ptr='\0';
if((ptr=strchr(Line,LF))!=NULL) *ptr='\0';
if(feof(stdin)) break;
if(!strlen(Line)) continue;
for(i=0;Commands[i].str;i++)
if(!strcasecmp(Line,Commands[i].str)) {
Commands[i].fnc();
break;
}
if(!Commands[i].str) {
Headers();
Search(Line);
fclose(mailto);
}
if(quitok) break;
}
}
--
当一个女孩儿觉得她不太容易了解那个男人的时候,她会爱他。
※ 来源:·哈工大紫丁香 bbs.hit.edu.cn·[FROM: 天外飞仙]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:3.480毫秒