home *** CD-ROM | disk | FTP | other *** search
- /*
- * $Filename: main.c $
- * $Revision: 2.8 $
- * $Date: 1994/01/17 15:05:04 $
- *
- * Copyright (C) 1993 by Peter Simons <simons@peti.GUN.de>
- *
- * 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.
- *
- * $Id: main.c,v 2.8 1994/01/17 15:05:04 simons Exp simons $
- *
- */
-
-
- /************************************* includes ***********/
- #include <exec/execbase.h>
- #include <proto/exec.h>
- #include <proto/dos.h>
- #include <dos.h>
-
- #include "listserv.h"
- #include "protos.h"
- #include "ListSERV_rev.h"
-
- /************************************* defines ************/
- #define STARTUP "Amiga " VERS " -- written by by Peter Simons <simons@peti.GUN.de>\n"
- #define USAGE "USAGE: ListSERV <mailfile [default_list]\n"
-
- /************************************* global variables ***/
- static char rcssid[] = "$Id: main.c,v 2.8 1994/01/17 15:05:04 simons Exp simons $";
- static char dosid[] = VERSTAG;
- struct NetSupportLibrary *NetSupportBase;
- FILE *mailer;
- char *def_listname;
- extern long __stack = 1024*5; /* In case the stack is too small,
- * allocate a new one. (SAS/C 6+) */
-
- /************************************* program ************/
- int main(int argc, char **argv)
- {
- extern struct ExecBase *SysBase;
- char *from = NULL;
- char buf[BUFSIZ];
- char grp[64];
- int gotcommand = 0;
- int garbage = 0;
-
- if (SysBase->LibNode.lib_Version < 37) {
- printf("Sorry, but %s requires at least OS 2.04!\n", VERS);
- return 10;
- }
- printf(STARTUP);
- if (argc > 2 || (argc == 2 && (!strcmp(argv[1], "?") || !stricmp(argv[1], "-h")))) {
- printf(USAGE);
- return 0;
- }
- if (!(NetSupportBase = (struct NetSupportLibrary *) OpenLibrary(NETSUPPORTNAME, 0L)))
- return 10;
-
- if (atexit(CleanUp)) {
- CleanUp();
- return 10;
- }
-
- /* init some variables */
- if (argc == 2 && argv[1])
- def_listname = argv[1];
-
-
- /* Parse header for "from". */
-
- while (fgets(buf, BUFSIZ, stdin) && buf[0] != '\n') {
- /* drop trailing newline */
- buf[strlen(buf)-1] = '\0';
-
- /* get the From: line so we can return the answer */
- if (!strnicmp(buf, "From: ",6))
- if (from = strdup(&buf[6]))
- ParseAddress(from);
- }
-
-
- /* Check "from" validity! */
-
- if (!from) {
- WriteLog("Received mail without 'From:'-line!");
- return 5;
- }
- if (!strnicmp(from, "listserv", 8)) {
- printf("I refuse to talk to myself.\n");
- return 5;
- }
- if (stristr(from, "-daemon")) {
- printf("We don't do business with your type.\n");
- return 5;
- }
-
-
- /* Process mail body. */
-
- while (fgets(buf, BUFSIZ, stdin)) {
- /* drop trailing newline */
- buf[strlen(buf)-1] = '\0';
-
- /* log the request */
- {
- char *tmp;
-
- if (tmp = malloc(BUFSIZ*2)) {
- sprintf(tmp, "%s: %s", from, buf);
- WriteLog(tmp);
- free(tmp);
- }
- }
-
- if (!strnicmp(buf, "list ",5)
- || !strnicmp(buf, "delete-all ",11)
- || !strnicmp(buf, "del-all ",8)
- || !strnicmp(buf, "unsubscribe-all ",16)
- || !stricmp(buf, "list")
- || !stricmp(buf, "delete-all")
- || !stricmp(buf, "del-all")
- || !stricmp(buf, "unsubscribe-all"))
- {
- gotcommand++;
- listsearch(from, buf);
- continue;
- }
-
- if (!strnicmp(buf,"add", 3)
- || !strnicmp(buf,"subscribe", 9)
- || !strnicmp(buf,"sub", 3))
- {
- gotcommand++;
- subscription(from,buf,1);
- continue;
- }
-
- if (!strnicmp(buf,"delete", 6)
- || !strnicmp(buf,"del", 3)
- || !strnicmp(buf,"unsubscribe", 11))
- {
- gotcommand++;
- subscription(from,buf,0);
- continue;
- }
-
- if (!stricmp(buf,"index")
- || !stricmp(buf,"longindex"))
- {
- gotcommand++;
- sendindex(from,buf,!stricmp(buf,"longindex"));
- continue;
- }
-
- if (!strnicmp(buf, "faq ", 4))
- {
- gotcommand++;
- sendfaq(from, buf);
- continue;
- }
-
- if (!stricmp(buf, "faq"))
- {
- gotcommand++;
- faqindex(from, buf);
- continue;
- }
-
- if (!stricmp(buf,"help"))
- {
- gotcommand++;
- sendhelp(from,buf);
- continue;
- }
-
- if (!strnicmp(buf,"help ",5))
- {
- gotcommand++;
- sscanf(buf,"%*s%s", grp);
- listhelp(from, grp, buf);
- continue;
- }
- /* Flush message if > 5 lines of garbage */
- if (++garbage > 5)
- {
- printf("Garbled message, flushing...\n");
- return 5;
- }
- }
- if (gotcommand == 0)
- sendhelp(from, "indecipherable");
-
- return 0;
- }
-
- void callmailer(char *carboncopy, char *toaddr, char *request)
- {
- char cbuf[BUFSIZ];
-
- printf("callmailer \"%s\",\"%s\",\"%s\"\n", carboncopy, toaddr, request);
-
- strcpy(cbuf, GetConfig(NULL, SENDMAIL, NULL, SENDMAIL));
-
- strcat(cbuf, " -f " LISTSERVADDR " -R \"Mailing List Processor\"");
- if (carboncopy && *carboncopy) {
- strcat(cbuf, " -c ");
- strcat(cbuf, carboncopy);
- }
- printf("mailer popen '%s'\n", cbuf);
- if (!(mailer = popen(cbuf,"w"))) {
- perror(cbuf);
- exit(10);
- }
- fprintf(mailer,"To: %s\n", toaddr);
- fprintf(mailer,"Subject: Re: your ListSERV request \"%s\"\n", request);
- fprintf(mailer,"X-ListSERV: Peti's Amiga " VERS "\n");
- fprintf(mailer,"\n");
- }
-
- void mailcat(char *mfname, char *prefix)
- {
- FILE *mf;
- char buf[BUFSIZ];
-
- printf("mailcat \"%s\", \"%s\"\n", mfname, prefix);
-
- LockFile(mfname);
- mf = fopen(mfname,"r");
- if (mf == NULL)
- {
- UnLockFile(mfname);
- perror(mfname);
- fprintf(mailer,"This should have been the contents of\n");
- fprintf(mailer,"file '%s' but it's missing!\n",mfname);
- return;
- }
-
- /* copy the message file into the mailer */
- while (fgets(buf,BUFSIZ,mf) != NULL)
- {
- fputs(prefix,mailer);
- fputs(buf,mailer);
- }
- fclose(mf);
- UnLockFile(mfname);
- return;
- }
-
-
- void ParseAddress(char *line)
- {
- int brackets, i, z;
-
- for (i = 0, z = 0, brackets = 0; line[i] != '\0'; i++) {
- if (line[i] == '(')
- brackets++;
- else if (line[i] == ')')
- brackets--;
- else if (line[i] == '<' && brackets == 0) {
- i++;
- while (1) { /* Forever! */
- if (line[i] == '>' && (line[i+1] == '\0' || isspace(line[i+1])))
- break;
- line[z++] = line[i++];
- }
- line[z] = '\0';
- break;
- }
- }
- if (z == 0) { /* No <address> occured. */
- i = 0;
- while (!isspace(line[i]))
- i++;
- line[i] = '\0';
- }
- }
-
-
- void CleanUp(void) /* is called by atexit() automatically */
- {
- UnLockFiles();
- CloseLibrary((struct Library *) NetSupportBase);
- }
-
-
- /* Replace the UNIX function with an AmigaOS version. */
-
- void perror(const char *dummy)
- {
- extern int _OSERR;
- PrintFault(_OSERR, "ListSERV");
- }
-
-
- void WriteLog(STRPTR logmessage)
- {
- struct DateTime dt;
- BPTR fh;
- TEXT StrDate[LEN_DATSTRING], StrTime[LEN_DATSTRING];
-
- DateStamp((struct DateStamp *) &dt);
- dt.dat_Format = FORMAT_DOS;
- dt.dat_StrDay = NULL;
- dt.dat_StrDate = StrDate;
- dt.dat_StrTime = StrTime;
-
- if (DateToStr(&dt)) {
- CheckDateString(StrDate); /* remove trailing blanks */
- CheckDateString(StrTime);
- LockFile(LOGFILE);
- if (fh = Open(LOGFILE, MODE_READWRITE)) {
- Seek(fh, 0L, OFFSET_END);
- FPrintf(fh, "(%s/%s) %s\n", StrDate, StrTime, logmessage);
- Printf("(%s/%s) %s\n", StrDate, StrTime, logmessage);
- Close(fh);
- }
- UnLockFile(LOGFILE);
- }
- }
-
-
- /* This routine removes trailing spaces from the generated date
- * string. These blanks are added in order make all date strings
- * the same length. However, this is not required for our
- * application.
- */
-
- void CheckDateString(STRPTR datestring)
- {
- while(*datestring)
- datestring++;
-
- while(*(datestring-1) == ' ')
- *(--datestring) = '\0';
- }
-
-