Вот еще переписал скрипт на Си, теперь будет быстрее работать...
greylist.c
================================================
#include <stdio.h>
#include "mysql.h"
/* Greylist parametrs */
#define BLOCK_EXPIRES 60*60*1
#define RECORD_EXPIRES_INITIALY 60*60*24
#define RECORD_EXPIRES_ELECTED 60*60*24*60
/* MySQL parameters */
#define MYSQL_HOST NULL
#define MYSQL_DATABASE "mail"
#define MYSQL_USER "exim"
#define MYSQL_PASSWORD "passwd"
#define MYSQL_PORT 3306
#define MYSQL_UNIXSOCKET "/tmp/mysql.sock"
#define MYSQL_CLIENTFLAG 0
void usage() {
printf ("Usage: greylist [relay_ip] [sender] [recipient]\n");
exit (0);
}
int main(int argc, char ** argv) {
MYSQL mysql;
MYSQL_ROW row;
MYSQL_RES * result;
unsigned long long id;
signed int delta_time;
char query[1024];
if (argc!=4) usage();
mysql_init(&mysql);
if (!mysql_real_connect(&mysql,MYSQL_HOST,MYSQL_USER,MYSQL_PASSWORD,MYSQL_DATABASE,MYSQL_PORT,MYSQL_UNIXSOCKET,MYSQL_CLIENTFLAG)) {
fprintf(stderr, "Connection to MySQL failed: %s\n", mysql_error(&mysql));
exit(1);
}
sprintf (query,"select id,unix_timestamp()-block_expires from Greylist where relay_ip=inet_aton('%s') and sender=md5('%s') and recipient=md5('%s') limit 1",argv[1],argv[2],argv[3]);
if (mysql_query(&mysql,query)) {
fprintf(stderr, "SQL: \"%s\" failed: %s\n",query, mysql_error(&mysql));
exit (1);
}
result = mysql_store_result(&mysql);
if (mysql_num_rows(result)) {
row = mysql_fetch_row(result);
mysql_free_result(result);
id=strtoull(row[0],0,10);
delta_time=strtol(row[1],0,10);
if (delta_time>0) {
sprintf (query,"update Greylist set pass_count=pass_count+1,record_expires=unix_timestamp()+%lu where id='%llu'",RECORD_EXPIRES_ELECTED,id);
if (mysql_query (&mysql,query)) fprintf(stderr, "SQL: \"%s\" failed: %s\n",query, mysql_error(&mysql));
mysql_close(&mysql);
exit (1);
}
else {
sprintf (query,"update Greylist set block_count=block_count+1 where id='%llu'",id);
if (mysql_query (&mysql,query)) {
fprintf(stderr, "SQL: \"%s\" failed: %s\n",query, mysql_error(&mysql));
exit (1);
}
}
}
else {
sprintf (query,"insert into Greylist (relay_ip,sender,recipient,block_expires,record_expires,create_time,block_count) values (inet_aton('%s'),md5('%s'),md5('%s'),unix_timestamp()+%lu,unix_timestamp()+%lu,unix_timestamp(),1)",argv[1],argv[2],argv[3],BLOCK_EXPIRES,RECORD_EXPIRES_INITIALY);
if (mysql_query(&mysql,query)) {
fprintf(stderr, "SQL: \"%s\" failed: %s\n",query, mysql_error(&mysql));
exit (1);
}
}
mysql_close(&mysql);
exit(0);
}