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 */
31 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <netinet/in.h>
43 /* Defined in network.c */
44 extern char tx_buffer
[WMNOTIFY_BUFSIZE
+ 1];
45 extern char rx_buffer
[WMNOTIFY_BUFSIZE
+ 1];
49 POP3_ReceiveResponse( void )
53 len
= WmnotifyGetResponse( rx_buffer
, WMNOTIFY_BUFSIZE
);
56 ErrorLocation( __FILE__
, __LINE__
);
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
);
81 POP3_SendCommand( int argc
, char *argv
[] )
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
);
107 len
= POP3_ReceiveResponse();
116 /* Return the number of new messages on success, -1 on error. */
118 POP3_ParseStatCommand( void )
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
);
133 fprintf( stderr
, "%s: Error parsing \"STAT\" response", PACKAGE
);
142 POP3_CheckForNewMail( void )
145 int new_messages
= -1;
148 status
= ConnectionEstablish( wmnotify_infos
.server_name
, wmnotify_infos
.port
);
149 if( status
!= EXIT_SUCCESS
) {
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
) {
187 goto pop3_close_connection
;
190 pop3_close_connection
:
191 status
= ConnectionTerminate();
192 if( status
!= EXIT_SUCCESS
) {