sas             Tue Apr 24 02:40:06 2001 EDT

  Modified files:              
    /php4/ext/ircg      ircg.c ircg_scanner.c php_ircg.h 
  Log:
  Add support for suppressing the "loopback" of messages sent using ircg_msg.
  
  Add support for nickname escaping
  
  Using this technique it becomes possible to use nicknames like
  "John Doe" in the front-end.  Scripts can use ircg_nickname_(un)escape.
  This should be applyable to channel names as well.
  
  A new flag character (2) is introduced which unescapes the respective
  argument.
  
  I.e. a format string could look like
  
  "%2f joins %c<script>parent.new_user('%12f','%1c')</script>"
  
  In the case of "G|27n|27R", this would be interpolated as:
  
  "G'n'R joins foobar<script>parent.new_user('G\'n\'R','foobar')</script>"
  
  I.e. %12f means "unescape the parameter and apply javascript escaping".
  
  
Index: php4/ext/ircg/ircg.c
diff -u php4/ext/ircg/ircg.c:1.60 php4/ext/ircg/ircg.c:1.61
--- php4/ext/ircg/ircg.c:1.60   Wed Apr 18 04:42:30 2001
+++ php4/ext/ircg/ircg.c        Tue Apr 24 02:40:05 2001
@@ -16,7 +16,7 @@
    +----------------------------------------------------------------------+
  */
 
-/* $Id: ircg.c,v 1.60 2001/04/18 11:42:30 sas Exp $ */
+/* $Id: ircg.c,v 1.61 2001/04/24 09:40:05 sas Exp $ */
 
 #include "php.h"
 #include "php_ini.h"
@@ -82,6 +82,8 @@
        PHP_FE(ircg_html_encode, NULL)
        PHP_FE(ircg_whois, NULL)
        PHP_FE(ircg_kick, NULL)
+       PHP_FE(ircg_nickname_escape, NULL)
+       PHP_FE(ircg_nickname_unescape, NULL)
        PHP_FE(ircg_ignore_add, NULL)
        PHP_FE(ircg_ignore_del, NULL)
        PHP_FE(ircg_disconnect, NULL)
@@ -218,6 +220,55 @@
        }
 }
 
+static const char hextab[] = "0123456789abcdef";
+
+#define NICKNAME_ESC_CHAR '|'
+
+static void ircg_nickname_escape(smart_str *input, smart_str *output)
+{
+       char *p;
+       char *end;
+       char c;
+
+       end = input->c + input->len;
+
+       for(p = input->c; p < end; p++) {
+               c = *p;
+               if ((c >= 'a' && c <= 'z')
+                               || (c >= 'A' && c <= 'Z')
+                               || (c >= '0' && c <= '9'))
+                       smart_str_appendc_ex(output, c, 1);
+               else {
+                       smart_str_appendc_ex(output, NICKNAME_ESC_CHAR, 1);
+                       smart_str_appendc_ex(output, hextab[c >> 4], 1);
+                       smart_str_appendc_ex(output, hextab[c & 15], 1);
+               }
+       }
+}
+
+#define HEX_VALUE(c) ((c>='a'&&c<='f')?c-'a':(c>='0'&&c<='9')?c-'0':0)
+
+static void ircg_nickname_unescape(smart_str *input, smart_str *output)
+{
+       char *p;
+       char *end;
+
+       end = input->c + input->len;
+
+       for(p = input->c; p < end; p++) {
+               switch (p[0]) {
+               case NICKNAME_ESC_CHAR:
+                       if (p + 2 >= end) break;
+                       smart_str_appendc_ex(output, (HEX_VALUE(p[1]) << 4) + 
+HEX_VALUE(p[2]), 1);
+                       p += 2;
+                       break;
+               default:
+                       smart_str_appendc_ex(output, p[0], 1);
+               }
+       }
+}
+
+
 void ircg_mirc_color(const char *, smart_str *, size_t);
 
 static void format_msg(const char *fmt, smart_str *channel, smart_str *to, smart_str 
*from, smart_str *msg, smart_str *result)
@@ -231,6 +282,7 @@
        int js_encoded = 0;
        unsigned long len;
        int mod_encode;
+       int nickname_decode;
        
        if (fmt[0] == '\0') {
                return;
@@ -243,19 +295,23 @@
                        smart_str_appendl_ex(result, p, len, 1);
                
                
-               mod_encode = 0;
+               nickname_decode = mod_encode = 0;
 
 
 #define IRCG_APPEND(what) \
-               if (mod_encode) { \
+               if (mod_encode && nickname_decode) { \
                        smart_str tmp = {0}; \
-                       ircg_js_escape(what, &tmp); \
-                       smart_str_append_ex(result, &tmp, 1); \
+                       ircg_nickname_unescape(what, &tmp); \
+                       ircg_js_escape(&tmp, result); \
                        smart_str_free_ex(&tmp, 1); \
+               } else if (mod_encode) { \
+                       ircg_js_escape(what, result); \
+               } else if (nickname_decode) { \
+                       ircg_nickname_unescape(what, result); \
                } else { \
                        smart_str_append_ex(result, what, 1); \
                }
-               
+
 again:
                c = q[1];
                
@@ -264,6 +320,10 @@
                        mod_encode = 1;
                        q++;
                        goto again;
+               case '2':
+                       nickname_decode = 1;
+                       q++;
+                       goto again;
                case 'c':
                        IRCG_APPEND(channel);
                        break;
@@ -683,6 +743,48 @@
 #endif
 }
 
+PHP_FUNCTION(ircg_nickname_escape)
+{
+       zval **p1;
+       smart_str in;
+       smart_str out = {0};
+
+       if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &p1) == FAILURE)
+               WRONG_PARAM_COUNT;
+
+       convert_to_string_ex(p1);
+
+       smart_str_setl(&in, Z_STRVAL_PP(p1), Z_STRLEN_PP(p1));
+
+       ircg_nickname_escape(&in, &out);
+       smart_str_0(&out);
+       
+       RETVAL_STRINGL(out.c, out.len, 1);
+
+       smart_str_free_ex(&out, 1);
+}
+
+PHP_FUNCTION(ircg_nickname_unescape)
+{
+       zval **p1;
+       smart_str in;
+       smart_str out = {0};
+
+       if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &p1) == FAILURE)
+               WRONG_PARAM_COUNT;
+
+       convert_to_string_ex(p1);
+
+       smart_str_setl(&in, Z_STRVAL_PP(p1), Z_STRLEN_PP(p1));
+
+       ircg_nickname_unescape(&in, &out);
+       smart_str_0(&out);
+       
+       RETVAL_STRINGL(out.c, out.len, 1);
+
+       smart_str_free_ex(&out, 1);
+}
+
 PHP_FUNCTION(ircg_new_window)
 {
        zval **p1;
@@ -1168,48 +1270,59 @@
 
 PHP_FUNCTION(ircg_msg)
 {
-       zval **id, **recipient, **msg;
+       zval **id, **recipient, **msg, **suppress;
        php_irconn_t *conn;
        smart_str l = {0};
        smart_str m = {0};
        smart_str tmp, tmp2;
+       int o_suppress = 0;
        
-       if (ZEND_NUM_ARGS() != 3 || zend_get_parameters_ex(3, &id, &recipient, &msg) 
== FAILURE)
+       if (ZEND_NUM_ARGS() < 3 || ZEND_NUM_ARGS() > 4 || 
+zend_get_parameters_ex(ZEND_NUM_ARGS(), &id, &recipient, &msg, &suppress) == FAILURE)
                WRONG_PARAM_COUNT;
 
-       convert_to_long_ex(id);
-       convert_to_string_ex(recipient);
-       convert_to_string_ex(msg);
+       switch (ZEND_NUM_ARGS()) {
+       case 4:
+               convert_to_long_ex(suppress);
+               o_suppress = Z_LVAL_PP(suppress);
+       case 3:
+               convert_to_long_ex(id);
+               convert_to_string_ex(recipient);
+               convert_to_string_ex(msg);
+       }
 
        conn = lookup_irconn(Z_LVAL_PP(id));
 
        if (!conn) RETURN_FALSE;
 
        irc_msg(&conn->conn, Z_STRVAL_PP(recipient), Z_STRVAL_PP(msg));
-       smart_str_setl(&l, Z_STRVAL_PP(msg), Z_STRLEN_PP(msg));
+       
+       if (!o_suppress) {
+               smart_str_setl(&l, Z_STRVAL_PP(msg), Z_STRLEN_PP(msg));
 
-       smart_str_setl(&tmp, Z_STRVAL_PP(recipient), Z_STRLEN_PP(recipient));
-       smart_str_setl(&tmp2, conn->conn.username, conn->conn.username_len);
+               smart_str_setl(&tmp, Z_STRVAL_PP(recipient), Z_STRLEN_PP(recipient));
+               smart_str_setl(&tmp2, conn->conn.username, conn->conn.username_len);
 
-       switch (Z_STRVAL_PP(recipient)[0]) {
-       case '#':
-       case '&':
-               if (l.c[0] == 1) {
-                       handle_ctcp(conn, &tmp, &tmp2, &l, &m);
-               } else {
-                       format_msg(MSG(conn, FMT_MSG_CHAN), &tmp, NULL, &tmp2, &l, &m);
-               }
-               break;
-       default:
-               if (l.c[0] == 1) {
-                       handle_ctcp(conn, NULL, &tmp2, &l, &m);
-               } else {
-                       format_msg(MSG(conn, FMT_MSG_PRIV_FROM_ME), NULL,
-                                       &tmp, &tmp2, &l, &m);
+
+               switch (Z_STRVAL_PP(recipient)[0]) {
+                       case '#':
+                       case '&':
+                               if (l.c[0] == 1) {
+                                       handle_ctcp(conn, &tmp, &tmp2, &l, &m);
+                               } else {
+                                       format_msg(MSG(conn, FMT_MSG_CHAN), &tmp, 
+NULL, &tmp2, &l, &m);
+                               }
+                               break;
+                       default:
+                               if (l.c[0] == 1) {
+                                       handle_ctcp(conn, NULL, &tmp2, &l, &m);
+                               } else {
+                                       format_msg(MSG(conn, FMT_MSG_PRIV_FROM_ME), 
+NULL,
+                                                       &tmp, &tmp2, &l, &m);
+                               }
                }
-       }
 
-       msg_send(conn, &m);
+               msg_send(conn, &m);
+       }
 
        RETURN_TRUE;
 }
Index: php4/ext/ircg/ircg_scanner.c
diff -u php4/ext/ircg/ircg_scanner.c:1.8 php4/ext/ircg/ircg_scanner.c:1.9
--- php4/ext/ircg/ircg_scanner.c:1.8    Sat Mar 10 15:21:49 2001
+++ php4/ext/ircg/ircg_scanner.c        Tue Apr 24 02:40:05 2001
@@ -1,4 +1,4 @@
-/* Generated by re2c 0.5 on Sun Mar 11 00:24:46 2001 */
+/* Generated by re2c 0.5 on Fri Apr 20 10:19:53 2001 */
 #line 1 "/home/sas/src/php4/ext/ircg/ircg_scanner.re"
 /*
    +----------------------------------------------------------------------+
@@ -18,7 +18,7 @@
    +----------------------------------------------------------------------+
  */
 
-/* $Id: ircg_scanner.c,v 1.8 2001/03/10 23:21:49 sas Exp $ */
+/* $Id: ircg_scanner.c,v 1.9 2001/04/24 09:40:05 sas Exp $ */
 
 #include <ext/standard/php_smart_str.h>
 #include <stdio.h>
Index: php4/ext/ircg/php_ircg.h
diff -u php4/ext/ircg/php_ircg.h:1.11 php4/ext/ircg/php_ircg.h:1.12
--- php4/ext/ircg/php_ircg.h:1.11       Wed Apr 18 04:42:30 2001
+++ php4/ext/ircg/php_ircg.h    Tue Apr 24 02:40:06 2001
@@ -48,6 +48,8 @@
 PHP_FUNCTION(ircg_is_conn_alive);
 PHP_FUNCTION(ircg_lookup_format_messages);
 PHP_FUNCTION(ircg_register_format_messages);
+PHP_FUNCTION(ircg_nickname_escape);
+PHP_FUNCTION(ircg_nickname_unescape);
 
 PHP_MINIT_FUNCTION(ircg);
 PHP_MSHUTDOWN_FUNCTION(ircg);



-- 
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to