Copied missing files from the subversion repository.
[collectd.git] / src / libping / pop3.c
1 /**
2  * POP3 module
3  *
4  * Copyright (C) 2001, 2002 by
5  * Jeffrey Fulmer <jdfulmer@armstrong.com>
6  * This file is part of LIBPING
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  */
23 #include <pop3.h>
24 #include <setup.h>
25
26 #ifdef HAVE_CONFIG_H
27 # include <config.h>
28 #endif/*HAVE_CONFIG_H*/
29
30 #include <sock.h>
31 #include <util.h>
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <string.h>
35  
36 #ifdef HAVE_SYS_TIMES_H
37 # include <sys/times.h>
38 #endif /*HAVE_SYS_TIMES_H*/
39 #if TIME_WITH_SYS_TIME
40 # include <sys/time.h>
41 # include <time.h>
42 #else
43 # if HAVE_SYS_TIME_H
44 #  include <sys/time.h>
45 # else
46 #  include <time.h>
47 # endif
48 #endif /*TIME_WITH_SYS_TIME*/ 
49
50 #include <signal.h>
51 #include <setjmp.h> 
52
53 #include "memory.h"
54
55 #define MSGBUF  1024
56
57 int send_cmd( CONN *C, char *cmd, char *val );
58
59 int
60 mypop3( POP3DATA *P )
61 {
62   CONN *C;
63   char buf[MSGBUF];
64   int ret  = 0;
65   struct timeval mytime;
66
67   C = (CONN*)xmalloc( sizeof( CONN ));
68   C->port    =   110;
69   C->timeout = ( P->timeout == 0 )?60:P->timeout; 
70   
71   if(( C->sock = JOEsocket( C, P->hostname )) <= 0 ){
72     return -1;
73   }   
74
75   /* set the rrt timer */
76   (void) gettimeofday( &mytime, (struct timezone *)NULL); 
77
78   if(( ret = JOEreadline( C, buf, MSGBUF )) < 0 ){
79     return ret;
80   }
81   if( !strncmp( buf, "+OK", 3 )) ret = 1; 
82     
83   if(( ret = send_cmd( C, "QUIT", NULL )) < 0 ){
84     return ret; 
85   }
86
87   JOEclose( C );
88   P->rtt = elapsed_time( &mytime ); 
89
90   return ret;
91 }
92
93 int
94 send_cmd( CONN *C, char *cmd, char *val )
95 {
96   char buf[256];
97   char rec[MSGBUF];
98
99   if( val )
100     snprintf( buf, sizeof( buf ), "%s %s\r\n", cmd, val );
101   else
102     snprintf( buf, sizeof( buf ), "%s\r\n", cmd );
103
104   if( JOEsocket_write( C, buf, sizeof( buf ))  < 0 )
105     return -1;
106
107   JOEreadline( C, rec, MSGBUF );
108   *rec='\0';
109
110   if( !strncmp( rec, "-ERR", 4 )){
111     return -1;
112   }
113
114   return 1; 
115 }
116
117 int 
118 pingpop3( const char *hostname )
119 {
120   POP3DATA *P;
121  
122   P = (POP3DATA*)xmalloc( sizeof( POP3DATA ));
123   P->hostname = (char*)strdup( hostname );
124   P->timeout = 0;
125  
126   return mypop3( P );
127 }
128
129 int
130 pingtpop3( const char *hostname, int t )
131 {
132   POP3DATA *P;
133   int ret;
134  
135   P = (POP3DATA*)xmalloc( sizeof( POP3DATA ));
136   P->hostname = (char*)strdup( hostname );
137   P->timeout = t;
138  
139   ret = mypop3( P ); 
140
141   return mypop3( P );
142 }
143
144 int
145 tpingpop3( const char *hostname )
146 {
147   POP3DATA *P;
148   int ret;
149  
150   P = (POP3DATA*)xmalloc( sizeof( POP3DATA ));
151   P->hostname = (char*)strdup( hostname );
152   P->timeout = 0;
153  
154   ret = mypop3( P ); 
155
156   if( ret > 0 ){ return P->rtt; }
157   else         { return ret; }
158 }
159  
160 int
161 tpingtpop3( const char *hostname, int t )
162 {
163   POP3DATA *P;
164   int ret;
165  
166   P = (POP3DATA*)xmalloc( sizeof( POP3DATA ));
167   P->hostname = (char*)strdup( hostname );
168   P->timeout = t; 
169   
170   ret = mypop3( P );
171  
172   if( ret > 0 ){ return P->rtt; }
173   else         { return ret; }
174  
175