示例#1
0
int
irc_buffer_close_cb (void *data, struct t_gui_buffer *buffer)
{
    struct t_irc_channel *next_channel;

    IRC_BUFFER_GET_SERVER_CHANNEL(buffer);

    /* make C compiler happy */
    (void) data;

    if (buffer == irc_raw_buffer)
    {
        irc_raw_buffer = NULL;
    }
    else
    {
        if (ptr_channel)
        {
            /* send PART for channel if its buffer is closed */
            if ((ptr_channel->type == IRC_CHANNEL_TYPE_CHANNEL)
                && (ptr_channel->nicks))
            {
                irc_command_part_channel (ptr_server, ptr_channel->name, NULL);
            }
            irc_channel_free (ptr_server, ptr_channel);
        }
        else
        {
            if (ptr_server)
            {
                if (!ptr_server->disconnected)
                {
                    /* send QUIT to server, then disconnect */
                    irc_command_quit_server (ptr_server, NULL);
                    irc_server_disconnect (ptr_server, 0, 0);
                }
                ptr_channel = ptr_server->channels;
                while (ptr_channel)
                {
                    next_channel = ptr_channel->next_channel;
                    if (ptr_channel->buffer != buffer)
                        weechat_buffer_close (ptr_channel->buffer);
                    ptr_channel = next_channel;
                }
                ptr_server->buffer = NULL;
            }
        }
    }

    return WEECHAT_RC_OK;
}
示例#2
0
static gboolean irc_channel_free_callback(gpointer data, gint fd, b_input_condition cond)
{
	struct irc_channel_free_data *d = data;

	if (g_slist_find(irc_connection_list, d->irc) &&
	    irc_channel_by_name(d->irc, d->name) == d->ic &&
	    !(d->ic->flags & IRC_CHANNEL_JOINED)) {
		irc_channel_free(d->ic);
	}

	g_free(d->name);
	g_free(d);
	return FALSE;
}
示例#3
0
static void cmd_channel(irc_t *irc, char **cmd)
{
	irc_channel_t *ic;
	int len;

	len = strlen(cmd[1]);

	if (len >= 1 && g_strncasecmp(cmd[1], "list", len) == 0) {
		GSList *l;
		int i = 0;

		if (strchr(irc->umode, 'b')) {
			irc_rootmsg(irc, "Channel list:");
		}

		for (l = irc->channels; l; l = l->next) {
			irc_channel_t *ic = l->data;

			irc_rootmsg(irc, "%2d. %s, %s channel%s", i, ic->name,
			            set_getstr(&ic->set, "type"),
			            ic->flags & IRC_CHANNEL_JOINED ? " (joined)" : "");

			i++;
		}
		irc_rootmsg(irc, "End of channel list");

		return;
	}

	if ((ic = irc_channel_get(irc, cmd[1])) == NULL) {
		/* If this doesn't match any channel, maybe this is the short
		   syntax (only works when used inside a channel). */
		if ((ic = irc->root->last_channel) &&
		    (len = strlen(cmd[1])) &&
		    g_strncasecmp(cmd[1], "set", len) == 0) {
			cmd_set_real(irc, cmd + 1, &ic->set, NULL);
		} else {
			irc_rootmsg(irc, "Could not find channel `%s'", cmd[1]);
		}

		return;
	}

	MIN_ARGS(2);
	len = strlen(cmd[2]);

	if (len >= 1 && g_strncasecmp(cmd[2], "set", len) == 0) {
		cmd_set_real(irc, cmd + 2, &ic->set, NULL);
	} else if (len >= 1 && g_strncasecmp(cmd[2], "del", len) == 0) {
		if (!(ic->flags & IRC_CHANNEL_JOINED) &&
		    ic != ic->irc->default_channel) {
			irc_rootmsg(irc, "Channel %s deleted.", ic->name);
			irc_channel_free(ic);
		} else {
			irc_rootmsg(irc, "Couldn't remove channel (main channel %s or "
			            "channels you're still in cannot be deleted).",
			            irc->default_channel->name);
		}
	} else {
		irc_rootmsg(irc,
		            "Unknown command: %s [...] %s. Please use \x02help commands\x02 to get a list of available commands.", "channel",
		            cmd[1]);
	}
}
示例#4
0
static void cmd_chat(irc_t *irc, char **cmd)
{
	account_t *acc;

	if (g_strcasecmp(cmd[1], "add") == 0) {
		char *channel, *s;
		struct irc_channel *ic;

		MIN_ARGS(3);

		if (!(acc = account_get(irc->b, cmd[2]))) {
			irc_rootmsg(irc, "Invalid account");
			return;
		} else if (!acc->prpl->chat_join) {
			irc_rootmsg(irc, "Named chatrooms not supported on that account.");
			return;
		}

		if (cmd[4] == NULL) {
			channel = g_strdup(cmd[3]);
			if ((s = strchr(channel, '@'))) {
				*s = 0;
			}
		} else {
			channel = g_strdup(cmd[4]);
		}

		if (strchr(CTYPES, channel[0]) == NULL) {
			s = g_strdup_printf("#%s", channel);
			g_free(channel);
			channel = s;

			irc_channel_name_strip(channel);
		}

		if ((ic = irc_channel_new(irc, channel)) &&
		    set_setstr(&ic->set, "type", "chat") &&
		    set_setstr(&ic->set, "chat_type", "room") &&
		    set_setstr(&ic->set, "account", cmd[2]) &&
		    set_setstr(&ic->set, "room", cmd[3])) {
			irc_rootmsg(irc, "Chatroom successfully added.");
		} else {
			if (ic) {
				irc_channel_free(ic);
			}

			irc_rootmsg(irc, "Could not add chatroom.");
		}
		g_free(channel);
	} else if (g_strcasecmp(cmd[1], "with") == 0) {
		irc_user_t *iu;

		MIN_ARGS(2);

		if ((iu = irc_user_by_name(irc, cmd[2])) &&
		    iu->bu && iu->bu->ic->acc->prpl->chat_with) {
			if (!iu->bu->ic->acc->prpl->chat_with(iu->bu->ic, iu->bu->handle)) {
				irc_rootmsg(irc, "(Possible) failure while trying to open "
				            "a groupchat with %s.", iu->nick);
			}
		} else {
			irc_rootmsg(irc, "Can't open a groupchat with %s.", cmd[2]);
		}
	} else if (g_strcasecmp(cmd[1], "list") == 0 ||
	           g_strcasecmp(cmd[1], "set") == 0 ||
	           g_strcasecmp(cmd[1], "del") == 0) {
		irc_rootmsg(irc,
		            "Warning: The \002chat\002 command was mostly replaced with the \002channel\002 command.");
		cmd_channel(irc, cmd);
	} else {
		irc_rootmsg(irc,
		            "Unknown command: %s %s. Please use \x02help commands\x02 to get a list of available commands.", "chat",
		            cmd[1]);
	}
}
示例#5
0
static void xml_start_element( GMarkupParseContext *ctx, const gchar *element_name, const gchar **attr_names, const gchar **attr_values, gpointer data, GError **error )
{
	struct xml_parsedata *xd = data;
	irc_t *irc = xd->irc;
	
	if( xd->unknown_tag > 0 )
	{
		xd->unknown_tag ++;
	}
	else if( g_strcasecmp( element_name, "user" ) == 0 )
	{
		char *nick = xml_attr( attr_names, attr_values, "nick" );
		char *pass = xml_attr( attr_names, attr_values, "password" );
		int st;
		
		if( !nick || !pass )
		{
			g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
			             "Missing attributes for %s element", element_name );
		}
		else if( ( st = md5_verify_password( xd->given_pass, pass ) ) == -1 )
		{
			xd->pass_st = XML_PASS_WRONG;
			g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
			             "Error while decoding password attribute" );
		}
		else if( st == 0 )
		{
			if( xd->pass_st != XML_PASS_CHECK_ONLY )
				xd->pass_st = XML_PASS_OK;
		}
		else
		{
			xd->pass_st = XML_PASS_WRONG;
			g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
			             "Password mismatch" );
		}
	}
	else if( xd->pass_st < XML_PASS_OK )
	{
		/* Let's not parse anything else if we only have to check
		   the password. */
	}
	else if( g_strcasecmp( element_name, "account" ) == 0 )
	{
		char *protocol, *handle, *server, *password = NULL, *autoconnect, *tag;
		char *pass_b64 = NULL;
		unsigned char *pass_cr = NULL;
		int pass_len;
		struct prpl *prpl = NULL;
		
		handle = xml_attr( attr_names, attr_values, "handle" );
		pass_b64 = xml_attr( attr_names, attr_values, "password" );
		server = xml_attr( attr_names, attr_values, "server" );
		autoconnect = xml_attr( attr_names, attr_values, "autoconnect" );
		tag = xml_attr( attr_names, attr_values, "tag" );
		
		protocol = xml_attr( attr_names, attr_values, "protocol" );
		if( protocol )
			prpl = find_protocol( protocol );
		
		if( !handle || !pass_b64 || !protocol )
			g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
			             "Missing attributes for %s element", element_name );
		else if( !prpl )
			g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
			             "Unknown protocol: %s", protocol );
		else if( ( pass_len = base64_decode( pass_b64, (unsigned char**) &pass_cr ) ) &&
		         arc_decode( pass_cr, pass_len, &password, xd->given_pass ) >= 0 )
		{
			xd->current_account = account_add( irc->b, prpl, handle, password );
			if( server )
				set_setstr( &xd->current_account->set, "server", server );
			if( autoconnect )
				set_setstr( &xd->current_account->set, "auto_connect", autoconnect );
			if( tag )
				set_setstr( &xd->current_account->set, "tag", tag );
		}
		else
		{
			/* Actually the _decode functions don't even return error codes,
			   but maybe they will later... */
			g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
			             "Error while decrypting account password" );
		}
		
		g_free( pass_cr );
		g_free( password );
	}
	else if( g_strcasecmp( element_name, "setting" ) == 0 )
	{
		char *setting;
		
		if( xd->current_setting )
		{
			g_free( xd->current_setting );
			xd->current_setting = NULL;
		}
		
		if( ( setting = xml_attr( attr_names, attr_values, "name" ) ) )
		{
			if( xd->current_channel != NULL )
				xd->current_set_head = &xd->current_channel->set;
			else if( xd->current_account != NULL )
				xd->current_set_head = &xd->current_account->set;
			else
				xd->current_set_head = &xd->irc->b->set;
			
			xd->current_setting = g_strdup( setting );
		}
		else
			g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
			             "Missing attributes for %s element", element_name );
	}
	else if( g_strcasecmp( element_name, "buddy" ) == 0 )
	{
		char *handle, *nick;
		
		handle = xml_attr( attr_names, attr_values, "handle" );
		nick = xml_attr( attr_names, attr_values, "nick" );
		
		if( xd->current_account && handle && nick )
		{
			nick_set_raw( xd->current_account, handle, nick );
		}
		else
		{
			g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
			             "Missing attributes for %s element", element_name );
		}
	}
	else if( g_strcasecmp( element_name, "channel" ) == 0 )
	{
		char *name, *type;
		
		name = xml_attr( attr_names, attr_values, "name" );
		type = xml_attr( attr_names, attr_values, "type" );
		
		if( !name || !type )
		{
			g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
			             "Missing attributes for %s element", element_name );
			return;
		}
		
		/* The channel may exist already, for example if it's &bitlbee.
		   Also, it's possible that the user just reconnected and the
		   IRC client already rejoined all channels it was in. They
		   should still get the right settings. */
		if( ( xd->current_channel = irc_channel_by_name( irc, name ) ) ||
		    ( xd->current_channel = irc_channel_new( irc, name ) ) )
			set_setstr(&xd->current_channel->set, "type", type );
	}
	/* Backward compatibility: Keep this around for a while for people
	   switching from BitlBee 1.2.4+. */
	else if( g_strcasecmp( element_name, "chat" ) == 0 )
	{
		char *handle, *channel;
		
		handle = xml_attr( attr_names, attr_values, "handle" );
		channel = xml_attr( attr_names, attr_values, "channel" );
		
		if( xd->current_account && handle && channel )
		{
			irc_channel_t *ic;
			
			if( ( ic = irc_channel_new( irc, channel ) ) &&
			    set_setstr( &ic->set, "type", "chat" ) &&
			    set_setstr( &ic->set, "chat_type", "room" ) &&
			    set_setstr( &ic->set, "account", xd->current_account->tag ) &&
			    set_setstr( &ic->set, "room", handle ) )
			{
				/* Try to pick up some settings where possible. */
				xd->current_channel = ic;
			}
			else if( ic )
				irc_channel_free( ic );
		}
		else
		{
			g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_INVALID_CONTENT,
			             "Missing attributes for %s element", element_name );
		}
	}
	else
	{
		xd->unknown_tag ++;
		irc_rootmsg( irc, "Warning: Unknown XML tag found in configuration file (%s). "
		                  "This may happen when downgrading BitlBee versions. "
		                  "This tag will be skipped and the information will be lost "
		                  "once you save your settings.", element_name );
		/*
		g_set_error( error, G_MARKUP_ERROR, G_MARKUP_ERROR_UNKNOWN_ELEMENT,
		             "Unkown element: %s", element_name );
		*/
	}
}
示例#6
0
文件: irc.c 项目: dgruss/bitlbee
void irc_free(irc_t * irc)
{
	GSList *l;

	irc->status |= USTATUS_SHUTDOWN;

	log_message(LOGLVL_INFO, "Destroying connection with fd %d", irc->fd);

	if (irc->status & USTATUS_IDENTIFIED && set_getbool(&irc->b->set, "save_on_quit")) {
		if (storage_save(irc, NULL, TRUE) != STORAGE_OK) {
			log_message(LOGLVL_WARNING, "Error while saving settings for user %s", irc->user->nick);
		}
	}

	for (l = irc_plugins; l; l = l->next) {
		irc_plugin_t *p = l->data;
		if (p->irc_free) {
			p->irc_free(irc);
		}
	}

	irc_connection_list = g_slist_remove(irc_connection_list, irc);

	while (irc->queries != NULL) {
		query_del(irc, irc->queries);
	}

	/* This is a little bit messy: bee_free() frees all b->users which
	   calls us back to free the corresponding irc->users. So do this
	   before we clear the remaining ones ourselves. */
	bee_free(irc->b);

	while (irc->users) {
		irc_user_free(irc, (irc_user_t *) irc->users->data);
	}

	while (irc->channels) {
		irc_channel_free(irc->channels->data);
	}

	if (irc->ping_source_id > 0) {
		b_event_remove(irc->ping_source_id);
	}
	if (irc->r_watch_source_id > 0) {
		b_event_remove(irc->r_watch_source_id);
	}
	if (irc->w_watch_source_id > 0) {
		b_event_remove(irc->w_watch_source_id);
	}

	closesocket(irc->fd);
	irc->fd = -1;

	g_hash_table_foreach_remove(irc->nick_user_hash, irc_free_hashkey, NULL);
	g_hash_table_destroy(irc->nick_user_hash);

	g_hash_table_foreach_remove(irc->watches, irc_free_hashkey, NULL);
	g_hash_table_destroy(irc->watches);

	if (irc->iconv != (GIConv) - 1) {
		g_iconv_close(irc->iconv);
	}
	if (irc->oconv != (GIConv) - 1) {
		g_iconv_close(irc->oconv);
	}

	g_free(irc->sendbuffer);
	g_free(irc->readbuffer);
	g_free(irc->password);

	g_free(irc);

	if (global.conf->runmode == RUNMODE_INETD ||
	    global.conf->runmode == RUNMODE_FORKDAEMON ||
	    (global.conf->runmode == RUNMODE_DAEMON &&
	     global.listen_socket == -1 &&
	     irc_connection_list == NULL)) {
		b_main_quit();
	}
}