callerid.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005, Digium, Inc.
  5. *
  6. * Mark Spencer <markster@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. *
  20. * \brief CallerID Generation support
  21. *
  22. */
  23. #include <time.h>
  24. #include <string.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <unistd.h>
  28. #include <math.h>
  29. #include <ctype.h>
  30. #include "asterisk.h"
  31. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  32. #include "asterisk/ulaw.h"
  33. #include "asterisk/alaw.h"
  34. #include "asterisk/frame.h"
  35. #include "asterisk/channel.h"
  36. #include "asterisk/callerid.h"
  37. #include "asterisk/logger.h"
  38. #include "asterisk/fskmodem.h"
  39. #include "asterisk/utils.h"
  40. struct callerid_state {
  41. fsk_data fskd;
  42. char rawdata[256];
  43. short oldstuff[160];
  44. int oldlen;
  45. int pos;
  46. int type;
  47. int cksum;
  48. char name[64];
  49. char number[64];
  50. int flags;
  51. int sawflag;
  52. int len;
  53. };
  54. float cid_dr[4], cid_di[4];
  55. float clidsb = 8000.0 / 1200.0;
  56. float sasdr, sasdi;
  57. float casdr1, casdi1, casdr2, casdi2;
  58. #define CALLERID_SPACE 2200.0 /*!< 2200 hz for "0" */
  59. #define CALLERID_MARK 1200.0 /*!< 1200 hz for "1" */
  60. #define SAS_FREQ 440.0
  61. #define CAS_FREQ1 2130.0
  62. #define CAS_FREQ2 2750.0
  63. #define AST_CALLERID_UNKNOWN "<unknown>"
  64. static inline void gen_tones(unsigned char *buf, int len, int codec, float ddr1, float ddi1, float ddr2, float ddi2, float *cr1, float *ci1, float *cr2, float *ci2)
  65. {
  66. int x;
  67. float t;
  68. for (x=0;x<len;x++) {
  69. t = *cr1 * ddr1 - *ci1 * ddi1;
  70. *ci1 = *cr1 * ddi1 + *ci1 * ddr1;
  71. *cr1 = t;
  72. t = 2.0 - (*cr1 * *cr1 + *ci1 * *ci1);
  73. *cr1 *= t;
  74. *ci1 *= t;
  75. t = *cr2 * ddr2 - *ci2 * ddi2;
  76. *ci2 = *cr2 * ddi2 + *ci2 * ddr2;
  77. *cr2 = t;
  78. t = 2.0 - (*cr2 * *cr2 + *ci2 * *ci2);
  79. *cr2 *= t;
  80. *ci2 *= t;
  81. buf[x] = AST_LIN2X((*cr1 + *cr2) * 2048.0);
  82. }
  83. }
  84. static inline void gen_tone(unsigned char *buf, int len, int codec, float ddr1, float ddi1, float *cr1, float *ci1)
  85. {
  86. int x;
  87. float t;
  88. for (x=0;x<len;x++) {
  89. t = *cr1 * ddr1 - *ci1 * ddi1;
  90. *ci1 = *cr1 * ddi1 + *ci1 * ddr1;
  91. *cr1 = t;
  92. t = 2.0 - (*cr1 * *cr1 + *ci1 * *ci1);
  93. *cr1 *= t;
  94. *ci1 *= t;
  95. buf[x] = AST_LIN2X(*cr1 * 8192.0);
  96. }
  97. }
  98. /*! \brief Initialize stuff for inverse FFT */
  99. void callerid_init(void)
  100. {
  101. cid_dr[0] = cos(CALLERID_SPACE * 2.0 * M_PI / 8000.0);
  102. cid_di[0] = sin(CALLERID_SPACE * 2.0 * M_PI / 8000.0);
  103. cid_dr[1] = cos(CALLERID_MARK * 2.0 * M_PI / 8000.0);
  104. cid_di[1] = sin(CALLERID_MARK * 2.0 * M_PI / 8000.0);
  105. sasdr = cos(SAS_FREQ * 2.0 * M_PI / 8000.0);
  106. sasdi = sin(SAS_FREQ * 2.0 * M_PI / 8000.0);
  107. casdr1 = cos(CAS_FREQ1 * 2.0 * M_PI / 8000.0);
  108. casdi1 = sin(CAS_FREQ1 * 2.0 * M_PI / 8000.0);
  109. casdr2 = cos(CAS_FREQ2 * 2.0 * M_PI / 8000.0);
  110. casdi2 = sin(CAS_FREQ2 * 2.0 * M_PI / 8000.0);
  111. }
  112. struct callerid_state *callerid_new(int cid_signalling)
  113. {
  114. struct callerid_state *cid;
  115. cid = malloc(sizeof(struct callerid_state));
  116. if (cid) {
  117. memset(cid, 0, sizeof(struct callerid_state));
  118. cid->fskd.spb = 7; /* 1200 baud */
  119. cid->fskd.hdlc = 0; /* Async */
  120. cid->fskd.nbit = 8; /* 8 bits */
  121. cid->fskd.nstop = 1; /* 1 stop bit */
  122. cid->fskd.paridad = 0; /* No parity */
  123. cid->fskd.bw=1; /* Filter 800 Hz */
  124. if (cid_signalling == 2) { /* v23 signalling */
  125. cid->fskd.f_mark_idx = 4; /* 1300 Hz */
  126. cid->fskd.f_space_idx = 5; /* 2100 Hz */
  127. } else { /* Bell 202 signalling as default */
  128. cid->fskd.f_mark_idx = 2; /* 1200 Hz */
  129. cid->fskd.f_space_idx = 3; /* 2200 Hz */
  130. }
  131. cid->fskd.pcola = 0; /* No clue */
  132. cid->fskd.cont = 0; /* Digital PLL reset */
  133. cid->fskd.x0 = 0.0;
  134. cid->fskd.state = 0;
  135. memset(cid->name, 0, sizeof(cid->name));
  136. memset(cid->number, 0, sizeof(cid->number));
  137. cid->flags = CID_UNKNOWN_NAME | CID_UNKNOWN_NUMBER;
  138. cid->pos = 0;
  139. } else
  140. ast_log(LOG_WARNING, "Out of memory\n");
  141. return cid;
  142. }
  143. void callerid_get(struct callerid_state *cid, char **name, char **number, int *flags)
  144. {
  145. *flags = cid->flags;
  146. if (cid->flags & (CID_UNKNOWN_NAME | CID_PRIVATE_NUMBER))
  147. *name = NULL;
  148. else
  149. *name = cid->name;
  150. if (cid->flags & (CID_UNKNOWN_NUMBER | CID_PRIVATE_NUMBER))
  151. *number = NULL;
  152. else
  153. *number = cid->number;
  154. }
  155. void callerid_get_dtmf(char *cidstring, char *number, int *flags)
  156. {
  157. int i;
  158. int code;
  159. /* "Clear" the number-buffer. */
  160. number[0] = 0;
  161. if (strlen(cidstring) < 2) {
  162. ast_log(LOG_DEBUG, "No cid detected\n");
  163. *flags = CID_UNKNOWN_NUMBER;
  164. return;
  165. }
  166. /* Detect protocol and special types */
  167. if (cidstring[0] == 'B') {
  168. /* Handle special codes */
  169. code = atoi(&cidstring[1]);
  170. if (code == 0)
  171. *flags = CID_UNKNOWN_NUMBER;
  172. else if (code == 10)
  173. *flags = CID_PRIVATE_NUMBER;
  174. else
  175. ast_log(LOG_DEBUG, "Unknown DTMF code %d\n", code);
  176. } else if (cidstring[0] == 'D' && cidstring[2] == '#') {
  177. /* .DK special code */
  178. if (cidstring[1] == '1')
  179. *flags = CID_PRIVATE_NUMBER;
  180. if (cidstring[1] == '2' || cidstring[1] == '3')
  181. *flags = CID_UNKNOWN_NUMBER;
  182. } else if (cidstring[0] == 'D' || cidstring[0] == 'A') {
  183. /* "Standard" callerid */
  184. for (i = 1; i < strlen(cidstring); i++ ) {
  185. if (cidstring[i] == 'C' || cidstring[i] == '#')
  186. break;
  187. if (isdigit(cidstring[i]))
  188. number[i-1] = cidstring[i];
  189. else
  190. ast_log(LOG_DEBUG, "Unknown CID digit '%c'\n",
  191. cidstring[i]);
  192. }
  193. number[i-1] = 0;
  194. } else if (isdigit(cidstring[0])) {
  195. /* It begins with a digit, so we parse it as a number and hope
  196. * for the best */
  197. ast_log(LOG_WARNING, "Couldn't detect start-character. CID "
  198. "parsing might be unreliable\n");
  199. for (i = 0; i < strlen(cidstring); i++) {
  200. if (isdigit(cidstring[i]))
  201. number[i] = cidstring[i];
  202. else
  203. break;
  204. }
  205. number[i] = 0;
  206. } else {
  207. ast_log(LOG_DEBUG, "Unknown CID protocol, start digit '%c'\n",
  208. cidstring[0]);
  209. *flags = CID_UNKNOWN_NUMBER;
  210. }
  211. }
  212. int ast_gen_cas(unsigned char *outbuf, int sendsas, int len, int codec)
  213. {
  214. int pos = 0;
  215. int saslen=2400;
  216. float cr1 = 1.0;
  217. float ci1 = 0.0;
  218. float cr2 = 1.0;
  219. float ci2 = 0.0;
  220. if (sendsas) {
  221. if (len < saslen)
  222. return -1;
  223. gen_tone(outbuf, saslen, codec, sasdr, sasdi, &cr1, &ci1);
  224. len -= saslen;
  225. pos += saslen;
  226. cr2 = cr1;
  227. ci2 = ci1;
  228. }
  229. gen_tones(outbuf + pos, len, codec, casdr1, casdi1, casdr2, casdi2, &cr1, &ci1, &cr2, &ci2);
  230. return 0;
  231. }
  232. int callerid_feed(struct callerid_state *cid, unsigned char *ubuf, int len, int codec)
  233. {
  234. int mylen = len;
  235. int olen;
  236. int b = 'X';
  237. int res;
  238. int x;
  239. short *buf = malloc(2 * len + cid->oldlen);
  240. short *obuf = buf;
  241. if (!buf) {
  242. ast_log(LOG_WARNING, "Out of memory\n");
  243. return -1;
  244. }
  245. memset(buf, 0, 2 * len + cid->oldlen);
  246. memcpy(buf, cid->oldstuff, cid->oldlen);
  247. mylen += cid->oldlen/2;
  248. for (x=0;x<len;x++)
  249. buf[x+cid->oldlen/2] = AST_XLAW(ubuf[x]);
  250. while(mylen >= 160) {
  251. olen = mylen;
  252. res = fsk_serie(&cid->fskd, buf, &mylen, &b);
  253. if (mylen < 0) {
  254. ast_log(LOG_ERROR, "fsk_serie made mylen < 0 (%d)\n", mylen);
  255. free(obuf);
  256. return -1;
  257. }
  258. buf += (olen - mylen);
  259. if (res < 0) {
  260. ast_log(LOG_NOTICE, "fsk_serie failed\n");
  261. free(obuf);
  262. return -1;
  263. }
  264. if (res == 1) {
  265. /* Ignore invalid bytes */
  266. if (b > 0xff)
  267. continue;
  268. switch(cid->sawflag) {
  269. case 0: /* Look for flag */
  270. if (b == 'U')
  271. cid->sawflag = 2;
  272. break;
  273. case 2: /* Get lead-in */
  274. if ((b == 0x04) || (b == 0x80)) {
  275. cid->type = b;
  276. cid->sawflag = 3;
  277. cid->cksum = b;
  278. }
  279. break;
  280. case 3: /* Get length */
  281. /* Not a lead in. We're ready */
  282. cid->sawflag = 4;
  283. cid->len = b;
  284. cid->pos = 0;
  285. cid->cksum += b;
  286. break;
  287. case 4: /* Retrieve message */
  288. if (cid->pos >= 128) {
  289. ast_log(LOG_WARNING, "Caller ID too long???\n");
  290. free(obuf);
  291. return -1;
  292. }
  293. cid->rawdata[cid->pos++] = b;
  294. cid->len--;
  295. cid->cksum += b;
  296. if (!cid->len) {
  297. cid->rawdata[cid->pos] = '\0';
  298. cid->sawflag = 5;
  299. }
  300. break;
  301. case 5: /* Check checksum */
  302. if (b != (256 - (cid->cksum & 0xff))) {
  303. ast_log(LOG_NOTICE, "Caller*ID failed checksum\n");
  304. /* Try again */
  305. cid->sawflag = 0;
  306. break;
  307. }
  308. cid->number[0] = '\0';
  309. cid->name[0] = '\0';
  310. /* If we get this far we're fine. */
  311. if (cid->type == 0x80) {
  312. /* MDMF */
  313. /* Go through each element and process */
  314. for (x=0;x< cid->pos;) {
  315. switch(cid->rawdata[x++]) {
  316. case 1:
  317. /* Date */
  318. break;
  319. case 2: /* Number */
  320. case 3: /* Number (for Zebble) */
  321. case 4: /* Number */
  322. res = cid->rawdata[x];
  323. if (res > 32) {
  324. ast_log(LOG_NOTICE, "Truncating long caller ID number from %d bytes to 32\n", cid->rawdata[x]);
  325. res = 32;
  326. }
  327. if (ast_strlen_zero(cid->number)) {
  328. memcpy(cid->number, cid->rawdata + x + 1, res);
  329. /* Null terminate */
  330. cid->number[res] = '\0';
  331. }
  332. break;
  333. case 6: /* Stentor Call Qualifier (ie. Long Distance call) */
  334. break;
  335. case 7: /* Name */
  336. case 8: /* Name */
  337. res = cid->rawdata[x];
  338. if (res > 32) {
  339. ast_log(LOG_NOTICE, "Truncating long caller ID name from %d bytes to 32\n", cid->rawdata[x]);
  340. res = 32;
  341. }
  342. memcpy(cid->name, cid->rawdata + x + 1, res);
  343. cid->name[res] = '\0';
  344. break;
  345. case 17: /* UK: Call type, 1=Voice Call, 2=Ringback when free, 129=Message waiting */
  346. case 19: /* UK: Network message system status (Number of messages waiting) */
  347. case 22: /* Something French */
  348. break;
  349. default:
  350. ast_log(LOG_NOTICE, "Unknown IE %d\n", cid->rawdata[x-1]);
  351. }
  352. x += cid->rawdata[x];
  353. x++;
  354. }
  355. } else {
  356. /* SDMF */
  357. ast_copy_string(cid->number, cid->rawdata + 8, sizeof(cid->number));
  358. }
  359. /* Update flags */
  360. cid->flags = 0;
  361. if (!strcmp(cid->number, "P")) {
  362. strcpy(cid->number, "");
  363. cid->flags |= CID_PRIVATE_NUMBER;
  364. } else if (!strcmp(cid->number, "O") || ast_strlen_zero(cid->number)) {
  365. strcpy(cid->number, "");
  366. cid->flags |= CID_UNKNOWN_NUMBER;
  367. }
  368. if (!strcmp(cid->name, "P")) {
  369. strcpy(cid->name, "");
  370. cid->flags |= CID_PRIVATE_NAME;
  371. } else if (!strcmp(cid->name, "O") || ast_strlen_zero(cid->name)) {
  372. strcpy(cid->name, "");
  373. cid->flags |= CID_UNKNOWN_NAME;
  374. }
  375. free(obuf);
  376. return 1;
  377. break;
  378. default:
  379. ast_log(LOG_ERROR, "Dunno what to do with a digit in sawflag %d\n", cid->sawflag);
  380. }
  381. }
  382. }
  383. if (mylen) {
  384. memcpy(cid->oldstuff, buf, mylen * 2);
  385. cid->oldlen = mylen * 2;
  386. } else
  387. cid->oldlen = 0;
  388. free(obuf);
  389. return 0;
  390. }
  391. void callerid_free(struct callerid_state *cid)
  392. {
  393. free(cid);
  394. }
  395. static int callerid_genmsg(char *msg, int size, char *number, char *name, int flags)
  396. {
  397. time_t t;
  398. struct tm tm;
  399. char *ptr;
  400. int res;
  401. int i,x;
  402. /* Get the time */
  403. time(&t);
  404. localtime_r(&t,&tm);
  405. ptr = msg;
  406. /* Format time and message header */
  407. res = snprintf(ptr, size, "\001\010%02d%02d%02d%02d", tm.tm_mon + 1,
  408. tm.tm_mday, tm.tm_hour, tm.tm_min);
  409. size -= res;
  410. ptr += res;
  411. if (ast_strlen_zero(number) || (flags & CID_UNKNOWN_NUMBER)) {
  412. /* Indicate number not known */
  413. res = snprintf(ptr, size, "\004\001O");
  414. size -= res;
  415. ptr += res;
  416. } else if (flags & CID_PRIVATE_NUMBER) {
  417. /* Indicate number is private */
  418. res = snprintf(ptr, size, "\004\001P");
  419. size -= res;
  420. ptr += res;
  421. } else {
  422. /* Send up to 16 digits of number MAX */
  423. i = strlen(number);
  424. if (i > 16) i = 16;
  425. res = snprintf(ptr, size, "\002%c", i);
  426. size -= res;
  427. ptr += res;
  428. for (x=0;x<i;x++)
  429. ptr[x] = number[x];
  430. ptr[i] = '\0';
  431. ptr += i;
  432. size -= i;
  433. }
  434. if (ast_strlen_zero(name) || (flags & CID_UNKNOWN_NAME)) {
  435. /* Indicate name not known */
  436. res = snprintf(ptr, size, "\010\001O");
  437. size -= res;
  438. ptr += res;
  439. } else if (flags & CID_PRIVATE_NAME) {
  440. /* Indicate name is private */
  441. res = snprintf(ptr, size, "\010\001P");
  442. size -= res;
  443. ptr += res;
  444. } else {
  445. /* Send up to 16 digits of name MAX */
  446. i = strlen(name);
  447. if (i > 16) i = 16;
  448. res = snprintf(ptr, size, "\007%c", i);
  449. size -= res;
  450. ptr += res;
  451. for (x=0;x<i;x++)
  452. ptr[x] = name[x];
  453. ptr[i] = '\0';
  454. ptr += i;
  455. size -= i;
  456. }
  457. return (ptr - msg);
  458. }
  459. int vmwi_generate(unsigned char *buf, int active, int mdmf, int codec)
  460. {
  461. unsigned char msg[256];
  462. int len=0;
  463. int sum;
  464. int x;
  465. int bytes = 0;
  466. float cr = 1.0;
  467. float ci = 0.0;
  468. float scont = 0.0;
  469. if (mdmf) {
  470. /* MDMF Message waiting */
  471. msg[len++] = 0x82;
  472. /* Length is 3 */
  473. msg[len++] = 3;
  474. /* IE is "Message Waiting Parameter" */
  475. msg[len++] = 0xb;
  476. /* Length of IE is one */
  477. msg[len++] = 1;
  478. /* Active or not */
  479. if (active)
  480. msg[len++] = 0xff;
  481. else
  482. msg[len++] = 0x00;
  483. } else {
  484. /* SDMF Message waiting */
  485. msg[len++] = 0x6;
  486. /* Length is 3 */
  487. msg[len++] = 3;
  488. if (active) {
  489. msg[len++] = 0x42;
  490. msg[len++] = 0x42;
  491. msg[len++] = 0x42;
  492. } else {
  493. msg[len++] = 0x6f;
  494. msg[len++] = 0x6f;
  495. msg[len++] = 0x6f;
  496. }
  497. }
  498. sum = 0;
  499. for (x=0;x<len;x++)
  500. sum += msg[x];
  501. sum = (256 - (sum & 255));
  502. msg[len++] = sum;
  503. /* Wait a half a second */
  504. for (x=0;x<4000;x++)
  505. PUT_BYTE(0x7f);
  506. /* Transmit 30 0x55's (looks like a square wave) for channel seizure */
  507. for (x=0;x<30;x++)
  508. PUT_CLID(0x55);
  509. /* Send 170ms of callerid marks */
  510. for (x=0;x<170;x++)
  511. PUT_CLID_MARKMS;
  512. for (x=0;x<len;x++) {
  513. PUT_CLID(msg[x]);
  514. }
  515. /* Send 50 more ms of marks */
  516. for (x=0;x<50;x++)
  517. PUT_CLID_MARKMS;
  518. return bytes;
  519. }
  520. int callerid_generate(unsigned char *buf, char *number, char *name, int flags, int callwaiting, int codec)
  521. {
  522. int bytes=0;
  523. int x, sum;
  524. int len;
  525. /* Initial carriers (real/imaginary) */
  526. float cr = 1.0;
  527. float ci = 0.0;
  528. float scont = 0.0;
  529. char msg[256];
  530. len = callerid_genmsg(msg, sizeof(msg), number, name, flags);
  531. if (!callwaiting) {
  532. /* Wait a half a second */
  533. for (x=0;x<4000;x++)
  534. PUT_BYTE(0x7f);
  535. /* Transmit 30 0x55's (looks like a square wave) for channel seizure */
  536. for (x=0;x<30;x++)
  537. PUT_CLID(0x55);
  538. }
  539. /* Send 150ms of callerid marks */
  540. for (x=0;x<150;x++)
  541. PUT_CLID_MARKMS;
  542. /* Send 0x80 indicating MDMF format */
  543. PUT_CLID(0x80);
  544. /* Put length of whole message */
  545. PUT_CLID(len);
  546. sum = 0x80 + strlen(msg);
  547. /* Put each character of message and update checksum */
  548. for (x=0;x<len; x++) {
  549. PUT_CLID(msg[x]);
  550. sum += msg[x];
  551. }
  552. /* Send 2's compliment of sum */
  553. PUT_CLID(256 - (sum & 255));
  554. /* Send 50 more ms of marks */
  555. for (x=0;x<50;x++)
  556. PUT_CLID_MARKMS;
  557. return bytes;
  558. }
  559. void ast_shrink_phone_number(char *n)
  560. {
  561. int x,y=0;
  562. int bracketed=0;
  563. for (x=0;n[x];x++) {
  564. switch(n[x]) {
  565. case '[':
  566. bracketed++;
  567. n[y++] = n[x];
  568. break;
  569. case ']':
  570. bracketed--;
  571. n[y++] = n[x];
  572. break;
  573. case '-':
  574. if (bracketed)
  575. n[y++] = n[x];
  576. break;
  577. case '.':
  578. if (!n[x+1])
  579. n[y++] = n[x];
  580. break;
  581. default:
  582. if (!strchr("( )", n[x]))
  583. n[y++] = n[x];
  584. }
  585. }
  586. n[y] = '\0';
  587. }
  588. /*! \brief checks if string consists only of digits and * \# and +
  589. \return 1 if string is valid AST phone number
  590. \return 0 if not
  591. */
  592. int ast_isphonenumber(char *n)
  593. {
  594. int x;
  595. if (ast_strlen_zero(n))
  596. return 0;
  597. for (x=0;n[x];x++)
  598. if (!strchr("0123456789*#+", n[x]))
  599. return 0;
  600. return 1;
  601. }
  602. /*! \brief parse string for caller id information
  603. \return returns -1 on failure, otherwise 0
  604. */
  605. int ast_callerid_parse(char *instr, char **name, char **location)
  606. {
  607. char *ns, *ne;
  608. char *ls, *le;
  609. char tmp[256];
  610. /* Try for "name" <location> format or
  611. name <location> format */
  612. if ((ls = strchr(instr, '<')) && (le = strchr(ls, '>'))) {
  613. /* Found the location */
  614. *le = '\0';
  615. *ls = '\0';
  616. *location = ls + 1;
  617. if ((ns = strchr(instr, '\"')) && (ne = strchr(ns + 1, '\"'))) {
  618. /* Get name out of quotes */
  619. *ns = '\0';
  620. *ne = '\0';
  621. *name = ns + 1;
  622. return 0;
  623. } else {
  624. /* Just trim off any trailing spaces */
  625. *name = instr;
  626. while(!ast_strlen_zero(instr) && (instr[strlen(instr) - 1] < 33))
  627. instr[strlen(instr) - 1] = '\0';
  628. /* And leading spaces */
  629. *name = ast_skip_blanks(*name);
  630. return 0;
  631. }
  632. } else {
  633. ast_copy_string(tmp, instr, sizeof(tmp));
  634. ast_shrink_phone_number(tmp);
  635. if (ast_isphonenumber(tmp)) {
  636. /* Assume it's just a location */
  637. *name = NULL;
  638. *location = instr;
  639. } else {
  640. /* Assume it's just a name. Make sure it's not quoted though */
  641. *name = instr;
  642. while(*(*name) && ((*(*name) < 33) || (*(*name) == '\"'))) (*name)++;
  643. ne = *name + strlen(*name) - 1;
  644. while((ne > *name) && ((*ne < 33) || (*ne == '\"'))) { *ne = '\0'; ne--; }
  645. *location = NULL;
  646. }
  647. return 0;
  648. }
  649. return -1;
  650. }
  651. static int __ast_callerid_generate(unsigned char *buf, char *name, char *number, int callwaiting, int codec)
  652. {
  653. if (ast_strlen_zero(name))
  654. name = NULL;
  655. if (ast_strlen_zero(number))
  656. number = NULL;
  657. return callerid_generate(buf, number, name, 0, callwaiting, codec);
  658. }
  659. int ast_callerid_generate(unsigned char *buf, char *name, char *number, int codec)
  660. {
  661. return __ast_callerid_generate(buf, name, number, 0, codec);
  662. }
  663. int ast_callerid_callwaiting_generate(unsigned char *buf, char *name, char *number, int codec)
  664. {
  665. return __ast_callerid_generate(buf, name, number, 1, codec);
  666. }
  667. char *ast_callerid_merge(char *buf, int bufsiz, const char *name, const char *num, const char *unknown)
  668. {
  669. if (!unknown)
  670. unknown = "<unknown>";
  671. if (name && num)
  672. snprintf(buf, bufsiz, "\"%s\" <%s>", name, num);
  673. else if (name)
  674. ast_copy_string(buf, name, bufsiz);
  675. else if (num)
  676. ast_copy_string(buf, num, bufsiz);
  677. else
  678. ast_copy_string(buf, unknown, bufsiz);
  679. return buf;
  680. }
  681. int ast_callerid_split(const char *buf, char *name, int namelen, char *num, int numlen)
  682. {
  683. char *tmp;
  684. char *l = NULL, *n = NULL;
  685. tmp = ast_strdupa(buf);
  686. if (!tmp) {
  687. name[0] = '\0';
  688. num[0] = '\0';
  689. return -1;
  690. }
  691. ast_callerid_parse(tmp, &n, &l);
  692. if (n)
  693. ast_copy_string(name, n, namelen);
  694. else
  695. name[0] = '\0';
  696. if (l) {
  697. ast_shrink_phone_number(l);
  698. ast_copy_string(num, l, numlen);
  699. } else
  700. num[0] = '\0';
  701. return 0;
  702. }
  703. static struct {
  704. int val;
  705. char *name;
  706. char *description;
  707. } pres_types[] = {
  708. { AST_PRES_ALLOWED_USER_NUMBER_NOT_SCREENED, "allowed_not_screened", "Presentation Allowed, Not Screened"},
  709. { AST_PRES_ALLOWED_USER_NUMBER_PASSED_SCREEN, "allowed_passed_screen", "Presentation Allowed, Passed Screen"},
  710. { AST_PRES_ALLOWED_USER_NUMBER_FAILED_SCREEN, "allowed_failed_screen", "Presentation Allowed, Failed Screen"},
  711. { AST_PRES_ALLOWED_NETWORK_NUMBER, "allowed", "Presentation Allowed, Network Number"},
  712. { AST_PRES_PROHIB_USER_NUMBER_NOT_SCREENED, "prohib_not_screened", "Presentation Prohibited, Not Screened"},
  713. { AST_PRES_PROHIB_USER_NUMBER_PASSED_SCREEN, "prohib_passed_screen", "Presentation Prohibited, Passed Screen"},
  714. { AST_PRES_PROHIB_USER_NUMBER_FAILED_SCREEN, "prohib_failed_screen", "Presentation Prohibited, Failed Screen"},
  715. { AST_PRES_PROHIB_NETWORK_NUMBER, "prohib", "Presentation Prohibited, Network Number"},
  716. { AST_PRES_NUMBER_NOT_AVAILABLE, "unavailable", "Number Unavailable"},
  717. };
  718. /*! \brief Convert caller ID text code to value
  719. used in config file parsing
  720. \param data text string
  721. \return value AST_PRES_ from callerid.h
  722. */
  723. int ast_parse_caller_presentation(const char *data)
  724. {
  725. int i;
  726. for (i = 0; i < ((sizeof(pres_types) / sizeof(pres_types[0]))); i++) {
  727. if (!strcasecmp(pres_types[i].name, data))
  728. return pres_types[i].val;
  729. }
  730. return -1;
  731. }
  732. /*! \brief Convert caller ID pres value to explanatory string
  733. \param data value (see callerid.h AST_PRES_ )
  734. \return string for human presentation
  735. */
  736. const char *ast_describe_caller_presentation(int data)
  737. {
  738. int i;
  739. for (i = 0; i < ((sizeof(pres_types) / sizeof(pres_types[0]))); i++) {
  740. if (pres_types[i].val == data)
  741. return pres_types[i].description;
  742. }
  743. return "unknown";
  744. }