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