cvs: php4 /ext/ircg ircg.c

From: Date: Sat, 01 Sep 2001 18:23:14 +0000
Subject: cvs: php4 /ext/ircg ircg.c
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-7153@lists.php.net to get a copy of this message
sas Sat Sep 1 14:23:14 2001 EDT Modified files: /php4/ext/ircg ircg.c Log: add a seventh parameter to ircg_pconnect to treat errors such as incorrect channel-keys in a non-fatal way. add a modifier to format-messages which stops the execution of the format-message, if the argument is not equal to the username. this is useful, if you want a client to only process channel-mode changes which concern the client (e.g. voice-change).

Index: php4/ext/ircg/ircg.c diff -u php4/ext/ircg/ircg.c:1.106 php4/ext/ircg/ircg.c:1.107 --- php4/ext/ircg/ircg.c:1.106 Tue Aug 21 08:57:51 2001 +++ php4/ext/ircg/ircg.c Sat Sep 1 14:23:13 2001 @@ -16,7 +16,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: ircg.c,v 1.106 2001/08/21 12:57:51 zeev Exp $ */ +/* $Id: ircg.c,v 1.107 2001/09/01 18:23:13 sas Exp $ */ #include <time.h> @@ -141,6 +141,7 @@ php_fmt_msgs_t *fmt_msgs; irc_write_buf wb; HashTable ctcp_msgs; + char bailout_on_trivial; /* Whether to handle trivial errors as fatal */ char *ident; /* NOT available outside of ircg_pconnect or register_hooks */ char *password; /* dito */ char *realname; /* dito */ @@ -163,7 +164,8 @@ P_MIRC = 4, P_MIRC_JS = 5, P_NOAUTO_LINKS = 8, /* Don't automatically convert links */ - P_CONV_BR = 16 /* Convert a special character to <br> */ + P_CONV_BR = 16, /* Convert a special character to <br> */ + P_COND_STOP = 32, /* If argument != username, stop */ }; static php_fmt_msgs_t fmt_msgs_default_compiled; @@ -182,9 +184,9 @@ "", "%f changes nick to %t<br />", "%f quits (%m)<br />", - "Welcome", + "Welcome to channel %c:", " %f", - " in this very fine channel", + " in this very fine channel %c", "%f: user(%t) host(%c) real name(%m)<br />", "%f: server(%c) server info(%m)<br />", "%f has been idle for %m seconds<br />", @@ -202,9 +204,9 @@ #define MSG(conn, type) \ (&conn->fmt_msgs->fmt_msgs[type]) -#define FORMAT_MSG(conn, type, chan, to, from, msg, res) { \ +#define FORMAT_MSG(conn, type, chan, to, from, msg, res, u, ulen) { \ format_msg(MSG(conn, type), chan, \ - to, from, msg, res); \ + to, from, msg, res, u, ulen); \ } static php_irconn_t *lookup_irconn(int id) @@ -459,6 +461,7 @@ case '2': mode |= P_NICKNAME; goto next; case '3': mode |= P_NOAUTO_LINKS; goto next; case '4': mode |= P_CONV_BR; goto next; + case '5': mode |= P_COND_STOP; goto next; /* associate mode bits with each command where applicable */ case 'c': NEW_TOKEN(C_CHANNEL, v) = mode; break; @@ -483,17 +486,25 @@ /* {{{ format_msg */ -static void format_msg(const format_msg_t *fmt_msg, smart_str *channel, smart_str *to, smart_str *from, smart_str *msg, smart_str *result) +static void format_msg(const format_msg_t *fmt_msg, smart_str *channel, + smart_str *to, smart_str *from, smart_str *msg, smart_str *result, + const char *username, int username_len) { smart_str encoded_msg = {0}; int encoded = 0; int i = 0; const token_t *t = fmt_msg->t; int ntoken = fmt_msg->ntoken; - + int para; + exec_fmt_msgs++; #define IRCG_APPEND(what) \ + if (t[i].para.v & P_COND_STOP) { \ + if (username_len != what->len || memcmp(what->c, username, username_len) != 0) \ + goto stop; \ + continue; \ + } \ switch (t[i].para.v & 7) { \ case P_JS: \ if (!what) break; \ @@ -539,7 +550,7 @@ break; \ } - while (ntoken-- > 0) { + for (; ntoken -- > 0; i++) { switch (t[i].code) { case C_STRING: smart_str_append_ex(result, t[i].para.ptr, 1); break; case C_FROM: IRCG_APPEND(from); break; @@ -548,9 +559,9 @@ case C_MESSAGE: IRCG_APPEND(msg); break; case C_PERCENT: smart_str_appendc_ex(result, '%', 1); break; } - i++; } - + +stop: if (encoded) smart_str_free_ex(&encoded_msg, 1); @@ -651,7 +662,7 @@ smart_str_setl(&tmp, real_msg, real_msg_end - real_msg); smart_str_setl(&tmp2, conn->conn.username, conn->conn.username_len); - format_msg(fmt_msg, chan, &tmp2, from, &tmp, result); + format_msg(fmt_msg, chan, &tmp2, from, &tmp, result, conn->conn.username, conn->conn.username_len); } } } @@ -669,10 +680,10 @@ if (msg->c[0] == '\001') { handle_ctcp(conn, chan, from, msg, &m); } else if (chan) { - FORMAT_MSG(conn, FMT_MSG_CHAN, chan, &s_username, from, msg, &m); + FORMAT_MSG(conn, FMT_MSG_CHAN, chan, &s_username, from, msg, &m, conn->conn.username, conn->conn.username_len); } else { format_msg(MSG(conn, FMT_MSG_PRIV_TO_ME), NULL, &s_username, from, - msg, &m); + msg, &m, conn->conn.username, conn->conn.username_len); } msg_send(conn, &m); @@ -685,7 +696,7 @@ smart_str m = {0}; format_msg(MSG(conn, FMT_MSG_NICK), NULL, newnick, oldnick, NULL, - &m); + &m, conn->conn.username, conn->conn.username_len); msg_send(conn, &m); } @@ -696,7 +707,7 @@ smart_str m = {0}; format_msg(MSG(conn, FMT_MSG_WHOIS_USER), host, user, nick, - real_name, &m); + real_name, &m, conn->conn.username, conn->conn.username_len); msg_send(conn, &m); } @@ -708,7 +719,7 @@ smart_str m = {0}; format_msg(MSG(conn, FMT_MSG_WHOIS_SERVER), server, NULL, nick, - server_info, &m); + server_info, &m, conn->conn.username, conn->conn.username_len); msg_send(conn, &m); } @@ -720,7 +731,7 @@ smart_str m = {0}; format_msg(MSG(conn, FMT_MSG_WHOIS_IDLE), NULL, NULL, nick, - idletime, &m); + idletime, &m, conn->conn.username, conn->conn.username_len); msg_send(conn, &m); } @@ -729,7 +740,7 @@ php_irconn_t *conn = dummy; smart_str m = {0}; - format_msg(MSG(conn, FMT_MSG_WHOIS_END), NULL, NULL, nick, NULL, &m); + format_msg(MSG(conn, FMT_MSG_WHOIS_END), NULL, NULL, nick, NULL, &m, conn->conn.username, conn->conn.username_len); msg_send(conn, &m); } @@ -743,7 +754,7 @@ for (i = 0; i < nr; i++) { format_msg(MSG(conn, FMT_MSG_WHOIS_CHANNEL), &channels[i], NULL, - nick, NULL, &m); + nick, NULL, &m, conn->conn.username, conn->conn.username_len); } msg_send(conn, &m); } @@ -850,17 +861,29 @@ smart_str m = {0}; smart_str s_username; smart_str tmp; + int disconn = 0; + + if (conn->bailout_on_trivial) { + if (id == 474 || id == 475) { + fatal = disconn = 1; + } + } smart_str_setl(&tmp, "IRC SERVER", sizeof("IRC SERVER") - 1); smart_str_setl(&s_username, ircc->username, ircc->username_len); format_msg(MSG(conn, fatal ? FMT_MSG_FATAL_ERROR : FMT_MSG_ERROR), NULL, - &s_username, &tmp, msg, &m); + &s_username, &tmp, msg, &m, conn->conn.username, conn->conn.username_len); if (fatal) { add_error_msg(msg, id, conn); } msg_send(conn, &m); + + /* Fatal messages from the IRCG layer automatically call irc_disconnect; + if we simulate a fatal error, we need to do that manually */ + if (disconn) + irc_disconnect(ircc, "A fatal error occured"); } static void banlist_handler(irconn_t *ircc, smart_str *channel, smart_str *mask, void *conn_data) @@ -868,7 +891,7 @@ php_irconn_t *conn = conn_data; smart_str m = {0}; - format_msg(MSG(conn, FMT_MSG_BANLIST), channel, NULL, NULL, mask, &m); + format_msg(MSG(conn, FMT_MSG_BANLIST), channel, NULL, NULL, mask, &m, conn->conn.username, conn->conn.username_len); msg_send(conn, &m); } @@ -877,7 +900,7 @@ php_irconn_t *conn = conn_data; smart_str m = {0}; - format_msg(MSG(conn, FMT_MSG_BANLIST_END), channel, NULL, NULL, NULL, &m); + format_msg(MSG(conn, FMT_MSG_BANLIST_END), channel, NULL, NULL, NULL, &m, conn->conn.username, conn->conn.username_len); msg_send(conn, &m); } @@ -890,19 +913,19 @@ if (nr > 1) { format_msg(MSG(conn, FMT_MSG_MASS_JOIN_BEGIN), channel, NULL, NULL, - NULL, &m); + NULL, &m, conn->conn.username, conn->conn.username_len); for (i = 0; i < nr; i++) { format_msg(MSG(conn, FMT_MSG_MASS_JOIN_ELEMENT), channel, NULL, - &users[i], NULL, &m); + &users[i], NULL, &m, conn->conn.username, conn->conn.username_len); } format_msg(MSG(conn, FMT_MSG_MASS_JOIN_END), channel, NULL, NULL, - NULL, &m); + NULL, &m, conn->conn.username, conn->conn.username_len); } else { FORMAT_MSG(conn, FMT_MSG_JOIN, channel, NULL, &users[0], - NULL, &m); + NULL, &m, conn->conn.username, conn->conn.username_len); FORMAT_MSG(conn, FMT_MSG_JOIN_LIST_END, channel, NULL, NULL, - NULL, &m); + NULL, &m, conn->conn.username, conn->conn.username_len); } msg_send(conn, &m); } @@ -912,7 +935,7 @@ php_irconn_t *conn = dummy; smart_str m = {0}; - format_msg(MSG(conn, FMT_MSG_TOPIC), channel, NULL, who, topic, &m); + format_msg(MSG(conn, FMT_MSG_TOPIC), channel, NULL, who, topic, &m, conn->conn.username, conn->conn.username_len); msg_send(conn, &m); } @@ -925,7 +948,7 @@ smart_str_setl(&s_username, ircc->username, ircc->username_len); format_msg(MSG(conn, FMT_MSG_SELF_PART), channel, NULL, &s_username, - NULL, &m); + NULL, &m, conn->conn.username, conn->conn.username_len); msg_send(conn, &m); } @@ -934,7 +957,7 @@ php_irconn_t *conn = dummy; smart_str m = {0}; - format_msg(MSG(conn, FMT_MSG_LEAVE), channel, NULL, user, NULL, &m); + format_msg(MSG(conn, FMT_MSG_LEAVE), channel, NULL, user, NULL, &m, conn->conn.username, conn->conn.username_len); msg_send(conn, &m); } @@ -943,7 +966,7 @@ php_irconn_t *conn = dummy; smart_str m = {0}; - format_msg(MSG(conn, FMT_MSG_QUIT), NULL, NULL, user, msg, &m); + format_msg(MSG(conn, FMT_MSG_QUIT), NULL, NULL, user, msg, &m, conn->conn.username, conn->conn.username_len); msg_send(conn, &m); } @@ -961,7 +984,7 @@ php_irconn_t *conn = dummy; smart_str m = {0}; - format_msg(MSG(conn, FMT_MSG_KICK), channel, who, kicked_by, reason, &m); + format_msg(MSG(conn, FMT_MSG_KICK), channel, who, kicked_by, reason, &m, conn->conn.username, conn->conn.username_len); msg_send(conn, &m); } @@ -981,11 +1004,11 @@ if (mode & IRCG_MODE_VOICE) { format_msg(MSG(conn, FMT_MSG_MODE_VOICE), channel, nick, who, &what, - &m); + &m, conn->conn.username, conn->conn.username_len); msg_send(conn, &m); } if (mode & IRCG_MODE_OP) { - format_msg(MSG(conn, FMT_MSG_MODE_OP), channel, nick, who, &what, &m); + format_msg(MSG(conn, FMT_MSG_MODE_OP), channel, nick, who, &what, &m, conn->conn.username, conn->conn.username_len); msg_send(conn, &m); } } @@ -1543,18 +1566,22 @@ PHP_FUNCTION(ircg_pconnect) { /* This should become an array very soon */ - zval **p1, **p2, **p3, **p4 = NULL, **p5 = NULL, **p6; + zval **p1, **p2, **p3, **p4 = NULL, **p5 = NULL, **p6, **p7; const char *username = 0; const char *server = "0"; int port = 6667; php_fmt_msgs_t *fmt_msgs = NULL; php_irconn_t *conn; + int bailout_on_trivial = 1; - if (ZEND_NUM_ARGS() < 1 || ZEND_NUM_ARGS() > 6 - || zend_get_parameters_ex(ZEND_NUM_ARGS(), &p1, &p2, &p3, &p4, &p5, &p6) == FAILURE) + if (ZEND_NUM_ARGS() < 1 || ZEND_NUM_ARGS() > 7 + || zend_get_parameters_ex(ZEND_NUM_ARGS(), &p1, &p2, &p3, &p4, &p5, &p6, &p7) == FAILURE) WRONG_PARAM_COUNT; switch (ZEND_NUM_ARGS()) { + case 7: + convert_to_boolean_ex(p7); + bailout_on_trivial = Z_LVAL_PP(p7); case 6: case 5: case 4: @@ -1739,7 +1766,7 @@ if (l.c[0] == 1) { handle_ctcp(conn, &tmp, &tmp2, &l, &m); } else { - FORMAT_MSG(conn, FMT_MSG_CHAN, &tmp, NULL, &tmp2, &l, &m); + FORMAT_MSG(conn, FMT_MSG_CHAN, &tmp, NULL, &tmp2, &l, &m, conn->conn.username, conn->conn.username_len); } break; default: @@ -1747,7 +1774,7 @@ handle_ctcp(conn, NULL, &tmp2, &l, &m); } else { format_msg(MSG(conn, FMT_MSG_PRIV_FROM_ME), NULL, - &tmp, &tmp2, &l, &m); + &tmp, &tmp2, &l, &m, conn->conn.username, conn->conn.username_len); } }
« previous php.cvs (#7153) next »