]> git.sur5r.net Git - openldap/blob - clients/fax500/faxtotpc.c
Add OpenLDAP RCSid to *.[ch] in clients, libraries, and servers.
[openldap] / clients / fax500 / faxtotpc.c
1 /* $OpenLDAP$ */
2 /*
3  * Copyright (c) 1993 Regents of the University of Michigan.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms are permitted
7  * provided that this notice is preserved and that due credit is given
8  * to the University of Michigan at Ann Arbor. The name of the University
9  * may not be used to endorse or promote products derived from this
10  * software without specific prior written permission. This software
11  * is provided ``as is'' without express or implied warranty.
12  *
13  *
14  * Routines for parsing the facsimileTelephoneNumber field out of
15  * an X.500 entry and converting it to a "tpc.int" domain name.
16  *
17  * char *faxtotpc( char *str, char *userinfo)
18  *
19  * faxtotpc() returns a pointer to a string allocated with malloc(3).
20  */
21
22 #include "portable.h"
23
24 #include <stdio.h>
25
26 #include <ac/ctype.h>
27 #include <ac/string.h>
28 #include <ac/unistd.h>
29
30 #include "fax500.h"
31
32 #define TPCDOMAIN       "tpc.int"
33
34 /*
35  * Remove everything from 'str' which is not a digit
36  */
37 void
38 strip_nonnum( char *str )
39 {
40         char *p, *q;
41         p = q = str;
42         for (;;) {
43                 if (*p == '\0') {
44                         *q = *p;
45                         return;
46                 }
47
48                 if (isdigit((u_char) *p)) {
49                         *q = *p;
50                         p++;
51                         q++;
52                 } else {
53                         p++;
54                 }
55         }
56 }
57
58
59
60 /* 
61  * Remove anything of the form (blah) where
62  * "blah" contains a non-numeric character.
63  */
64 char *
65 remove_parens( char *ibuf, char *obuf )
66 {
67         char *p = ibuf;
68         char *q = obuf;
69
70         while (*p != '\0') {
71                 char *s;
72                 char *t;
73                 if (*p == '(') {
74                         /* look for a closing paren */
75                         if (( s = strchr(p, ')')) != NULL) {
76                                 /* Check the string between p and s */
77                                 /* for non-numeric characters       */
78                                 t = p + 1;
79                                 while (t < s) {
80                                         if (!isdigit((u_char) *t)) {
81                                                 /* garbage, delete */
82                                                 p = s + 1;
83                                                 t = p;
84                                                 break;
85                                         }
86                                         t++;
87                                 }
88                                 /* when we get here, p points to the first */
89                                 /* thing we want to keep, t to the last.   */
90                                 strncpy(q, p,  t - p);
91                                 q += t - p;
92                                 p = t;
93                         } else {
94                                 /* no closing paren, what to do?  keep it */
95                                 *q = *p;
96                                 p++;
97                                 q++;
98                         }
99                 } else {
100                         /* not a paren - copy out */
101                         *q = *p;
102                         p++;
103                         q++;
104                 }
105         }
106         *q = '\0';      /* terminate output string */
107         return(obuf);
108 }
109
110
111
112
113 /*
114  * Apply local fixups to phone numbers here.  Replace this routine
115  * with code to expand common "abbreviations" for phone numbers.  For
116  * example, on the U-M campus, it's only necessary to dial the last
117  * 5 digits of the telephone number, and hence people here tend to
118  * give only the last 5 digits of their fax numbers.
119  *
120  * Local U-M mods:
121  * If exactly 5 digits were provided, assume it's a campus
122  * phone number and prepend "1313nm" where "mn" are computed
123  * according to the following:
124  * first digit of 
125  * 5-digit "Local" 
126  * phone              mn
127  * -----              --
128  * 3                  76 e.g. "31234" -> "7631234"
129  * 4                  76
130  * 7                  74
131  * 6                  93
132  * 8                  99
133  */
134 char *
135 munge_phone( char *ibuf, char *obuf )
136 {
137 #define UMAREACODE      "1313"
138
139         char prefix[3];
140
141         if (strlen(ibuf) == 7) {
142                 /* Assume local number w/o area code */
143                 sprintf(obuf, "%s%s", UMAREACODE, ibuf);
144                 return(obuf);
145         }
146         if (strlen(ibuf) == 10) {
147                 /* Assume local number with area code */
148                 sprintf(obuf, "%s%s", "1", ibuf);
149                 return(obuf);
150         }
151         if (strlen(ibuf) != 5) {
152                 /* Not 5 digits - leave alone */
153                 strcpy(obuf, ibuf);
154                 return(obuf);
155         }
156
157         switch (ibuf[0]) {
158           case '3'      :
159           case '4'      :       strcpy(prefix, "76");
160                                 break;
161           case '7'      :       strcpy(prefix, "74");
162                                 break;
163           case '6'      :       strcpy(prefix, "93");
164                                 break;
165           case '8'      :       strcpy(prefix, "99");
166                                 break;
167           default       :       /* Unknown, leave alone */
168                                 strcpy(obuf, ibuf);
169                                 return(obuf);
170         }
171         sprintf(obuf, "%s%s%s", UMAREACODE, prefix, ibuf);
172         return(obuf);
173 }
174
175
176
177 /* 
178  * Convert string to "tpc.int" domain name.
179  */
180 char *
181 faxtotpc( char *phone, char *userinfo )
182 {
183         char *p;
184         char *q;
185         char ibuf[255];
186         char obuf[255];
187
188         /* nuke spaces */
189         strcpy(ibuf, phone);
190         for (p = ibuf, q = obuf; *p != '\0'; p++) {
191                 if (*p != ' ') {
192                         *q = *p;
193                         q++;
194                 }
195         }
196         *q = '\0';
197         strcpy(ibuf, obuf);
198
199         remove_parens(ibuf, obuf);
200         strcpy(ibuf, obuf);
201
202         /* Look for "+" - if followed by a number,
203            assume it's an international number and leave
204            it alone.
205         */
206         if ((p = strchr(ibuf, '+')) != NULL) {
207                 if (isdigit((u_char) *(p + 1))) {
208                         /* strip any non-digits */
209                         strip_nonnum(ibuf);
210                 }
211         } else {
212                 strip_nonnum(ibuf);
213
214                 /* Apply local munges */
215                 munge_phone(ibuf, obuf);
216                 strcpy(ibuf, obuf);
217         }
218
219         /* Convert string of form abcd to remote-printer@d.c.b.a.tpc.int */
220         q = obuf;
221         for (p = ibuf + strlen(ibuf) - 1; p >= ibuf; p--) {
222                 *q++ = *p;
223                 *q++ = '.';
224         }
225         *q = '\0';
226         strcpy(ibuf, obuf);
227         strcpy(obuf, "remote-printer");
228
229         /* include userinfo if present */
230         if (userinfo != NULL && strlen(userinfo)) {
231                 strcat(obuf, ".");
232                 strcat(obuf, userinfo);
233         }
234         strcat(obuf, "@");
235         strcat(obuf, ibuf);             /* tack on reversed phone number */
236         strcat(obuf, TPCDOMAIN);        /* tack on domain name */
237         p = strdup(obuf);
238         return(p);
239 }