shithub: toe

ref: 07d85c76c7fd2529b053282f0037455a6e0ce51f
dir: /toe.c/

View raw version
#include <u.h>
#include <libc.h>
#include <bio.h>

void
visit(char *domain, char *user)
{
	char *naddr;
	char *line;
	int fd;
	Biobuf out, body;

	naddr = netmkaddr(domain, "tcp", "79");
	fd = dial(naddr, 0, 0, 0);
	if(fd < 0){
		print("unable to connect to %s: %r", domain);
		return;
	}

	if (user == nil) {
		fprint(fd, "\r\n");
	} else {
		fprint(fd, "%s\r\n", user);
	}
	
	Binit(&body, fd, OREAD);
	Binit(&out, 1, OWRITE);

	while((line = Brdstr(&body, '\n', 0)) != nil){
		Bprint(&out, "%s", line);
		free(line);
	}
	
	Bflush(&out);
	Bflush(&body);
	close(fd);
}

void
main(int argc, char **argv)
{
	char *user, *domain, *query;
	
	if( argc == 2 ) {
		query = argv[1];

		if (query[0] == '@') {
			user = nil;
			domain = query + 1;	
		} else if (strstr(argv[1], "@") == nil) {
			user = argv[1];
			domain = "localhost";
		} else {
			user = strtok(argv[1], "@");
			domain = strtok(nil, "@");
		}
				
		visit(domain, user);
		exits(0);
   	}
   	else if( argc > 2 ) {
      	print("Too many arguments supplied.\n");
   	}
   	else {
      	print("Usage: toe [user]@server.\n");
   	}	
}