Skip to content

Commit

Permalink
Refactor dccutil.c:add_cr() and dcc.c:escape_telnet() into a single m…
Browse files Browse the repository at this point in the history
…isc.c:add_cr()
  • Loading branch information
michaelortmann committed Oct 19, 2020
1 parent 5b223c3 commit 151f120
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 35 deletions.
20 changes: 2 additions & 18 deletions src/dcc.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,22 +90,6 @@ static int detect_telnet(unsigned char *buf)
return 0;
}

/* Escape telnet IAC and prepend CR to LF */
static char *escape_telnet(char *s)
{
static char buf[1024];
char *p;

for (p = buf; *s && (p < (buf + sizeof(buf) - 2)); *p++ = *s++)
if ((unsigned char) *s == TLN_IAC)
*p++ = *s;
else if (*s == '\n')
*p++ = '\r';
*p = 0;

return buf;
}

static void strip_telnet(int sock, char *buf, int *len)
{
unsigned char *p = (unsigned char *) buf, *o = (unsigned char *) buf;
Expand Down Expand Up @@ -929,7 +913,7 @@ static void out_dcc_general(int idx, char *buf, void *x)

strip_mirc_codes(p->strip_flags, buf);
if (dcc[idx].status & STAT_TELNET)
y = escape_telnet(buf);
y = add_cr(buf, 1);
if (dcc[idx].status & STAT_PAGE)
append_line(idx, y);
else
Expand Down Expand Up @@ -1768,7 +1752,7 @@ static void dcc_telnet_pass(int idx, int atr)
/* Turn off remote telnet echo (send IAC WILL ECHO). */
if (dcc[idx].status & STAT_TELNET) {
char buf[1030];
egg_snprintf(buf, sizeof buf, "\n%s%s\r\n", escape_telnet(DCC_ENTERPASS),
egg_snprintf(buf, sizeof buf, "\n%s%s\r\n", add_cr(DCC_ENTERPASS, 1),
TLN_IAC_C TLN_WILL_C TLN_ECHO_C);
tputs(dcc[idx].sock, buf, strlen(buf));
} else
Expand Down
17 changes: 1 addition & 16 deletions src/dccutil.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,21 +132,6 @@ int findanyidx(int z)
return -1;
}

/* Replace \n with \r\n */
char *add_cr(char *buf)
{
static char WBUF[1024];
char *p, *q;

for (p = buf, q = WBUF; *p; p++, q++) {
if (*p == '\n')
*q++ = '\r';
*q = *p;
}
*q = *p;
return WBUF;
}

extern void (*qserver) (int, char *, int);

void dprintf EGG_VARARGS_DEF(int, arg1)
Expand Down Expand Up @@ -208,7 +193,7 @@ void dprint(int idx, char *buf, int len)
len = LOGLINEMAX-10;
}
if (dcc[idx].type && ((long) (dcc[idx].type->output) == 1)) {
char *p = add_cr(buf);
char *p = add_cr(buf, 0);

tputs(dcc[idx].sock, p, strlen(p));
} else if (dcc[idx].type && dcc[idx].type->output)
Expand Down
23 changes: 23 additions & 0 deletions src/misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1575,3 +1575,26 @@ void kill_bot(char *s1, char *s2)
write_userfile(-1);
fatal(s2, 2);
}

/* Prepend CR to LF (Replace \n with \r\n) and escape telnet IAC if
* escape_telnet is set
*/
char *add_cr(const char *p, const int escape_telnet)
{
static int buf_size = 0;
static char *buf;
char *q;

if ((strlen(p) << 1) > buf_size) {
buf_size = strlen(p) << 1;
buf = nrealloc(buf, buf_size);
}
for (q = buf; *p; *q++ = *p++) {
if (escape_telnet && ((unsigned char) *p == TLN_IAC))
*q++ = *p;
else if (*p == '\n')
*q++ = '\r';
}
*q = 0;
return buf;
}
2 changes: 1 addition & 1 deletion src/proto.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ int dcc_fingerprint(int);
int increase_socks_max();
int findidx(int);
int findanyidx(int);
char *add_cr(char *);
void dprint(int, char *, int);
void dprintf EGG_VARARGS(int, arg1);
void chatout EGG_VARARGS(char *, arg1);
Expand Down Expand Up @@ -261,6 +260,7 @@ char *strchr_unescape(char *, const char, const char);
void str_unescape(char *, const char);
int str_isdigit(const char *);
void kill_bot(char *, char *);
char *add_cr(const char *, const int);

void maskaddr(const char *, char *, int);
#define maskhost(a,b) maskaddr((a),(b),3)
Expand Down

0 comments on commit 151f120

Please sign in to comment.