/*  Copyright (C) 2002 Tom Parker (tom@carrott.org),
                       Matthias Münch (matthias@amigaworld.de)

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    In addition, as a special exception, Tom Parker and Matthias Münch give
    permission to link the code of this program with a TCP stack of your 
    choice, any official MUI libraries or classes and any custom MUI classes
    that should be necessary for the operation of this program.  This 
    exception also gives you permission to distribute linked combinations 
    including this software with any of the before-mentioned libraries and 
    classes.  You must obey the GNU General Public License in all respects for
    all of the code used other than that provided by the before-mentioned 
    libraries and classes.  As part of this exception you are obliged to 
    follow the license terms of the before-mentioned libraries, this license
    does not compel you to follow those terms, but if you do not then you may
    not link with those libraries.  If you modify this file, you may extend
    this exception to your version of the file, but you are not obligated to
    do so.  If you do not wish to do so, delete this exception statement from
    your version.
*/
/*
** ARexx support
*/

#include "common.h"

#include <proto/exec.h>
#include <proto/dos.h>
#include <proto/rexxsyslib.h>
#include <rexx/errors.h>
#include <rexx/storage.h>

static void rexx_str(char *stem, int num, char *attrib, char *value);
static void rexx_int(char *stem, int num, char *attrib, int value);

#define REXX_HOOK(x,y) \
	static MUI_HOOK_DECL( x , Object *app, char **array); \
	static struct Hook y = { {0,0}, & x , NULL, NULL }

REXX_HOOK(rexx_connect, conHook);
REXX_HOOK(rexx_disconnect, disHook);
REXX_HOOK(rexx_isonline, isoHook);
REXX_HOOK(rexx_getroster, rosHook);
REXX_HOOK(rexx_addcontact, addHook);
REXX_HOOK(rexx_sendmessage, msgHook);
REXX_HOOK(rexx_setpresence, prsHook);
REXX_HOOK(rexx_getuser, useHook);
REXX_HOOK(rexx_sendraw, rawHook);

struct MUI_Command rexx_cmds[] =
{
	{ "connect", "JID/K,PASSWORD/K", 2, &conHook },
	{ "disconnect", NULL, 0, &disHook },
	{ "isonline", NULL, 0, &isoHook },
	{ "getroster", "STEM/A", 1, &rosHook },
	{ "addcontact", "JID/A,NAME/K,GREET/K", 3, &addHook },
	{ "sendmessage", "TO/A,MSG/A", 2, &msgHook },
	{ "setpresence", "PRESENCE/A,NOTE/K", 2, &prsHook },
	{ "getuser", "STEM/A", 1, &useHook },
	{ "sendraw", "XML/A", 1, &rawHook },

	{ NULL, NULL, 0, NULL }
};


MUI_HOOK_STATIC(rexx_connect, Object *app, char **array)
{
	net.regflag = 0;
	net_connect();
	return 0;
}


MUI_HOOK_STATIC(rexx_disconnect, Object *app, char **array)
{
	net_disconnect("Connection closed.");
	return 0;
}


MUI_HOOK_STATIC(rexx_isonline, Object *app, char **array)
{
	if(net.state != NET_ON) return(0);
	return 1;
}


MUI_HOOK_STATIC(rexx_getroster, Object *app, char **array)
{
	int i;
	char *t;
	juser u;

	for(i=0;;i++)
	{
		u = roster_next(i);
		if(!u) break;

		rexx_str(array[0], i, "USER", u->id->user);
		rexx_str(array[0], i, "SERVER", u->id->server);
		rexx_str(array[0], i, "RESOURCE", u->id->resource);
		rexx_str(array[0], i, "NAME", u->name);
		rexx_str(array[0], i, "NOTE", u->status);

		switch(u->show)
		{
			case IKS_SHOW_UNAVAILABLE: t = "unavailable"; break;
			case IKS_SHOW_AVAILABLE: t = "available"; break;
			case IKS_SHOW_CHAT: t = "chat"; break;
			case IKS_SHOW_AWAY: t = "away"; break;
			case IKS_SHOW_XA: t = "xa"; break;
			case IKS_SHOW_DND: t = "dnd"; break;
			default: t = "?"; break;
		}
		rexx_str(array[0], i, "PRESENCE", t);
	}
	rexx_int(array[0], -1, "COUNT", i);

	return 0;
}


MUI_HOOK_STATIC(rexx_addcontact, Object *app, char **array)
{
	iks *x;

	if(net.state != NET_ON) return(1);

	x = iks_make_pres(IKS_TYPE_SUBSCRIBE, 0, array[0], convert_utf8(array[2]));
	iks_send(net.parser, x);
	iks_delete(x);

	return 0;
}


MUI_HOOK_STATIC(rexx_sendmessage, Object *app, char **array)
{
	iks *x;

	if(net.state != NET_ON) return(1);

	x = iks_make_msg(0, array[0], convert_utf8(array[1]), NULL);
	iks_send(net.parser, x);
	iks_delete(x);

	return 0;
}


MUI_HOOK_STATIC(rexx_setpresence, Object *app, char **array)
{
	iks *x;
	int show = IKS_SHOW_AVAILABLE, type = IKS_TYPE_AVAILABLE;

	if(net.state != NET_ON) return(1);

	if(strcmp(array[0], "chat") == 0)
		show = IKS_SHOW_CHAT;
	else if(strcmp(array[0], "away") == 0)
		show = IKS_SHOW_AWAY;
	else if(strcmp(array[0], "xa") == 0)
		show = IKS_SHOW_XA;
	else if(strcmp(array[0], "dnd") == 0)
		show = IKS_SHOW_DND;
	else if(strcmp(array[0], "unavailable") == 0)
		type = IKS_TYPE_UNAVAILABLE;

	x = iks_make_pres(type, show, NULL, convert_utf8(array[1]));
	iks_send(net.parser, x);
	iks_delete(x);

	return 0;
}


MUI_HOOK_STATIC(rexx_getuser, Object *app, char **array)
{
	rexx_str(array[0], -1, "USER", net.id->user);
	rexx_str(array[0], -1, "SERVER", net.id->server);
	rexx_str(array[0], -1, "RESOURCE", net.id->resource);

	return 0;
}


MUI_HOOK_STATIC(rexx_sendraw, Object *app, char **array)
{
	if(net.state != NET_ON) return(1);

	iks_send_raw(net.parser, convert_utf8(array[0]));

	return 0;
}


static void rexx_str(char *stem, int num, char *attrib, char *value)
{
	struct Message *m;
	char rxvar[1024], *t;

	if(!value) return;

	GetAttr(MUIA_Application_RexxMsg, gui.app, &m);

	if(num == -1)
		sprintf(rxvar, "%s.%s", stem, attrib);
	else
		sprintf(rxvar, "%s.%d.%s", stem, num, attrib);

	t = convert_locale(value);
	SetRexxVar(m, rxvar, t, strlen(t));
}


static void rexx_int(char *stem, int num, char *attrib, int value)
{
	char buf[32];

	sprintf(buf, "%d", value);
	rexx_str(stem, num, attrib, buf);
}
