#include <mysql.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

/*
rm udf_mail.so
gcc -I /usr/local/mysql/include/mysql/ -fPIC -shared -o udf_mail.so mail.c -Wall
rm /usr/lib/udf_mail.so
cp udf_mail.so /usr/lib/

drop function mail;
create function mail returns integer soname 'udf_mail.so';
select mail('rolf@winmutt.com','Subject: UDF Mail\r\n\r\nYou got mail');
*/

my_bool mail_init(UDF_INIT *initid, UDF_ARGS *args, char *message) {
    initid->maybe_null = 0;

    return 0;
}

long long mail(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char
*error) {
    char *prg;
    FILE *p;
    int i;

    if( args->arg_count != 2 ) {
        strcpy(error, "mail() needs recipient and body");
        return 1;
    }

    if( args->arg_type[0] != STRING_RESULT || args->arg_type[1] != STRING_RESULT ) {
        strcpy(error, "mail() needs recipient and body as string");
        return 1;
    }

    for( i = 0; i < args->lengths[0]; i++ ) {
        char c = args->args[0][i];

        if( !((c >= 'a' && c <= 'z') ||
              (c >= 'A' && c <= 'Z') ||
              (c == '@') ||
              (c == '.') ||
              (c >= '0' && c <= '9') ||
              (c == '-' ) ||
              (c == '_' )) ) {
            strcpy(error, "the receipient contains an illegal char");

            return -1;
        }
    }

    prg = malloc( strlen("/usr/lib/sendmail -fdb@mynatnet.com ") + args->lengths[0] + 1 );
    strcpy(prg, "/usr/lib/sendmail -fdb@mynatnet.com ");

    strcat(prg, args->args[0]);

    p = popen(prg, "w");

    free(prg);

    if( p == NULL ) {
        strcpy(error, "opening pipe failed");
        return -1;
    }

    fwrite(args->args[1], args->lengths[1], 1, p);
    pclose(p);

    *is_null = 0;
    *error   = 0;

    return 0;
}
