Exemple #1
0
int32_t fs_opr_t::eval(const char* key, char* val, uint32_t val_len) {
    FUNC_BEGIN();
    snprintf(szcmd, LEN_512, "api eval %s\n\n", key);
    TRACE_LOG("eval, (%s)", szcmd);

    if (esl_send_recv(&_handle, szcmd) == ESL_SUCCESS
            && (fs_resp = esl_event_get_body(_handle.last_sr_event))) {
        strncpy(val, fs_resp, val_len);

        fs_resp = val;

        while (fs_resp && *fs_resp && (fs_resp - val) < val_len) {
            if ('\n' == *fs_resp || '\r' == *fs_resp) {
                val[fs_resp - val] = '\0';
                break;
            }

            ++fs_resp;
        }

        ret = IMS_SUCCESS;
    } else {
        WARNING_LOG("fs_eval(%s) failed", key);
    }

    FUNC_END();
}
Exemple #2
0
int32_t fs_opr_t::record(const char* uuid, const char* file, uint32_t time_s) {
    FUNC_BEGIN();

    std::string full_file = _recordbase;
    full_file += "/";
    full_file += file;

    TRACE_LOG("fs::record(%s) start.", full_file.c_str());
    //set_channel_attribute(uuid,"playback_terminators=none");
    //set_channel_attribute(uuid,"RECORD_HANGUP_ON_ERROR=false");//1.0.7的fs才有
    //set_channel_attribute(uuid,"RECORD_MIN_SEC=0");

    //TRACE_LOG("fs::record(%s) middle.",full_file.c_str());
    snprintf(szcmd, LEN_512, CMD_RECORD_FMT, uuid, full_file.c_str(), time_s);

    //if(esl_send_recv(&_handle,szcmd)==ESL_SUCCESS){
    //    if(is_result_ok(fs_resp=esl_event_get_body(_handle.last_sr_event))){
    //        ret=IMS_SUCCESS;
    //    }
    //    else{
    //        WARNING_LOG("fs:record(%s,%s,%u);ret(%s)",uuid,full_file.c_str(),time_s,fs_resp);
    //    }
    //}
    if (esl_send_recv(&_handle, szcmd) == ESL_SUCCESS &&
            is_result_ok(fs_resp = esl_event_get_header(_handle.last_sr_event, "Reply-Text"))) {
        ret = IMS_SUCCESS;
    } else {
        WARNING_LOG("fs:record(%s,%s,%u) esl_send_recv != ESL_SUCCESS", uuid, full_file.c_str(), time_s);
    }

    //TRACE_LOG("fs::record(%s) done.",full_file.c_str());

    FUNC_END();
}
ESLevent *ESLconnection::bgapi(const char *cmd, const char *arg)
{
	size_t len;
	char *cmd_buf;
	
	if (!cmd) {
		return NULL;
	}

	len = strlen(cmd) + (arg ? strlen(arg) : 0) + 10;

	cmd_buf = (char *) malloc(len + 1);
	assert(cmd_buf);

	snprintf(cmd_buf, len, "bgapi %s %s", cmd, arg ? arg : "");
	*(cmd_buf + (len)) = '\0';

	if (esl_send_recv(&handle, cmd_buf) == ESL_SUCCESS) {
		esl_event_t *event;
		esl_event_dup(&event, handle.last_sr_event);
		return new ESLevent(event, 1);
	}

	free(cmd_buf);
	
	return NULL;
}
ESLevent *ESLconnection::sendRecv(const char *cmd)
{
	if (esl_send_recv(&handle, cmd) == ESL_SUCCESS) {
		esl_event_t *event;
		esl_event_dup(&event, handle.last_sr_event);
		return new ESLevent(event, 1);
	}
	
	return NULL;
}
static void mycallback(esl_socket_t server_sock, esl_socket_t client_sock, struct sockaddr_in *addr, void *user_data)
{
	esl_handle_t handle = {{0}};
	int done = 0;
	esl_status_t status;
	time_t exp = 0;

	if (fork()) {
		return;
	}

	if (esl_attach_handle(&handle, client_sock, addr) != ESL_SUCCESS) {
		return;
	}


	esl_log(ESL_LOG_INFO, "Connected! %d\n", handle.sock);

	esl_filter(&handle, "unique-id", esl_event_get_header(handle.info_event, "caller-unique-id"));
	esl_events(&handle, ESL_EVENT_TYPE_PLAIN, "SESSION_HEARTBEAT CHANNEL_ANSWER CHANNEL_ORIGINATE CHANNEL_PROGRESS CHANNEL_HANGUP "
			   "CHANNEL_BRIDGE CHANNEL_UNBRIDGE CHANNEL_OUTGOING CHANNEL_EXECUTE CHANNEL_EXECUTE_COMPLETE DTMF CUSTOM conference::maintenance");

	esl_send_recv(&handle, "linger");

	esl_execute(&handle, "answer", NULL, NULL);
	esl_execute(&handle, "conference", "3000@default", NULL);
	
	while((status = esl_recv_timed(&handle, 1000)) != ESL_FAIL) {
		if (done) {
			if (time(NULL) >= exp) {
				break;
			}
		} else if (status == ESL_SUCCESS) {
			const char *type = esl_event_get_header(handle.last_event, "content-type");
			if (type && !strcasecmp(type, "text/disconnect-notice")) {
				const char *dispo = esl_event_get_header(handle.last_event, "content-disposition");
				esl_log(ESL_LOG_INFO, "Got a disconnection notice dispostion: [%s]\n", dispo ? dispo : "");
				if (dispo && !strcmp(dispo, "linger")) {
					done = 1;
					esl_log(ESL_LOG_INFO, "Waiting 5 seconds for any remaining events.\n");
					exp = time(NULL) + 5;
				}
			}
		}
	}
	
	esl_log(ESL_LOG_INFO, "Disconnected! %d\n", handle.sock);
	esl_disconnect(&handle);
}
Exemple #6
0
int32_t fs_opr_t::unbridge(const char* uuid) {
    FUNC_BEGIN();
    snprintf(szcmd, LEN_512, CMD_UNBRIDGE_FMT, uuid);
    TRACE_LOG("unbridge(%s)", szcmd);

    if (esl_send_recv(&_handle, szcmd) == ESL_SUCCESS) {
        if (is_result_ok(fs_resp = esl_event_get_body(_handle.last_sr_event))) {
            ret = IMS_SUCCESS;
        } else {
            WARNING_LOG("fs:unbridge(%s);ret(%s)", uuid, fs_resp);
        }
    }

    FUNC_END();
}
Exemple #7
0
int32_t fs_opr_t::conference_mute(const char* name, const char* member) {
    FUNC_BEGIN();

    snprintf(szcmd, LEN_512, "api conference %s mute %s\n\n", name, member);
    TRACE_LOG("conference_mute(%s)", szcmd);

    if (esl_send_recv(&_handle, szcmd) == ESL_SUCCESS) {
        if (is_result_ok(fs_resp = esl_event_get_body(_handle.last_sr_event))) {
            ret = IMS_SUCCESS;
        } else {
            WARNING_LOG("fs:conference_mute (%s,%s);ret(%s)", name, member, fs_resp);
        }
    }

    FUNC_END();
}
Exemple #8
0
bool get_uuid(esl_handle_t * pHandle, char * uuid)
{
    esl_send_recv(pHandle, "api create_uuid\n\n");
    if (pHandle->last_sr_event && pHandle->last_sr_event->body)
    {
        //disp_msg("uuid:[%s]****************************************************\n", pHandle->last_sr_event->body);
        strcpy(uuid, pHandle->last_sr_event->body);
        return true;
        //strcpy(uuid,(handle.last_sr_event->body)+strlen("+OK Job-UUID: "));
    }
    else
    {
        //disp_msg("[%s] last_sr_reply\n", pHandle->last_sr_reply);
        return false;
    }
    return false;
}
Exemple #9
0
int32_t fs_opr_t::join_conference(const char* name, const char* dest_disp, const char* destno,
                                  CONF_MODE mode, uint64_t sessionid) {
    FUNC_BEGIN();
    snprintf(szcmd, LEN_512, CMD_JOINCONFERENCE_FMT, dest_disp, dest_disp, sessionid, destno, name,
             ((CONF_LISTEN == mode) ? "+flags{mute}" : ""));
    TRACE_LOG("join_conference(%s)", szcmd);

    if (esl_send_recv(&_handle, szcmd) == ESL_SUCCESS) {
        if (is_result_ok(fs_resp = esl_event_get_header(_handle.last_sr_event, "Reply-Text"))) {
            ret = IMS_SUCCESS;
        } else {
            WARNING_LOG("fs:join_conference(%s,%s,%s,%u,%lu);ret(%s)", name, dest_disp, destno, mode, sessionid,
                        fs_resp);
        }
    }

    FUNC_END();
}
Exemple #10
0
bool nway_bridge(esl_handle_t* pHandle, const char * a_uuid, const char * b_uuid)
{
    char szCmd[2048] = { 0 };
    sprintf(szCmd, "bgapi uuid_bridge %s %s\n\n", a_uuid, b_uuid);
    esl_send_recv(pHandle, szCmd);//
    //	uuid[0]=0;
    if (pHandle->last_sr_event && pHandle->last_sr_event->body)
    {
        printf("[%s]\n", pHandle->last_sr_event->body);
        //	strcpy(uuid,(handle.last_sr_event->body)+strlen("+OK Job-UUID: "));
        return true;
    }
    else
    {
        printf("[%s] last_sr_reply\n", pHandle->last_sr_reply);
        return false;
    }
}
Exemple #11
0
bool nway_hangup(esl_handle_t * pHandle, const char * uuid)
{
    char szCmd[2048] = { 0 };
    sprintf(szCmd, "bgapi uuid_kill %s\n\n", uuid);
    esl_send_recv(pHandle, szCmd);// "bgapi originate {originate_timeout=30}sofia/internal/1005%192.168.1.102 &park()\n\n");
    //	uuid[0]=0;
    if (pHandle->last_sr_event && pHandle->last_sr_event->body)
    {
        printf("[%s]\n", pHandle->last_sr_event->body);
        //	strcpy(uuid,(handle.last_sr_event->body)+strlen("+OK Job-UUID: "));
        return true;
    }
    else
    {
        printf("[%s] last_sr_reply\n", pHandle->last_sr_reply);
        return false;
    }
}
Exemple #12
0
int32_t fs_opr_t::unjoin_conference(const char* name, const char* uuid) {
    FUNC_BEGIN();

    snprintf(szcmd, LEN_512,
             "api uuid_transfer %s -aleg 'set:hangup_after_bridge=false,set:park_after_bridge=true,park:' inline \n\n",
             uuid);
    TRACE_LOG("unjoin_conference(%s)", szcmd);

    if (esl_send_recv(&_handle, szcmd) == ESL_SUCCESS) {
        if (is_result_ok(fs_resp = esl_event_get_body(_handle.last_sr_event))) {
            ret = IMS_SUCCESS;
        } else {
            WARNING_LOG("fs:unjoin_conference(%s,%s);ret(%s)", name, uuid, fs_resp);
        }
    }

    FUNC_END();
}
Exemple #13
0
int32_t fs_opr_t::uuid_exists(const char* uuid) {
    FUNC_BEGIN();
    const char* uuid_exists = "api uuid_exists %s";
    snprintf(szcmd, LEN_512, uuid_exists, uuid);
    TRACE_LOG("uuid_exists(%s)", szcmd);

    if (esl_send_recv(&_handle, szcmd) == ESL_SUCCESS) {
        fs_resp = esl_event_get_body(_handle.last_sr_event);

        if (fs_resp && strncasecmp(fs_resp, "true", 4) == 0) {
            TRACE_LOG("uuid=%s exists.", uuid);
            ret = IMS_SUCCESS;
        } else {
            WARNING_LOG("fs:uuid=%s not exists;ret(%s)", uuid, fs_resp);
        }
    }

    FUNC_END();
}
Exemple #14
0
///////////////////////////////////// 呼叫 START /////////////////////////////////////////////////
int32_t fs_opr_t::originate(const char* ani, const char* dnis,
                            const char* caller_disp, const char* called_disp,
                            uint64_t sessionid, uint32_t timeout) {
    FUNC_BEGIN();
    snprintf(szcmd, LEN_512, CMD_ORIGINATE_FMT,
             caller_disp, caller_disp, sessionid, ani, called_disp, called_disp, timeout, sessionid, dnis);
    TRACE_LOG("originate(%s)", szcmd);

    if (esl_send_recv(&_handle, szcmd) == ESL_SUCCESS &&
            is_result_ok(fs_resp = esl_event_get_header(_handle.last_sr_event, "Reply-Text"))) {
        ret = IMS_SUCCESS;
        break;
    } else {
        WARNING_LOG("fs:originate(%s,%s,%s,%s,%u,%lu);ret(%s)", ani, dnis, caller_disp, called_disp,
                    timeout, sessionid, fs_resp);
    }

    FUNC_END();
}
Exemple #15
0
int32_t fs_opr_t::uuid_bridge(const char* uuid1, const char* uuid2, char* uuid_output,
                              uint32_t output_len) {
    FUNC_BEGIN();

    snprintf(szcmd, LEN_512, CMD_UUIDBRIDGE_FMT, uuid1, uuid2);
    TRACE_LOG("uuid_bridge(%s)", szcmd);

    if (set_channel_attribute(uuid1, "bypass_media=false") &&
            set_channel_attribute(uuid2, "bypass_media=false")) {
        if (esl_send_recv(&_handle, szcmd) == ESL_SUCCESS) {
            if (is_result_ok(fs_resp = esl_event_get_body(_handle.last_sr_event))) {
                fs_resp += 3;

                while (*fs_resp && ' ' == *fs_resp) {
                    fs_resp++;
                }

                if (*fs_resp && uuid_output && output_len > 0) {
                    strncpy(uuid_output, fs_resp, output_len);
                    uint32_t idx = 0;

                    do {
                        if ('\n' == uuid_output[idx]) {
                            uuid_output[idx] = '\0';
                            break;
                        }

                        ++idx;
                    } while (uuid_output[idx]);
                }

                ret = IMS_SUCCESS;
            } else {
                WARNING_LOG("fs:uuid_bridge(%s,%s,%s);ret(%s)", uuid1, uuid2, uuid_output, fs_resp);
            }
        }
    } else {
        WARNING_LOG("fs:uuid_bridge(%s,%s) failed(set channel attribute err)", uuid1, uuid2);
    }

    FUNC_END();
}
Exemple #16
0
static int process_command(esl_handle_t *handle, const char *cmd)
{
	while (*cmd == ' ') cmd++;


	if ((*cmd == '/' && cmd++) || !strncasecmp(cmd, "...", 3)) {
		if (!strcasecmp(cmd, "help")) {
			output_printf("%s", cli_usage);
			goto end;
		}
		if (!strcasecmp(cmd, "exit") ||
			!strcasecmp(cmd, "quit") ||
			!strcasecmp(cmd, "...") ||
			!strcasecmp(cmd, "bye")
			) {
			esl_log(ESL_LOG_INFO, "Goodbye!\nSee you at ClueCon http://www.cluecon.com/\n");
			return -1;
		} else if (!strncasecmp(cmd, "logfilter", 9)) {
			cmd += 9;
			while (*cmd && *cmd == ' ') {
				cmd++;
			}
			if (!esl_strlen_zero(cmd)) {
				esl_safe_free(logfilter);
				logfilter = strdup(cmd);
			} else {
				esl_safe_free(logfilter);
			}
			output_printf("Logfilter %s\n", logfilter ? "enabled" : "disabled");
		} else if (!strncasecmp(cmd, "uuid", 4)) {
			cmd += 4;
			while (*cmd && *cmd == ' ') {
				cmd++;
			}
			if (!esl_strlen_zero(cmd)) {
				filter_uuid = strdup(cmd);
			} else {
				esl_safe_free(filter_uuid);
			}
			output_printf("UUID filtering %s\n", filter_uuid ? "enabled" : "disabled");
		} else if (!strncasecmp(cmd, "event", 5) ||
				   !strncasecmp(cmd, "noevents", 8) ||
				   !strncasecmp(cmd, "nixevent", 8) ||
				   !strncasecmp(cmd, "log", 3) ||
				   !strncasecmp(cmd, "nolog", 5) ||
				   !strncasecmp(cmd, "filter", 6)
				   ) {
			esl_send_recv(handle, cmd);
			printf("%s\n", handle->last_sr_reply);
		} else if (!strncasecmp(cmd, "debug", 5)) {
			int tmp_debug = atoi(cmd+6);
			if (tmp_debug > -1 && tmp_debug < 8) {
				esl_global_set_default_logger(tmp_debug);
				output_printf("fs_cli debug level set to %d\n", tmp_debug);
			} else {
				output_printf("fs_cli debug level must be 0 - 7\n");
			}
		} else {
			output_printf("Unknown command [%s]\n", cmd);
		}
	} else {
		char cmd_str[1024] = "";
		const char *err = NULL;

		if (!strncasecmp(cmd, "console loglevel ", 17)) { 
			snprintf(cmd_str, sizeof(cmd_str), "log %s", cmd + 17);
			esl_send_recv(handle, cmd_str);
			printf("%s\n", handle->last_sr_reply);
		}

		snprintf(cmd_str, sizeof(cmd_str), "api %s\nconsole_execute: true\n\n", cmd);
		if (esl_send_recv(handle, cmd_str)) {
			output_printf("Socket interrupted, bye!\n");
			return -1;
		}
		if (handle->last_sr_event) {
			if (handle->last_sr_event->body) {
				output_printf("%s\n", handle->last_sr_event->body);
			} else if ((err = esl_event_get_header(handle->last_sr_event, "reply-text")) && !strncasecmp(err, "-err", 3)) {
				output_printf("Error: %s!\n", err + 4);
			}
		}
	}
 end:
	return 0;
}
Exemple #17
0
static int process_command(esl_handle_t *handle, const char *cmd) 
{
	if ((*cmd == '/' && cmd++) || !strncasecmp(cmd, "...", 3)) {
		
		if (!strcasecmp(cmd, "help")) {
			printf(
				   "Command                    \tDescription\n"
				   "-----------------------------------------------\n"
				   "/help                      \tHelp\n"
				   "/exit, /quit, /bye, ...    \tExit the program.\n"
				   "/event, /noevent, /nixevent\tEvent commands.\n"
				   "/log, /nolog               \tLog commands.\n"
				   "/uuid                      \tFilter logs for a single call uuid\n"
				   "/filter                    \tFilter commands.\n"
				   "/debug [0-7]               \tSet debug level.\n"
				   "\n"
				   );

			goto end;
		}

		if (
			!strcasecmp(cmd, "exit") ||
			!strcasecmp(cmd, "quit") ||
			!strcasecmp(cmd, "...") ||
			!strcasecmp(cmd, "bye")
			) {
			esl_log(ESL_LOG_INFO, "Goodbye!\nSee you at ClueCon http://www.cluecon.com/\n");
			return -1;
		} else if (!strncasecmp(cmd, "uuid", 4)) {
			cmd += 4;
			
			while (*cmd && *cmd == ' ') {
				cmd++;
			}

			if (!esl_strlen_zero(cmd)) {
				filter_uuid = strdup(cmd);
			} else {
				esl_safe_free(filter_uuid);
			}
			
			printf("UUID filtering %s\n", filter_uuid ? "enabled" : "disabled");

		} else if (
			!strncasecmp(cmd, "event", 5) || 
			!strncasecmp(cmd, "noevent", 7) ||
			!strncasecmp(cmd, "nixevent", 8) ||
			!strncasecmp(cmd, "log", 3) || 
			!strncasecmp(cmd, "nolog", 5) || 
			!strncasecmp(cmd, "filter", 6)
			) {

			esl_send_recv(handle, cmd);	

			printf("%s\n", handle->last_sr_reply);
		} else if (!strncasecmp(cmd, "debug", 5)){
			int tmp_debug = atoi(cmd+6);
			if (tmp_debug > -1 && tmp_debug < 8){
				esl_global_set_default_logger(tmp_debug);
				printf("fs_cli debug level set to %d\n", tmp_debug);
			} else {
				printf("fs_cli debug level must be 0 - 7\n");
			}
		} else {
			printf("Unknown command [%s]\n", cmd);	
		}
	} else {
		char cmd_str[1024] = "";
		const char *err = NULL;
		
		snprintf(cmd_str, sizeof(cmd_str), "api %s\nconsole_execute: true\n\n", cmd);
		if (esl_send_recv(handle, cmd_str)) {
			printf("Socket interrupted, bye!\n");
			return 1;
		}
		if (handle->last_sr_event) {
			if (handle->last_sr_event->body) {
				printf("%s\n", handle->last_sr_event->body);
			} else if ((err = esl_event_get_header(handle->last_sr_event, "reply-text")) && !strncasecmp(err, "-err", 3)) {
				printf("Error: %s!\n", err + 4);
			}
		}
	}
	
 end:

	return 0;

}
Exemple #18
0
static unsigned char esl_console_complete(const char *buffer, const char *cursor)
{
	char cmd_str[2048] = "";
	unsigned char ret = CC_REDISPLAY;
	char *dup = strdup(buffer);
	char *buf = dup;
	int pos = 0, sc = 0;
	char *p;

	if (!esl_strlen_zero(cursor) && !esl_strlen_zero(buffer)) {
		pos = (int)(cursor - buffer);
	}
	if (pos > 0) {
		*(buf + pos) = '\0';
	}

	if ((p = strchr(buf, '\r')) || (p = strchr(buf, '\n'))) {
		*p = '\0';
	}

	while (*buf == ' ') {
		buf++;
		sc++;
	}

#ifdef HAVE_EDITLINE
	if (!*buf && sc) {
		el_deletestr(el, sc);
	}
#endif

	sc = 0;

	p = end_of_p(buf);
	while(p >= buf && *p == ' ') {
		sc++;
		p--;
	}

#ifdef HAVE_EDITLINE
	if (sc > 1) {
		el_deletestr(el, sc - 1);
		*(p + 2) = '\0';
	}
#endif
	

	if (*cursor) {
		snprintf(cmd_str, sizeof(cmd_str), "api console_complete c=%ld;%s\n\n", (long)pos, buf);
	} else {
		snprintf(cmd_str, sizeof(cmd_str), "api console_complete %s\n\n", buf);
	}

	esl_send_recv(global_handle, cmd_str);


	if (global_handle->last_sr_event && global_handle->last_sr_event->body) {
		char *r = global_handle->last_sr_event->body;
		char *w, *p1;
		
		if (r) {
			if ((w = strstr(r, "\n\nwrite="))) {
				int len = 0;
				*w = '\0';
				w += 8;

				len = atoi(w);

				if ((p1= strchr(w, ':'))) {
					w = p1+ 1;
				}
				
				printf("%s\n\n\n", r);

#ifdef HAVE_EDITLINE
				el_deletestr(el, len);
				el_insertstr(el, w);
#else
#ifdef _MSC_VER
				console_bufferInput(0, len, (char*)buffer, DELETE_REFRESH_OP);
				console_bufferInput(w, (int)strlen(w), (char*)buffer, 0);
#endif
#endif
				
			} else {
				printf("%s\n", r);
#ifdef _MSC_VER
				console_bufferInput(0, 0, (char*)buffer, DELETE_REFRESH_OP);
#endif
			}
		}

		fflush(stdout);
	}	

	esl_safe_free(dup);

	return ret;
}
Exemple #19
0
int main(int argc, char *argv[])
{
	esl_handle_t handle = {{0}};
	int count = 0;
	const char *line = NULL;
	char cmd_str[1024] = "";
	cli_profile_t *profile = NULL;
#ifndef WIN32
	char hfile[512] = "/tmp/fs_cli_history";
	char cfile[512] = "/etc/fs_cli.conf";
	char dft_cfile[512] = "/etc/fs_cli.conf";
#else
	char hfile[512] = "fs_cli_history";
	char cfile[512] = "fs_cli.conf";
	char dft_cfile[512] = "fs_cli.conf";
#endif
	char *home = getenv("HOME");
	/* Vars for optargs */
	int opt;
	static struct option options[] = {
		{"help", 0, 0, 'h'},
		{"no-color", 0, 0, 'n'},
		{"host", 1, 0, 'H'},
		{"port", 1, 0, 'P'},
		{"user", 1, 0, 'u'},
		{"password", 1, 0, 'p'},
		{"debug", 1, 0, 'd'},
		{"execute", 1, 0, 'x'},
		{"loglevel", 1, 0, 'l'},
		{"log-uuid", 0, 0, 'U'},
		{"quiet", 0, 0, 'q'},
		{"batchmode", 0, 0, 'b'},
		{"retry", 0, 0, 'r'},
		{"interrupt", 0, 0, 'i'},
		{"reconnect", 0, 0, 'R'},
		{"timeout", 1, 0, 't'},
		{"connect-timeout", 1, 0, 'T'},
		{0, 0, 0, 0}
	};
	char temp_host[128];
	int argv_host = 0;
	char temp_user[256];
	char temp_pass[128];
	int argv_pass = 0 ;
	int argv_user = 0 ;
	int temp_port = 0;
	int argv_port = 0;
	int temp_log = -1;
	int argv_error = 0;
	int argv_exec = 0;
	char argv_command[1024] = "";
	char argv_loglevel[128] = "";
	int argv_log_uuid = 0;
	int argv_quiet = 0;
	int argv_batch = 0;
	int loops = 2, reconnect = 0;
	char *ccheck;

#ifdef WIN32
	feature_level = 0;
#else
	feature_level = 1;
#endif

	if ((ccheck = getenv("FS_CLI_COLOR"))) {
		is_color = esl_true(ccheck);
	}

	strncpy(internal_profile.host, "127.0.0.1", sizeof(internal_profile.host));
	strncpy(internal_profile.pass, "ClueCon", sizeof(internal_profile.pass));
	strncpy(internal_profile.name, "internal", sizeof(internal_profile.name));
	internal_profile.port = 8021;
	set_fn_keys(&internal_profile);
	esl_set_string(internal_profile.prompt_color, prompt_color);
	esl_set_string(internal_profile.input_text_color, input_text_color);
	esl_set_string(internal_profile.output_text_color, output_text_color);
	if (home) {
		snprintf(hfile, sizeof(hfile), "%s/.fs_cli_history", home);
		snprintf(cfile, sizeof(cfile), "%s/.fs_cli_conf", home);
	}
	signal(SIGINT, handle_SIGINT);
#ifdef SIGTSTP
	signal(SIGTSTP, handle_SIGINT);
#endif
#ifdef SIGQUIT
	signal(SIGQUIT, handle_SIGQUIT);
#endif
	esl_global_set_default_logger(6); /* default debug level to 6 (info) */
	for(;;) {
		int option_index = 0;
		opt = getopt_long(argc, argv, "H:P:S:u:p:d:x:l:Ut:T:qrRhib?n", options, &option_index);
		if (opt == -1) break;
		switch (opt) {
			case 'H':
				esl_set_string(temp_host, optarg);
				argv_host = 1;
				break;
			case 'P':
				temp_port= atoi(optarg);
				if (temp_port > 0 && temp_port < 65536) {
					argv_port = 1;
				} else {
					printf("ERROR: Port must be in range 1 - 65535\n");
					argv_error = 1;
				}
				break;
			case 'n':
				is_color = 0;
				break;
			case 'u':
				esl_set_string(temp_user, optarg);
				argv_user = 1;
				break;
			case 'p':
				esl_set_string(temp_pass, optarg);
				argv_pass = 1;
				break;
			case 'd':
				temp_log=atoi(optarg);
				if (temp_log < 0 || temp_log > 7) {
					printf("ERROR: Debug level should be 0 - 7.\n");
					argv_error = 1;
				} else {
					esl_global_set_default_logger(temp_log);
				}
				break;
			case 'x':
				argv_exec = 1;
				esl_set_string(argv_command, optarg);
				break;
			case 'l':
				esl_set_string(argv_loglevel, optarg);
				break;
			case 'U':
				argv_log_uuid = 1;
				break;
			case 'q':
				argv_quiet = 1;
				break;
			case 'b':
				argv_batch = 1;
				break;
			case 'i':
				allow_ctl_c = 1;
				break;
			case 'r':
				loops += 120;
				break;
			case 'R':
				reconnect = 1;
				break;
			case 't':
				timeout = atoi(optarg);
				break;
			case 'T':
				connect_timeout = atoi(optarg);
				break;
			case 'h':
			case '?':
				print_banner(stdout, is_color);
				usage(argv[0]);
				return 0;
		}
	}
	if (argv_error) {
		printf("\n");
		return usage(argv[0]);
	}
	read_config(dft_cfile, cfile);
	if (optind < argc) {
		get_profile(argv[optind], &profile);
	}
	if (!profile) {
		if (get_profile("default", &profile)) {
			esl_log(ESL_LOG_DEBUG, "profile default does not exist using builtin profile\n");
			profile = &internal_profile;
		}
	}
	if (temp_log < 0 ) {
		esl_global_set_default_logger(profile->debug);
	}
	if (argv_host) {
		esl_set_string(profile->host, temp_host);
	}
	if (argv_port) {
		profile->port = (esl_port_t)temp_port;
	}
	if (argv_user) {
		esl_set_string(profile->user, temp_user);
	}
	if (argv_pass) {
		esl_set_string(profile->pass, temp_pass);
	}
	if (argv_batch || profile->batch_mode) {
		profile->batch_mode = 1;
		feature_level=0;
	}
	if (*argv_loglevel) {
		esl_set_string(profile->loglevel, argv_loglevel);
		profile->quiet = 0;
	}
	if (argv_log_uuid) {
		profile->log_uuid = 1;
	}
	esl_log(ESL_LOG_DEBUG, "Using profile %s [%s]\n", profile->name, profile->host);
	esl_set_string(prompt_color, profile->prompt_color);
	esl_set_string(input_text_color, profile->input_text_color);
	esl_set_string(output_text_color, profile->output_text_color);
	if (argv_host) {
		if (argv_port && profile->port != 8021) {
			snprintf(bare_prompt_str, sizeof(bare_prompt_str), "freeswitch@%s:%u@%s> ", profile->host, profile->port, profile->name);
		} else {
			snprintf(bare_prompt_str, sizeof(bare_prompt_str), "freeswitch@%s@%s> ", profile->host, profile->name);
		}
	} else {
		snprintf(bare_prompt_str, sizeof(bare_prompt_str), "freeswitch@%s> ", profile->name);
	}
	bare_prompt_str_len = (int)strlen(bare_prompt_str);
	if (feature_level) {
		snprintf(prompt_str, sizeof(prompt_str), "%s%s%s", prompt_color, bare_prompt_str, input_text_color);
	} else {
		snprintf(prompt_str, sizeof(prompt_str), "%s", bare_prompt_str);
	}
 connect:
	connected = 0;
	while (--loops > 0) {
		memset(&handle, 0, sizeof(handle));
		if (esl_connect_timeout(&handle, profile->host, profile->port, profile->user, profile->pass, connect_timeout)) {
			esl_global_set_default_logger(7);
			esl_log(ESL_LOG_ERROR, "Error Connecting [%s]\n", handle.err);
			if (loops == 1) {
				if (!argv_exec) usage(argv[0]);
				return -1;
			} else {
				sleep_s(1);
				esl_log(ESL_LOG_INFO, "Retrying\n");
			}
		} else {
			connected = 1;
			if (temp_log < 0 ) {
				esl_global_set_default_logger(profile->debug);
			} else {
				esl_global_set_default_logger(temp_log);
			}
			break;
		}
	}
	if (argv_exec) {
		const char *err = NULL;
		snprintf(cmd_str, sizeof(cmd_str), "api %s\nconsole_execute: true\n\n", argv_command);
		if (timeout) {
			esl_status_t status = esl_send_recv_timed(&handle, cmd_str, timeout);
			if (status != ESL_SUCCESS) {
				printf("Request timed out.\n");
				esl_disconnect(&handle);
				return -2;
			}
		} else {
			esl_send_recv(&handle, cmd_str);
		}
		if (handle.last_sr_event) {
			if (handle.last_sr_event->body) {
				printf("%s\n", handle.last_sr_event->body);
			} else if ((err = esl_event_get_header(handle.last_sr_event, "reply-text")) && !strncasecmp(err, "-err", 3)) {
				printf("Error: %s!\n", err + 4);
			}
		}
		esl_disconnect(&handle);
		return 0;
	}
	global_handle = &handle;
	global_profile = profile;

	if (esl_thread_create_detached(msg_thread_run, &handle) != ESL_SUCCESS) {
		printf("Error starting thread!\n");
		esl_disconnect(&handle);
		return 0;
	}

#ifdef HAVE_EDITLINE
	el = el_init(__FILE__, stdin, stdout, stderr);
	el_set(el, EL_PROMPT, &prompt);
	el_set(el, EL_EDITOR, "emacs");

	el_set(el, EL_ADDFN, "f1-key", "F1 KEY PRESS", console_f1key);
	el_set(el, EL_ADDFN, "f2-key", "F2 KEY PRESS", console_f2key);
	el_set(el, EL_ADDFN, "f3-key", "F3 KEY PRESS", console_f3key);
	el_set(el, EL_ADDFN, "f4-key", "F4 KEY PRESS", console_f4key);
	el_set(el, EL_ADDFN, "f5-key", "F5 KEY PRESS", console_f5key);
	el_set(el, EL_ADDFN, "f6-key", "F6 KEY PRESS", console_f6key);
	el_set(el, EL_ADDFN, "f7-key", "F7 KEY PRESS", console_f7key);
	el_set(el, EL_ADDFN, "f8-key", "F8 KEY PRESS", console_f8key);
	el_set(el, EL_ADDFN, "f9-key", "F9 KEY PRESS", console_f9key);
	el_set(el, EL_ADDFN, "f10-key", "F10 KEY PRESS", console_f10key);
	el_set(el, EL_ADDFN, "f11-key", "F11 KEY PRESS", console_f11key);
	el_set(el, EL_ADDFN, "f12-key", "F12 KEY PRESS", console_f12key);
	el_set(el, EL_ADDFN, "EOF-key", "EOF (^D) KEY PRESS", console_eofkey);

	el_set(el, EL_BIND, "\033OP", "f1-key", NULL);
	el_set(el, EL_BIND, "\033OQ", "f2-key", NULL);
	el_set(el, EL_BIND, "\033OR", "f3-key", NULL);
	el_set(el, EL_BIND, "\033OS", "f4-key", NULL);
	el_set(el, EL_BIND, "\033OT", "f5-key", NULL);
	el_set(el, EL_BIND, "\033OU", "f6-key", NULL);
	el_set(el, EL_BIND, "\033OV", "f7-key", NULL);
	el_set(el, EL_BIND, "\033OW", "f8-key", NULL);
	el_set(el, EL_BIND, "\033OX", "f9-key", NULL);
	el_set(el, EL_BIND, "\033OY", "f10-key", NULL);
	el_set(el, EL_BIND, "\033OZ", "f11-key", NULL);
	el_set(el, EL_BIND, "\033O[", "f12-key", NULL);

	el_set(el, EL_BIND, "\033[11~", "f1-key", NULL);
	el_set(el, EL_BIND, "\033[12~", "f2-key", NULL);
	el_set(el, EL_BIND, "\033[13~", "f3-key", NULL);
	el_set(el, EL_BIND, "\033[14~", "f4-key", NULL);
	el_set(el, EL_BIND, "\033[15~", "f5-key", NULL);
	el_set(el, EL_BIND, "\033[17~", "f6-key", NULL);
	el_set(el, EL_BIND, "\033[18~", "f7-key", NULL);
	el_set(el, EL_BIND, "\033[19~", "f8-key", NULL);
	el_set(el, EL_BIND, "\033[20~", "f9-key", NULL);
	el_set(el, EL_BIND, "\033[21~", "f10-key", NULL);
	el_set(el, EL_BIND, "\033[23~", "f11-key", NULL);
	el_set(el, EL_BIND, "\033[24~", "f12-key", NULL);

	el_set(el, EL_BIND, "\004", "EOF-key", NULL);

	el_set(el, EL_ADDFN, "ed-complete", "Complete argument", complete);
	el_set(el, EL_BIND, "^I", "ed-complete", NULL);

	/* "Delete" key. */
	el_set(el, EL_BIND, "\033[3~", "ed-delete-next-char", NULL);

	if (!(myhistory = history_init())) {
		esl_log(ESL_LOG_ERROR, "history could not be initialized\n");
		goto done;
	}
	history(myhistory, &ev, H_SETSIZE, 800);
	el_set(el, EL_HIST, history, myhistory);
	history(myhistory, &ev, H_LOAD, hfile);
	el_source(el, NULL);
#endif
#ifdef WIN32
	hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
	if (hStdout != INVALID_HANDLE_VALUE && GetConsoleScreenBufferInfo(hStdout, &csbiInfo)) {
		wOldColorAttrs = csbiInfo.wAttributes;
	}
#endif
	if (!argv_quiet && !profile->quiet) {
		snprintf(cmd_str, sizeof(cmd_str), "log %s\n\n", profile->loglevel);
		esl_send_recv(&handle, cmd_str);
	}
	if (global_profile->batch_mode) {
		setvbuf(stdout, (char*)NULL, _IONBF, 0);
	}
	print_banner(stdout, is_color);
	esl_log(ESL_LOG_INFO, "FS CLI Ready.\nenter /help for a list of commands.\n");
	output_printf("%s\n", handle.last_sr_reply);
	while (running > 0) {
		int r;
#ifdef HAVE_EDITLINE
		if (!(global_profile->batch_mode)) {
			line = el_gets(el, &count);
		} else {
#endif
		line = basic_gets(&count);
#ifdef HAVE_EDITLINE
		}
#endif
		if (count > 1 && !esl_strlen_zero(line)) {
			char *p, *cmd = strdup(line);
			assert(cmd);
			if ((p = strrchr(cmd, '\r')) || (p = strrchr(cmd, '\n'))) {
				*p = '\0';
			}
#ifdef HAVE_EDITLINE
			history(myhistory, &ev, H_ENTER, line);
#endif
			if ((r = process_command(&handle, cmd))) {
				running = r;
			}
			free(cmd);
			clear_el_buffer();
		}
		sleep_ms(1);
	}
	if (running < 0 && reconnect) {
		running = 1;
		loops = 120;
		goto connect;
	}
#ifdef HAVE_EDITLINE
 done:
	history(myhistory, &ev, H_SAVE, hfile);
	history_end(myhistory);
	el_end(el);
#endif
	esl_disconnect(&handle);
	global_handle = NULL;
	thread_running = 0;
	return 0;
}
Exemple #20
0
int32_t fs_opr_t::single_originate(const char* ani, const char* caller_disp,
                                   uint64_t sessionid, bool return_ring_ready, bool bgapi,
                                   char* uuid_output, uint32_t output_len,
                                   char* result, uint32_t result_len) {
    FUNC_BEGIN();
    const char* originate = "%s originate {return_ring_ready=%s,origination_caller_id_number=%s,"
                            "origination_caller_id_name=%s,IMSDATA=%lu}%s &park()";
    const char* ring_ready = return_ring_ready ? "true" : "false";
    const char* api_type = bgapi ? "bgapi" : "api";
    snprintf(szcmd, LEN_512, originate, api_type, ring_ready, caller_disp, caller_disp, sessionid, ani);
    TRACE_LOG("originate(%s)", szcmd);

    if (bgapi && esl_send_recv(&_handle, szcmd) == ESL_SUCCESS &&
            is_result_ok(fs_resp = esl_event_get_header(_handle.last_sr_event, "Reply-Text"))) {
        ret = IMS_SUCCESS;
        break;
    }

    if (esl_send_recv(&_handle, szcmd) == ESL_SUCCESS) {
        if (is_result_ok(fs_resp = esl_event_get_body(_handle.last_sr_event))) {
            TRACE_LOG("fs_resp=(%s)", fs_resp);
            fs_resp += 3;

            while (*fs_resp && ' ' == *fs_resp) {
                fs_resp++;
            }

            if (*fs_resp && uuid_output && output_len > 0) {
                strncpy(uuid_output, fs_resp, output_len);
                uint32_t idx = 0;

                do {
                    if ('\n' == uuid_output[idx]) {
                        uuid_output[idx] = '\0';
                        break;
                    }

                    ++idx;
                } while (uuid_output[idx]);
            }

            //TRACE_LOG("new create uuid = %s",uuid_output);
            ret = IMS_SUCCESS;
        } else {
            TRACE_LOG("fs_resp=(%s)", fs_resp);

            if (fs_resp && *fs_resp && result && result_len > 0) {
                strncpy(result, fs_resp + 5, result_len);
                uint32_t idx = 0;

                do {
                    if ('\n' == result[idx]) {
                        result[idx] = '\0';
                        break;
                    }

                    ++idx;
                } while (result[idx]);
            }

            WARNING_LOG("fs:single_originate(%s) failed;ret(%s)", ani, fs_resp);
        }
    }

    FUNC_END();
}
Exemple #21
0
int main(int argc, char *argv[])
{
	esl_handle_t handle = {{0}};
	int count = 0;
	const char *line = NULL;
	char cmd_str[1024] = "";
	esl_config_t cfg;
	cli_profile_t *profile = NULL;
	int rv = 0;

#ifndef WIN32
	char hfile[512] = "/etc/fs_cli_history";
	char cfile[512] = "/etc/fs_cli.conf";
	char dft_cfile[512] = "/etc/fs_cli.conf";
#else
	char hfile[512] = "fs_cli_history";
	char cfile[512] = "fs_cli.conf";
	char dft_cfile[512] = "fs_cli.conf";
#endif
	char *home = getenv("HOME");
	/* Vars for optargs */
	int opt;
	static struct option options[] = {
		{"help", 0, 0, 'h'},
		{"host", 1, 0, 'H'},
		{"port", 1, 0, 'P'},
		{"user", 1, 0, 'u'},
		{"password", 1, 0, 'p'},
		{"debug", 1, 0, 'd'},
		{"execute", 1, 0, 'x'},
		{"loglevel", 1, 0, 'l'},
		{"quiet", 0, 0, 'q'},
		{0, 0, 0, 0}
	};

	char temp_host[128];
	int argv_host = 0;
	char temp_user[256];
	char temp_pass[128];
	int argv_pass = 0 ;
	int argv_user = 0 ;
	int temp_port = 0;
	int argv_port = 0;
	int temp_log = -1;
	int argv_error = 0;
	int argv_exec = 0;
	char argv_command[256] = "";
	char argv_loglevel[128] = "";
	int argv_quiet = 0;
	

	strncpy(internal_profile.host, "127.0.0.1", sizeof(internal_profile.host));
	strncpy(internal_profile.pass, "ClueCon", sizeof(internal_profile.pass));
	strncpy(internal_profile.name, "internal", sizeof(internal_profile.name));
	internal_profile.port = 8021;
	set_fn_keys(&internal_profile);


	if (home) {
		snprintf(hfile, sizeof(hfile), "%s/.fs_cli_history", home);
		snprintf(cfile, sizeof(cfile), "%s/.fs_cli_conf", home);
	}
	
	signal(SIGINT, handle_SIGINT);
#ifdef SIGQUIT
	signal(SIGQUIT, handle_SIGQUIT);
#endif
	esl_global_set_default_logger(6); /* default debug level to 6 (info) */
	
	for(;;) {
		int option_index = 0;
		opt = getopt_long(argc, argv, "H:U:P:S:u:p:d:x:l:qh?", options, &option_index);
		if (opt == -1) break;
		switch (opt)
		{
			case 'H':
				esl_set_string(temp_host, optarg);
				argv_host = 1;
				break;
			case 'P':
				temp_port= atoi(optarg);
				if (temp_port > 0 && temp_port < 65536){
					argv_port = 1;
				} else {
					printf("ERROR: Port must be in range 1 - 65535\n");
					argv_error = 1;
				}
				break;
			case 'u':
				esl_set_string(temp_user, optarg);
				argv_user = 1;
				break;
			case 'p':
				esl_set_string(temp_pass, optarg);
				argv_pass = 1;
				break;
			case 'd':
				temp_log=atoi(optarg);
				if (temp_log < 0 || temp_log > 7){
					printf("ERROR: Debug level should be 0 - 7.\n");
					argv_error = 1;
				} else {
					esl_global_set_default_logger(temp_log);
				}
				break;
			case 'x':
				argv_exec = 1;
				esl_set_string(argv_command, optarg);
				break;
			case 'l':
				esl_set_string(argv_loglevel, optarg);
				break;
			case 'q':
				argv_quiet = 1;
				break;
				
			case 'h':
			case '?':
				print_banner(stdout);
				usage(argv[0]);
				return 0;
			default:
				opt = 0;
		}
	}
	
	if (argv_error){
		printf("\n");
		return usage(argv[0]);
	}

	if (!(rv = esl_config_open_file(&cfg, cfile))) {
		rv = esl_config_open_file(&cfg, dft_cfile);
	}

	if (rv) {
		char *var, *val;
		char cur_cat[128] = "";

		while (esl_config_next_pair(&cfg, &var, &val)) {
			if (strcmp(cur_cat, cfg.category)) {
				esl_set_string(cur_cat, cfg.category);
				esl_set_string(profiles[pcount].name, cur_cat);
				esl_set_string(profiles[pcount].host, "localhost");
				esl_set_string(profiles[pcount].pass, "ClueCon");
				profiles[pcount].port = 8021;
				set_fn_keys(&profiles[pcount]);
				esl_log(ESL_LOG_DEBUG, "Found Profile [%s]\n", profiles[pcount].name);
				pcount++;
			}
			
			if (!strcasecmp(var, "host")) {
				esl_set_string(profiles[pcount-1].host, val);
			} else if (!strcasecmp(var, "user")) {
				esl_set_string(profiles[pcount-1].user, val);
			} else if (!strcasecmp(var, "password")) {
				esl_set_string(profiles[pcount-1].pass, val);
			} else if (!strcasecmp(var, "port")) {
				int pt = atoi(val);
				if (pt > 0) {
					profiles[pcount-1].port = (esl_port_t)pt;
				}
			} else if (!strcasecmp(var, "debug")) {
				int dt = atoi(val);
				if (dt > -1 && dt < 8){
					 profiles[pcount-1].debug = dt;
				}	
 			} else if(!strcasecmp(var, "loglevel")) {
 				esl_set_string(profiles[pcount-1].loglevel, val);
 			} else if(!strcasecmp(var, "quiet")) {
 				profiles[pcount-1].quiet = esl_true(val);
			} else if (!strncasecmp(var, "key_F", 5)) {
				char *key = var + 5;

				if (key) {
					int i = atoi(key);
				
					if (i > 0 && i < 13) {
						profiles[pcount-1].console_fnkeys[i - 1] = strdup(val);
					}
				}
			} 
		}
		esl_config_close_file(&cfg);
	}
	
	if (optind < argc) {
		get_profile(argv[optind], &profile);
	}
	
	if (!profile) {
		if (get_profile("default", &profile)) {
			esl_log(ESL_LOG_DEBUG, "profile default does not exist using builtin profile\n");
			profile = &internal_profile;
		}
	}

	if (temp_log < 0 ) {
		esl_global_set_default_logger(profile->debug);
	}	

	if (argv_host) {
		esl_set_string(profile->host, temp_host);
	}
	if (argv_port) {
		profile->port = (esl_port_t)temp_port;
	}

	if (argv_user) {
		esl_set_string(profile->user, temp_user);
	}

	if (argv_pass) {
		esl_set_string(profile->pass, temp_pass);
	}
	
	if (*argv_loglevel) {
		esl_set_string(profile->loglevel, argv_loglevel);
		profile->quiet = 0;
	}

	esl_log(ESL_LOG_DEBUG, "Using profile %s [%s]\n", profile->name, profile->host);
	
	if (argv_host) {
		if (argv_port && profile->port != 8021) {
			snprintf(prompt_str, sizeof(prompt_str), "freeswitch@%s:%u@%s> ", profile->host, profile->port, profile->name);
		} else {
			snprintf(prompt_str, sizeof(prompt_str), "freeswitch@%s@%s> ", profile->host, profile->name);
		}
	} else {
		snprintf(prompt_str, sizeof(prompt_str), "freeswitch@%s> ", profile->name);
	}

	if (esl_connect(&handle, profile->host, profile->port, profile->user, profile->pass)) {
		esl_global_set_default_logger(7);
		esl_log(ESL_LOG_ERROR, "Error Connecting [%s]\n", handle.err);
		if (!argv_exec) usage(argv[0]);
		return -1;
	}


	if (argv_exec){
		const char *err = NULL;

		snprintf(cmd_str, sizeof(cmd_str), "api %s\n\n", argv_command);
		esl_send_recv(&handle, cmd_str);
		if (handle.last_sr_event) {
			if (handle.last_sr_event->body) {
				printf("%s\n", handle.last_sr_event->body);
			} else if ((err = esl_event_get_header(handle.last_sr_event, "reply-text")) && !strncasecmp(err, "-err", 3)) {
				printf("Error: %s!\n", err + 4);
			}
		}

		esl_disconnect(&handle);
		return 0;
	} 

	global_handle = &handle;
	global_profile = profile;

	esl_thread_create_detached(msg_thread_run, &handle);

#ifdef HAVE_EDITLINE
	el = el_init(__FILE__, stdout, stdout, stdout);
	el_set(el, EL_PROMPT, &prompt);
	el_set(el, EL_EDITOR, "emacs");

	myhistory = history_init();

	el_set(el, EL_ADDFN, "f1-key", "F1 KEY PRESS", console_f1key);
	el_set(el, EL_ADDFN, "f2-key", "F2 KEY PRESS", console_f2key);
	el_set(el, EL_ADDFN, "f3-key", "F3 KEY PRESS", console_f3key);
	el_set(el, EL_ADDFN, "f4-key", "F4 KEY PRESS", console_f4key);
	el_set(el, EL_ADDFN, "f5-key", "F5 KEY PRESS", console_f5key);
	el_set(el, EL_ADDFN, "f6-key", "F6 KEY PRESS", console_f6key);
	el_set(el, EL_ADDFN, "f7-key", "F7 KEY PRESS", console_f7key);
	el_set(el, EL_ADDFN, "f8-key", "F8 KEY PRESS", console_f8key);
	el_set(el, EL_ADDFN, "f9-key", "F9 KEY PRESS", console_f9key);
	el_set(el, EL_ADDFN, "f10-key", "F10 KEY PRESS", console_f10key);
	el_set(el, EL_ADDFN, "f11-key", "F11 KEY PRESS", console_f11key);
	el_set(el, EL_ADDFN, "f12-key", "F12 KEY PRESS", console_f12key);

	el_set(el, EL_ADDFN, "EOF-key", "EOF (^D) KEY PRESS", console_eofkey);

	el_set(el, EL_BIND, "\033OP", "f1-key", NULL);
	el_set(el, EL_BIND, "\033OQ", "f2-key", NULL);
	el_set(el, EL_BIND, "\033OR", "f3-key", NULL);
	el_set(el, EL_BIND, "\033OS", "f4-key", NULL);


	el_set(el, EL_BIND, "\033[11~", "f1-key", NULL);
	el_set(el, EL_BIND, "\033[12~", "f2-key", NULL);
	el_set(el, EL_BIND, "\033[13~", "f3-key", NULL);
	el_set(el, EL_BIND, "\033[14~", "f4-key", NULL);
	el_set(el, EL_BIND, "\033[15~", "f5-key", NULL);
	el_set(el, EL_BIND, "\033[17~", "f6-key", NULL);
	el_set(el, EL_BIND, "\033[18~", "f7-key", NULL);
	el_set(el, EL_BIND, "\033[19~", "f8-key", NULL);
	el_set(el, EL_BIND, "\033[20~", "f9-key", NULL);
	el_set(el, EL_BIND, "\033[21~", "f10-key", NULL);
	el_set(el, EL_BIND, "\033[23~", "f11-key", NULL);
	el_set(el, EL_BIND, "\033[24~", "f12-key", NULL);

	el_set(el, EL_BIND, "\004", "EOF-key", NULL);

	el_set(el, EL_ADDFN, "ed-complete", "Complete argument", complete);
	el_set(el, EL_BIND, "^I", "ed-complete", NULL);

	if (myhistory == 0) {
		esl_log(ESL_LOG_ERROR, "history could not be initialized\n");
		goto done;
	}

	history(myhistory, &ev, H_SETSIZE, 800);
	el_set(el, EL_HIST, history, myhistory);
	history(myhistory, &ev, H_LOAD, hfile);

	el_source(el, NULL);

#endif
#ifdef WIN32
	hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
	if (hStdout != INVALID_HANDLE_VALUE && GetConsoleScreenBufferInfo(hStdout, &csbiInfo)) {
		wOldColorAttrs = csbiInfo.wAttributes;
	}
#endif

	if (!argv_quiet && !profile->quiet) {
		snprintf(cmd_str, sizeof(cmd_str), "log %s\n\n", profile->loglevel);	
		esl_send_recv(&handle, cmd_str);
	}

	print_banner(stdout);

	esl_log(ESL_LOG_INFO, "FS CLI Ready.\nenter /help for a list of commands.\n");
	printf("%s\n", handle.last_sr_reply);

	while (running) {

#ifdef HAVE_EDITLINE
		line = el_gets(el, &count);
#else
		line = basic_gets(&count);
#endif

		if (count > 1) {
			if (!esl_strlen_zero(line)) {
				char *cmd = strdup(line);
				char *p;

#ifdef HAVE_EDITLINE
				const LineInfo *lf = el_line(el);
				char *foo = (char *) lf->buffer;
#endif

				if ((p = strrchr(cmd, '\r')) || (p = strrchr(cmd, '\n'))) {
					*p = '\0';
				}
				assert(cmd != NULL);

#ifdef HAVE_EDITLINE
				history(myhistory, &ev, H_ENTER, line);
#endif
				
				if (process_command(&handle, cmd)) {
					running = 0;
				}

#ifdef HAVE_EDITLINE
				el_deletestr(el, strlen(foo) + 1);
				memset(foo, 0, strlen(foo));
#endif
				free(cmd);
			}
		}

		usleep(1000);

	}

#ifdef HAVE_EDITLINE
 done:
	history(myhistory, &ev, H_SAVE, hfile);

	/* Clean up our memory */
	history_end(myhistory);
	el_end(el);
#endif

	esl_disconnect(&handle);
	
	thread_running = 0;

	return 0;
}