]> asedeno.scripts.mit.edu Git - 1ts-debian.git/blob - zephyr/clients/znol/znol.c
cdb03499c318785cfb10e4c64b88bd68f66c61c5
[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                     if (comment_ptr = strchr(cleanname, '#'))
141                         *comment_ptr = '\0'; /* Ignore from # onwards */
142                     /* Get rid of old-style nol entries, just in case */
143                     cp = cleanname + strlen(cleanname) - 1;
144                     *cp = '\0';
145                     while(*--cp == ' ')
146                         *cp = '\0';
147                     if (*cleanname == '@' || !*cleanname)
148                         continue;
149                 } else if (ind)
150                     break;              /* only do the one name */
151
152                 subs[ind].zsub_class = LOGIN_CLASS;
153                 (void) strcpy(name,cleanname);
154                 if (!strchr(name,'@')) {
155                         cp = name + strlen(name);
156                         *cp++ = '@';
157                         (void) strcpy(cp,ZGetRealm());
158                 }
159                 if ((subs[ind].zsub_classinst = malloc((unsigned)(strlen(name)+1))) == NULL) {
160                         fprintf (stderr, "znol: out of memory");
161                         exit (1);
162                 }
163                 (void) strcpy(subs[ind].zsub_classinst, name);
164                 subs[ind++].zsub_recipient = "";
165
166                 if (!quiet && onoff == ON) {
167                         if ((retval = ZLocateUser(name,&numlocs,ZAUTH))
168                             != ZERR_NONE) {
169                                 com_err(argv[0],retval,"locating user");
170                                 exit(1);
171                         }
172                         one = 1;
173                         if (numlocs) {
174                                 for (i=0;i<numlocs;i++) {
175                                         if ((retval =
176                                              ZGetLocations(&locations,&one))
177                                             != ZERR_NONE) {
178                                                 com_err(argv[0],retval,
179                                                         "getting location");
180                                                 continue;
181                                         }
182                                         if (one != 1) {
183                                                 printf("%s: internal failure while getting location\n",argv[0]);
184                                                 exit(1);
185                                         }
186                                         printf("%s: %s\t%s\t%s\n",cleanname,
187                                                locations.host,
188                                                locations.tty,
189                                                locations.time);
190                                 }
191                         }
192                 }
193                 
194                 if (ind == SUBSATONCE) {
195                         if (!justlist)
196                                 if ((retval = (onoff==ON)?
197                                      ZSubscribeToSansDefaults(subs,ind,
198                                                               (u_short)wgport):
199                                      ZUnsubscribeTo(subs,ind,(u_short)wgport)) !=
200                                     ZERR_NONE) {
201                                         com_err(argv[0],retval,(onoff==ON)?
202                                                 "subscribing":
203                                                 "unsubscribing");
204                                         exit(1);
205                                 } 
206                         for (ind=0;ind<SUBSATONCE;ind++)
207                                 free(subs[ind].zsub_classinst);
208                         ind = 0;
209                 }
210         }
211
212         if (ind && !justlist)
213                 if ((retval = (onoff==ON)?
214                      ZSubscribeToSansDefaults(subs,ind,(u_short)wgport):
215                      ZUnsubscribeTo(subs,ind,(u_short)wgport)) !=
216                     ZERR_NONE) {
217                         com_err(argv[0],retval,(onoff==ON)?
218                                 "subscribing":
219                                 "unsubscribing");
220                         exit(1);
221                 } 
222
223         if (!useronly)
224             (void) fclose(fp);          /* file is open read-only,
225                                            ignore errs */
226         exit(0);
227 }