Skip to content

Commit 5153fc9

Browse files
stefan11111emekoi
andauthored
some code improvements and a new feature (#137)
Co-authored-by: Emeka Nkurumeh <[email protected]>
1 parent 0f70e02 commit 5153fc9

File tree

4 files changed

+56
-22
lines changed

4 files changed

+56
-22
lines changed

Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ PREFIX ?= /usr/local
44
BINDIR = $(PREFIX)/bin
55
MANDIR = $(PREFIX)/share/man
66

7-
all: kirc.c kirc.h
8-
$(CC) $(CFLAGS) $(LDFLAGS) kirc.c -o kirc
7+
kirc: kirc.c kirc.h
8+
$(CC) $(CFLAGS) $(LDFLAGS) ${ALL_WARNING} kirc.c -o kirc
9+
all: kirc
910

1011
install: kirc
1112
mkdir -p $(DESTDIR)$(BINDIR)

kirc.1

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ received.
6363
.BI /<command>
6464
Send message to IRC host (e.g. /JOIN, /PART, /WHOIS, etc.)
6565
.TP
66+
.BI /setprivmsg <nick>
67+
Set privmsg target. Useful when sending privmsg's to a nick instead of a chan.
68+
.TP
6669
.BI // " <text>"
6770
Send text to current channel (useful when sending '@' and '/', usually to bots)
6871
.TP

kirc.c

Lines changed: 49 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@ static inline void state_reset(state l)
582582
history_add("");
583583
}
584584

585-
static char *ctime_now(char buf[26])
585+
static char *ctime_now(char *buf)
586586
{
587587
struct tm tm_;
588588
time_t now = time(NULL);
@@ -737,17 +737,13 @@ static short parse_dcc_send_message(const char *message, char *filename, unsigne
737737
{
738738
/* TODO: Fix horrible hacks */
739739

740-
char ipv6 = 0;
741-
742740
if (sscanf(message, "SEND \"%" STR(FNM_MAX) "[^\"]\" %41s %hu %zu", filename, ipv6_addr, port, file_size) == 4) {
743-
ipv6 = !!(ipv6_addr[15]);
744-
if (ipv6 == 1) {
741+
if (ipv6_addr[15]) {
745742
return 1;
746743
}
747744
}
748745
if (sscanf(message, "SEND %" STR(FNM_MAX) "s %41s %hu %zu", filename, ipv6_addr, port, file_size) == 4) {
749-
ipv6 = !!(ipv6_addr[15]);
750-
if (ipv6 == 1) {
746+
if (ipv6_addr[15]) {
751747
return 1;
752748
}
753749
}
@@ -773,15 +769,15 @@ static short parse_dcc_accept_message(const char *message, char *filename, unsig
773769
return 1;
774770
}
775771

776-
static void open_socket(int slot, int *file_fd)
772+
static void open_socket(int slot, int file_fd)
777773
{
778774
int sock_fd;
779775
if(!ipv6) {
780776
sock_fd = socket(AF_INET, SOCK_STREAM, 0);
781777
struct sockaddr_in sockaddr = dcc_sessions.slots[slot].sin;
782778
if (connect(sock_fd, (const struct sockaddr *)&sockaddr, sizeof(sockaddr)) < 0) {
783779
close(sock_fd);
784-
close(*file_fd);
780+
close(file_fd);
785781
perror("connect");
786782
return;
787783
}
@@ -791,20 +787,20 @@ static void open_socket(int slot, int *file_fd)
791787
struct sockaddr_in6 sockaddr = dcc_sessions.slots[slot].sin6;
792788
if (connect(sock_fd, (const struct sockaddr *)&sockaddr, sizeof(sockaddr)) < 0) {
793789
close(sock_fd);
794-
close(*file_fd);
790+
close(file_fd);
795791
perror("connect");
796792
return;
797793
}
798794
}
799795
if (sock_fd < 0) {
800-
close(*file_fd);
796+
close(file_fd);
801797
perror("socket");
802798
return;
803799
}
804800
int flags = fcntl(sock_fd, F_GETFL, 0) | O_NONBLOCK;
805801
if (flags < 0 || fcntl(sock_fd, F_SETFL, flags) < 0) {
806802
close(sock_fd);
807-
close(*file_fd);
803+
close(file_fd);
808804
perror("fcntl");
809805
return;
810806
}
@@ -819,7 +815,9 @@ static void handle_dcc(param p)
819815
size_t file_size = 0;
820816
unsigned int ip_addr = 0;
821817
unsigned short port = 0;
822-
char ipv6_addr[42];
818+
char ipv6_addr[INET6_ADDRSTRLEN];
819+
820+
memset(ipv6_addr, 0, sizeof(ipv6_addr));
823821

824822
int slot = -1;
825823
int file_fd;
@@ -876,6 +874,7 @@ static void handle_dcc(param p)
876874
.file_size = file_size,
877875
.file_fd = file_fd,
878876
};
877+
879878
strcpy(dcc_sessions.slots[slot].filename, filename);
880879
if(!ipv6) {
881880
dcc_sessions.slots[slot].sin = (struct sockaddr_in){
@@ -885,6 +884,7 @@ static void handle_dcc(param p)
885884
};
886885
goto check_resume;
887886
}
887+
888888
struct in6_addr result;
889889
if (inet_pton(AF_INET6, ipv6_addr, &result) != 1){
890890
close(file_fd);
@@ -901,9 +901,10 @@ static void handle_dcc(param p)
901901
p->nickname, filename, port, bytes_read);
902902
return;
903903
}
904-
open_socket(slot, &file_fd);
904+
open_socket(slot, file_fd);
905905
return;
906906
}
907+
907908
if (!strncmp(message, "ACCEPT", 6)) {
908909
if(parse_dcc_accept_message(message, filename, &port, &file_size)) {
909910
return;
@@ -916,7 +917,7 @@ static void handle_dcc(param p)
916917
}
917918

918919
file_fd = dcc_sessions.slots[slot].file_fd;
919-
open_socket(slot, &file_fd);
920+
open_socket(slot, file_fd);
920921
return;
921922
}
922923
}
@@ -931,7 +932,7 @@ static void handle_ctcp(param p)
931932
raw("NOTICE %s :\001VERSION kirc " VERSION "\001\r\n", p->nickname);
932933
return;
933934
}
934-
if (!strncmp(message, "TIME", 7)) {
935+
if (!strncmp(message, "TIME", 4)) {
935936
char buf[26];
936937
if (!ctime_now(buf)) {
937938
raw("NOTICE %s :\001TIME %s\001\r\n", p->nickname, buf);
@@ -983,6 +984,7 @@ static void param_print_private(param p)
983984
if (p->channel != NULL && (strcmp(p->channel, nick) == 0)) {
984985
handle_ctcp(p);
985986
printf("%*s\x1b[33;1m%-.*s [PRIVMSG]\x1b[36m ", s, "", p->nicklen, p->nickname);
987+
p->offset += sizeof(" [PRIVMSG]");
986988
} else if (p->channel != NULL && strcmp(p->channel + 1, chan)) {
987989
printf("%*s\x1b[33;1m%-.*s\x1b[0m", s, "", p->nicklen, p->nickname);
988990
printf(" [\x1b[33m%s\x1b[0m] ", p->channel);
@@ -1132,6 +1134,7 @@ static void join_command(state l)
11321134
raw("join #%s\r\n", chan);
11331135
printf("\x1b[35m%s\x1b[0m\r\n", l->buf);
11341136
printf("\x1b[35mJoined #%s!\x1b[0m\r\n", chan);
1137+
l->nick_privmsg = 0;
11351138
}
11361139

11371140
static void part_command(state l)
@@ -1178,16 +1181,29 @@ static void msg_command(state l)
11781181

11791182
static void action_command(state l)
11801183
{
1181-
char *tok;
1182-
strtok_r(l->buf + 7, " ", &tok);
11831184
int offset = 0;
11841185
while (*(l->buf + 7 + offset) == ' ') {
11851186
offset ++;
11861187
}
1188+
11871189
raw("privmsg #%s :\001ACTION %s\001\r\n", chan, l->buf + 7 + offset);
11881190
printf("\x1b[35mprivmsg #%s :ACTION %s\x1b[0m\r\n", chan, l->buf + 7 + offset);
11891191
}
11901192

1193+
static void set_privmsg_command(state l)
1194+
{
1195+
int offset = 0;
1196+
while (*(l->buf + 11 + offset) == ' ') {
1197+
offset ++;
1198+
}
1199+
1200+
strcpy(chan, l->buf + 11 + offset);
1201+
1202+
printf("\x1b[35mNew privmsg target: %s\x1b[0m\r\n", l->buf + 11 + offset);
1203+
l->nick_privmsg = 1;
1204+
}
1205+
1206+
11911207
static void nick_command(state l)
11921208
{
11931209
char *tok;
@@ -1238,8 +1254,14 @@ static void handle_user_input(state l)
12381254
action_command(l);
12391255
return;
12401256
}
1257+
if (!strncmp(l->buf + 1, "SETPRIVMSG", 10) || !strncmp(l->buf + 1, "setprivmsg", 10)) {
1258+
set_privmsg_command(l);
1259+
return;
1260+
}
1261+
12411262
if (l->buf[1] == '#') {
12421263
strcpy(chan, l->buf + 2);
1264+
l->nick_privmsg = 0;
12431265
printf("\x1b[35mnew channel: #%s\x1b[0m\r\n", chan);
12441266
return;
12451267
}
@@ -1257,8 +1279,14 @@ static void handle_user_input(state l)
12571279
printf("\x1b[35mprivmsg %s :ACTION %s\x1b[0m\r\n", l->buf + 2, tok);
12581280
return;
12591281
default: /* send private message to default channel */
1260-
raw("privmsg #%s :%s\r\n", chan, l->buf);
1261-
printf("\x1b[35mprivmsg #%s :%s\x1b[0m\r\n", chan, l->buf);
1282+
if(l->nick_privmsg == 0) {
1283+
raw("privmsg #%s :%s\r\n", chan, l->buf);
1284+
printf("\x1b[35mprivmsg #%s :%s\x1b[0m\r\n", chan, l->buf);
1285+
}
1286+
else {
1287+
raw("privmsg %s :%s\r\n", chan, l->buf);
1288+
printf("\x1b[35mprivmsg %s :%s\x1b[0m\r\n", chan, l->buf);
1289+
}
12621290
return;
12631291
}
12641292
}
@@ -1434,6 +1462,7 @@ int main(int argc, char **argv)
14341462
dcc_sessions.sock_fds[CON_MAX] = (struct pollfd){.fd = ttyinfd,.events = POLLIN};
14351463
dcc_sessions.sock_fds[CON_MAX + 1] = (struct pollfd){.fd = conn,.events = POLLIN};
14361464
state_t l;
1465+
memset(&l, 0, sizeof(l));
14371466
l.buflen = MSG_MAX;
14381467
state_reset(&l);
14391468
int rc, editReturnFlag = 0;

kirc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ typedef struct STATE {
102102
size_t lenu8; /* Current edited line length. */
103103
size_t cols; /* Number of columns in terminal. */
104104
int history_index; /* Current line in the edit history */
105+
char nick_privmsg; /* whether or not we are sending messages to a chan or nick */
105106
} state_t, *state;
106107

107108
struct abuf {

0 commit comments

Comments
 (0)