| 1 |
/* este fichero agi se encarga de reorganizar de forma aleatoria una lista de |
|---|
| 2 |
extensiones que le hayamos pasado. Deberiamos ponerlo en: |
|---|
| 3 |
/var/lib/asterisk/agi-bin/resort.agi |
|---|
| 4 |
para ello compilar con: |
|---|
| 5 |
gcc resort-agi.c --static -o /var/lib/asterisk/agi-bin/resort.agi |
|---|
| 6 |
*/ |
|---|
| 7 |
#include <stdio.h> |
|---|
| 8 |
#include <stdlib.h> |
|---|
| 9 |
#include <string.h> |
|---|
| 10 |
#include <time.h> |
|---|
| 11 |
|
|---|
| 12 |
#define DEBUG 1 |
|---|
| 13 |
FILE *DEBUGFILE=NULL; |
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
int ResortExt(char *in,char *out) |
|---|
| 17 |
{ |
|---|
| 18 |
int a,b,cont,ran2; |
|---|
| 19 |
float ran; |
|---|
| 20 |
char campos[1024],*punt; |
|---|
| 21 |
|
|---|
| 22 |
cont=1; /* contar cuantos campos tenemos */ |
|---|
| 23 |
for(a=0;in[a];a++) /* 1 mas que el numero de signos menos */ |
|---|
| 24 |
if(in[a]=='-') |
|---|
| 25 |
cont++; |
|---|
| 26 |
memset(campos,0,sizeof(campos)); /* inicializar a 0s */ |
|---|
| 27 |
out[0]=0; |
|---|
| 28 |
for(a=0;a<cont;a++) { /* reorganizar lista */ |
|---|
| 29 |
ran=(float)random(); |
|---|
| 30 |
ran=ran/RAND_MAX; |
|---|
| 31 |
ran=ran*(cont-a); |
|---|
| 32 |
ran2=(int)ran; |
|---|
| 33 |
for(b=0;b<cont;b++) /* buscar campo a devolver */ |
|---|
| 34 |
if(campos[b]==0) { |
|---|
| 35 |
ran2--; |
|---|
| 36 |
if(ran2<0) |
|---|
| 37 |
break; |
|---|
| 38 |
} |
|---|
| 39 |
ran2=b; |
|---|
| 40 |
if(ran2>=cont) { /* no deberia pasar, pero por si acaso */ |
|---|
| 41 |
ran2=cont-1; |
|---|
| 42 |
} |
|---|
| 43 |
campos[ran2]=1; /* poner que campo ocupado */ |
|---|
| 44 |
for(b=0,punt=in;b<ran2;b++) /* buscar de donde sacar valor */ |
|---|
| 45 |
punt=strchr(punt,'-')+1; |
|---|
| 46 |
while(*punt && *punt!='-') /* copiar hasta el menos o el final */ |
|---|
| 47 |
*out++=*punt++; |
|---|
| 48 |
*out++='-'; /* aniadir el caracter menos */ |
|---|
| 49 |
} |
|---|
| 50 |
out[-1]=0; /* quitar el ultimo menos */ |
|---|
| 51 |
return cont; |
|---|
| 52 |
} |
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
int main(int narg,char *args[]) |
|---|
| 56 |
{ |
|---|
| 57 |
char buffer[1024],buffer2[1024]; |
|---|
| 58 |
int a; |
|---|
| 59 |
|
|---|
| 60 |
if(DEBUG) |
|---|
| 61 |
DEBUGFILE=fopen("/tmp/debugfile","wt"); |
|---|
| 62 |
else |
|---|
| 63 |
DEBUGFILE=stderr; |
|---|
| 64 |
|
|---|
| 65 |
srandom(time(NULL)); /* inicializar numero aleatorios */ |
|---|
| 66 |
/* use line buffering */ |
|---|
| 67 |
setlinebuf(stdout); |
|---|
| 68 |
setlinebuf(stderr); |
|---|
| 69 |
if(DEBUGFILE) |
|---|
| 70 |
fprintf(DEBUGFILE,"Numero parametros %i\n",narg); |
|---|
| 71 |
for(a=0;a<narg;a++) |
|---|
| 72 |
if(DEBUGFILE) |
|---|
| 73 |
fprintf(DEBUGFILE,"%s\n",args[a]); |
|---|
| 74 |
/* read and ignore AGI environment */ |
|---|
| 75 |
while (1) { |
|---|
| 76 |
fgets(buffer,sizeof(buffer),stdin); |
|---|
| 77 |
if(DEBUGFILE) fprintf(DEBUGFILE,"%s",buffer); |
|---|
| 78 |
if (strlen(buffer) <= 1) break; |
|---|
| 79 |
} |
|---|
| 80 |
|
|---|
| 81 |
// printf("GET VARIABLE ARG3\n"); |
|---|
| 82 |
// fgets(buffer,sizeof(buffer),stdin); |
|---|
| 83 |
// if(DEBUGFILE) |
|---|
| 84 |
// fprintf(DEBUGFILE,"%s\n",buffer); |
|---|
| 85 |
|
|---|
| 86 |
// printf("GET VARIABLE RingGroupMethod\n"); |
|---|
| 87 |
// fgets(buffer,sizeof(buffer),stdin); |
|---|
| 88 |
// if(DEBUGFILE) |
|---|
| 89 |
// fprintf(DEBUGFILE,"%s\n",buffer); |
|---|
| 90 |
|
|---|
| 91 |
sprintf(buffer,"12345-23456-34567-45678"); |
|---|
| 92 |
if(narg>=2) |
|---|
| 93 |
strcpy(buffer,args[1]); |
|---|
| 94 |
|
|---|
| 95 |
|
|---|
| 96 |
ResortExt(buffer,buffer2); |
|---|
| 97 |
|
|---|
| 98 |
printf("SET VARIABLE RESULT %s\n",buffer2); |
|---|
| 99 |
if(DEBUGFILE) |
|---|
| 100 |
fprintf(DEBUGFILE,"SET VAR RESULT %s\n",buffer2); |
|---|
| 101 |
|
|---|
| 102 |
/* Read response from Asterisk and show on console */ |
|---|
| 103 |
fgets(buffer,sizeof(buffer),stdin); |
|---|
| 104 |
if(DEBUGFILE) |
|---|
| 105 |
fprintf(DEBUGFILE,"%s\n",buffer); |
|---|
| 106 |
|
|---|
| 107 |
return 0; |
|---|
| 108 |
} |
|---|