Skip to content

Commit cfd9b51

Browse files
committed
squash commits related to commands and stabilty
Update README add default command condition improved color scheme fix total column widtg remove leave/join channel since it does work for quit condition fix for WAIT_SIG being missread added command add additional commands :n, :j, and :l change bad cmd message Update README Update README Update README
1 parent 5e54e4f commit cfd9b51

File tree

2 files changed

+94
-50
lines changed

2 files changed

+94
-50
lines changed

README

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,23 @@ result is a portable <250 sloc application that has no dependencies other
1111
than a C99 compiler.
1212

1313

14-
TODO
15-
----
16-
17-
- [ ] capture CTRL-C for safer child process shutdown
18-
- [ ] better color default color scheme
19-
- [ ] dynamic word wrap based on column width
20-
- [ ] additional shortcut keys
21-
- [ ] fix default arrow key and backspace behavior
22-
23-
2414
FEATURES
2515
--------
2616

2717
- automatic host PING response.
18+
- automatic word wrapping based on default (or user defined) widths.
2819
- vi-like shortcuts:
2920

30-
:m <message> send a message to the connected channel
31-
:q close the host connection and quit kirc
21+
:m <message> send a message to the current channel
22+
:M <nick|channel> <message> send a message to a specified nick or channel
23+
:n <message> send a message to NickServ
24+
:j <channel> join a specified channel
25+
:p <channel> leave a specified channel
26+
:q close the host connection and quit kirc
27+
28+
- Color scheme definition via ANSI 8-bit colors [1]. Therefore, one could
29+
theoretically achieve uniform color definition across all shell applications
30+
and tools.
3231

3332

3433
INSTALLATION
@@ -45,11 +44,36 @@ Building and installing from source:
4544
USAGE
4645
-----
4746

48-
usage: kirc [-s hostname] [-p port] [-c channel] [-n nick] [-k password] [-v|V]
47+
usage: kirc [-s hostname] [-p port] [-c channel] [-n nick] [-k password] [-w
48+
columns] [-W columns] [-v|V]
4949
-s server address (default: 'irc.freenode.org')
5050
-p server port (default: '6667')
5151
-c channel name (default: '#kisslinux')
5252
-n user nickname
5353
-k user password
5454
-v version information
5555
-V verbose output (e.g. raw stream)
56+
-w maximum width of the printed left column (default: '10')
57+
-W maximum width of the entire printed stream (default '82')
58+
59+
60+
CUSTOMIZATION
61+
-------------
62+
63+
- Regarding the behavior of the '-W' and '-w' arguments, these are intended to
64+
allow the user to customize the "look abd feel" of the IRC stream. By fault,
65+
kirc will only print the first 10 characters (as defined by the '-w' argument)
66+
of any given nick. this also sets the maximum character and word wrapping
67+
width of the printed message. For example, any printed character or word
68+
exceeding the defined '-W' argument will automatically be printed to a new
69+
line, offset by '-w' plus 2 characters (which are the ":" and " " printed
70+
after the nick in the left column). Therefore, assuming that the default
71+
values are being used, an IRC message could never exceed 80 printed characters
72+
before word wrapping.
73+
74+
75+
REFERENCES
76+
----------
77+
78+
[0] https://tools.ietf.org/html/rfc2812
79+
[1] https://en.wikipedia.org/wiki/ANSI_escape_code

kirc.c

Lines changed: 57 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
#include <termios.h>
1313

1414
#define BUFF 512 /* buffer size (see RFC 2812) */
15-
#define CMAX 92 /* max number of columns */
16-
#define GUTL 10 /* left gutter width and alignment */
1715

1816
static int conn; /* socket connection */
1917
static char sbuf[BUFF]; /* string buffer */
2018
static int verb = 0; /* verbose output (e.g. raw stream) */
19+
static int cmax = 82;
20+
static int gutl = 10;
2121
static char *host = "irc.freenode.org"; /* irc host address */
2222
static char *chan = "kisslinux"; /* channel */
2323
static char *port = "6667"; /* port */
@@ -55,7 +55,7 @@ raw(char *fmt, ...) {
5555
vsnprintf(sbuf, BUFF, fmt, ap);
5656
va_end(ap);
5757

58-
if (verb) printf("<< \x1b[33m%s\x1b[0m", sbuf);
58+
if (verb) printf("<< %s", sbuf);
5959

6060
write(conn, sbuf, strlen(sbuf));
6161
}
@@ -82,32 +82,33 @@ con(void) {
8282
static void
8383
printw(const char *format, ...) {
8484

85-
int s1 = 0, s2, i, o;
85+
int s1 = 0, s2, len, i;
8686
va_list argptr;
8787
char line[BUFF + 1];
8888

8989
va_start(argptr, format);
9090
vsnprintf(line, BUFF + 1, format, argptr);
9191
va_end(argptr);
9292

93-
if (strlen(line) <= CMAX) printf("%s", line);
94-
else if (strlen(line) > CMAX) {
93+
len = strlen(line);
94+
if (len <= cmax + gutl) printf("%s", &line[0]);
95+
else if (len > cmax + gutl) {
9596

96-
for (i = 0; i < CMAX; i++) {
97-
if (line[i] == ' ') s1 = i;
98-
if (i == CMAX - 1) printf("%-*.*s\n", s1, s1, line);
97+
for (i = gutl; i < cmax + gutl; i++) {
98+
if (isspace(line[i])) s1 = i;
99+
if (i == cmax + gutl - 1) printf("%-*.*s\n", s1, s1, &line[0]);
99100
}
100101

101-
s2 = o = s1;
102+
s2 = s1;
102103

103104
for (i = s1; line[i] != '\0'; i++) {
104-
if (line[i] == ' ') s2 = i;
105-
if ((i - o) == (CMAX - GUTL)) {
106-
printf("%*s %-*.*s\n", GUTL, " ", s2 - o, s2 - o, &line[o + 1]);
107-
o = i = s2;
105+
if (isspace(line[i])) s2 = i;
106+
if ((i - s1) == (cmax - gutl)) {
107+
printf("%*s %-*.*s\n", gutl, "", s2 - s1, s2 - s1, &line[s1 + 1]);
108+
s1 = s2;
108109
}
109110
else if (line[i + 1] == '\0') {
110-
printf("%*s %-*.*s", GUTL, " ", i - o, i - o, &line[o + 1]);
111+
printf("%*s %-*.*s", gutl, "", i - s1, i - s1, &line[s1 + 1]);
111112
}
112113
}
113114
}
@@ -128,7 +129,7 @@ pars(int sl, char *buf) {
128129
buf_c[o + 1] = '\0';
129130
o = -1;
130131

131-
if (verb) printf(">> \x1b[33m%s\x1b[0m", buf_c);
132+
if (verb) printf(">> %s", buf_c);
132133

133134
if (!strncmp(buf_c, "PING", 4)) {
134135
buf_c[1] = 'O';
@@ -143,15 +144,13 @@ pars(int sl, char *buf) {
143144
if (!strncmp(ltr, "001", 3)) raw("JOIN #%s\r\n", chan);
144145

145146
if (!strncmp(ltr, "QUIT", 4)) {
146-
printw("%*.*s \x1b[34;1m%s\x1b[0m left %s\n", \
147-
GUTL, GUTL, "<--", nic, cha);
147+
printw("%*.*s \x1b[34;1m%s\x1b[0m\n", gutl, gutl, "<--", nic);
148148
}
149149
else if (!strncmp(ltr, "JOIN", 4)) {
150-
printw("%*.*s \x1b[32;1m%s\x1b[0m joined %s\n", \
151-
GUTL, GUTL, "-->", nic, cha);
150+
printw("%*.*s \x1b[32;1m%s\x1b[0m\n", gutl, gutl, "-->", nic);
152151
}
153152
else {
154-
printw("\x1b[1m%*.*s\x1b[0m %s\n", GUTL, GUTL, nic, msg);
153+
printw("\x1b[33;1m%*.*s\x1b[0m %s\n", gutl, gutl, nic, msg);
155154
}
156155
}
157156
}
@@ -163,11 +162,14 @@ main(int argc, char **argv) {
163162

164163
int fd[2], cval;
165164

166-
while ((cval = getopt(argc, argv, "s:p:n:k:c:vV")) != -1) {
165+
166+
while ((cval = getopt(argc, argv, "s:p:n:k:c:w:W:vV")) != -1) {
167167
switch (cval) {
168168
case 'v' : puts("kirc 0.0.1"); break;
169169
case 'V' : verb = 1; break;
170170
case 's' : host = optarg; break;
171+
case 'w' : gutl = atoi(optarg); break;
172+
case 'W' : cmax = atoi(optarg); break;
171173
case 'p' : port = optarg; break;
172174
case 'n' : nick = optarg; break;
173175
case 'k' : pass = optarg; break;
@@ -189,46 +191,64 @@ main(int argc, char **argv) {
189191
pid_t pid = fork();
190192

191193
if (pid == 0) {
192-
int sl, i, j;
193-
char u[CMAX];
194+
int sl, i;
195+
char u[cmax];
194196

195197
con();
196198

197199
while ((sl = read(conn, sbuf, BUFF))) {
198200
pars(sl, sbuf);
199-
if (read(fd[0], u, CMAX) > 0) {
201+
if (read(fd[0], u, cmax) > 0) {
200202
for (i = 0; u[i] != '\n'; i++) continue;
201-
for (j = i; j < CMAX; j++) u[j] = ' ';
202-
if (!strstr(u, "WAIT_SIG")) raw("%-*.*s\r\n", i, i, u);
203+
if (u[0] != ':') raw("%-*.*s\r\n", i, i, u);
203204
}
204205
}
205-
printf("(press <ENTER> to quit)\n");
206+
printf("%*s \x1b[31mpress <RETURN> key to quit...\x1b[0m", gutl, " ");
206207
}
207208
else {
208-
char usrin[CMAX];
209+
char usrin[cmax], val1[cmax], val2[20];
209210

210211
while (waitpid(pid, NULL, WNOHANG) == 0) {
211212
while (!kbhit() && waitpid(pid, NULL, WNOHANG) == 0) {
212-
write(fd[1], "WAIT_SIG", CMAX);
213+
dprintf(fd[1], ":\n");
213214
}
214215

215-
fgets(usrin, CMAX, stdin);
216-
217-
if (usrin[0] == ':' && usrin[1]) {
218-
char *cmd_val = &usrin[2];
216+
fgets(usrin, cmax, stdin);
219217

218+
if (usrin[0] == ':') {
220219
switch (usrin[1]) {
221220
case 'q':
222221
dprintf(fd[1],"quit\n");
223222
break;
224223
case 'm':
225-
while (isspace(*cmd_val)) cmd_val++;
226-
dprintf(fd[1], "privmsg #%s :%s", chan, cmd_val);
224+
sscanf(usrin, ":m %[^\n]\n", val1);
225+
dprintf(fd[1], "privmsg #%s :%s\n", chan, val1);
226+
break;
227+
case 'n':
228+
sscanf(usrin, ":n %[^\n]\n", val1);
229+
dprintf(fd[1], "privmsg nickserv :%s\n", val1);
230+
break;
231+
case 'j':
232+
sscanf(usrin, ":j %[^\n]\n", val1);
233+
dprintf(fd[1], "join %s\n", val1);
234+
break;
235+
case 'p':
236+
sscanf(usrin, ":p %[^\n]\n", val1);
237+
dprintf(fd[1], "part %s\n", val1);
238+
break;
239+
case 'M':
240+
sscanf(usrin, ":M %s %[^\n]\n", val2, val1);
241+
dprintf(fd[1], "privmsg %s :%s\n", val2, val1);
242+
break;
243+
case '\n':
244+
break;
245+
default:
246+
printf("\x1b[31munknown ':%c' \x1b[0m\n", usrin[1]);
227247
break;
228248
}
229249
}
230250
else {
231-
write(fd[1], usrin, CMAX);
251+
dprintf(fd[1], "%s", usrin);
232252
}
233253
fflush(stdout);
234254
}

0 commit comments

Comments
 (0)