libsrsirc
0.0.14
Lightweight, cross-platform IRC library
|
This is the basic interface to the core facilities of libsrsirc. More...
Functions | |
irc * | irc_init (void) |
Allocate and initialize a new IRC context. More... | |
void | irc_dispose (irc *ctx) |
Destroy an IRC context, invalidating the provided pointer. More... | |
bool | irc_connect (irc *ctx) |
Connect and log on to IRC. More... | |
void | irc_reset (irc *ctx) |
Force a disconnect from the IRC server. More... | |
bool | irc_online (irc *ctx) |
Tell whether or not we (think we) are connected. More... | |
int | irc_read (irc *ctx, tokarr *tok, uint64_t to_us) |
Read and process the next protocol message from the IRC server. More... | |
bool | irc_write (irc *ctx, const char *line) |
Send a protocol message to the IRC server. More... | |
bool | irc_printf (irc *ctx, const char *fmt,...) |
Send a formatted message to the IRC server, printf-style. More... | |
const char * | irc_mynick (irc *ctx) |
Tell what our nickname is or was. More... | |
bool | irc_set_server (irc *ctx, const char *host, uint16_t port) |
Set IRC server and port to connect to. More... | |
bool | irc_set_pass (irc *ctx, const char *srvpass) |
Set server password (NULL means no password). More... | |
bool | irc_set_uname (irc *ctx, const char *uname) |
set IRC 'user name' (not the same as nickname). More... | |
bool | irc_set_fname (irc *ctx, const char *fname) |
set IRC 'full name' for the next connection. More... | |
bool | irc_set_nick (irc *ctx, const char *nick) |
set IRC 'nickname' for the next connection. More... | |
void | irc_dump (irc *ctx) |
Dump state for debugging purposes. More... | |
This is the basic interface to the core facilities of libsrsirc.
Everything needed to get an IRC connection going and do simple IRC operations is provided by this header.
Usage example for a simple ECHO-like IRC bot (receives messages and sends them back to the channel it saw them on):
Assuming that code is saved as ircecho.c and libsrsirc was installed under the /usr/local prefix (./configure --prefix=/usr/local
), you'd build it as follows:
And run as:
If that doesn't seem to work, run it with debugging messages as follows:
Have fun!
irc* irc_init | ( | void | ) |
Allocate and initialize a new IRC context.
This is the function you will typically use before calling any other libsrsirc function. It allocates and initializes a new IRC context that is used internally to hold all the state associated with this IRC connection, as well as to distinguish it from other, unrelated IRC connections made using libsrsirc.
An opaque pointer (i.e. a handle) to the new IRC context is returned to the user and will remain valid until irc_dispose() is called for that pointer. The user will generally need to supply this pointer as the first argument to most libsrsirc functions, but will never dereference it themselves. (This is analoguous to how FILE * works in standard C.)
This function can be called an arbitrary number of times, if there is a need for many independent IRC connections using libsrsirc. However, most programs will probably need only one IRC connection and therefore call this function only once.
If the IRC context is no longer required, call irc_dispose() on it to release the associated state and resources.
void irc_dispose | ( | irc * | ctx | ) |
Destroy an IRC context, invalidating the provided pointer.
This is the counterpart to irc_init(). Calling this function will, after an implicit call to irc_reset(), deallocate all resources associated with the provided context, invalidating it in the process.
Use this function when you don't need your IRC context anymore, maybe as part of other cleanup your program does when terminating.
ctx | IRC context as obtained by irc_init() |
bool irc_connect | ( | irc * | ctx | ) |
Connect and log on to IRC.
Attempts to connect/log on to the IRC server specified earlier using irc_set_server().
Connecting will involve, if applicable, resolving a DNS name and attempting to establish a TCP connection to each of the resulting A- or AAAA records (until one works for us).
Behavior is affected by two timeouts (a "hard" and a "soft" timeout) The "hard" timeout limits the overall time we try to get an IRC connection going, while the "soft" timeout applies each time anew for each attempt to establish a TCP connection (so that we can be sure all A- or AAAA records are tried) (see irc_set_connect_timeout() for further information regarding the soft timeout).
We consider ourselves logged on once the IRC server sends us a "004" message (or 383 if we're a service). If of interest, the "log on conversation" i.e. 001, 002, 003 and 004 can be retrieved by irc_logonconv() if there is a need to see them (there usually isn't).
ctx | IRC context as obtained by irc_init() |
void irc_reset | ( | irc * | ctx | ) |
Force a disconnect from the IRC server.
Contrary to what the name suggests, this function does not reset any other state apart from the TCP connection, so functions like irc_lasterror(), irc_mynick() etc. will keep returning the information they would have returned when the connection was still alive.
The mentioned kind of information will be reset not before the next call to irc_connect()
ctx | IRC context as obtained by irc_init() |
bool irc_online | ( | irc * | ctx | ) |
Tell whether or not we (think we) are connected.
Of course, we can't /really/ know unless we attempt to do I/O with the server by calling irc_read() (or perhaps irc_write()).
The following functions have the ability to change our idea of whether or not we're connected: irc_connect(), irc_read(), irc_write(), irc_printf(), irc_reset().
ctx | IRC context as obtained by irc_init() |
Read and process the next protocol message from the IRC server.
Reads a protocol message from the server, field-splits it and populates the tokarr structure pointed to by tok
with the results.
Usage example:
ctx | IRC context as obtained by irc_init() |
tok | Pointer to the result array, where pointers to the identified fields are stored in. (*tok)[0] will point to the "prefix" (not including the leading colon), or NULL if the message did not contain a prefix (see also doc/terminology.txt). (*tok)[1] will point to the (mandatory) "command" (*tok)[2+n] will point to the n-th "argument", if it exists; NULL otherwise |
to_us | Read timeout in microseconds. 0 means no timeout, 1 is special in that it will effectively case a poll (return immediately when there's nothing to read yet). Note that very small timeouts >1 will likely NOT cause satisfactory results, depending on system speed, because the timeout might expire before the first actual call to select(2) (or equivalent). |
The data pointed to by the elements of tok
is valid until the next call to this function is made.
In the case of failure, an implicit call to irc_reset() is performed.
bool irc_write | ( | irc * | ctx, |
const char * | line | ||
) |
Send a protocol message to the IRC server.
ctx | IRC context as obtained by irc_init() |
line | Data to send, typically a single IRC protocol line (but may be multiple if properly separated by \r\n). If the (last or only) line does not end in \r\n, it will be appended. |
In the case of failure, an implicit call to irc_reset() is performed.
bool irc_printf | ( | irc * | ctx, |
const char * | fmt, | ||
... | |||
) |
Send a formatted message to the IRC server, printf-style.
ctx | IRC context as obtained by irc_init() |
fmt | A printf-style format string |
After evaluating the format string and its respective arguments, irc_write() is used to actually send the message.
In the case of failure, an implicit call to irc_reset() is performed.
const char* irc_mynick | ( | irc * | ctx | ) |
Tell what our nickname is or was.
We keep track of changes to our own nickname, this function can be used to tell what our current nick is, or what our last nickname was before we (got) disconnected.
ctx | IRC context as obtained by irc_init() |
When used before the first call to irc_connect(), the returned pointer will point to an empty string.
bool irc_set_server | ( | irc * | ctx, |
const char * | host, | ||
uint16_t | port | ||
) |
Set IRC server and port to connect to.
This setting will take effect not before the next call to irc_connect()
ctx | IRC context as obtained by irc_init() |
host | Server host, may be an IPv4 or IPv6 address or a DNS name. It is an error to pass NULL. We do not depend on this pointer to remain valid after we return, i.e. we do make a copy. |
port | Server port (0 uses default (6667 or 6697 (with SSL))) |
In case of failure, the old value is left unchanged.
bool irc_set_pass | ( | irc * | ctx, |
const char * | srvpass | ||
) |
Set server password (NULL means no password).
This setting will take effect not before the next call to irc_connect().
ctx | IRC context as obtained by irc_init() |
srvpass | Server password for the next IRC connection. Passing NULL or a pointer to an empty string means not to use any server password whatsoever. We do not depend on this pointer to remain valid after we return, i.e. we do make a copy. |
In case of failure, the old value is left unchanged.
bool irc_set_uname | ( | irc * | ctx, |
const char * | uname | ||
) |
set IRC 'user name' (not the same as nickname).
The 'username' is the middle part of an IRC identity (nickname!username@host
) (see also doc/terminology.txt).
This setting will take effect not before the next call to irc_connect().
ctx | IRC context as obtained by irc_init() |
uname | Desired username for the next IRC connection. It is an error to pass NULL, but there is a default username in case this function is not used at all. We do not depend on this pointer to remain valid after we return, i.e. we do make a copy. |
In case of failure, the old value is left unchanged.
bool irc_set_fname | ( | irc * | ctx, |
const char * | fname | ||
) |
set IRC 'full name' for the next connection.
The 'full name' is typically shown in responses to the WHOIS IRC command.
This setting will take effect not before the next call to irc_connect()
ctx | IRC context as obtained by irc_init() |
fname | Desired full name for the next IRC connection. It is an error to pass NULL, but there is a default full name in case this function is not used at all. We do not depend on this pointer to remain valid after we return, i.e. we do make a copy. |
In case of failure, the old value is left unchanged.
bool irc_set_nick | ( | irc * | ctx, |
const char * | nick | ||
) |
set IRC 'nickname' for the next connection.
The nickname is the primary means to identify users on IRC.
This setting will take effect not before the next call to irc_connect(), i.e. it does not cause an on-line nick change. (To do that, send a NICK message using irc_write() or irc_printf())
ctx | IRC context as obtained by irc_init() |
nick | Desired nickname for the next IRC connection. It is an error to pass NULL, but there is a default nickname in case this function is not used at all. We do not depend on this pointer to remain valid after we return, i.e. we do make a copy. |
In case of failure, the old value is left unchanged.
void irc_dump | ( | irc * | ctx | ) |
Dump state for debugging purposes.
This function is intended for debugging and will puke out a dump of all state associated with the given context to stderr.
ctx | IRC context as obtained by irc_init() |