From 88b15b9e09c92535bc84cd7eb8841565541fb1f6 Mon Sep 17 00:00:00 2001
From: Giovanni Maruzzelli <gmaruzz@gmail.com>
Date: Mon, 14 Sep 2009 22:13:00 +0000
Subject: [PATCH] skypiax: added skypiax_chat command. SYNTAX: skypiax_chat
 interface remote_skypename TEXT

git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@14857 d0543943-73ff-0310-b7d9-9358b9ac24b2
---
 src/mod/endpoints/mod_skypiax/mod_skypiax.c   | 82 +++++++++++++++++++
 src/mod/endpoints/mod_skypiax/skypiax.h       |  9 ++
 .../endpoints/mod_skypiax/skypiax_protocol.c  | 57 ++++++++++---
 3 files changed, 137 insertions(+), 11 deletions(-)

diff --git a/src/mod/endpoints/mod_skypiax/mod_skypiax.c b/src/mod/endpoints/mod_skypiax/mod_skypiax.c
index be094129dc..25aecf1248 100644
--- a/src/mod/endpoints/mod_skypiax/mod_skypiax.c
+++ b/src/mod/endpoints/mod_skypiax/mod_skypiax.c
@@ -91,6 +91,8 @@ SWITCH_STANDARD_API(sk_function);
 SWITCH_STANDARD_API(skypiax_function);
 #define SKYPIAX_SYNTAX "interface_name skype_API_msg"
 
+SWITCH_STANDARD_API(skypiax_chat_function);
+#define SKYPIAX_CHAT_SYNTAX "interface_name remote_skypename TEXT"
 /* BEGIN: Changes here */
 #define FULL_RELOAD 0
 #define SOFT_RELOAD 1
@@ -1425,6 +1427,7 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_skypiax_load)
 
 		SWITCH_ADD_API(commands_api_interface, "sk", "Skypiax console commands", sk_function, SK_SYNTAX);
 		SWITCH_ADD_API(commands_api_interface, "skypiax", "Skypiax interface commands", skypiax_function, SKYPIAX_SYNTAX);
+		SWITCH_ADD_API(commands_api_interface, "skypiax_chat", "Skypiax_chat interface remote_skypename TEXT", skypiax_chat_function, SKYPIAX_CHAT_SYNTAX);
 
 		/* indicate that the module should continue to be loaded */
 		return SWITCH_STATUS_SUCCESS;
@@ -1904,6 +1907,85 @@ SWITCH_STANDARD_API(sk_function)
 
 	return SWITCH_STATUS_SUCCESS;
 }
+SWITCH_STANDARD_API(skypiax_chat_function)
+{
+	char *mycmd = NULL, *argv[10] = { 0 };
+	int argc = 0;
+	private_t *tech_pvt = NULL;
+	int tried =0;
+	int i;
+	int found = 0;
+	char skype_msg[1024];
+
+	if (!switch_strlen_zero(cmd) && (mycmd = strdup(cmd))) {
+		argc = switch_separate_string(mycmd, ' ', argv, (sizeof(argv) / sizeof(argv[0])));
+	}
+
+	if (!argc) {
+		stream->write_function(stream, "ERROR, usage: %s", SKYPIAX_CHAT_SYNTAX);
+		goto end;
+	}
+
+	if (argc < 3) {
+		stream->write_function(stream, "ERROR, usage: %s", SKYPIAX_CHAT_SYNTAX);
+		goto end;
+	}
+
+	if (argv[0]) {
+
+		for (i = 0; !found && i < SKYPIAX_MAX_INTERFACES; i++) {
+			/* we've been asked for a normal interface name, or we have not found idle interfaces to serve as the "ANY" interface */
+			if (strlen(globals.SKYPIAX_INTERFACES[i].name)
+					&& (strncmp(globals.SKYPIAX_INTERFACES[i].name, argv[0], strlen(argv[0])) == 0)) {
+				tech_pvt = &globals.SKYPIAX_INTERFACES[i];
+				stream->write_function(stream, "Using interface: globals.SKYPIAX_INTERFACES[%d].name=|||%s|||\n", i, globals.SKYPIAX_INTERFACES[i].name);
+				found = 1;
+				break;
+			}
+
+		}
+		if (!found) {
+			stream->write_function(stream, "ERROR: A Skypiax interface with name='%s' was not found\n", argv[0]);
+			goto end;
+		} else {
+			char skype_msg[256];
+			//NOTICA("TEXT is: %s\n", SKYPIAX_P_LOG, (char *) &cmd[strlen(argv[0]) + 1 + strlen(argv[1]) + 1] );
+			snprintf(skype_msg, sizeof(skype_msg), "CHAT CREATE %s", argv[1]);
+			skypiax_signaling_write(tech_pvt, skype_msg);
+			usleep(100);
+		}
+	} else {
+		stream->write_function(stream, "ERROR, usage: %s", SKYPIAX_CHAT_SYNTAX);
+	}
+
+
+	found=0;
+
+	while(!found){
+		for(i=0; i<MAX_CHATS; i++){
+			if(!strcmp(tech_pvt->chats[i].dialog_partner, argv[1]) ){
+				snprintf(skype_msg, sizeof(skype_msg), "CHATMESSAGE %s %s", tech_pvt->chats[i].chatname, (char *) &cmd[strlen(argv[0]) + 1 + strlen(argv[1]) + 1]);
+				skypiax_signaling_write(tech_pvt, skype_msg);
+				found=1;
+				break;
+			}
+		}
+		if(found){
+			break;
+		}
+		if(tried > 1000){
+			stream->write_function(stream, "ERROR: no chat with dialog_partner='%s' was not found\n", argv[1]);
+			break;
+		}
+		usleep(1000);
+	}
+
+end:
+	switch_safe_free(mycmd);
+
+	return SWITCH_STATUS_SUCCESS;
+}
+
 
 SWITCH_STANDARD_API(skypiax_function)
 {
diff --git a/src/mod/endpoints/mod_skypiax/skypiax.h b/src/mod/endpoints/mod_skypiax/skypiax.h
index cfff98893e..96e2562b68 100644
--- a/src/mod/endpoints/mod_skypiax/skypiax.h
+++ b/src/mod/endpoints/mod_skypiax/skypiax.h
@@ -163,6 +163,14 @@ struct SkypiaxHandles {
 };
 #endif //WIN32
 
+#define MAX_CHATS 10
+
+struct chat {
+	char chatname[256];
+	char dialog_partner[256];
+};
+typedef struct chat chat_t;
+
 #define MAX_CHATMESSAGES 10
 
 struct chatmessage {
@@ -261,6 +269,7 @@ struct private_object {
 	uint32_t ob_failed_calls;
 
 	chatmessage_t chatmessages[MAX_CHATMESSAGES];
+	chat_t chats[MAX_CHATS];
 };
 
 typedef struct private_object private_t;
diff --git a/src/mod/endpoints/mod_skypiax/skypiax_protocol.c b/src/mod/endpoints/mod_skypiax/skypiax_protocol.c
index 7d4a1f6181..7ac3b466d9 100644
--- a/src/mod/endpoints/mod_skypiax/skypiax_protocol.c
+++ b/src/mod/endpoints/mod_skypiax/skypiax_protocol.c
@@ -222,6 +222,45 @@ int skypiax_signaling_read(private_t * tech_pvt)
 					skypiax_signaling_write(tech_pvt, msg_to_skype);
 				}
 			}
+			if (!strcasecmp(message, "CHAT")) {
+				char msg_to_skype[256];
+				int i;
+				int found;
+
+				skypiax_strncpy(obj, where, sizeof(obj) - 1);
+				where = strsep(stringp, " ");
+				skypiax_strncpy(id, where, sizeof(id) - 1);
+				where = strsep(stringp, " ");
+				skypiax_strncpy(prop, where, sizeof(prop) - 1);
+				skypiax_strncpy(value, *stringp, sizeof(value) - 1);
+
+				if (!strcasecmp(prop, "STATUS") && !strcasecmp(value, "DIALOG")) {
+					DEBUGA_SKYPE("CHAT %s is DIALOG\n", SKYPIAX_P_LOG, id);
+					sprintf(msg_to_skype, "GET CHAT %s DIALOG_PARTNER", id);
+					skypiax_signaling_write(tech_pvt, msg_to_skype);
+				}
+
+				if (!strcasecmp(prop, "DIALOG_PARTNER")) {
+					DEBUGA_SKYPE("CHAT %s has DIALOG_PARTNER %s\n", SKYPIAX_P_LOG, id, value);
+					found=0;
+					for(i=0; i<MAX_CHATS; i++){
+						if(strlen(tech_pvt->chats[i].chatname) == 0 || !strcmp(tech_pvt->chats[i].chatname, id) ){
+							strncpy(tech_pvt->chats[i].chatname, id, sizeof(tech_pvt->chats[i].chatname));
+							strncpy(tech_pvt->chats[i].dialog_partner, value, sizeof(tech_pvt->chats[i].dialog_partner));
+							found=1;
+							break;
+						}
+					}
+					if(!found){
+						ERRORA("why we do not have a chats slot free? we have more than %d chats in parallel?\n", SKYPIAX_P_LOG, MAX_CHATS);
+					} 
+
+					DEBUGA_SKYPE("CHAT %s is in position %d in the chats array, chatname=%s, dialog_partner=%s\n", SKYPIAX_P_LOG, id, i, tech_pvt->chats[i].chatname, tech_pvt->chats[i].dialog_partner);
+				}
+
+			}
+
+
 			if (!strcasecmp(message, "CHATMESSAGE")) {
 				char msg_to_skype[256];
 				int i;
@@ -232,20 +271,16 @@ int skypiax_signaling_read(private_t * tech_pvt)
 				skypiax_strncpy(id, where, sizeof(id) - 1);
 				where = strsep(stringp, " ");
 				skypiax_strncpy(prop, where, sizeof(prop) - 1);
-				//where = strsep(stringp, " ");
 				skypiax_strncpy(value, *stringp, sizeof(value) - 1);
-				//where = strsep(stringp, " ");
-
-				//ERRORA ("Skype MSG, obj: %s, id: %s, prop: %s, value: %s,where: %s!\n", SKYPIAX_P_LOG, obj, id, prop, value, where ? where : "NULL");
 
 				if (!strcasecmp(prop, "STATUS") && !strcasecmp(value, "RECEIVED")) {
-					NOTICA("RECEIVED CHATMESSAGE %s, let's see which type it is\n", SKYPIAX_P_LOG, id);
+					DEBUGA_SKYPE("RECEIVED CHATMESSAGE %s, let's see which type it is\n", SKYPIAX_P_LOG, id);
 					sprintf(msg_to_skype, "GET CHATMESSAGE %s TYPE", id);
 					skypiax_signaling_write(tech_pvt, msg_to_skype);
 				}
 
 				if (!strcasecmp(prop, "TYPE") && !strcasecmp(value, "SAID")) {
-					NOTICA("CHATMESSAGE %s is of type SAID, let's get the other infos\n", SKYPIAX_P_LOG, id);
+					DEBUGA_SKYPE("CHATMESSAGE %s is of type SAID, let's get the other infos\n", SKYPIAX_P_LOG, id);
 					found=0;
 					for(i=0; i<MAX_CHATMESSAGES; i++){
 						if(strlen(tech_pvt->chatmessages[i].id) == 0){
@@ -258,7 +293,7 @@ int skypiax_signaling_read(private_t * tech_pvt)
 					if(!found){
 						ERRORA("why we do not have a chatmessages slot free? we have more than %d chatmessages in parallel?\n", SKYPIAX_P_LOG, MAX_CHATMESSAGES);
 					} else {
-						NOTICA("CHATMESSAGE %s is in position %d in the chatmessages array, type=%s, id=%s\n", SKYPIAX_P_LOG, id, i, tech_pvt->chatmessages[i].type, tech_pvt->chatmessages[i].id);
+						DEBUGA_SKYPE("CHATMESSAGE %s is in position %d in the chatmessages array, type=%s, id=%s\n", SKYPIAX_P_LOG, id, i, tech_pvt->chatmessages[i].type, tech_pvt->chatmessages[i].id);
 						sprintf(msg_to_skype, "GET CHATMESSAGE %s CHATNAME", id);
 						skypiax_signaling_write(tech_pvt, msg_to_skype);
 						skypiax_sleep(100);
@@ -274,7 +309,7 @@ int skypiax_signaling_read(private_t * tech_pvt)
 				}
 
 				if (!strcasecmp(prop, "CHATNAME")) {
-					NOTICA("CHATMESSAGE %s belongs to the CHAT %s\n", SKYPIAX_P_LOG, id, value);
+					DEBUGA_SKYPE("CHATMESSAGE %s belongs to the CHAT %s\n", SKYPIAX_P_LOG, id, value);
 					found=0;
 					for(i=0; i<MAX_CHATMESSAGES; i++){
 						if(!strcmp(tech_pvt->chatmessages[i].id, id)){
@@ -288,7 +323,7 @@ int skypiax_signaling_read(private_t * tech_pvt)
 					} 
 				}
 				if (!strcasecmp(prop, "FROM_HANDLE")) {
-					NOTICA("CHATMESSAGE %s was sent by FROM_HANDLE %s\n", SKYPIAX_P_LOG, id, value);
+					DEBUGA_SKYPE("CHATMESSAGE %s was sent by FROM_HANDLE %s\n", SKYPIAX_P_LOG, id, value);
 					found=0;
 					for(i=0; i<MAX_CHATMESSAGES; i++){
 						if(!strcmp(tech_pvt->chatmessages[i].id, id)){
@@ -303,7 +338,7 @@ int skypiax_signaling_read(private_t * tech_pvt)
 
 				}
 				if (!strcasecmp(prop, "FROM_DISPNAME")) {
-					NOTICA("CHATMESSAGE %s was sent by FROM_DISPNAME %s\n", SKYPIAX_P_LOG, id, value);
+					DEBUGA_SKYPE("CHATMESSAGE %s was sent by FROM_DISPNAME %s\n", SKYPIAX_P_LOG, id, value);
 					found=0;
 					for(i=0; i<MAX_CHATMESSAGES; i++){
 						if(!strcmp(tech_pvt->chatmessages[i].id, id)){
@@ -318,7 +353,7 @@ int skypiax_signaling_read(private_t * tech_pvt)
 
 				}
 				if (!strcasecmp(prop, "BODY")) {
-					NOTICA("CHATMESSAGE %s has BODY %s\n", SKYPIAX_P_LOG, id, value);
+					DEBUGA_SKYPE("CHATMESSAGE %s has BODY %s\n", SKYPIAX_P_LOG, id, value);
 					found=0;
 					for(i=0; i<MAX_CHATMESSAGES; i++){
 						if(!strcmp(tech_pvt->chatmessages[i].id, id)){