Compare commits
72 Commits
Author | SHA1 | Date | |
---|---|---|---|
a665c369e3 | |||
f7b307bb34 | |||
|
058547e707 | ||
|
68c1c4e511 | ||
|
f2c5daa9fe | ||
|
81533f966e | ||
|
b188c78432 | ||
|
d9bda20849 | ||
|
df4c061136 | ||
|
ec293427a0 | ||
|
9bb34de449 | ||
|
7f0141bbe9 | ||
|
6ae3c37e30 | ||
|
6d4fd01fc1 | ||
|
6703fe4592 | ||
|
5eaa0fff0d | ||
|
f87d3f5bef | ||
|
7cfa52d6eb | ||
|
16c1c2df60 | ||
|
fe525b3209 | ||
|
a28f8b49c8 | ||
|
bade9ccb6c | ||
|
2831ca36c3 | ||
|
3b5d848cd4 | ||
|
efb5435694 | ||
|
fdaf2adbc9 | ||
|
a4ba8e3b08 | ||
|
6af1b812ae | ||
|
75d42255f2 | ||
|
dad75aa6ba | ||
|
d78d10d1a1 | ||
|
1a4256ef71 | ||
|
9c7027fcbf | ||
|
7f1961d4c6 | ||
|
2b85380411 | ||
|
14e430ac5b | ||
|
261dc71e58 | ||
|
4a4f924fee | ||
|
b3ed5f414f | ||
|
ca6ba9a64a | ||
|
f3827eec2a | ||
|
8b2eeee541 | ||
|
2a372e2df1 | ||
|
888ab22725 | ||
|
8a58b25cb7 | ||
|
589cce35c5 | ||
|
5463bed7ed | ||
|
aa40ba1f3d | ||
|
ef99e720ff | ||
|
f9f6f8a19c | ||
|
82946e102d | ||
|
7ef7529687 | ||
|
bcda402396 | ||
|
1160be7ffa | ||
|
75f0af7c42 | ||
|
81238e16fe | ||
|
60fc462aa9 | ||
|
9b093ac5c6 | ||
|
264f8e9422 | ||
|
fe1e3560be | ||
|
1edd94af86 | ||
|
a3abf3b3ef | ||
|
e3cbbe71c2 | ||
|
93842a5fe0 | ||
|
1f1228547c | ||
|
ea71b6e88e | ||
|
cb68ce9e2c | ||
|
5569a14e8e | ||
|
2e945e48fb | ||
|
a63ccb4600 | ||
|
0a903a0c79 | ||
|
8edb0e23f4 |
21
.editorconfig
Normal file
21
.editorconfig
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
root = true
|
||||||
|
|
||||||
|
[*]
|
||||||
|
indent_style = space
|
||||||
|
indent_size = 4
|
||||||
|
end_of_line = lf
|
||||||
|
charset = utf-8
|
||||||
|
trim_trailing_whitespace = true
|
||||||
|
insert_final_newline = true
|
||||||
|
|
||||||
|
[{*.c,*.h}]
|
||||||
|
indent_style = tab
|
||||||
|
indent_size = 4
|
||||||
|
|
||||||
|
[*.md]
|
||||||
|
trim_trailing_whitespace = false
|
||||||
|
indent_style = tab
|
||||||
|
|
||||||
|
[Makefile]
|
||||||
|
indent_style = tab
|
||||||
|
indent_size = 4
|
6
.hgtags
6
.hgtags
|
@ -1,6 +0,0 @@
|
||||||
de32c537aaf66554894712563ffba8d9bc4c2714 0.1
|
|
||||||
56350a01f27753cfbdbb3dbc25f2a53dd4c2ac45 0.2
|
|
||||||
d77f00af559258679a0fad5d264685d663e6975a 0.3
|
|
||||||
a3549fb4c72ff0edb816c8c29be7ff289db5b003 0.4
|
|
||||||
70d49a37b35695f2f771bddaf309f05ea60af8bc 0.5
|
|
||||||
d7923d9e717c1c6f1ed3b17ec90bfdd7e7bfcca0 0.6
|
|
7
LICENSE
7
LICENSE
|
@ -1,7 +1,10 @@
|
||||||
MIT/X Consortium License
|
MIT/X Consortium License
|
||||||
|
|
||||||
(C)opyright MMV-MMVI Anselm R. Garbe <garbeam@gmail.com>
|
© 2022 Alexander Popov <iiiypuk@fastmail.fm>
|
||||||
(C)opyright MMV Nico Golde <nico at ngolde dot de>
|
© 2005-2017 Anselm R Garbe <anselm@garbe.us>
|
||||||
|
© 2008-2009 Jeroen Schot <schot@a-eskwadraat.nl>
|
||||||
|
© 2007-2009 Kris Maglione <maglione.k@gmail.com>
|
||||||
|
© 2005 Nico Golde <nico at ngolde dot de>
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a
|
Permission is hereby granted, free of charge, to any person obtaining a
|
||||||
copy of this software and associated documentation files (the "Software"),
|
copy of this software and associated documentation files (the "Software"),
|
||||||
|
|
84
Makefile
84
Makefile
|
@ -1,57 +1,59 @@
|
||||||
# sic - simple irc client
|
.POSIX:
|
||||||
# (C)opyright MMVI Anselm R. Garbe
|
|
||||||
|
|
||||||
include config.mk
|
NAME = sic
|
||||||
|
VERSION = 1.3
|
||||||
|
|
||||||
SRC = sic.c
|
# paths
|
||||||
|
PREFIX = /usr/local
|
||||||
|
MANPREFIX = ${PREFIX}/share/man
|
||||||
|
|
||||||
|
# use system flags.
|
||||||
|
SIC_CFLAGS = ${CFLAGS}
|
||||||
|
SIC_LDFLAGS = ${LDFLAGS}
|
||||||
|
SIC_CPPFLAGS = ${LDFLAGS} -DVERSION=\"${VERSION}\" -D_GNU_SOURCE
|
||||||
|
|
||||||
|
BIN = sic
|
||||||
|
SRC = ${BIN:=.c}
|
||||||
OBJ = ${SRC:.c=.o}
|
OBJ = ${SRC:.c=.o}
|
||||||
|
MAN1 = ${BIN:=.1}
|
||||||
|
|
||||||
all: options sic
|
all: ${BIN}
|
||||||
|
|
||||||
options:
|
${BIN}: ${@:=.o}
|
||||||
@echo sic build options:
|
|
||||||
@echo "CFLAGS = ${CFLAGS}"
|
${OBJ}: config.h strlcpy.c util.c
|
||||||
@echo "LDFLAGS = ${LDFLAGS}"
|
|
||||||
@echo "CC = ${CC}"
|
.o:
|
||||||
@echo "LD = ${LD}"
|
${CC} -o $@ $< ${SIC_LDFLAGS}
|
||||||
|
|
||||||
.c.o:
|
.c.o:
|
||||||
@echo CC $<
|
${CC} -c ${SIC_CFLAGS} ${SIC_CPPFLAGS} -o $@ -c $<
|
||||||
@${CC} -c ${CFLAGS} $<
|
|
||||||
|
|
||||||
${OBJ}: config.mk
|
config.h:
|
||||||
|
cp config.def.h $@
|
||||||
sic: ${OBJ}
|
|
||||||
@echo LD $@
|
|
||||||
@${LD} -o $@ ${OBJ} ${LDFLAGS}
|
|
||||||
@strip $@
|
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
@echo cleaning
|
rm -f ${BIN} ${OBJ} "${NAME}-${VERSION}.tar.gz"
|
||||||
@rm -f sic ${OBJ} sic-${VERSION}.tar.gz
|
|
||||||
|
|
||||||
dist: clean
|
dist:
|
||||||
@echo creating dist tarball
|
mkdir -p "${NAME}-${VERSION}"
|
||||||
@mkdir -p sic-${VERSION}
|
cp -fR LICENSE Makefile README arg.h config.def.h \
|
||||||
@cp -R LICENSE Makefile README config.mk sic.1 ${SRC} sic-${VERSION}
|
${MAN1} ${SRC} util.c strlcpy.c "${NAME}-${VERSION}"
|
||||||
@tar -cf sic-${VERSION}.tar sic-${VERSION}
|
tar -cf - "${NAME}-${VERSION}" | \
|
||||||
@gzip sic-${VERSION}.tar
|
gzip -c > "${NAME}-${VERSION}.tar.gz"
|
||||||
@rm -rf sic-${VERSION}
|
rm -rf "${NAME}-${VERSION}"
|
||||||
|
|
||||||
install: all
|
install: all
|
||||||
@echo installing executable file to ${DESTDIR}${PREFIX}/bin
|
mkdir -p ${DESTDIR}${PREFIX}/bin
|
||||||
@mkdir -p ${DESTDIR}${PREFIX}/bin
|
cp -f ${BIN} "${DESTDIR}${PREFIX}/bin"
|
||||||
@cp -f sic ${DESTDIR}${PREFIX}/bin
|
chmod 755 "${DESTDIR}${PREFIX}/bin/${BIN}"
|
||||||
@chmod 755 ${DESTDIR}${PREFIX}/bin/sic
|
mkdir -p "${DESTDIR}${MANPREFIX}/man1"
|
||||||
@echo installing manual page to ${DESTDIR}${MANPREFIX}/man1
|
sed "s/VERSION/${VERSION}/g" < ${MAN1} > "${DESTDIR}${MANPREFIX}/man1/${MAN1}"
|
||||||
@mkdir -p ${DESTDIR}${MANPREFIX}/man1
|
chmod 644 "${DESTDIR}${MANPREFIX}/man1/${MAN1}"
|
||||||
@sed 's/VERSION/${VERSION}/g' < sic.1 > ${DESTDIR}${MANPREFIX}/man1/sic.1
|
|
||||||
@chmod 644 ${DESTDIR}${MANPREFIX}/man1/sic.1
|
|
||||||
|
|
||||||
uninstall:
|
uninstall:
|
||||||
@echo removing executable file from ${DESTDIR}${PREFIX}/bin
|
rm -f \
|
||||||
@rm -f ${DESTDIR}${PREFIX}/bin/sic
|
"${DESTDIR}${PREFIX}/bin/${BIN}"\
|
||||||
@echo removing manual page from ${DESTDIR}${MANPREFIX}/man1
|
"${DESTDIR}${MANPREFIX}/man1/${MAN1}"
|
||||||
@rm -f ${DESTDIR}${MANPREFIX}/man1/sic.1
|
|
||||||
|
|
||||||
.PHONY: all options clean dist install uninstall
|
.PHONY: all clean dist install uninstall
|
||||||
|
|
22
README
22
README
|
@ -1,22 +0,0 @@
|
||||||
sic - simple irc client
|
|
||||||
=======================
|
|
||||||
sic is an extremly fast, small and simple irc client. It reads commands from
|
|
||||||
standard input and prints all server output to standard output. It multiplexes
|
|
||||||
also all channel traffic into one output, that you don't have to switch
|
|
||||||
different channel buffers, that's actually a feature.
|
|
||||||
|
|
||||||
|
|
||||||
Installation
|
|
||||||
------------
|
|
||||||
Edit config.mk to match your local setup. sic is installed into
|
|
||||||
/usr/local by default.
|
|
||||||
|
|
||||||
Afterwards enter the following command to build and install sic
|
|
||||||
(if necessary as root):
|
|
||||||
|
|
||||||
$ make clean install
|
|
||||||
|
|
||||||
|
|
||||||
Running sic
|
|
||||||
-----------
|
|
||||||
Simply invoke the 'sic' command with the required arguments.
|
|
23
README.md
Normal file
23
README.md
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
sic - simple irc client
|
||||||
|
=======================
|
||||||
|
sic is an extremely fast, small and simple irc client. It reads commands from
|
||||||
|
standard input and prints all server output to standard output. It multiplexes
|
||||||
|
also all channel traffic into one output so that you don't have to switch
|
||||||
|
different channel buffers: that's actually a feature.
|
||||||
|
|
||||||
|
|
||||||
|
Installation
|
||||||
|
------------
|
||||||
|
Edit the Makefile or override the flags to match your local setup. sic is
|
||||||
|
installed into /usr/local by default.
|
||||||
|
|
||||||
|
Afterwards enter the following command to build and install sic
|
||||||
|
(if necessary as root):
|
||||||
|
|
||||||
|
$ make
|
||||||
|
# make install
|
||||||
|
|
||||||
|
|
||||||
|
Running sic
|
||||||
|
-----------
|
||||||
|
Simply invoke the `sic` command with the required arguments.
|
63
arg.h
Normal file
63
arg.h
Normal file
|
@ -0,0 +1,63 @@
|
||||||
|
/*
|
||||||
|
* Copy me if you can.
|
||||||
|
* by 20h
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef ARG_H__
|
||||||
|
#define ARG_H__
|
||||||
|
|
||||||
|
extern char *argv0;
|
||||||
|
|
||||||
|
/* use main(int argc, char *argv[]) */
|
||||||
|
#define ARGBEGIN for (argv0 = *argv, argv++, argc--;\
|
||||||
|
argv[0] && argv[0][1]\
|
||||||
|
&& argv[0][0] == '-';\
|
||||||
|
argc--, argv++) {\
|
||||||
|
char argc_;\
|
||||||
|
char **argv_;\
|
||||||
|
int brk_;\
|
||||||
|
if (argv[0][1] == '-' && argv[0][2] == '\0') {\
|
||||||
|
argv++;\
|
||||||
|
argc--;\
|
||||||
|
break;\
|
||||||
|
}\
|
||||||
|
for (brk_ = 0, argv[0]++, argv_ = argv;\
|
||||||
|
argv[0][0] && !brk_;\
|
||||||
|
argv[0]++) {\
|
||||||
|
if (argv_ != argv)\
|
||||||
|
break;\
|
||||||
|
argc_ = argv[0][0];\
|
||||||
|
switch (argc_)
|
||||||
|
|
||||||
|
/* Handles obsolete -NUM syntax */
|
||||||
|
#define ARGNUM case '0':\
|
||||||
|
case '1':\
|
||||||
|
case '2':\
|
||||||
|
case '3':\
|
||||||
|
case '4':\
|
||||||
|
case '5':\
|
||||||
|
case '6':\
|
||||||
|
case '7':\
|
||||||
|
case '8':\
|
||||||
|
case '9'
|
||||||
|
|
||||||
|
#define ARGEND }\
|
||||||
|
}
|
||||||
|
|
||||||
|
#define ARGC() argc_
|
||||||
|
|
||||||
|
#define ARGNUMF(base) (brk_ = 1, estrtol(argv[0], (base)))
|
||||||
|
|
||||||
|
#define EARGF(x) ((argv[0][1] == '\0' && argv[1] == NULL)?\
|
||||||
|
((x), abort(), (char *)0) :\
|
||||||
|
(brk_ = 1, (argv[0][1] != '\0')?\
|
||||||
|
(&argv[0][1]) :\
|
||||||
|
(argc--, argv++, argv[0])))
|
||||||
|
|
||||||
|
#define ARGF() ((argv[0][1] == '\0' && argv[1] == NULL)?\
|
||||||
|
(char *)0 :\
|
||||||
|
(brk_ = 1, (argv[0][1] != '\0')?\
|
||||||
|
(&argv[0][1]) :\
|
||||||
|
(argc--, argv++, argv[0])))
|
||||||
|
|
||||||
|
#endif
|
14
config.def.h
Normal file
14
config.def.h
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
/* Host used when "-h" is not given */
|
||||||
|
#define DEFAULT_HOST "iiiypuk.me"
|
||||||
|
|
||||||
|
/* Port used when "-p" is not given */
|
||||||
|
#define DEFAULT_PORT "6667"
|
||||||
|
|
||||||
|
/* Timestamp format; see strftime(3). */
|
||||||
|
#define TIMESTAMP_FORMAT "%Y-%m-%d %R"
|
||||||
|
|
||||||
|
/* Command prefix character. In most IRC clients this is '/'. */
|
||||||
|
#define COMMAND_PREFIX_CHARACTER ':'
|
||||||
|
|
||||||
|
/* Parting message used when none is specified with ":l ..." command. */
|
||||||
|
#define DEFAULT_PARTING_MESSAGE "sic - 250 LOC are too much!"
|
22
config.mk
22
config.mk
|
@ -1,22 +0,0 @@
|
||||||
# sic version
|
|
||||||
VERSION = 0.7
|
|
||||||
|
|
||||||
# Customize below to fit your system
|
|
||||||
|
|
||||||
# paths
|
|
||||||
PREFIX = /usr/local
|
|
||||||
MANPREFIX = ${PREFIX}/share/man
|
|
||||||
|
|
||||||
# includes and libs
|
|
||||||
INCS = -I. -I/usr/include
|
|
||||||
LIBS = -L/usr/lib -lc
|
|
||||||
|
|
||||||
# flags
|
|
||||||
CFLAGS = -Os ${INCS} -DVERSION=\"${VERSION}\"
|
|
||||||
LDFLAGS = ${LIBS}
|
|
||||||
#CFLAGS = -g -Wall -O2 ${INCS} -DVERSION=\"${VERSION}\"
|
|
||||||
#LDFLAGS = -g ${LIBS}
|
|
||||||
|
|
||||||
# compiler and linker
|
|
||||||
CC = cc
|
|
||||||
LD = ${CC}
|
|
61
sic.1
61
sic.1
|
@ -3,53 +3,52 @@
|
||||||
sic \- simple irc client
|
sic \- simple irc client
|
||||||
.SH SYNOPSIS
|
.SH SYNOPSIS
|
||||||
.B sic
|
.B sic
|
||||||
.RB [ \-s " <server>"]
|
.RB [ \-h
|
||||||
.RB [ \-p " <port>"]
|
.IR host ]
|
||||||
.RB [ \-n " <nick>"]
|
.RB [ \-p
|
||||||
.RB [ \-k " <keyword>"]
|
.IR port ]
|
||||||
.RB [ \-f " <fullname>"]
|
.RB [ \-n
|
||||||
|
.IR nick ]
|
||||||
|
.RB [ \-k
|
||||||
|
.IR pass ]
|
||||||
.RB [ \-v ]
|
.RB [ \-v ]
|
||||||
.SH DESCRIPTION
|
.SH DESCRIPTION
|
||||||
.B sic
|
.B sic
|
||||||
is an extremly fast, small and simple irc client. It reads commands from
|
is an extremely fast, small and simple irc client. It reads commands from
|
||||||
standard input and prints all server output to standard output. It multiplexes
|
standard input and prints all server output to standard output. It also
|
||||||
also all channel traffic into one output, that you don't have to switch
|
multiplexes all channel traffic into one output. That way you don't have to
|
||||||
different channel buffers, that's actually a feature.
|
switch different channel buffers. So that's actually a feature.
|
||||||
.SH OPTIONS
|
.SH OPTIONS
|
||||||
.TP
|
.TP
|
||||||
.B \-s <server>
|
.BI \-h " host"
|
||||||
Overrides the default server (irc.oftc.net)
|
Overrides the default host (irc.oftc.net)
|
||||||
.TP
|
.TP
|
||||||
.B \-p <port>
|
.BI \-p " port"
|
||||||
Overrides the default port (6667)
|
Overrides the default port (6667)
|
||||||
.TP
|
.TP
|
||||||
.B \-n <nickname>
|
.BI \-n " nick"
|
||||||
Override the default nick ($USER)
|
Override the default nick ($USER)
|
||||||
.TP
|
.TP
|
||||||
.B \-k <keyword>
|
.BI \-k " pass"
|
||||||
Specifies the keyword to authenticate your nick on the server
|
Specifies the PASS connection password
|
||||||
.TP
|
.TP
|
||||||
.B \-f <fullname>
|
.B \-v
|
||||||
Specify the real name (default is $USER)
|
Prints version information to stderr, then exits
|
||||||
.TP
|
|
||||||
.BI \-v
|
|
||||||
Prints version information to standard output, then exits.
|
|
||||||
.SH COMMANDS
|
.SH COMMANDS
|
||||||
.TP
|
.TP
|
||||||
.B /j #channel
|
.BI :j " #channel"
|
||||||
Join a channel
|
Join a channel
|
||||||
.TP
|
.TP
|
||||||
.B /l #channel
|
.BI :l " #channel"
|
||||||
Leave a channel
|
Leave a channel
|
||||||
.TP
|
.TP
|
||||||
.B /m #channel/user msg
|
.BI :m " #channel/user message"
|
||||||
Write a message to #channel/user
|
Send a message to channel or user
|
||||||
.TP
|
.TP
|
||||||
.B /s #channel/user
|
.BI :s " #channel/user"
|
||||||
Set default channel/user
|
Set default channel or user
|
||||||
.TP
|
.TP
|
||||||
.B /t topic
|
.BI : command
|
||||||
Set the channel topic
|
Any line beginning with
|
||||||
.TP
|
.B :
|
||||||
Everything which is not a command will simply be posted into the channel or to
|
is sent as a command
|
||||||
the server.
|
|
||||||
|
|
428
sic.c
428
sic.c
|
@ -1,326 +1,222 @@
|
||||||
/* (C)opyright MMV-MMVI Anselm R. Garbe <garbeam at gmail dot com>
|
/* See LICENSE file for license details. */
|
||||||
* (C)opyright MMV-MMVI Nico Golde <nico at ngolde dot de>
|
#include <sys/select.h>
|
||||||
* See LICENSE file for license details.
|
|
||||||
*/
|
#include <ctype.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <netdb.h>
|
#include <stdarg.h>
|
||||||
#include <netinet/in.h>
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <sys/socket.h>
|
|
||||||
#include <sys/time.h>
|
|
||||||
|
|
||||||
#define PINGTIMEOUT 300
|
#include "arg.h"
|
||||||
#define MAXMSG 4096
|
#include "config.h"
|
||||||
|
|
||||||
enum { Tnick, Tuser, Tcmd, Tchan, Targ, Ttext, Tlast };
|
char *argv0;
|
||||||
|
static char *host = DEFAULT_HOST;
|
||||||
static char *server = "irc.oftc.net";
|
static char *port = DEFAULT_PORT;
|
||||||
static unsigned short port = 6667;
|
static char *password;
|
||||||
static char *nick = NULL;
|
static char nick[32];
|
||||||
static char *fullname = NULL;
|
static char bufin[4096];
|
||||||
static char *password = NULL;
|
static char bufout[4096];
|
||||||
|
|
||||||
static char bufin[MAXMSG], bufout[MAXMSG];
|
|
||||||
static char channel[256];
|
static char channel[256];
|
||||||
static int srv;
|
|
||||||
static time_t trespond;
|
static time_t trespond;
|
||||||
|
static FILE *srv;
|
||||||
|
|
||||||
static int
|
#undef strlcpy
|
||||||
getline(int fd, unsigned int len, char *buf) {
|
#include "strlcpy.c"
|
||||||
unsigned int i = 0;
|
#include "util.c"
|
||||||
char c;
|
|
||||||
|
|
||||||
do {
|
static void
|
||||||
if(read(fd, &c, sizeof(char)) != sizeof(char))
|
pout(char *channel, char *fmt, ...) {
|
||||||
return -1;
|
static char timestr[80];
|
||||||
buf[i++] = c;
|
time_t t;
|
||||||
}
|
va_list ap;
|
||||||
while(c != '\n' && i < len);
|
|
||||||
buf[i - 1] = 0;
|
va_start(ap, fmt);
|
||||||
return 0;
|
vsnprintf(bufout, sizeof bufout, fmt, ap);
|
||||||
|
va_end(ap);
|
||||||
|
t = time(NULL);
|
||||||
|
strftime(timestr, sizeof timestr, TIMESTAMP_FORMAT, localtime(&t));
|
||||||
|
fprintf(stdout, "%-12s: %s %s\n", channel, timestr, bufout);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
pout(char *channel, char *msg) {
|
sout(char *fmt, ...) {
|
||||||
static char timestr[18];
|
va_list ap;
|
||||||
time_t t = time(0);
|
|
||||||
|
|
||||||
strftime(timestr, sizeof timestr, "%F %R", localtime(&t));
|
va_start(ap, fmt);
|
||||||
fprintf(stdout, "%-8.8s: %s %s\n", channel, timestr, msg);
|
vsnprintf(bufout, sizeof bufout, fmt, ap);
|
||||||
|
va_end(ap);
|
||||||
|
fprintf(srv, "%s\r\n", bufout);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
privmsg(char *channel, char *msg) {
|
privmsg(char *channel, char *msg) {
|
||||||
snprintf(bufout, sizeof bufout, "<%s> %s", nick, msg);
|
if(channel[0] == '\0') {
|
||||||
pout(channel, bufout);
|
pout("", "No channel to send to");
|
||||||
snprintf(bufout, sizeof bufout, "PRIVMSG %s :%s\r\n", channel, msg);
|
return;
|
||||||
write(srv, bufout, strlen(bufout));
|
}
|
||||||
|
pout(channel, "<%s> %s", nick, msg);
|
||||||
|
sout("PRIVMSG %s :%s", channel, msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
parsein(char *msg) {
|
parsein(char *s) {
|
||||||
char *p;
|
char c, *p;
|
||||||
|
|
||||||
if(msg[0] == 0)
|
if(s[0] == '\0')
|
||||||
return;
|
return;
|
||||||
if(msg[0] != '/') {
|
skip(s, '\n');
|
||||||
privmsg(channel, msg);
|
if(s[0] != COMMAND_PREFIX_CHARACTER) {
|
||||||
|
privmsg(channel, s);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if(!strncmp(msg + 1, "j ", 2) && (msg[3] == '#'))
|
c = *++s;
|
||||||
snprintf(bufout, sizeof bufout, "JOIN %s\r\n", &msg[3]);
|
if(c != '\0' && isspace((unsigned char)s[1])) {
|
||||||
else if(!strncmp(msg + 1, "l ", 2))
|
p = s + 2;
|
||||||
snprintf(bufout, sizeof bufout, "PART %s :sic\r\n", &msg[3]);
|
switch(c) {
|
||||||
else if(!strncmp(msg + 1, "m ", 2)) {
|
case 'j':
|
||||||
if((p = strchr(&msg[3], ' ')))
|
sout("JOIN %s", p);
|
||||||
*(p++) = 0;
|
if(channel[0] == '\0')
|
||||||
privmsg(&msg[3], p);
|
strlcpy(channel, p, sizeof channel);
|
||||||
return;
|
return;
|
||||||
}
|
case 'l':
|
||||||
else if(!strncmp(msg + 1, "s ", 2)) {
|
s = eat(p, isspace, 1);
|
||||||
strncpy(channel, &msg[3], sizeof channel);
|
p = eat(s, isspace, 0);
|
||||||
return;
|
if(!*s)
|
||||||
}
|
s = channel;
|
||||||
else if(!strncmp(msg + 1, "t ", 2)) {
|
if(*p)
|
||||||
if((p = strchr(&msg[3], ' ')))
|
*p++ = '\0';
|
||||||
*(p++) = 0;
|
if(!*p)
|
||||||
snprintf(bufout, sizeof bufout, "TOPIC %s :%s\r\n", &msg[3], p);
|
p = DEFAULT_PARTING_MESSAGE;
|
||||||
}
|
sout("PART %s :%s", s, p);
|
||||||
else
|
return;
|
||||||
snprintf(bufout, sizeof bufout, "%s\r\n", &msg[1]);
|
case 'm':
|
||||||
write(srv, bufout, strlen(bufout));
|
s = eat(p, isspace, 1);
|
||||||
}
|
p = eat(s, isspace, 0);
|
||||||
|
if(*p)
|
||||||
static unsigned int
|
*p++ = '\0';
|
||||||
tokenize(char **result, unsigned int reslen, char *str, char delim) {
|
privmsg(s, p);
|
||||||
char *p, *n;
|
return;
|
||||||
unsigned int i = 0;
|
case 's':
|
||||||
|
strlcpy(channel, p, sizeof channel);
|
||||||
if(!str)
|
|
||||||
return 0;
|
|
||||||
for(n = str; *n == delim; n++);
|
|
||||||
p = n;
|
|
||||||
for(i = 0; *n != 0;) {
|
|
||||||
if(i == reslen)
|
|
||||||
return i;
|
|
||||||
if(*n == delim) {
|
|
||||||
*n = 0;
|
|
||||||
if(strlen(p))
|
|
||||||
result[i++] = p;
|
|
||||||
p = ++n;
|
|
||||||
} else
|
|
||||||
n++;
|
|
||||||
}
|
|
||||||
if((i < reslen) && (p < n) && strlen(p))
|
|
||||||
result[i++] = p;
|
|
||||||
return i; /* number of tokens */
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
parsesrv(char *msg) {
|
|
||||||
char *argv[Tlast], *cmd, *p;
|
|
||||||
int i;
|
|
||||||
|
|
||||||
if(!msg || !(*msg))
|
|
||||||
return;
|
|
||||||
|
|
||||||
for(i = 0; i < Tlast; i++)
|
|
||||||
argv[i] = NULL;
|
|
||||||
|
|
||||||
/* <bufout> ::= [':' <prefix> <SPACE> ] <command> <params> <crlf>
|
|
||||||
* <prefix> ::= <servername> | <nick> [ '!' <user> ] [ '@' <server> ]
|
|
||||||
* <command> ::= <letter> { <letter> } | <number> <number> <number>
|
|
||||||
* <SPACE> ::= ' ' { ' ' }
|
|
||||||
* <params> ::= <SPACE> [ ':' <trailing> | <middle> <params> ]
|
|
||||||
* <middle> ::= <Any *non-empty* sequence of octets not including SPACE
|
|
||||||
* or NUL or CR or LF, the first of which may not be ':'>
|
|
||||||
* <trailing> ::= <Any, possibly *empty*, sequence of octets not including NUL or CR or LF>
|
|
||||||
* <crlf> ::= CR LF
|
|
||||||
*/
|
|
||||||
if(msg[0] == ':') { /* check prefix */
|
|
||||||
if (!(p = strchr(msg, ' ')))
|
|
||||||
return;
|
return;
|
||||||
*p = 0;
|
|
||||||
for(++p; *p == ' '; p++);
|
|
||||||
cmd = p;
|
|
||||||
argv[Tnick] = &msg[1];
|
|
||||||
if((p = strchr(msg, '!'))) {
|
|
||||||
*p = 0;
|
|
||||||
argv[Tuser] = ++p;
|
|
||||||
}
|
}
|
||||||
} else
|
|
||||||
cmd = msg;
|
|
||||||
/* remove CRLFs */
|
|
||||||
for(p = cmd; p && *p != 0; p++)
|
|
||||||
if(*p == '\r' || *p == '\n')
|
|
||||||
*p = 0;
|
|
||||||
if((p = strchr(cmd, ':'))) {
|
|
||||||
*p = 0;
|
|
||||||
argv[Ttext] = ++p;
|
|
||||||
}
|
}
|
||||||
tokenize(&argv[Tcmd], Tlast - Tcmd, cmd, ' ');
|
sout("%s", s);
|
||||||
if(!argv[Tcmd] || !strncmp("PONG", argv[Tcmd], 5))
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
parsesrv(char *cmd) {
|
||||||
|
char *usr, *par, *txt;
|
||||||
|
|
||||||
|
usr = host;
|
||||||
|
if(!cmd || !*cmd)
|
||||||
return;
|
return;
|
||||||
else if(!strncmp("PING", argv[Tcmd], 5)) {
|
if(cmd[0] == ':') {
|
||||||
snprintf(bufout, sizeof bufout, "PONG %s\r\n", argv[Ttext]);
|
usr = cmd + 1;
|
||||||
write(srv, bufout, strlen(bufout));
|
cmd = skip(usr, ' ');
|
||||||
|
if(cmd[0] == '\0')
|
||||||
|
return;
|
||||||
|
skip(usr, '!');
|
||||||
|
}
|
||||||
|
skip(cmd, '\r');
|
||||||
|
par = skip(cmd, ' ');
|
||||||
|
txt = skip(par, ':');
|
||||||
|
trim(par);
|
||||||
|
if(!strcmp("PONG", cmd))
|
||||||
return;
|
return;
|
||||||
|
if(!strcmp("PRIVMSG", cmd))
|
||||||
|
pout(par, "<%s> %s", usr, txt);
|
||||||
|
else if(!strcmp("PING", cmd))
|
||||||
|
sout("PONG %s", txt);
|
||||||
|
else {
|
||||||
|
pout(usr, ">< %s (%s): %s", cmd, par, txt);
|
||||||
|
if(!strcmp("NICK", cmd) && !strcmp(usr, nick))
|
||||||
|
strlcpy(nick, txt, sizeof nick);
|
||||||
}
|
}
|
||||||
else if(!argv[Tnick] || !argv[Tuser]) { /* server command */
|
}
|
||||||
snprintf(bufout, sizeof bufout, "%s", argv[Ttext] ? argv[Ttext] : "");
|
|
||||||
pout(server, bufout);
|
|
||||||
return;
|
static void
|
||||||
}
|
usage(void) {
|
||||||
else if(!strncmp("ERROR", argv[Tcmd], 6))
|
eprint("usage: sic [-h host] [-p port] [-n nick] [-k keyword] [-v]\n", argv0);
|
||||||
snprintf(bufout, sizeof bufout, "-!- error %s",
|
|
||||||
argv[Ttext] ? argv[Ttext] : "unknown");
|
|
||||||
else if(!strncmp("JOIN", argv[Tcmd], 5)) {
|
|
||||||
if(argv[Ttext]!=NULL){
|
|
||||||
p = strchr(argv[Ttext], ' ');
|
|
||||||
if(p)
|
|
||||||
*p = 0;
|
|
||||||
}
|
|
||||||
argv[Tchan] = argv[Ttext];
|
|
||||||
snprintf(bufout, sizeof bufout, "-!- %s(%s) has joined %s",
|
|
||||||
argv[Tnick], argv[Tuser], argv[Ttext]);
|
|
||||||
}
|
|
||||||
else if(!strncmp("PART", argv[Tcmd], 5)) {
|
|
||||||
snprintf(bufout, sizeof bufout, "-!- %s(%s) has left %s",
|
|
||||||
argv[Tnick], argv[Tuser], argv[Tchan]);
|
|
||||||
}
|
|
||||||
else if(!strncmp("MODE", argv[Tcmd], 5))
|
|
||||||
snprintf(bufout, sizeof bufout, "-!- %s changed mode/%s -> %s %s",
|
|
||||||
argv[Tnick], argv[Tcmd + 1],
|
|
||||||
argv[Tcmd + 2], argv[Tcmd + 3]);
|
|
||||||
else if(!strncmp("QUIT", argv[Tcmd], 5))
|
|
||||||
snprintf(bufout, sizeof bufout, "-!- %s(%s) has quit \"%s\"",
|
|
||||||
argv[Tnick], argv[Tuser],
|
|
||||||
argv[Ttext] ? argv[Ttext] : "");
|
|
||||||
else if(!strncmp("NICK", argv[Tcmd], 5))
|
|
||||||
snprintf(bufout, sizeof bufout, "-!- %s changed nick to %s",
|
|
||||||
argv[Tnick], argv[Ttext]);
|
|
||||||
else if(!strncmp("TOPIC", argv[Tcmd], 6))
|
|
||||||
snprintf(bufout, sizeof bufout, "-!- %s changed topic to \"%s\"",
|
|
||||||
argv[Tnick], argv[Ttext] ? argv[Ttext] : "");
|
|
||||||
else if(!strncmp("KICK", argv[Tcmd], 5))
|
|
||||||
snprintf(bufout, sizeof bufout, "-!- %s kicked %s (\"%s\")",
|
|
||||||
argv[Tnick], argv[Targ],
|
|
||||||
argv[Ttext] ? argv[Ttext] : "");
|
|
||||||
else if(!strncmp("NOTICE", argv[Tcmd], 7))
|
|
||||||
snprintf(bufout, sizeof bufout, "-!- \"%s\")",
|
|
||||||
argv[Ttext] ? argv[Ttext] : "");
|
|
||||||
else if(!strncmp("PRIVMSG", argv[Tcmd], 8))
|
|
||||||
snprintf(bufout, sizeof bufout, "<%s> %s",
|
|
||||||
argv[Tnick], argv[Ttext] ? argv[Ttext] : "");
|
|
||||||
if(!argv[Tchan] || !strncmp(argv[Tchan], nick, strlen(nick)))
|
|
||||||
pout(argv[Tnick], bufout);
|
|
||||||
else
|
|
||||||
pout(argv[Tchan], bufout);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
main(int argc, char *argv[]) {
|
main(int argc, char *argv[]) {
|
||||||
int i;
|
|
||||||
struct timeval tv;
|
struct timeval tv;
|
||||||
struct hostent *hp;
|
const char *user = getenv("USER");
|
||||||
static struct sockaddr_in addr; /* initially filled with 0's */
|
int n;
|
||||||
char ping[256];
|
|
||||||
fd_set rd;
|
fd_set rd;
|
||||||
|
|
||||||
nick = fullname = getenv("USER");
|
strlcpy(nick, user ? user : "unknown", sizeof nick);
|
||||||
for(i = 1; i < argc; i++)
|
ARGBEGIN {
|
||||||
if(!strncmp(argv[i], "-s", 3)) {
|
case 'h':
|
||||||
if(++i < argc) server = argv[i];
|
host = EARGF(usage());
|
||||||
}
|
break;
|
||||||
else if(!strncmp(argv[i], "-p", 3)) {
|
case 'p':
|
||||||
if(++i < argc) port = (unsigned short)atoi(argv[i]);
|
port = EARGF(usage());
|
||||||
}
|
break;
|
||||||
else if(!strncmp(argv[i], "-n", 3)) {
|
case 'n':
|
||||||
if(++i < argc) nick = argv[i];
|
strlcpy(nick, EARGF(usage()), sizeof nick);
|
||||||
}
|
break;
|
||||||
else if(!strncmp(argv[i], "-k", 3)) {
|
case 'k':
|
||||||
if(++i < argc) password = argv[i];
|
password = EARGF(usage());
|
||||||
}
|
break;
|
||||||
else if(!strncmp(argv[i], "-f", 3)) {
|
case 'v':
|
||||||
if(++i < argc) fullname = argv[i];
|
eprint("sic-"VERSION", © 2005-2017 Anselm R Garbe, Jeroen Schot, Kris Maglione, Nico Golde\n");
|
||||||
}
|
break;
|
||||||
else if(!strncmp(argv[i], "-v", 3)) {
|
default:
|
||||||
fputs("sic-"VERSION", (C)opyright MMVI Anselm R. Garbe\n", stdout);
|
usage();
|
||||||
exit(EXIT_SUCCESS);
|
} ARGEND;
|
||||||
}
|
|
||||||
else {
|
|
||||||
fputs("usage: sic [-s server] [-p port] [-n nick]"
|
|
||||||
" [-k keyword] [-f fullname] [-v]\n", stderr);
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* init */
|
/* init */
|
||||||
if((srv = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
|
srv = fdopen(dial(host, port), "r+");
|
||||||
fprintf(stderr, "sic: cannot connect server '%s'\n", server);
|
if (!srv)
|
||||||
exit(EXIT_FAILURE);
|
eprint("fdopen:");
|
||||||
}
|
|
||||||
if (NULL == (hp = gethostbyname(server))) {
|
|
||||||
fprintf(stderr, "sic: cannot resolve hostname '%s'\n", server);
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
addr.sin_family = AF_INET;
|
|
||||||
addr.sin_port = htons(port);
|
|
||||||
memcpy(&addr.sin_addr, hp->h_addr, hp->h_length);
|
|
||||||
if(connect(srv, (struct sockaddr *) &addr, sizeof(struct sockaddr_in))) {
|
|
||||||
close(srv);
|
|
||||||
fprintf(stderr, "sic: cannot connect server '%s'\n", server);
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
/* login */
|
/* login */
|
||||||
if(password)
|
if(password)
|
||||||
snprintf(bufout, sizeof bufout,
|
sout("PASS %s", password);
|
||||||
"PASS %s\r\nNICK %s\r\nUSER %s localhost %s :%s\r\n",
|
sout("NICK %s", nick);
|
||||||
password, nick, nick, server, fullname);
|
sout("USER %s localhost %s :%s", nick, host, nick);
|
||||||
else
|
fflush(srv);
|
||||||
snprintf(bufout, sizeof bufout, "NICK %s\r\nUSER %s localhost %s :%s\r\n",
|
setbuf(stdout, NULL);
|
||||||
nick, nick, server, fullname);
|
setbuf(srv, NULL);
|
||||||
write(srv, bufout, strlen(bufout));
|
setbuf(stdin, NULL);
|
||||||
snprintf(ping, sizeof ping, "PING %s\r\n", server);
|
#ifdef __OpenBSD__
|
||||||
channel[0] = 0;
|
if (pledge("stdio", NULL) == -1)
|
||||||
setbuf(stdout, NULL); /* unbuffered stdout */
|
eprint("error: pledge:");
|
||||||
|
#endif
|
||||||
for(;;) { /* main loop */
|
for(;;) { /* main loop */
|
||||||
FD_ZERO(&rd);
|
FD_ZERO(&rd);
|
||||||
FD_SET(0, &rd);
|
FD_SET(0, &rd);
|
||||||
FD_SET(srv, &rd);
|
FD_SET(fileno(srv), &rd);
|
||||||
tv.tv_sec = 120;
|
tv.tv_sec = 120;
|
||||||
tv.tv_usec = 0;
|
tv.tv_usec = 0;
|
||||||
i = select(srv + 1, &rd, 0, 0, &tv);
|
n = select(fileno(srv) + 1, &rd, 0, 0, &tv);
|
||||||
if(i < 0) {
|
if(n < 0) {
|
||||||
if(errno == EINTR)
|
if(errno == EINTR)
|
||||||
continue;
|
continue;
|
||||||
perror("sic: error on select()");
|
eprint("sic: error on select():");
|
||||||
exit(EXIT_FAILURE);
|
}
|
||||||
} else if(i == 0) {
|
else if(n == 0) {
|
||||||
if(time(NULL) - trespond >= PINGTIMEOUT) {
|
if(time(NULL) - trespond >= 300)
|
||||||
pout(server, "-!- sic shutting down: parse timeout");
|
eprint("sic shutting down: parse timeout\n");
|
||||||
exit(EXIT_FAILURE);
|
sout("PING %s", host);
|
||||||
}
|
|
||||||
write(srv, ping, strlen(ping));
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if(FD_ISSET(srv, &rd)) {
|
if(FD_ISSET(fileno(srv), &rd)) {
|
||||||
if(getline(srv, sizeof bufin, bufin) == -1) {
|
if(fgets(bufin, sizeof bufin, srv) == NULL)
|
||||||
perror("sic: remote server closed connection");
|
eprint("sic: remote host closed connection\n");
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
parsesrv(bufin);
|
parsesrv(bufin);
|
||||||
trespond = time(NULL);
|
trespond = time(NULL);
|
||||||
}
|
}
|
||||||
if(FD_ISSET(0, &rd)) {
|
if(FD_ISSET(0, &rd)) {
|
||||||
if(getline(0, sizeof bufin, bufin) == -1) {
|
if(fgets(bufin, sizeof bufin, stdin) == NULL)
|
||||||
perror("sic: broken pipe");
|
eprint("sic: broken pipe\n");
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
parsein(bufin);
|
parsein(bufin);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
46
strlcpy.c
Normal file
46
strlcpy.c
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com>
|
||||||
|
*
|
||||||
|
* Permission to use, copy, modify, and distribute this software for any
|
||||||
|
* purpose with or without fee is hereby granted, provided that the above
|
||||||
|
* copyright notice and this permission notice appear in all copies.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||||
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||||
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||||
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||||
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||||
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||||
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Copy src to string dst of size siz. At most siz-1 characters
|
||||||
|
* will be copied. Always NUL terminates (unless siz == 0).
|
||||||
|
* Returns strlen(src); if retval >= siz, truncation occurred.
|
||||||
|
*/
|
||||||
|
size_t
|
||||||
|
strlcpy(char *dst, const char *src, size_t siz)
|
||||||
|
{
|
||||||
|
char *d = dst;
|
||||||
|
const char *s = src;
|
||||||
|
size_t n = siz;
|
||||||
|
/* Copy as many bytes as will fit */
|
||||||
|
if (n != 0) {
|
||||||
|
while (--n != 0) {
|
||||||
|
if ((*d++ = *s++) == '\0')
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/* Not enough room in dst, add NUL and traverse rest of src */
|
||||||
|
if (n == 0) {
|
||||||
|
if (siz != 0)
|
||||||
|
*d = '\0'; /* NUL-terminate dst */
|
||||||
|
while (*s++)
|
||||||
|
;
|
||||||
|
}
|
||||||
|
return(s - src - 1); /* count does not include NUL */
|
||||||
|
}
|
66
util.c
Normal file
66
util.c
Normal file
|
@ -0,0 +1,66 @@
|
||||||
|
/* See LICENSE file for license details. */
|
||||||
|
#include <netdb.h>
|
||||||
|
#include <netinet/in.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
|
||||||
|
static void
|
||||||
|
eprint(const char *fmt, ...) {
|
||||||
|
va_list ap;
|
||||||
|
|
||||||
|
va_start(ap, fmt);
|
||||||
|
vsnprintf(bufout, sizeof bufout, fmt, ap);
|
||||||
|
va_end(ap);
|
||||||
|
fprintf(stderr, "%s", bufout);
|
||||||
|
if(fmt[0] && fmt[strlen(fmt) - 1] == ':')
|
||||||
|
fprintf(stderr, " %s\n", strerror(errno));
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
dial(char *host, char *port) {
|
||||||
|
struct addrinfo hints;
|
||||||
|
struct addrinfo *res, *r;
|
||||||
|
int fd;
|
||||||
|
|
||||||
|
memset(&hints, 0, sizeof hints);
|
||||||
|
hints.ai_family = AF_UNSPEC;
|
||||||
|
hints.ai_socktype = SOCK_STREAM;
|
||||||
|
if(getaddrinfo(host, port, &hints, &res) != 0)
|
||||||
|
eprint("error: cannot resolve hostname '%s':", host);
|
||||||
|
for(r = res; r; r = r->ai_next) {
|
||||||
|
if((fd = socket(r->ai_family, r->ai_socktype, r->ai_protocol)) == -1)
|
||||||
|
continue;
|
||||||
|
if(connect(fd, r->ai_addr, r->ai_addrlen) == 0)
|
||||||
|
break;
|
||||||
|
close(fd);
|
||||||
|
}
|
||||||
|
freeaddrinfo(res);
|
||||||
|
if(!r)
|
||||||
|
eprint("error: cannot connect to host '%s'\n", host);
|
||||||
|
return fd;
|
||||||
|
}
|
||||||
|
|
||||||
|
static char *
|
||||||
|
eat(char *s, int (*p)(int), int r) {
|
||||||
|
while(*s != '\0' && p((unsigned char)*s) == r)
|
||||||
|
s++;
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
static char*
|
||||||
|
skip(char *s, char c) {
|
||||||
|
while(*s != c && *s != '\0')
|
||||||
|
s++;
|
||||||
|
if(*s != '\0')
|
||||||
|
*s++ = '\0';
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
trim(char *s) {
|
||||||
|
char *e;
|
||||||
|
|
||||||
|
for (e = s + strlen(s); e > s && isspace((unsigned char)*(e - 1)); e--)
|
||||||
|
;
|
||||||
|
*e = '\0';
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user