/* ============================================================================ Name : generate_paging_conf.sh Author : ssc Version : v1.0 Copyright : ZYCOO copyright Description : Generate paging info from mysql to paging conf file ============================================================================ */ #include #include #include #include #include #include #include #include #include #include #include int main(int argc, char **argv) { char callfile[64], cmd[128], key[64]; char *redis_host = getenv("REDIS"); char *redis_password = getenv("REDIS_PASSWORD"); unsigned int redis_port = atoi(getenv("REDIS_PORT")); char *exten; cJSON *pJson; redisReply *reply; redisReply *reply2; struct timeval timeout = { 1, 500000 }; redisContext * pContext = redisConnectWithTimeout(redis_host, redis_port, timeout); if (pContext == NULL || pContext->err) { if (pContext) { printf("Connection error: %s\n", pContext->errstr); redisFree(pContext); } else { printf("Connection error: can't allocate redis context\n"); } return 0; } //数据库登录认证 reply = redisCommand(pContext, "AUTH %s", redis_password); if (reply->type == REDIS_REPLY_ERROR) { printf("Redis认证失败!\n"); freeReplyObject(reply); redisFree(pContext); return 0; } freeReplyObject(reply); //选择数据库 reply = redisCommand(pContext, "SELECT 0"); freeReplyObject(reply); while(1) { reply = redisCommand(pContext, "KEYS alarm-*"); if (reply == NULL || reply->type == REDIS_REPLY_ERROR) { printf("Error: %s\n", reply ? reply->str : "NULL reply"); continue; } if (reply->type == REDIS_REPLY_ARRAY) { for (size_t i = 0; i < reply->elements; i++) { memset(key, '\0', sizeof(key)); strcpy(key, reply->element[i]->str); exten = reply->element[i]->str + 6; char *n = strchr(exten,'-'); if(n) { exten[n - exten] = '\0'; } reply2 = redisCommand(pContext, "GET sip-status-id-%s", exten); if (reply2 == NULL || reply2->type == REDIS_REPLY_ERROR) { printf("Error: %s\n", reply2 ? reply2->str : "NULL reply"); continue; } if(reply2->type == REDIS_REPLY_STRING) { if(strcmp(reply2->str,"idle") == 0) { freeReplyObject(reply2); reply2 = redisCommand(pContext, "GET %s", key); if (reply2 == NULL || reply2->type == REDIS_REPLY_ERROR) { printf("Error: %s\n", reply2 ? reply2->str : "NULL reply"); continue; } if(reply2->type == REDIS_REPLY_STRING) { pJson = cJSON_Parse(reply2->str); if ( pJson ) { memset(callfile, 0, sizeof(callfile)); sprintf(callfile, "/tmp/%s.call", key); FILE *callfile_fp = fopen(callfile, "w+"); fprintf(callfile_fp, "\ Channel: Local/%s@clickdial\n\ Context: playback-ttspaging\n\ Extension: s\n\ Priority: 1\n\ Callerid: Alarm <999999>\n\ MaxRetries: 30\n\ RetryTime: 10\n\ Setvar: __AUDIOFILE=%s\n\ Setvar: WDAutoAnswer=1\n\ WaitTime: 600\n\ \n", \ cJSON_GetObjectItem(pJson, "exten")->valuestring,\ cJSON_GetObjectItem(pJson, "audiofile")->valuestring\ ); fclose(callfile_fp); sprintf(cmd, "mv %s /var/spool/asterisk/outgoing/", callfile); system(cmd); redisCommand(pContext, "DEL %s", key); } } } } freeReplyObject(reply2); sleep(1); } } freeReplyObject(reply); sleep(1); } redisFree( pContext ); }