From fa3eec2e65fd21a269359de808e2464ccba33ecb Mon Sep 17 00:00:00 2001 From: reinelt <> Date: Thu, 8 Apr 2004 11:59:26 +0000 Subject: [lcd4linux @ 2004-04-08 11:59:26 by reinelt] added plugin_pop3 from Javi --- Makefile.am | 3 +- Makefile.in | 15 +-- lcd4linux.conf.sample | 8 ++ plugin.c | 9 +- plugin_pop3.c | 336 ++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 362 insertions(+), 9 deletions(-) create mode 100644 plugin_pop3.c diff --git a/Makefile.am b/Makefile.am index 205857d..b004b38 100644 --- a/Makefile.am +++ b/Makefile.am @@ -54,7 +54,8 @@ plugin_exec.c \ plugin_xmms.c \ plugin_mysql.c \ plugin_wireless.c \ -plugin_seti.c +plugin_seti.c \ +plugin_pop3.c #liblcd4linux_la_DEPENDENCIES = @DRIVERS@ #liblcd4linux_la_LDFLAGS = -version-info 9:12:9 diff --git a/Makefile.in b/Makefile.in index f26474a..2dd938b 100644 --- a/Makefile.in +++ b/Makefile.in @@ -91,7 +91,7 @@ lcd4linux_LDADD = @DRIVERS@ @DRVLIBS@ #remove next line for liblcd4linux lcd4linux_DEPENDENCIES = @DRIVERS@ -lcd4linux_SOURCES = lcd4linux.c cfg.c cfg.h debug.c debug.h drv.c drv.h evaluator.c evaluator.h hash.c hash.h layout.c layout.h lock.c lock.h pid.c pid.h timer.c timer.h thread.c thread.h udelay.c udelay.h qprintf.c qprintf.h widget.c widget.h widget_text.c widget_text.h widget_bar.c widget_bar.h widget_icon.c widget_icon.h plugin.c plugin.h plugin_math.c plugin_string.c plugin_cfg.c plugin_uname.c plugin_loadavg.c plugin_proc_stat.c plugin_cpuinfo.c plugin_meminfo.c plugin_netdev.c plugin_ppp.c plugin_dvb.c plugin_apm.c plugin_i2c_sensors.c plugin_imon.c plugin_exec.c plugin_xmms.c plugin_mysql.c plugin_wireless.c plugin_seti.c +lcd4linux_SOURCES = lcd4linux.c cfg.c cfg.h debug.c debug.h drv.c drv.h evaluator.c evaluator.h hash.c hash.h layout.c layout.h lock.c lock.h pid.c pid.h timer.c timer.h thread.c thread.h udelay.c udelay.h qprintf.c qprintf.h widget.c widget.h widget_text.c widget_text.h widget_bar.c widget_bar.h widget_icon.c widget_icon.h plugin.c plugin.h plugin_math.c plugin_string.c plugin_cfg.c plugin_uname.c plugin_loadavg.c plugin_proc_stat.c plugin_cpuinfo.c plugin_meminfo.c plugin_netdev.c plugin_ppp.c plugin_dvb.c plugin_apm.c plugin_i2c_sensors.c plugin_imon.c plugin_exec.c plugin_xmms.c plugin_mysql.c plugin_wireless.c plugin_seti.c plugin_pop3.c #liblcd4linux_la_DEPENDENCIES = @DRIVERS@ @@ -127,7 +127,7 @@ plugin_string.o plugin_cfg.o plugin_uname.o plugin_loadavg.o \ plugin_proc_stat.o plugin_cpuinfo.o plugin_meminfo.o plugin_netdev.o \ plugin_ppp.o plugin_dvb.o plugin_apm.o plugin_i2c_sensors.o \ plugin_imon.o plugin_exec.o plugin_xmms.o plugin_mysql.o \ -plugin_wireless.o plugin_seti.o +plugin_wireless.o plugin_seti.o plugin_pop3.o CFLAGS = @CFLAGS@ COMPILE = $(CC) $(DEFS) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) CCLD = $(CC) @@ -154,11 +154,12 @@ DEP_FILES = .deps/BeckmannEgle.P .deps/MilfordInstruments.P \ .deps/plugin_cpuinfo.P .deps/plugin_dvb.P .deps/plugin_exec.P \ .deps/plugin_i2c_sensors.P .deps/plugin_imon.P .deps/plugin_loadavg.P \ .deps/plugin_math.P .deps/plugin_meminfo.P .deps/plugin_mysql.P \ -.deps/plugin_netdev.P .deps/plugin_ppp.P .deps/plugin_proc_stat.P \ -.deps/plugin_seti.P .deps/plugin_string.P .deps/plugin_uname.P \ -.deps/plugin_wireless.P .deps/plugin_xmms.P .deps/qprintf.P \ -.deps/thread.P .deps/timer.P .deps/udelay.P .deps/widget.P \ -.deps/widget_bar.P .deps/widget_icon.P .deps/widget_text.P +.deps/plugin_netdev.P .deps/plugin_pop3.P .deps/plugin_ppp.P \ +.deps/plugin_proc_stat.P .deps/plugin_seti.P .deps/plugin_string.P \ +.deps/plugin_uname.P .deps/plugin_wireless.P .deps/plugin_xmms.P \ +.deps/qprintf.P .deps/thread.P .deps/timer.P .deps/udelay.P \ +.deps/widget.P .deps/widget_bar.P .deps/widget_icon.P \ +.deps/widget_text.P SOURCES = $(lcd4linux_SOURCES) $(EXTRA_lcd4linux_SOURCES) OBJECTS = $(lcd4linux_OBJECTS) diff --git a/lcd4linux.conf.sample b/lcd4linux.conf.sample index 40cc963..543ce18 100644 --- a/lcd4linux.conf.sample +++ b/lcd4linux.conf.sample @@ -109,6 +109,14 @@ Plugin MySQL { database 'lcd4linux' # MUST be specified } +Plugin Pop3 { + server1 'localhost' + port1 110 + user1 'michael' + password1 'secret' +} + + Widget OS { class 'Text' expression '*** '.uname('sysname').' '.uname('release').' ***' diff --git a/plugin.c b/plugin.c index 289dd50..f5fd4c8 100644 --- a/plugin.c +++ b/plugin.c @@ -1,4 +1,4 @@ -/* $Id: plugin.c,v 1.25 2004/04/07 08:29:05 hejl Exp $ +/* $Id: plugin.c,v 1.26 2004/04/08 11:59:26 reinelt Exp $ * * plugin handler for the Evaluator * @@ -22,6 +22,9 @@ * * * $Log: plugin.c,v $ + * Revision 1.26 2004/04/08 11:59:26 reinelt + * added plugin_pop3 from Javi + * * Revision 1.25 2004/04/07 08:29:05 hejl * New plugin for wireless info * @@ -177,6 +180,7 @@ int plugin_init_imon(void); int plugin_init_mysql(void); int plugin_init_seti(void); int plugin_init_wireless(void); +int plugin_init_pop3(void); void plugin_exit_math (void); @@ -198,6 +202,7 @@ void plugin_exit_imon(void); void plugin_exit_mysql(void); void plugin_exit_seti(void); void plugin_exit_wireless(void); +void plugin_exit_pop3(void); int plugin_init (void) { @@ -220,6 +225,7 @@ int plugin_init (void) plugin_init_mysql(); plugin_init_seti(); plugin_init_wireless(); + plugin_init_pop3(); return 0; } @@ -244,6 +250,7 @@ void plugin_exit(void) { plugin_exit_mysql(); plugin_exit_seti(); plugin_exit_wireless(); + plugin_exit_pop3(); DeleteFunctions(); DeleteVariables(); diff --git a/plugin_pop3.c b/plugin_pop3.c new file mode 100644 index 0000000..9d7c35e --- /dev/null +++ b/plugin_pop3.c @@ -0,0 +1,336 @@ +/* $Id: plugin_pop3.c,v 1.1 2004/04/08 11:59:26 reinelt Exp $ + * + * Plugin to check POP3 mail accounts + * + * Copyright 2004 Javi Garcia Dominguez (aka Stolz) + * Based on code from pop3check (c) 1999 http://sourceforge.net/projects/pop3check + * Simon Liddington is the pop3check current maintainer. + * The pop3check original author is Steven Radack . + * + * Copyright 2004 The LCD4Linux Team + * + * This file is a pluging for LCD4Linux. + * + * LCD4Linux 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, or (at your option) + * any later version. + * + * LCD4Linux 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. + * + * + * $Log: plugin_pop3.c,v $ + * Revision 1.1 2004/04/08 11:59:26 reinelt + * added plugin_pop3 from Javi + * + */ + + +#include "config.h" +#include +#include +#include +#include "debug.h" +#include "plugin.h" +#include "cfg.h" + +//added +#include +#include +//#include +#include +#include +//#include +#include +// + +#ifdef WITH_DMALLOC +#include +#endif + +//POP 3 +#define POPERR "-ERR" +#define LOCKEDERR "-ERR account is locked by another session or for maintenance, try again." +#define BUFSIZE 8192 +#define POP3PORT 110 +#define MAX_NUM_ACCOUNTS 3 +// + +static struct check { + int id; + char *username; + char *password; + char *server; + int port; + int messages; + struct check *next; +}; + +// ************************ PROTOTYPES ******************************** +// list +static struct check *check_node_alloc(void); +void check_node_add(struct check **head, struct check *new_check); +void check_destroy(struct check **head); +// pop3 +void pop3_check_messages(struct check *hi,int verbose); +void pop3_recv_crlf_terminated(int sockfd, char *buf, int size); +// socket +int tcp_connect(struct check *hi); +// ************************ GLOBAL *********************************** +static char Section[] = "Plugin:POP3"; +static struct check *head = NULL; +// ******************************************************************** +static void my_POP3check (RESULT *result, RESULT *check) +{ + double value; + double param=R2N(check); + + struct check *node = NULL; + + for (node = head; node; node = node->next) + { + if (node->id == param) + break; + } + if (node == NULL) //Inexistent account + value=-1.0; + else + pop3_check_messages(node,0); + value = (double)node->messages; + SetResult(&result, R_NUMBER, &value); +} + +static int getConfig (void) +{ + struct check *node = NULL; + int i,n=0; + char *user= (char *)calloc(1, sizeof("user")+sizeof(int)); + char *password= (char *)calloc(1, sizeof("password")+sizeof(int)); + char *server=(char *)calloc(1, sizeof("server")+sizeof(int)); + char *port= (char *)calloc(1, sizeof("port")+sizeof(int)); + + for (i =1;i <= MAX_NUM_ACCOUNTS; i++) + { + sprintf(user,"user%d",i); + sprintf(password,"password%d",i); + sprintf(server,"server%d",i); + sprintf(port,"port%d",i); + + char *x = cfg_get (Section, server, ""); + if (*x=='\0') + { + info ("[POP3] No '%s.%s' entry from %s, disabling POP3 account #%d", Section, server, cfg_source(),i); + free(x); + } + else + { + node = check_node_alloc(); + node->id = i; + node->server = x; + node->messages = 0; + node->next = NULL; + + x = cfg_get (Section, user, ""); + if (*x=='\0') + { + info ("[POP3] No '%s.%s' entry from %s, disabling POP3 account #%d", Section, user, cfg_source(),i); + free(x); + } + else + { + node->username = x; + x = cfg_get (Section, password, ""); + if (*x=='\0') + { + info ("[POP3] No '%s.%s' entry from %s, disabling POP3 account #%d", Section, password, cfg_source(),i); + free(x); + } + else + { + node->password = x; + if (cfg_number(Section, port, POP3PORT, 1, 65536, &node->port)<1) + info("[POP3] No '%s.%s' entry from %s, %d will be used for account #%d", Section, port, cfg_source(),POP3PORT,i); + check_node_add(&head, node); + n++; + } + } + } + } + return(n); +} + +int plugin_init_pop3(void) +{ + + int n = getConfig(); + // by now, head should point to a list of all our accounts + if (head) + { + info("[POP3] %d POP3 accounts have been succesfully defined",n); + AddFunction ("POP3check", 1, my_POP3check); + } + return 0; +} + +void plugin_exit_pop3(void) +{ + check_destroy(&head); +} + +// ************************ LIST *********************************** +static struct check *check_node_alloc(void) +{ + struct check *new_check; + new_check = (struct check *)calloc(1, sizeof(struct check)); + if (new_check == NULL) + { + error( "[POP3] out of memory\n"); + } + return new_check; +} + + void check_node_add(struct check **head, struct check *new_check) +{ + new_check->next = *head ; + *head = new_check; +} + +void check_destroy(struct check **head) +{ + struct check *iter; + while (*head) + { + iter = (*head)->next; + free((*head)->username); + free((*head)->password); + free((*head)->server); + free(*head); + *head = iter; + } + *head = NULL; +} + +// ************************ POP3 ******************************** +void pop3_check_messages(struct check *hi,int verbose) +{ + char buf[BUFSIZE]; + int sockfd; + + if ((sockfd = tcp_connect(hi)) < 0) + { + hi->messages = -1; + return; + } + + pop3_recv_crlf_terminated(sockfd, buf, sizeof(buf)); /* server greeting */ + if (verbose) + info("[POP3] %s -> %s\n", hi->server, buf); + + snprintf(buf, sizeof(buf), "USER %s\r\n", hi->username); + write(sockfd, buf, strlen(buf)); + buf[strlen(buf)-1] = '\0'; + if (verbose) + info("[POP3] %s <- %s\n", hi->server, buf); + pop3_recv_crlf_terminated(sockfd, buf, sizeof(buf)); /* response from USER command */ + if (verbose) + info("[POP3] %s -> %s\n", hi->server, buf); + + snprintf(buf, sizeof(buf), "PASS %s\r\n", hi->password); + write(sockfd, buf, strlen(buf)); + if (verbose) + info("[POP3] %s <- PASS ???\n", hi->server); + pop3_recv_crlf_terminated(sockfd, buf, sizeof(buf)); /* response from PASS command */ + if (verbose) + info("[POP3] %s -> %s\n", hi->server, buf); + + if (strncmp(buf, LOCKEDERR, strlen(LOCKEDERR)) == 0) + { + hi->messages = -2; + close(sockfd); + return; + } + if (strncmp(buf, POPERR, strlen(POPERR)) == 0) + { + error("[POP3] error logging into %s\n", hi->server); + error("[POP3] server responded: %s\n", buf); + hi->messages = -1; + close(sockfd); + return; + } + + snprintf(buf, sizeof(buf), "STAT\r\n"); + write(sockfd, buf, strlen(buf)); + if (verbose) + info("[POP3] %s <- STAT\n", hi->server); + pop3_recv_crlf_terminated(sockfd, buf, sizeof(buf)); /* response from PASS command */ + if (verbose) + info("[POP3] %s -> %s\n", hi->server, buf); + + strtok(buf, " "); + hi->messages = atoi(strtok(NULL, " ")); + + snprintf(buf, sizeof(buf), "QUIT\r\n"); + write(sockfd, buf, strlen(buf)); + if (verbose) + info("[POP3] %s <- QUIT\n", hi->server); + pop3_recv_crlf_terminated(sockfd, buf, sizeof(buf)); /* response from QUIT command */ + if (verbose) + info("[POP3] %s -> %s\n", hi->server, buf); + + close(sockfd); +} + +void pop3_recv_crlf_terminated(int sockfd, char *buf, int size) +{ +/* receive one line server responses terminated with CRLF */ + char *pos; + int bytes = 0; + memset(buf, 0, size); + while ((pos = strstr(buf, "\r\n")) == NULL) + bytes += read(sockfd, buf+bytes, size-bytes); + *pos = '\0'; +} + +// ************************ SOCKET ******************************** +int tcp_connect(struct check *hi) +{ + struct sockaddr_in addr; + struct hostent *he = gethostbyname(hi->server); + int sockfd; + + if (hi == NULL) + return -1; + + if (!he) + { + error("[POP3] Failed to lookup %s\n",hi->server); + return(-1); + } + + memset((char *)&addr, 0, sizeof(struct sockaddr_in)); + addr.sin_family = AF_INET; + memcpy(&(addr.sin_addr.s_addr), he->h_addr, he->h_length); + addr.sin_port = htons(hi->port); + + if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) + { + perror("socket()"); + return(-1); + } + + if (connect(sockfd, (struct sockaddr *)&addr, sizeof(struct sockaddr)) < 0) + { + perror("connect()"); + close(sockfd); + return(-1); + } + + return(sockfd); +} -- cgit v1.2.3