File: event.c

package info (click to toggle)
wuzzah 0.53-3.1
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye
  • size: 596 kB
  • sloc: ansic: 1,830; sh: 330; makefile: 36
file content (54 lines) | stat: -rw-r--r-- 1,346 bytes parent folder | download | duplicates (4)
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
#include "event.h"

void event(const struct buddy *b, const struct bud_utrec *u, short write){
	char *status_msg=NULL, *wr_msg=NULL, *extra_cmd=NULL;

	if(u->verified==B_LOGGED_IN || u->verified==B_LOGGED_OUT){
		if(b->conf) {
			status_msg=bud_expand_fmt(u, b->conf->status_msg);
			if(b->conf->write_users && write != -1) 
				wr_msg=bud_expand_fmt(u, b->conf->buddy_msg);
			extra_cmd=bud_expand_fmt(u, b->conf->eventcmd);
		} else {
			status_msg=bud_expand_fmt(u, g_config.status_msg);
			if(write > 0) wr_msg=bud_expand_fmt(u, g_config.buddy_msg);
			extra_cmd=bud_expand_fmt(u, g_config.eventcmd);
		}
	}

	if(status_msg) {
		fprintf(stdout, "%s", status_msg);
		free(status_msg);
	}
	if(wr_msg) {
		if(u->verified==B_LOGGED_IN)
			wri_do_write(u->u->ut_user, u->u->ut_line, wr_msg);
		free(wr_msg);
	}
	if(extra_cmd) {
		if(u->verified==B_LOGGED_IN)
			fork_and_exec(extra_cmd);
		free(extra_cmd);
	}
}

pid_t fork_and_exec(const char *command){
	int cmd_result;
	pid_t child = fork();
	switch(child){
	case 0:			// child
		cmd_result = system(command);
		if(cmd_result==-1) perror("fork_and_exec");
		if(cmd_result!=0){
			fprintf(stderr, "warning: system(%s) returned %d\n",
					command, cmd_result);
		}
		exit(cmd_result);
		break;
	case -1:
		perror("unable to fork in fork_and_exec");
		break;
	default: break;
	}
	return child;
}