irctk

libircclient binding for scripts
git clone https://a3nm.net/git/irctk/
Log | Files | Refs | README

commit f786cde676105af9b228082d86b9cdabf799f35c
parent 9092523ce9df9d410eb4b7276eaed09f60402642
Author: Antoine Amarilli <ant.amarilli@free.fr>
Date:   Sun, 13 Feb 2011 17:52:30 +0100

add -n -m -c (manage events)

Diffstat:
irctk.c | 146++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------
1 file changed, 107 insertions(+), 39 deletions(-)

diff --git a/irctk.c b/irctk.c @@ -36,9 +36,14 @@ #define DEFAULT_LAST_OUT 2 #define DEFAULT_ALL 3 +#define NOTHING 0 +#define COMMAND 1 +#define MESSAGE 2 + // TODO get rid of that #define MAX_LEN 4096 +// TODO pseudo tracking // TODO customize full name, username... @@ -73,13 +78,16 @@ static struct argp_option options[] = { PARSING, }, // TODO {"no-destination-prefix", 0, 0, 0, "Don't look for []", - PARSING}, // TODO + PARSING}, // TODO */ + {"event-to-nothing", 'n', 0, 0, + "Ignore server events (default).", + PARSING}, // TODO {"event-to-command", 'c', 0, 0, "Translate server events to messages with \"/quit\", \"/me\"...", PARSING}, // TODO {"event-to-message", 'm', 0, 0, "Translate server events to a human-readable description", - PARSING}, // TODO */ + PARSING}, // TODO {"with-host", 'w', 0, 0, "Keep the host info in pseudos" , DISPLAY_SELECTION}, @@ -152,6 +160,7 @@ struct arguments int filter; int prune; int command_to_event; + int event_to; }; typedef struct arguments irc_ctx_t; @@ -178,6 +187,7 @@ void initialize_args() args.last_chans_out[0] = 0; args.default_destination = DEFAULT_LAST_IN; args.show_prefix = 0; + args.event_to = NOTHING; } @@ -198,6 +208,15 @@ static error_t parse_opt (int key, char *arg, struct argp_state *state) case 'q': arguments->verbosity = -1; break; + case 'n': + arguments->event_to = NOTHING; + break; + case 'c': + arguments->event_to = COMMAND; + break; + case 'm': + arguments->event_to = MESSAGE; + break; case 'i': arguments->interval = (int) (atof(arg) * 1000000.); break; @@ -480,63 +499,112 @@ THREAD_FUNCTION(irc_listen) void dump_event (irc_session_t * session, const char * event, const char * origin, const char ** params, unsigned int count) { - char buf[512]; - int cnt; + char buf[512]; // TODO danger + int cnt; - buf[0] = '\0'; + buf[0] = '\0'; - for ( cnt = 0; cnt < count; cnt++ ) - { - if ( cnt ) - strcat (buf, "|"); + for ( cnt = 0; cnt < count; cnt++ ) + { + if ( cnt ) + strcat (buf, "|"); - strcat (buf, params[cnt]); - } + strcat (buf, params[cnt]); + } - debug("Event \"%s\", origin: \"%s\", params: %d [%s]", event, origin ? origin : "NULL", cnt, buf); + debug("Event \"%s\", origin: \"%s\", params: %d [%s]", event, origin ? origin : "NULL", cnt, buf); } +void event_nick (irc_session_t * session, const char * event, const char * origin, const char ** params, unsigned int count) +{ + if (args.event_to == COMMAND) + printf("[] <%s> %s\n", origin, params[0]); + if (args.event_to == MESSAGE) + printf("[] -!- %s is now known as %s\n", origin, params[0]); +} void event_join (irc_session_t * session, const char * event, const char * origin, const char ** params, unsigned int count) { - dump_event (session, event, origin, params, count); - irc_cmd_user_mode (session, "+i"); + // TODO check if it is us! + dump_event (session, event, origin, params, count); + irc_cmd_user_mode (session, "+i"); - debug("Joined!"); + if (args.event_to == COMMAND) + printf("[%s] <%s> /join %s\n", params[0], origin, params[0]); + if (args.event_to == MESSAGE) + printf("[%s] -!- %s has joined %s\n", params[0], origin, params[0]); - /* Check for that before talking */ + debug("Joined!"); +} - /*irc_ctx_t * ctx = (irc_ctx_t *) irc_get_ctx (session); +void event_part (irc_session_t * session, const char * event, const char * origin, const char ** params, unsigned int count) +{ + if (args.event_to == COMMAND) + printf("[%s] <%s> /part %s\n", params[0], origin, params[1]); + if (args.event_to == MESSAGE) + printf("[%s] -!- %s has left %s (%s)\n", params[0], origin, params[0], params[1]); +} - if ( !origin ) - return;*/ +void event_topic (irc_session_t * session, const char * event, const char * origin, const char ** params, unsigned int count) +{ + if (args.event_to == COMMAND) + printf("[%s] <%s> /topic %s\n", params[0], origin, params[1]); + if (args.event_to == MESSAGE) + printf("[%s] -!- %s changed the topic of %s to %s\n", params[0], origin, params[0], params[1]); +} + +void event_quit (irc_session_t * session, const char * event, const char * origin, const char ** params, unsigned int count) +{ + if (args.event_to == COMMAND) + printf("[] <%s> /quit %s\n", origin, params[0]); + if (args.event_to == MESSAGE) + printf("[] -!- %s has quit (%s)\n", origin, params[1]); +} + +void event_ctcp_action (irc_session_t * session, const char * event, const char * origin, const char ** params, unsigned int count) +{ + //TODO where is the channel?! + if (args.event_to == COMMAND) + printf("[%s] <%s> /me %s\n", params[0], origin, params[1]); + if (args.event_to == MESSAGE) + printf("[%s] *** %s %s\n", params[0], origin, params[1]); +} +void event_kick (irc_session_t * session, const char * event, const char * origin, const char ** params, unsigned int count) +{ + // TODO check for optional params + if (args.event_to == COMMAND) + printf("[%s] <%s> /kick %s %s\n", params[0], origin, params[1], params[2]); + if (args.event_to == MESSAGE) + printf("[%s] -!- %s was kicked from %s by %s (%s)\n", params[0], params[1], params[0], origin, params[2]); } +// TODO manage other events! +// TODO manage channel passwords void event_connect (irc_session_t * session, const char * event, const char * origin, const char ** params, unsigned int count) { - irc_ctx_t * ctx = (irc_ctx_t *) irc_get_ctx (session); - dump_event (session, event, origin, params, count); + irc_ctx_t * ctx = (irc_ctx_t *) irc_get_ctx (session); + dump_event (session, event, origin, params, count); - ctx->ready = 1; - debug("Connected!"); + ctx->ready = 1; + debug("Connected!"); - int i; - for (i = 0; i < ctx->n_channels; i++) - { - debug("Attempt to join %s", ctx->channels[i]); - irc_cmd_join (session, ctx->channels[i], 0); - } + int i; + for (i = 0; i < ctx->n_channels; i++) + { + debug("Attempt to join %s", ctx->channels[i]); + irc_cmd_join (session, ctx->channels[i], 0); + } } void event_privmsg (irc_session_t * session, const char * event, const char * origin, const char ** params, unsigned int count) { - dump_event (session, event, origin, params, count); + dump_event (session, event, origin, params, count); - /* TODO sure? */ - event_channel (session, event, origin, params, count); + /* TODO sure? */ + event_channel (session, event, origin, params, count); } @@ -599,7 +667,7 @@ void dcc_file_recv_callback (irc_session_t * session, irc_dcc_t id, int status, void event_channel (irc_session_t * session, const char * event, const char * origin, const char ** params, unsigned int count) { - //char nickbuf[128]; + //TODO if event_to == COMMAND, escape with "/ /blah" int i=0; int ok = 1, ok2; @@ -726,19 +794,19 @@ irc_session_t* do_connect() callbacks.event_connect = event_connect; callbacks.event_join = event_join; - callbacks.event_nick = dump_event; - callbacks.event_quit = dump_event; - callbacks.event_part = dump_event; + callbacks.event_nick = event_nick; + callbacks.event_quit = event_quit; + callbacks.event_part = event_part; callbacks.event_mode = dump_event; - callbacks.event_topic = dump_event; - callbacks.event_kick = dump_event; + callbacks.event_topic = event_topic; + callbacks.event_kick = event_kick; callbacks.event_channel = event_channel; callbacks.event_privmsg = event_privmsg; callbacks.event_notice = dump_event; callbacks.event_invite = dump_event; callbacks.event_umode = dump_event; callbacks.event_ctcp_rep = dump_event; - callbacks.event_ctcp_action = dump_event; + callbacks.event_ctcp_action = event_ctcp_action; callbacks.event_unknown = dump_event; callbacks.event_numeric = event_numeric;