#include "ircfmt.h"
#include "passwd.h"
#include "job.h"
-
-struct dynarr databuf;
-
+std::vector<char> databuf;
clientmap client_map = {};
channelmap channel_map = {};
stringset autojoin = {};
const char *hostname = 0;
void sendbuf(struct client_data *client) {
- log_client(client, "SERVER: %.*s", (int)databuf.len, (char*)databuf.buf);
- sendClient(client, databuf.buf, databuf.len);
+ log_client(client, "SERVER: %.*s", (int)databuf.size(), (char*)databuf.data());
+ sendClient(client, databuf.data(), databuf.size());
}
static void chanlog_time(time_t timestamp) {
if (fchanlog) {
- fprintf(fchanlog, "$%lu %.*s", (unsigned long)timestamp, (int)databuf.len, databuf.buf);
+ fprintf(fchanlog, "$%lu %.*s", (unsigned long)timestamp, (int)databuf.size(), databuf.data());
fflush(fchanlog);
}
}
static void chanlog(void) {
extern FILE *fchanlog;
if (fchanlog) {
- fprintf(fchanlog, "%.*s", (int)databuf.len, databuf.buf);
+ fprintf(fchanlog, "%.*s", (int)databuf.size(), databuf.data());
fflush(fchanlog);
}
}
}
int processPacket(struct client_data *client, char *source, char *command, int argc, char *args[]) {
(void)source;
- databuf.len = 0;
+ databuf.clear();
if(strcmp(command, "") == 0) {
return 0;
} else if(strcmp(command, "PING") == 0) {
if (client->caps & c->caps)
append_strf(&databuf, "%s ", c->name);
}
- if (databuf.buf[databuf.len-1] == ' ')
- databuf.len--;
+ if (databuf.back() == ' ')
+ databuf.pop_back();
append_strf(&databuf, "\r\n");
sendbuf(client);
} else if (strcmp(args[0], "REQ") == 0) {
}
void channel_sendnames(struct client_data *client, struct channel *chan)
{
- databuf.len = 0;
+ databuf.clear();
for (auto cl : chan->members)
if (rpl_namreply(client, chan, cl)) {
sendbuf(client);
- databuf.len = 0;
+ databuf.clear();
rpl_namreply(client, chan, cl);
}
if (rpl_namreply(client, chan, NULL))
void channel_kill(struct channel *chan);
void channel_mode(struct client_data *client, struct channel *chan, char *args[]);
void channel_topic(struct client_data *src, struct channel *chan, const char *topic, const char *who, time_t when);
-extern struct dynarr databuf;
+extern std::vector<char> databuf;
extern FILE *fchanlog;
extern const char *hostname;
#endif
int rpl_namreply(struct client_data *client, struct channel *chan, struct client_data *cl) // 353
{
if (!cl) {
- if (databuf.len) {
+ if (databuf.size()) {
append_str(&databuf, "\r\n");
return 1;
} else {
}
}
const char *prefix = format_prefixes(client, cl, chan);
- if (databuf.len == 0) {
+ if (databuf.empty()) {
const char *chanchar = chan->dmode & DMODE_SECRET ? "@" : "=";
copy_strf(&databuf, ":%s 353 %s %s %s :", hostname, client->nick, chanchar, chan->name);
} else {
- if (databuf.len + 1 + strlen(prefix) + strlen(cl->nick) > 510) {
+ if (databuf.size() + 1 + strlen(prefix) + strlen(cl->nick) > 510) {
append_str(&databuf, "\r\n");
return 1;
}
#ifdef HAVE_LUA
admin_init();
#endif
- databuf.buf = (char *)malloc(sizeof(char) * 512);
- databuf.cap = 512;
struct tls *tls_ctx = NULL;
#ifdef HAVE_LIBTLS
#ifdef HAVE_LIBTLS
tls_free(tls_ctx);
#endif
- free(databuf.buf);
/* free any persistent channels */
for (auto chan : channel_map.pop_view())
channel_kill(chan);
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
-static void append_strn(struct dynarr *d, const char *s, int len){
- if (d->len + len > d->cap){
- while (d->len + len > d->cap)
- d->cap *= 2;
- char *ndata = (char *)realloc(d->buf, d->cap);
- if(!ndata)
- abortf("append_strn: allocation failure\n");
- d->buf = ndata;
- }
- memcpy(d->buf + d->len, s, len);
- d->len += len;
+static void append_strn(std::vector<char> *v, const char *s, int len) {
+ v->insert(v->end(), s, s+len);
}
-void append_str(struct dynarr *d, const char *s){
- append_strn(d, s, strlen(s));
+void append_str(std::vector<char> *v, const char *s) {
+ append_strn(v, s, strlen(s));
}
-void append_strf(struct dynarr *d, const char *s, ...){
+void append_strf(std::vector<char> *v, const char *s, ...) {
va_list ap;
va_start(ap, s);
for (;;) {
int len = strlen(s);
if (len) {
if (s[len-1] == '\n' && (len == 1 || s[len-2] != '\r')) {
- append_strn(d, s, len-1);
- append_strn(d, "\r\n", 2);
+ append_strn(v, s, len-1);
+ append_strn(v, "\r\n", 2);
} else {
- append_strn(d, s, len);
+ append_strn(v, s, len);
}
}
break;
}
if (end[1] == 's') { // %s
- append_strn(d, s, end-s);
+ append_strn(v, s, end-s);
const char *c = va_arg(ap, const char *);
- append_str(d, c ? c : "(null)");
+ append_str(v, c ? c : "(null)");
s = end + 2;
} else if (end[1] == 'u') { // %u
- append_strn(d, s, end-s);
+ append_strn(v, s, end-s);
unsigned c = va_arg(ap, unsigned);
char buf[11] = {0};
char *p = &buf[10];
*--p = '0' + c%10;
c /= 10;
}
- append_str(d, p);
+ append_str(v, p);
s = end + 2;
} else if (end[1] == '%') { // %%
- append_strn(d, s, end-s+1);
+ append_strn(v, s, end-s+1);
s = end + 2;
} else { // invalid
break;
#include <stdlib.h>
#include <string.h>
#include <time.h>
-
-struct dynarr {
- char *buf;
- size_t len;
- size_t cap;
-};
-
-void append_str(struct dynarr *d, const char *s);
-void append_strf(struct dynarr *d, const char *s, ...);
-#define copy_strf(d, ...) append_strf(((d)->len=0, (d)), __VA_ARGS__)
+#include <vector>
+void append_str(std::vector<char> *v, const char *s);
+void append_strf(std::vector<char> *v, const char *s, ...);
+#define copy_strf(d, ...) append_strf(((d)->clear(), (d)), __VA_ARGS__)
void stringupper(char *s);
void stringlower(char *s);
#define abortf(...) (fprintf(stderr, "(abort) " __VA_ARGS__), abort())