static void do_rotate(cdr_fd_t *fd) { switch_time_exp_t tm; char date[80] = ""; switch_size_t retsize; char *p; close(fd->fd); fd->fd = -1; if (globals.rotate) { switch_time_exp_lt(&tm, switch_micro_time_now()); switch_strftime_nocheck(date, &retsize, sizeof(date), "%Y-%m-%d-%H-%M-%S", &tm); p = switch_mprintf("%s.%s", fd->path, date); assert(p); switch_file_rename(fd->path, p, globals.pool); free(p); } do_reopen(fd); if (fd->fd < 0) { switch_event_t *event; switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Error opening %s\n", fd->path); if (switch_event_create(&event, SWITCH_EVENT_TRAP) == SWITCH_STATUS_SUCCESS) { switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Critical-Error", "Error opening cdr file %s\n", fd->path); switch_event_fire(&event); } } else { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "%s CDR logfile %s\n", globals.rotate ? "Rotated" : "Re-opened", fd->path); } }
static switch_status_t do_snap(switch_core_session_t *session) { switch_channel_t *channel = switch_core_session_get_channel(session); switch_media_bug_t *bug = switch_channel_get_private(channel, "snapshot"); char *file; switch_file_handle_t fh = { 0 }; switch_codec_implementation_t read_impl = { 0 }; switch_size_t bytes_read; int16_t pdata[4096] = { 0 }; if (bug) { switch_time_exp_t tm; switch_size_t retsize; char date[80] = ""; struct cap_cb *cb = (struct cap_cb *) switch_core_media_bug_get_user_data(bug); if (!cb) { return SWITCH_STATUS_FALSE; } switch_time_exp_lt(&tm, switch_time_make(switch_epoch_time_now(NULL), 0)); switch_strftime(date, &retsize, sizeof(date), "%Y_%m_%d_%H_%M_%S", &tm); file = switch_core_session_sprintf(session, "%s%s%s_%s.wav", SWITCH_GLOBAL_dirs.sounds_dir, SWITCH_PATH_SEPARATOR, cb->base, date); switch_core_session_get_read_impl(session, &read_impl); fh.channels = 0; fh.native_rate = read_impl.actual_samples_per_second; if (switch_core_file_open(&fh, file, 0, read_impl.actual_samples_per_second, SWITCH_FILE_FLAG_WRITE | SWITCH_FILE_DATA_SHORT, NULL) != SWITCH_STATUS_SUCCESS) { switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Error opening %s\n", file); switch_channel_hangup(channel, SWITCH_CAUSE_DESTINATION_OUT_OF_ORDER); switch_core_session_reset(session, SWITCH_TRUE, SWITCH_TRUE); return SWITCH_STATUS_FALSE; } switch_mutex_lock(cb->mutex); while ((bytes_read = switch_buffer_read(cb->buffer, pdata, sizeof(pdata)))) { switch_size_t samples = bytes_read / 2; if (switch_core_file_write(&fh, pdata, &samples) != SWITCH_STATUS_SUCCESS) { break; } } switch_mutex_unlock(cb->mutex); switch_core_file_close(&fh); switch_core_set_variable("file", file); switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_INFO, "Wrote %s\n", file); return SWITCH_STATUS_SUCCESS; } switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "%s Bug is not attached.\n", switch_channel_get_name(channel)); return SWITCH_STATUS_FALSE; }
/* rotate the log file */ static switch_status_t mod_logfile_rotate(logfile_profile_t *profile) { unsigned int i = 0; char *filename = NULL; switch_status_t stat = 0; int64_t offset = 0; switch_memory_pool_t *pool = NULL; switch_time_exp_t tm; char date[80] = ""; switch_size_t retsize; switch_status_t status = SWITCH_STATUS_SUCCESS; switch_mutex_lock(globals.mutex); switch_time_exp_lt(&tm, switch_micro_time_now()); switch_strftime_nocheck(date, &retsize, sizeof(date), "%Y-%m-%d-%H-%M-%S", &tm); profile->log_size = 0; stat = switch_file_seek(profile->log_afd, SWITCH_SEEK_SET, &offset); if (stat != SWITCH_STATUS_SUCCESS) { status = SWITCH_STATUS_FALSE; goto end; } switch_core_new_memory_pool(&pool); filename = switch_core_alloc(pool, strlen(profile->logfile) + WARM_FUZZY_OFFSET); for (i = 1; i < MAX_ROT; i++) { sprintf((char *) filename, "%s.%s.%i", profile->logfile, date, i); if (switch_file_exists(filename, pool) == SWITCH_STATUS_SUCCESS) { continue; } switch_file_close(profile->log_afd); switch_file_rename(profile->logfile, filename, pool); if ((status = mod_logfile_openlogfile(profile, SWITCH_FALSE)) != SWITCH_STATUS_SUCCESS) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Error Rotating Log!\n"); goto end; } break; } switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "New log started.\n"); end: if (pool) { switch_core_destroy_memory_pool(&pool); } switch_mutex_unlock(globals.mutex); return status; }
SWITCH_DECLARE(void) switch_console_printf(switch_text_channel_t channel, const char *file, const char *func, int line, const char *fmt, ...) { char *data = NULL; int ret = 0; va_list ap; FILE *handle = switch_core_data_channel(channel); const char *filep = switch_cut_path(file); char date[80] = ""; switch_size_t retsize; switch_time_exp_t tm; switch_event_t *event; va_start(ap, fmt); ret = switch_vasprintf(&data, fmt, ap); va_end(ap); if (ret == -1) { fprintf(stderr, "Memory Error\n"); goto done; } if (channel == SWITCH_CHANNEL_ID_LOG_CLEAN) { fprintf(handle, "%s", data); goto done; } switch_time_exp_lt(&tm, switch_micro_time_now()); switch_strftime_nocheck(date, &retsize, sizeof(date), "%Y-%m-%d %T", &tm); if (channel == SWITCH_CHANNEL_ID_LOG) { fprintf(handle, "[%d] %s %s:%d %s() %s", (int) getpid(), date, filep, line, func, data); goto done; } if (channel == SWITCH_CHANNEL_ID_EVENT && switch_event_running() == SWITCH_STATUS_SUCCESS && switch_event_create(&event, SWITCH_EVENT_LOG) == SWITCH_STATUS_SUCCESS) { switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Log-Data", data); switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Log-File", filep); switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Log-Function", func); switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Log-Line", "%d", line); switch_event_fire(&event); } done: if (data) { free(data); } fflush(handle); }
switch_status_t perform_send_define_current_time_date(listener_t *listener, const char *file, const char *func, int line) { switch_time_t ts; switch_time_exp_t tm; ts = switch_micro_time_now(); switch_time_exp_lt(&tm, ts); return send_define_time_date(listener, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_wday, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, tm.tm_usec / 1000, ts / 1000000); }
static switch_status_t set_json_cdr_log_dirs() { switch_time_exp_t tm; char *path = NULL; char date[80] = ""; switch_size_t retsize; switch_status_t status = SWITCH_STATUS_SUCCESS, dir_status; int err_dir_index; switch_time_exp_lt(&tm, switch_micro_time_now()); switch_strftime_nocheck(date, &retsize, sizeof(date), "%Y-%m-%d-%H-%M-%S", &tm); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Rotating log file paths\n"); if (!zstr(globals.base_log_dir)) { if (globals.rotate) { if ((path = switch_mprintf("%s%s%s", globals.base_log_dir, SWITCH_PATH_SEPARATOR, date))) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Rotating log file path to %s\n", path); dir_status = SWITCH_STATUS_SUCCESS; if (switch_directory_exists(path, globals.pool) != SWITCH_STATUS_SUCCESS) { dir_status = switch_dir_make(path, SWITCH_FPROT_OS_DEFAULT, globals.pool); } if (dir_status == SWITCH_STATUS_SUCCESS) { switch_thread_rwlock_wrlock(globals.log_path_lock); switch_safe_free(globals.log_dir); globals.log_dir = path; switch_thread_rwlock_unlock(globals.log_path_lock); } else { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Failed to create new mod_json_cdr log_dir path\n"); switch_safe_free(path); status = SWITCH_STATUS_FALSE; } } else { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Failed to generate new mod_json_cdr log_dir path\n"); status = SWITCH_STATUS_FALSE; } } else { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Setting log file path to %s\n", globals.base_log_dir); if ((path = switch_safe_strdup(globals.base_log_dir))) { switch_thread_rwlock_wrlock(globals.log_path_lock); switch_safe_free(globals.log_dir); switch_dir_make_recursive(path, SWITCH_DEFAULT_DIR_PERMS, globals.pool); globals.log_dir = path; switch_thread_rwlock_unlock(globals.log_path_lock); } else { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Failed to set log_dir path\n"); status = SWITCH_STATUS_FALSE; } } } for (err_dir_index = 0; err_dir_index < globals.err_dir_count; err_dir_index++) { if (globals.rotate) { if ((path = switch_mprintf("%s%s%s", globals.base_err_log_dir[err_dir_index], SWITCH_PATH_SEPARATOR, date))) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Rotating err log file path to %s\n", path); dir_status = SWITCH_STATUS_SUCCESS; if (switch_directory_exists(path, globals.pool) != SWITCH_STATUS_SUCCESS) { dir_status = switch_dir_make(path, SWITCH_FPROT_OS_DEFAULT, globals.pool); } if (dir_status == SWITCH_STATUS_SUCCESS) { switch_thread_rwlock_wrlock(globals.log_path_lock); switch_safe_free(globals.err_log_dir[err_dir_index]); globals.err_log_dir[err_dir_index] = path; switch_thread_rwlock_unlock(globals.log_path_lock); } else { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Failed to create new mod_json_cdr err_log_dir path\n"); switch_safe_free(path); status = SWITCH_STATUS_FALSE; } } else { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Failed to generate new mod_json_cdr err_log_dir path\n"); status = SWITCH_STATUS_FALSE; } } else { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Setting err log file path to %s\n", globals.base_err_log_dir[err_dir_index]); if ((path = switch_safe_strdup(globals.base_err_log_dir[err_dir_index]))) { switch_thread_rwlock_wrlock(globals.log_path_lock); switch_safe_free(globals.err_log_dir[err_dir_index]); switch_dir_make_recursive(path, SWITCH_DEFAULT_DIR_PERMS, globals.pool); globals.err_log_dir[err_dir_index] = path; switch_thread_rwlock_unlock(globals.log_path_lock); } else { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Failed to set err_log_dir path\n"); status = SWITCH_STATUS_FALSE; } } } return status; }
/* This is where we actually charge the guy This can be called anytime a call is in progress or at the end of a call before the session is destroyed */ static switch_status_t do_billing(switch_core_session_t *session) { /* FS vars we will use */ switch_channel_t *channel; switch_caller_profile_t *profile; /* Local vars */ nibble_data_t *nibble_data; switch_time_t ts = switch_micro_time_now(); double billamount; char date[80] = ""; char *uuid; switch_size_t retsize; switch_time_exp_t tm; const char *billrate; const char *billincrement; const char *billaccount; double nobal_amt = globals.nobal_amt; double lowbal_amt = globals.lowbal_amt; double balance; if (!session) { /* Why are we here? */ return SWITCH_STATUS_SUCCESS; } uuid = switch_core_session_get_uuid(session); /* Get channel var */ if (!(channel = switch_core_session_get_channel(session))) { return SWITCH_STATUS_SUCCESS; } /* Variables kept in FS but relevant only to this module */ billrate = switch_channel_get_variable(channel, "nibble_rate"); billincrement = switch_channel_get_variable(channel, "nibble_increment"); billaccount = switch_channel_get_variable(channel, "nibble_account"); if (!zstr(switch_channel_get_variable(channel, "nobal_amt"))) { nobal_amt = atof(switch_channel_get_variable(channel, "nobal_amt")); } if (!zstr(switch_channel_get_variable(channel, "lowbal_amt"))) { lowbal_amt = atof(switch_channel_get_variable(channel, "lowbal_amt")); } /* Return if there's no billing information on this session */ if (!billrate || !billaccount) { return SWITCH_STATUS_SUCCESS; } switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Attempting to bill at $%s per minute to account %s\n", billrate, billaccount); /* Get caller profile info from channel */ profile = switch_channel_get_caller_profile(channel); if (!profile || !profile->times) { /* No caller profile (why would this happen?) */ return SWITCH_STATUS_SUCCESS; } if (profile->times->answered < 1) { switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Not billing %s - call is not in answered state\n", billaccount); /* See if this person has enough money left to continue the call */ balance = get_balance(billaccount, channel); switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Comparing %f to hangup balance of %f\n", balance, nobal_amt); if (balance <= nobal_amt) { /* Not enough money - reroute call to nobal location */ switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Balance of %f fell below allowed amount of %f! (Account %s)\n", balance, nobal_amt, billaccount); transfer_call(session, globals.nobal_action); } return SWITCH_STATUS_SUCCESS; } /* Lock this session's data for this module while we tinker with it */ if (globals.mutex) { switch_mutex_lock(globals.mutex); } /* Get our nibble data var. This will be NULL if it's our first call here for this session */ nibble_data = (nibble_data_t *) switch_channel_get_private(channel, "_nibble_data_"); /* Are we in paused mode? If so, we don't do anything here - go back! */ if (nibble_data && (nibble_data->pausets > 0)) { if (globals.mutex) { switch_mutex_unlock(globals.mutex); } switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Received heartbeat, but we're paused - ignoring\n"); return SWITCH_STATUS_SUCCESS; } /* Have we done any billing on this channel yet? If no, set up vars for doing so */ if (!nibble_data) { nibble_data = switch_core_session_alloc(session, sizeof(*nibble_data)); memset(nibble_data, 0, sizeof(*nibble_data)); /* Setup new billing data (based on call answer time, in case this module started late with active calls) */ nibble_data->lastts = profile->times->answered; /* Set the initial answer time to match when the call was really answered */ switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_INFO, "Beginning new billing on %s\n", uuid); } switch_time_exp_lt(&tm, nibble_data->lastts); switch_strftime_nocheck(date, &retsize, sizeof(date), "%Y-%m-%d %T", &tm); switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "%d seconds passed since last bill time of %s\n", (int) ((ts - nibble_data->lastts) / 1000000), date); if ((ts - nibble_data->lastts) >= 0) { /* If billincrement is set we bill by it and not by time elapsed */ if (!(switch_strlen_zero(billincrement))) { switch_time_t chargedunits = (ts - nibble_data->lastts) / 1000000 <= atol(billincrement) ? atol(billincrement) * 1000000 : (switch_time_t)(ceil((ts - nibble_data->lastts) / (atol(billincrement) * 1000000.0))) * atol(billincrement) * 1000000; billamount = (atof(billrate) / 1000000 / 60) * chargedunits - nibble_data->bill_adjustments; /* Account for the prepaid amount */ nibble_data->lastts += chargedunits; } else { /* Convert billrate into microseconds and multiply by # of microseconds that have passed since last *successful* bill */ billamount = (atof(billrate) / 1000000 / 60) * ((ts - nibble_data->lastts)) - nibble_data->bill_adjustments; /* Update the last time we billed */ nibble_data->lastts = ts; } switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Billing $%f to %s (Call: %s / %f so far)\n", billamount, billaccount, uuid, nibble_data->total); /* DO ODBC BILLING HERE and reset counters if it's successful! */ if (bill_event(billamount, billaccount, channel) == SWITCH_STATUS_SUCCESS) { /* Increment total cost */ nibble_data->total += billamount; /* Reset manual billing adjustments from pausing */ nibble_data->bill_adjustments = 0; /* Update channel variable with current billing */ switch_channel_set_variable_printf(channel, "nibble_total_billed", "%f", nibble_data->total); } else { switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_CRIT, "Failed to log to database!\n"); } } else { if (switch_strlen_zero(billincrement)) switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_WARNING, "Just tried to bill %s negative minutes! That should be impossible.\n", uuid); } /* Save this location */ if (channel) { switch_channel_set_private(channel, "_nibble_data_", nibble_data); /* don't verify balance and transfer to nobal if we're done with call */ if (switch_channel_get_state(channel) != CS_REPORTING && switch_channel_get_state(channel) != CS_HANGUP) { balance = get_balance(billaccount, channel); /* See if we've achieved low balance */ if (!nibble_data->lowbal_action_executed && balance <= lowbal_amt) { switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Balance of %f fell below low balance amount of %f! (Account %s)\n", balance, lowbal_amt, billaccount); if (exec_app(session, globals.lowbal_action) != SWITCH_STATUS_SUCCESS) switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "Low balance action didn't execute\n"); else nibble_data->lowbal_action_executed = 1; } /* See if this person has enough money left to continue the call */ if (balance <= nobal_amt) { /* Not enough money - reroute call to nobal location */ switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_CRIT, "Balance of %f fell below allowed amount of %f! (Account %s)\n", balance, nobal_amt, billaccount); /* IMPORTANT: Billing must be paused before the transfer occurs! This prevents infinite loops, since the transfer will result */ /* in nibblebill checking the call again in the routing process for an allowed balance! */ /* If you intend to give the user the option to re-up their balance, you must clear & resume billing once the balance is updated! */ nibblebill_pause(session); transfer_call(session, globals.nobal_action); } } } /* Done changing - release lock */ if (globals.mutex) { switch_mutex_unlock(globals.mutex); } /* Go check if this call is allowed to continue */ return SWITCH_STATUS_SUCCESS; }
static switch_status_t my_on_reporting(switch_core_session_t *session) { switch_xml_t cdr = NULL; switch_channel_t *channel = switch_core_session_get_channel(session); rc_handle *rad_config; switch_status_t retval = SWITCH_STATUS_TERM; VALUE_PAIR *send = NULL; uint32_t client_port = 0; uint32_t framed_addr = 0; uint32_t status_type = PW_STATUS_STOP; switch_time_t callstartdate = 0; switch_time_t callanswerdate = 0; switch_time_t callenddate = 0; switch_time_t calltransferdate = 0; switch_time_t billusec = 0; uint32_t billsec = 0; char *uuid_str; switch_time_exp_t tm; char buffer[32] = ""; if (globals.shutdown) { return SWITCH_STATUS_FALSE; } if (channel) { const char *disable_flag = switch_channel_get_variable(channel, "disable_radius_stop"); if (switch_true(disable_flag)) { switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "[mod_radius_cdr] Not Sending RADIUS Stop\n"); return SWITCH_STATUS_SUCCESS; } } switch_thread_rwlock_rdlock(globals.rwlock); switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "[mod_radius_cdr] Entering my_on_reporting\n"); rad_config = my_radius_init(); if (rad_config == NULL) { switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "[mod_radius_cdr] Error initializing radius, session not logged.\n"); goto end; } if (switch_ivr_generate_xml_cdr(session, &cdr) == SWITCH_STATUS_SUCCESS) { uuid_str = switch_core_session_get_uuid(session); } else { switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "[mod_radius_cdr] Error Generating Data!\n"); goto end; } /* Create the radius packet */ /* Set Status Type */ if (rc_avpair_add(rad_config, &send, PW_ACCT_STATUS_TYPE, &status_type, -1, 0) == NULL) { switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "failed adding Acct-Session-ID: %s\n", uuid_str); rc_destroy(rad_config); goto end; } if (rc_avpair_add(rad_config, &send, PW_ACCT_SESSION_ID, uuid_str, -1, 0) == NULL) { switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "failed adding Acct-Session-ID: %s\n", uuid_str); rc_destroy(rad_config); goto end; } /* Add VSAs */ if (channel) { switch_call_cause_t cause; switch_caller_profile_t *profile; cause = switch_channel_get_cause(channel); if (rc_avpair_add(rad_config, &send, PW_FS_HANGUPCAUSE, &cause, -1, PW_FS_PEC) == NULL) { switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "failed adding Freeswitch-Hangupcause: %d\n", cause); rc_destroy(rad_config); goto end; } profile = switch_channel_get_caller_profile(channel); if (profile) { switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "[mod_radius_cdr] Calculating billable time\n"); /* calculate billable time */ callstartdate = profile->times->created; callanswerdate = profile->times->answered; calltransferdate = profile->times->transferred; callenddate = profile->times->hungup; if (switch_channel_test_flag(channel, CF_ANSWERED)) { if (callstartdate && callanswerdate) { if (callenddate) billusec = callenddate - callanswerdate; else if (calltransferdate) billusec = calltransferdate - callanswerdate; } } else if (switch_channel_test_flag(channel, CF_TRANSFER)) { if (callanswerdate && calltransferdate) billusec = calltransferdate - callanswerdate; } billsec = (billusec / 1000000); switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "[mod_radius_cdr] Finished calculating billable time\n"); if (profile->username) { if (rc_avpair_add(rad_config, &send, PW_USER_NAME, (void *) profile->username, -1, 0) == NULL) { switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "failed adding User-Name: %s\n", profile->username); rc_destroy(rad_config); goto end; } } if (profile->caller_id_number) { if (rc_avpair_add(rad_config, &send, PW_FS_SRC, (void *) profile->caller_id_number, -1, PW_FS_PEC) == NULL) { switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "failed adding Freeswitch-Src: %s\n", profile->caller_id_number); rc_destroy(rad_config); goto end; } } if (profile->caller_id_name) { if (rc_avpair_add(rad_config, &send, PW_FS_CLID, (void *) profile->caller_id_name, -1, PW_FS_PEC) == NULL) { switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "failed adding Freeswitch-CLID: %s\n", profile->caller_id_name); rc_destroy(rad_config); goto end; } } if (profile->destination_number) { if (rc_avpair_add(rad_config, &send, PW_FS_DST, (void *) profile->destination_number, -1, PW_FS_PEC) == NULL) { switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "failed adding Freeswitch-Dst: %s\n", profile->destination_number); rc_destroy(rad_config); goto end; } } if (profile->dialplan) { if (rc_avpair_add(rad_config, &send, PW_FS_DIALPLAN, (void *) profile->dialplan, -1, PW_FS_PEC) == NULL) { switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "failed adding Freeswitch-Dialplan: %s\n", profile->dialplan); rc_destroy(rad_config); goto end; } } if (profile->network_addr) { inet_pton(AF_INET, (void *) profile->network_addr, &framed_addr); framed_addr = htonl(framed_addr); if (rc_avpair_add(rad_config, &send, PW_FRAMED_IP_ADDRESS, &framed_addr, -1, 0) == NULL) { switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "failed adding Framed-IP-Address: %s\n", profile->network_addr); rc_destroy(rad_config); goto end; } } if (profile->rdnis) { if (rc_avpair_add(rad_config, &send, PW_FS_RDNIS, (void *) profile->rdnis, -1, PW_FS_PEC) == NULL) { switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "failed adding Freeswitch-RDNIS: %s\n", profile->rdnis); rc_destroy(rad_config); goto end; } } if (profile->context) { if (rc_avpair_add(rad_config, &send, PW_FS_CONTEXT, (void *) profile->context, -1, PW_FS_PEC) == NULL) { switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "failed adding Freeswitch-Context: %s\n", profile->context); rc_destroy(rad_config); goto end; } } if (profile->ani) { if (rc_avpair_add(rad_config, &send, PW_FS_ANI, (void *) profile->ani, -1, PW_FS_PEC) == NULL) { switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "failed adding Freeswitch-ANI: %s\n", profile->ani); rc_destroy(rad_config); goto end; } } if (profile->aniii) { if (rc_avpair_add(rad_config, &send, PW_FS_ANIII, (void *) profile->aniii, -1, PW_FS_PEC) == NULL) { switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "failed adding Freeswitch-ANIII: %s\n", profile->aniii); rc_destroy(rad_config); goto end; } } if (profile->source) { if (rc_avpair_add(rad_config, &send, PW_FS_SOURCE, (void *) profile->source, -1, PW_FS_PEC) == NULL) { switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "failed adding Freeswitch-Source: %s\n", profile->source); rc_destroy(rad_config); goto end; } } if (profile->caller_extension && profile->caller_extension->last_application && profile->caller_extension->last_application->application_name) { if (rc_avpair_add(rad_config, &send, PW_FS_LASTAPP, (void *) profile->caller_extension->last_application->application_name, -1, PW_FS_PEC) == NULL) { switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "failed adding Freeswitch-Lastapp: %s\n", profile->source); rc_destroy(rad_config); goto end; } } if (rc_avpair_add(rad_config, &send, PW_FS_BILLUSEC, &billusec, -1, PW_FS_PEC) == NULL) { switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "failed adding Freeswitch-Billusec: %u\n", (uint32_t) billusec); rc_destroy(rad_config); goto end; } if (callstartdate > 0) { switch_time_exp_lt(&tm, callstartdate); switch_snprintf(buffer, sizeof(buffer), "%04u-%02u-%02uT%02u:%02u:%02u.%06u%+03d%02d", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, tm.tm_usec, tm.tm_gmtoff / 3600, tm.tm_gmtoff % 3600); if (rc_avpair_add(rad_config, &send, PW_FS_CALLSTARTDATE, &buffer, -1, PW_FS_PEC) == NULL) { switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "failed adding Freeswitch-Callstartdate: %s\n", buffer); rc_destroy(rad_config); goto end; } } if (callanswerdate > 0) { switch_time_exp_lt(&tm, callanswerdate); switch_snprintf(buffer, sizeof(buffer), "%04u-%02u-%02uT%02u:%02u:%02u.%06u%+03d%02d", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, tm.tm_usec, tm.tm_gmtoff / 3600, tm.tm_gmtoff % 3600); if (rc_avpair_add(rad_config, &send, PW_FS_CALLANSWERDATE, &buffer, -1, PW_FS_PEC) == NULL) { switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "failed adding Freeswitch-Callanswerdate: %s\n", buffer); rc_destroy(rad_config); goto end; } } if (calltransferdate > 0) { switch_time_exp_lt(&tm, calltransferdate); switch_snprintf(buffer, sizeof(buffer), "%04u-%02u-%02uT%02u:%02u:%02u.%06u%+03d%02d", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, tm.tm_usec, tm.tm_gmtoff / 3600, tm.tm_gmtoff % 3600); if (rc_avpair_add(rad_config, &send, PW_FS_CALLTRANSFERDATE, &buffer, -1, PW_FS_PEC) == NULL) { switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "failed adding Freeswitch-Calltransferdate: %s\n", buffer); rc_destroy(rad_config); goto end; } } if (callenddate > 0) { switch_time_exp_lt(&tm, callenddate); switch_snprintf(buffer, sizeof(buffer), "%04u-%02u-%02uT%02u:%02u:%02u.%06u%+03d%02d", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, tm.tm_usec, tm.tm_gmtoff / 3600, tm.tm_gmtoff % 3600); if (rc_avpair_add(rad_config, &send, PW_FS_CALLENDDATE, &buffer, -1, PW_FS_PEC) == NULL) { switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "failed adding Freeswitch-Callenddate: %s\n", buffer); rc_destroy(rad_config); goto end; } } if (rc_avpair_add(rad_config, &send, PW_ACCT_SESSION_TIME, &billsec, -1, 0) == NULL) { switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "failed adding Acct-Session-Time: %u\n", billsec); rc_destroy(rad_config); goto end; } { const char *direction_str = profile->direction == SWITCH_CALL_DIRECTION_INBOUND ? "inbound" : "outbound"; if (rc_avpair_add(rad_config, &send, PW_FS_DIRECTION, (void *) direction_str, -1, PW_FS_PEC) == NULL) { switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "failed adding Freeswitch-Direction: %s\n", direction_str); rc_destroy(rad_config); goto end; } } } else { /* no profile, can't create data to send */ switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "profile == NULL\n"); } } if (rc_acct(rad_config, client_port, send) == OK_RC) { switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "RADIUS Accounting OK\n"); retval = SWITCH_STATUS_SUCCESS; } else { switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "RADIUS Accounting Failed\n"); retval = SWITCH_STATUS_TERM; } rc_avpair_free(send); rc_destroy(rad_config); end: switch_xml_free(cdr); switch_thread_rwlock_unlock(globals.rwlock); return (retval); }
static switch_status_t hu_say_time(switch_core_session_t *session, char *tosay, switch_say_args_t *say_args, switch_input_args_t *args) { int32_t t; switch_time_t target = 0, target_now = 0; switch_time_exp_t tm, tm_now; uint8_t say_date = 0, say_time = 0, say_year = 0, say_month = 0, say_dow = 0, say_day = 0, say_yesterday = 0, say_today = 0; switch_channel_t *channel = switch_core_session_get_channel(session); const char *tz = switch_channel_get_variable(channel, "timezone"); if (say_args->type == SST_TIME_MEASUREMENT) { int64_t hours = 0; int64_t minutes = 0; int64_t seconds = 0; int64_t r = 0; if (strchr(tosay, ':')) { char *tme = switch_core_session_strdup(session, tosay); char *p; if ((p = strrchr(tme, ':'))) { *p++ = '\0'; seconds = atoi(p); if ((p = strchr(tme, ':'))) { *p++ = '\0'; minutes = atoi(p); if (tme) { hours = atoi(tme); } } else { minutes = atoi(tme); } } } else { if ((seconds = atol(tosay)) <= 0) { seconds = (int64_t) switch_epoch_time_now(NULL); } if (seconds >= 60) { minutes = seconds / 60; r = seconds % 60; seconds = r; } if (minutes >= 60) { hours = minutes / 60; r = minutes % 60; minutes = r; } } if (hours) { say_num(hours, SSM_PRONOUNCED); say_file("time/hour.wav"); } else { say_file("digits/0.wav"); say_file("time/hour.wav"); } if (minutes) { say_num(minutes, SSM_PRONOUNCED); say_file("time/minute.wav"); } else { say_file("digits/0.wav"); say_file("time/minute.wav"); } if (seconds) { say_num(seconds, SSM_PRONOUNCED); say_file("time/second.wav"); } else { say_file("digits/0.wav"); say_file("time/second.wav"); } return SWITCH_STATUS_SUCCESS; } if ((t = atol(tosay)) > 0) { target = switch_time_make(t, 0); target_now = switch_micro_time_now(); } else { target = switch_micro_time_now(); target_now = switch_micro_time_now(); } if (tz) { int check = atoi(tz); switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Timezone is [%s]\n", tz); if (check) { switch_time_exp_tz(&tm, target, check); switch_time_exp_tz(&tm_now, target_now, check); } else { switch_time_exp_tz_name(tz, &tm, target); switch_time_exp_tz_name(tz, &tm_now, target_now); } } else { switch_time_exp_lt(&tm, target); switch_time_exp_lt(&tm_now, target_now); } switch (say_args->type) { case SST_CURRENT_DATE_TIME: say_date = say_time = 1; break; case SST_CURRENT_DATE: say_date = 1; break; case SST_CURRENT_TIME: say_time = 1; break; case SST_SHORT_DATE_TIME: say_time = 1; if (tm.tm_year != tm_now.tm_year) { say_date = 1; break; } if (tm.tm_yday == tm_now.tm_yday) { say_today = 1; break; } if (tm.tm_yday == tm_now.tm_yday - 1) { say_yesterday = 1; break; } if (tm.tm_yday >= tm_now.tm_yday - 5) { say_dow = 1; break; } if (tm.tm_mon != tm_now.tm_mon) { say_month = say_day = say_dow = 1; break; } say_month = say_day = say_dow = 1; break; default: break; } if (say_today) { say_file("time/today.wav"); } if (say_yesterday) { say_file("time/yesterday.wav"); } if (say_dow) { say_file("time/day-%d.wav", tm.tm_wday); } if (say_date) { say_year = say_month = say_day = say_dow = 1; say_today = say_yesterday = 0; } if (say_year) { say_num(tm.tm_year + 1900, SSM_PRONOUNCED); } if (say_month) { say_file("time/mon-%d.wav", tm.tm_mon); } if (say_day) { say_num(tm.tm_mday, SSM_PRONOUNCED); } if (say_time) { say_num(tm.tm_hour, SSM_PRONOUNCED); say_file("time/hour.wav"); say_num(tm.tm_min, SSM_PRONOUNCED); say_file("time/minute.wav"); } return SWITCH_STATUS_SUCCESS; }
static switch_status_t de_say_time(switch_core_session_t *session, char *tosay, switch_say_args_t *say_args, switch_input_args_t *args) { int32_t t; switch_time_t target = 0; switch_time_exp_t tm; uint8_t say_date = 0, say_time = 0; if (say_args->type == SST_TIME_MEASUREMENT) { int64_t hours = 0; int64_t minutes = 0; int64_t seconds = 0; int64_t r = 0; if (strchr(tosay, ':')) { char *tme = switch_core_session_strdup(session, tosay); char *p; if ((p = strrchr(tme, ':'))) { *p++ = '\0'; seconds = atoi(p); if ((p = strchr(tme, ':'))) { *p++ = '\0'; minutes = atoi(p); if (tme) { hours = atoi(tme); } } else { minutes = atoi(tme); } } } else { if ((seconds = atoi(tosay)) <= 0) { seconds = (int64_t) switch_epoch_time_now(NULL); } if (seconds >= 60) { minutes = seconds / 60; r = seconds % 60; seconds = r; } if (minutes >= 60) { hours = minutes / 60; r = minutes % 60; minutes = r; } } if (hours) { say_num(hours, SSM_PRONOUNCED); if (hours == 1) { say_file("time/hour.wav"); } else { say_file("time/hours.wav"); } } else { say_file("digits/0.wav"); say_file("time/hours.wav"); } if (minutes) { say_num(minutes, SSM_PRONOUNCED); if (minutes == 1) { say_file("time/minute.wav"); } else { say_file("time/minutes.wav"); } } else { say_file("digits/0.wav"); say_file("time/minutes.wav"); } if (seconds) { say_num(seconds, SSM_PRONOUNCED); if (seconds == 1) { say_file("time/second.wav"); } else { say_file("time/seconds.wav"); } } else { say_file("digits/0.wav"); say_file("time/seconds.wav"); } return SWITCH_STATUS_SUCCESS; } if ((t = atoi(tosay)) > 0) { target = switch_time_make(t, 0); } else { target = switch_micro_time_now(); } switch_time_exp_lt(&tm, target); switch (say_args->type) { case SST_CURRENT_DATE_TIME: say_date = say_time = 1; break; case SST_CURRENT_DATE: say_date = 1; break; case SST_CURRENT_TIME: say_time = 1; break; default: break; } if (say_date) { say_file("time/day-%d.wav", tm.tm_wday); say_file("time/mon-%d.wav", tm.tm_mon); say_num(tm.tm_mday, SSM_COUNTED); say_num(tm.tm_year + 1900, SSM_PRONOUNCED); } if (say_time) { int32_t hour = tm.tm_hour, pm = 0; if (hour > 12) { hour -= 12; pm = 1; } else if (hour == 12) { pm = 1; } else if (hour == 0) { hour = 12; pm = 0; } say_num(hour, SSM_PRONOUNCED); say_file("time/oclock.wav"); if (tm.tm_min > 9) { say_num(tm.tm_min, SSM_PRONOUNCED); } else if (tm.tm_min) { say_file("time/oh.wav"); say_num(tm.tm_min, SSM_PRONOUNCED); } say_file("time/%s.wav", pm ? "p-m" : "a-m"); } return SWITCH_STATUS_SUCCESS; }
static switch_status_t ru_say_time(switch_say_file_handle_t *sh, char *tosay, switch_say_args_t *say_args,say_opt_t *say_opt) { int32_t t; char buf[80]; switch_time_t target = 0, target_now = 0; switch_time_exp_t tm, tm_now; uint8_t say_date = 0, say_time = 0, say_year = 0, say_month = 0, say_dow = 0, say_day = 0, say_yesterday = 0, say_today = 0; const char *tz = NULL; tz = switch_say_file_handle_get_variable(sh, "timezone"); //switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, " ru_say_time %s type=%d method=%d\n", tosay, say_args->type, say_args->method); if (say_args->type == SST_TIME_MEASUREMENT) { int64_t hours = 0; int64_t minutes = 0; int64_t seconds = 0; int64_t r = 0; if (strchr(tosay, ':')) { char *tme = strdup(tosay); char *p; if ((p = strrchr(tme, ':'))) { *p++ = '\0'; seconds = atoi(p); if ((p = strchr(tme, ':'))) { *p++ = '\0'; minutes = atoi(p); if (tme) { hours = atoi(tme); } } else { minutes = atoi(tme); } } free(tme); } else { if ((seconds = atol(tosay)) <= 0) { seconds = (int64_t) switch_epoch_time_now(NULL); } if (seconds >= 60) { minutes = seconds / 60; r = seconds % 60; } if (minutes >= 60) { hours = minutes / 60; r = minutes % 60; minutes = r; } } switch_snprintf(buf, sizeof(buf), "%u", (unsigned) hours); ru_say_count(sh, buf, male, nominativus); if (((hours % 10) == 1) && (hours != 11)) { /* час */ switch_say_file(sh, "time/hour"); } else if (((hours % 10 > 1) && (hours % 10 < 5)) && ((hours < 12) || (hours > 14))) { switch_say_file(sh, "time/hours-a"); /* часа */ } else { switch_say_file(sh, "time/hours"); /* часов */ } switch_snprintf(buf, sizeof(buf), "%u", (unsigned) minutes); //перевести минуты в *char ru_say_count(sh, buf, female, nominativus); if (((minutes % 10) == 1) && (minutes != 11)) { switch_say_file(sh, "time/minute"); //минута } else if (((minutes % 10 > 1) && (minutes % 10 < 5)) && ((minutes < 12) || (minutes > 14))) { switch_say_file(sh, "time/minutes-i"); // минуты } else { switch_say_file(sh, "time/minutes"); //минут } if (seconds != 0) { switch_snprintf(buf, sizeof(buf), "%u", (unsigned) seconds); ru_say_count(sh, buf, female, nominativus); if (((seconds % 10) == 1) && (seconds != 11)) { switch_say_file(sh, "time/second"); // секунда } else if (((seconds % 10 > 1) && (seconds % 10 < 5)) && ((seconds < 12) || (seconds > 14))) { switch_say_file(sh, "time/seconds-i"); // секуны } else { switch_say_file(sh, "time/seconds"); //секунд } } return SWITCH_STATUS_SUCCESS; } if ((t = atol(tosay)) > 0) { target = switch_time_make(t, 0); target_now = switch_micro_time_now(); } else { target = switch_micro_time_now(); target_now = switch_micro_time_now(); } if (tz) { int check = atoi(tz); //switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Timezone is [%s]\n", tz); if (check) { switch_time_exp_tz(&tm, target, check); switch_time_exp_tz(&tm_now, target_now, check); } else { switch_time_exp_tz_name(tz, &tm, target); switch_time_exp_tz_name(tz, &tm_now, target_now); } } else { switch_time_exp_lt(&tm, target); switch_time_exp_lt(&tm_now, target_now); } switch (say_args->type) { case SST_CURRENT_DATE_TIME: say_date = say_time = 1; break; case SST_CURRENT_DATE: say_date = 1; break; case SST_CURRENT_TIME: say_time = 1; break; case SST_SHORT_DATE_TIME: say_time = 1; tm.tm_sec = 0; // В коротком варианте секунды не проговариваем if (tm.tm_year != tm_now.tm_year) { say_date = 1; break; } if (tm.tm_yday == tm_now.tm_yday) { say_today = 1; break; } if (tm.tm_yday == tm_now.tm_yday - 1) { say_yesterday = 1; break; } if (tm.tm_yday >= tm_now.tm_yday - 5) { say_dow = 1; break; } if (tm.tm_mon != tm_now.tm_mon) { say_month = say_day = say_dow = 1; break; } say_month = say_day = say_dow = 1; break; default: break; } if (say_today) { switch_say_file(sh, "time/today"); } if (say_yesterday) { switch_say_file(sh, "time/yesterday"); } if (say_dow) { switch_say_file(sh, "time/day-%d", tm.tm_wday); } if (say_date) { say_year = say_month = say_day = say_dow = 1; say_today = say_yesterday = 0; } if (say_day) { switch_snprintf(buf, sizeof(buf), "%u", (unsigned) tm.tm_mday); ru_say_count(sh, buf, male_h, genitivus); } if (say_month) { switch_say_file(sh, "time/mon-%d", tm.tm_mon); } if (say_year) { switch_snprintf(buf, sizeof(buf), "%u", (unsigned) (tm.tm_year + 1900)); ru_say_count(sh, buf, male_h, genitivus); switch_say_file(sh, "time/h-year"); } if (say_time) { if (say_month || say_year || say_date || say_dow) { switch_say_file(sh, "time/at"); } switch_snprintf(buf, sizeof(buf), "%d:%d:%d", tm.tm_hour, tm.tm_min, tm.tm_sec); say_args->type = SST_TIME_MEASUREMENT; ru_say_time(sh, buf, say_args,say_opt); } return SWITCH_STATUS_SUCCESS; }
SWITCH_DECLARE(void) switch_log_vprintf(switch_text_channel_t channel, const char *file, const char *func, int line, const char *userdata, switch_log_level_t level, const char *fmt, va_list ap) { char *data = NULL; char *new_fmt = NULL; int ret = 0; FILE *handle; const char *filep = (file ? switch_cut_path(file) : ""); const char *funcp = (func ? func : ""); char *content = NULL; switch_time_t now = switch_micro_time_now(); uint32_t len; #ifdef SWITCH_FUNC_IN_LOG const char *extra_fmt = "%s [%s] %s:%d %s()%c%s"; #else const char *extra_fmt = "%s [%s] %s:%d%c%s"; #endif switch_log_level_t limit_level = runtime.hard_log_level; if (channel == SWITCH_CHANNEL_ID_SESSION && userdata) { switch_core_session_t *session = (switch_core_session_t *) userdata; if (limit_level < session->loglevel) { limit_level = session->loglevel; } } if (level > 100) { if ((uint32_t) (level - 100) > runtime.debug_level) { return; } level = 7; } if (level > limit_level) { return; } switch_assert(level < SWITCH_LOG_INVALID); handle = switch_core_data_channel(channel); if (channel != SWITCH_CHANNEL_ID_LOG_CLEAN) { char date[80] = ""; //switch_size_t retsize; switch_time_exp_t tm; switch_time_exp_lt(&tm, now); switch_snprintf(date, sizeof(date), "%0.4d-%0.2d-%0.2d %0.2d:%0.2d:%0.2d.%0.6d", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, tm.tm_usec); //switch_strftime_nocheck(date, &retsize, sizeof(date), "%Y-%m-%d %T", &tm); #ifdef SWITCH_FUNC_IN_LOG len = (uint32_t) (strlen(extra_fmt) + strlen(date) + strlen(filep) + 32 + strlen(funcp) + strlen(fmt)); #else len = (uint32_t) (strlen(extra_fmt) + strlen(date) + strlen(filep) + 32 + strlen(fmt)); #endif new_fmt = malloc(len + 1); switch_assert(new_fmt); #ifdef SWITCH_FUNC_IN_LOG switch_snprintf(new_fmt, len, extra_fmt, date, switch_log_level2str(level), filep, line, funcp, 128, fmt); #else switch_snprintf(new_fmt, len, extra_fmt, date, switch_log_level2str(level), filep, line, 128, fmt); #endif fmt = new_fmt; } ret = switch_vasprintf(&data, fmt, ap); if (ret == -1) { fprintf(stderr, "Memory Error\n"); goto end; } if (channel == SWITCH_CHANNEL_ID_LOG_CLEAN) { content = data; } else { if ((content = strchr(data, 128))) { *content = ' '; } } if (channel == SWITCH_CHANNEL_ID_EVENT) { switch_event_t *event; if (switch_event_running() == SWITCH_STATUS_SUCCESS && switch_event_create(&event, SWITCH_EVENT_LOG) == SWITCH_STATUS_SUCCESS) { switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Log-Data", data); switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Log-File", filep); switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "Log-Function", funcp); switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Log-Line", "%d", line); switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Log-Level", "%d", (int) level); if (!zstr(userdata)) { switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "User-Data", userdata); } switch_event_fire(&event); data = NULL; } goto end; } if (console_mods_loaded == 0 || !do_mods) { if (handle) { int aok = 1; #ifndef WIN32 fd_set can_write; int fd; struct timeval to; fd = fileno(handle); memset(&to, 0, sizeof(to)); FD_ZERO(&can_write); FD_SET(fd, &can_write); to.tv_sec = 0; to.tv_usec = 100000; if (select(fd + 1, NULL, &can_write, NULL, &to) > 0) { aok = FD_ISSET(fd, &can_write); } else { aok = 0; } #endif if (aok) { if (COLORIZE) { #ifdef WIN32 SetConsoleTextAttribute(hStdout, COLORS[level]); WriteFile(hStdout, data, (DWORD) strlen(data), NULL, NULL); SetConsoleTextAttribute(hStdout, wOldColorAttrs); #else fprintf(handle, "%s%s%s", COLORS[level], data, SWITCH_SEQ_DEFAULT_COLOR); #endif } else { fprintf(handle, "%s", data); } } } } if (do_mods && level <= MAX_LEVEL) { switch_log_node_t *node = switch_log_node_alloc(); node->data = data; data = NULL; switch_set_string(node->file, filep); switch_set_string(node->func, funcp); node->line = line; node->level = level; node->content = content; node->timestamp = now; node->channel = channel; if (channel == SWITCH_CHANNEL_ID_SESSION) { node->userdata = userdata ? strdup(switch_core_session_get_uuid((switch_core_session_t *) userdata)) : NULL; } else { node->userdata = !zstr(userdata) ? strdup(userdata) : NULL; } if (switch_queue_trypush(LOG_QUEUE, node) != SWITCH_STATUS_SUCCESS) { switch_log_node_free(&node); } } end: switch_safe_free(data); switch_safe_free(new_fmt); }
static switch_status_t sin_say_time(switch_say_file_handle_t *sh, char *tosay, switch_say_args_t *say_args) { int32_t t; switch_time_t target = 0, target_now = 0; switch_time_exp_t tm, tm_now; uint8_t say_date = 0, say_time = 0, say_year = 0, say_month = 0, say_dow = 0, say_day = 0, say_yesterday = 0, say_today = 0; const char *tz = NULL; tz = switch_say_file_handle_get_variable(sh, "timezone"); if (say_args->type == SST_TIME_MEASUREMENT) { int64_t hours = 0; int64_t minutes = 0; int64_t seconds = 0; int64_t r = 0; if (strchr(tosay, ':')) { char *tme = strdup(tosay); char *p; if ((p = strrchr(tme, ':'))) { *p++ = '\0'; seconds = atoi(p); if ((p = strchr(tme, ':'))) { *p++ = '\0'; minutes = atoi(p); if (tme) { hours = atoi(tme); } } else { minutes = atoi(tme); } } free(tme); } else { if ((seconds = atol(tosay)) <= 0) { seconds = (int64_t) switch_epoch_time_now(NULL); } if (seconds >= 60) { minutes = seconds / 60; r = seconds % 60; seconds = r; } if (minutes >= 60) { hours = minutes / 60; r = minutes % 60; minutes = r; } } if (hours) { if (hours == 1) { switch_say_file(sh, "time/payai"); } else { switch_say_file(sh, "time/paya"); say_num(sh, hours, SSM_PRONOUNCED); } } else { /*switch_say_file(sh, "time/s_hours"); switch_say_file(sh, "digits/s_i-0"); */ } if (minutes) { if (minutes == 1) { switch_say_file(sh, "time/winadiyai"); } else { switch_say_file(sh, "time/winadi"); say_num(sh, minutes, SSM_PRONOUNCED); } } else { /*switch_say_file(sh, "time/s_minutes"); switch_say_file(sh, "digits/s_i-0");*/ } if (seconds) { if (seconds == 1) { switch_say_file(sh, "time/thathparayai"); } else { switch_say_file(sh, "time/thathpara"); say_num(sh, seconds, SSM_PRONOUNCED); } } else { /* switch_say_file(sh, "time/s_seconds"); switch_say_file(sh, "digits/s_i-0");*/ } return SWITCH_STATUS_SUCCESS; } if ((t = atol(tosay)) > 0) { target = switch_time_make(t, 0); target_now = switch_micro_time_now(); } else { target = switch_micro_time_now(); target_now = switch_micro_time_now(); } if (tz) { int check = atoi(tz); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Timezone is [%s]\n", tz); if (check) { switch_time_exp_tz(&tm, target, check); switch_time_exp_tz(&tm_now, target_now, check); } else { switch_time_exp_tz_name(tz, &tm, target); switch_time_exp_tz_name(tz, &tm_now, target_now); } } else { switch_time_exp_lt(&tm, target); switch_time_exp_lt(&tm_now, target_now); } switch (say_args->type) { case SST_CURRENT_DATE_TIME: say_date = say_time = 1; break; case SST_CURRENT_DATE: say_date = 1; break; case SST_CURRENT_TIME: say_time = 1; break; case SST_SHORT_DATE_TIME: say_time = 1; if (tm.tm_year != tm_now.tm_year) { say_date = 1; break; } if (tm.tm_yday == tm_now.tm_yday) { say_today = 1; break; } if (tm.tm_yday == tm_now.tm_yday - 1) { say_yesterday = 1; break; } if (tm.tm_yday >= tm_now.tm_yday - 5) { say_dow = 1; break; } if (tm.tm_mon != tm_now.tm_mon) { say_month = say_day = say_dow = 1; break; } say_month = say_day = say_dow = 1; break; default: break; } if (say_today) { switch_say_file(sh, "time/today"); } if (say_yesterday) { switch_say_file(sh, "time/yesterday"); } if (say_dow) { switch_say_file(sh, "time/day-s_%d", tm.tm_wday); } if (say_date) { say_year = say_month = say_day = say_dow = 1; say_today = say_yesterday = 0; } if (say_month) { switch_say_file(sh, "time/mon-s_%d", tm.tm_mon); } if (say_day) { say_num(sh, tm.tm_mday, SSM_COUNTED); } if (say_year) { say_num(sh, tm.tm_year + 1900, SSM_PRONOUNCED_YEAR); } if (say_time) { int32_t hour = tm.tm_hour, pm = 0; if (say_date || say_today || say_yesterday || say_dow) { switch_say_file(sh, "time/at"); } if (hour > 12) { hour -= 12; pm = 1; } else if (hour == 12) { pm = 1; } else if (hour == 0) { hour = 12; pm = 0; } say_num(sh, hour, SSM_PRONOUNCED); if (tm.tm_min > 9) { say_num(sh, tm.tm_min, SSM_PRONOUNCED); } else if (tm.tm_min) { switch_say_file(sh, "time/oh"); say_num(sh, tm.tm_min, SSM_PRONOUNCED); } else { switch_say_file(sh, "time/oclock"); } switch_say_file(sh, "time/%s", pm ? "p-m" : "a-m"); } return SWITCH_STATUS_SUCCESS; }
static switch_status_t th_say_time(switch_core_session_t *session, char *tosay, switch_say_args_t *say_args, switch_input_args_t *args) { int32_t t; switch_time_t target = 0; switch_time_exp_t tm; #if 0 switch_time_t this_morning; switch_time_exp_t tm2; #endif uint8_t say_date = 0; uint8_t say_time = 0; if (say_args->type == SST_TIME_MEASUREMENT) { int64_t hours = 0; int64_t minutes = 0; int64_t seconds = 0; int64_t r = 0; if (strchr(tosay, ':')) { char *tme = switch_core_session_strdup(session, tosay); char *p; if ((p = strrchr(tme, ':'))) { *p++ = '\0'; seconds = atoi(p); if ((p = strchr(tme, ':'))) { *p++ = '\0'; minutes = atoi(p); if (tme) { hours = atoi(tme); } } else { minutes = atoi(tme); } } } else { if ((seconds = atoi(tosay)) <= 0) { seconds = (int64_t) switch_epoch_time_now(NULL); } if (seconds >= 60) { minutes = seconds / 60; r = seconds % 60; seconds = r; } if (minutes >= 60) { hours = minutes / 60; r = minutes % 60; minutes = r; } } if (hours) { if (hours == 2) { say_file("time/2s.wav"); } else { say_num(hours, SSM_PRONOUNCED); } say_file("time/hours.wav"); } if (minutes) { if (minutes == 2) { say_file("time/2s.wav"); } else { say_num(minutes, SSM_PRONOUNCED); } say_file("time/minute.wav"); } else { if (hours) { say_file("digits/0.wav"); say_file("time/minute.wav"); } } if (seconds) { if (seconds == 2) { say_file("time/2s.wav"); } else { say_num(hours, SSM_PRONOUNCED); } say_file("time/seconds.wav"); } else { if (hours || minutes) { say_file("digits/0.wav"); say_file("time/second.wav"); } } return SWITCH_STATUS_SUCCESS; } if ((t = atoi(tosay)) > 0) target = switch_time_make(t, 0); else target = switch_micro_time_now(); switch_time_exp_lt(&tm, target); switch (say_args->type) { case SST_CURRENT_DATE_TIME: say_date = say_time = 1; break; case SST_CURRENT_DATE: say_date = 1; break; case SST_CURRENT_TIME: say_time = 1; break; default: break; } if (say_date) { say_num(tm.tm_year + 1900, SSM_ITERATED); say_file("time/year.wav"); say_num(tm.tm_mon + 1, SSM_PRONOUNCED); say_file("time/month.wav"); say_num(tm.tm_mday, SSM_PRONOUNCED); say_file("time/day.wav"); say_file("time/day-%d.wav", tm.tm_wday); #if 0 tm = *localtime(&then); this_morning = switch_micro_time_now(); switch_time_exp_lt(&tm2, this_morning); tm2->tm_hour = 0; tm2->tm_min = 0; tm2->tm_sec = 0; this_morning = mktime(tm2); if (this_morning <= then && then < (this_morning + 86400L)) { say_file("time/today.wav"); } else if ((this_morning - 86400L) <= then && then < this_morning) { say_file("time/yesterday.wav"); } else if ((this_morning + 86400L) <= then && then < (this_morning + 2 * 86400L)) { say_file("time/tomorrow.wav"); } else if ((this_morning - 7 * 86400L) <= then && then < this_morning) { say_file("time/day-%d.wav", tm.tm_wday); } else { if (tm2->tm_year != tm.tm_year) { say_num(tm.tm_year + 1900, SSM_ITERATED); say_file("time/year.wav"); } /*endif */ if (tm2->tm_year != tm.tm_year || tm2->tm_mon != tm.tm_mon) { say_num(tm.tm_mon + 1, SSM_PRONOUNCED); say_file("time/month.wav"); } /*endif */ /* Always say the day and the day of the week */ say_num(tm.tm_mday, SSM_PRONOUNCED); say_file("time/day.wav"); say_file("time/day-%d.wav", tm.tm_wday); } #endif } if (say_time) { int32_t hour = tm.tm_hour; if (hour < 6) { say_file("time/morning.wav"); } else if (hour < 12) { say_file("time/am.wav"); } else if (tm.tm_hour < 18) { say_file("time/pm.wav"); } else { say_file("time/evening.wav"); } if (hour > 12) hour -= 12; if (hour == 0) hour = 12; if (hour == 2) { say_file("time/2s.wav"); } else { say_num(hour, SSM_PRONOUNCED); } say_file("time/hour.wav"); if (tm.tm_min == 2) { say_file("time/2s.wav"); } else { say_num(tm.tm_min, SSM_PRONOUNCED); } say_file("time/minute.wav"); } return SWITCH_STATUS_SUCCESS; }
switch_status_t mod_xml_radius_add_params(switch_core_session_t *session, switch_event_t *params, rc_handle *handle, VALUE_PAIR **send, switch_xml_t fields) { switch_xml_t param; void *av_value = NULL; if ( (param = switch_xml_child(fields, "param")) == NULL) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Failed to locate a param under the fields section\n"); goto err; } for (; param; param = param->next) { DICT_ATTR *attribute = NULL; DICT_VENDOR *vendor = NULL; int attr_num = 0, vend_num = 0; char *var = (char *) switch_xml_attr(param, "name"); char *vend = (char *) switch_xml_attr(param, "vendor"); char *variable = (char *) switch_xml_attr(param, "variable"); char *variable_secondary = (char *) switch_xml_attr(param, "variable_secondary"); char *val_default = (char *) switch_xml_attr(param, "default"); char *format = (char *) switch_xml_attr(param, "format"); char *other_leg = (char *) switch_xml_attr(param, "other_leg"); attribute = rc_dict_findattr(handle, var); if ( attribute == NULL ) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "mod_xml_radius: Could not locate attribute '%s' in the configured dictionary\n", var); goto err; } if ( GLOBAL_DEBUG ) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "mod_xml_radius: dict attr '%s' value '%d' type '%d'\n", attribute->name, attribute->value, attribute->type); } attr_num = attribute->value; if ( vend ) { vendor = rc_dict_findvend(handle, vend); if ( vendor == NULL ) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "mod_xml_radius: Could not locate vendor '%s' in the configured dictionary %p\n", vend, vend); goto err; } if ( GLOBAL_DEBUG ) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "mod_xml_radius: dict vend name '%s' vendorpec '%d'\n", vendor->vendorname, vendor->vendorpec); } vend_num = vendor->vendorpec; } if ( var ) { if ( session ) { switch_channel_t *channel = switch_core_session_get_channel(session); /* Accounting only */ if ( strncmp( var, "h323-setup-time", 15) == 0 ) { switch_caller_profile_t *profile = switch_channel_get_caller_profile(channel); switch_time_t time = profile->times->created; switch_time_exp_t tm; if ( !time ) { goto end_loop; } switch_time_exp_lt(&tm, time); if ( GLOBAL_TIME_FORMAT == 1 ) { av_value = switch_mprintf("%02u:%02u:%02u.%03u %s %s %s %02u %04u", tm.tm_hour, tm.tm_min, tm.tm_sec, tm.tm_usec/1000, GLOBAL_TIME_ZONE, radattrdays[tm.tm_wday], radattrmonths[tm.tm_mon], tm.tm_mday, tm.tm_year + 1900); } else { av_value = switch_mprintf("%04u-%02u-%02uT%02u:%02u:%02u.%06u%+03d%02d", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, tm.tm_usec, tm.tm_gmtoff / 3600, tm.tm_gmtoff % 3600); } if (rc_avpair_add(handle, send, attr_num, av_value, -1, vend_num) == NULL) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "mod_xml_radius: failed to add option to handle\n"); goto err; } if ( GLOBAL_DEBUG ) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "mod_xml_radius: value: %s\n", (char *) av_value); } } else if ( strncmp( var, "h323-connect-time", 17) == 0 ) { switch_caller_profile_t *profile = switch_channel_get_caller_profile(channel); switch_time_t time = profile->times->answered; switch_time_exp_t tm; if ( !time ) { goto end_loop; } switch_time_exp_lt(&tm, time); if ( GLOBAL_TIME_FORMAT == 1 ) { av_value = switch_mprintf("%02u:%02u:%02u.%03u %s %s %s %02u %04u", tm.tm_hour, tm.tm_min, tm.tm_sec, tm.tm_usec/1000, GLOBAL_TIME_ZONE, radattrdays[tm.tm_wday], radattrmonths[tm.tm_mon], tm.tm_mday, tm.tm_year + 1900); } else { av_value = switch_mprintf("%04u-%02u-%02uT%02u:%02u:%02u.%06u%+03d%02d", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, tm.tm_usec, tm.tm_gmtoff / 3600, tm.tm_gmtoff % 3600); } if (rc_avpair_add(handle, send, attr_num, av_value, -1, vend_num) == NULL) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "mod_xml_radius: failed to add option to handle\n"); goto err; } if ( GLOBAL_DEBUG ) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "mod_xml_radius: value: %s\n", (char *) av_value); } } else if ( strncmp( var, "h323-disconnect-time", 20) == 0 ) { switch_caller_profile_t *profile = switch_channel_get_caller_profile(channel); switch_time_t time = profile->times->hungup; switch_time_exp_t tm; if ( !time ) { if ( variable_secondary != NULL && strncmp(variable_secondary, "now", 3) == 0 ) { time = switch_time_now(); } else { goto end_loop; } } switch_time_exp_lt(&tm, time); if ( GLOBAL_TIME_FORMAT == 1 ) { av_value = switch_mprintf("%02u:%02u:%02u.%03u %s %s %s %02u %04u", tm.tm_hour, tm.tm_min, tm.tm_sec, tm.tm_usec/1000, GLOBAL_TIME_FORMAT, radattrdays[tm.tm_wday], radattrmonths[tm.tm_mon], tm.tm_mday, tm.tm_year + 1900); } else { av_value = switch_mprintf("%04u-%02u-%02uT%02u:%02u:%02u.%06u%+03d%02d", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, tm.tm_usec, tm.tm_gmtoff / 3600, tm.tm_gmtoff % 3600); } if (rc_avpair_add(handle, send, attr_num, av_value, -1, vend_num) == NULL) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "mod_xml_radius: failed to add option to handle\n"); goto err; } if ( GLOBAL_DEBUG ) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "mod_xml_radius: value: %s\n", (char *) av_value); } } else if ( strncmp( var, "h323-disconnect-cause", 21) == 0 ) { switch_call_cause_t cause = switch_channel_get_cause(channel); av_value = switch_mprintf("h323-disconnect-cause=%x", cause); if (rc_avpair_add(handle, send, 30, av_value, -1, 9) == NULL) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "mod_xml_radius: failed to add disconnect cause \n"); goto err; } } else { if ( format == NULL ) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Missing format attribute for %s variable\n", variable); goto err; } if ( attribute->type == 0 ) { const char *val = NULL; if ( other_leg ) { val = switch_channel_get_variable_partner(channel, variable); if ( val == NULL && variable_secondary != NULL) { val = switch_channel_get_variable_partner(channel, variable_secondary); } } else { val = switch_channel_get_variable(channel, variable); if ( val == NULL && variable_secondary != NULL) { val = switch_channel_get_variable(channel, variable_secondary); } } if ( val == NULL && val_default != NULL) { av_value = switch_mprintf(format, val_default); } else { av_value = switch_mprintf(format, val); } if ( GLOBAL_DEBUG ) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "mod_xml_radius: value: %s\n", (char *) av_value); } if (rc_avpair_add(handle, send, attr_num, av_value, -1, vend_num) == NULL) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "mod_xml_radius: failed to add option with val '%s' to handle\n", (char *) av_value); goto err; } } else if ( attribute->type == 1 ) { int number = atoi(switch_channel_get_variable(channel, variable)); if (rc_avpair_add(handle, send, attr_num, &number, -1, vend_num) == NULL) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "mod_xml_radius: failed to add option with value '%d' to handle\n", number); goto err; } } } } else if ( params ) { /* Auth only */ char *tmp = switch_event_get_header(params, variable); if ( GLOBAL_DEBUG ) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "mod_xml_radius: param var '%s' val: %s\n", variable, tmp); } if ( tmp == NULL ) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "mod_xml_radius: Unable to locate '%s' on the event\n", variable); goto err; } av_value = switch_mprintf(format, tmp); if (rc_avpair_add(handle, send, attr_num, av_value, -1, vend_num) == NULL) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "mod_xml_radius: failed to add option to handle\n"); goto err; } } else { goto err; } } else { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "mod_xml_radius: all params must have a name attribute\n"); goto err; } end_loop: if ( av_value != NULL ) { free(av_value); av_value = NULL; } } return SWITCH_STATUS_SUCCESS; err: if ( av_value != NULL ) { free(av_value); av_value = NULL; } return SWITCH_STATUS_GENERR; }
static switch_status_t fr_say_time(switch_core_session_t *session, char *tosay, switch_say_args_t *say_args, switch_input_args_t *args) { int32_t t; switch_time_t target = 0, target_now = 0; switch_time_exp_t tm, tm_now; uint8_t say_date = 0, say_time = 0, say_year = 0, say_month = 0, say_dow = 0, say_day = 0, say_yesterday = 0, say_today = 0; switch_channel_t *channel = switch_core_session_get_channel(session); const char *tz = switch_channel_get_variable(channel, "timezone"); if (say_args->type == SST_TIME_MEASUREMENT) { int64_t hours = 0; int64_t minutes = 0; int64_t seconds = 0; int64_t r = 0; if (strchr(tosay, ':')) { char *tme = switch_core_session_strdup(session, tosay); char *p; if ((p = strrchr(tme, ':'))) { *p++ = '\0'; seconds = atoi(p); if ((p = strchr(tme, ':'))) { *p++ = '\0'; minutes = atoi(p); if (tme) { hours = atoi(tme); } } else { minutes = atoi(tme); } } } else { if ((seconds = atol(tosay)) <= 0) { seconds = (int64_t) switch_epoch_time_now(NULL); } if (seconds >= 60) { minutes = seconds / 60; r = seconds % 60; seconds = r; } if (minutes >= 60) { hours = minutes / 60; r = minutes % 60; minutes = r; } } say_args->gender = SSG_FEMININE; if (hours) { say_num(hours, SSM_PRONOUNCED); say_file("time/hour.wav"); } else { /* TODO MINUIT */ say_file("digits/0.wav"); say_file("time/hour.wav"); } if (minutes) { say_num(minutes, SSM_PRONOUNCED); say_file("time/minute.wav"); } else { /* TODO Aucune idee quoi faire jouer icit */ say_file("digits/0.wav"); say_file("time/minute.wav"); } if (seconds) { say_num(seconds, SSM_PRONOUNCED); say_file("time/second.wav"); } else { /* TODO Aucune idee quoi faire jouer icit */ say_file("digits/0.wav"); say_file("time/second.wav"); } return SWITCH_STATUS_SUCCESS; } if ((t = atol(tosay)) > 0) { target = switch_time_make(t, 0); target_now = switch_micro_time_now(); } else { target = switch_micro_time_now(); target_now = switch_micro_time_now(); } if (tz) { int check = atoi(tz); if (check) { switch_time_exp_tz(&tm, target, check); switch_time_exp_tz(&tm_now, target_now, check); } else { switch_time_exp_tz_name(tz, &tm, target); switch_time_exp_tz_name(tz, &tm_now, target_now); } } else { switch_time_exp_lt(&tm, target); switch_time_exp_lt(&tm_now, target_now); } switch (say_args->type) { case SST_CURRENT_DATE_TIME: say_date = say_time = 1; break; case SST_CURRENT_DATE: say_date = 1; break; case SST_CURRENT_TIME: say_time = 1; break; case SST_SHORT_DATE_TIME: say_time = 1; if (tm.tm_year != tm_now.tm_year) { say_date = 1; break; } if (tm.tm_yday == tm_now.tm_yday) { say_today = 1; break; } if (tm.tm_yday == tm_now.tm_yday - 1) { say_yesterday = 1; break; } if (tm.tm_yday >= tm_now.tm_yday - 5) { say_dow = 1; break; } if (tm.tm_mon != tm_now.tm_mon) { say_month = say_day = say_dow = 1; break; } say_month = say_day = say_dow = 1; break; default: break; } if (say_today) { say_file("time/today.wav"); } if (say_yesterday) { say_file("time/yesterday.wav"); } if (say_dow) { say_file("time/day-%d.wav", tm.tm_wday); } if (say_date) { say_year = say_month = say_day = say_dow = 1; say_today = say_yesterday = 0; } if (say_day) { if (tm.tm_mday == 1) { /* 1 er Janvier,... 2 feb, 23 dec... */ say_args->gender = SSG_MASCULINE; say_num(tm.tm_mday, SSM_COUNTED); } else { say_args->gender = SSG_FEMININE; say_num(tm.tm_mday, SSM_PRONOUNCED); } } if (say_month) { say_file("time/mon-%d.wav", tm.tm_mon); } if (say_year) { say_args->gender = SSG_MASCULINE; say_num(tm.tm_year + 1900, SSM_PRONOUNCED); } if (say_time) { if (say_date || say_today || say_yesterday || say_dow) { say_file("time/at.wav"); } say_args->gender = SSG_FEMININE; say_num(tm.tm_hour, SSM_PRONOUNCED); say_file("time/hour.wav"); if (tm.tm_min) { say_num(tm.tm_min, SSM_PRONOUNCED); } } return SWITCH_STATUS_SUCCESS; }
static switch_status_t pl_say_time(switch_say_file_handle_t *sh, char *tosay, switch_say_args_t *say_args) { int32_t t; switch_time_t target = 0, target_now = 0; switch_time_exp_t tm, tm_now; uint8_t say_date = 0, say_time = 0, say_year = 0, say_month = 0, say_dow = 0, say_day = 0, say_yesterday = 0, say_today = 0; const char *tz = NULL; tz = switch_say_file_handle_get_variable(sh, "timezone"); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "SAY: %s\n", tosay); if (say_args->type == SST_TIME_MEASUREMENT) { int64_t hours = 0; int64_t minutes = 0; int64_t seconds = 0; int64_t r = 0; if (strchr(tosay, ':')) { char *tme = strdup(tosay); char *p; switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Parse time string!\n"); if ((p = strrchr(tme, ':'))) { *p++ = '\0'; seconds = atoi(p); if ((p = strchr(tme, ':'))) { *p++ = '\0'; minutes = atoi(p); if (tme) { hours = atoi(tme); } } else { minutes = atoi(tme); } } free(tme); } else { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Parse time in seconds!\n"); if ((seconds = atol(tosay)) <= 0) { seconds = (int64_t) switch_epoch_time_now(NULL); } if (seconds >= 60) { minutes = seconds / 60; r = seconds % 60; seconds = r; } if (minutes >= 60) { hours = minutes / 60; r = minutes % 60; minutes = r; } } if (hours) { int hd = 0, hj = 0; say_num(sh, hours, SSM_PRONOUNCED, SSG_FEMININE); hj = hours % 10; hd = (hours/10) % 10; if (hours == 1) { switch_say_file(sh, "time/t_godzina"); }else if (hd != 1 && ( hj == 2 || hj == 3 || hj == 4)) { switch_say_file(sh, "time/t_godziny"); } else { switch_say_file(sh, "time/t_godzin"); } } else { switch_say_file(sh, "digits/0"); switch_say_file(sh, "time/t_godzin"); } if (minutes) { int md = 0, mj = 0; say_num(sh, minutes, SSM_PRONOUNCED, SSG_FEMININE); mj = minutes % 10; md = (minutes/10) % 10; if (minutes == 1) { switch_say_file(sh, "time/minuta"); }else if (md != 1 && ( mj == 2 || mj == 3 || mj == 4)) { switch_say_file(sh, "time/t_minuty"); } else { switch_say_file(sh, "time/t_minut"); } } else { switch_say_file(sh, "digits/0"); switch_say_file(sh, "time/t_minut"); } if (seconds) { int sd = 0, sj = 0; say_num(sh, seconds, SSM_PRONOUNCED, SSG_FEMININE); sj = seconds % 10; sd = (seconds/10) % 10; if (seconds == 1) { switch_say_file(sh, "time/t_sekunda"); }else if (sd != 1 && ( sj == 2 || sj == 3 || sj == 4)) { switch_say_file(sh, "time/t_sekundy"); } else { switch_say_file(sh, "time/t_sekund"); } } else { switch_say_file(sh, "digits/0"); switch_say_file(sh, "time/t_sekund"); } return SWITCH_STATUS_SUCCESS; } if ((t = atol(tosay)) > 0) { target = switch_time_make(t, 0); target_now = switch_micro_time_now(); } else { target = switch_micro_time_now(); target_now = switch_micro_time_now(); } if (tz) { int check = atoi(tz); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Timezone is [%s]\n", tz); if (check) { switch_time_exp_tz(&tm, target, check); switch_time_exp_tz(&tm_now, target_now, check); } else { switch_time_exp_tz_name(tz, &tm, target); switch_time_exp_tz_name(tz, &tm_now, target_now); } } else { switch_time_exp_lt(&tm, target); switch_time_exp_lt(&tm_now, target_now); } switch (say_args->type) { case SST_CURRENT_DATE_TIME: say_date = say_time = 1; break; case SST_CURRENT_DATE: say_date = 1; break; case SST_CURRENT_TIME: say_time = 1; break; case SST_SHORT_DATE_TIME: say_time = 1; if (tm.tm_year != tm_now.tm_year) { say_date = 1; break; } if (tm.tm_yday == tm_now.tm_yday) { say_today = 1; break; } if (tm.tm_yday == tm_now.tm_yday - 1) { say_yesterday = 1; break; } if (tm.tm_yday >= tm_now.tm_yday - 5) { say_dow = 1; break; } if (tm.tm_mon != tm_now.tm_mon) { say_month = say_day = say_dow = 1; break; } say_month = say_day = say_dow = 1; break; default: break; } if (say_today) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "SAY: today\n"); switch_say_file(sh, "time/t_dzisiaj"); } if (say_yesterday) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "SAY: yesterday\n"); switch_say_file(sh, "time/t_wczoraj"); } if (say_dow) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "SAY: dow\n"); switch_say_file(sh, "time/day-%d", tm.tm_wday); } if (say_date) { say_year = say_month = say_day = say_dow = 1; say_today = say_yesterday = 0; } if (say_day) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "SAY: day\n"); //say_num(sh, tm.tm_mday, SSM_COUNTED, SSG_MASCULINE); switch_say_file(sh, "digits/%02d_pm", tm.tm_mday); } if (say_month) { switch_say_file(sh, "time/mon-%d_D", tm.tm_mon); } if (say_year) { say_num(sh, tm.tm_year + 1900, SSM_COUNTED, SSG_MASCULINE); //switch_say_file(sh, "time/t_roku"); } if (say_time) { switch_say_file(sh, "time/t_godzina"); say_num(sh, tm.tm_hour, SSM_COUNTED, SSG_FEMININE); //switch_say_file(sh, "digits/%da", tm.tm_hour); say_num(sh, tm.tm_min, SSM_PRONOUNCED, SSG_FEMININE); /* switch_say_file(sh, "digits/t_minut");*/ } return SWITCH_STATUS_SUCCESS; }
int modem_close(modem_t *modem) { int r = 0; switch_status_t was_running = switch_test_flag(modem, MODEM_FLAG_RUNNING); switch_clear_flag(modem, MODEM_FLAG_RUNNING); #ifndef WIN32 if (modem->master > -1) { shutdown(modem->master, 2); close(modem->master); modem->master = -1; #else if (modem->master) { SetEvent(modem->threadAbort); CloseHandle(modem->threadAbort); CloseHandle(modem->master); modem->master = 0; #endif r++; } if (modem->slave > -1) { shutdown(modem->slave, 2); close(modem->slave); modem->slave = -1; r++; } if (modem->t31_state) { t31_free(modem->t31_state); modem->t31_state = NULL; } unlink(modem->devlink); if (was_running) { switch_mutex_lock(globals.mutex); globals.REF_COUNT--; switch_mutex_unlock(globals.mutex); } return r; } switch_status_t modem_init(modem_t *modem, modem_control_handler_t control_handler) { switch_status_t status = SWITCH_STATUS_SUCCESS; #ifdef WIN32 COMMTIMEOUTS timeouts={0}; #endif memset(modem, 0, sizeof(*modem)); modem->master = -1; modem->slave = -1; /* windows will have to try something like: http://com0com.cvs.sourceforge.net/viewvc/com0com/com0com/ReadMe.txt?revision=RELEASED */ #if USE_OPENPTY if (openpty(&modem->master, &modem->slave, NULL, NULL, NULL)) { if (modem->master < 0) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Fatal error: failed to initialize pty\n"); status = SWITCH_STATUS_FALSE; goto end; } modem->stty = ttyname(modem->slave); #else #if WIN32 modem->slot = 4+globals.NEXT_ID++; /* need work here we start at COM4 for now*/ snprintf(modem->devlink, sizeof(modem->devlink), "COM%d", modem->slot); modem->master = CreateFile(modem->devlink, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0); if(modem->master==INVALID_HANDLE_VALUE) { status = SWITCH_STATUS_FALSE; if(GetLastError()==ERROR_FILE_NOT_FOUND) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Fatal error: Serial port does not exist\n"); goto end; } switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Fatal error: Serial port open error\n"); goto end; } #elif !defined(HAVE_POSIX_OPENPT) modem->master = open("/dev/ptmx", O_RDWR); #else modem->master = posix_openpt(O_RDWR | O_NOCTTY); #endif #ifndef WIN32 if (modem->master < 0) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Fatal error: failed to initialize UNIX98 master pty\n"); } if (grantpt(modem->master) < 0) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Fatal error: failed to grant access to slave pty\n"); } if (unlockpt(modem->master) < 0) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Fatal error: failed to unlock slave pty\n"); } modem->stty = ptsname(modem->master); if (modem->stty == NULL) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Fatal error: failed to obtain slave pty filename\n"); } modem->slave = open(modem->stty, O_RDWR); if (modem->slave < 0) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Fatal error: failed to open slave pty %s\n", modem->stty); } #endif #ifdef SOLARIS ioctl(modem->slave, I_PUSH, "ptem"); /* push ptem */ ioctl(modem->slave, I_PUSH, "ldterm"); /* push ldterm*/ #endif #endif #ifndef WIN32 modem->slot = globals.NEXT_ID++; snprintf(modem->devlink, sizeof(modem->devlink), "/dev/FS%d", modem->slot); unlink(modem->devlink); if (symlink(modem->stty, modem->devlink)) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Fatal error: failed to create %s symbolic link\n", modem->devlink); modem_close(modem); status = SWITCH_STATUS_FALSE; goto end; } if (fcntl(modem->master, F_SETFL, fcntl(modem->master, F_GETFL, 0) | O_NONBLOCK)) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Cannot set up non-blocking read on %s\n", ttyname(modem->master)); modem_close(modem); status = SWITCH_STATUS_FALSE; goto end; } #else timeouts.ReadIntervalTimeout=50; timeouts.ReadTotalTimeoutConstant=50; timeouts.ReadTotalTimeoutMultiplier=10; timeouts.WriteTotalTimeoutConstant=50; timeouts.WriteTotalTimeoutMultiplier=10; SetCommMask(modem->master, EV_RXCHAR); if(!SetCommTimeouts(modem->master, &timeouts)){ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Cannot set up non-blocking read on %s\n", modem->devlink); modem_close(modem); status = SWITCH_STATUS_FALSE; goto end; } modem->threadAbort = CreateEvent(NULL, TRUE, FALSE, NULL); #endif if (!(modem->t31_state = t31_init(NULL, t31_at_tx_handler, modem, t31_call_control_handler, modem, NULL, NULL))) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Cannot initialize the T.31 modem\n"); modem_close(modem); status = SWITCH_STATUS_FALSE; goto end; } if (spandsp_globals.modem_verbose) { span_log_set_message_handler(&modem->t31_state->logging, spanfax_log_message, NULL); span_log_set_message_handler(&modem->t31_state->audio.modems.fast_modems.v17_rx.logging, spanfax_log_message, NULL); span_log_set_message_handler(&modem->t31_state->audio.modems.fast_modems.v29_rx.logging, spanfax_log_message, NULL); span_log_set_message_handler(&modem->t31_state->audio.modems.fast_modems.v27ter_rx.logging, spanfax_log_message, NULL); modem->t31_state->logging.level = SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_FLOW; modem->t31_state->audio.modems.fast_modems.v17_rx.logging.level = SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_FLOW; modem->t31_state->audio.modems.fast_modems.v29_rx.logging.level = SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_FLOW; modem->t31_state->audio.modems.fast_modems.v27ter_rx.logging.level = SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_FLOW; } modem->control_handler = control_handler; modem->flags = 0; switch_set_flag(modem, MODEM_FLAG_RUNNING); switch_mutex_init(&modem->mutex, SWITCH_MUTEX_NESTED, globals.pool); switch_mutex_init(&modem->cond_mutex, SWITCH_MUTEX_NESTED, globals.pool); switch_thread_cond_create(&modem->cond, globals.pool); modem_set_state(modem, MODEM_STATE_INIT); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Modem [%s]->[%s] Ready\n", modem->devlink, modem->stty); switch_mutex_lock(globals.mutex); globals.REF_COUNT++; switch_mutex_unlock(globals.mutex); end: return status; } static switch_endpoint_interface_t *modem_endpoint_interface = NULL; struct private_object { switch_mutex_t *mutex; switch_core_session_t *session; switch_channel_t *channel; switch_codec_t read_codec; switch_codec_t write_codec; switch_frame_t read_frame; unsigned char databuf[SWITCH_RECOMMENDED_BUFFER_SIZE]; switch_timer_t timer; modem_t *modem; switch_caller_profile_t *caller_profile; int dead; }; typedef struct private_object private_t; static switch_status_t channel_on_init(switch_core_session_t *session); static switch_status_t channel_on_hangup(switch_core_session_t *session); static switch_status_t channel_on_destroy(switch_core_session_t *session); static switch_status_t channel_on_routing(switch_core_session_t *session); static switch_status_t channel_on_exchange_media(switch_core_session_t *session); static switch_status_t channel_on_soft_execute(switch_core_session_t *session); static switch_call_cause_t channel_outgoing_channel(switch_core_session_t *session, switch_event_t *var_event, switch_caller_profile_t *outbound_profile, switch_core_session_t **new_session, switch_memory_pool_t **pool, switch_originate_flag_t flags, switch_call_cause_t *cancel_cause); static switch_status_t channel_read_frame(switch_core_session_t *session, switch_frame_t **frame, switch_io_flag_t flags, int stream_id); static switch_status_t channel_write_frame(switch_core_session_t *session, switch_frame_t *frame, switch_io_flag_t flags, int stream_id); static switch_status_t channel_kill_channel(switch_core_session_t *session, int sig); /* State methods they get called when the state changes to the specific state returning SWITCH_STATUS_SUCCESS tells the core to execute the standard state method next so if you fully implement the state you can return SWITCH_STATUS_FALSE to skip it. */ static switch_status_t channel_on_init(switch_core_session_t *session) { switch_channel_t *channel; private_t *tech_pvt = NULL; int to_ticks = 60, ring_ticks = 10, rt = ring_ticks; int rest = 500000; tech_pvt = switch_core_session_get_private(session); switch_assert(tech_pvt != NULL); channel = switch_core_session_get_channel(session); switch_assert(channel != NULL); if (switch_channel_direction(channel) == SWITCH_CALL_DIRECTION_OUTBOUND) { #ifndef WIN32 int tioflags; #endif char call_time[16]; char call_date[16]; switch_size_t retsize; switch_time_exp_t tm; switch_time_exp_lt(&tm, switch_micro_time_now()); switch_strftime(call_date, &retsize, sizeof(call_date), "%m%d", &tm); switch_strftime(call_time, &retsize, sizeof(call_time), "%H%M", &tm); #ifndef WIN32 ioctl(tech_pvt->modem->slave, TIOCMGET, &tioflags); tioflags |= TIOCM_RI; ioctl(tech_pvt->modem->slave, TIOCMSET, &tioflags); #endif at_reset_call_info(&tech_pvt->modem->t31_state->at_state); at_set_call_info(&tech_pvt->modem->t31_state->at_state, "DATE", call_date); at_set_call_info(&tech_pvt->modem->t31_state->at_state, "TIME", call_time); at_set_call_info(&tech_pvt->modem->t31_state->at_state, "NAME", tech_pvt->caller_profile->caller_id_name); at_set_call_info(&tech_pvt->modem->t31_state->at_state, "NMBR", tech_pvt->caller_profile->caller_id_number); at_set_call_info(&tech_pvt->modem->t31_state->at_state, "ANID", tech_pvt->caller_profile->ani); at_set_call_info(&tech_pvt->modem->t31_state->at_state, "USER", tech_pvt->caller_profile->username); at_set_call_info(&tech_pvt->modem->t31_state->at_state, "CDID", tech_pvt->caller_profile->context); at_set_call_info(&tech_pvt->modem->t31_state->at_state, "NDID", tech_pvt->caller_profile->destination_number); modem_set_state(tech_pvt->modem, MODEM_STATE_RINGING); t31_call_event(tech_pvt->modem->t31_state, AT_CALL_EVENT_ALERTING); while(to_ticks > 0 && switch_channel_up(channel) && modem_get_state(tech_pvt->modem) == MODEM_STATE_RINGING) { if (--rt <= 0) { t31_call_event(tech_pvt->modem->t31_state, AT_CALL_EVENT_ALERTING); rt = ring_ticks; } switch_yield(rest); to_ticks--; } if (to_ticks < 1 || modem_get_state(tech_pvt->modem) != MODEM_STATE_ANSWERED) { t31_call_event(tech_pvt->modem->t31_state, AT_CALL_EVENT_NO_ANSWER); switch_channel_hangup(channel, SWITCH_CAUSE_NO_ANSWER); } else { t31_call_event(tech_pvt->modem->t31_state, AT_CALL_EVENT_ANSWERED); modem_set_state(tech_pvt->modem, MODEM_STATE_CONNECTED); switch_channel_mark_answered(channel); } } switch_channel_set_state(channel, CS_ROUTING); return SWITCH_STATUS_SUCCESS; }
/* rotate the log file */ static switch_status_t mod_logfile_rotate(logfile_profile_t *profile) { unsigned int i = 0; char *filename = NULL; switch_status_t stat = 0; int64_t offset = 0; switch_memory_pool_t *pool = NULL; switch_time_exp_t tm; char date[80] = ""; switch_size_t retsize; switch_status_t status = SWITCH_STATUS_SUCCESS; switch_mutex_lock(globals.mutex); switch_time_exp_lt(&tm, switch_micro_time_now()); switch_strftime_nocheck(date, &retsize, sizeof(date), "%Y-%m-%d-%H-%M-%S", &tm); profile->log_size = 0; stat = switch_file_seek(profile->log_afd, SWITCH_SEEK_SET, &offset); if (stat != SWITCH_STATUS_SUCCESS) { status = SWITCH_STATUS_FALSE; goto end; } switch_core_new_memory_pool(&pool); filename = switch_core_alloc(pool, strlen(profile->logfile) + WARM_FUZZY_OFFSET); if (profile->max_rot) { char *from_filename = NULL; char *to_filename = NULL; from_filename = switch_core_alloc(pool, strlen(profile->logfile) + WARM_FUZZY_OFFSET); to_filename = switch_core_alloc(pool, strlen(profile->logfile) + WARM_FUZZY_OFFSET); for (i=profile->suffix; i>1; i--) { sprintf((char *) to_filename, "%s.%i", profile->logfile, i); sprintf((char *) from_filename, "%s.%i", profile->logfile, i-1); if (switch_file_exists(to_filename, pool) == SWITCH_STATUS_SUCCESS) { if ((status = switch_file_remove(to_filename, pool)) != SWITCH_STATUS_SUCCESS) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Error removing log %s\n",to_filename); goto end; } } if ((status = switch_file_rename(from_filename, to_filename, pool)) != SWITCH_STATUS_SUCCESS) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Error renaming log from %s to %s [%s]\n", from_filename, to_filename, strerror(errno)); goto end; } } sprintf((char *) to_filename, "%s.%i", profile->logfile, i); if (switch_file_exists(to_filename, pool) == SWITCH_STATUS_SUCCESS) { if ((status = switch_file_remove(to_filename, pool)) != SWITCH_STATUS_SUCCESS) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Error removing log %s [%s]\n", to_filename, strerror(errno)); goto end; } } switch_file_close(profile->log_afd); if ((status = switch_file_rename(profile->logfile, to_filename, pool)) != SWITCH_STATUS_SUCCESS) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Error renaming log from %s to %s [%s]\n", profile->logfile, to_filename, strerror(errno)); goto end; } if ((status = mod_logfile_openlogfile(profile, SWITCH_FALSE)) != SWITCH_STATUS_SUCCESS) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Error reopening log %s\n", profile->logfile); } if (profile->suffix < profile->max_rot) { profile->suffix++; } switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "New log started.\n"); goto end; } /* XXX This have no real value EXCEPT making sure if we rotate within the same second, the end index will increase */ for (i = 1; i < MAX_ROT; i++) { sprintf((char *) filename, "%s.%s.%i", profile->logfile, date, i); if (switch_file_exists(filename, pool) == SWITCH_STATUS_SUCCESS) { continue; } switch_file_close(profile->log_afd); switch_file_rename(profile->logfile, filename, pool); if ((status = mod_logfile_openlogfile(profile, SWITCH_FALSE)) != SWITCH_STATUS_SUCCESS) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Error Rotating Log!\n"); goto end; } break; } switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "New log started.\n"); end: if (pool) { switch_core_destroy_memory_pool(&pool); } switch_mutex_unlock(globals.mutex); return status; }
static switch_status_t hr_say_time(switch_core_session_t *session, char *tosay, switch_say_args_t *say_args, switch_input_args_t *args) { int32_t t; switch_time_t target = 0, target_now = 0; switch_time_exp_t tm, tm_now; uint8_t say_date = 0, say_time = 0, say_year = 0, say_month = 0, say_dow = 0, say_day = 0, say_yesterday = 0, say_today = 0; switch_channel_t *channel = switch_core_session_get_channel(session); const char *tz = switch_channel_get_variable(channel, "timezone"); int sat_desetinka = 0; int sat_jedinica = 0; int minuta_desetinka = 0; int minuta_jedinica = 0; int sekunda_desetinka = 0; int sekunda_jedinica = 0; if (say_args->type == SST_TIME_MEASUREMENT) { int64_t hours = 0; int64_t minutes = 0; int64_t seconds = 0; int64_t r = 0; if (strchr(tosay, ':')) { char *tme = switch_core_session_strdup(session, tosay); char *p; if ((p = strrchr(tme, ':'))) { *p++ = '\0'; seconds = atoi(p); if ((p = strchr(tme, ':'))) { *p++ = '\0'; minutes = atoi(p); if (tme) { hours = atoi(tme); } } else { minutes = atoi(tme); } } } else { if ((seconds = atol(tosay)) <= 0) { seconds = (int64_t) switch_epoch_time_now(NULL); } if (seconds >= 60) { minutes = seconds / 60; r = seconds % 60; seconds = r; } if (minutes >= 60) { hours = minutes / 60; r = minutes % 60; minutes = r; } } sat_jedinica = hours % 10; if (hours > 10) sat_desetinka = (int) (hours / 10); if (hours) { say_num(hours, SSM_PRONOUNCED, ""); if (sat_desetinka == 0 && sat_jedinica == 0) { nop; } else if (sat_desetinka == 1) { say_file("time/sati.wav"); } else { switch(sat_jedinica) { case 1: say_file("time/sat.wav"); break; case 2: case 3: case 4: say_file("time/sata.wav"); break; case 0: case 5: case 6: case 7: case 8: case 9: say_file("time/sati.wav"); break; } } } minuta_jedinica = minutes % 10; if (minutes > 10) minuta_desetinka = (int) (minutes / 10); if (minutes) { if (minuta_desetinka == 1) { say_num(minutes, SSM_PRONOUNCED, ""); say_file("time/minuta.wav"); } else { switch(minuta_jedinica) { case 2: say_num(minutes, SSM_PRONOUNCED, "je"); say_file("time/minute.wav"); break; case 3: case 4: say_num(minutes, SSM_PRONOUNCED, ""); say_file("time/minute.wav"); break; case 1: say_num(minutes, SSM_PRONOUNCED, "a"); say_file("time/minuta.wav"); break; case 0: case 5: case 6: case 7: case 8: case 9: say_num(minutes, SSM_PRONOUNCED, ""); say_file("time/minuta.wav"); break; } } } sekunda_jedinica = seconds % 10; if (seconds > 10) sekunda_desetinka = (int) (seconds / 10); if (seconds) { if (sekunda_desetinka == 1) { say_num(seconds, SSM_PRONOUNCED, ""); say_file("time/sekundi.wav"); } else if (sekunda_desetinka == 0) { switch(sekunda_jedinica) { case 1: say_num(seconds, SSM_PRONOUNCED, "a"); say_file("time/sekunda.wav"); break; case 2: say_num(seconds, SSM_PRONOUNCED, "je"); say_file("time/sekunde.wav"); break; case 3: case 4: say_num(seconds, SSM_PRONOUNCED, ""); say_file("time/sekunde.wav"); break; case 0: case 5: case 6: case 7: case 8: case 9: say_file("time/sekundi.wav"); break; } } else { switch(sekunda_jedinica) { case 1: say_num(seconds, SSM_PRONOUNCED, "a"); say_file("time/sekunda.wav"); break; case 2: say_num(seconds, SSM_PRONOUNCED, "je"); say_file("time/sekunde.wav"); break; case 3: case 4: say_num(seconds, SSM_PRONOUNCED, ""); say_file("time/sekunde.wav"); break; case 0: case 5: case 6: case 7: case 8: case 9: say_num(seconds, SSM_PRONOUNCED, ""); say_file("time/sekundi.wav"); break; } } } return SWITCH_STATUS_SUCCESS; } if ((t = atol(tosay)) > 0) { target = switch_time_make(t, 0); target_now = switch_micro_time_now(); } else { target = switch_micro_time_now(); target_now = switch_micro_time_now(); } if (tz) { int check = atoi(tz); switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Timezone is [%s]\n", tz); if (check) { switch_time_exp_tz(&tm, target, check); switch_time_exp_tz(&tm_now, target_now, check); } else { switch_time_exp_tz_name(tz, &tm, target); switch_time_exp_tz_name(tz, &tm_now, target_now); } } else { switch_time_exp_lt(&tm, target); switch_time_exp_lt(&tm_now, target_now); } switch (say_args->type) { case SST_CURRENT_DATE_TIME: say_date = say_time = 1; break; case SST_CURRENT_DATE: say_date = 1; break; case SST_CURRENT_TIME: say_time = 1; break; case SST_SHORT_DATE_TIME: say_time = 1; if (tm.tm_year != tm_now.tm_year) { say_date = 1; break; } if (tm.tm_yday == tm_now.tm_yday) { say_today = 1; break; } if (tm.tm_yday == tm_now.tm_yday - 1) { say_yesterday = 1; break; } if (tm.tm_yday >= tm_now.tm_yday - 5) { say_dow = 1; break; } if (tm.tm_mon != tm_now.tm_mon) { say_month = say_day = say_dow = 1; break; } say_month = say_day = say_dow = 1; break; default: break; } if (say_today) { say_file("time/today.wav"); } if (say_yesterday) { say_file("time/yesterday.wav"); } if (say_dow) { say_file("time/day-%d.wav", tm.tm_wday); } if (say_date) { say_year = say_month = say_day = say_dow = 1; say_today = say_yesterday = 0; } if (say_day) { say_num(tm.tm_mday, SSM_COUNTED, ""); } if (say_month) { say_file("time/mon-%d.wav", tm.tm_mon); } if (say_year) { int y = tm.tm_year + 1900; int tis = 0; //say_num(tm.tm_year + 1900, SSM_PRONOUNCED, ""); //1 - 99 => h-1a.wav - h-99a.wav if (y >= 1 && y <= 99) { say_file("digits/h-%da.wav", y); } // [1-9]00 => h-[1-9]00a.wav else if (y >= 100 && y <= 900 && y % 100 == 0) { say_file("digits/h-%da.wav", y); } //[1-9]01 - [1-9]99 => [1-9]00.wav + (h-1a.wav - h-99a.wav) else if (y >= 100 && y <= 900 && y % 100 != 0) { say_file("digits/h-%da.wav", (int) y - (y % 100)); say_file("digits/h-%da.wav", (int) y % 100); } // 1000 => thousand-ta.wav else if (y == 1000) { say_file("digits/thousand-ta.wav"); } // 1001 - 1999 => thousand-u.wav + [1-9]00.wav + (h-1a.wav - h-99a.wav) else if (y >= 1001 && y <= 1999) { say_file("digits/thousand-u.wav"); if (0 != (int) ((y - 1000) - ((y - 1000) % 100))) say_file("digits/h-%da.wav", (int) ((y - 1000) - ((y - 1000) % 100))); say_file("digits/h-%da.wav", (int) y % 100); } //2000 => 2je.wav + thousand-ta.wav else if (y == 2000) { say_file("digits/2je.wav"); say_file("digits/thousand-ta.wav"); } // 2001 - 2999 => 2je.wav + thousands-e.wav + [1-9]00.wav + (h-1a.wav - h-99a.wav) else if (y >= 2001 && y <= 2999) { say_file("digits/2je.wav"); say_file("digits/thousands-e.wav"); if (0 != (int) ((y - 2000) - ((y - 2000) % 100))) say_file("digits/h-%da.wav", (int) ((y - 2000) - ((y - 2000) % 100))); say_file("digits/h-%da.wav", (int) y % 100); } // 3000 => [3-9].wav + thousand-ta.wav else if (y >= 3000 && y <= 9000 && y % 1000 == 0) { say_file("digits/%d.wav", (int) (y / 1000)); say_file("digits/thousand-ta.wav"); } // [3-9]001 - [3-9]999 => [3-9].wav + thousands-e.wav + [1-9]00.wav + (h-1a.wav - h-99a.wav) else if (y >= 3000 && y <= 9000 && y % 1000 != 0) { say_file("digits/%d.wav", (int) (y / 1000)); say_file("digits/thousands-e.wav"); tis = y - (y % 1000); if (0 != (int) ((y - tis) - ((y - tis) % 100))) say_file("digits/h-%da.wav", (int) ((y - tis) - ((y - tis) % 100))); say_file("digits/h-%da.wav", (int) y % 100); } //say_num(tm.tm_year + 1900, SSM_COUNTED, "a"); } if (say_time) { say_num(tm.tm_hour, SSM_PRONOUNCED, ""); sat_jedinica = tm.tm_hour % 10; if (tm.tm_hour >= 10) sat_desetinka = (int) (tm.tm_hour / 10); if (tm.tm_hour) { if (sat_desetinka == 0 && sat_jedinica == 0) { nop; } else if (sat_desetinka == 1) { say_file("time/sati.wav"); } else { switch(sat_jedinica) { case 1: say_file("time/sat.wav"); break; case 2: case 3: case 4: say_file("time/sata.wav"); break; case 0: case 5: case 6: case 7: case 8: case 9: say_file("time/sati.wav"); break; } } } minuta_jedinica = tm.tm_min % 10; if (tm.tm_min >= 10) minuta_desetinka = (int) (tm.tm_min / 10); if (tm.tm_min) { if (minuta_desetinka == 1) { say_num(tm.tm_min, SSM_PRONOUNCED, ""); say_file("time/minuta.wav"); } else { switch(minuta_jedinica) { case 2: say_num(tm.tm_min, SSM_PRONOUNCED, "je"); say_file("time/minute.wav"); break; case 3: case 4: say_num(tm.tm_min, SSM_PRONOUNCED, ""); say_file("time/minute.wav"); break; case 1: say_num(tm.tm_min, SSM_PRONOUNCED, "a"); say_file("time/minuta.wav"); break; case 0: case 5: case 6: case 7: case 8: case 9: say_num(tm.tm_min, SSM_PRONOUNCED, ""); say_file("time/minuta.wav"); break; } } } sekunda_jedinica = tm.tm_sec % 10; if (tm.tm_sec >= 10) sekunda_desetinka = (int) (tm.tm_sec / 10); if (tm.tm_sec) { if (sekunda_desetinka == 1) { say_num(tm.tm_sec, SSM_PRONOUNCED, ""); say_file("time/sekundi.wav"); } else if (sekunda_desetinka == 0) { switch(sekunda_jedinica) { case 1: say_num(tm.tm_sec, SSM_PRONOUNCED, "a"); say_file("time/sekunda.wav"); break; case 2: say_num(tm.tm_sec, SSM_PRONOUNCED, "je"); say_file("time/sekunde.wav"); break; case 3: case 4: say_num(tm.tm_sec, SSM_PRONOUNCED, ""); say_file("time/sekunde.wav"); break; case 0: case 5: case 6: case 7: case 8: case 9: say_file("time/sekundi.wav"); break; } } else { switch(sekunda_jedinica) { case 1: say_num(tm.tm_sec, SSM_PRONOUNCED, "a"); say_file("time/sekunda.wav"); break; case 2: say_num(tm.tm_sec, SSM_PRONOUNCED, "je"); say_file("time/sekunde.wav"); break; case 3: case 4: say_num(tm.tm_sec, SSM_PRONOUNCED, ""); say_file("time/sekunde.wav"); break; case 0: case 5: case 6: case 7: case 8: case 9: say_num(tm.tm_sec, SSM_PRONOUNCED, ""); say_file("time/sekundi.wav"); break; } } } } return SWITCH_STATUS_SUCCESS; }
switch_status_t mod_amqp_logging_recv(const switch_log_node_t *node, switch_log_level_t level) { switch_hash_index_t *hi = NULL; mod_amqp_message_t *msg = NULL; mod_amqp_logging_profile_t *logging = NULL; char *json = NULL; if (!strcmp(node->file, "mod_amqp_logging.c")) { return SWITCH_STATUS_SUCCESS; } /* 1. Loop through logging hash of profiles. Check for a profile that accepts this logging level, and file regex. 2. If event not already parsed/created, then create it now 3. Queue copy of event into logging profile send queue 4. Destroy local event copy */ for (hi = switch_core_hash_first(globals.logging_hash); hi; hi = switch_core_hash_next(&hi)) { switch_core_hash_this(hi, NULL, NULL, (void **)&logging); if ( logging && switch_log_check_mask(logging->log_level_mask, level) ) { char file[128] = {0}; if ( !json ) { cJSON *body = NULL; char date[80] = ""; switch_time_exp_t tm; switch_time_exp_lt(&tm, node->timestamp); switch_snprintf(date, sizeof(date), "%0.4d-%0.2d-%0.2d %0.2d:%0.2d:%0.2d.%0.6d", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, tm.tm_usec); /* Create cJSON body */ body = cJSON_CreateObject(); cJSON_AddItemToObject(body, "file", cJSON_CreateString((const char *) node->file)); cJSON_AddItemToObject(body, "function", cJSON_CreateString((const char *) node->func)); cJSON_AddItemToObject(body, "line", cJSON_CreateNumber((double) node->line)); cJSON_AddItemToObject(body, "level", cJSON_CreateString(switch_log_level2str(node->level))); cJSON_AddItemToObject(body, "timestamp", cJSON_CreateString((const char *)date)); cJSON_AddItemToObject(body, "timestamp_epoch", cJSON_CreateNumber((double) node->timestamp / 1000000)); cJSON_AddItemToObject(body, "content", cJSON_CreateString(node->content )); json = cJSON_Print(body); cJSON_Delete(body); } /* Create message */ switch_malloc(msg, sizeof(mod_amqp_message_t)); msg->pjson = strdup(json); strcpy(file, node->file); switch_replace_char(file, '.', '_', 0); snprintf(msg->routing_key, sizeof(msg->routing_key), "%s.%s.%s.%s", switch_core_get_hostname(), node->userdata, switch_log_level2str(node->level), file); if (switch_queue_trypush(logging->send_queue, msg) != SWITCH_STATUS_SUCCESS) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "AMQP logging message queue full. Messages will be dropped!\n"); return SWITCH_STATUS_SUCCESS; } } } switch_safe_free(json); return SWITCH_STATUS_SUCCESS; }
static switch_status_t fa_say_time(switch_core_session_t *session, char *tosay, switch_say_args_t *say_args, switch_input_args_t *args) { int32_t t; switch_time_t target = 0, target_now = 0; switch_time_exp_t tm, tm_now; uint8_t say_date = 0, say_time = 0, say_year = 0, say_month = 0, say_dow = 0, say_day = 0, say_yesterday = 0, say_today = 0; switch_channel_t *channel = switch_core_session_get_channel(session); const char *tz = switch_channel_get_variable(channel, "timezone"); int jalali_year = 1, jalali_month = 0, jalali_day = 0; if (say_args->type == SST_TIME_MEASUREMENT) { int64_t hours = 0; int64_t minutes = 0; int64_t seconds = 0; int64_t r = 0; if (strchr(tosay, ':')) { char *tme = switch_core_session_strdup(session, tosay); char *p; if ((p = strrchr(tme, ':'))) { *p++ = '\0'; seconds = atoi(p); if ((p = strchr(tme, ':'))) { *p++ = '\0'; minutes = atoi(p); if (tme) { hours = atoi(tme); } } else { minutes = atoi(tme); } } } else { if ((seconds = atol(tosay)) <= 0) { seconds = (int64_t) switch_epoch_time_now(NULL); } if (seconds >= 60) { minutes = seconds / 60; r = seconds % 60; seconds = r; } if (minutes >= 60) { hours = minutes / 60; r = minutes % 60; minutes = r; } } if (hours) { say_num(hours, SSM_PRONOUNCED); if(minutes || seconds) { say_file("time/hour+.wav"); } else say_file("time/hour.wav"); } if (minutes) { say_num(minutes, SSM_PRONOUNCED); if( seconds ) { say_file("time/minutes+.wav"); } else say_file("time/minutes.wav"); } if (seconds) { say_num(seconds, SSM_PRONOUNCED); say_file("time/seconds.wav"); } return SWITCH_STATUS_SUCCESS; } if ((t = atol(tosay)) > 0) { target = switch_time_make(t, 0); target_now = switch_micro_time_now(); } else { target = switch_micro_time_now(); target_now = switch_micro_time_now(); } if (tz) { int check = atoi(tz); switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_DEBUG, "Timezone is [%s]\n", tz); if (check) { switch_time_exp_tz(&tm, target, check); switch_time_exp_tz(&tm_now, target_now, check); } else { switch_time_exp_tz_name(tz, &tm, target); switch_time_exp_tz_name(tz, &tm_now, target_now); } } else { switch_time_exp_lt(&tm, target); switch_time_exp_lt(&tm_now, target_now); } switch (say_args->type) { case SST_CURRENT_DATE_TIME: say_date = say_time = 1; break; case SST_CURRENT_DATE: say_date = 1; break; case SST_CURRENT_TIME: say_time = 1; break; case SST_SHORT_DATE_TIME: say_time = 1; if (tm.tm_year != tm_now.tm_year) { say_date = 1; break; } if (tm.tm_yday == tm_now.tm_yday) { say_today = 1; break; } if (tm.tm_yday == tm_now.tm_yday - 1) { say_yesterday = 1; break; } if (tm.tm_yday >= tm_now.tm_yday - 5) { say_dow = 1; break; } if (tm.tm_mon != tm_now.tm_mon) { say_month = say_day = say_dow = 1; break; } say_month = say_day = say_dow = 1; break; default: break; } if (say_today) { say_file("time/today.wav"); } if (say_yesterday) { say_file("time/yesterday.wav"); } if (say_dow) { say_file("time/day-%d.wav", tm.tm_wday); } if (say_date) { say_year = say_month = say_day = say_dow = 1; say_today = say_yesterday = 0; } gregorian_to_jalali(&jalali_year,&jalali_month,&jalali_day,tm.tm_year + 1900,tm.tm_mon + 1,tm.tm_mday); if (say_day) { if(jalali_day > 20 && jalali_day != 30) { say_file("digits/%d+.wav", (jalali_day - jalali_day % 10)); say_file("digits/%de.wav", jalali_day % 10); } else say_file("digits/%de.wav", jalali_day); } if (say_month) { say_file("time/mon-%d.wav", jalali_month - 1); } if (say_year) { say_num(jalali_year, SSM_PRONOUNCED); } if (say_time) { int32_t hour = tm.tm_hour, pm = 0; if (say_date || say_today || say_yesterday || say_dow) { say_file("time/at.wav"); } if (hour > 12) { hour -= 12; pm = 1; } else if (hour == 12) { pm = 1; } else if (hour == 0) { hour = 12; pm = 0; } say_file("time/hour-e.wav"); say_file("digits/%do.wav",hour); play_group(SSM_PRONOUNCED, 0, (tm.tm_min - tm.tm_min % 10) / 10, tm.tm_min % 10, "time/minutes-e.wav", session, args); say_file("time/%s.wav", pm ? "p-m" : "a-m"); } return SWITCH_STATUS_SUCCESS; }