Skip to content

Commit 33e2086

Browse files
committed
fix command parsed lengths
bump version differentiate PRIVMSG sent user nick by color change direct PRIVMSG to nick color to cyan add descriptive pointer names in parser() function invert direct PRIVMSG to nick color
1 parent b50c3b2 commit 33e2086

File tree

1 file changed

+46
-49
lines changed

1 file changed

+46
-49
lines changed

kirc.c

Lines changed: 46 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
#include <sys/wait.h>
1313
#include <termios.h>
1414

15-
#define IRC_MSG_MAX 512 /* guaranteed max message length */
16-
#define IRC_CHAN_MAX 200 /* gauranteed max channel length */
15+
#define MSG_MAX 512 /* guaranteed max message length */
16+
#define CHA_MAX 200 /* gauranteed max channel length */
1717

1818
static int conn; /* connection socket */
1919
static int verb = 0; /* verbose output (e.g. raw stream) */
@@ -26,17 +26,15 @@ static char * nick = NULL; /* nickname */
2626
static char * pass = NULL; /* server password */
2727
static char * user = NULL; /* server user name */
2828
static char * real = NULL; /* server user real name */
29-
static char * olog = NULL; /* log irc stream parh */
29+
static char * olog = NULL; /* log irc stream path */
3030

31-
/* append string to specified file path */
3231
static void
3332
printa(char *str) {
3433
FILE *out = fopen(olog, "a");
3534
fprintf(out, "%s", str);
3635
fclose(out);
3736
}
3837

39-
/* wait for keyboard press to interrupt stream */
4038
static int
4139
kbhit(void) {
4240

@@ -57,7 +55,6 @@ kbhit(void) {
5755
return byteswaiting > 0;
5856
}
5957

60-
/* handle keyboard strokes for command input */
6158
static void
6259
input_handler(char *usrin, int len) {
6360

@@ -71,15 +68,14 @@ input_handler(char *usrin, int len) {
7168
tcsetattr(STDIN_FILENO, TCSANOW, &save);
7269
}
7370

74-
/* send command to irc server */
7571
static void
7672
raw(char *fmt, ...) {
7773

7874
va_list ap;
79-
char *cmd_str = malloc(sizeof(char) * (IRC_MSG_MAX + 1));
75+
char *cmd_str = malloc(sizeof(char) * (MSG_MAX + 1));
8076

8177
va_start(ap, fmt);
82-
vsnprintf(cmd_str, IRC_MSG_MAX, fmt, ap);
78+
vsnprintf(cmd_str, MSG_MAX, fmt, ap);
8379
va_end(ap);
8480

8581
if (verb) printf("<< %s", cmd_str);
@@ -88,7 +84,6 @@ raw(char *fmt, ...) {
8884
write(conn, cmd_str, strlen(cmd_str));
8985
}
9086

91-
/* initial irc server connection */
9287
static void
9388
irc_init() {
9489

@@ -110,16 +105,15 @@ irc_init() {
110105
fcntl(conn, F_SETFL, O_NONBLOCK);
111106
}
112107

113-
/* print formatted irc stream with word wrap and hanging indent */
114108
static void
115109
printw(const char *format, ...) {
116110

117111
va_list argptr;
118-
char *tok, line[IRC_MSG_MAX + 1];
112+
char *tok, line[MSG_MAX + 1];
119113
int i, wordwidth, spaceleft, spacewidth = 1;
120114

121115
va_start(argptr, format);
122-
vsnprintf(line, IRC_MSG_MAX + 1, format, argptr);
116+
vsnprintf(line, MSG_MAX + 1, format, argptr);
123117
va_end(argptr);
124118

125119
if (olog) printa(line);
@@ -140,30 +134,34 @@ printw(const char *format, ...) {
140134
}
141135
}
142136

143-
/* parse irc stream */
144137
static void
145138
parser(char *in) {
146139

147-
if (verb) printf(">> %s\n", in);
140+
int len = 0;
141+
142+
if (verb) printf(">> %s\n", in);
148143
if (!strncmp(in, "PING", 4)) {
149144
in[1] = 'O';
150145
raw("%s\r\n", in);
151146
} else if (in[0] == ':') {
152-
char *pre = strtok(in, " ") + 1; /* prefix */
153-
char *cmd = strtok(NULL, ":"); /* command */
154-
char *msg = strtok(NULL, "\r"); /* message */
155-
char *nic = strtok(pre, "!"); //, *usr = strtok(NULL, "@"), *hos = pre;
156-
char *ltr = strtok(cmd, "#& "); //, *cha = cmd;
157-
if (!strncmp(ltr, "001", 3)) raw("JOIN #%s\r\n", chan);
158-
if (!strncmp(ltr, "QUIT", 4)) {
159-
printw("%*.*s \x1b[34;1m%s\x1b[0m\n", gutl, gutl, "<--", nic);
160-
} else if (!strncmp(ltr, "JOIN", 4)) {
161-
printw("%*.*s \x1b[32;1m%s\x1b[0m\n", gutl, gutl, "-->", nic);
147+
char *prefix = strtok(in, " ") + 1;
148+
char *suffix = strtok(NULL, ":");
149+
char *message = strtok(NULL, "\r");
150+
char *nickname = strtok(prefix, "!"); /* , *usr = strtok(NULL, "@"), *hos = prefix; */
151+
char *command = strtok(suffix, "#& "), *channel = strtok(NULL, " ");
152+
if (!strncmp(command, "001", 3)) raw("JOIN #%s\r\n", chan);
153+
else if (!strncmp(command, "QUIT", 4)) {
154+
printw("%*s \x1b[34;1m%s\x1b[0m\n", gutl, "<--", nickname);
155+
} else if (!strncmp(command, "JOIN", 4)) {
156+
printw("%*s \x1b[32;1m%s\x1b[0m\n", gutl, "-->", nickname);
157+
} else if (!strncmp(command, "PRIVMSG", 7) && !strncmp(channel, nick, strlen(nick))) {
158+
while (nickname[len] != '\0') len++;
159+
printw("%*s\x1b[43;1m%-.*s\x1b[0m %s\n", \
160+
gutl-(len <= gutl ? len : gutl), "", gutl, nickname, message);
162161
} else {
163-
int len = 0;
164-
while (nic[len] != '\0') len++;
162+
while (nickname[len] != '\0') len++;
165163
printw("%*s\x1b[33;1m%-.*s\x1b[0m %s\n", \
166-
gutl-(len <= gutl ? len : gutl), "", gutl, nic, msg);
164+
gutl-(len <= gutl ? len : gutl), "", gutl, nickname, message);
167165
}
168166
}
169167
}
@@ -175,19 +173,19 @@ main(int argc, char **argv) {
175173

176174
while ((cval = getopt(argc, argv, "s:p:o:n:k:c:u:r:w:W:vV")) != -1) {
177175
switch (cval) {
178-
case 'v' : puts("kirc 0.0.5"); return 0;
179-
case 'V' : verb = 1; break;
180-
case 's' : host = optarg; break;
176+
case 'v' : puts("kirc-0.0.6"); return 0;
177+
case 'V' : verb = 1; break;
178+
case 's' : host = optarg; break;
181179
case 'w' : gutl = atoi(optarg); break;
182180
case 'W' : cmax = atoi(optarg); break;
183-
case 'p' : port = optarg; break;
184-
case 'r' : real = optarg; break;
185-
case 'u' : user = optarg; break;
186-
case 'o' : olog = optarg; break;
187-
case 'n' : nick = optarg; break;
188-
case 'k' : pass = optarg; break;
189-
case 'c' : chan = optarg; break;
190-
case '?' : return 1;
181+
case 'p' : port = optarg; break;
182+
case 'r' : real = optarg; break;
183+
case 'u' : user = optarg; break;
184+
case 'o' : olog = optarg; break;
185+
case 'n' : nick = optarg; break;
186+
case 'k' : pass = optarg; break;
187+
case 'c' : chan = optarg; break;
188+
case '?' : return 1;
191189
}
192190
}
193191

@@ -205,45 +203,44 @@ main(int argc, char **argv) {
205203

206204
if (pid == 0) {
207205
int sl, i, o = 0;
208-
char u[IRC_MSG_MAX], s, b[IRC_MSG_MAX];
206+
char u[MSG_MAX], s, b[MSG_MAX];
209207

210208
irc_init();
211209

212210
while ((sl = read(conn, &s, 1))) {
213211
if (sl > 0) b[o] = s;
214212

215-
if ((o > 0 && b[o - 1] == '\r' && b[o] == '\n') || o == IRC_MSG_MAX) {
213+
if ((o > 0 && b[o - 1] == '\r' && b[o] == '\n') || o == MSG_MAX) {
216214
b[o + 1] = '\0';
217215
parser(b);
218216
o = 0;
219217
} else if (sl > 0) o++;
220218

221-
if (read(fd[0], u, IRC_MSG_MAX) > 0) {
219+
if (read(fd[0], u, MSG_MAX) > 0) {
222220
for (i = 0; u[i] != '\n'; i++) continue;
223221
if (u[0] != ':') raw("%-*.*s\r\n", i, i, u);
224222
}
225223
}
226224
}
227225
else {
228-
char usrin[IRC_MSG_MAX], v1[IRC_MSG_MAX-20], v2[20], c1;
226+
char usrin[MSG_MAX], v1[MSG_MAX - CHA_MAX], v2[CHA_MAX], c1;
229227

230228
while (waitpid(pid, NULL, WNOHANG) == 0) {
231229
if (!kbhit()) dprintf(fd[1], ":\n");
232230
else {
233-
input_handler(usrin, IRC_MSG_MAX);
231+
input_handler(usrin, MSG_MAX);
234232

235233
if (sscanf(usrin, ":%[M] %s %[^\n]\n", &c1, v2, v1) == 3 ||
236234
sscanf(usrin, ":%[Qnjpm] %[^\n]\n", &c1, v1) == 2 ||
237235
sscanf(usrin, ":%[q]\n", &c1) == 1) {
238236
switch (c1) {
239-
case 'q': dprintf(fd[1], "quit\n"); break;
240-
case 'Q': dprintf(fd[1], "quit %s\n", v1); break;
241-
case 'j': dprintf(fd[1], "join %s\n", v1); break;
242-
case 'p': dprintf(fd[1], "part %s\n", v1); break;
237+
case 'q': dprintf(fd[1], "quit\n"); break;
238+
case 'Q': dprintf(fd[1], "quit %s\n", v1); break;
239+
case 'j': dprintf(fd[1], "join %s\n", v1); break;
240+
case 'p': dprintf(fd[1], "part %s\n", v1); break;
243241
case 'm': dprintf(fd[1], "privmsg #%s :%s\n", chan, v1); break;
244242
case 'n': dprintf(fd[1], "privmsg nickserv :%s\n", v1); break;
245243
case 'M': dprintf(fd[1], "privmsg %s :%s\n", v2, v1); break;
246-
case '?': break;
247244
}
248245
} else dprintf(fd[1], "%s", usrin);
249246
}

0 commit comments

Comments
 (0)