Commit 707861a5 authored by Dustin L. Howett's avatar Dustin L. Howett

Make nick and hostname private and expose them only via accessors.

parent 370188a2
......@@ -27,7 +27,7 @@ void dispatch_event(server *server, char *name, struct sender *sender, char *msg
_EVENT(001) {
char *nick = strsep(&msgbuf, " ");
server->nick = strdup(nick);
server->setNick(nick);
server->printf("JOIN #bacon");
}
......@@ -45,7 +45,7 @@ _EVENT(PING) {
_EVENT(PRIVMSG) {
char *recipient = strsep(&msgbuf, " ");
bool priv = false;
if(server->nick && strcmp(recipient, server->nick) == 0) {
if(server->getNick() && strcmp(recipient, server->getNick()) == 0) {
priv = true;
}
......
......@@ -141,8 +141,7 @@ void _new_server(char *hostname) {
exit(1);
}
server *newserver = new server();
newserver->hostname = strdup(hostname);
server *newserver = new server(hostname);
newserver->fd = socket_fd;
control_fd = 0;
......
......@@ -6,7 +6,7 @@
#include <stdarg.h>
/* {{{ DADY */
static int _serv_internal_tag_cb(uint32_t tagtype, uint32_t len, uint8_t *data, struct dady_callback *cb, server *server) {
int _serv_internal_tag_cb(uint32_t tagtype, uint32_t len, uint8_t *data, struct dady_callback *cb, server *server) {
switch(tagtype) {
case 'RBUF':
if(len > 0) {
......@@ -37,8 +37,8 @@ static int _serv_internal_tag_cb(uint32_t tagtype, uint32_t len, uint8_t *data,
static struct dady_callback _serv_internal_callbacks = { NULL, (tag_callback)&_serv_internal_tag_cb };
/* }}} */
server::server() :
hostname(NULL),
server::server(const char *hostname) :
hostname(strdup(hostname)),
nick(NULL),
readbuf(new char[READ_BUF_LEN]),
nbytes_read(0),
......@@ -81,6 +81,16 @@ server::~server() {
free(this->nick);
}
const char *server::getHostname() { return hostname; }
const char *server::getNick() { return nick; }
void server::setNick(const char *newNick) {
if(nick) {
free(nick);
nick = NULL;
}
nick = strdup(newNick);
}
void server::printf(const char *format, ...) {
char *intermediate;
......
......@@ -3,10 +3,11 @@
#include <stdint.h>
class server {
public:
private:
char *hostname;
char *nick;
public:
int fd;
char *readbuf;
unsigned long long int nbytes_read;
......@@ -14,13 +15,25 @@ public:
char *writebuf;
unsigned long long int nbytes_write;
server();
public:
server(const char *hostname);
server(uint8_t *data, uint32_t size);
/* {{{ Accessors */
const char *getHostname();
const char *getNick();
void setNick(const char *newNick);
/* }}} */
void printf(const char *format, ...);
uint8_t *freeze(uint32_t *outlen);
~server();
private:
server();
friend int _serv_internal_tag_cb(uint32_t tagtype, uint32_t len, uint8_t *data, struct dady_callback *cb, server *server);
};
#endif /* end of include guard: SERVER_H */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment