]> asedeno.scripts.mit.edu Git - 1ts-debian.git/blob - zephyr/clients/znol/znol.c
r4275@bucket (orig r265): kcr | 2008-01-21 02:57:32 -0500
[1ts-debian.git] / zephyr / clients / znol / znol.c
1 /* This file is part of the Project Athena Zephyr Notification System.
2  * It contains code for the "znol" command.
3  *
4  *      Created by:     Robert French
5  *
6  *      $Id$
7  *
8  *      Copyright (c) 1987 by the Massachusetts Institute of Technology.
9  *      For copying and distribution information, see the file
10  *      "mit-copyright.h". 
11  */
12
13 #include <sysdep.h>
14 #include <zephyr/zephyr.h>
15
16 #include <pwd.h>
17
18 #ifndef lint
19 static const char rcsid_znol_c[] = "$Id$";
20 #endif 
21
22 #define SUBSATONCE 7
23 #define ON 1
24 #define OFF 0
25
26 int
27 main(int argc,
28      char *argv[])
29 {
30         register char *cp;
31         ZSubscription_t subs[SUBSATONCE];
32         ZLocations_t locations;
33         FILE *fp;
34         struct passwd *pwd;
35         char anyonename[BUFSIZ],name[BUFSIZ],cleanname[BUFSIZ],*envptr;
36         char *comment_ptr;
37         int onoff = ON,quiet = 0,justlist = 0,useronly = 0, filenamed = 0;
38         int retval,arg,ind,one,numlocs,i;
39         int wgport;
40
41         if ((retval = ZInitialize()) != ZERR_NONE) {
42                 com_err(argv[0],retval,"initializing");
43                 exit (1);
44         }
45
46         for (arg=1;arg<argc;arg++) {
47                 if (!strcmp(argv[arg],"on")) {
48                         onoff = ON;
49                         continue;
50                 }
51                 if (!strcmp(argv[arg],"off")) {
52                         onoff = OFF;
53                         continue;
54                 } 
55                 if (argv[arg][0] == '-') {
56                         char opt = argv[arg][1];
57                         if (opt == 0 || argv[arg][2] != 0)
58                                 goto usage;
59                         switch (argv[arg][1]) {
60                         case 'q':
61                                 quiet = 1;
62                                 break;
63                         case 'l':
64                                 justlist = 1;
65                                 break;
66                         case 'f':
67                                 if (arg == argc-1) {
68                                         fprintf(stderr,"No file name specified\n");
69                                         goto usage;
70                                 }
71                                 (void) strcpy(anyonename,argv[++arg]);
72                                 filenamed = 1;
73                                 break;
74                         case 'u':
75                                 if (arg == argc-1) {
76                                         fprintf(stderr,"No username specified\n");
77                                         goto usage;
78                                 }
79                                 (void) strcpy(cleanname,argv[++arg]);
80                                 useronly = 1;
81                                 break;
82                         default:
83                                 goto usage;
84                         }
85                         continue;
86                 }
87             usage:
88                 fprintf(stderr,"Usage: %s [on|off] [-q | -l] [-f file | -u username]\n", argv[0]);
89                 exit (1);
90         }
91
92         if (quiet && justlist) {
93                 fprintf(stderr,"-q and -l cannot both be used\n");
94                 goto usage;
95         } 
96         if (useronly && filenamed) {
97                 fprintf(stderr,"-u and -f cannot both be used\n");
98                 goto usage;
99         } 
100         if (!justlist)
101                 if ((wgport = ZGetWGPort()) == -1) {
102                         com_err(argv[0],errno,"while getting WindowGram port");
103                         exit(1);
104                 }
105         
106         if (!useronly) {
107                 /* If no filename specified, get the default */
108                 if (!filenamed) {
109                         envptr = getenv("HOME");
110                         if (envptr)
111                             (void) strcpy(anyonename,envptr);
112                         else {
113                                 if (!(pwd = getpwuid((int) getuid()))) {
114                                         fprintf(stderr,"You are not listed in the password file.\n");
115                                         exit (1);
116                                 }
117                                 (void) strcpy(anyonename,pwd->pw_dir);
118                         }
119                         (void) strcat(anyonename,"/.anyone");
120                 }
121
122                 /* if the filename is "-", read stdin */
123                 if (strcmp(anyonename,"-") == 0) {
124                         fp = stdin;
125                 } else if (!(fp = fopen(anyonename,"r"))) {
126                         fprintf(stderr,"Can't open %s for input\n",anyonename);
127                         exit (1);
128                 }
129         }
130
131         ind = 0;
132         
133         for (;;) {
134                 if (!useronly) {
135                     if (!fgets(cleanname,sizeof cleanname,fp))
136                         break;
137                     if (cleanname[0] == '#' || cleanname[0] == '\0' ||
138                         cleanname[0] == '\n')
139                         continue;       /* ignore comment and empty lines */
140                     comment_ptr = strchr(cleanname, '#');
141                     if (comment_ptr)
142                         *comment_ptr = '\0'; /* Ignore from # onwards */
143                     /* Get rid of old-style nol entries, just in case */
144                     cp = cleanname + strlen(cleanname) - 1;
145                     *cp = '\0';
146                     while(*--cp == ' ')
147                         *cp = '\0';
148                     if (*cleanname == '@' || !*cleanname)
149                         continue;
150                 } else if (ind)
151                     break;              /* only do the one name */
152
153                 subs[ind].zsub_class = LOGIN_CLASS;
154                 (void) strcpy(name,cleanname);
155                 if (!strchr(name,'@')) {
156                         cp = name + strlen(name);
157                         *cp++ = '@';
158                         (void) strcpy(cp,ZGetRealm());
159                 }
160                 if ((subs[ind].zsub_classinst = malloc((unsigned)(strlen(name)+1))) == NULL) {
161                         fprintf (stderr, "znol: out of memory");
162                         exit (1);
163                 }
164                 (void) strcpy(subs[ind].zsub_classinst, name);
165                 subs[ind++].zsub_recipient = "";
166
167                 if (!quiet && onoff == ON) {
168                         if ((retval = ZLocateUser(name,&numlocs,ZAUTH))
169                             != ZERR_NONE) {
170                                 com_err(argv[0],retval,"locating user");
171                                 exit(1);
172                         }
173                         one = 1;
174                         if (numlocs) {
175                                 for (i=0;i<numlocs;i++) {
176                                         if ((retval =
177                                              ZGetLocations(&locations,&one))
178                                             != ZERR_NONE) {
179                                                 com_err(argv[0],retval,
180                                                         "getting location");
181                                                 continue;
182                                         }
183                                         if (one != 1) {
184                                                 printf("%s: internal failure while getting location\n",argv[0]);
185                                                 exit(1);
186                                         }
187                                         printf("%s: %s\t%s\t%s\n",cleanname,
188                                                locations.host,
189                                                locations.tty,
190                                                locations.time);
191                                 }
192                         }
193                 }
194                 
195                 if (ind == SUBSATONCE) {
196                         if (!justlist)
197                                 if ((retval = (onoff==ON)?
198                                      ZSubscribeToSansDefaults(subs,ind,
199                                                               (u_short)wgport):
200                                      ZUnsubscribeTo(subs,ind,(u_short)wgport)) !=
201                                     ZERR_NONE) {
202                                         com_err(argv[0],retval,(onoff==ON)?
203                                                 "subscribing":
204                                                 "unsubscribing");
205                                         exit(1);
206                                 } 
207                         for (ind=0;ind<SUBSATONCE;ind++)
208                                 free(subs[ind].zsub_classinst);
209                         ind = 0;
210                 }
211         }
212
213         if (ind && !justlist)
214                 if ((retval = (onoff==ON)?
215                      ZSubscribeToSansDefaults(subs,ind,(u_short)wgport):
216                      ZUnsubscribeTo(subs,ind,(u_short)wgport)) !=
217                     ZERR_NONE) {
218                         com_err(argv[0],retval,(onoff==ON)?
219                                 "subscribing":
220                                 "unsubscribing");
221                         exit(1);
222                 } 
223
224         if (!useronly)
225             (void) fclose(fp);          /* file is open read-only,
226                                            ignore errs */
227         exit(0);
228 }