#include "irc_net.h"
#include "log.h"
#include <stdlib.h>
#include <string.h>
err_t irc_net_create (struct irc_net **net_ptr, const struct irc_net_info *info, struct error_info *err)
{
struct irc_net *net;
struct sock_stream *sock;
// allocate
if ((net = calloc(1, sizeof(*net))) == NULL)
return SET_ERROR(err, ERR_CALLOC);
// initialize
TAILQ_INIT(&net->channels);
// XXX: over-simplified blocking connect
if (info->use_ssl) {
log_info("connecting to [%s]:%s using SSL", info->hostname, info->service);
if (sock_ssl_connect(&sock, info->hostname, info->service, err))
goto error;
} else {
log_info("connecting to [%s]:%s", info->hostname, info->service);
if (sock_tcp_connect(&sock, info->hostname, info->service, err))
goto error;
}
log_info("connected, registering");
// create the irc connection state
if (irc_conn_create(&net->conn, sock, &info->register_info, err))
goto error;
// ok
*net_ptr = net;
return SUCCESS;
error:
// XXX: cleanup
return ERROR_CODE(err);
}
struct irc_chan* irc_net_add_chan (struct irc_net *net, const struct irc_chan_info *info)
{
struct irc_chan *chan;
struct error_info err;
// create the new irc_chan struct
if (irc_chan_create(&chan, net, info, &err))
// XXX: we lose error info
return NULL;
// add to network list
TAILQ_INSERT_TAIL(&net->channels, chan, node);
// then join
if ((ERROR_CODE(&err) = irc_chan_join(chan)))
// XXX
return NULL;
// ok, return
return chan;
}
struct irc_chan* irc_net_get_chan (struct irc_net *net, const char *channel)
{
struct irc_chan *chan = NULL;
// look for it...
TAILQ_FOREACH(chan, &net->channels, node) {
if (strcasecmp(chan->info.channel, channel) == 0)
// found it
return chan;
}
// no such channel
return NULL;
}