faxtotpc.c 4.88 KB
Newer Older
Kurt Zeilenga's avatar
Kurt Zeilenga committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*
 * Copyright (c) 1993 Regents of the University of Michigan.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms are permitted
 * provided that this notice is preserved and that due credit is given
 * to the University of Michigan at Ann Arbor. The name of the University
 * may not be used to endorse or promote products derived from this
 * software without specific prior written permission. This software
 * is provided ``as is'' without express or implied warranty.
 *
 *
 * Routines for parsing the facsimileTelephoneNumber field out of
 * an X.500 entry and converting it to a "tpc.int" domain name.
 *
 * char *faxtotpc(str)
 * char *str;
 *
 * faxtotpc() returns a pointer to a string allocated with malloc(3).
 */

Kurt Zeilenga's avatar
Kurt Zeilenga committed
22
23
#include "portable.h"

Kurt Zeilenga's avatar
Kurt Zeilenga committed
24
25
26
#include <stdio.h>
#include <ctype.h>

Kurt Zeilenga's avatar
Kurt Zeilenga committed
27
28
#include <sys/types.h>
#include <ac/string.h>
Kurt Zeilenga's avatar
Kurt Zeilenga committed
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246

#define	TPCDOMAIN	"tpc.int"

/*
 * Remove everything from 'str' which is not a digit
 */
void strip_nonnum(str)
char *str;
{
	char *p, *q;
	p = q = str;
	for (;;) {
		if (*p == '\0') {
			*q = *p;
			return;
		}

		if (isdigit((u_char) *p)) {
			*q = *p;
			p++;
			q++;
		} else {
			p++;
		}
	}
}



/* 
 * Remove anything of the form (blah) where
 * "blah" contains a non-numeric character.
 */
char *remove_parens(ibuf, obuf)
char *ibuf;
char *obuf;
{
	char *p = ibuf;
	char *q = obuf;

	while (*p != '\0') {
		char *s;
		char *t;
		if (*p == '(') {
			/* look for a closing paren */
			if (( s = strchr(p, ')')) != NULL) {
				/* Check the string between p and s */
				/* for non-numeric characters       */
				t = p + 1;
				while (t < s) {
					if (!isdigit((u_char) *t)) {
						/* garbage, delete */
						p = s + 1;
						t = p;
						break;
					}
					t++;
				}
				/* when we get here, p points to the first */
				/* thing we want to keep, t to the last.   */
				strncpy(q, p,  t - p);
				q += t - p;
				p = t;
			} else {
				/* no closing paren, what to do?  keep it */
				*q = *p;
				p++;
				q++;
			}
		} else {
			/* not a paren - copy out */
			*q = *p;
			p++;
			q++;
		}
	}
	*q = '\0';	/* terminate output string */
	return(obuf);
}




/*
 * Apply local fixups to phone numbers here.  Replace this routine
 * with code to expand common "abbreviations" for phone numbers.  For
 * example, on the U-M campus, it's only necessary to dial the last
 * 5 digits of the telephone number, and hence people here tend to
 * give only the last 5 digits of their fax numbers.
 *
 * Local U-M mods:
 * If exactly 5 digits were provided, assume it's a campus
 * phone number and prepend "1313nm" where "mn" are computed
 * according to the following:
 * first digit of 
 * 5-digit "Local" 
 * phone              mn
 * -----              --
 * 3                  76 e.g. "31234" -> "7631234"
 * 4                  76
 * 7                  74
 * 6                  93
 * 8                  99
 */
char *munge_phone(ibuf, obuf)
char *ibuf;
char *obuf;
{
#define	UMAREACODE	"1313"

	char prefix[3];

	if (strlen(ibuf) == 7) {
		/* Assume local number w/o area code */
		sprintf(obuf, "%s%s", UMAREACODE, ibuf);
		return(obuf);
	}
	if (strlen(ibuf) == 10) {
		/* Assume local number with area code */
		sprintf(obuf, "%s%s", "1", ibuf);
		return(obuf);
	}
	if (strlen(ibuf) != 5) {
		/* Not 5 digits - leave alone */
		strcpy(obuf, ibuf);
		return(obuf);
	}

	switch (ibuf[0]) {
	  case '3'	:
	  case '4'	:	strcpy(prefix, "76");
				break;
	  case '7'	:	strcpy(prefix, "74");
				break;
	  case '6'	:	strcpy(prefix, "93");
				break;
	  case '8'	:	strcpy(prefix, "99");
				break;
	  default	:	/* Unknown, leave alone */
				strcpy(obuf, ibuf);
				return(obuf);
	}
	sprintf(obuf, "%s%s%s", UMAREACODE, prefix, ibuf);
	return(obuf);
}








/* 
 * Convert string to "tpc.int" domain name.
 */
char *faxtotpc(phone, userinfo)
char *phone;
char *userinfo;
{
	char *p;
	char *q;
	char ibuf[255];
	char obuf[255];

	/* nuke spaces */
	strcpy(ibuf, phone);
	for (p = ibuf, q = obuf; *p != '\0'; p++) {
		if (*p != ' ') {
			*q = *p;
			q++;
		}
	}
	*q = '\0';
	strcpy(ibuf, obuf);

	remove_parens(ibuf, obuf);
	strcpy(ibuf, obuf);

	/* Look for "+" - if followed by a number,
	   assume it's an international number and leave
	   it alone.
	*/
	if ((p = strchr(ibuf, '+')) != NULL) {
		if (isdigit((u_char) *(p + 1))) {
			/* strip any non-digits */
			strip_nonnum(ibuf);
		}
	} else {
		strip_nonnum(ibuf);

		/* Apply local munges */
		munge_phone(ibuf, obuf);
		strcpy(ibuf, obuf);
	}

	/* Convert string of form abcd to remote-printer@d.c.b.a.tpc.int */
	q = obuf;
	for (p = ibuf + strlen(ibuf) - 1; p >= ibuf; p--) {
		*q++ = *p;
		*q++ = '.';
	}
	*q = '\0';
	strcpy(ibuf, obuf);
	strcpy(obuf, "remote-printer");

	/* include userinfo if present */
	if (userinfo != NULL && strlen(userinfo)) {
		strcat(obuf, ".");
		strcat(obuf, userinfo);
	}
	strcat(obuf, "@");
	strcat(obuf, ibuf);		/* tack on reversed phone number */
	strcat(obuf, TPCDOMAIN);	/* tack on domain name */
	p = strdup(obuf);
	return(p);
				
}