#include "logwatch.h"
#include "../str.h"
#include <assert.h>
bool logwatch_chan_has_source (struct logwatch_chan *chan, const struct logwatch_source *source)
{
const struct logwatch_filter *filter;
// look for a link
TAILQ_FOREACH(filter, &chan->ctx->filters, logwatch_filters)
if (filter->chan == chan && filter->source == source)
return true;
// no match
return false;
}
struct logwatch_chan* logwatch_chan_lookup (struct logwatch *ctx, struct irc_chan *irc_chan)
{
struct logwatch_chan *chan;
// look for it
TAILQ_FOREACH(chan, &ctx->channels, logwatch_channels)
if (chan->irc_chan == irc_chan)
return chan;
// not found
return NULL;
}
/**
* Destroy a logwatch_chan
*/
static void logwatch_chan_destroy (struct logwatch_chan *chan)
{
// XXX: check refcount
assert(chan->refcount == 0);
// remove
TAILQ_REMOVE(&chan->ctx->channels, chan, logwatch_channels);
// release
free(chan);
}
struct logwatch_chan* logwatch_chan_create (struct logwatch *ctx, struct irc_chan *irc_chan)
{
struct logwatch_chan *chan;
// alloc
if ((chan = calloc(1, sizeof(*chan))) == NULL)
return NULL;
// store
chan->ctx = ctx;
chan->irc_chan = irc_chan;
chan->refcount = 1;
// add
TAILQ_INSERT_TAIL(&ctx->channels, chan, logwatch_channels);
// ok
return chan;
}
void logwatch_chan_put (struct logwatch_chan *chan)
{
// valid refcount
assert(chan->refcount);
// still alive?
if (--chan->refcount)
return;
// destroy
logwatch_chan_destroy(chan);
}
err_t logwatch_chan_msg (struct logwatch_chan *chan, const char *fmt, ...)
{
char buf[LOGWATCH_CHAN_MSG_MAX + 1];
va_list vargs;
// format the output
// XXX: overflow...
va_start(vargs, fmt);
str_append_fmt_va(buf, sizeof(buf), fmt, vargs);
va_end(vargs);
// send it
return irc_chan_NOTICE(chan->irc_chan, buf);
}