cvs: php4 /ext/ircg ircg.c php_ircg.h
| From: | Sascha Schumann | Date: | Tue, 27 Nov 2001 14:20:00 +0000 |
| Subject: | cvs: php4 /ext/ircg ircg.c php_ircg.h | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-8323@lists.php.net to get a copy of this message | ||
sas Tue Nov 27 09:20:00 2001 EDT
Modified files:
/php4/ext/ircg ircg.c php_ircg.h
Log:
Add support for redirecting output to a file while maintaining an
open HTTP connection.
The output can be used to render a complete textual representation
of a user's chat which can be stored in a simple .html file and
served to the end-user. Because it is static, thttpd is an
excellent choice for this task.
We continue to use an open HTTP connection to figure out when
a client departs.
Index: php4/ext/ircg/ircg.c
diff -u php4/ext/ircg/ircg.c:1.116 php4/ext/ircg/ircg.c:1.117
--- php4/ext/ircg/ircg.c:1.116 Sun Nov 25 14:38:42 2001
+++ php4/ext/ircg/ircg.c Tue Nov 27 09:19:59 2001
@@ -16,7 +16,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: ircg.c,v 1.116 2001/11/25 19:38:42 eschmid Exp $ */
+/* $Id: ircg.c,v 1.117 2001/11/27 14:19:59 sas Exp $ */
/* {{{ includes */
@@ -91,6 +91,7 @@
PHP_FE(ircg_set_on_die, NULL)
PHP_FE(ircg_pconnect, NULL)
PHP_FE(ircg_set_current, NULL)
+ PHP_FE(ircg_set_file, NULL)
PHP_FE(ircg_join, NULL)
PHP_FE(ircg_part, NULL)
PHP_FE(ircg_msg, NULL)
@@ -159,6 +160,8 @@
short od_port;
#endif
+ int file_fd;
+
char bailout_on_trivial; /* Whether to handle trivial errors as fatal */
char *ident; /* NOT available outside of ircg_pconnect or register_hooks */
@@ -303,9 +306,15 @@
irc_quit_handlers++;
if (conn->fd > -1) {
zend_hash_index_del(&h_fd2irconn, conn->fd);
- irc_write_buf_del(&conn->wb);
+ if (conn->file_fd == -1)
+ irc_write_buf_del(&conn->wb);
shutdown(conn->fd, 2);
}
+ if (conn->file_fd != -1) {
+ close(conn->file_fd);
+ conn->file_fd = -1;
+ }
+
conn->fd = -2;
zend_hash_index_del(&h_irconn, conn->irconn_id);
@@ -651,6 +660,11 @@
{
if (msg->c == 0) return;
+ if (conn->file_fd != -1) {
+ write(conn->file_fd, msg->c, msg->len);
+ return;
+ }
+
switch (conn->fd) {
case -2: /* Connection was finished */
smart_str_free_ex(msg, 1);
@@ -691,7 +705,7 @@
static void msg_send(php_irconn_t *conn, smart_str *msg)
{
msg_accum_send(conn, msg);
- if (conn->fd != -1)
+ if (conn->fd != -1 && conn->file_fd == -1)
irc_write_buf_flush(&conn->wb);
}
@@ -1048,10 +1062,9 @@
static void idle_recv_queue(irconn_t *ircc, void *dummy)
{
php_irconn_t *conn = dummy;
- smart_str m = {0};
- smart_str_appendl_ex(&m, "<!---->", 7, 1);
- msg_send(conn, &m);
+ if (conn->fd >= 0)
+ send(conn->fd, " ", 2, 0);
}
static void user_kick(irconn_t *ircc, smart_str *channel, smart_str *who, smart_str *kicked_by,
smart_str *reason, void *dummy)
@@ -1143,6 +1156,42 @@
}
/* }}} */
+/* {{{ proto bool ircg_set_file */
+PHP_FUNCTION(ircg_set_file)
+{
+ zval **p1, **p2;
+ php_irconn_t *conn;
+ int fd;
+
+ if (ZEND_NUM_ARGS() != 2 || zend_get_parameters_ex(2, &p1, &p2) == FAILURE)
+ WRONG_PARAM_COUNT;
+
+ convert_to_long_ex(p1);
+ convert_to_string_ex(p2);
+
+ conn = lookup_irconn(Z_LVAL_PP(p1));
+
+ if (!conn) RETURN_FALSE;
+
+ if (conn->fd != -1) {
+ php_error(E_WARNING, "ircg_set_file was used AFTER ircg_set_current. You must set the output
filename before opening the persistent HTTP connection which is kept alive.");
+ RETURN_FALSE;
+ }
+
+ if (conn->file_fd != -1)
+ close(conn->file_fd);
+
+ conn->file_fd = open(Z_STRVAL_PP(p2), O_WRONLY | O_CREAT | O_TRUNC, 0644);
+ if (conn->file_fd == -1) {
+ RETURN_FALSE;
+ }
+
+ msg_replay_buffer(conn);
+
+ RETURN_TRUE;
+}
+/* }}} */
+
/* {{{ proto bool ircg_set_current */
PHP_FUNCTION(ircg_set_current)
{
@@ -1160,11 +1209,13 @@
if (!conn) RETURN_FALSE;
if (conn->fd >= 0) {
- /* Migrate IRC session to another HTTP connection */
+ /* There is a HTTP connection alive, kill it */
zend_hash_index_del(&h_fd2irconn, conn->fd);
- irc_write_buf_del(&conn->wb);
+ if (conn->file_fd == -1)
+ irc_write_buf_del(&conn->wb);
shutdown(conn->fd, 2);
}
+
irc_set_currents++;
thttpd_register_on_close(http_closed_connection);
thttpd_set_dont_close();
@@ -1174,10 +1225,12 @@
RETURN_FALSE;
}
zend_hash_index_update(&h_fd2irconn, conn->fd, &Z_LVAL_PP(p1), sizeof(int), NULL);
- irc_write_buf_add(&conn->wb, conn->fd);
- msg_http_start(conn TSRMLS_CC);
- msg_replay_buffer(conn);
- irc_write_buf_flush(&conn->wb);
+ if (conn->file_fd == -1) {
+ irc_write_buf_add(&conn->wb, conn->fd);
+ msg_http_start(conn TSRMLS_CC);
+ msg_replay_buffer(conn);
+ irc_write_buf_flush(&conn->wb);
+ }
RETURN_TRUE;
#endif
@@ -1702,7 +1755,7 @@
* so we have to allocate it ourselves.
*/
conn = malloc(sizeof(*conn));
- conn->fd = -1;
+ conn->fd = conn->file_fd = -1;
conn->ident = conn->password = conn->realname = NULL;
if (ZEND_NUM_ARGS() > 5 && Z_TYPE_PP(p6) == IS_ARRAY) {
zval **val;
Index: php4/ext/ircg/php_ircg.h
diff -u php4/ext/ircg/php_ircg.h:1.16 php4/ext/ircg/php_ircg.h:1.17
--- php4/ext/ircg/php_ircg.h:1.16 Mon Nov 5 09:38:49 2001
+++ php4/ext/ircg/php_ircg.h Tue Nov 27 09:19:59 2001
@@ -33,6 +33,7 @@
PHP_FUNCTION(ircg_pconnect);
PHP_FUNCTION(ircg_join);
PHP_FUNCTION(ircg_set_current);
+PHP_FUNCTION(ircg_set_file);
PHP_FUNCTION(ircg_part);
PHP_FUNCTION(ircg_register_current_conn);
PHP_FUNCTION(ircg_whois);