shithub: castor9

ref: 5a55f1448d2004d6cb54cbd34172ec3b9925715d
dir: /castor.c/

View raw version
#include <u.h>
#include <libc.h>
#include <libsec.h>
#include <String.h>
#include <regexp.h>
#include <draw.h>
#include <event.h>
#include <keyboard.h>
#include <panel.h>
#include <bio.h>
#include <stdio.h>
#include <ctype.h>
#include <plumb.h>
#include "castor.h"


typedef struct Ctx Ctx;
typedef struct Hist Hist;
typedef struct Response Response;

struct Response
{
	Url *url;
	char *meta;
	int status;
	int fd;
};

struct Ctx
{
	Url *url;
	Rtext *text;
};

struct Hist
{
	Hist *p;
	Hist *n;
	Ctx *c;
};

int request(Url *u);
void gemini_get(Url *u);
void texthit(Panel *p, int b, Rtext *t);
void message(char *s, ...);

Panel *root;
Panel *backp;
Panel *fwdp;
Panel *entryp;
Panel *urlp;
Panel *textp;
Panel *statusp;
Panel *popup;
Url *current_base_url;
Mouse *mouse;
Hist *hist = nil;
int preformatted = 0;

enum
{
	Mback,
	Mforward,
	Msearch,
	Mexit,
};

char *menu3[] = {
	"back",
	"forward",
	"search",
	"exit",
	0
};


char* 
replace_char(char* str, char find, char replace){
    char *current_pos = strchr(str,find);
    while(current_pos){
        *current_pos = replace;
        current_pos = strchr(current_pos,find);
    }
    return str;
}

char*
cleanup(char *line)
{
	if(line=="" || line==NULL)
		return line;

	char *src, *dst;
    for(src=dst=line; *src != '\0'; src++){
        *dst = *src;
        if(*dst != '\r' && *dst != '\n') 
			dst++;
    }
    *dst = '\0';
	
	replace_char(line, '\t', ' ');
	return line;
}

void
set_current_base_url(Url *url)
{
	freeurl(current_base_url);
	current_base_url = url;
}

void
show(Ctx *c)
{
	plinittextview(textp, PACKE|EXPAND, ZP, c->text, texthit);
	pldraw(textp, screen);
	plinitlabel(urlp, PACKN|FILLX, c->url->raw);
	pldraw(urlp, screen);
	message("Castor9");
}

void
plumburl(Url *u)
{
	int fd;

	fd = plumbopen("send", OWRITE|OCEXEC);
	if(fd<0)
		return;
	plumbsendtext(fd, "castor9", nil, nil, u->raw);
	close(fd);
	freeurl(u);
}

void
page(Url *u)
{
	int fd;
	char tmp[32] = "/tmp/castor9XXXXXXXXXXX", *cmd;

	fd = request(u);
	if(fd < 0)
		sysfatal("dial: %r");

	fprint(fd, "%s\r\n", u->raw);
	
	switch(rfork(RFFDG|RFPROC|RFMEM|RFREND|RFNOWAIT|RFNOTEG)){
	case -1:
		fprint(2, "Can't fork!");
		break;
	case 0:
		mktemp(tmp);
		cmd = smprint("tail -n +2 >%s >[2]/dev/null; page -w %s; rm %s", tmp, tmp, tmp);
		dup(fd, 0);
		close(fd);
		execl("/bin/rc", "rc", "-c", cmd, nil);
	}
}

char*
protocol(char *link)
{
	if(strstr(link, "http://") != nil){
		return " [WWW]";
	}else if(strstr(link, "https://") != nil){
		return " [WWW]";
	}else if(strstr(link, "gopher://") != nil){
		return " [GOPHER]";
	}else if(strstr(link, "finger://") != nil){
		return " [FINGER]";
	}else{
		return "";
	}
}

char*
symbol(char *link)
{
	if(strstr(link, "http://") != nil){
		return "⇄";
	}else if(strstr(link, "https://") != nil){
		return "⇄";
	}else if(strstr(link, "gopher://") != nil){
		return "⇒";
	}else if(strstr(link, "finger://") != nil){
		return "⇒";
	}else{
		return "→";
	}
}

void
handle_status(char *status, Response *r)
{
	int code;
	char *meta;
	code = atoi(strtok(status, " \t"));
	if(code == 0)
		message("STATUS: %s\n", status);
	meta = strtok(NULL, "\n");
	r->status = code;
	r->meta = cleanup(meta);
}

void
render_text(Ctx *c, char *line)
{
	char *base, *right_margin;
    int length, width;

    length = strlen(strdup(line));
    base = strdup(line);
    width = 80;

	char *preformatted_marker = "```";
	if(strbeg(line, preformatted_marker) == 0){
		if(preformatted==0){
			preformatted=1;
		}else{
			preformatted=0;
		}
		return;
	}

	while(*base){
		/* Preformatted text */
		if(preformatted==1){
			plrtstr(&c->text, 1000000, 8, 0, font, strdup(cleanup(base)), PL_HEAD, 0);
			break;
		}
		/* Headers */
		if(strbeg(line, "#") == 0){
			plrtstr(&c->text, 1000000, 8, 0, font, strdup(cleanup(base)), PL_SEL, 0);
			break;
        } /* Small lines */		
		if((length <= width)){
			plrtstr(&c->text, 1000000, 8, 0, font, strdup(cleanup(base)), 0, 0);
			break;
        }
		
		/* Wrapping the rest */
		right_margin = base + width;
		while(!isspace(*right_margin)){
			right_margin--;
			if(right_margin == base){
				right_margin += width;
				while(!isspace(*right_margin)){
					if(*right_margin == '\0')
						break;
					right_margin++;
				}
			}
		}
		*right_margin = '\0';
		plrtstr(&c->text, 1000000, 8, 0, font, strdup(cleanup(base)), 0, 0);
		length -= right_margin - base + 1; /* +1 for the space */
		base = right_margin + 1;
	}
}

void
render_link(Ctx *c, char *line)
{
	char *copy = strdup(cleanup(line + 2)); /* bypass => */
	char *link = strtok(copy, " ");
	char *rest = strtok(NULL, "\0");
	char *label;

	if(rest != NULL){
		while(isspace(*rest))
 			rest++;

		label = smprint("%s %s%s", symbol(link), rest, protocol(link));
	}else{
		label = smprint("%s %s%s", symbol(link), link, protocol(link));
	}

	plrtstr(&c->text, 1000000, 8, 0, font, strdup(label), PL_HOT, estrdup(link));
}

Url*
base_url(Url *url)
{
	char *base_url, *path, *ptr;
	
	if(url->path == "/" || url->path == NULL){
		path = "/";
	}else{
		path = estrdup(url->path);
		ptr = strrchr(path, '/');
		if(path[strlen(path)-1] != '/')
			strcpy(ptr, "/");
	}
	base_url = smprint("gemini://%s%s", url->host, path);

	return urlparse(nil, base_url);
}

int
request(Url *url)
{
	Thumbprint *th;
	TLSconn conn;
	int fd;
	char *port;

	if(url->port == NULL){
		port = "1965";
	}else{
		port = url->port;
	}
	char *naddr = netmkaddr(url->host, "tcp", port);
	fd = dial(naddr, 0, 0, 0);
	if(fd < 0){
		message("unable to connect to %s:%s: %r", url->host, url->port);
		return -1;
	}

	conn.serverName = url->host;
	memset(&conn, 0, sizeof(conn));

	fd = tlsClient(fd, &conn);
	th = initThumbprints("/sys/lib/ssl/gemini", nil, "x509");

	if(th != nil){
		okCertificate(conn.cert, conn.certlen, th);
		freeThumbprints(th);
		free(conn.cert);
	}

	return fd;
}

void
gemini_get(Url *url)
{
	int fd;
	char *line;
	Biobuf body;

	Ctx *c;
	c = malloc(sizeof *c);
	if(c==nil)
		sysfatal("malloc: %r");
	c->text = nil;

	Response *r;
	r = malloc(sizeof *r);
	if(r == nil)
		sysfatal("malloc: %r");
	r->url = url;

	Hist *h;
	h = malloc(sizeof *h);
	if(h == nil)
		sysfatal("malloc: %r");

	plrtstr(&c->text, 1000000, 0, 0, font, strdup(" "), 0, 0);

	message("loading %s...", url->raw);

	fd = request(url);
	fprint(fd, "%s\r\n", url->raw);
	Binit(&body, fd, OREAD);

	char *status = Brdstr(&body, '\n', '0');
	handle_status(status, r);

	if(r->status == 20){
		c->url = url;
		set_current_base_url(base_url(url));

		if(r->meta != NULL && strbeg(r->meta, "text/") != 0){
			Bflush(&body);
			close(fd);

			page(url);
			message("Castor9");
		}else{
			while((line = Brdstr(&body, '\n', 0)) != nil){
				if(strbeg(line, "=>") == 0){
					render_link(c, line);					
				}else{
					render_text(c, line);
				}
				free(line);
			}

			Bflush(&body);
			close(fd);

			h->p = hist;
			h->n = nil;
			h->c = c;
			hist = h;

			show(c);
		}
	}else if(r->status == 31){
		Url *redirect = urlparse(nil, r->meta);
		gemini_get(redirect);
	}
}

void
search(void)
{
	static char last[256];
	char buf[256];
	Reprog *re;
	Rtext *tp;
	int yoff;

	for(;;){
		if(hist == nil || hist->c == nil || hist->c->text == nil)
			return;
		strncpy(buf, last, sizeof(buf)-1);
		if(eenter("Search for", buf, sizeof(buf), mouse) <= 0)
			return;
		strncpy(last, buf, sizeof(buf)-1);
		re = regcompnl(buf);
		if(re == nil){
			message("%r");
			continue;
		}
		for(tp=hist->c->text;tp;tp=tp->next)
			if(tp->flags & PL_SEL)
				break;
		if(tp == nil)
			tp = hist->c->text;
		else {
			tp->flags &= ~PL_SEL;
			tp = tp->next;
		}
		while(tp != nil){
			tp->flags &= ~PL_SEL;
			if(tp->text && *tp->text)
			if(regexec(re, tp->text, nil, 0)){
				tp->flags |= PL_SEL;
				plsetpostextview(textp, tp->topy);
				break;
			}
			tp = tp->next;
		}
		free(re);
		yoff = plgetpostextview(textp);
		plinittextview(textp, PACKE|EXPAND, ZP, hist->c->text, texthit);
		plsetpostextview(textp, yoff);
		pldraw(textp, screen);
	}
}

void
backhit(Panel *p, int b)
{
	USED(p);
	if(b!=1)
		return;
	if(hist==nil || hist->p==nil)
		return;
	hist->p->n = hist;
	hist = hist->p;
	set_current_base_url(base_url(hist->c->url));
	show(hist->c);
}

void
nexthit(Panel *p, int b)
{
	USED(p);
	if(b!=1)
		return;
	if(hist==nil || hist->n==nil)
		return;
	hist = hist->n;
	set_current_base_url(base_url(hist->c->url));
	show(hist->c);
}

void
menuhit(int button, int item)
{
	USED(button);

	switch(item){
	case Mback:
		backhit(backp, 1);
		break;
	case Mforward:
		nexthit(fwdp, 1);
		break;
	case Msearch:
		search();
		break;
	case Mexit:
		exits(nil);
		break;
	}
}

void
entryhit(Panel *p, char *t)
{
	USED(p);
	if(strlen(t) == 0)
		return;

	if(strstr(t, "gemini://") == NULL)
		t = smprint("gemini://%s", t);
			
	gemini_get(urlparse(nil, t));
	
	plinitentry(entryp, PACKN|FILLX, 0, "", entryhit);
	pldraw(root, screen);
}

void
texthit(Panel *p, int b, Rtext *rt)
{
	char *n;
	Url *next_url;
	char *link = rt->user;

	USED(p);
	if(b!=1)
		return;
	if(link==nil)
		return;

	if(strstr(link, "gemini://") != nil || strstr(link, "://") != nil){
		next_url = urlparse(nil, link);
	}else{
		/* assuming relative URL */
		if(*link == '/'){
			n = smprint("%s%s", urlparse(current_base_url, link)->raw, estrdup(link)+1);
		}else{
			n = smprint("%s%s", urlparse(current_base_url, link)->raw, estrdup(link));
		}
		next_url = urlparse(nil, n);
	}
	
	if(strcmp(next_url->scheme, "gemini") == 0){
		gemini_get(next_url);
	}else{
		plumburl(next_url);
	}
}

void 
message(char *s, ...)
{
	static char buf[1024];
	char *out;
	va_list args;

	va_start(args, s);
	out = buf + vsnprint(buf, sizeof(buf), s, args);
	va_end(args);
	*out='\0';
	plinitlabel(statusp, PACKN|FILLX, buf);
	pldraw(statusp, screen);
	flushimage(display, 1);
}

void
mkpanels(void)
{
	Panel *p, *ybar, *xbar, *m;

	m = plmenu(0, 0, menu3, PACKN|FILLX, menuhit);
	root = plpopup(0, EXPAND, 0, 0, m);
	  p = plgroup(root, PACKN|FILLX);
	    statusp = pllabel(p, PACKN|FILLX, "Castor!");
	    plplacelabel(statusp, PLACEW);
	    pllabel(p, PACKW, "Go: ");
	    entryp = plentry(p, PACKN|FILLX, 0, "", entryhit);
	  p = plgroup(root, PACKN|FILLX);
	    urlp = pllabel(p, PACKN|FILLX, "");
	    plplacelabel(urlp, PLACEW);
	  p = plgroup(root, PACKN|EXPAND);
	    ybar = plscrollbar(p, PACKW|USERFL);
	    xbar = plscrollbar(p, IGNORE);
	    textp = pltextview(p, PACKE|EXPAND, ZP, nil, nil);
	    plscroll(textp, xbar, ybar);
	plgrabkb(entryp);
}

void
eresized(int new)
{
	if(new && getwindow(display, Refnone)<0)
		sysfatal("cannot reattach: %r");
	plpack(root, screen->r);
	pldraw(root, screen);
}

void scrolltext(int dy, int whence)
{
	Scroll s;

	s = plgetscroll(textp);
	switch(whence){
	case 0:
		s.pos.y = dy;
		break;
	case 1:
		s.pos.y += dy;
		break;
	case 2:
		s.pos.y = s.size.y+dy;
		break;
	}
	if(s.pos.y > s.size.y)
		s.pos.y = s.size.y;
	if(s.pos.y < 0)
		s.pos.y = 0;
	plsetscroll(textp, s);
	/* BUG: there is a redraw issue when scrolling
	   This fixes the issue albeit not properly */
	pldraw(textp, screen);
}

void
main(int argc, char *argv[])
{
	Event e;
	Url *url;
	
	if(argc == 2)
		if(strcmp(argv[1], "gemini://") != 0){
			url = urlparse(nil, smprint("gemini://%s", argv[1]));
		}else{
			url = urlparse(nil, argv[1]);
		}
	else
		url = urlparse(nil, "gemini://gemini.circumlunar.space/capcom/");

	quotefmtinstall();
	if(initdraw(nil, nil, "gemini")<0)
		sysfatal("initdraw: %r");
	einit(Emouse|Ekeyboard);
	plinit(screen->depth);
	mkpanels();
	gemini_get(url);
	eresized(0);
	for(;;){
		switch(event(&e)){
		case Ekeyboard:
			switch(e.kbdc){
			default:
				plgrabkb(entryp);
				plkeyboard(e.kbdc);
				break;
			case Khome:
				scrolltext(0, 0);
				break;
			case Kup:
				scrolltext(-textp->size.y/4, 1);
				break;
			case Kpgup:
				scrolltext(-textp->size.y/2, 1);
				break;
			case Kdown:
				scrolltext(textp->size.y/4, 1);
				break;
			case Kpgdown:
				scrolltext(textp->size.y/2, 1);
				break;
			case Kend:
				scrolltext(-textp->size.y, 2);
				break;
			case Kdel:
				exits(nil);
				break;
			}
			break;
		case Emouse:
			mouse = &e.mouse;
			if(mouse->buttons & (8|16) && ptinrect(mouse->xy, textp->r)){
				if(mouse->buttons & 8)
					scrolltext(textp->r.min.y - mouse->xy.y, 1);
				else
					scrolltext(mouse->xy.y - textp->r.min.y, 1);
				break;
			}
			plmouse(root, mouse);
			/* BUG: there is a redraw issue when scrolling
			   This fixes the issue albeit not properly */
			//pldraw(textp, screen);
			break;
		}
	}
}