irctk

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

commit 184782d97f9b3add672905074e6dfa04a53224e2
parent b914bb239869bf18bb10e01e91f18466d93547a6
Author: Antoine Amarilli <a3nm@a3nm.net>
Date:   Wed, 11 Jul 2012 23:01:48 +0200

error handling

Diffstat:
irctk.c | 95++++++++++++++++++++++++++++++++++++++++++++++++-------------------------------
1 file changed, 58 insertions(+), 37 deletions(-)

diff --git a/irctk.c b/irctk.c @@ -1037,11 +1037,19 @@ char* consume_word(char **from) return result; } +int do_say(irc_session_t *s, char *chan, char *line) { + /* TODO2 only join channels we haven't joined yet */ + if (args.join) + join_channel(s, chan); + return irc_cmd_msg(s, chan, line); +} + + /* Send a message or do a command */ /* Return value is 0 if OK, >0 if error, -1 if correctly exited */ -int do_cmd_msg(irc_session_t *s, char* chan, char* line) +int do_cmd_msg(irc_session_t *s, char *chan, char* line) { - int rsl = -1; + int rsl = 0; int is_local = 1; debug("do_cmd_msg %s %s\n", chan, line); @@ -1049,10 +1057,6 @@ int do_cmd_msg(irc_session_t *s, char* chan, char* line) /* TODOponey ask the server to notify, if possible */ /* TODO2 find a way to get real host name (part after !) */ - /* TODO2 only join channels we haven't joined yet */ - if (args.join) - join_channel(s, chan); - /* TODO2 provide a means to escape '/' */ if (line[0] == '/' && args.command_to_event) { char *arg; @@ -1063,22 +1067,30 @@ int do_cmd_msg(irc_session_t *s, char* chan, char* line) rsl = irc_cmd_part(s, chan); } else if (*arg == ' ') { rsl = irc_cmd_part(s, arg); + } else { + info("Unrecognized command: %s", line); } } else if ((arg = MATCH_CMD0(line, "join"))) { - if (args.join) { - if (!*arg || *arg == '\n') { - debug("join %s", chan); - rsl = join_channel(s, chan); - } else if (*arg == ' ') { - debug("join %s", arg); - rsl = join_channel(s, arg); + char *to_join = NULL; + if (!*arg || *arg == '\n') { + to_join = chan; + } else if (*arg == ' ') { + to_join = arg; + } + if (to_join) { + if (args.join) { + debug("join %s", to_join); + rsl = join_channel(s, to_join); + } else { + info("Will not join because --no-auto-join was set"); } + } else { + info("Unrecognized command: %s", line); } } else if ((arg = MATCH_CMD(line, "topic"))) { debug("topic %s", arg); rsl = irc_cmd_topic(s, chan, arg); } else if ((arg = MATCH_CMD0(line, "quit"))) { - // TODO notify main thread that is is over if (IS_END(*arg)) { rsl = irc_cmd_quit(s, NULL); if (!rsl) @@ -1087,11 +1099,15 @@ int do_cmd_msg(irc_session_t *s, char* chan, char* line) rsl = irc_cmd_quit(s, arg); if (!rsl) rsl = -1; + } else { + info("Unrecognized command: %s", line); } } else if ((arg = MATCH_CMD(line, "invite"))) { char *inick = consume_word(&arg); if (!IS_END(*arg)) { rsl = irc_cmd_invite(s, inick, arg); + } else { + info("Not enough arguments for command: %s", line); } free(inick); } else if ((arg = MATCH_CMD(line, "kick"))) { @@ -1100,14 +1116,20 @@ int do_cmd_msg(irc_session_t *s, char* chan, char* line) free(knick); } else if ((arg = MATCH_CMD0(line, "say"))) { // just a way to escape messages starting by '/' - rsl = irc_cmd_msg(s, chan, arg); + rsl = do_say(s, chan, arg); } else if (line[1] == ' ') { // another way, compatible with irssi: "/ /message" - rsl = irc_cmd_msg (s, chan, line + 2); + rsl = do_say(s, chan, line + 2); + } else { + info("Unrecognized or malformed command: %s", line); } // TODO: names, list, topic, chanmode, usermode, whois, raw... // with correct user tracking! } else { + /* TODO2 only join channels we haven't joined yet */ + if (args.join) + join_channel(s, chan); + if (args.track != NO) { char lline[MAX_LEN + MAX_NICK_LEN + 2]; int seppos = -1; @@ -1121,7 +1143,7 @@ int do_cmd_msg(irc_session_t *s, char* chan, char* line) break; } if (seppos == -1) { - rsl = irc_cmd_msg (s, chan, line); + rsl = do_say(s, chan, line); } else { char sep; sep = line[seppos]; @@ -1136,10 +1158,10 @@ int do_cmd_msg(irc_session_t *s, char* chan, char* line) pthread_mutex_unlock(&fifos.ctrl.mutex); line[seppos] = sep; strncat(lline, line + seppos, MAX_LEN-1); - rsl = irc_cmd_msg (s, chan, lline); + rsl = do_say(s, chan, lline); } } else { - rsl = irc_cmd_msg (s, chan, line); + rsl = do_say(s, chan, line); } } if (rsl) @@ -1259,7 +1281,7 @@ void saw_user(const char *nick) { void manage_event (irc_session_t *session, const char *event, const char *origin, const char **params, unsigned int count) { - int rsl; // TODO use + int rsl = 0; // TODO use saw_user(origin); @@ -1270,7 +1292,7 @@ void manage_event (irc_session_t *session, const char *event, const char *origin // TODO does not work info("Invalid nick, reverting to \"irctk\""); strncpy(args.nick, "irctk", MAX_NICK_LEN-1); - rsl = irc_cmd_nick (session, "irctk"); + rsl = irc_cmd_nick(session, "irctk"); } } else if (atoi(event) == LIBIRC_RFC_ERR_NICKCOLLISION || atoi(event) == LIBIRC_RFC_ERR_NICKNAMEINUSE) { @@ -1285,22 +1307,24 @@ void manage_event (irc_session_t *session, const char *event, const char *origin args.nick[len+1] = 0; // no need to rename self because the surface_name of ourself points // to args.nick - rsl = irc_cmd_nick (session, args.nick); + rsl = irc_cmd_nick(session, args.nick); info("Requested nick is taken, try %s\n", args.nick, args.nick); } } } else if (atoi(event) == LIBIRC_RFC_ERR_BADCHANNELKEY) { - // TODO report name of channel last joined, die if it was the one specified - // on CLI - info("Cannot join channel: bad password."); + info("Cannot join %s: bad password.", params[1]); } else if (atoi(event) == LIBIRC_RFC_ERR_NOSUCHCHANNEL) { - info("Cannot join channel: no such channel."); + info("Cannot join %s: no such channel.", params[1]); } else if (atoi(event) == LIBIRC_RFC_ERR_TOOMANYCHANNELS) { - info("Cannot join channel: you have joined too many channels."); + info("Cannot join %s: you have joined too many channels.", params[1]); } else if (atoi(event) == LIBIRC_RFC_ERR_INVITEONLYCHAN) { - info("Cannot join channel: channel is invite-only."); + info("Cannot join %s: channel is invite-only.", params[1]); } else if (atoi(event) == ERR_BADCHANNAME) { - info("Could not join channel."); + info("Could not join %s.", params[1]); + } else if (atoi(event) == LIBIRC_RFC_ERR_USERONCHANNEL) { + info("Cannot invite %s to %s: %s is already on %s.", params[1], params[2], params[1], params[2]); + } else if (atoi(event) == LIBIRC_RFC_ERR_NOSUCHNICK) { + info("No such nick/channel: %s", params[1]); } if (count == 2) { @@ -1311,6 +1335,11 @@ void manage_event (irc_session_t *session, const char *event, const char *origin debug("Event \"%s\", origin: \"%s\", params: %d", event, origin ? origin : "NULL", count); } + if (rsl > 0) { + int error = irc_errno(session); + info("(%d) %s when managing event: %s", + error, irc_strerror(error), event); + } } // Handle a nick event @@ -1700,14 +1729,6 @@ int start (int max_wait) return start(max_wait); } - /* TODO no wait on empty lines */ - /* TODO separate delay per channel */ - // gettimeofday(&tp2, NULL); - // tp = 1000000*(tp2.tv_sec - tp1.tv_sec) + tp2.tv_usec - tp1.tv_usec; - // if (tp < args.interval && !first) - // usleep(args.interval - tp); - // first = 0; - if (args.show_inferred && args.default_destination == DEFAULT_LAST_OUT) fprintf(stderr, "[%s] ", args.last_chans_out);