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 ...@@ -27,7 +27,7 @@ void dispatch_event(server *server, char *name, struct sender *sender, char *msg
_EVENT(001) { _EVENT(001) {
char *nick = strsep(&msgbuf, " "); char *nick = strsep(&msgbuf, " ");
server->nick = strdup(nick); server->setNick(nick);
server->printf("JOIN #bacon"); server->printf("JOIN #bacon");
} }
...@@ -45,7 +45,7 @@ _EVENT(PING) { ...@@ -45,7 +45,7 @@ _EVENT(PING) {
_EVENT(PRIVMSG) { _EVENT(PRIVMSG) {
char *recipient = strsep(&msgbuf, " "); char *recipient = strsep(&msgbuf, " ");
bool priv = false; bool priv = false;
if(server->nick && strcmp(recipient, server->nick) == 0) { if(server->getNick() && strcmp(recipient, server->getNick()) == 0) {
priv = true; priv = true;
} }
......
...@@ -141,8 +141,7 @@ void _new_server(char *hostname) { ...@@ -141,8 +141,7 @@ void _new_server(char *hostname) {
exit(1); exit(1);
} }
server *newserver = new server(); server *newserver = new server(hostname);
newserver->hostname = strdup(hostname);
newserver->fd = socket_fd; newserver->fd = socket_fd;
control_fd = 0; control_fd = 0;
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
#include <stdarg.h> #include <stdarg.h>
/* {{{ DADY */ /* {{{ 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) { switch(tagtype) {
case 'RBUF': case 'RBUF':
if(len > 0) { if(len > 0) {
...@@ -37,8 +37,8 @@ static int _serv_internal_tag_cb(uint32_t tagtype, uint32_t len, uint8_t *data, ...@@ -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 }; static struct dady_callback _serv_internal_callbacks = { NULL, (tag_callback)&_serv_internal_tag_cb };
/* }}} */ /* }}} */
server::server() : server::server(const char *hostname) :
hostname(NULL), hostname(strdup(hostname)),
nick(NULL), nick(NULL),
readbuf(new char[READ_BUF_LEN]), readbuf(new char[READ_BUF_LEN]),
nbytes_read(0), nbytes_read(0),
...@@ -81,6 +81,16 @@ server::~server() { ...@@ -81,6 +81,16 @@ server::~server() {
free(this->nick); 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, ...) { void server::printf(const char *format, ...) {
char *intermediate; char *intermediate;
......
...@@ -3,10 +3,11 @@ ...@@ -3,10 +3,11 @@
#include <stdint.h> #include <stdint.h>
class server { class server {
public: private:
char *hostname; char *hostname;
char *nick; char *nick;
public:
int fd; int fd;
char *readbuf; char *readbuf;
unsigned long long int nbytes_read; unsigned long long int nbytes_read;
...@@ -14,13 +15,25 @@ public: ...@@ -14,13 +15,25 @@ public:
char *writebuf; char *writebuf;
unsigned long long int nbytes_write; unsigned long long int nbytes_write;
server(); public:
server(const char *hostname);
server(uint8_t *data, uint32_t size); server(uint8_t *data, uint32_t size);
/* {{{ Accessors */
const char *getHostname();
const char *getNick();
void setNick(const char *newNick);
/* }}} */
void printf(const char *format, ...); void printf(const char *format, ...);
uint8_t *freeze(uint32_t *outlen); uint8_t *freeze(uint32_t *outlen);
~server(); ~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 */ #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