Modify

#119 new enhancement

Add basic HTML translation to AIM

Reported by: sethk@… Owned by:
Priority: wishlist Milestone:
Component: BitlBee Version: 1.0.1
Keywords: Cc:
IRC client+version: Client-independent Operating System: Public server
OS version/distro:

Description

Patch to translate between HTML and IRC escape codes:

diff -u bitlbee-1.0.1.orig/bitlbee.h bitlbee-1.0.1/bitlbee.h
--- bitlbee-1.0.1.orig/bitlbee.h	Sat Jan 14 07:16:07 2006
+++ bitlbee-1.0.1/bitlbee.h	Tue Mar 14 02:45:31 2006
@@ -130,6 +130,9 @@
 int bitlbee_save( irc_t *irc );
 void bitlbee_shutdown( gpointer data );
 double gettime( void );
+G_MODULE_EXPORT void strip_html( char *in );
+G_MODULE_EXPORT void decode_html( char *in );
+G_MODULE_EXPORT char *encode_html( char *in );
 G_MODULE_EXPORT void http_encode( char *s );
 G_MODULE_EXPORT void http_decode( char *s );
 G_MODULE_EXPORT char *strip_newlines(char *source);
diff -u bitlbee-1.0.1.orig/irc.h bitlbee-1.0.1/irc.h
--- bitlbee-1.0.1.orig/irc.h	Sat Jan 14 07:13:11 2006
+++ bitlbee-1.0.1/irc.h	Tue Mar 14 02:45:08 2006
@@ -32,6 +32,13 @@
 #define IRC_LOGIN_TIMEOUT 60
 #define IRC_PING_STRING "PinglBee"
 
+#define IRC_BOLD '\2'
+#define IRC_BOLD_MASK (1 << 0)
+#define IRC_ITALIC '\26'
+#define IRC_ITALIC_MASK (1 << 1)
+#define IRC_UNDER '\37'
+#define IRC_UNDER_MASK (1 << 2)
+
 #define UMODES "ais"
 #define CMODES "nt"
 #define CMODE "t"
diff -u bitlbee-1.0.1.orig/util.c bitlbee-1.0.1/util.c
--- bitlbee-1.0.1.orig/util.c	Sat Jan 14 07:13:11 2006
+++ bitlbee-1.0.1/util.c	Tue Mar 14 03:29:48 2006
@@ -221,7 +221,6 @@
 	char *start = in;
 	char *out = g_malloc( strlen( in ) + 1 );
 	char *s = out, *cs;
-	int i, matched;
 	
 	memset( out, 0, strlen( in ) + 1 );
 	
@@ -250,34 +249,6 @@
 				*(s++) = *(in++);
 			}
 		}
-		else if( *in == '&' )
-		{
-			cs = ++in;
-			while( *in && isalpha( *in ) )
-				in ++;
-			
-			if( *in == ';' ) in ++;
-			matched = 0;
-			
-			for( i = 0; *ent[i].code; i ++ )
-				if( g_strncasecmp( ent[i].code, cs, strlen( ent[i].code ) ) == 0 )
-				{
-					int j;
-					
-					for( j = 0; ent[i].is[j]; j ++ )
-						*(s++) = ent[i].is[j];
-					
-					matched = 1;
-					break;
-				}
-
-			/* None of the entities were matched, so return the string */
-			if( !matched )
-			{
-				in = cs - 1;
-				*(s++) = *(in++);
-			}
-		}
 		else
 		{
 			*(s++) = *(in++);
@@ -288,10 +259,75 @@
 	g_free( out );
 }
 
-char *escape_html( const char *html )
+void decode_html( char *in )
+{
+	char *html = in;
+	char *out = g_malloc( strlen( html ) );
+	char *irc = out;
+	char *next;
+	int i;
+
+	while( *html )
+	{
+		if( *html == '<' && ( next = strchr( html + 1, '>' ) ) )
+		{
+			++html;
+			*strpbrk ( html + 1, " />" ) = '\0';
+
+			if( *html == '/' )
+				++html;
+
+			if( !g_strcasecmp( html, "b" ) )
+				*irc++ = IRC_BOLD;
+			else if( !g_strcasecmp( html, "i" ) )
+				*irc++ = IRC_ITALIC;
+			else if( !g_strcasecmp( html, "u" ) )
+				*irc++ = IRC_UNDER;
+
+			html = next + 1;
+			continue;
+		}
+		else if( *html == '&' )
+		{
+			next = html + 1;
+			while ( isalpha( *next ) )
+				++next;
+			if ( *next == ';' )
+			{
+				++html;
+				*next = '\0';
+				for ( i = 0; *ent[i].code; ++i )
+					if ( !g_strcasecmp( ent[i].code, html) )
+					{
+						int j;
+
+						for ( j = 0; ent[i].is[j]; ++j )
+							*irc++ = ent[i].is[j];
+
+						break;
+					}
+
+				if ( !*ent[i].code )
+					*irc++ = '?';
+
+				html = next + 1;
+				continue;
+			}
+		}
+
+		*irc++ = *html++;
+	}
+	*irc = '\0';
+
+	strcpy( in, out );
+	g_free( out );
+}
+
+char *encode_html( const char *html )
 {
 	const char *c = html;
 	GString *ret;
+	unsigned flags = 0;
 	char *str;
 	
 	if( html == NULL )
@@ -303,6 +339,30 @@
 	{
 		switch( *c )
 		{
+			case IRC_BOLD:
+				g_string_append_c( out, '<' );
+				if( flags & IRC_BOLD_MASK )
+					g_string_append_c( out, '/' );
+				g_string_append( out, "b>" );
+				flags^= IRC_BOLD_MASK;
+				break;
+
+			case IRC_ITALIC:
+				g_string_append_c( out, '<' );
+				if( flags & IRC_ITALIC_MASK )
+					g_string_append_c( out, '/' );
+				g_string_append( out, "i>" );
+				flags^= IRC_ITALIC_MASK;
+				break;
+
+			case IRC_UNDER:
+				g_string_append_c( out, '<' );
+				if( flags & IRC_UNDER_MASK )
+					g_string_append_c( out, '/' );
+				g_string_append( out, "u>" );
+				flags^= IRC_UNDER_MASK;
+				break;
+
 			case '&':
 				ret = g_string_append( ret, "&amp;" );
 				break;

Attachments (0)

Change History (2)

comment:1 Changed at 2006-03-19T13:22:54Z by wilmer

Looks nice. Now I'm starting to regret ever renaming the html setting to strip_html... :-/ There's an irssi script for parsing HTML too, but doing it like this works with every IRC client, which is always nicer.

comment:2 Changed at 2006-04-01T13:05:33Z by wilmer

Priority: normalwishlist

Sethk, can you please try to update this patch to the current development version? I changed the format of the HTML entity table.

Modify Ticket

Action
as new The ticket will remain with no owner.

Add Comment


E-mail address and name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.