From c946c46f1fd3c4b3c24136a111839577629823a2 Mon Sep 17 00:00:00 2001
From: Přemysl Janouch <p.janouch@gmail.com>
Date: Sun, 19 Apr 2015 21:34:11 +0200
Subject: degesch: implement /join and /part

---
 degesch.c | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 77 insertions(+), 2 deletions(-)

diff --git a/degesch.c b/degesch.c
index e017e8b..5bb6b7d 100644
--- a/degesch.c
+++ b/degesch.c
@@ -1987,6 +1987,77 @@ handle_command_quit (struct app_context *ctx, char *arguments)
 	initiate_quit (ctx);
 }
 
+static void
+handle_command_join (struct app_context *ctx, char *arguments)
+{
+	if (ctx->current_buffer->type == BUFFER_GLOBAL)
+	{
+		buffer_send_error (ctx, ctx->current_buffer,
+			"Can't join from a global buffer");
+		return;
+	}
+
+	if (ctx->irc_fd == -1)
+	{
+		buffer_send_error (ctx, ctx->server_buffer, "Not connected");
+		return;
+	}
+
+	if (*arguments)
+		// TODO: check if the arguments are in the form of
+		//   "channel(,channel)* key(,key)*"
+		irc_send (ctx, "JOIN %s", arguments);
+	else
+	{
+		if (ctx->current_buffer->type != BUFFER_CHANNEL)
+			buffer_send_error (ctx, ctx->current_buffer,
+				"%s: %s", "Can't join",
+				"no argument given and this buffer is not a channel");
+		// TODO: have a better way of checking if we're on the channel
+		else if (ctx->current_buffer->channel->users)
+			buffer_send_error (ctx, ctx->current_buffer,
+				"%s: %s", "Can't join",
+				"you already are on the channel");
+		else
+			// TODO: send the key if known
+			irc_send (ctx, "JOIN %s", ctx->current_buffer->channel->name);
+	}
+}
+
+static void
+handle_command_part (struct app_context *ctx, char *arguments)
+{
+	if (ctx->current_buffer->type == BUFFER_GLOBAL)
+	{
+		buffer_send_error (ctx, ctx->current_buffer,
+			"Can't part from a global buffer");
+		return;
+	}
+
+	if (ctx->irc_fd == -1)
+	{
+		buffer_send_error (ctx, ctx->server_buffer, "Not connected");
+		return;
+	}
+
+	if (*arguments)
+		// TODO: check if the arguments are in the form of "channel(,channel)*"
+		irc_send (ctx, "PART %s", arguments);
+	else
+	{
+		if (ctx->current_buffer->type != BUFFER_CHANNEL)
+			buffer_send_error (ctx, ctx->current_buffer,
+				"%s: %s", "Can't part",
+				"no argument given and this buffer is not a channel");
+		// TODO: have a better way of checking if we're on the channel
+		else if (!ctx->current_buffer->channel->users)
+			buffer_send_error (ctx, ctx->current_buffer,
+				"%s: %s", "Can't join", "you're not on the channel");
+		else
+			irc_send (ctx, "PART %s", ctx->current_buffer->channel->name);
+	}
+}
+
 static void
 handle_command_quote (struct app_context *ctx, char *arguments)
 {
@@ -2014,9 +2085,13 @@ g_command_handlers[] =
 	{ "notice",  NULL, "", "" },
 	{ "ctcp",    NULL, "", "" },
 	{ "me",      NULL, "", "" },
+#endif
 
-	{ "join",    NULL, "", "" },
-	{ "part",    NULL, "", "" },
+	{ "join",    handle_command_join,   "Join channels",
+	  "[channel...]" },
+	{ "part",    handle_command_part,   "Leave channels",
+	  "[channel...]" },
+#if 0
 	{ "cycle",   NULL, "", "" },
 
 	{ "mode",    NULL, "", "" },
-- 
cgit v1.2.3-70-g09d2