git.y1.nz

fbui

Framebuffer-based graphical environment
download: https://git.y1.nz/archives/fbui.tar.gz
README | Files | Log | Refs

libfbui/MailCheck/check.c

      1 #include <stdio.h>
      2 #include <sys/types.h>
      3 #include <sys/socket.h>
      4 #include <netdb.h>
      5 #include <netinet/in.h>
      6 #include <arpa/nameser.h>
      7 #include <resolv.h>
      8 
      9 #define PORTNUM 110
     10 
     11 typedef unsigned char uchar;
     12 
     13 static int
     14 getline (int fd, char *buf, int buflen)
     15 {
     16 	int got;
     17 	char ch;
     18 	int ix=0;
     19 
     20 	buf[0] = 0;
     21 
     22 	while (ix < (buflen-1)) {
     23 		got = recv (fd, &ch, 1, 0);
     24 
     25 		if (-1 == got && !ix) {
     26 			perror ("recv");
     27 			return -1;
     28 		}
     29 
     30 		if (!got)
     31 			continue;
     32 
     33 		if (ch == '\n')
     34 			break;
     35 		if (ch == '\r') 
     36 			continue;
     37 
     38 		buf [ix++] = ch;
     39 	}
     40 
     41 	buf [ix] = 0;
     42 	return ix;
     43 }
     44 
     45 
     46 int
     47 checkmail (char *server, char *username, char *password)
     48 {
     49 	int fd, result;
     50 	char buf[256];
     51 	char *tmp;
     52 	int total=0;
     53 	int tries;
     54 
     55 	fd = socket(PF_INET, SOCK_STREAM, 0);
     56 	if (fd == -1) {
     57 		perror("socket");
     58 		return -1;
     59 	}
     60 
     61 	char *s = strchr (server, '\n');
     62 	if (s) *s=0;
     63 	s = strchr (server, '\r');
     64 	if (s) *s=0;
     65 	s = strchr (username, '\n');
     66 	if (s) *s=0;
     67 	s = strchr (username, '\r');
     68 	if (s) *s=0;
     69 	s = strchr (password, '\n');
     70 	if (s) *s=0;
     71 	s = strchr (password, '\r');
     72 	if (s) *s=0;
     73 
     74 	struct hostent* host;
     75 	host = gethostbyname (server);
     76 	if (!host) {
     77 		perror ("gethostbyname");
     78 		close(fd);
     79 		return -1;
     80 	}
     81 
     82 	struct sockaddr_in sockaddr;
     83 	memset (&sockaddr, 0, sizeof (struct sockaddr_in));
     84 	sockaddr.sin_family = AF_INET;
     85 	sockaddr.sin_port = htons(PORTNUM);
     86 	bcopy(host->h_addr, &sockaddr.sin_addr, host->h_length);
     87 
     88 	result = connect (fd, (struct sockaddr*) &sockaddr, 
     89 			sizeof(struct sockaddr));
     90 	if (result) {
     91 		perror ("connect");
     92 		close(fd);
     93 		return -1;
     94 	}
     95 
     96 	if (-1 == getline(fd, buf, 256)) {
     97 		close(fd);
     98 		return -1;
     99 	}
    100 
    101 	sprintf (buf, "user %s\n", username);
    102 	if (0 > write (fd, buf, strlen(buf))) {
    103 		perror ("write");
    104 		close(fd);
    105 		return -1;
    106 	}
    107 	
    108 	if (-1 == getline(fd, buf, 256)) {
    109 		close(fd);
    110 		return -1;
    111 	}
    112 
    113 	sprintf (buf, "pass %s\n", password);
    114 	if (0 > write (fd, buf, strlen(buf))) {
    115 		perror ("write");
    116 		close(fd);
    117 		return -1;
    118 	}
    119 
    120 	if (-1 == getline(fd, buf, 256)) {
    121 		close(fd);
    122 		return -1;
    123 	}
    124 
    125 	sprintf (buf, "list\n");
    126 	if (0 > write (fd, buf, strlen(buf))) {
    127 		perror ("write");
    128 		close(fd);
    129 		return -1;
    130 	}
    131 
    132 	if (-1 == getline(fd, buf, 256)) {
    133 		close(fd);
    134 		return -1;
    135 	}
    136 
    137 	if (!strncmp ("+OK", buf, 3)) {
    138 
    139 		/* the next line has a count */
    140 		if (-1 == getline(fd, buf, 256)) {
    141 			close(fd);
    142 			return -1;
    143 		}
    144 
    145 		total = atoi(buf);
    146 		printf ("you have %d message(s).\n", total);
    147 	}
    148 	else
    149 	{
    150 		printf ("response not +OK: (%s)\n",buf);
    151 	}
    152 	
    153 	sprintf (buf, "quit\n");
    154 	if (0 > write (fd, buf, strlen(buf))) {
    155 		perror ("write");
    156 		close(fd);
    157 		return -1;
    158 	}
    159 
    160 	close(fd);
    161 	return total;
    162 }
    163 
    164 

This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.