Index: protocols/twitter/twitter.c
===================================================================
--- protocols/twitter/twitter.c	(revision f9110b483d89b6dbc15d980f8b431863e8a872f3)
+++ protocols/twitter/twitter.c	(revision bb5ce4d1ad2aa6cbcf1042ccf87cf280d9645d4c)
@@ -27,4 +27,5 @@
 #include "twitter_http.h"
 #include "twitter_lib.h"
+#include "url.h"
 
 /**
@@ -160,4 +161,7 @@
 	set_t *s;
 	
+	s = set_add( &acc->set, "base_url", TWITTER_API_URL, NULL, acc );
+	s->flags |= ACC_SET_OFFLINE_ONLY;
+	
 	s = set_add( &acc->set, "message_length", "140", set_eval_int, acc );
 	
@@ -175,16 +179,31 @@
 {
 	struct im_connection *ic = imcb_new( acc );
-	struct twitter_data *td = g_new0( struct twitter_data, 1 );
+	struct twitter_data *td;
 	char name[strlen(acc->user)+9];
-
+	url_t url;
+
+	if( !url_set( &url, set_getstr( &ic->acc->set, "base_url" ) ) ||
+	    ( url.proto != PROTO_HTTP && url.proto != PROTO_HTTPS ) )
+	{
+		imcb_error( ic, "Incorrect API base URL: %s", set_getstr( &ic->acc->set, "base_url" ) );
+		imc_logout( ic, FALSE );
+		return;
+	}
+	
 	twitter_connections = g_slist_append( twitter_connections, ic );
+	td = g_new0( struct twitter_data, 1 );
 	ic->proto_data = td;
 	
+	td->url_ssl = url.proto == PROTO_HTTPS;
+	td->url_port = url.port;
+	td->url_host = g_strdup( url.host );
+	if( strcmp( url.file, "/" ) != 0 )
+		td->url_path = g_strdup( url.file );
+	else
+		td->url_path = g_strdup( "" );
+	
 	td->user = acc->user;
-	if( !set_getbool( &acc->set, "oauth" ) )
-		td->pass = g_strdup( acc->pass );
-	else if( strstr( acc->pass, "oauth_token=" ) )
+	if( strstr( acc->pass, "oauth_token=" ) )
 		td->oauth_info = oauth_from_string( acc->pass, &twitter_oauth );
-	td->home_timeline_id = 0;
 	
 	sprintf( name, "twitter_%s", acc->user );
@@ -192,5 +211,5 @@
 	imcb_buddy_status( ic, name, OPT_LOGGED_IN, NULL, NULL );
 	
-	if( td->pass || td->oauth_info )
+	if( td->oauth_info || !set_getbool( &acc->set, "oauth" ) )
 		twitter_main_loop_start( ic );
 	else
Index: protocols/twitter/twitter.h
===================================================================
--- protocols/twitter/twitter.h	(revision f9110b483d89b6dbc15d980f8b431863e8a872f3)
+++ protocols/twitter/twitter.h	(revision bb5ce4d1ad2aa6cbcf1042ccf87cf280d9645d4c)
@@ -42,4 +42,9 @@
 	struct groupchat *home_timeline_gc;
 	gint http_fails;
+	
+	gboolean url_ssl;
+	int url_port;
+	char *url_host;
+	char *url_path;
 };
 
Index: protocols/twitter/twitter_http.c
===================================================================
--- protocols/twitter/twitter_http.c	(revision f9110b483d89b6dbc15d980f8b431863e8a872f3)
+++ protocols/twitter/twitter_http.c	(revision bb5ce4d1ad2aa6cbcf1042ccf87cf280d9645d4c)
@@ -41,5 +41,5 @@
 
 
-char *twitter_url_append(char *url, char *key, char* value);
+static char *twitter_url_append(char *url, char *key, char* value);
 
 /**
@@ -47,37 +47,13 @@
  * This is actually pretty generic function... Perhaps it should move to the lib/http_client.c
  */
-void *twitter_http(char *url_string, http_input_function func, gpointer data, int is_post, char* user, char* pass, struct oauth_info* oi, char** arguments, int arguments_len)
+void *twitter_http(struct im_connection *ic, char *url_string, http_input_function func, gpointer data, int is_post, char** arguments, int arguments_len)
 {
-	url_t *url = g_new0( url_t, 1 );
+	struct twitter_data *td = ic->proto_data;
 	char *tmp;
-	char *request;
+	GString *request = g_string_new("");
 	void *ret;
-	char *userpass = NULL;
-	char *userpass_base64;
 	char *url_arguments;
 
-	// Fill the url structure.
-	if( !url_set( url, url_string ) )
-	{
-		g_free( url );
-		return NULL;
-	}
-
-	if( url->proto != PROTO_HTTP && url->proto != PROTO_HTTPS )
-	{
-		g_free( url );
-		return NULL;
-	}
-
-	// Concatenate user and pass
-	if (user && pass) {
-		userpass = g_strdup_printf("%s:%s", user, pass);
-		userpass_base64 = base64_encode((unsigned char*)userpass, strlen(userpass));
-	} else {
-		userpass_base64 = NULL;
-	}
-
-	url_arguments = g_malloc(1);
-	url_arguments[0] = '\0';
+	url_arguments = g_strdup("");
 
 	// Construct the url arguments.
@@ -93,40 +69,36 @@
 	}
 
-	// Do GET stuff...
-	if (!is_post)
-	{
-		// Find the char-pointer of the end of the string.
-		tmp = url->file + strlen(url->file);
-		tmp[0] = '?';
-		// append the url_arguments to the end of the url->file.
-		// TODO GM: Check the length?
-		g_stpcpy (tmp+1, url_arguments);
-	}
-
-
 	// Make the request.
-	request = g_strdup_printf(  "%s %s HTTP/1.0\r\n"
-	                            "Host: %s\r\n"
-	                            "User-Agent: BitlBee " BITLBEE_VERSION " " ARCH "/" CPU "\r\n",
-	                            is_post ? "POST" : "GET", url->file, url->host );
+	g_string_printf(request, "%s %s%s%s%s HTTP/1.0\r\n"
+	                         "Host: %s\r\n"
+	                         "User-Agent: BitlBee " BITLBEE_VERSION " " ARCH "/" CPU "\r\n",
+	                         is_post ? "POST" : "GET",
+	                         td->url_path, url_string,
+	                         is_post ? "" : "?", is_post ? "" : url_arguments,
+	                         td->url_host);
 
 	// If a pass and user are given we append them to the request.
-	if (oi)
+	if (td->oauth_info)
 	{
 		char *full_header;
+		char *full_url;
 		
-		full_header = oauth_http_header(oi, is_post ? "POST" : "GET",
-		                                url_string, url_arguments);
+		full_url = g_strconcat(set_getstr(&ic->acc->set, "base_url" ), url_string, NULL);
+		full_header = oauth_http_header(td->oauth_info, is_post ? "POST" : "GET",
+		                                full_url, url_arguments);
 		
-		tmp = g_strdup_printf("%sAuthorization: %s\r\n", request, full_header);
-		g_free(request);
+		g_string_append_printf(request, "Authorization: %s\r\n", full_header);
 		g_free(full_header);
-		request = tmp;
+		g_free(full_url);
 	}
-	else if (userpass_base64)
+	else
 	{
-		tmp = g_strdup_printf("%sAuthorization: Basic %s\r\n", request, userpass_base64);
-		g_free(request);
-		request = tmp;
+		char userpass[strlen(ic->acc->user)+2+strlen(ic->acc->pass)];
+		char *userpass_base64;
+		
+		g_snprintf(userpass, sizeof(userpass), "%s:%s", ic->acc->user, ic->acc->pass);
+		userpass_base64 = base64_encode((unsigned char*)userpass, strlen(userpass));
+		g_string_append_printf(request, "Authorization: Basic %s\r\n", userpass_base64);
+		g_free( userpass_base64 );
 	}
 
@@ -135,26 +107,21 @@
 	{
 		// Append the Content-Type and url-encoded arguments.
-		tmp = g_strdup_printf("%sContent-Type: application/x-www-form-urlencoded\r\nContent-Length: %zd\r\n\r\n%s", 
-								request, strlen(url_arguments), url_arguments);
-		g_free(request);
-		request = tmp;
+		g_string_append_printf(request,
+		                       "Content-Type: application/x-www-form-urlencoded\r\n"
+		                       "Content-Length: %zd\r\n\r\n%s",
+		                       strlen(url_arguments), url_arguments);
 	} else {
 		// Append an extra \r\n to end the request...
-		tmp = g_strdup_printf("%s\r\n", request);
-		g_free(request);
-		request = tmp;
+		g_string_append(request, "\r\n");
 	}
 
-	ret = http_dorequest( url->host, url->port,	url->proto == PROTO_HTTPS, request, func, data );
+	ret = http_dorequest(td->url_host, td->url_port, td->url_ssl, request->str, func, data);
 
-	g_free( url );
-	g_free( userpass );
-	g_free( userpass_base64 );
 	g_free( url_arguments );
-	g_free( request );
+	g_string_free( request, TRUE );
 	return ret;
 }
 
-char *twitter_url_append(char *url, char *key, char* value)
+static char *twitter_url_append(char *url, char *key, char* value)
 {
 	char *key_encoded = g_strndup(key, 3 * strlen(key));
Index: protocols/twitter/twitter_http.h
===================================================================
--- protocols/twitter/twitter_http.h	(revision f9110b483d89b6dbc15d980f8b431863e8a872f3)
+++ protocols/twitter/twitter_http.h	(revision bb5ce4d1ad2aa6cbcf1042ccf87cf280d9645d4c)
@@ -30,6 +30,6 @@
 struct oauth_info;
 
-void *twitter_http(char *url_string, http_input_function func, gpointer data, int is_post, 
-                   char* user, char* pass, struct oauth_info *oi, char** arguments, int arguments_len);
+void *twitter_http(struct im_connection *ic, char *url_string, http_input_function func,
+                   gpointer data, int is_post, char** arguments, int arguments_len);
 
 #endif //_TWITTER_HTTP_H
Index: protocols/twitter/twitter_lib.c
===================================================================
--- protocols/twitter/twitter_lib.c	(revision f9110b483d89b6dbc15d980f8b431863e8a872f3)
+++ protocols/twitter/twitter_lib.c	(revision bb5ce4d1ad2aa6cbcf1042ccf87cf280d9645d4c)
@@ -130,5 +130,5 @@
 	args[0] = "cursor";
 	args[1] = g_strdup_printf ("%d", next_cursor);
-	twitter_http(TWITTER_FRIENDS_IDS_URL, twitter_http_get_friends_ids, ic, 0, td->user, td->pass, td->oauth_info, args, 2);
+	twitter_http(ic, TWITTER_FRIENDS_IDS_URL, twitter_http_get_friends_ids, ic, 0, args, 2);
 
 	g_free(args[1]);
@@ -396,5 +396,5 @@
 	}
 
-	twitter_http(TWITTER_HOME_TIMELINE_URL, twitter_http_get_home_timeline, ic, 0, td->user, td->pass, td->oauth_info, args, td->home_timeline_id ? 4 : 2);
+	twitter_http(ic, TWITTER_HOME_TIMELINE_URL, twitter_http_get_home_timeline, ic, 0, args, td->home_timeline_id ? 4 : 2);
 
 	g_free(args[1]);
@@ -623,5 +623,5 @@
 	args[1] = g_strdup_printf ("%d", next_cursor);
 
-	twitter_http(TWITTER_SHOW_FRIENDS_URL, twitter_http_get_statuses_friends, ic, 0, td->user, td->pass, td->oauth_info, args, 2);
+	twitter_http(ic, TWITTER_SHOW_FRIENDS_URL, twitter_http_get_statuses_friends, ic, 0, args, 2);
 
 	g_free(args[1]);
@@ -657,5 +657,5 @@
 	args[0] = "status";
 	args[1] = msg;
-	twitter_http(TWITTER_STATUS_UPDATE_URL, twitter_http_post_status, ic, 1, td->user, td->pass, td->oauth_info, args, 2);
+	twitter_http(ic, TWITTER_STATUS_UPDATE_URL, twitter_http_post_status, ic, 1, args, 2);
 //	g_free(args[1]);
 }
@@ -675,5 +675,5 @@
 	args[3] = msg;
 	// Use the same callback as for twitter_post_status, since it does basically the same.
-	twitter_http(TWITTER_DIRECT_MESSAGES_NEW_URL, twitter_http_post_status, ic, 1, td->user, td->pass, td->oauth_info, args, 4);
+	twitter_http(ic, TWITTER_DIRECT_MESSAGES_NEW_URL, twitter_http_post_status, ic, 1, args, 4);
 //	g_free(args[1]);
 //	g_free(args[3]);
Index: protocols/twitter/twitter_lib.h
===================================================================
--- protocols/twitter/twitter_lib.h	(revision f9110b483d89b6dbc15d980f8b431863e8a872f3)
+++ protocols/twitter/twitter_lib.h	(revision bb5ce4d1ad2aa6cbcf1042ccf87cf280d9645d4c)
@@ -32,47 +32,47 @@
 
 /* Status URLs */
-#define TWITTER_STATUS_UPDATE_URL TWITTER_API_URL "/statuses/update.xml"
-#define TWITTER_STATUS_SHOW_URL TWITTER_API_URL "/statuses/show/"
-#define TWITTER_STATUS_DESTROY_URL TWITTER_API_URL "/statuses/destroy/"
+#define TWITTER_STATUS_UPDATE_URL "/statuses/update.xml"
+#define TWITTER_STATUS_SHOW_URL "/statuses/show/"
+#define TWITTER_STATUS_DESTROY_URL "/statuses/destroy/"
 
 /* Timeline URLs */
-#define TWITTER_PUBLIC_TIMELINE_URL TWITTER_API_URL "/statuses/public_timeline.xml"
-#define TWITTER_FEATURED_USERS_URL TWITTER_API_URL "/statuses/featured.xml"
-#define TWITTER_FRIENDS_TIMELINE_URL TWITTER_API_URL "/statuses/friends_timeline.xml"
-#define TWITTER_HOME_TIMELINE_URL TWITTER_API_URL "/statuses/home_timeline.xml"
-#define TWITTER_MENTIONS_URL TWITTER_API_URL "/statuses/mentions.xml"
-#define TWITTER_USER_TIMELINE_URL TWITTER_API_URL "/statuses/user_timeline.xml"
+#define TWITTER_PUBLIC_TIMELINE_URL "/statuses/public_timeline.xml"
+#define TWITTER_FEATURED_USERS_URL "/statuses/featured.xml"
+#define TWITTER_FRIENDS_TIMELINE_URL "/statuses/friends_timeline.xml"
+#define TWITTER_HOME_TIMELINE_URL "/statuses/home_timeline.xml"
+#define TWITTER_MENTIONS_URL "/statuses/mentions.xml"
+#define TWITTER_USER_TIMELINE_URL "/statuses/user_timeline.xml"
 
 /* Users URLs */
-#define TWITTER_SHOW_USERS_URL TWITTER_API_URL "/users/show.xml"
-#define TWITTER_SHOW_FRIENDS_URL TWITTER_API_URL "/statuses/friends.xml"
-#define TWITTER_SHOW_FOLLOWERS_URL TWITTER_API_URL "/statuses/followers.xml"
+#define TWITTER_SHOW_USERS_URL "/users/show.xml"
+#define TWITTER_SHOW_FRIENDS_URL "/statuses/friends.xml"
+#define TWITTER_SHOW_FOLLOWERS_URL "/statuses/followers.xml"
 
 /* Direct messages URLs */
-#define TWITTER_DIRECT_MESSAGES_URL TWITTER_API_URL "/direct_messages.xml"
-#define TWITTER_DIRECT_MESSAGES_NEW_URL TWITTER_API_URL "/direct_messages/new.xml"
-#define TWITTER_DIRECT_MESSAGES_SENT_URL TWITTER_API_URL "/direct_messages/sent.xml"
-#define TWITTER_DIRECT_MESSAGES_DESTROY_URL TWITTER_API_URL "/direct_messages/destroy/"
+#define TWITTER_DIRECT_MESSAGES_URL "/direct_messages.xml"
+#define TWITTER_DIRECT_MESSAGES_NEW_URL "/direct_messages/new.xml"
+#define TWITTER_DIRECT_MESSAGES_SENT_URL "/direct_messages/sent.xml"
+#define TWITTER_DIRECT_MESSAGES_DESTROY_URL "/direct_messages/destroy/"
 
 /* Friendships URLs */
-#define TWITTER_FRIENDSHIPS_CREATE_URL TWITTER_API_URL "/friendships/create.xml"
-#define TWITTER_FRIENDSHIPS_DESTROY_URL TWITTER_API_URL "/friendships/destroy.xml"
-#define TWITTER_FRIENDSHIPS_SHOW_URL TWITTER_API_URL "/friendships/show.xml"
+#define TWITTER_FRIENDSHIPS_CREATE_URL "/friendships/create.xml"
+#define TWITTER_FRIENDSHIPS_DESTROY_URL "/friendships/destroy.xml"
+#define TWITTER_FRIENDSHIPS_SHOW_URL "/friendships/show.xml"
 
 /* Social graphs URLs */
-#define TWITTER_FRIENDS_IDS_URL TWITTER_API_URL "/friends/ids.xml"
-#define TWITTER_FOLLOWERS_IDS_URL TWITTER_API_URL "/followers/ids.xml"
+#define TWITTER_FRIENDS_IDS_URL "/friends/ids.xml"
+#define TWITTER_FOLLOWERS_IDS_URL "/followers/ids.xml"
 
 /* Account URLs */
-#define TWITTER_ACCOUNT_RATE_LIMIT_URL TWITTER_API_URL "/account/rate_limit_status.xml"
+#define TWITTER_ACCOUNT_RATE_LIMIT_URL "/account/rate_limit_status.xml"
 
 /* Favorites URLs */
-#define TWITTER_FAVORITES_GET_URL TWITTER_API_URL "/favorites.xml"
-#define TWITTER_FAVORITE_CREATE_URL TWITTER_API_URL "/favorites/create/"
-#define TWITTER_FAVORITE_DESTROY_URL TWITTER_API_URL "/favorites/destroy/"
+#define TWITTER_FAVORITES_GET_URL "/favorites.xml"
+#define TWITTER_FAVORITE_CREATE_URL "/favorites/create/"
+#define TWITTER_FAVORITE_DESTROY_URL "/favorites/destroy/"
 
 /* Block URLs */
-#define TWITTER_BLOCKS_CREATE_URL TWITTER_API_URL "/blocks/create/"
-#define TWITTER_BLOCKS_DESTROY_URL TWITTER_API_URL "/blocks/destroy/"
+#define TWITTER_BLOCKS_CREATE_URL "/blocks/create/"
+#define TWITTER_BLOCKS_DESTROY_URL "/blocks/destroy/"
 
 void twitter_get_friends_ids(struct im_connection *ic, int next_cursor);
