7 #include <sys/socket.h>
8 #include <netinet/in.h>
10 #include <arpa/inet.h>
13 #include <libnotify/notify.h>
16 #define QUEUE_LENGTH 10
18 // a function to reap future child processes
19 void sigchld_handler(int s
)
21 while(waitpid(-1, NULL
, WNOHANG
) > 0);
24 // look for an IPv4 host:port in an address structure
25 void *get_in_addr(struct sockaddr
*sa
)
27 if (sa
->sa_family
== AF_INET
) {
28 return &(((struct sockaddr_in
*)sa
)->sin_addr
);
34 // new connections on activefd, always listening on sockfd
36 struct addrinfo hints
, *servinfo
, *port
;
37 struct sockaddr_storage their_addr
;
40 char source
[INET6_ADDRSTRLEN
];
44 memset(&hints
, 0, sizeof hints
);
45 hints
.ai_family
= AF_INET
;
46 hints
.ai_socktype
= SOCK_STREAM
;
47 hints
.ai_flags
= AI_PASSIVE
;
49 // look up local address information
50 if ((rv
= getaddrinfo(NULL
, PORT
, &hints
, &servinfo
)) != 0) {
51 fprintf(stderr
, "getaddrinfo: %s\n", gai_strerror(rv
));
55 // loop over available interfaces and try to bind to port
56 for (port
= servinfo
; port
!= NULL
; port
= port
->ai_next
) {
57 if ((sockfd
= socket(port
->ai_family
, port
->ai_socktype
,
58 port
->ai_protocol
)) == -1) {
59 perror("irssi-notify: socket");
62 if (setsockopt(sockfd
, SOL_SOCKET
, SO_REUSEADDR
, &yes
,
67 if (bind(sockfd
, port
->ai_addr
, port
->ai_addrlen
) == -1) {
69 perror("irssi-notify: bind");
76 fprintf(stderr
, "irssi-notify: failed to bind\n");
80 freeaddrinfo(servinfo
);
82 if (listen(sockfd
, QUEUE_LENGTH
) == -1) {
87 // bind sigchld_handler to reap future child processes
88 sa
.sa_handler
= sigchld_handler
;
89 sigemptyset(&sa
.sa_mask
);
90 sa
.sa_flags
= SA_RESTART
;
91 if (sigaction(SIGCHLD
, &sa
, NULL
) == -1) {
96 printf("irssi-notify: waiting for connections...\n");
99 sin_size
= sizeof their_addr
;
100 activefd
= accept(sockfd
, (struct sockaddr
*)&their_addr
, &sin_size
);
101 if (activefd
== -1) {
106 inet_ntop(their_addr
.ss_family
,
107 get_in_addr((struct sockaddr
*)&their_addr
),
108 source
, sizeof source
);
111 if (strcmp(source
, "50.16.219.8") != 0 &&
112 strcmp(source
, "127.0.0.1") != 0){
117 // start a child process to handle notifications
119 // the child process doesn't need the listener
122 if (recv(activefd
, buf
, 99, 0) == -1) {
127 printf("irssi-notify: recieved '%s'\n", buf
);
128 notify_init("irssi-notify");
129 NotifyNotification
* notification
=
130 notify_notification_new("", buf
, "irssi-notify");
131 notify_notification_show(notification
, NULL
);