shithub: xmpp

ref: 458a91724a672b45602ef8cbd6093b6f8c76fef6
dir: /misc.c/

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

extern Biobuf kbin;

char*
strtime(void)
{
	static char buf[6];
	Tm *tm;

	tm = localtime(time(nil));
	sprint(buf, "%02d:%02d", tm->hour, tm->min);
	return buf;
}

char*
strenttime(char **tzo)
{
	static char buf[32];
	Tm *tm;
	int n;
	long t;

	t = time(nil);
	tm = gmtime(t);
	n = sprint(buf, "%04d-%02d-%02dT%02d:%02d:%02dZ",
		tm->year+1900,
		tm->mon+1,
		tm->mday,
		tm->hour,
		tm->min,
		tm->sec);
	*tzo = &buf[n+1];
	tm = localtime(t);
	sprint(*tzo, "%+03d:%02d", tm->tzoff/3600, abs(tm->tzoff/60)%60);
	return buf;
}

void
cleaninput(int n)
{
	static char d[] = {8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8};

	while(n > 0)
		n -= write(1, d, sizeof(d) > n ? n : sizeof(d));
}

void
setlabel(char *label, char *prsuffix)
{
	static char *oldlabel;
	int fd;

	if(oldlabel == nil){
		oldlabel = mallocz(64, 1);
		fd = open("/dev/label", OREAD);
		read(fd, oldlabel, 63);
		close(fd);
	}

	fd = open("/dev/label", OWRITE);
	if(label != nil){
		if(label[0] == 0){
			fprint(fd, "xmpp");
			print("no target\n");
		}else{
			fprint(fd, "%s", label);
			if(prsuffix != nil && prsuffix[0] != 0)
				print("←→ %s [%s]\n", label, prsuffix);
			else
				print("←→ %s\n", label);
		}
	}else
		fprint(fd, "%s", oldlabel);
	close(fd);
}

char*
strstamp(char *v)
{
	static char buf[32];
	Tm utc, *tm;
	int i, len;

	len = strlen(v);
	for(i = 0; i < len && (isdigit(v[i]) || v[i] == '.') ; i++);
	if(i == len){ /* 1456830231.000345 this is Slack being a bag of shit */
		tm = localtime(atol(v));
		sprint(buf, "%02d:%02d", tm->hour, tm->min);
		return buf;
	}

	if(len < 17) /* can't parse, just use the current time */
		return strtime();

	/* 20130327T16:28:58 http://xmpp.org/extensions/xep-0091.html
	 * or
	 * 2016-02-25T10:00:15.400Z http://xmpp.org/extensions/xep-0203.html
	 */
	for(i = 0; i < len; i++)
		if(isdigit(v[i]))
			v[i] -= '0';

	memset(&utc, 0, sizeof(utc));
	utc.year = v[0]*1000+v[1]*100+v[2]*10+v[3] - 1900;
	if(v[4] == '-' && v[7] == '-'){
		utc.mon = v[5]*10+v[6] - 1;
		utc.mday = v[8]*10+v[9];
		i = 2;
	}else{
		utc.mon = v[4]*10+v[5] - 1;
		utc.mday = v[6]*10+v[7];
		i = 0;
	}
	utc.hour = v[9+i]*10+v[10+i];
	utc.min = v[12+i]*10+v[13+i];
	utc.zone[0] = 'G';
	utc.zone[1] = 'M';
	utc.zone[2] = 'T';

	tm = localtime(tm2sec(&utc));
	sprint(buf, "%04d/%02d/%02d %02d:%02d",
		tm->year+1900,
		tm->mon+1,
		tm->mday,
		tm->hour,
		tm->min);

	memmove(&utc, tm, sizeof(utc));
	tm = localtime(time(nil));
	if(tm->year != utc.year || tm->mon != utc.mon || tm->mday != utc.mday)
		return buf;

	return buf+11;
}

char *
readlines(void)
{
	char *str, *s;
	int len, catlen, total;

	str = strdup("");
	len = total = 0;
	for(; s = Brdstr(&kbin, '\n', 0);){
		catlen = strlen(s);
		total += utflen(s);
		str = realloc(str, len + catlen + 1);
		memcpy(&str[len], s, catlen + 1);
		free(s);
		len += catlen;
		if(len >= 2 && str[len-2] == '.' && (len < 3 || str[len-3] == '\n')){
			str[len - (len > 2 ? 3 : 2)] = 0;
			break;
		}
	}
	cleaninput(total);
	return str;
}