コード例 #1
0
ファイル: autorespond.c プロジェクト: debdungeon/qint
/*********************************************************
** returns the content boundary string for this message */
char *get_content_boundary()
{
    char *s, *r;

    if ( (s = inspect_headers( "Content-Type", (char *)NULL )) == (char *)NULL)
        return (char *)NULL;

    if ( (r = strcasestr( s, "boundary=" )) == (char *)NULL)
        return (char *)NULL;

    *(r+strlen(r)-2) = '\0'; /* delete quote at the end */

    return r+10; /* point to first cahr after quote */
}
コード例 #2
0
ファイル: wsgi.c プロジェクト: Logout22/bjoern
static PyObject*
start_response(PyObject* self, PyObject* args, PyObject* kwargs)
{
  Request* request = ((StartResponse*)self)->request;

  if(request->state.start_response_called) {
    /* not the first call of start_response --
     * throw away any previous status and headers. */
    Py_CLEAR(request->status);
    Py_CLEAR(request->headers);
    request->state.response_length_unknown = false;
  }

  PyObject* status = NULL;
  PyObject* headers = NULL;
  PyObject* exc_info = NULL;
  if(!PyArg_UnpackTuple(args, "start_response", 2, 3, &status, &headers, &exc_info))
    return NULL;

  if(exc_info && exc_info != Py_None) {
    if(!PyTuple_Check(exc_info) || PyTuple_GET_SIZE(exc_info) != 3) {
      TYPE_ERROR("start_response argument 3", "a 3-tuple", exc_info);
      return NULL;
    }

    restore_exception_tuple(exc_info, /* incref items? */ true);

    if(request->state.wsgi_call_done) {
      /* Too late to change headers. According to PEP 333, we should let
       * the exception propagate in this case. */
      return NULL;
    }

    /* Headers not yet sent; handle this start_response call as if 'exc_info'
     * would not have been passed, but print and clear the exception. */
    PyErr_Print();
  }
  else if(request->state.start_response_called) {
    PyErr_SetString(PyExc_TypeError, "'start_response' called twice without "
                     "passing 'exc_info' the second time");
    return NULL;
  }

  if(!PyString_Check(status)) {
    TYPE_ERROR("start_response argument 1", "a 'status reason' string", status);
    return NULL;
  }
  if(!PyList_Check(headers)) {
    TYPE_ERROR("start response argument 2", "a list of 2-tuples", headers);
    return NULL;
  }

  request->headers = headers;

  if(!inspect_headers(request)) {
    request->headers = NULL;
    return NULL;
  }

  request->status = status;

  Py_INCREF(request->status);
  Py_INCREF(request->headers);

  request->state.start_response_called = true;

  Py_RETURN_NONE;
}
コード例 #3
0
ファイル: autorespond.c プロジェクト: debdungeon/qint
int main(int argc, char ** argv)
{
    char * sender;

    char * message;
    unsigned int time_message;
    unsigned int timer;
    unsigned int num;
    char * message_filename;
    char * dir;

    char * ptr;
    char * my_delivered_to;

    DIR * dirp;
    struct dirent * direntp;
    unsigned int message_time;
    char * address;
    unsigned int count;
    char filename[256];
    FILE * f;
    unsigned int message_handling = DEFAULT_MH;
    char buffer[256];
    char *content_boundary;
    char *rpath = DEFAULT_FROM;
    char *TheUser;
    char *TheDomain;

    if(argc > 7 || argc < 5) {
        fprintf(stderr, "\nautorespond: ");
        fprintf(stderr, "usage: time num message dir [ flag arsender ]\n\n");
        fprintf(stderr, "time - amount of time to consider a message (in seconds)\n");
        fprintf(stderr, "num - maximum number of messages to allow within time seconds\n");
        fprintf(stderr, "message - the filename of the message to send\n");
        fprintf(stderr, "dir - the directory to hold the log of messages\n\n");
        fprintf(stderr, "optional parameters:\n\n");
        fprintf(stderr, "flag - handling of original message:\n\n");
        fprintf(stderr, "0 - append nothing\n");
        fprintf(stderr, "1 - append quoted original message without attachments <default>\n\n");
        fprintf(stderr, "arsender - from address in generated message, or:\n\n");
        fprintf(stderr, "+ = blank from envelope !\n");
        fprintf(stderr, "$ = To: address will be used\n\n");
        _exit(111);
    }

    TheUser= getenv("EXT");
    TheDomain= getenv("HOST");

    setvbuf(stderr, NULL, _IONBF, 0);

    if(argc > 7 || argc < 5) {
        fprintf(stderr, "AUTORESPOND: Invalid arguments. (%d)\n",argc);
        _exit(111);
    }

    time_message     = strtoul(argv[1],NULL,10);
    num              = strtoul(argv[2],NULL,10);
    message_filename = argv[3];
    dir              = argv[4];

    if ( argc > 5 )
        message_handling = strtoul(argv[5],NULL,10);
    if ( argc > 6 )
        rpath = argv[6];

    if ( *rpath == '+' )
        rpath = "";
    if ( *rpath == '$' )
    {
        rpath = safe_malloc( strlen(TheUser) + strlen(TheDomain) + 2);
        strncpy( rpath, TheUser, strlen(TheUser) );
        strncat( rpath, "@", 1 );
        strncat( rpath, TheDomain, strlen(TheDomain) );
    }

    timer = time(NULL);

    /*prepare the "delivered-to" string*/
    my_delivered_to = "Delivered-To: Autoresponder\n";

    read_headers( stdin );


    message = read_file(message_filename);
    if(message==NULL) {
        fprintf(stderr, "AUTORESPOND: Failed to open message file.\n");
        _exit(111);
    }

    /*don't autorespond in certain situations*/
    sender = getenv("SENDER");
    if(sender==NULL)
        sender = "";

    /*don't autorespond to a mailer-daemon*/
    if( sender[0]==0 || strncasecmp(sender,"mailer-daemon",13)==0 || strchr(sender,'@')==NULL || strcmp(sender,"#@[]")==0 ) {
        /*exit with success and continue parsing .qmail file*/
        fprintf(stderr,"AUTORESPOND:  Stopping on mail from [%s].\n",sender);
        _exit(0);
    }


    if ( inspect_headers("mailing-list", (char *)NULL ) != (char *)NULL )
    {
        fprintf(stderr,"AUTORESPOND: This looks like it's from a mailing list, I will ignore it.\n");
        _exit(0);			/*report success and exit*/
    }
    if ( inspect_headers("Delivered-To", "Autoresponder" ) != (char *)NULL )
    {
        /*got one of my own messages...*/
        fprintf(stderr,"AUTORESPOND: This message is looping...it has my Delivered-To header.\n");
        _exit(100);			/*hard error*/
    }
    if ( inspect_headers("precedence", "junk" ) != (char *)NULL ||
            inspect_headers("precedence", "bulk" ) != (char *)NULL ||
            inspect_headers("precedence", "list" ) != (char *)NULL )
    {
        fprintf(stderr,"AUTORESPOND: Junk mail received.\n");
        _exit(0); /* don't reply to bulk, junk, or list mail */
    }

    /*check the logs*/
    if(chdir(dir) == -1) {
        fprintf(stderr,"AUTORESPOND: Failed to change into directory.\n");
        _exit(111);
    }

    /*add entry*/
    sprintf(filename,"A%u.%u",getpid(),timer);
    f = fopen(filename,"wb");
    if(f==NULL) {
        fprintf(stderr,"AUTORESPOND: Unable to create file for [%s].",sender);
        _exit(111);
    }
    if(fwrite(sender,1,strlen(sender),f)!=strlen(sender)) {
        fprintf(stderr,"AUTORESPOND: Unable to create file for [%s].",sender);
        fclose(f);
        unlink(filename);
        _exit(111);
    }
    fclose(f);

    /*check if there are too many responses in the logs*/
    dirp = opendir(".");
    count = 0;
    while((direntp = readdir(dirp)) != NULL) {
        if(direntp->d_name[0] != 'A')
            continue;
        ptr = strchr(direntp->d_name,'.');
        if(ptr==NULL)
            continue;
        message_time = strtoul(ptr+1,NULL,10);
        if(message_time < timer-time_message) {
            /*too old..ignore errors on unlink*/
            unlink(direntp->d_name);
        } else {
            address = read_file(direntp->d_name);
            if(address==NULL) {
                /*ignore this?*/
                continue;
            }
            if(strcasecmp(address,sender)==0) {
                count++;
            }
            free(address);
        }
    }
    if(count>num) {
        fprintf(stderr,"AUTORESPOND: too many received from [%s]\n",sender);
        _exit(0); /* don't reply to this message, but allow it to be delivered */
    }

    sprintf(filename,"tmp%u.%u",getpid(),timer);
    f = fopen(filename,"wb");

    fprintf( f, "%sTo: %s\nFrom: %s\nSubject: Re:%s\n%s\n",
             my_delivered_to, sender, rpath, inspect_headers( "Subject", (char *) NULL ), message );

    if ( message_handling == 1 ) {
        fprintf( f, "%s\n\n", "-------- Original Message --------" );
        if ( (content_boundary = get_content_boundary()) == (char *)NULL )
        {
            while ( fgets( buffer, sizeof(buffer), stdin ) != NULL )
            {
                fputs( "> ", f );
                fputs( buffer, f );
            }
        } else
        {
            int content_found = 0;
            while ( fgets( buffer, sizeof(buffer), stdin ) != NULL )
            {
                if ( content_found == 1 )
                {
                    if ( strstr( buffer, content_boundary ) != (char *)NULL )
                        break;
                    fputs( "> ", f );
                    fputs( buffer, f );
                }
                if ( strstr( buffer, content_boundary ) != (char *)NULL )
                {
                    if ( content_found == 1 )
                        break;
                    else
                    {
                        free_headers();
                        read_headers( stdin );
                        if ( inspect_headers("Content-Type", "text/plain" ) != (char *)NULL )
                            content_found = 1;
                        continue;
                    }
                }
            }
        }
    }

    fprintf( f, "\n\n" );

    fclose( f );

    /*send the autoresponse...ignore errors?*/
    send_message(filename,rpath,&sender,1);

    unlink( filename );

    _exit(0);
    return 0;					/*compiler warning squelch*/
}