|
int num_msgs()
{
struct in_addr *sin;
char recv[1024];
char command[256];
int fd;
int num = 0;
int step = 0;
int len;
sin = (struct in_addr *)get_address(mailhost);
fd = connect_address(sin->s_addr, mailport);
while ((len = read(fd, recv, 1023))>0) {
recv[len] = 0;
if (!strncmp(recv, "-ERR", strlen("-ERR"))) {
step = 4;
break;
} else if (!strncmp(recv, "+OK", strlen("+OK"))) {
if (step == 3) {
if (sscanf(recv, "+OK %d %d\n", &num, &step) != 2)
break;
g_snprintf(command, sizeof(command), "QUIT\n");
write(fd, command, strlen(command));
close(fd);
return num;
}
if (step == 0) {
g_snprintf(command, sizeof(command), "USER %s\n", username);
write(fd, command, strlen(command));
step = 1;
} else if (step == 1) {
g_snprintf(command, sizeof(command), "PASS %s\n", password);
write(fd, command, strlen(command));
step = 2;
} else if (step == 2) {
g_snprintf(command, sizeof(command), "STAT\n");
write(fd, command, strlen(command));
step = 3;
}
}
}
close(fd);
return 0;
|