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