Skip to content

Commit 56da0af

Browse files
authored
Merge pull request #11 from mcpcpc/0.0.7
0.0.7
2 parents baca85c + ca81e18 commit 56da0af

File tree

2 files changed

+36
-53
lines changed

2 files changed

+36
-53
lines changed

README

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ FEATURES
3333
- automatic host PING response.
3434
- vi-like shortcuts:
3535

36-
:m <message> send a message to the current channel
37-
:M <nick|channel> <message> send a message to a specified nick or channel
38-
:n <message> send a message to NickServ
39-
:j <channel> join a specified channel
40-
:p <channel> leave a specified channel
41-
:Q <message> send a message and close the host connection
42-
:q close the host connection
36+
<message> send a message to the current channel
37+
/m <nick|channel> <message> send a message to a specified nick or channel
38+
/n <message> send a message to NickServ
39+
/j <channel> join a specified channel
40+
/p <channel> leave (part) a specified channel
41+
/Q <message> send a message and close the host connection
42+
/q close the host connection
4343

4444
- automatic word wrapping using the greedy algorithm.
4545
- color scheme definition via ANSI 8-bit colors [1]. Therefore, one could

kirc.c

Lines changed: 29 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
#define CHA_MAX 200 /* gauranteed max channel length */
1717

1818
static int conn; /* connection socket */
19-
static int verb = 0; /* verbose output (e.g. raw stream) */
20-
static int cmax = 80; /* max number of chars per line */
21-
static int gutl = 10; /* max char width of left column */
19+
static size_t verb = 0; /* verbose output (e.g. raw stream) */
20+
static size_t cmax = 80; /* max number of chars per line */
21+
static size_t gutl = 10; /* max char width of left column */
2222
static char * host = "irc.freenode.org"; /* irc host address */
2323
static char * chan = "kisslinux"; /* channel */
2424
static char * port = "6667"; /* server port */
@@ -39,49 +39,30 @@ static int
3939
kbhit(void) {
4040

4141
int byteswaiting;
42-
struct termios term;
43-
tcgetattr(0, &term);
4442
fd_set fds;
4543
struct timespec ts = {0};
46-
struct termios term2 = term;
4744

48-
term2.c_lflag &= ~ICANON;
49-
tcsetattr(0, TCSANOW, &term2);
5045
FD_ZERO(&fds);
5146
FD_SET(0, &fds);
5247
byteswaiting = pselect(1, &fds, NULL, NULL, &ts, NULL);
53-
tcsetattr(0, TCSANOW, &term);
5448

5549
return byteswaiting > 0;
5650
}
5751

58-
static void
59-
input_handler(char *usrin, int len) {
60-
61-
struct termios tp, save;
62-
63-
tcgetattr(STDIN_FILENO, &tp);
64-
save = tp;
65-
tp.c_cc[VERASE] = 127;
66-
tcsetattr(STDIN_FILENO, TCSANOW, &tp);
67-
fgets(usrin, len, stdin);
68-
tcsetattr(STDIN_FILENO, TCSANOW, &save);
69-
}
70-
7152
static void
7253
raw(char *fmt, ...) {
7354

7455
va_list ap;
75-
char *cmd_str = malloc(sizeof(char) * (MSG_MAX + 1));
56+
char *cmd_str = malloc(MSG_MAX);
7657

7758
va_start(ap, fmt);
7859
vsnprintf(cmd_str, MSG_MAX, fmt, ap);
7960
va_end(ap);
8061

8162
if (verb) printf("<< %s", cmd_str);
8263
if (olog) printa(cmd_str);
64+
if (write(conn, cmd_str, strlen(cmd_str)) < 0) exit(1);
8365

84-
write(conn, cmd_str, strlen(cmd_str));
8566
free(cmd_str);
8667
}
8768

@@ -110,11 +91,11 @@ static void
11091
printw(const char *format, ...) {
11192

11293
va_list argptr;
113-
char *tok, line[MSG_MAX + 1];
114-
int i, wordwidth, spaceleft, spacewidth = 1;
94+
char *tok, line[MSG_MAX];
95+
size_t i, wordwidth, spaceleft, spacewidth = 1;
11596

11697
va_start(argptr, format);
117-
vsnprintf(line, MSG_MAX + 1, format, argptr);
98+
vsnprintf(line, MSG_MAX, format, argptr);
11899
va_end(argptr);
119100

120101
if (olog) printa(line);
@@ -126,7 +107,7 @@ printw(const char *format, ...) {
126107
for(tok = strtok(&line[i], " "); tok != NULL; tok = strtok(NULL, " ")) {
127108
wordwidth = strlen(tok);
128109
if ((wordwidth + spacewidth) > spaceleft) {
129-
printf("\n%*.s%s", gutl + 2, "", tok);
110+
printf("\n%*.s%s", (int) gutl + 2, "", tok);
130111
spaceleft = cmax - (gutl + 2 + wordwidth);
131112
} else {
132113
printf(" %s", tok);
@@ -136,10 +117,7 @@ printw(const char *format, ...) {
136117
}
137118

138119
static void
139-
parser(char *in) {
140-
141-
int len = 0;
142-
120+
raw_parser(char *in) {
143121
if (verb) printf(">> %s\n", in);
144122
if (!strncmp(in, "PING", 4)) {
145123
in[1] = 'O';
@@ -156,11 +134,11 @@ parser(char *in) {
156134
} else if (!strncmp(command, "JOIN", 4)) {
157135
printw("%*s \x1b[32;1m%s\x1b[0m\n", gutl, "-->", nickname);
158136
} else if (!strncmp(command, "PRIVMSG", 7) && !strncmp(channel, nick, strlen(nick))) {
159-
while (nickname[len] != '\0') len++;
137+
size_t len = strlen(nickname);
160138
printw("%*s\x1b[43;1m%-.*s\x1b[0m %s\n", \
161139
gutl-(len <= gutl ? len : gutl), "", gutl, nickname, message);
162140
} else {
163-
while (nickname[len] != '\0') len++;
141+
size_t len = strlen(nickname);
164142
printw("%*s\x1b[33;1m%-.*s\x1b[0m %s\n", \
165143
gutl-(len <= gutl ? len : gutl), "", gutl, nickname, message);
166144
}
@@ -174,7 +152,7 @@ main(int argc, char **argv) {
174152

175153
while ((cval = getopt(argc, argv, "s:p:o:n:k:c:u:r:w:W:vV")) != -1) {
176154
switch (cval) {
177-
case 'v' : puts("kirc-0.0.6"); return 0;
155+
case 'v' : puts("kirc-0.0.7"); return 0;
178156
case 'V' : verb = 1; break;
179157
case 's' : host = optarg; break;
180158
case 'w' : gutl = atoi(optarg); break;
@@ -213,41 +191,46 @@ main(int argc, char **argv) {
213191

214192
if ((o > 0 && b[o - 1] == '\r' && b[o] == '\n') || o == MSG_MAX) {
215193
b[o + 1] = '\0';
216-
parser(b);
194+
raw_parser(b);
217195
o = 0;
218196
} else if (sl > 0) o++;
219197

220198
if (read(fd[0], u, MSG_MAX) > 0) {
221199
for (i = 0; u[i] != '\n'; i++) continue;
222-
if (u[0] != ':') raw("%-*.*s\r\n", i, i, u);
200+
if (u[0] != '/') raw("%-*.*s\r\n", i, i, u);
223201
}
224202
}
225203
}
226204
else {
227205
char usrin[MSG_MAX], v1[MSG_MAX - CHA_MAX], v2[CHA_MAX], c1;
206+
struct termios tp, save;
207+
tcgetattr(STDIN_FILENO, &tp);
208+
save = tp;
209+
tp.c_cc[VERASE] = 127;
228210

229211
while (waitpid(pid, NULL, WNOHANG) == 0) {
230-
if (!kbhit()) dprintf(fd[1], ":\n");
212+
if (!kbhit()) dprintf(fd[1], "/\n");
231213
else {
232-
input_handler(usrin, MSG_MAX);
214+
tcsetattr(STDIN_FILENO, TCSANOW, &tp);
215+
if (fgets(usrin, MSG_MAX, stdin) == NULL) return 1;
216+
tcsetattr(STDIN_FILENO, TCSANOW, &save);
233217

234-
if (sscanf(usrin, ":%[M] %s %[^\n]\n", &c1, v2, v1) == 3 ||
235-
sscanf(usrin, ":%[Qnjpm] %[^\n]\n", &c1, v1) == 2 ||
236-
sscanf(usrin, ":%[q]\n", &c1) == 1) {
218+
if (sscanf(usrin, "/%[m] %s %[^\n]\n", &c1, v2, v1) == 3 ||
219+
sscanf(usrin, "/%[Qnjp] %[^\n]\n", &c1, v1) == 2 ||
220+
sscanf(usrin, "/%[q]\n", &c1) == 1) {
237221
switch (c1) {
238222
case 'q': dprintf(fd[1], "quit\n"); break;
239223
case 'Q': dprintf(fd[1], "quit %s\n", v1); break;
240224
case 'j': dprintf(fd[1], "join %s\n", v1); break;
241225
case 'p': dprintf(fd[1], "part %s\n", v1); break;
242-
case 'm': dprintf(fd[1], "privmsg #%s :%s\n", chan, v1); break;
243226
case 'n': dprintf(fd[1], "privmsg nickserv :%s\n", v1); break;
244-
case 'M': dprintf(fd[1], "privmsg %s :%s\n", v2, v1); break;
227+
case 'm': dprintf(fd[1], "privmsg %s :%s\n", v2, v1); break;
245228
}
246-
} else dprintf(fd[1], "%s", usrin);
229+
} else dprintf(fd[1], "privmsg #%s :%s", chan, usrin);
247230
}
248231
}
249232

250-
fprintf(stderr, "%*s <<irc server connection closed>>\n", gutl, "");
233+
fprintf(stderr, "<< irc server connection closed\n");
251234
}
252235
return 0;
253236
}

0 commit comments

Comments
 (0)