# include gnu.gpl, (*) 2000 Micha 'malekith' Moskal <malekith@topnet.pl>. 
# $Id: parse,v 1.2 2000/08/27 18:32:51 malekith Exp $ 

proc do_notice(string chan, string blurb)
{
	if (windows[irc_tolower(chan)])
		puts(chan, `- \e<chan_start>$input{nick}`
			`\e<chan_end> - $blurb`);
	else
		puts("", `- \e<chan_start>$input{nick}:$chan`
			`\e<chan_end> - $blurb`);
}

proc do_privmsg(string chan, string blurb, string who)
{
	string n;

	if (windows[irc_tolower(chan)])
		n = input{nick};
	else
		n = input{nick} + ":" + chan;
		
	if (same(chan, nick)) {
		if (blurb @ /^\cAversion\cA$/i) {
			puts("", "\e<ctcp> " + who + " requests version");
			put_server(`NOTICE $input{nick} `
				`:\cAVERSION schiza - the alternative `
				`/ by malekith\cA`);
		} else if (windows[irc_tolower(input{nick})])
			puts(input{nick}, `\e<chan_start>$input{nick}`
					`\e<chan_end> $blurb`);
		else
			puts(input{nick}, `\e<msg_start>$input{nick}`
				`\e<msg_user>$input{user}`
				`\e<msg_host>$input{host}`
				`\e<msg_end> $blurb`);
	} else if (blurb @ /^\cAaction (.*)\cA$/i)
		puts(chan, `\e<me_start>$n\e<me_end> ` + $1);
	else
		puts(chan, `\e<chan_start>$n\e<chan_end> $blurb`);
}

proc do_nick(string old, string new)
{
	if (old == nick) {
		puts("", `\e<nick> You ($nick) are now known as \cB$new\cB`);
		nick = new;
	} else
		puts("", `\e<nick> $old is now known as \cB$new\cB`);
	# change nick hashes here ...
}

proc do_join(string who, string ch)
{
	puts(ch, `\e<joins> \cB$who\cB ($input{user}@$input{host}) `
		`joined \cB$ch\cB.`);
	# add_user()
}

proc do_quit(string who, string reason)
{
	puts("", `\e<parts> \cB$who\cB ($input{user}@$input{host}) `
		`left \cBIRC\cB. [$reason]`);
	#del_user()
}

proc do_part(string who, string ch, string reason)
{
	puts(ch, `\e<parts> \cB$who\cB ($input{user}@$input{host}) `
		`left \cB$ch\cB. [$reason]`);
	# del_user()
}

proc do_mode(string who, string ch, string m)
{
	string o = `\e<mode> mode\e{gray}/\e{norm}$ch \e{gray}[\e{white}$m`;
	int n;

	for (n = 3; input["argv" + itoa(n)] != ""; n++)
		o += " " + input["argv" + itoa(n)];
	o += `\e{gray}]\e{norm} by \e{white}$who\e{norm}`;
	puts(ch, o);
	# change users modes ...
}

proc x_subst(string pat)
{
	int i, n, pl;
	string r;

	for (i = 0; i < strlen(pat); i++) {
		if (pat[i] == '%') {
			i++;
			if (pat[i] == '+') {
				i++;
				pl = 1;
			} else
				pl = 0;
			if (pat[i] >= '0' && pat[i] <= '9')
				if (pl)
					r += plus[itoa(pat[i] - '0')];
				else
					r += input["argv" + itoa(pat[i] - '0')];
		} else
			r += str(pat[i]);
	}
	return r;
}


proc fill(string x, int len, string c)
{
	int a = 0;
	
	if (len < 0) {
		len = -len;
		a = 1;
	}
	
	len -= strlen(x);
	while (len-- > 0)
		if (a)
			x = c + x;
		else
			x += c;
	return x;
}

proc do_who(string chan, string user, string host, string nick, 
	    string stat, string rn)
{
	if (rn @ /^\d+ (.*)/)
		rn = $1;
	puts(chan, fill(chan, 10, " ") + " " + fill(stat, 3, "-") + " " +
		fill(nick, 10, " ") +
	"\e<uhost1>" + user + "\e<uhost2>" + host + "\e<uhost3> : " +
	rn);
}

proc do_numeric(int n)
{
	int i;
	string o;

	if (num[n] != "") {
		puts("", x_subst(num[n]));
		return;
	}
	
	if (n == 352) {
		do_who(input{argv2},input{argv3},input{argv4},input{argv6},
			input{argv7}, input{argv8});
		return;
	} else if (n == 1) 
		put_server(`MODE $nick +iw`);
	else if (n == 433) {
		string alt;
		if (!(nick @ /_$/)) alt = nick + "_";
		else if (!(nick @ /^_/)) alt = "_" + nick;
		else alt = randstring("1234567890qwertyuiopasdfghjklzxcvbnm{}|",6);
		
		puts("", `Your nick ($nick) is in use. Using $alt.`);
		nick = alt;
		put_server(`NICK $alt`);
	}
	
	o = "\e<numeric_start>";
	o += input{command} + "\e<numeric_end>";
	
	if (!same(input{argv1},nick))
		o += " " + input{argv1};
		
	for (i = 2; input["argv" + itoa(i)] != ""; i++)
		o += " " + input["argv" + itoa(i)];
	puts("", o);	
}

proc set_plus()
{
	int i, j;
	string s;
	
	for (i = 1; i < 19; i++) {
		s = "";
		
		for (j = i; j < 19; j++) {
			if (input["argv" + itoa(j)] == "")
				break;
			if (j != i)
				s += " ";
			s += input["argv" + itoa(j)]; 
		}
		plus[itoa(i)] = s;
	}
}

proc do_kick(string n, string chan, string r, string who)
{
	puts(chan, `\e<kick> \cB$n\cB has been kicked off \cB$chan\cB by $who`
		` [$r]`);
	#del_user();
}

proc got_input()
{
	string c = toupper(input{command}),
		who = "\cB" + input{nick} + "\cB \e<uhost1>" + 
		input{user} + "\e<uhost2>" + input{host} +
		"\e<uhost3>";

	set_plus();
	
	if (c == "PING") 
		put_server(`PONG $input{argv1}`);
	else if (c == "PRIVMSG")
		do_privmsg(input{argv1}, input{argv2}, who);
	else if (c == "NICK")
		do_nick(input{nick}, input{argv1});
	else if (c == "JOIN")
		do_join(input{nick}, input{argv1});
	else if (c == "PART")
		do_part(input{nick}, input{argv1}, input{argv2});
	else if (c == "TOPIC")
		puts(cmd{argv1}, `\e<topic> Topic \e{white}@\e{norm} `
				`$input{argv1} set by $input{nick}\e{gray}:`
				`\e{cyan} $input{argv2}`);
	else if (c == "MODE")
		do_mode(input{nick}, input{argv1}, input{argv2});
	else if (c == "QUIT")
		do_quit(input{nick}, input{argv1});
	else if (c == "NOTICE")
		do_notice(input{argv1}, input{argv2});
	else if (c == "KICK")
		do_kick(input{argv2}, input{argv1}, input{argv3}, who);
	else if (input{numeric} != 0)
		do_numeric(input{numeric});
	else
info(`$input{command} $input{argv1} $input{argv2} $input{argv3} $input{argv4}`);
}
