File: wuzzah.c

package info (click to toggle)
wuzzah 0.53-3
  • links: PTS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, stretch
  • size: 596 kB
  • ctags: 195
  • sloc: ansic: 1,830; sh: 330; makefile: 36
file content (351 lines) | stat: -rw-r--r-- 10,008 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
 * wuzzah.c
 *
 * written and (c) 2001 by Sean Finney
 *
 * This program can be freely copied and redistributed under
 * the terms of the GNU General Public License, v2.  For details
 * of said terms, please see the file COPYING
 *
 */

//	Includes
#include "includes.h"
#include "buddies.h"
#include "hashtable.h"
#include "common.h"

//	Definitions

//	Macros

//	Function Prototypes
void usage(void);
void version(void);
void wuzzah_whoami(void);
void wuzzah_add_extrabuddies(const char *line, htable_t *ht);

//	Global (config) Variables 
//	g_progname:		the name of this program (basename(argv[0]))
//	g_infile:		file to be read in 
//	g_buddy_msg:		message to be sent to buddy
//	g_status_msg:		message to be displayed on local tty
//	g_write_users:		whether or not to send users a message
//	g_sleep_interval:	how long to sleep between polling utmp
//	g_whoami:		local username
//	g_no_newline:		don't hard-handedly add a newline to the mesg
//	g_eventcmd:		a command to run when a buddy logs on/off

wuzzah_config_t g_config;

static const char *g_default_buddy_msg=
	"(wuzzah)  %u says: \"shoutout to my homie %b.\"";
static const char *g_default_status_msg=
	"(%d)  %b %o %l from %h";
static const char *g_default_infile=".wuzzah";
static const int g_default_sleep_interval=1;

struct option program_opts[] = {
	{ "all-users",1,0,'a' },
	{ "exec-command",1,0,'c' },
	{ "help",0,0,'h' },
	{ "buddyfile",1,0,'f' },
	{ "no-buddyfile",0,0,'F'},
	{ "interval",1,0,'i' },
	{ "message",1,0,'m' },
	{ "no-newline",0,0,'n' },
	{ "process-once",0,0,'o' },
	{ "process-current",0,0,'p' },
	{ "silent",0,0,'q' },
	{ "status-message",1,0,'s' },
	{ "users",1,0,'u' },
	{ "version",0,0,'v' },
	{ "write-buddies",0,0,'w' },
	{ NULL, 0, 0, 0}
};

extern char *optarg;
extern int optind;

//	---- MAIN ----
int main(int argc, char *argv[]){
	htable_t *ht=NULL;
	process_args(argc, argv, &g_config);

	ht=bud_init_budtable();
	wuzzah_whoami();
	if(g_config.all_users){
		bud_load_every_user(ht);
	} else {
		if(g_config.extrabuddies)
			wuzzah_add_extrabuddies(g_config.extrabuddies, ht);
		if(!g_config.noloadfile) bud_load(g_config.infile, ht);
	}

	if(ht->size==0){
		fprintf(stderr, 
			"%s: no buddies specified\n", g_config.progname);
		fprintf(stderr, "well, if you don't have any friends for\n");
		fprintf(stderr, "me to watch, I'm going away!\n");
		exit(1);
	}

	// let's do it once first, and not spam everyone when
	// we start up (hence 0).
	if(!g_config.process_current) bud_chk_utmpx(ht, -1);
	else bud_chk_utmpx(ht, g_config.write_users);
	// if we were only supposed to go once
	if(g_config.run_once) return 0;
	// otherwise...
	while(!sleep(g_config.sleep_interval)){
		bud_chk_utmpx(ht, g_config.write_users);
	}

	return 0;
}

//	---- Other Functions ----

void process_args(int ac, char *av[], wuzzah_config_t *conf){
	int c, len;
	char *homedir;

	optind=0;

	memset(conf, 0, sizeof(wuzzah_config_t));
	conf->progname=av[0]=basename(av[0]);	

	while(1){
		c=getopt_long(ac, av, "ac:f:Fhi:m:nopqs:u:vw", 
				program_opts, NULL);
		switch(c){
		case 'a': conf->all_users=1; break;
		case 'c': conf->eventcmd=strdup(optarg); break;
		case 'f': conf->infile=strdup(optarg); break;
		case 'F': conf->noloadfile=1; break;
		case 'h': version(); usage(); exit(0); break;
		case 'i': conf->sleep_interval=atoi(optarg); break;
		case 'm': conf->buddy_msg=strdup(optarg); break;
		case 'n': conf->no_newline=1; break;
		case 'o': conf->run_once=1;
		case 'p': conf->process_current=1; break;
		case 'q': conf->write_users=0; break;
		case 's': conf->status_msg=strdup(optarg); break;
		case 'u': conf->extrabuddies=strdup(optarg); break;
		case 'v': version(); exit(0); break;
		case 'w': conf->write_users=1; break;
		} 
		if(c==-1)break;
	}
	if(!conf->buddy_msg)conf->buddy_msg=g_default_buddy_msg;
	if(!conf->status_msg)conf->status_msg=g_default_status_msg;

	//	if the userfile hasn't been specified, use ${HOME}/.wuzzah
	if(!conf->infile && !conf->noloadfile){
		homedir=getenv("HOME");
		if(homedir==NULL) { 
			conf->noloadfile=1; 
		} else {
			len=strlen(homedir)+1+strlen(g_default_infile); // 1 for '/'
			conf->infile=(char*)malloc(sizeof(char)*(len+1));
			strncpy(conf->infile, homedir, strlen(homedir)+1);
			strncat(conf->infile, "/", 1);
			strncat(conf->infile, g_default_infile,
				strlen(g_default_infile));
		}
	}
	if(!conf->sleep_interval)
		conf->sleep_interval=g_default_sleep_interval;
}

/*
 * gaaaah, my eyes, this is so ugly.  please make me prettier without
 * losing any functionality...
 *
 */
int string_to_argv(const char *str, char **av[]){
	int c=0, i=0, j=0, num_args=1, len=strlen(str), procd_str_pipe[2];
	int arg_len=0, arg_max_len=0;
	short parsing_whitespace=1;
	char **argv=NULL, *tmp=strdup(str);
	FILE *p_write, *p_read;

	if(pipe(procd_str_pipe)) perror("opening pipe");
	p_read=fdopen(procd_str_pipe[0], "r");
	if(!p_read) perror("opening read end of pipe");
	p_write=fdopen(procd_str_pipe[1], "w");
	if(!p_write) perror("opening write end of pipe");

	for(i=0;i<len;i++){
		switch(str[i]){
		case '\\':
			if(parsing_whitespace) {
				num_args++;
				parsing_whitespace=0;
			}
			if(i==len-1) {
				fprintf(stderr, 
					"syntax err: unterminated \\\n");
				return -1;
			}
			fputc(str[i++], p_write);
			fputc(str[i], p_write);
			arg_len+=2;
			break;
		case ' ': case '\n': case '\t':
			if(!parsing_whitespace){
				if(arg_len>arg_max_len)arg_max_len=arg_len;
				arg_len=0;
				fputc('\0', p_write);
				parsing_whitespace=1;
			} 
			break;
		case '\'':
			if(parsing_whitespace) {
				num_args++;
				parsing_whitespace=0;
			}
			while(str[i+1] != '\'' || str[i] == '\\'){
				if(++i > len){
					fprintf(stderr, 
						"error: unterminated '\n");
					return -1;
				}
				if(str[i+1]=='\'' && str[i] == '\\') continue;
				else {
					arg_len++;
					fputc(str[i], p_write);
				}
			}
			i++;
			break;
		case '"':
			if(parsing_whitespace) {
				num_args++;
				parsing_whitespace=0;
			}
			while(str[i+1] != '"' || str[i] == '\\'){
				if(++i > len){
					fprintf(stderr, 
						"error: unterminated \"\n");
					return -1;
				}
				if(str[i+1]=='"' && str[i] == '\\') continue;
				else {
					arg_len++;
					fputc(str[i], p_write);
				}
			}
			i++;
			break;
		default: 
			if(parsing_whitespace) {
				num_args++;
				parsing_whitespace=0;
			}
			arg_len++;
			fputc(str[i], p_write);
			break;
		}
	}

	fclose(p_write);
	argv=(char **)malloc(sizeof(char*)*(num_args+1));
	memset(argv, 0, sizeof(char)*(num_args+1));

	argv[0]=strdup(g_config.progname);

	for(i=1; i<num_args; i++){
		argv[i]=(char*)malloc(sizeof(char)*(arg_max_len+1));
		memset(argv[i], 0, sizeof(char)*(arg_max_len+1));
		c=fgetc(p_read);
		for(j=0; c!='\0' && !feof(p_read) && !ferror(p_read); j++){
			argv[i][j] = c;	
			c=fgetc(p_read);
		}
		argv[i][j]='\0';
	}
	free(tmp);
	argv[num_args]=NULL;
	*(av)=argv;
	return num_args;
}

void wuzzah_whoami(void){
	struct passwd *pwd=getpwuid(getuid());	
	// get the user's name as gracefully as we can :)
	if(!pwd){
		fprintf(stderr, 
			"uh, your uid (%d) wasn't found..\n", (int)getuid());
		fprintf(stderr, "so I'm setting your name to \"dumbass\".\n");
		g_config.whoami=(char*)malloc(sizeof(char)*8);
		strncpy(g_config.whoami, "dumbass", 8);
	} else {
		g_config.whoami=(char*)malloc(sizeof(char)*(strlen(pwd->pw_name)+1));
		strncpy(g_config.whoami, pwd->pw_name, (strlen(pwd->pw_name)+1));
	}
}

void wuzzah_add_extrabuddies(const char *line, htable_t *ht){
	int i=0, len=strlen(line);
	const char *head=line;
	char *buf=NULL;

	buf=(char*)malloc(sizeof(char)*(strlen(line)+1));
	while((int)(head-line)<len){
		i=strcspn(head, " \t\n,:");
		if(i>0) {
			strncpy(buf, head, i);
			buf[i]='\0';
		} else {
			strncpy(buf, head, strlen(head));
			buf[strlen(head)]='\0';
		}
		ht_insert(bud_create_buddy(buf), ht);
		head=&head[i+1];
	}
	free(buf);
}

void version(void){
	fprintf(stderr, "%s v%s\t-\twuuzzzaaah?\n", g_config.progname, 
			PACKAGE_VERSION);
	fprintf(stderr, "copyright 2002 sean finney <seanius@seanius.net>\n");
}

void usage(void){
        fprintf(stderr, "\nusage:\t");
        fprintf(stderr, "%s [OPTIONS] ...\n", g_config.progname);
        fprintf(stderr, ""
"  -h, --help                   this informative usage summary :)\n"
"  -v, --version                the current version and copyright\n\n"
"  -a, --all-users              load and watch for all users\n"
"  -c, --exec-cmd=CMD           evaluate/execute CMD with every login\n"
"  -f, --buddyfile=FILE         use FILE as buddyfile, can be '-' for\n"
"                               signifying to use stdin.\n"
"  -F, --no-buddyfile           do not attempt to load the file which\n"
"                               contains the list of buddies to watch\n"
"  -i, --interval=NUM           sleep NUM seconds between each polling\n"
"  -m, --message=STRING         use STRING as a message template to greet\n"
"                               logged-in buddies. RTFM for more information\n"
"  -n, --no-newline             don't end the various messages in newlines\n"
"  -o, --process-once           exit after having scanned once through the\n"
"                               records.\n"
"  -p, --process-current        override wuzzah's default behavior and\n"
"                               message/write/exec-cmd the users already\n"
"                               logged in.\n"
"  -q, --silent                 don't message buddies when they log in\n"
"  -s, --status-message=STRING  use STRING as a template for displaying the\n"
"                               status of people logging in and out\n"
"  -u, --users=LIST             adds LIST to the list of buddies for which\n"
"                               to watch.\n"
"  -w, --write-budies           be obnoxious and write your buddy every time\n"
"                               you see him/her log in.\n"
        );
}

void bail(char *reason, int exitval){
	if(reason) fprintf(stderr, "%s: bailing: %s\n", 
			g_config.progname, reason);
	exit(exitval);
}