Plan 9 from Bell Labs’s /usr/web/sources/contrib/anothy/src/cmd/prowl.c

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


/* Send messages to Prowl (prowl.weks.net) to send push messages to iOS devics */

#include <u.h>
#include <libc.h>

void
usage(void)
{
	fprint(2, "usage: prowl [-e event] [-k keyfile] [file...]\n");
	exits("usage");
}

/* url encoding lifted from /n/sources/contrib/maht/url_encode.c, 2010-10-02 */

static int url_trans_flag[] = {
	[32] 0,	[33] 0,	[34] 0,	[35] 0,	[36] 0,	[37] 0,	[38] 0,	[39] 0,
	[40] 0,	[41] 0,	[42] 0,	[43] 0,	[44] 0,	[45] '-',	[46] '.',	[47] 0,
	[48] '0',	[49] '1',	[50] '2',	[51] '3',	[52] '4',	[53] '5',	[54] '6',	[55] '7',
	[56] '8',	[57] '9',	[58] 0,	[59] 0,	[60] 0,	[61] 0,	[62] 0,	[63] 0,
	[64] 0,	[65] 'A',	[66] 'B',	[67] 'C',	[68] 'D',	[69] 'E',	[70] 'F',	[71] 'G',
	[72] 'H',	[73] 'I',	[74] 'J',	[75] 'K',	[76] 'L',	[77] 'M',	[78] 'N',	[79] 'O',
	[80] 'P',	[81] 'Q',	[82] 'R',	[83] 'S',	[84] 'T',	[85] 'U',	[86] 'V',	[87] 'W',
	[88] 'X',	[89] 'Y',	[90] 'Z',	[91] 0,	[92] 0,	[93] 0,	[94] 0,	[95] '_',
	[96] 0,	[97] 'a',	[98] 'b',	[99] 'c',	[100] 'd',	[101] 'e',	[102] 'f',	[103] 'g',
	[104] 'h',	[105] 'i',	[106] 'j',	[107] 'k',	[108] 'l',	[109] 'm',	[110] 'n',	[111] 'o',
	[112] 'p',	[113] 'q',	[114] 'r',	[115] 's',	[116] 't',	[117] 'u',	[118] 'v',	[119] 'w',
	[120] 'x',	[121] 'y',	[122] 'z',	[123] 0,	[124] 0,	[125] 0,	[126] 0,	[127] 0
};

int
url_encode(Fmt *fmt)
{
	int cnt;
	uchar c, e;
	char *str_start;
	char *str_index;

	str_start = str_index= smprint("%s", va_arg(fmt->args, char *));
	if (fmt->flags & FmtSign)
		url_trans_flag[32] = '+';
	else
		url_trans_flag[32] = 0;

	cnt = 0;
	while(c = *(str_index++)) {
		if (c > 127 || c < 32) {
			cnt += fmtprint(fmt, "%%%02x", c);
			continue;
		}

		e = url_trans_flag[c];
		if(e)
			cnt += fmtprint(fmt, "%c", e);
		else
			cnt += fmtprint(fmt, "%%%02x", c);
	}
	free(str_start);
	return cnt;
}


void
main(int argc, char *argv[])
{
	char *apibase = "https://prowl.weks.net/publicapi/";
	char *application = "p9prowl";
	char *apikey[40];
	char *event[1024];
	char *description[10000];
	char *keyfile[1000] = smprint("/usr/%s/lib/prowl", getuser());
	int keyf;
	long n;

	fmtinstall('R', url_encode);

	ARGBEGIN {
		case 'e':
			*event = smprint("%R", EARGF(usage()));
			break;
		case 'k':
			*keyfile = EARGF(usage());
			break;
		default:
			usage();
	} ARGEND

	keyf = open(*keyfile, OREAD);
	if(keyf < 0) {
		fprint(2, "open %s: %r\n", *keyfile);
		exits("keyfile");
	}
	read(keyf, apikey, 40);
	close(keyf);

	n=readn(0, description, 10000);
	if(n < 0)
		sysfatal("error reading <stdin>: %r");

	fprint(1, "%s/add?application=%s&apikey=%s&event=%s&description=%R\n",
		apibase, application, apikey, (*event="") ? "" : *event, description);
}

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to [email protected].