summary refs log tree commit diff
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--bounce.c9
-rw-r--r--bounce.h1
-rw-r--r--client.c10
3 files changed, 13 insertions, 7 deletions
diff --git a/bounce.c b/bounce.c
index 407dd44..c174249 100644
--- a/bounce.c
+++ b/bounce.c
@@ -228,7 +228,6 @@ int main(int argc, char *argv[]) {
 	const char *real = NULL;
 
 	const char *join = NULL;
-	const char *away = "pounced :3";
 	const char *quit = "connection reset by purr";
 
 	const char *Opts = "!A:C:H:K:NP:Q:U:W:a:c:ef:g:h:j:k:n:p:r:s:u:vw:x";
@@ -264,7 +263,7 @@ int main(int argc, char *argv[]) {
 	while (0 < (opt = getopt_config(argc, argv, Opts, LongOpts, NULL))) {
 		switch (opt) {
 			break; case '!': insecure = true;
-			break; case 'A': away = optarg;
+			break; case 'A': clientAway = optarg;
 			break; case 'C': strlcpy(certPath, optarg, sizeof(certPath));
 			break; case 'H': bindHost = optarg;
 			break; case 'K': strlcpy(privPath, optarg, sizeof(privPath));
@@ -322,6 +321,7 @@ int main(int argc, char *argv[]) {
 	}
 	if (!user) user = nick;
 	if (!real) real = nick;
+	if (!clientAway) clientAway = "pounced :3";
 
 	ringAlloc(ringSize);
 	if (savePath) saveLoad(savePath);
@@ -367,7 +367,7 @@ int main(int argc, char *argv[]) {
 	if (plain) explicit_bzero(plain, strlen(plain));
 
 	while (!stateReady()) serverRecv();
-	serverFormat("AWAY :%s\r\n", away);
+	serverFormat("AWAY :%s\r\n", clientAway);
 	if (join) serverFormat("JOIN :%s\r\n", join);
 
 	signal(SIGINT, signalHandler);
@@ -383,7 +383,6 @@ int main(int argc, char *argv[]) {
 	}
 	eventAdd(server, NULL);
 
-	size_t clients = 0;
 	for (;;) {
 		int nfds = poll(event.fds, event.len, -1);
 		if (nfds < 0 && errno != EINTR) err(EX_IOERR, "poll");
@@ -423,7 +422,6 @@ int main(int argc, char *argv[]) {
 					close(fd);
 				} else {
 					eventAdd(fd, clientAlloc(tls));
-					if (!clients++) serverFormat("AWAY\r\n");
 				}
 				continue;
 			}
@@ -434,7 +432,6 @@ int main(int argc, char *argv[]) {
 			if (clientError(client) || revents & (POLLHUP | POLLERR)) {
 				clientFree(client);
 				eventRemove(i);
-				if (!--clients) serverFormat("AWAY :%s\r\n", away);
 			}
 		}
 
diff --git a/bounce.h b/bounce.h
index 08313d5..2c997c3 100644
--- a/bounce.h
+++ b/bounce.h
@@ -80,6 +80,7 @@ void serverFormat(const char *format, ...)
 	__attribute__((format(printf, 1, 2)));
 
 char *clientPass;
+char *clientAway;
 struct Client *clientAlloc(struct tls *tls);
 void clientFree(struct Client *client);
 bool clientError(const struct Client *client);
diff --git a/client.c b/client.c
index 3e8b15b..32b5b0f 100644
--- a/client.c
+++ b/client.c
@@ -28,6 +28,8 @@
 
 #include "bounce.h"
 
+static size_t count;
+
 enum Need {
 	NeedNick = 1 << 0,
 	NeedUser = 1 << 1,
@@ -54,6 +56,9 @@ struct Client *clientAlloc(struct tls *tls) {
 }
 
 void clientFree(struct Client *client) {
+	if (!client->need) {
+		if (!--count) serverFormat("AWAY :%s\r\n", clientAway);
+	}
 	tls_close(client->tls);
 	tls_free(client->tls);
 	free(client);
@@ -100,7 +105,10 @@ static void passRequired(struct Client *client) {
 
 static void maybeSync(struct Client *client) {
 	if (client->need == NeedPass) passRequired(client);
-	if (!client->need) stateSync(client);
+	if (!client->need) {
+		stateSync(client);
+		if (!count++) serverFormat("AWAY\r\n");
+	}
 }
 
 typedef void Handler(struct Client *client, struct Message *msg);