backup
[lcr.git] / extension.c
1 /*****************************************************************************\
2 **                                                                           **
3 ** PBX4Linux                                                                 **
4 **                                                                           **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg                                              **
7 **                                                                           **
8 ** reading and writing files for extensions                                  **
9 **                                                                           **
10 \*****************************************************************************/ 
11
12 #include "main.h"
13
14 /* extension */
15
16 char *ext_rights[] = {
17         "none",
18         "internal",
19         "local",
20         "national",
21         "international",
22         NULL
23 };
24
25 char *ext_yesno[] = {
26         "no",
27         "yes",
28         NULL
29 };
30
31
32 /* read extension
33  *
34  * reads extension from given extension number and fills structure
35  */
36 int read_extension(struct extension *ext, char *number)
37 {
38         FILE *fp=NULL;
39         char filename[256];
40         char *p;
41         char option[32];
42         char param[256],param2[256];
43         unsigned int line,i;
44         char buffer[1024];
45         int last_in_count = 0, last_out_count = 0;
46
47         if (number[0] == '\0')
48                 return(0);
49
50         SPRINT(filename, "%s/%s/%s/settings", INSTALL_DATA, options.extensions_dir, number);
51
52         if (!(fp = fopen(filename, "r")))
53         {
54                 PDEBUG(DEBUG_CONFIG, "the given extension doesn't exist: \"%s\"\n", filename);
55                 return(0);
56         }
57
58         /* default values */
59         memset(ext, 0, sizeof(struct extension));
60         ext->rights = 4; /* international */
61         ext->tout_setup = 120;
62         ext->tout_dialing = 120;
63         ext->tout_proceeding = 120;
64         ext->tout_alerting = 120;
65         ext->tout_disconnect = 120;
66 //      ext->tout_hold = 900;
67 //      ext->tout_park = 900;
68         ext->cfnr_delay = 20;
69         ext->vbox_codec = CODEC_MONO;
70
71         line=0;
72         while((fgets(buffer, sizeof(buffer), fp)))
73         {
74                 line++;
75                 buffer[sizeof(buffer)-1] = '\0';
76                 if (buffer[0]) buffer[strlen(buffer)-1] = '\0';
77                 p = buffer;
78
79                 while(*p <= 32) /* skip spaces */
80                 {
81                         if (*p == 0)
82                                 break;
83                         p++;
84                 }
85                 if (*p==0 || *p=='#') /* ignore comments and empty line */
86                         continue;
87
88                 option[0]=0;
89                 i=0; /* read option */
90                 while(*p > 32)
91                 {
92                         if (i+1 >= sizeof(option))
93                         {
94                                 PERROR_RUNTIME("Error in %s (line %d): option too long.\n",filename,line);
95                                 break;
96                         }
97                         option[i+1] = '\0';
98                         option[i++] = *p++;
99                 }
100
101                 while(*p <= 32) /* skip spaces */
102                 {
103                         if (*p == 0)
104                                 break;
105                         p++;
106                 }
107
108                 param[0]=0;
109                 param2[0]=0;
110                 if (*p!=0 && *p!='#') /* param */
111                 {
112                         i=0; /* read param */
113                         while(*p > 32)
114                         {
115                                 if (i+1 >= sizeof(param))
116                                 {
117                                         PERROR_RUNTIME("Error in %s (line %d): param too long.\n",filename,line);
118                                         break;
119                                 }
120                                 param[i+1] = '\0';
121                                 param[i++] = *p++;
122                         }
123
124                         while(*p <= 32) /* skip spaces */
125                         {
126                                 if (*p == 0)
127                                         break;
128                                 p++;
129                         }
130
131                         if (*p!=0 && *p!='#') /* param2 */
132                         {
133                                 i=0; /* read param2 */
134                                 while(*p >= 32)
135                                 {
136                                         if (i+1 >= sizeof(param2))
137                                         {
138                                                 PERROR_RUNTIME("Error in %s (line %d): param too long.\n",filename,line);
139                                                 break;
140                                         }
141                                         param2[i+1] = '\0';
142                                         param2[i++] = *p++;
143                                 }
144                         }
145                 }
146
147                 /* at this point we have option and param */
148
149                 /* check option */
150                 if (!strcmp(option,"name"))
151                 {
152                         SCPY(ext->name, param);
153                         if (param2[0])
154                         {
155                                 SCAT(ext->name, " ");
156                                 SCAT(ext->name, param2);
157                         }
158
159                         PDEBUG(DEBUG_CONFIG, "name of extension: %s\n",param);
160                 } else
161                 if (!strcmp(option,"prefix"))
162                 {
163                         SCPY(ext->prefix, param);
164
165                         PDEBUG(DEBUG_CONFIG, "dial prefix on pickup: %s\n",param);
166                 } else
167                 if (!strcmp(option,"next"))
168                 {
169                         SCPY(ext->next, param);
170
171                         PDEBUG(DEBUG_CONFIG, "dial next on pickup: %s\n",param);
172                 } else
173                 if (!strcmp(option,"alarm"))
174                 {
175                         SCPY(ext->alarm, param);
176
177                         PDEBUG(DEBUG_CONFIG, "alarm message (if prefix): %s\n",param);
178                 } else
179                 if (!strcmp(option,"cfu"))
180                 {
181                         SCPY(ext->cfu, param);
182
183                         PDEBUG(DEBUG_CONFIG, "call forward unconditional: %s\n",param);
184                 } else
185                 if (!strcmp(option,"cfb"))
186                 {
187                         SCPY(ext->cfb, param);
188
189                         PDEBUG(DEBUG_CONFIG, "call forward when busy: %s\n",param);
190                 } else
191                 if (!strcmp(option,"cfnr"))
192                 {
193                         SCPY(ext->cfnr, param);
194
195                         PDEBUG(DEBUG_CONFIG, "call forward on no response: %s\n",param);
196                 } else
197                 if (!strcmp(option,"cfnr_delay"))
198                 {
199                         ext->cfnr_delay = atoi(param);
200                         if (ext->cfnr_delay < 0)
201                                 ext->cfnr_delay = 1;
202
203                         PDEBUG(DEBUG_CONFIG, "call forward no response delay: %d\n",ext->cfnr_delay);
204                 } else
205                 if (!strcmp(option,"cfp"))
206                 {
207                         SCPY(ext->cfp, param);
208
209                         PDEBUG(DEBUG_CONFIG, "call forward parallel: %s\n",param);
210                 } else
211                 if (!strcmp(option,"change_forward"))
212                 {
213                         i=0;
214                         while(ext_yesno[i])
215                         {
216                                 if (!strcasecmp(param,ext_yesno[i]))
217                                         break;
218                                 i++;
219                         }
220                         if (ext_yesno[i])
221                         {
222                                 ext->change_forward = i;
223                                 PDEBUG(DEBUG_CONFIG, "allow the change of forwarding: %s\n", ext_yesno[i]);
224                         } else
225                         {
226                                 PDEBUG(DEBUG_CONFIG, "unknown parameter for change_forward: %s\n", param);
227                         }
228                 } else
229                 if (!strcmp(option,"interfaces"))
230                 {
231                         SCPY(ext->interfaces, param);
232
233                         PDEBUG(DEBUG_CONFIG, "interfaces to ring calls to extension: %s %s\n",param,param2);
234                 } else
235                 if (!strcmp(option,"callerid"))
236                 {
237                         ext->callerid_present = INFO_PRESENT_ALLOWED;
238                         if (!strncasecmp(param2, "anonymous", 9))
239                                 ext->callerid_present = INFO_PRESENT_RESTRICTED;
240                         if (!strncasecmp(param, "non", 3))
241                         {
242                                 ext->callerid[0] = '\0';
243                                 ext->callerid_present = INFO_PRESENT_NOTAVAIL;
244                                 ext->callerid_type = INFO_NTYPE_UNKNOWN;
245                                 PDEBUG(DEBUG_CONFIG, "caller id: ID NOT AVAILABLE\n");
246                         } else
247                         switch(param[0])
248                         {
249                                 case 'i':
250                                 case 'I':
251                                 ext->callerid_type = INFO_NTYPE_INTERNATIONAL;
252                                 SCPY(ext->callerid, param+1);
253                                 PDEBUG(DEBUG_CONFIG, "caller id: %s INTERNATIONAL\n",param+1);
254                                 break;
255                                 case 'n':
256                                 case 'N':
257                                 ext->callerid_type = INFO_NTYPE_NATIONAL;
258                                 SCPY(ext->callerid, param+1);
259                                 PDEBUG(DEBUG_CONFIG, "caller id: %s NATIONAL\n",param+1);
260                                 break;
261                                 case 's':
262                                 case 'S':
263                                 ext->callerid_type = INFO_NTYPE_SUBSCRIBER;
264                                 SCPY(ext->callerid, param+1);
265                                 PDEBUG(DEBUG_CONFIG, "caller id: %s SUBSCRIBER\n",param+1);
266                                 break;
267                                 default:
268                                 ext->callerid_type = INFO_NTYPE_UNKNOWN;
269                                 SCPY(ext->callerid, param);
270                                 PDEBUG(DEBUG_CONFIG, "caller id: %s UNKNOWN\n",param);
271                         }
272                         ext->callerid[sizeof(ext->callerid)-1] = 0;
273                 } else
274                 if (!strcmp(option,"id_next_call"))
275                 {
276                         ext->id_next_call_present = INFO_PRESENT_ALLOWED;
277                         if (!strncasecmp(param2, "anonymous", 9))
278                                 ext->id_next_call_present = INFO_PRESENT_RESTRICTED;
279                         if (param[0] == '\0')
280                         {
281                                 ext->id_next_call_present = -1;
282                                 PDEBUG(DEBUG_CONFIG, "id next call: no id for next call\n");
283                         } else
284                         if (!strncasecmp(param, "none", 3))
285                         {
286                                 ext->id_next_call[0] = '\0';
287                                 ext->id_next_call_present = INFO_PRESENT_NOTAVAIL;
288                                 ext->id_next_call_type = INFO_NTYPE_UNKNOWN;
289                                 PDEBUG(DEBUG_CONFIG, "id next call: ID NOT AVAILABLE\n");
290                         } else
291                         switch(param[0])
292                         {
293                                 case 'i':
294                                 case 'I':
295                                 ext->id_next_call_type = INFO_NTYPE_INTERNATIONAL;
296                                 SCPY(ext->id_next_call, param+1);
297                                 PDEBUG(DEBUG_CONFIG, "id next call: %s INTERNATIONAL\n",param+1);
298                                 break;
299                                 case 'n':
300                                 case 'N':
301                                 ext->id_next_call_type = INFO_NTYPE_NATIONAL;
302                                 SCPY(ext->id_next_call, param+1);
303                                 PDEBUG(DEBUG_CONFIG, "id next call: %s NATIONAL\n",param+1);
304                                 break;
305                                 case 's':
306                                 case 'S':
307                                 ext->id_next_call_type = INFO_NTYPE_SUBSCRIBER;
308                                 SCPY(ext->id_next_call, param+1);
309                                 PDEBUG(DEBUG_CONFIG, "id next call: %s SUBSCRIBER\n",param+1);
310                                 break;
311                                 default:
312                                 ext->id_next_call_type = INFO_NTYPE_UNKNOWN;
313                                 SCPY(ext->id_next_call, param);
314                                 PDEBUG(DEBUG_CONFIG, "id next call: %s UNKNOWN\n",param);
315                         }
316
317
318
319                 } else
320                 if (!strcmp(option,"change_callerid"))
321                 {
322                         i=0;
323                         while(ext_yesno[i])
324                         {
325                                 if (!strcasecmp(param,ext_yesno[i]))
326                                         break;
327                                 i++;
328                         }
329                         if (ext_yesno[i])
330                         {
331                                 ext->change_callerid = i;
332                                 PDEBUG(DEBUG_CONFIG, "allow the change of caller id: %s\n", ext_yesno[i]);
333                         } else
334                         {
335                                 PDEBUG(DEBUG_CONFIG, "unknown parameter for change_callerid: %s\n", param);
336                         }
337                 } else
338                 if (!strcmp(option,"anon-ignore"))
339                 {
340                         i=0;
341                         while(ext_yesno[i])
342                         {
343                                 if (!strcasecmp(param,ext_yesno[i]))
344                                         break;
345                                 i++;
346                         }
347                         if (ext_yesno[i])
348                         {
349                                 ext->anon_ignore = i;
350                                 PDEBUG(DEBUG_CONFIG, "ignore restriction of CLIP & COLP %s\n", ext_yesno[i]);
351                         } else
352                         {
353                                 PDEBUG(DEBUG_CONFIG, "unknown parameter given anon-ignore: %s\n", param);
354                         }
355                 } else
356                 if (!strcmp(option,"clip"))
357                 {
358                         if (!strcasecmp(param, "hide"))
359                                 ext->clip = CLIP_HIDE;
360                         else
361                                 ext->clip = CLIP_ASIS;
362
363                         PDEBUG(DEBUG_CONFIG, "clip: %d\n",ext->clip);
364                 } else
365                 if (!strcmp(option,"colp"))
366                 {
367                         if (!strcasecmp(param, "hide"))
368                                 ext->colp = COLP_HIDE;
369                         else if (!strcasecmp(param, "force"))
370                                 ext->colp = COLP_FORCE;
371                         else
372                                 ext->colp = COLP_ASIS;
373
374                         PDEBUG(DEBUG_CONFIG, "colp: %d\n",ext->colp);
375                 } else
376                 if (!strcmp(option,"clip_prefix"))
377                 {
378                         SCPY(ext->clip_prefix, param);
379
380                         PDEBUG(DEBUG_CONFIG, "clip prefix: %s\n",param);
381                 } else
382                 if (!strcmp(option,"keypad"))
383                 {
384                         i=0;
385                         while(ext_yesno[i])
386                         {
387                                 if (!strcasecmp(param,ext_yesno[i]))
388                                         break;
389                                 i++;
390                         }
391                         if (ext_yesno[i])
392                         {
393                                 ext->keypad = i;
394                                 PDEBUG(DEBUG_CONFIG, "use keypad to do call control %s\n", ext_yesno[i]);
395                         } else
396                         {
397                                 PDEBUG(DEBUG_CONFIG, "unknown parameter given keypad: %s\n", param);
398                         }
399                 } else
400                 if (!strcmp(option,"centrex"))
401                 {
402                         i=0;
403                         while(ext_yesno[i])
404                         {
405                                 if (!strcasecmp(param,ext_yesno[i]))
406                                         break;
407                                 i++;
408                         }
409                         if (ext_yesno[i])
410                         {
411                                 ext->centrex = i;
412                                 PDEBUG(DEBUG_CONFIG, "use centrex to display name %s\n", ext_yesno[i]);
413                         } else
414                         {
415                                 PDEBUG(DEBUG_CONFIG, "unknown parameter given centrex: %s\n", param);
416                         }
417                 } else
418                 if (!strcmp(option,"rights"))
419                 {
420                         i=0;
421                         while(ext_rights[i])
422                         {
423                                 if (!strcasecmp(param,ext_rights[i]))
424                                         break;
425                                 i++;
426                         }
427                         if (ext_rights[i])
428                         {
429                                 ext->rights = i;
430                                 PDEBUG(DEBUG_CONFIG, "rights to dial: %s\n", ext_rights[i]);
431                         } else
432                         {
433                                 PDEBUG(DEBUG_CONFIG, "given rights unknown: %s\n", param);
434                         }
435                 } else
436                 if (!strcmp(option,"delete_ext"))
437                 {
438                         i=0;
439                         while(ext_yesno[i])
440                         {
441                                 if (!strcasecmp(param,ext_yesno[i]))
442                                         break;
443                                 i++;
444                         }
445                         if (ext_yesno[i])
446                         {
447                                 ext->delete_ext = i;
448                                 PDEBUG(DEBUG_CONFIG, "enables the delete key function for external calls: %s\n", ext_yesno[i]);
449                         } else
450                         {
451                                 PDEBUG(DEBUG_CONFIG, "unknown parameter given delete: %s\n", param);
452                         }
453                 } else
454                 if (!strcmp(option,"noknocking"))
455                 {
456                         i=0;
457                         while(ext_yesno[i])
458                         {
459                                 if (!strcasecmp(param,ext_yesno[i]))
460                                         break;
461                                 i++;
462                         }
463                         if (ext_yesno[i])
464                         {
465                                 ext->noknocking = i;
466                                 PDEBUG(DEBUG_CONFIG, "noknocking %s\n", ext_yesno[i]);
467                         } else
468                         {
469                                 PDEBUG(DEBUG_CONFIG, "given noknocking param unknown: %s\n", param);
470                         }
471                 } else
472                 if (!strcmp(option,"rxvol"))
473                 {
474                         ext->rxvol = atoi(param);
475                         if (ext->rxvol<-8 || ext->rxvol>8)
476                                 ext->rxvol = 0;
477
478                         PDEBUG(DEBUG_CONFIG, "receive volume: %d\n",ext->rxvol);
479                 } else
480                 if (!strcmp(option,"txvol"))
481                 {
482                         ext->txvol = atoi(param);
483                         if (ext->txvol<-8 || ext->txvol>8)
484                                 ext->txvol = 0;
485
486                         PDEBUG(DEBUG_CONFIG, "transmit volume: %d\n",ext->txvol);
487                 } else
488                 if (!strcmp(option,"tout_setup"))
489                 {
490                         ext->tout_setup = atoi(param);
491                         if (ext->tout_setup < 0)
492                                 ext->tout_setup = 0;
493
494                         PDEBUG(DEBUG_CONFIG, "timeout setup: %d\n",ext->tout_setup);
495                 } else
496                 if (!strcmp(option,"tout_dialing"))
497                 {
498                         ext->tout_dialing = atoi(param);
499                         if (ext->tout_dialing < 0)
500                                 ext->tout_dialing = 0;
501
502                         PDEBUG(DEBUG_CONFIG, "timeout dialing: %d\n",ext->tout_dialing);
503                 } else
504                 if (!strcmp(option,"tout_proceeding"))
505                 {
506                         ext->tout_proceeding = atoi(param);
507                         if (ext->tout_proceeding < 0)
508                                 ext->tout_proceeding = 0;
509
510                         PDEBUG(DEBUG_CONFIG, "timeout proceeding: %d\n",ext->tout_proceeding);
511                 } else
512                 if (!strcmp(option,"tout_alerting"))
513                 {
514                         ext->tout_alerting = atoi(param);
515                         if (ext->tout_alerting < 0)
516                                 ext->tout_alerting = 0;
517
518                         PDEBUG(DEBUG_CONFIG, "timeout alerting: %d\n",ext->tout_alerting);
519                 } else
520                 if (!strcmp(option,"tout_disconnect"))
521                 {
522                         ext->tout_disconnect = atoi(param);
523                         if (ext->tout_disconnect < 0)
524                                 ext->tout_disconnect = 0;
525
526                         PDEBUG(DEBUG_CONFIG, "timeout disconnect: %d\n",ext->tout_disconnect);
527                 } else
528 #if 0
529                 if (!strcmp(option,"tout_hold"))
530                 {
531                         ext->tout_hold = atoi(param);
532                         if (ext->tout_hold < 0)
533                                 ext->tout_hold = 0;
534
535                         PDEBUG(DEBUG_CONFIG, "timeout hold: %d\n",ext->tout_hold);
536                 } else
537                 if (!strcmp(option,"tout_park"))
538                 {
539                         ext->tout_park = atoi(param);
540                         if (ext->tout_park < 0)
541                                 ext->tout_park = 0;
542
543                         PDEBUG(DEBUG_CONFIG, "timeout park: %d\n",ext->tout_park);
544                 } else
545 #endif
546                 if (!strcmp(option,"own_setup"))
547                 {
548                         i=0;
549                         while(ext_yesno[i])
550                         {
551                                 if (!strcasecmp(param,ext_yesno[i]))
552                                         break;
553                                 i++;
554                         }
555                         if (ext_yesno[i])
556                         {
557                                 ext->own_setup = i;
558                                 PDEBUG(DEBUG_CONFIG, "own_setup %s\n", ext_yesno[i]);
559                         } else
560                         {
561                                 PDEBUG(DEBUG_CONFIG, "given own_setup param unknown: %s\n", param);
562                         }
563                 } else
564                 if (!strcmp(option,"own_proceeding"))
565                 {
566                         i=0;
567                         while(ext_yesno[i])
568                         {
569                                 if (!strcasecmp(param,ext_yesno[i]))
570                                         break;
571                                 i++;
572                         }
573                         if (ext_yesno[i])
574                         {
575                                 ext->own_proceeding = i;
576                                 PDEBUG(DEBUG_CONFIG, "own_proceeding %s\n", ext_yesno[i]);
577                         } else
578                         {
579                                 PDEBUG(DEBUG_CONFIG, "given own_proceeding param unknown: %s\n", param);
580                         }
581                 } else
582                 if (!strcmp(option,"own_alerting"))
583                 {
584                         i=0;
585                         while(ext_yesno[i])
586                         {
587                                 if (!strcasecmp(param,ext_yesno[i]))
588                                         break;
589                                 i++;
590                         }
591                         if (ext_yesno[i])
592                         {
593                                 ext->own_alerting = i;
594                                 PDEBUG(DEBUG_CONFIG, "own_alerting %s\n", ext_yesno[i]);
595                         } else
596                         {
597                                 PDEBUG(DEBUG_CONFIG, "given own_alerting param unknown: %s\n", param);
598                         }
599                 } else
600                 if (!strcmp(option,"own_cause"))
601                 {
602                         i=0;
603                         while(ext_yesno[i])
604                         {
605                                 if (!strcasecmp(param,ext_yesno[i]))
606                                         break;
607                                 i++;
608                         }
609                         if (ext_yesno[i])
610                         {
611                                 ext->own_cause = i;
612                                 PDEBUG(DEBUG_CONFIG, "own_cause %s\n", ext_yesno[i]);
613                         } else
614                         {
615                                 PDEBUG(DEBUG_CONFIG, "given own_cause param unknown: %s\n", param);
616                         }
617                 } else
618                 if (!strcmp(option,"facility"))
619                 {
620                         i=0;
621                         while(ext_yesno[i])
622                         {
623                                 if (!strcasecmp(param,ext_yesno[i]))
624                                         break;
625                                 i++;
626                         }
627                         if (ext_yesno[i])
628                         {
629                                 ext->facility = i;
630                                 PDEBUG(DEBUG_CONFIG, "facility %s\n", ext_yesno[i]);
631                         } else
632                         {
633                                 PDEBUG(DEBUG_CONFIG, "given facility param unknown: %s\n", param);
634                         }
635                 } else
636                 if (!strcmp(option,"display_cause"))
637                 {
638                         if (!strcasecmp(param, "german"))
639                                 ext->display_cause = DISPLAY_CAUSE_GERMAN;
640                         else if (!strcasecmp(param, "english"))
641                                 ext->display_cause = DISPLAY_CAUSE_ENGLISH;
642                         else if (!strcasecmp(param, "german-location"))
643                                 ext->display_cause = DISPLAY_LOCATION_GERMAN;
644                         else if (!strcasecmp(param, "english-location"))
645                                 ext->display_cause = DISPLAY_LOCATION_ENGLISH;
646                         else if (!strcasecmp(param, "number"))
647                                 ext->display_cause = DISPLAY_CAUSE_NUMBER;
648                         else
649                                 ext->display_cause = DISPLAY_CAUSE_NONE;
650
651                         PDEBUG(DEBUG_CONFIG, "display cause: %d\n",ext->display_cause);
652                 } else
653 #if 0
654                 if (!strcmp(option,"display_ext"))
655                 {
656                         if (!strcasecmp(param, "number"))
657                                 ext->display_ext = DISPLAY_CID_NUMBER;
658                         else if (!strcasecmp(param, "abbrev"))
659                                 ext->display_ext = DISPLAY_CID_ABBREVIATION;
660                         else if (!strcasecmp(param, "name"))
661                                 ext->display_ext = DISPLAY_CID_NAME;
662                         else if (!strcasecmp(param, "number-name"))
663                                 ext->display_ext = DISPLAY_CID_NUMBER_NAME;
664                         else if (!strcasecmp(param, "name-number"))
665                                 ext->display_ext = DISPLAY_CID_NAME_NUMBER;
666                         else if (!strcasecmp(param, "abbrev-number"))
667                                 ext->display_ext = DISPLAY_CID_ABBREV_NUMBER;
668                         else if (!strcasecmp(param, "abbrev-name"))
669                                 ext->display_ext = DISPLAY_CID_ABBREV_NAME;
670                         else if (!strcasecmp(param, "abbrev-name-number"))
671                                 ext->display_ext = DISPLAY_CID_ABBREV_NAME_NUMBER;
672                         else if (!strcasecmp(param, "abbrev-number-name"))
673                                 ext->display_ext = DISPLAY_CID_ABBREV_NUMBER_NAME;
674                         else
675                                 ext->display_ext = DISPLAY_CID_ASIS;
676
677                         PDEBUG(DEBUG_CONFIG, "display ext: %d\n",ext->display_ext);
678                 } else
679 #endif
680                 if (!strcmp(option,"display_ext"))
681                 {
682                         i=0;
683                         while(ext_yesno[i])
684                         {
685                                 if (!strcasecmp(param,ext_yesno[i]))
686                                         break;
687                                 i++;
688                         }
689                         if (ext_yesno[i])
690                         {
691                                 ext->display_ext = i;
692                                 PDEBUG(DEBUG_CONFIG, "display ext %s\n", ext_yesno[i]);
693                         } else
694                         {
695                                 PDEBUG(DEBUG_CONFIG, "given display_ext param unknown: %s\n", param);
696                         }
697                 } else
698                 if (!strcmp(option,"display_int"))
699                 {
700                         i=0;
701                         while(ext_yesno[i])
702                         {
703                                 if (!strcasecmp(param,ext_yesno[i]))
704                                         break;
705                                 i++;
706                         }
707                         if (ext_yesno[i])
708                         {
709                                 ext->display_int = i;
710                                 PDEBUG(DEBUG_CONFIG, "display int %s\n", ext_yesno[i]);
711                         } else
712                         {
713                                 PDEBUG(DEBUG_CONFIG, "given display_int param unknown: %s\n", param);
714                         }
715                 } else
716                 if (!strcmp(option,"display_fake"))
717                 {
718                         i=0;
719                         while(ext_yesno[i])
720                         {
721                                 if (!strcasecmp(param,ext_yesno[i]))
722                                         break;
723                                 i++;
724                         }
725                         if (ext_yesno[i])
726                         {
727                                 ext->display_fake = i;
728                                 PDEBUG(DEBUG_CONFIG, "display fake caller ids %s\n", ext_yesno[i]);
729                         } else
730                         {
731                                 PDEBUG(DEBUG_CONFIG, "given display_fake param unknown: %s\n", param);
732                         }
733                 } else
734                 if (!strcmp(option,"display_anon"))
735                 {
736                         i=0;
737                         while(ext_yesno[i])
738                         {
739                                 if (!strcasecmp(param,ext_yesno[i]))
740                                         break;
741                                 i++;
742                         }
743                         if (ext_yesno[i])
744                         {
745                                 ext->display_anon = i;
746                                 PDEBUG(DEBUG_CONFIG, "display anonymouse ids %s\n", ext_yesno[i]);
747                         } else
748                         {
749                                 PDEBUG(DEBUG_CONFIG, "given display_anon param unknown: %s\n", param);
750                         }
751                 } else
752                 if (!strcmp(option,"display_menu"))
753                 {
754                         i=0;
755                         while(ext_yesno[i])
756                         {
757                                 if (!strcasecmp(param,ext_yesno[i]))
758                                         break;
759                                 i++;
760                         }
761                         if (ext_yesno[i])
762                         {
763                                 ext->display_menu = i;
764                                 PDEBUG(DEBUG_CONFIG, "display menu %s\n", ext_yesno[i]);
765                         } else
766                         {
767                                 PDEBUG(DEBUG_CONFIG, "given display_menu param unknown: %s\n", param);
768                         }
769                 } else
770                 if (!strcmp(option,"display_dialing"))
771                 {
772                         i=0;
773                         while(ext_yesno[i])
774                         {
775                                 if (!strcasecmp(param,ext_yesno[i]))
776                                         break;
777                                 i++;
778                         }
779                         if (ext_yesno[i])
780                         {
781                                 ext->display_dialing = i;
782                                 PDEBUG(DEBUG_CONFIG, "display dialing %s\n", ext_yesno[i]);
783                         } else
784                         {
785                                 PDEBUG(DEBUG_CONFIG, "given display_dialing param unknown: %s\n", param);
786                         }
787                 } else
788                 if (!strcmp(option,"display_name"))
789                 {
790                         i=0;
791                         while(ext_yesno[i])
792                         {
793                                 if (!strcasecmp(param,ext_yesno[i]))
794                                         break;
795                                 i++;
796                         }
797                         if (ext_yesno[i])
798                         {
799                                 ext->display_name = i;
800                                 PDEBUG(DEBUG_CONFIG, "display name %s\n", ext_yesno[i]);
801                         } else
802                         {
803                                 PDEBUG(DEBUG_CONFIG, "given display_name param unknown: %s\n", param);
804                         }
805                 } else
806                 if (!strcmp(option,"tones_dir"))
807                 {
808                         if (param[strlen(param)-1] == '/')
809                                 param[strlen(param)-1]=0;
810                         SCPY(ext->tones_dir, param);
811
812                         PDEBUG(DEBUG_CONFIG, "directory of tones: %s\n",param);
813                 } else
814                 if (!strcmp(option,"record"))
815                 {
816                         if (!strcasecmp(param, "mono"))
817                                 ext->record = CODEC_MONO;
818                         else if (!strcasecmp(param, "stereo"))
819                                 ext->record = CODEC_STEREO;
820                         else if (!strcasecmp(param, "8bit"))
821                                 ext->record = CODEC_8BIT;
822                         else if (!strcasecmp(param, "law"))
823                                 ext->record = CODEC_LAW;
824                         else
825                                 ext->record = CODEC_OFF;
826                         PDEBUG(DEBUG_CONFIG, "given record param: %s\n", param);
827                 } else
828                 if (!strcmp(option,"password"))
829                 {
830                         SCPY(ext->password, param);
831
832                         PDEBUG(DEBUG_CONFIG, "password: %s\n",param);
833                 } else
834                 if (!strcmp(option,"vbox_mode"))
835                 {
836                         if (!strcasecmp(param, "parallel"))
837                                 ext->vbox_mode = VBOX_MODE_PARALLEL;
838                         else if (!strcasecmp(param, "announcement"))
839                                 ext->vbox_mode = VBOX_MODE_ANNOUNCEMENT;
840                         else
841                                 ext->vbox_mode = VBOX_MODE_NORMAL;
842                         PDEBUG(DEBUG_CONFIG, "given vbox mode: %s\n", param);
843                 } else
844                 if (!strcmp(option,"vbox_codec"))
845                 {
846                         if (!strcasecmp(param, "stereo"))
847                                 ext->vbox_codec = CODEC_STEREO;
848                         else if (!strcasecmp(param, "8bit"))
849                                 ext->vbox_codec = CODEC_8BIT;
850                         else if (!strcasecmp(param, "law"))
851                                 ext->vbox_codec = CODEC_LAW;
852                         else
853                                 ext->vbox_codec = CODEC_MONO;
854                         PDEBUG(DEBUG_CONFIG, "given record param: %s\n", param);
855                 } else
856                 if (!strcmp(option,"vbox_time"))
857                 {
858                         ext->vbox_time = atoi(param);
859                         if (ext->vbox_time < 0)
860                                 ext->vbox_time = 0;
861
862                         PDEBUG(DEBUG_CONFIG, "vbox time to record: %d\n",ext->vbox_time);
863                 } else
864                 if (!strcmp(option,"vbox_display"))
865                 {
866                         if (!strcasecmp(param, "detailed")
867                          || !strcasecmp(param, "detailled"))
868                                 ext->vbox_display = VBOX_DISPLAY_DETAILED;
869                         else if (!strcasecmp(param, "off"))
870                                 ext->vbox_display = VBOX_DISPLAY_OFF;
871                         else
872                                 ext->vbox_display = VBOX_DISPLAY_BRIEF;
873                         PDEBUG(DEBUG_CONFIG, "given vbox mode: %s\n", param);
874                 } else
875                 if (!strcmp(option,"vbox_language"))
876                 {
877                         if (!strcasecmp(param, "german"))
878                                 ext->vbox_language = VBOX_LANGUAGE_GERMAN;
879                         else
880                                 ext->vbox_language = VBOX_LANGUAGE_ENGLISH;
881                         PDEBUG(DEBUG_CONFIG, "given vbox mode: %s\n", param);
882                 } else
883                 if (!strcmp(option,"vbox_email"))
884                 {
885                         SCPY(ext->vbox_email, param);
886                         PDEBUG(DEBUG_CONFIG, "given vbox email: %s\n", param);
887                 } else
888                 if (!strcmp(option,"vbox_email_file"))
889                 {
890                         i=0;
891                         while(ext_yesno[i])
892                         {
893                                 if (!strcasecmp(param,ext_yesno[i]))
894                                         break;
895                                 i++;
896                         }
897                         if (ext_yesno[i])
898                         {
899                                 ext->vbox_email_file = i;
900                                 PDEBUG(DEBUG_CONFIG, "attach audio file %s\n", ext_yesno[i]);
901                         } else
902                         {
903                                 PDEBUG(DEBUG_CONFIG, "given vbox_email_file param unknown: %s\n", param);
904                         }
905                 } else
906                 if (!strcmp(option,"vbox_free"))
907                 {
908                         i=0;
909                         while(ext_yesno[i])
910                         {
911                                 if (!strcasecmp(param,ext_yesno[i]))
912                                         break;
913                                 i++;
914                         }
915                         if (ext_yesno[i])
916                         {
917                                 ext->vbox_free = i;
918                                 PDEBUG(DEBUG_CONFIG, "vbox_free %s\n", ext_yesno[i]);
919                         } else
920                         {
921                                 PDEBUG(DEBUG_CONFIG, "given vbox_free param unknown: %s\n", param);
922                         }
923                 } else
924                 if (!strcmp(option,"last_in"))
925                 {
926                         if (param[0] && last_in_count<MAX_REMEMBER)
927                         {
928                                 SCPY(ext->last_in[last_in_count], param);
929                                 last_in_count++;
930                         }
931                         PDEBUG(DEBUG_CONFIG, "last_in dialed number: %s\n",param);
932                 } else
933                 if (!strcmp(option,"last_out"))
934                 {
935                         if (param[0] && last_out_count<MAX_REMEMBER)
936                         {
937                                 SCPY(ext->last_out[last_out_count], param);
938                                 last_out_count++;
939                         }
940                         PDEBUG(DEBUG_CONFIG, "last_out dialed number: %s\n",param);
941                 } else
942                 if (!strcmp(option,"datacall"))
943                 {
944                         i=0;
945                         while(ext_yesno[i])
946                         {
947                                 if (!strcasecmp(param,ext_yesno[i]))
948                                         break;
949                                 i++;
950                         }
951                         if (ext_yesno[i])
952                         {
953                                 ext->datacall = i;
954                                 PDEBUG(DEBUG_CONFIG, "datacall %s\n", ext_yesno[i]);
955                         } else
956                         {
957                                 PDEBUG(DEBUG_CONFIG, "given datacall param unknown: %s\n", param);
958                         }
959                 } else
960                 if (!strcmp(option,"seconds"))
961                 {
962                         i=0;
963                         while(ext_yesno[i])
964                         {
965                                 if (!strcasecmp(param,ext_yesno[i]))
966                                         break;
967                                 i++;
968                         }
969                         if (ext_yesno[i])
970                         {
971                                 ext->no_seconds = 1-i;
972                                 PDEBUG(DEBUG_CONFIG, "seconds %s\n", ext_yesno[i]);
973                         } else
974                         {
975                                 PDEBUG(DEBUG_CONFIG, "unknown param for seconds: %s\n", param);
976                         }
977                 } else
978                 {
979                         PERROR_RUNTIME("Error in %s (line %d): wrong option keyword %s.\n",filename,line,option);
980                 }
981         }
982
983         if (fp) fclose(fp);
984         return(1);
985 }
986
987
988 /* write extension
989  *
990  * writes extension for given extension number from structure
991  */
992 int write_extension(struct extension *ext, char *number)
993 {
994         FILE *fp=NULL;
995         char filename[256];
996         int i;
997
998         if (number[0] == '\0')
999                 return(0);
1000
1001         SPRINT(filename, "%s/%s/%s/settings", INSTALL_DATA, options.extensions_dir, number);
1002
1003         if (!(fp = fopen(filename, "w")))
1004         {
1005                 PERROR("Cannot open settings: \"%s\"\n", filename);
1006                 return(0);
1007         }
1008
1009         fprintf(fp,"# Settings of extension %s\n\n", number);
1010
1011         fprintf(fp,"# Name of extension:\n");
1012         fprintf(fp,"name            %s\n\n",ext->name);
1013
1014         fprintf(fp,"# Predialed prefix after pick-up of the phone\n");
1015         fprintf(fp,"prefix          %s\n\n",ext->prefix);
1016
1017         fprintf(fp,"# Next prefix to dial pick-up of the phone\n");
1018         fprintf(fp,"# This will be cleared on hangup.\n");
1019         fprintf(fp,"next            %s\n\n",ext->next);
1020
1021 //      fprintf(fp,"# Set up alarm message after prefix is dialed and connection is established\n");
1022 //      fprintf(fp,"alarm           %s\n\n",ext->alarm);
1023
1024         fprintf(fp,"# Ports to ring on calls to extension (starting from 1)\n");
1025         fprintf(fp,"# Seperate ports by using komma. (example: 1,3 would ring incoming calls on\n# port 1 and 3)\n");
1026         fprintf(fp,"interfaces      %s\n\n",ext->interfaces);
1027
1028         fprintf(fp,"# Call Forward Unconditional (CFU)\n");
1029         fprintf(fp,"# No port will be called, CFB, CFNR and CFP is ignored.\n");
1030         fprintf(fp,"# Use keyword \"vbox\" to forward call directly to answering machine.\n");
1031         fprintf(fp,"cfu             %s\n\n",ext->cfu);
1032
1033         fprintf(fp,"# Call Forward when Busy (CFB)\n");
1034         fprintf(fp,"# If the extension is in use at least once, this forward is done.\n");
1035         fprintf(fp,"# In case of busy line, CFNR and CFP is ignored.\n");
1036         fprintf(fp,"# Use keyword \"vbox\" to forward call to answering machine when busy.\n");
1037         fprintf(fp,"cfb             %s\n\n",ext->cfb);
1038
1039         fprintf(fp,"# Call Forward on No Response (CFNR)\n");
1040         fprintf(fp,"# If noone answers, the call is forwarded, ports and CFP will be released.\n");
1041         fprintf(fp,"# The default delay is 20 seconds.\n");
1042         fprintf(fp,"# Use keyword \"vbox\" to forward call to answering machine on no response.\n");
1043         fprintf(fp,"cfnr            %s\n",ext->cfnr);
1044         fprintf(fp,"cfnr_delay      %d\n\n",ext->cfnr_delay);
1045
1046         fprintf(fp,"# Call Forward Parallel (CFP)\n");
1047         fprintf(fp,"# Call will ring on the forwarded number, simulaniousely with the ports.\n");
1048         fprintf(fp,"cfp             %s\n\n",ext->cfp);
1049
1050         fprintf(fp,"# Allow user to change call forwarding.\n");
1051         fprintf(fp,"change_forward  %s\n\n", ext_yesno[ext->change_forward]);
1052
1053         fprintf(fp,"# Caller id\n# This must be one of the following:\n");
1054         fprintf(fp,"# <number> (as dialed from your local area)\n");
1055         fprintf(fp,"# <number> anonymous (will only be shown to emergency phones)\n");
1056         fprintf(fp,"# none (no number available at all)\n");
1057         fprintf(fp,"# by default the number is of type UNKNOWN (for MULTIPOINT lines)\n");
1058         fprintf(fp,"# if your caller id is not screened on outgoing calls use one of the following:\n");
1059         fprintf(fp,"# use prefix 'i' for TYPE INTERNATIONAL (i<county code><areacode+number>)\n");
1060         fprintf(fp,"# use prefix 'n' for TYPE NATIONAL (n<areacode+number>)\n");
1061         fprintf(fp,"# use prefix 's' for TYPE SUBSCRIBER (s<local number>)\n");
1062         if (ext->callerid_present == INFO_PRESENT_NOTAVAIL)
1063                 fprintf(fp,"callerid        none\n\n");
1064         else
1065         {
1066                 switch(ext->callerid_type)
1067                 {
1068                         case INFO_NTYPE_INTERNATIONAL:
1069                         fprintf(fp,"callerid        i%s%s\n\n",ext->callerid, (ext->callerid_present==INFO_PRESENT_RESTRICTED)?" anonymous":"");
1070                         break;
1071                         case INFO_NTYPE_NATIONAL:
1072                         fprintf(fp,"callerid        n%s%s\n\n",ext->callerid, (ext->callerid_present==INFO_PRESENT_RESTRICTED)?" anonymous":"");
1073                         break;
1074                         case INFO_NTYPE_SUBSCRIBER:
1075                         fprintf(fp,"callerid        s%s%s\n\n",ext->callerid, (ext->callerid_present==INFO_PRESENT_RESTRICTED)?" anonymous":"");
1076                         break;
1077                         default:
1078                         fprintf(fp,"callerid        %s%s\n\n",ext->callerid, (ext->callerid_present==INFO_PRESENT_RESTRICTED)?" anonymous":"");
1079                 }
1080         }
1081
1082         fprintf(fp,"# Caller id for next call (see caller id)\n");
1083         if (ext->id_next_call_present < 0)
1084                 fprintf(fp,"id_next_call    \n\n");
1085         else if (ext->id_next_call_present == INFO_PRESENT_NOTAVAIL)
1086                 fprintf(fp,"id_next_call    none\n\n");
1087         else
1088         {
1089                 switch(ext->id_next_call_type)
1090                 {
1091                         case INFO_NTYPE_INTERNATIONAL:
1092                         fprintf(fp,"id_next_call    i%s%s\n\n",ext->id_next_call, (ext->id_next_call_present==INFO_PRESENT_RESTRICTED)?" anonymous":"");
1093                         break;
1094                         case INFO_NTYPE_NATIONAL:
1095                         fprintf(fp,"id_next_call    n%s%s\n\n",ext->id_next_call, (ext->id_next_call_present==INFO_PRESENT_RESTRICTED)?" anonymous":"");
1096                         break;
1097                         case INFO_NTYPE_SUBSCRIBER:
1098                         fprintf(fp,"id_next_call    s%s%s\n\n",ext->id_next_call, (ext->id_next_call_present==INFO_PRESENT_RESTRICTED)?" anonymous":"");
1099                         break;
1100                         default:
1101                         fprintf(fp,"id_next_call    %s%s\n\n",ext->id_next_call, (ext->id_next_call_present==INFO_PRESENT_RESTRICTED)?" anonymous":"");
1102                 }
1103         }
1104
1105         fprintf(fp,"# Allow user to change caller ID.\n");
1106         fprintf(fp,"change_callerid %s\n\n", ext_yesno[ext->change_callerid]);
1107
1108         fprintf(fp,"# Caller Line Identification Presentation (CLIP)\n");
1109         fprintf(fp,"# clip (asis|hide)\n");
1110         fprintf(fp,"# asis: On forwarded calls the CLIP is used as presented by the calling party.\n");
1111         fprintf(fp,"# hide: Always use extension's caller id, even on forwared calls.\n");
1112         switch(ext->clip)
1113         {
1114                 case CLIP_HIDE:
1115                 fprintf(fp,"clip            hide\n\n");
1116                 break;
1117                 default:
1118                 fprintf(fp,"clip            asis\n\n");
1119         }
1120
1121         fprintf(fp,"# Connected Line Identification Presentation (COLP)\n");
1122         fprintf(fp,"# colp (asis|hide|force)\n");
1123         fprintf(fp,"# asis: Provides colp as defined by the extension's caller id.\n");
1124         fprintf(fp,"#       On forwarded calls the COLP is used as presented by the called party.\n");
1125         fprintf(fp,"# hide: Always use extension's caller id, even on forwared calls.\n");
1126         fprintf(fp,"# force: If COLP is not presented by forwarded calls the dialed number is used.\n");
1127         switch(ext->colp)
1128         {
1129                 case COLP_HIDE:
1130                 fprintf(fp,"colp            hide\n\n");
1131                 break;
1132                 case COLP_FORCE:
1133                 fprintf(fp,"colp            force\n\n");
1134                 break;
1135                 default:
1136                 fprintf(fp,"colp            asis\n\n");
1137         }
1138
1139         fprintf(fp,"# CLIP Prefix\n");
1140         fprintf(fp,"# Adds a prefix to incomming caller IDs, so telephones will be able to respond\n");
1141         fprintf(fp,"# to unanswered calls from their list. The prefix must be the digit(s) to get\n");
1142         fprintf(fp,"# an external line. The caller ID will then be extendet so that they can be\n");
1143         fprintf(fp,"# dialed from internal telephones. Many telephones have this feature, but some\n");
1144         fprintf(fp,"# don't.\n");
1145         fprintf(fp,"clip_prefix     %s\n\n",ext->clip_prefix);
1146
1147         fprintf(fp,"# Keypad control\n");
1148         fprintf(fp,"# If supported by telephone, pressing a key on the keypad will not result in\n");
1149         fprintf(fp,"# DTMF tone, but the digit is transmitted via D-channel diaing info.\n");
1150         fprintf(fp,"keypad          %s\n\n",(ext->keypad)?"yes":"no");
1151
1152         fprintf(fp,"# Called Name Identification Presentation (CNIP/CONP)\n");
1153         fprintf(fp,"# If supported by telephone, special information element on the d-channel are\n");
1154         fprintf(fp,"# used to show name of caller. It is supported by newer Siemens telephones\n# (Centrex).\n");
1155         fprintf(fp,"centrex         %s  #this is currently not working!!!\n\n",(ext->centrex)?"yes":"no");
1156
1157         fprintf(fp,"# Ignore restriction of COLP and CLIP\n");
1158         fprintf(fp,"# In this case even restricted numbers are presented to this extension.\n");
1159         fprintf(fp,"# This also works for incoming external anonymous calls IF:\n");
1160         fprintf(fp,"# You have the CLIRIGN feature like POLICE or equivalent.\n");
1161         fprintf(fp,"anon-ignore     %s\n\n",(ext->anon_ignore)?"yes":"no");
1162
1163         fprintf(fp,"# Dialing rights (none|internal|local|national|international)\n");
1164         fprintf(fp,"rights          %s\n\n",ext_rights[ext->rights]);
1165
1166         fprintf(fp,"# Delete function for external calls. '*' will delete the last digit, '#' will\n");
1167         fprintf(fp,"# delete the complete number. Also enable 'display_dialing' to see on the\n");
1168         fprintf(fp,"# display what actually happens.\n");
1169         fprintf(fp,"delete_ext      %s\n\n",ext_yesno[ext->delete_ext]);
1170
1171         fprintf(fp,"# If noknocking is enabled, the caller will get a busy message when the\n");
1172         fprintf(fp,"# extension is doing at least one call.\n");
1173         fprintf(fp,"noknocking      %s\n\n",ext_yesno[ext->noknocking]);
1174
1175         fprintf(fp,"# Transmit volume (-8 .. 8)\n");
1176         fprintf(fp,"# 0 = normal\n");
1177         fprintf(fp,"# 1 = double, 2 = quadrupel, 8 = 256 times (amplitude)\n");
1178         fprintf(fp,"# -1 = half, -2 = quarter, 8 = 1/256th (amplitude)\n");
1179         fprintf(fp,"# Audio data is limited to the maximum value when exceeds limit.\n");
1180         fprintf(fp,"txvol           %d\n\n",ext->txvol);
1181
1182         fprintf(fp,"# Receive volume (-8 .. 8)\n");
1183         fprintf(fp,"# (see txvol)\n");
1184         fprintf(fp,"rxvol           %d\n\n",ext->rxvol);
1185
1186         fprintf(fp,"# Timeout values\n# The keywords specify the following timeouts:\n");
1187         fprintf(fp,"# tout_setup: after pickup before dialing anything. (default 60 seconds)\n");
1188         fprintf(fp,"# tout_dialing: after dialing last digit of uncomplete number (default 15)\n");
1189         fprintf(fp,"# tout_proceeding: after start proceeding (default 120)\n");
1190         fprintf(fp,"# tout_alerting: after start ringing (default 120)\n");
1191         fprintf(fp,"# tout_disconnect: after disconnect (default 120)\n");
1192 //      fprintf(fp,"# tout_hold: maximum time to hold a call (default 900)\n");
1193 //      fprintf(fp,"# tout_park: maximum time to park a call (default 900)\n");
1194         fprintf(fp,"# All timeouts may be disabled by using keyword 'off' instead of seconds.\n");
1195         fprintf(fp,"# All timeouts refer to internal ports only. External timeouts are controlled\n");
1196         fprintf(fp,"# by external line.\n");
1197         if (ext->tout_setup)
1198                 fprintf(fp,"tout_setup      %d\n",ext->tout_setup);
1199         else
1200                 fprintf(fp,"tout_setup      off\n");
1201         if (ext->tout_dialing)
1202                 fprintf(fp,"tout_dialing    %d\n",ext->tout_dialing);
1203         else
1204                 fprintf(fp,"tout_dialing    off\n");
1205         if (ext->tout_proceeding)
1206                 fprintf(fp,"tout_proceeding %d\n",ext->tout_proceeding);
1207         else
1208                 fprintf(fp,"tout_proceeding off\n");
1209         if (ext->tout_alerting)
1210                 fprintf(fp,"tout_alerting   %d\n",ext->tout_alerting);
1211         else
1212                 fprintf(fp,"tout_alerting   off\n");
1213         if (ext->tout_disconnect)
1214                 fprintf(fp,"tout_disconnect %d\n\n",ext->tout_disconnect);
1215         else
1216                 fprintf(fp,"tout_disconnect off\n\n");
1217 //      if (ext->tout_hold)
1218 //              fprintf(fp,"tout_hold       %d\n",ext->tout_hold);
1219 //      else
1220 //              fprintf(fp,"tout_hold       off\n");
1221 //      if (ext->tout_park)
1222 //              fprintf(fp,"tout_park       %d\n\n",ext->tout_park);
1223 //      else
1224 //              fprintf(fp,"tout_park       off\n\n");
1225
1226         fprintf(fp,"# Force to use tones and announcements generated by the pbx.\n");
1227         fprintf(fp,"# For internal calls always own tones are used. You may specify own tones for\n");
1228         fprintf(fp,"# different call states:\n");
1229         fprintf(fp,"# own_setup (dialtone and during dialing)\n");
1230         fprintf(fp,"# own_proceeding (call in poceeding state)\n");
1231         fprintf(fp,"# own_alerting (call is ringing)\n");
1232         fprintf(fp,"# own_cause (when the call gets disconnected or failed to be completed)\n");
1233         fprintf(fp,"own_setup       %s\n",ext_yesno[ext->own_setup]);
1234         fprintf(fp,"own_proceeding  %s\n",ext_yesno[ext->own_proceeding]);
1235         fprintf(fp,"own_alerting    %s\n",ext_yesno[ext->own_alerting]);
1236         fprintf(fp,"own_cause       %s\n\n",ext_yesno[ext->own_cause]);
1237
1238         fprintf(fp,"# Allow facility information to be transfered to the telephone.\n");
1239         fprintf(fp,"# This is equired to receive advice of charge.\n");
1240         fprintf(fp,"facility        %s\n\n",ext_yesno[ext->facility]);
1241
1242         fprintf(fp,"# Display clear causes using display messages (Q.850)\n# This must be one of the following:\n");
1243         fprintf(fp,"# none (no displaying of clear causes)\n");
1244         fprintf(fp,"# english (display cause text in english)\n");
1245         fprintf(fp,"# german (display cause text in german)\n");
1246         fprintf(fp,"# number (display cause number only)\n");
1247         fprintf(fp,"# english-location (display cause text in english and location)\n");
1248         fprintf(fp,"# german-location (display cause text in german and location)\n");
1249         switch(ext->display_cause)
1250         {
1251                 case DISPLAY_CAUSE_ENGLISH:
1252                 fprintf(fp,"display_cause   english\n\n");
1253                 break;
1254                 case DISPLAY_CAUSE_GERMAN:
1255                 fprintf(fp,"display_cause   german\n\n");
1256                 break;
1257                 case DISPLAY_LOCATION_ENGLISH:
1258                 fprintf(fp,"display_cause   english-location\n\n");
1259                 break;
1260                 case DISPLAY_LOCATION_GERMAN:
1261                 fprintf(fp,"display_cause   german-location\n\n");
1262                 break;
1263                 case DISPLAY_CAUSE_NUMBER:
1264                 fprintf(fp,"display_cause   number\n\n");
1265                 break;
1266                 default:
1267                 fprintf(fp,"display_cause   none\n\n");
1268         }
1269
1270         fprintf(fp,"# Display external caller ids using display override (yes or no)\n");
1271         fprintf(fp,"# example: \"15551212\"\n");
1272         fprintf(fp,"display_ext     %s\n\n",(ext->display_ext)?"yes":"no");
1273
1274         fprintf(fp,"# Display internal caller ids using display override (yes or no)\n");
1275         fprintf(fp,"# example: \"200 (int)\"\n");
1276         fprintf(fp,"display_int     %s\n\n",(ext->display_int)?"yes":"no");
1277
1278         fprintf(fp,"# Display if calls are anonymous using display override (yes or no)\n");
1279         fprintf(fp,"# This makes only sense if the anon-ignore feature is enabled.\n");
1280         fprintf(fp,"# example: \"15551212 anon\"\n");
1281         fprintf(fp,"display_anon    %s\n\n",(ext->display_anon)?"yes":"no");
1282
1283         fprintf(fp,"# Display fake caller ids using display override (yes or no)\n");
1284         fprintf(fp,"# If the caller uses \"clip no screening\", you will see if the number is\n");
1285         fprintf(fp,"# real or fake\n");
1286         fprintf(fp,"# example: \"15551212 fake\"\n");
1287         fprintf(fp,"display_fake    %s\n\n",(ext->display_fake)?"yes":"no");
1288
1289         fprintf(fp,"# Display caller's name if available. (yes or no)\n");
1290         fprintf(fp,"# example: \"15551212 Axel\"\n");
1291         fprintf(fp,"display_name    %s\n\n",(ext->display_name)?"yes":"no");
1292
1293         fprintf(fp,"# Display menu when '*' and '#' is pressed. The menu shows all prefixes for\n");
1294         fprintf(fp,"# internal dialing by pressing '*' for previous prefix and '#' for next prefix.\n");
1295         fprintf(fp,"# Also the dialed prefix is show on display. (yes or no)\n");
1296         fprintf(fp,"display_menu    %s\n\n",(ext->display_menu)?"yes":"no");
1297
1298         fprintf(fp,"# Display digits as they are interpreted by pbx. (yes or no)\n");
1299         fprintf(fp,"display_dialing %s\n\n",(ext->display_dialing)?"yes":"no");
1300
1301         fprintf(fp,"# Tones directory for announcements and patterns\n");
1302         fprintf(fp,"# Enter nothing for default tones as selected by options.conf.\n");
1303         fprintf(fp,"tones_dir       %s\n\n",ext->tones_dir);
1304
1305         fprintf(fp,"# Record calls to extension's directory. The file is written as wave.\n");
1306         fprintf(fp,"# This must be one of the following:\n");
1307         fprintf(fp,"# off (no recording)\n");
1308         fprintf(fp,"# mono (records wave 16 bit mono, 128kbits/s)\n");
1309         fprintf(fp,"# stereo (records wave 32 bit stereo, 256kbits/s)\n");
1310         fprintf(fp,"# 8bit (records wave 8 bit mono, 64kbits/s)\n");
1311         fprintf(fp,"# law (records xLaw encoded, as specified in options.conf, 64kbps/s)\n");
1312         switch(ext->record)
1313         {
1314                 case CODEC_MONO:
1315                 fprintf(fp,"record          mono\n\n");
1316                 break;
1317                 case CODEC_STEREO:
1318                 fprintf(fp,"record          stereo\n\n");
1319                 break;
1320                 case CODEC_8BIT:
1321                 fprintf(fp,"record          8bit\n\n");
1322                 break;
1323                 case CODEC_LAW:
1324                 fprintf(fp,"record          law\n\n");
1325                 break;
1326                 default:
1327                 fprintf(fp,"record          off\n\n");
1328         }
1329
1330         fprintf(fp,"# Password for callback and login\n");
1331         fprintf(fp,"# Enter nothing if callback and login should not be possible.\n");
1332         fprintf(fp,"password        %s\n\n",ext->password);
1333
1334         fprintf(fp,"# The Answering Machine. Enter the mode of answering machine.\n");
1335         fprintf(fp,"# This must be one of the following:\n");
1336         fprintf(fp,"# normal (plays announcement and records after that)\n");
1337         fprintf(fp,"# parallel (plays announcement and records also DURING announcement.)\n");
1338         fprintf(fp,"# announcement (just plays announcement and hangs up)\n");
1339         switch(ext->vbox_mode)
1340         {
1341                 case VBOX_MODE_PARALLEL:
1342                 fprintf(fp,"vbox_mode       parallel\n\n");
1343                 break;
1344                 case VBOX_MODE_ANNOUNCEMENT:
1345                 fprintf(fp,"vbox_mode       announcement\n\n");
1346                 break;
1347                 default:
1348                 fprintf(fp,"vbox_mode       normal\n\n");
1349         }
1350
1351         fprintf(fp,"# The Answering Machine. Enter the type of codec for recording.\n");
1352         fprintf(fp,"# This must be one of the following:\n");
1353         fprintf(fp,"# law (alaw/ulas codec, as specified in options.conf)\n");
1354         fprintf(fp,"# mono (16 bit mono wave file)\n");
1355         fprintf(fp,"# stereo (16 bit stereo wave file)\n");
1356         fprintf(fp,"# 8bit (8 bit mono wave file)\n");
1357         switch(ext->vbox_codec)
1358         {
1359                 case CODEC_LAW:
1360                 fprintf(fp,"vbox_codec      law\n\n");
1361                 break;
1362                 case CODEC_STEREO:
1363                 fprintf(fp,"vbox_codec      stereo\n\n");
1364                 break;
1365                 case CODEC_8BIT:
1366                 fprintf(fp,"vbox_codec      8bit\n\n");
1367                 break;
1368                 default:
1369                 fprintf(fp,"vbox_codec      mono\n\n");
1370         }
1371
1372         fprintf(fp,"# The Answering Machine. Enter maximum time to record after announcement.\n");
1373         fprintf(fp,"# Leave empty, enter \"infinite\" or give time in seconds.\n");
1374         fprintf(fp,"# Enter nothing if callback and login should not be possible.\n");
1375         if (ext->vbox_time)
1376                 fprintf(fp,"vbox_time       %d\n\n",ext->vbox_time);
1377         else
1378                 fprintf(fp,"vbox_time       infinite\n\n");
1379
1380         fprintf(fp,"# The Answering Machine. Enter mode for display current state.\n");
1381         fprintf(fp,"# This must be one of the following:\n");
1382         fprintf(fp,"# brief (displays brief information, for small displays)\n");
1383         fprintf(fp,"# detailed (displays detailed information, for larger displays)\n");
1384         fprintf(fp,"# off (don't display anything)\n");
1385         switch(ext->vbox_display)
1386         {
1387                 case VBOX_DISPLAY_OFF:
1388                 fprintf(fp,"vbox_display    off\n\n");
1389                 break;
1390                 case VBOX_DISPLAY_DETAILED:
1391                 fprintf(fp,"vbox_display    detailed\n\n");
1392                 break;
1393                 default:
1394                 fprintf(fp,"vbox_display    brief\n\n");
1395         }
1396
1397         fprintf(fp,"# The Answering Machine. Enter type of language: \"english\" or \"german\"\n");
1398         fprintf(fp,"# Display information of the menu, will be provided as specified.\n");
1399         fprintf(fp,"# The menu's voice is located in \"vbox_english\" and \"vbox_german\".\n");
1400         if (ext->vbox_language)
1401                 fprintf(fp,"vbox_language   german\n\n");
1402         else
1403                 fprintf(fp,"vbox_language   english\n\n");
1404
1405         fprintf(fp,"# The Answering Machine. Enter email to send incoming messages to:\n");
1406         fprintf(fp,"# All incoming message will be send to the given address.\n");
1407         fprintf(fp,"# The audio file is attached if \"vbox_email_file\" is 'yes'\n");
1408         fprintf(fp,"vbox_email      %s\n", ext->vbox_email);
1409         fprintf(fp,"vbox_email_file %s\n\n",ext_yesno[ext->vbox_email_file]);
1410
1411         fprintf(fp,"# If audio path is connected prior answering of a call, say 'yes'\n");
1412         fprintf(fp,"# will cause the call to be billed after playing the announcement. (yes or no)\n");
1413         fprintf(fp,"vbox_free       %s\n\n",(ext->vbox_free)?"yes":"no");
1414
1415         fprintf(fp,"# Accept incoming data calls as it would be an audio call.\n");
1416         fprintf(fp,"datacall        %s\n\n",ext_yesno[ext->datacall]);
1417
1418         fprintf(fp,"# Include seconds (time) in the connect message. (Should be always enabled.)\n");
1419         fprintf(fp,"seconds         %s\n\n",ext_yesno[1-ext->no_seconds]);
1420
1421         fprintf(fp,"# Last outgoing and incoming numbers (including prefix)\n");
1422         i = 0;
1423         while(i < MAX_REMEMBER)
1424         {
1425                 if (ext->last_out[i][0])
1426                         fprintf(fp,"last_out        %s\n",ext->last_out[i]);
1427                 i++;
1428         }
1429         i = 0;
1430         while(i < MAX_REMEMBER)
1431         {
1432                 if (ext->last_in[i][0])
1433                         fprintf(fp,"last_in         %s\n",ext->last_in[i]);
1434                 i++;
1435         }
1436         fprintf(fp,"\n");
1437
1438
1439         if (fp) fclose(fp);
1440         return(1);
1441 }
1442
1443
1444 /* write log for extension
1445  *
1446  */
1447 int write_log(char *number, char *callerid, char *calledid, time_t start, time_t stop, int aoce, int cause, int location)
1448 {
1449         char *mon[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
1450         FILE *fp=NULL;
1451         char filename[256];
1452         struct tm *tm;
1453
1454         if (callerid[0] == '\0')
1455                 callerid = "<unknown>";
1456
1457         SPRINT(filename, "%s/%s/%s/log", INSTALL_DATA, options.extensions_dir, number);
1458
1459         if (!(fp = fopen(filename, "a")))
1460         {
1461                 PERROR("Cannot open log: \"%s\"\n", filename);
1462                 return(0);
1463         }
1464
1465         tm = localtime(&start);
1466         fprintf(fp,"%s %2d %04d %02d:%02d:%02d %s", mon[tm->tm_mon], tm->tm_mday, tm->tm_year+1900, tm->tm_hour, tm->tm_min, tm->tm_sec, number);
1467         if (stop)
1468                 fprintf(fp," %2ld:%02ld:%02ld", (stop-start)/3600, (((unsigned long)(stop-start))/60)%60, ((unsigned long)(stop-start))%60);
1469         else
1470                 fprintf(fp," --:--:--");
1471         fprintf(fp," %s -> %s", callerid, calledid);
1472         if (cause >= 1 && cause <=127 && location>=0 && location<=15)
1473                 fprintf(fp," (cause=%d '%s' location=%d '%s')", cause, isdn_cause[cause].german, location, isdn_location[location].german);
1474         fprintf(fp,"\n");
1475
1476         if (fp) fclose(fp);
1477         return(1);
1478 }
1479
1480
1481 /* parse phonebook
1482  *
1483  * reads phone book of extextension and compares the given elements which
1484  * are: abreviation, phone number, name (name is not compared)
1485  * on success a 1 is returned and the pointers of elements are set to the
1486  * result.
1487  */
1488 int parse_phonebook(char *number, char **abbrev_pointer, char **phone_pointer, char **name_pointer)
1489 {
1490         FILE *fp=NULL;
1491         char filename[256];
1492         char *p;
1493         static char abbrev[32], phone[256], name[256];
1494         unsigned int line,i;
1495         char buffer[1024];
1496         int found = 0, found_if_more_digits = 0;
1497
1498         SPRINT(filename, "%s/%s/%s/phonebook", INSTALL_DATA, options.extensions_dir, number);
1499
1500         if (!(fp = fopen(filename, "r")))
1501         {
1502                 PERROR("Cannot open phonebook: \"%s\"\n", filename);
1503                 return(0);
1504         }
1505
1506         line=0;
1507         while((fgets(buffer, sizeof(buffer), fp)))
1508         {
1509                 line++;
1510                 buffer[sizeof(buffer)-1] = '\0';
1511                 if (buffer[0]) buffer[strlen(buffer)-1] = '\0';
1512                 p = buffer;
1513
1514                 while(*p <= 32) /* skip spaces */
1515                 {
1516                         if (*p == 0)
1517                                 break;
1518                         p++;
1519                 }
1520                 if (*p==0 || *p=='#') /* ignore comments and empty line */
1521                         continue;
1522
1523                 abbrev[0]=0;
1524                 phone[0]=0;
1525                 name[0]=0;
1526
1527                 i=0; /* read abbrev */
1528                 while(*p > 32)
1529                 {
1530                         if (i+1 >= sizeof(abbrev))
1531                         {
1532                                 PERROR_RUNTIME("Error in %s (line %d): abbrev too long.\n",filename,line);
1533                                 break;
1534                         }
1535                         abbrev[i+1] = '\0';
1536                         abbrev[i++] = *p++;
1537                 }
1538
1539                 while(*p <= 32) /* skip spaces */
1540                 {
1541                         if (*p == 0)
1542                                 break;
1543                         p++;
1544                 }
1545
1546                 if (*p!=0 && *p!='#') /* phone */
1547                 {
1548                         i=0; /* read phone */
1549                         while(*p > 32)
1550                         {
1551                                 if (i+1 >= sizeof(phone))
1552                                 {
1553                                         PERROR_RUNTIME("Error in %s (line %d): phone too long.\n",filename,line);
1554                                         break;
1555                                 }
1556                                 phone[i+1] = '\0';
1557                                 phone[i++] = *p++;
1558                         }
1559                         while(*p <= 32) /* skip spaces */
1560                         {
1561                                 if (*p == 0)
1562                                         break;
1563                                 p++;
1564                         }
1565                 }
1566
1567                 if (*p!=0 && *p!='#') /* name */
1568                 {
1569                         i=0; /* read name */
1570                         while(*p > 0)
1571                         {
1572                                 if (i+1 >= sizeof(name))
1573                                 {
1574                                         PERROR_RUNTIME("Error in %s (line %d): name too long.\n",filename,line);
1575                                         break;
1576                                 }
1577                                 name[i+1] = '\0';
1578                                 name[i++] = *p++;
1579                         }
1580                 }
1581
1582                 if (*abbrev_pointer)
1583                 {
1584                         if (!strncmp(*abbrev_pointer, abbrev, strlen(*abbrev_pointer)))
1585                         {
1586                                 /* may match if abbreviation is longer */
1587                                 found_if_more_digits = 1;
1588                         }
1589                         if (!!strcasecmp(*abbrev_pointer, abbrev))
1590                                 continue;
1591                 }
1592                 if (*phone_pointer)
1593                         if (!!strcasecmp(*phone_pointer, phone))
1594                                 continue;
1595                 if (*name_pointer)
1596                         if (!!strcasecmp(*name_pointer, name))
1597                                 continue;
1598
1599                 found = 1;
1600                 break; /* found entry */
1601         }
1602
1603         if (fp) fclose(fp);
1604
1605         if (found)
1606         {
1607                 *abbrev_pointer = abbrev;
1608                 *phone_pointer = phone;
1609                 *name_pointer = name;
1610         }
1611
1612         if (found == 0)
1613         {
1614                 if (found_if_more_digits)
1615                         found = -1;
1616         }
1617         return(found);
1618 }
1619
1620 /* parsing secrets file
1621  *
1622  * 'number' specifies the externsion number, not the caller id
1623  * 'remote_id' specifies the dialed number, or the caller id for incoming calls
1624  * the result is the auth, crypt and key string, and 1 is returned.
1625  * on failure or not matching number, the 0 is returned
1626  */
1627 int parse_secrets(char *number, char *remote_id, char **auth_pointer, char **crypt_pointer, char **key_pointer)
1628 {
1629         FILE *fp=NULL;
1630         char filename[256];
1631         char *p;
1632         char remote[128];
1633         static char auth[64], crypt[64], key[4096];
1634         unsigned int line,i;
1635         char buffer[4096];
1636         int found = 0;
1637
1638         SPRINT(filename, "%s/%s/%s/secrets", INSTALL_DATA, options.extensions_dir, number);
1639
1640         if (!(fp = fopen(filename, "r")))
1641         {
1642                 PERROR("Cannot open secrets: \"%s\"\n", filename);
1643                 return(0);
1644         }
1645
1646         line=0;
1647         while((fgets(buffer, sizeof(buffer), fp)))
1648         {
1649                 line++;
1650                 buffer[sizeof(buffer)-1] = '\0';
1651                 if (buffer[0]) buffer[strlen(buffer)-1] = '\0';
1652                 p = buffer;
1653
1654                 while(*p <= 32) /* skip spaces */
1655                 {
1656                         if (*p == 0)
1657                                 break;
1658                         p++;
1659                 }
1660                 if (*p==0 || *p=='#') /* ignore comments and empty line */
1661                         continue;
1662
1663                 remote[0]=0;
1664                 auth[0]=0;
1665                 crypt[0]=0;
1666                 key[0]=0;
1667
1668                 i=0; /* read auth */
1669                 while(*p > 32)
1670                 {
1671                         if (i+1 >= sizeof(remote))
1672                         {
1673                                 PERROR_RUNTIME("Error in %s (line %d): remote too long.\n",filename,line);
1674                                 break;
1675                         }
1676                         remote[i+1] = '\0';
1677                         remote[i++] = *p++;
1678                 }
1679
1680                 while(*p <= 32) /* skip spaces */
1681                 {
1682                         if (*p == 0)
1683                                 break;
1684                         p++;
1685                 }
1686
1687                 if (*p!=0 && *p!='#') /* auth */
1688                 {
1689                         i=0; /* read auth */
1690                         while(*p > 32)
1691                         {
1692                                 if (i+1 >= sizeof(auth))
1693                                 {
1694                                         PERROR_RUNTIME("Error in %s (line %d): auth too long.\n",filename,line);
1695                                         break;
1696                                 }
1697                                 auth[i+1] = '\0';
1698                                 auth[i++] = *p++;
1699                         }
1700                         while(*p <= 32) /* skip spaces */
1701                         {
1702                                 if (*p == 0)
1703                                         break;
1704                                 p++;
1705                         }
1706                 }
1707
1708                 if (*p!=0 && *p!='#') /* crypt */
1709                 {
1710                         i=0; /* read crypt */
1711                         while(*p > 32)
1712                         {
1713                                 if (i+1 >= sizeof(crypt))
1714                                 {
1715                                         PERROR_RUNTIME("Error in %s (line %d): crypt too long.\n",filename,line);
1716                                         break;
1717                                 }
1718                                 crypt[i+1] = '\0';
1719                                 crypt[i++] = *p++;
1720                         }
1721                         while(*p <= 32) /* skip spaces */
1722                         {
1723                                 if (*p == 0)
1724                                         break;
1725                                 p++;
1726                         }
1727                 }
1728
1729                 if (*p!=0 && *p!='#') /* key */
1730                 {
1731                         i=0; /* read key */
1732                         while(*p > 0)
1733                         {
1734                                 if (i+1 >= sizeof(key))
1735                                 {
1736                                         PERROR_RUNTIME("Error in %s (line %d): key too long.\n",filename,line);
1737                                         break;
1738                                 }
1739                                 key[i+1] = '\0';
1740                                 key[i++] = *p++;
1741                         }
1742                 }
1743 //printf("COMPARING: '%s' with '%s' %s %s %s\n", remote_id, remote, auth, crypt, key);
1744
1745                 if (!!strcasecmp(remote, remote_id))
1746                         continue;
1747
1748                 found = 1;
1749                 break; /* found entry */
1750         }
1751
1752         if (fp) fclose(fp);
1753
1754         if (found)
1755         {
1756                 *auth_pointer = auth;
1757                 *crypt_pointer = crypt;
1758                 *key_pointer = key;
1759         }
1760
1761         return(found);
1762 }
1763
1764 /* parse directory
1765  *
1766  * the caller id is given and the name is returned. if the name is not found,
1767  * NULL is returned.
1768  * on success a 1 is returned and the pointers of elements are set to the
1769  * result.
1770  */
1771 char *parse_directory(char *number, int type)
1772 {
1773         FILE *fp=NULL;
1774         char filename[256];
1775         char *p;
1776         static char phone[32], name[64];
1777         unsigned int line,i;
1778         char buffer[256];
1779         int found = 0;
1780
1781         SPRINT(filename, "%s/directory.list", INSTALL_DATA);
1782
1783         if (!(fp = fopen(filename, "r")))
1784         {
1785                 PERROR("Cannot open directory: \"%s\"\n", filename);
1786                 return(NULL);
1787         }
1788
1789         line=0;
1790         while((fgets(buffer, sizeof(buffer), fp)))
1791         {
1792                 line++;
1793                 buffer[sizeof(buffer)-1] = '\0';
1794                 if (buffer[0]) buffer[strlen(buffer)-1] = '\0';
1795                 p = buffer;
1796
1797                 while(*p <= 32) /* skip spaces */
1798                 {
1799                         if (*p == 0)
1800                                 break;
1801                         p++;
1802                 }
1803                 if (*p==0 || *p=='#') /* ignore comments and empty line */
1804                         continue;
1805
1806                 phone[0]=0;
1807                 name[0]=0;
1808
1809                 i=0; /* read number */
1810                 while(*p > 32)
1811                 {
1812                         if (i+1 >= sizeof(phone))
1813                         {
1814                                 PERROR_RUNTIME("Error in %s (line %d): number too long.\n",filename,line);
1815                                 break;
1816                         }
1817                         phone[i+1] = '\0';
1818                         phone[i++] = *p++;
1819                 }
1820
1821                 while(*p <= 32) /* skip spaces */
1822                 {
1823                         if (*p == 0)
1824                                 break;
1825                         p++;
1826                 }
1827
1828                 i=0; /* read name */
1829                 while(*p >= 32)
1830                 {
1831                         if (i+1 >= sizeof(name))
1832                         {
1833                                 PERROR_RUNTIME("Error in %s (line %d): name too long.\n",filename,line);
1834                                 break;
1835                         }
1836                         name[i+1] = '\0';
1837                         name[i++] = *p++;
1838                 }
1839
1840                 if (phone[0] == 'i')
1841                 {
1842                         if (type != INFO_NTYPE_INTERNATIONAL)
1843                                 continue;
1844                         if (!strcmp(number, phone+1))
1845                         {
1846                                 found = 1;
1847                                 break;
1848                         }
1849                         continue;
1850                 }
1851                 if (phone[0] == 'n')
1852                 {
1853                         if (type != INFO_NTYPE_NATIONAL)
1854                                 continue;
1855                         if (!strcmp(number, phone+1))
1856                         {
1857                                 found = 1;
1858                                 break;
1859                         }
1860                         continue;
1861                 }
1862                 if (phone[0] == 's')
1863                 {
1864                         if (type==INFO_NTYPE_NATIONAL || type==INFO_NTYPE_INTERNATIONAL)
1865                                 continue;
1866                         if (!strcmp(number, phone+1))
1867                         {
1868                                 found = 1;
1869                                 break;
1870                         }
1871                         continue;
1872                 }
1873                 if (!strncmp(phone, options.international, strlen(options.international)))
1874                 {
1875                         if (type != INFO_NTYPE_INTERNATIONAL)
1876                                 continue;
1877                         if (!strcmp(number, phone+strlen(options.international)))
1878                         {
1879                                 found = 1;
1880                                 break;
1881                         }
1882                         continue;
1883                 }
1884                 if (!options.national[0]) /* no national prefix */
1885                 {
1886                         if (type == INFO_NTYPE_INTERNATIONAL)
1887                                 continue;
1888                         if (!strcmp(number, phone))
1889                         {
1890                                 found = 1;
1891                                 break;
1892                         }
1893                         continue;
1894                 }
1895                 if (!strncmp(phone, options.national, strlen(options.national)))
1896                 {
1897                         if (type != INFO_NTYPE_NATIONAL)
1898                                 continue;
1899                         if (!strcmp(number, phone+strlen(options.national)))
1900                         {
1901                                 found = 1;
1902                                 break;
1903                         }
1904                         continue;
1905                 }
1906                 if (type==INFO_NTYPE_NATIONAL || type==INFO_NTYPE_INTERNATIONAL)
1907                         continue;
1908                 if (!strcmp(number, phone))
1909                 {
1910                         found = 1;
1911                         break;
1912                 }
1913         }
1914
1915         if (fp) fclose(fp);
1916
1917         if (found)
1918                 return(name);
1919         else
1920                 return(NULL);
1921 }
1922
1923 /* parse callbackauth
1924  *
1925  * searches for the given caller id and returns 1 == true or 0 == false
1926  */
1927 int parse_callbackauth(char *number, struct caller_info *callerinfo)
1928 {
1929         FILE *fp = NULL;
1930         char filename[256];
1931         char *p;
1932         unsigned int line,i;
1933         char buffer[256];
1934         static char caller_type[32], caller_id[64];
1935         int found = 0;
1936
1937         SPRINT(filename, "%s/%s/%s/callbackauth", INSTALL_DATA, options.extensions_dir, number);
1938
1939         if (!(fp = fopen(filename, "r")))
1940         {
1941                 PDEBUG(DEBUG_EPOINT, "Cannot open callbackauth: \"%s\"\n", filename);
1942                 return(0);
1943         }
1944
1945         line=0;
1946         while((fgets(buffer, sizeof(buffer), fp)))
1947         {
1948                 line++;
1949                 buffer[sizeof(buffer)-1] = '\0';
1950                 if (buffer[0]) buffer[strlen(buffer)-1] = '\0';
1951                 p = buffer;
1952
1953                 while(*p <= 32) /* skip spaces */
1954                 {
1955                         if (*p == 0)
1956                                 break;
1957                         p++;
1958                 }
1959                 if (*p==0 || *p=='#') /* ignore comments and empty line */
1960                         continue;
1961
1962                 caller_type[0]=0;
1963                 caller_id[0]=0;
1964
1965                 i=0; /* read caller_type */
1966                 while(*p > 32)
1967                 {
1968                         if (i+1 >= sizeof(caller_type))
1969                         {
1970                                 PERROR_RUNTIME("Error in %s (line %d): caller_type too long.\n",filename,line);
1971                                 break;
1972                         }
1973                         caller_type[i+1] = '\0';
1974                         caller_type[i++] = *p++;
1975                 }
1976
1977                 while(*p <= 32) /* skip spaces */
1978                 {
1979                         if (*p == 0)
1980                                 break;
1981                         p++;
1982                 }
1983
1984                 if (*p!=0 && *p!='#') /* caller_id */
1985                 {
1986                         i=0; /* read caller_id */
1987                         while(*p > 32)
1988                         {
1989                                 if (i+1 >= sizeof(caller_id))
1990                                 {
1991                                         PERROR_RUNTIME("Error in %s (line %d): caller_id too long.\n",filename,line);
1992                                         break;
1993                                 }
1994                                 caller_id[i+1] = '\0';
1995                                 caller_id[i++] = *p++;
1996                         }
1997                         // ignoring more
1998                 }
1999
2000                 if (caller_type[0]=='\0' && caller_id[0]=='\0')
2001                         continue;
2002
2003                 if (atoi(caller_type) != callerinfo->ntype)
2004                         continue;
2005
2006                 if (!!strcmp(caller_id, callerinfo->id))
2007                         continue;
2008
2009                 found = 1;
2010                 break; /* found entry */
2011         }
2012
2013         if (fp) fclose(fp);
2014
2015         if (found)
2016                 return(1);
2017         return(0);
2018 }
2019
2020
2021 /* append line to callbackauth
2022  *
2023  */
2024 void append_callbackauth(char *number, struct caller_info *callerinfo)
2025 {
2026         FILE *fp = NULL;
2027         char filename[256];
2028
2029         SPRINT(filename, "%s/%s/%s/callbackauth", INSTALL_DATA, options.extensions_dir, number);
2030
2031         if (callerinfo->id[0]=='\0')
2032         {
2033                 PERROR("caller has no id.\n");
2034                 return;
2035         }
2036         if (!(fp = fopen(filename, "a")))
2037         {
2038                 PERROR("Cannot open callbackauth: \"%s\"\n", filename);
2039                 return;
2040         }
2041
2042         fprintf(fp, "%6d  %s\n", callerinfo->ntype, callerinfo->id);
2043
2044         fclose(fp);
2045
2046 }
2047
2048