Skip to content

Commit 87a59b5

Browse files
committed
simplified commands
1 parent f068d65 commit 87a59b5

File tree

2 files changed

+23
-47
lines changed

2 files changed

+23
-47
lines changed

README.md

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,10 @@ usage: kirc [-s hostname] [-p port] [-c channel] [-n nick] [-r real name] [-u us
4545
* Simple commands and full support for all IRC commands in the [RFC 2812](https://tools.ietf.org/html/rfc2812) standard:
4646

4747
```shell
48-
<message> Send a message to the current channel.
49-
/m <nick|channel> <message> Send a message to a specified channel or nick.
50-
/M <message> Send a message to NickServ.
51-
/Q <message> Send a message and close the host connection.
52-
/x <message> Send a message directly to the server.
53-
/j <channel> Join a specified channel.
54-
/p <channel> Leave (part) a specified channel.
55-
/u <channel> Assign new default message channel.
56-
/n List all users on the current channel.
57-
/q Close the host connection.
58-
/h Print a list of available kirc commands.
48+
<message> Send a PRIVMSG to the current channel.
49+
/<command> Send command to IRC server.
50+
/#<channel> Assign new default message channel.
51+
/? Print current message channel.
5952
```
6053

6154
* Color scheme definition via [ANSI 8-bit colors](https://en.wikipedia.org/wiki/ANSI_escape_code). Therefore, one could theoretically achieve uniform color definition across all shell applications and tools.

kirc.c

Lines changed: 19 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/* See LICENSE file for license details. */
22
#define _POSIX_C_SOURCE 200809L
3-
#include <ctype.h>
43
#include <stdarg.h>
54
#include <netdb.h>
65
#include <stdio.h>
@@ -18,21 +17,8 @@
1817
#define CHA_MAX 200 /* guaranteed max channel length */
1918
#define VERSION "0.1.0" /* software version */
2019
#define USAGE "kirc [-s hostname] [-p port] [-c channel] [-n nick] \
21-
[-r real name] [-u username] [-k password] [-x init command] [-w columns] \
22-
[-W columns] [-o path] [-h|v|V]"
23-
#define HELP "\
24-
<message> Send a message to the current channel.\n\
25-
/m <nick|channel> <message> Send a message to a specified channel or nick.\n\
26-
/M <message> Send a message to NickServ.\n\
27-
/Q <message> Send a message and close the host connection.\n\
28-
/x <message> Send a message directly to the server.\n\
29-
/j <channel> Join a specified channel.\n\
30-
/p <channel> Leave (part) a specified channel.\n\
31-
/u <channel> Assign new default message channel.\n\
32-
/n List all users on the current channel.\n\
33-
/q Close the host connection.\n\
34-
/h Print a list of available kirc commands."
35-
20+
[-r real_name] [-u username] [-k password] [-a token] [-x init_command] \
21+
[-w columns] [-W columns] [-o path] [-h|v|V]"
3622

3723
static int conn; /* connection socket */
3824
static int verb = 0; /* verbose output (e.g. raw stream) */
@@ -143,7 +129,7 @@ printw(const char *format, ...) {
143129

144130
if (olog) log_append(line, olog);
145131

146-
for (i = 0; isspace(line[i]); ++i) putchar(line[i]);
132+
for (i = 0; line[i] == ' '; ++i) putchar(line[i]);
147133

148134
spaceleft = cmax + gutl - (i - 1);
149135

@@ -176,7 +162,7 @@ raw_parser(char *usrin) {
176162
if (!strncmp(usrin, "AUTHENTICATE +", 14)) {
177163
raw("AUTHENTICATE %s\r\n", auth);
178164
return;
179-
}
165+
}
180166

181167
if (usrin[0] != ':') return;
182168

@@ -244,28 +230,25 @@ handle_server_message(void) {
244230
static void
245231
handle_user_input(void) {
246232

247-
char usrin[MSG_MAX], v1[MSG_MAX - CHA_MAX], v2[CHA_MAX], c1;
233+
char usrin[MSG_MAX];
248234

249235
fgets(usrin, MSG_MAX, stdin);
250-
if (sscanf(usrin, "/%[m] %s %[^\n]\n", &c1, v2, v1) > 2 ||
251-
sscanf(usrin, "/%[a-zA-Z] %[^\n]\n", &c1, v1) > 0) {
252-
switch (c1) {
253-
case 'x': raw("%s\r\n", v1); break;
254-
case 'q': raw("quit\r\n"); break;
255-
case 'Q': raw("quit %s\r\n", v1); break;
256-
case 'j': raw("join %s\r\n", v1); break;
257-
case 'p': raw("part %s\r\n", v1); break;
258-
case 'n': raw("names #%s\r\n", chan); break;
259-
case 'M': raw("privmsg nickserv :%s\r\n", v1); break;
260-
case 'm': raw("privmsg %s :%s\r\n", v2, v1); break;
261-
case 'u': strcpy(chan, v1); break;
262-
default : puts(HELP); break;
236+
237+
size_t msg_len = strlen(usrin);
238+
if (usrin[msg_len - 1] == '\n') {
239+
usrin[msg_len - 1] = '\0';
240+
}
241+
242+
if (usrin[0] == '/') {
243+
if (usrin[1] == '#') {
244+
strcpy(chan, usrin + 2);
245+
printf("new channel: #%s\n", chan);
246+
} else if (usrin[1] == '?' && msg_len == 3) {
247+
printf("current channel: #%s\n", chan);
248+
} else {
249+
raw("%s\r\n", usrin + 1);
263250
}
264251
} else {
265-
size_t msg_len = strlen(usrin);
266-
if (usrin[msg_len - 1] == '\n') {
267-
usrin[msg_len - 1] = '\0';
268-
}
269252
raw("privmsg #%s :%s\r\n", chan, usrin);
270253
}
271254
}

0 commit comments

Comments
 (0)