wmacpi: Merge clean and clean-all targets in Makefile.
[dockapps.git] / wmnotify / src / pop3.c
blobbf498f9f44fa171789d6b5d9f851e570cf7fcb63
1 /*
2 * pop3.c -- Routines for communication with a pop3 server
4 * Copyright (C) 2003 Hugo Villeneuve <hugo@hugovil.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
21 /* Define filename_M */
22 #define POP3_M 1
24 #if HAVE_CONFIG_H
25 # include "config.h"
26 #endif
28 #include <stdio.h>
29 #include <unistd.h>
30 #include <stdlib.h>
31 #include <sys/types.h>
32 #include <sys/wait.h>
33 #include <sys/socket.h>
34 #include <netinet/in.h>
35 #include <netdb.h>
37 #include "common.h"
38 #include "wmnotify.h"
39 #include "network.h"
40 #include "pop3.h"
43 /* Defined in network.c */
44 extern char tx_buffer[WMNOTIFY_BUFSIZE + 1];
45 extern char rx_buffer[WMNOTIFY_BUFSIZE + 1];
48 static int
49 POP3_ReceiveResponse( void )
51 int len;
53 len = WmnotifyGetResponse( rx_buffer, WMNOTIFY_BUFSIZE );
54 if( len < 0 ) {
55 perror( PACKAGE );
56 ErrorLocation( __FILE__, __LINE__ );
57 return len;
60 rx_buffer[ len - 2 ] = '\0';
62 if( wmnotify_infos.debug ) {
63 printf( "Response: \"%s\"\n", rx_buffer );
66 /* No error in recv at this point. Now we parse response from POP3 server. */
68 /* Check the status indicator returned by the POP3 server.
69 There are currently two status indicators: positive ("+OK") and negative
70 ("-ERR"). Servers MUST send the status indicators in upper case. */
71 if( STREQ_LEN( rx_buffer, POP3_RSP_SUCCESS, strlen(POP3_RSP_SUCCESS) ) == false ) {
72 fprintf( stderr, "%s: Error, POP3 server responded:\n \"%s\"\n", PACKAGE, rx_buffer );
73 len = -1;
76 return len;
80 static int
81 POP3_SendCommand( int argc, char *argv[] )
83 int len;
84 int i;
86 /* Adding command and it's arguments. */
87 for( i = 0, len = 0; i < argc; i++ ) {
88 len += sprintf( tx_buffer + len, "%s", argv[i] );
89 if( i != ( argc - 1 ) ) {
90 len += sprintf( tx_buffer + len, " " );
94 if( wmnotify_infos.debug ) {
95 tx_buffer[len] = '\0';
96 printf( "Command: \"%s\"\n", tx_buffer );
99 /* Adding termination characters. */
100 len += sprintf( tx_buffer + len, POP3_ENDL );
102 len = WmnotifySendData( tx_buffer, len );
103 if( len < 0 ) {
104 return EXIT_FAILURE;
107 len = POP3_ReceiveResponse();
108 if( len < 0 ) {
109 return EXIT_FAILURE;
112 return EXIT_SUCCESS;
116 /* Return the number of new messages on success, -1 on error. */
117 static int
118 POP3_ParseStatCommand( void )
120 int new_messages;
121 char *token;
123 /* STAT command:
124 * The positive response consists of "+OK" followed by a single space, the number of messages
125 * in the maildrop, a single space, and the size of the maildrop in octets. */
126 token = strtok( rx_buffer, " " );
127 token = strtok( NULL, " " );
128 if( token != NULL ) {
129 /* Do more checks for digits... */
130 new_messages = atoi( token );
132 else {
133 fprintf( stderr, "%s: Error parsing \"STAT\" response", PACKAGE );
134 new_messages = -1;
137 return new_messages;
142 POP3_CheckForNewMail( void )
144 int status;
145 int new_messages = -1;
146 char *argv[10];
148 status = ConnectionEstablish( wmnotify_infos.server_name, wmnotify_infos.port );
149 if( status != EXIT_SUCCESS ) {
150 return -1;
153 /* Sending username. */
154 argv[0] = POP3_CMD_USERNAME;
155 argv[1] = wmnotify_infos.username;
156 status = POP3_SendCommand( 2, argv );
157 if( status != EXIT_SUCCESS ) {
158 goto pop3_close_connection;
161 /* Sending password. */
162 argv[0] = POP3_CMD_PASSWORD;
163 argv[1] = wmnotify_infos.password;
164 status = POP3_SendCommand( 2, argv );
165 if( status != EXIT_SUCCESS ) {
166 goto pop3_close_connection;
169 /* Sending STAT command to inquiry about new messages. */
170 argv[0] = POP3_CMD_STAT;
171 status = POP3_SendCommand( 1, argv );
172 if( status != EXIT_SUCCESS ) {
173 goto pop3_close_connection;
176 /* Parsing STAT command. */
177 new_messages = POP3_ParseStatCommand();
178 if( new_messages < 0 ) {
179 goto pop3_close_connection;
182 /* Sending QUIT command. */
183 argv[0] = POP3_CMD_QUIT;
184 status = POP3_SendCommand( 1, argv );
185 if( status != EXIT_SUCCESS ) {
186 new_messages = -1;
187 goto pop3_close_connection;
190 pop3_close_connection:
191 status = ConnectionTerminate();
192 if( status != EXIT_SUCCESS ) {
193 new_messages = -1;
196 return new_messages;