]> asedeno.scripts.mit.edu Git - 1ts-debian.git/blob - zephyr/clients/znol/znol.c
need automake as a build-dep, even though we don't use most of it
[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, char *argv[])
28 {
29         register char *cp;
30         ZSubscription_t subs[SUBSATONCE];
31         ZLocations_t locations;
32         FILE *fp = NULL;
33         struct passwd *pwd;
34         char anyonename[BUFSIZ],name[BUFSIZ],cleanname[BUFSIZ],*envptr;
35         char *comment_ptr;
36         int onoff = ON,quiet = 0,justlist = 0,useronly = 0, filenamed = 0;
37         int retval,arg,ind,one,numlocs,i;
38         int wgport = 0;
39
40         if ((retval = ZInitialize()) != ZERR_NONE) {
41                 com_err(argv[0],retval,"initializing");
42                 exit (1);
43         }
44
45         for (arg=1;arg<argc;arg++) {
46                 if (!strcmp(argv[arg],"on")) {
47                         onoff = ON;
48                         continue;
49                 }
50                 if (!strcmp(argv[arg],"off")) {
51                         onoff = OFF;
52                         continue;
53                 } 
54                 if (argv[arg][0] == '-') {
55                         char opt = argv[arg][1];
56                         if (opt == 0 || argv[arg][2] != 0)
57                                 goto usage;
58                         switch (argv[arg][1]) {
59                         case 'q':
60                                 quiet = 1;
61                                 break;
62                         case 'l':
63                                 justlist = 1;
64                                 break;
65                         case 'f':
66                                 if (arg == argc-1) {
67                                         fprintf(stderr,"No file name specified\n");
68                                         goto usage;
69                                 }
70                                 (void) strcpy(anyonename,argv[++arg]);
71                                 filenamed = 1;
72                                 break;
73                         case 'u':
74                                 if (arg == argc-1) {
75                                         fprintf(stderr,"No username specified\n");
76                                         goto usage;
77                                 }
78                                 (void) strcpy(cleanname,argv[++arg]);
79                                 useronly = 1;
80                                 break;
81                         default:
82                                 goto usage;
83                         }
84                         continue;
85                 }
86             usage:
87                 fprintf(stderr,"Usage: %s [on|off] [-q | -l] [-f file | -u username]\n", argv[0]);
88                 exit (1);
89         }
90
91         if (quiet && justlist) {
92                 fprintf(stderr,"-q and -l cannot both be used\n");
93                 goto usage;
94         } 
95         if (useronly && filenamed) {
96                 fprintf(stderr,"-u and -f cannot both be used\n");
97                 goto usage;
98         } 
99         if (!justlist)
100                 if ((wgport = ZGetWGPort()) == -1) {
101                         com_err(argv[0],errno,"while getting WindowGram port");
102                         exit(1);
103                 }
104         
105         if (!useronly) {
106                 /* If no filename specified, get the default */
107                 if (!filenamed) {
108                         envptr = getenv("HOME");
109                         if (envptr)
110                             (void) strcpy(anyonename,envptr);
111                         else {
112                                 if (!(pwd = getpwuid((int) getuid()))) {
113                                         fprintf(stderr,"You are not listed in the password file.\n");
114                                         exit (1);
115                                 }
116                                 (void) strcpy(anyonename,pwd->pw_dir);
117                         }
118                         (void) strcat(anyonename,"/.anyone");
119                 }
120
121                 /* if the filename is "-", read stdin */
122                 if (strcmp(anyonename,"-") == 0) {
123                         fp = stdin;
124                 } else if (!(fp = fopen(anyonename,"r"))) {
125                         fprintf(stderr,"Can't open %s for input\n",anyonename);
126                         exit (1);
127                 }
128         }
129
130         ind = 0;
131         
132         for (;;) {
133                 if (!useronly) {
134                     if (!fgets(cleanname,sizeof cleanname,fp))
135                         break;
136                     if (cleanname[0] == '#' || cleanname[0] == '\0' ||
137                         cleanname[0] == '\n')
138                         continue;       /* ignore comment and empty lines */
139                     comment_ptr = strchr(cleanname, '#');
140                     if (comment_ptr)
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 }