Esempio n. 1
0
int conf_set(char *parameter, char *value)
{
    SETTING *s;
    if ((s = set_find(parameter)) == NULL)
    {   /* can only set existing settings */
        log(LOG_WARNING, "No such configuration symbol: %s", parameter);
        log(LOG_WARNING, "Assuming that %s is a setting of type STRING",parameter);
        set_add(SET_STRING, parameter, "");
        s = set_find(parameter);
    }

    switch (s->type)
    {
    case SET_BOOL:
        set_set(s, (strcmp(value, "yes") == 0 && atoi(value) == 1)?1:0);
        break;
    case SET_FLOAT:
        set_set(s, atof(value));
        break;
    case SET_INT:
        set_set(s, atol(value));
        break;
    case SET_STRING:
    case SET_LONGSTRING:
        set_set(s, value);
        break;
    }

    return 0;
}
Esempio n. 2
0
static void handle_settings(struct xt_node *node, set_t **head)
{
	struct xt_node *c;
	struct set *s;

	for (c = node->children; (c = xt_find_node(c, "setting")); c = c->next) {
		char *name = xt_find_attr(c, "name");
		char *locked = xt_find_attr(c, "locked");

		if (!name) {
			continue;
		}

		if (strcmp(node->name, "account") == 0) {
			set_t *s = set_find(head, name);
			if (s && (s->flags & ACC_SET_ONLINE_ONLY)) {
				continue; /* U can't touch this! */
			}
		}
		set_setstr(head, name, c->text);
		if (locked && !g_strcasecmp(locked, "true")) {
			s = set_find(head, name);
			if (s) {
				s->flags |= SET_LOCKED;
			}
		}
	}
}
Esempio n. 3
0
static int cmd_set_real(irc_t *irc, char **cmd, set_t **head, cmd_set_checkflags checkflags)
{
	char *set_name = NULL, *value = NULL;
	gboolean del = FALSE;

	if (cmd[1] && g_strncasecmp(cmd[1], "-del", 4) == 0) {
		MIN_ARGS(2, 0);
		set_name = cmd[2];
		del = TRUE;
	} else {
		set_name = cmd[1];
		value = cmd[2];
	}

	if (set_name && (value || del)) {
		set_t *s = set_find(head, set_name);
		int st;

		if (s && s->flags & SET_LOCKED) {
			irc_rootmsg(irc, "This setting can not be changed");
			return 0;
		}
		if (s && checkflags && checkflags(irc, s) == 0) {
			return 0;
		}

		if (del) {
			st = set_reset(head, set_name);
		} else {
			st = set_setstr(head, set_name, value);
		}

		if (set_getstr(head, set_name) == NULL &&
		    set_find(head, set_name)) {
			/* This happens when changing the passwd, for example.
			   Showing these msgs instead gives slightly clearer
			   feedback. */
			if (st) {
				irc_rootmsg(irc, "Setting changed successfully");
			} else {
				irc_rootmsg(irc, "Failed to change setting");
			}
		} else {
			cmd_showset(irc, head, set_name);
		}
	} else if (set_name) {
		cmd_showset(irc, head, set_name);
	} else {
		set_t *s = *head;
		while (s) {
			if (set_isvisible(s)) {
				cmd_showset(irc, &s, s->key);
			}
			s = s->next;
		}
	}

	return 1;
}
Esempio n. 4
0
set_t *set_add( set_t **head, char *key, char *def, set_eval eval, void *data )
{
	set_t *s = set_find( head, key );
	
	/* Possibly the setting already exists. If it doesn't exist yet,
	   we create it. If it does, we'll just change the default. */
	if( !s )
	{
		if( ( s = *head ) )
		{
			while( s->next ) s = s->next;
			s->next = g_new0( set_t, 1 );
			s = s->next;
		}
		else
		{
			s = *head = g_new0( set_t, 1 );
		}
		s->key = g_strdup( key );
	}
	
	if( s->def )
	{
		g_free( s->def );
		s->def = NULL;
	}
	if( def ) s->def = g_strdup( def );
	
	s->eval = eval;
	s->data = data;
	
	return s;
}
Esempio n. 5
0
static void xml_text( GMarkupParseContext *ctx, const gchar *text_orig, gsize text_len, gpointer data, GError **error )
{
	char text[text_len+1];
	struct xml_parsedata *xd = data;
	
	strncpy( text, text_orig, text_len );
	
	/// making text a null terminated string
	text[text_len] = 0;
	
	if( xd->pass_st < XML_PASS_OK )
	{
		/* Let's not parse anything else if we only have to check
		   the password, or if we didn't get the chance to check it
		   yet. */
	}
	else if( g_strcasecmp( g_markup_parse_context_get_element( ctx ), "setting" ) == 0 && xd->current_setting )
	{
		if( xd->current_account )
		{
			set_t *s = set_find( xd->current_set_head, xd->current_setting );
			if( s && ( s->flags & ACC_SET_ONLINE_ONLY ) )
			{
				g_free( xd->current_setting );
				xd->current_setting = NULL;
				return;
			}
		}
		set_setstr( xd->current_set_head, xd->current_setting, (char*) text );
		g_free( xd->current_setting );
		xd->current_setting = NULL;
	}
}
Esempio n. 6
0
static void add_depend(const char *source, set_t *incl, map_t *deps)
{
    set_t *mydeps;
    llnode_t *tmp;
    int i,num;

    if (source == NULL) return;

    mydeps = map_find(deps,source);
    if (mydeps != NULL) {
        num = mydeps->nbuckets;

        for (i = 0; i < num; ++i) {
            tmp = mydeps->buckets + i;
            while (tmp->next != NULL) {
                if (set_find(incl,tmp->key) == 0) {
                    set_add(incl,tmp->key);
                    add_depend(tmp->key,incl,deps);
                }
                tmp = tmp->next;
            }
        }
    }

}
Esempio n. 7
0
cmd_t *
cmd_set(char *setname_p, cmd_kind_t kind, char *fcnname_p)
{
	cmd_t		  *new_p;
	set_t		  *set_p;

	set_p = set_find(setname_p);
	if (!set_p) {
		semantic_err(gettext("no set named \"$%s\""), setname_p);
		return (NULL);
	}
	if (kind == CMD_CONNECT && !fcn_find(fcnname_p)) {
		semantic_err(gettext("no function named \"&%s\""), fcnname_p);
		return (NULL);
	}
	new_p = new(cmd_t);
	queue_init(&new_p->qn);
#ifdef LATEBINDSETS
	new_p->isnamed = B_TRUE;
	new_p->expr.setname_p = setname_p;
#else
	new_p->isnamed = B_FALSE;
	new_p->expr.expr_p = expr_dup(set_p->exprlist_p);
#endif
	new_p->isnew = B_TRUE;
	new_p->kind = kind;
	new_p->fcnname_p = fcnname_p;

	(void) queue_append(&g_cmdlist, &new_p->qn);
	return (new_p);

}				/* end cmd_set */
Esempio n. 8
0
/* Creates a new setting with the default value if it doesnt exist */
struct settings* set_default(struct settings *set, char *name, char *value)
{
        if (!set_find(set, name))
                return (set_create(set, name, value));

        return set;
}
Esempio n. 9
0
File: set.c Progetto: MrSam/bitlbee
char *set_getstr( set_t **head, const char *key )
{
	set_t *s = set_find( head, key );
	
	if( !s || ( !s->value && !s->def ) )
		return NULL;
	
	return set_value( s );
}
Esempio n. 10
0
char *set_getstr( set_t **head, char *key )
{
	set_t *s = set_find( head, key );
	
	if( !s || ( !s->value && !s->def ) )
		return NULL;
	
	return s->value ? s->value : s->def;
}
Esempio n. 11
0
/* Changes an already existing setting, if setting doesnt exist: NOTHING */
void set_change_value(struct settings *set, char *name, char *value)
{
        struct settings *res = set_find(set, name);

        if (res) {
                free(res->value);
                res->value = strdup(value);
        }
}
Esempio n. 12
0
int set_reset( set_t **head, char *key )
{
	set_t *s;
	
	s = set_find( head, key );
	if( s )
		return set_setstr( head, key, s->def );
	
	return 0;
}
Esempio n. 13
0
/* Returns the value from the setting "name" */
char* set_lookup(struct settings *set, char *name)
{
        struct settings *ret = set_find(set, name);

        if (!ret) {
                debug_print("Couldnt find named setting");
                return(NULL);
        }

        return ret->value;
}
Esempio n. 14
0
static AstarArray* search(AstarNode *current, int dst, Set *open_set, Set *close_set, void *ud) {
    AstarArray *adjs = NULL;
    if (!current)
        return NULL;

    adjs = get_adjs(ud, current->index);
    int j;
    for (j = 0; j < adjs->len; j++) {
        int i = adjs->arr[j];

        if (i == dst) {
            return path_backtrace(current, dst);
        }

        if (set_find(close_set, i) != -1)
            continue;
        int new_g = gscore(ud, current->index, i) + current->g;

        int index;
        if ((index = set_find(open_set, i)) != -1) {
            AstarNode *node = set_index(open_set, index);
            if (node->g < new_g) {
                continue;
            }
            node->g = new_g;
            node->parent = current;
            set_bubble(open_set, index);
        } else {
            AstarNode *node = create_node(ud, i, dst, current);
            set_insert(open_set, node);
        }
    }
    array_release(&adjs);
    int x = current->index % 30;
    int y = current->index / 30;
    printf("current is %d %d\n",x,y );
    fflush(stdout);
    set_push(close_set, current);
    AstarNode *next = set_pop(open_set);
    return search(next, dst, open_set, close_set, ud);
}
Esempio n. 15
0
// Searches for scope data (properties, events) by given selector.
ScopeSelectorData*
scope_data_find(ScopeArena* A, ScopeSelector* selector)
{
    memi index;
    ScopeSelectorData *selector_data;
    if (set_find(&A->selectors, 0,
                 (u8*)selector, (u8*)selector + selector->size(),
                 &index,
                 (void**)&selector_data))
    {
        hale_assert_requirement(selector_data);
        return selector_data;
    }

    return NULL;
}
Esempio n. 16
0
static void cmd_showset(irc_t *irc, set_t **head, char *key)
{
	set_t *set;
	char *val;

	if ((val = set_getstr(head, key))) {
		irc_rootmsg(irc, "%s = `%s'", key, val);
	} else if (!(set = set_find(head, key))) {
		irc_rootmsg(irc, "Setting `%s' does not exist.", key);
		if (*head == irc->b->set) {
			irc_rootmsg(irc, "It might be an account or channel setting. "
			            "See \x02help account set\x02 and \x02help channel set\x02.");
		}
	} else if (set->flags & SET_PASSWORD) {
		irc_rootmsg(irc, "%s = `********' (hidden)", key);
	} else {
		irc_rootmsg(irc, "%s is empty", key);
	}
}
Esempio n. 17
0
static void prplcb_conn_connected(PurpleConnection *gc)
{
	struct im_connection *ic = purple_ic_by_gc(gc);
	const char *dn;
	set_t *s;

	imcb_connected(ic);

	if ((dn = purple_connection_get_display_name(gc)) &&
	    (s = set_find(&ic->acc->set, "display_name"))) {
		g_free(s->value);
		s->value = g_strdup(dn);
	}

	// user list needs to be requested for Gadu-Gadu
	purple_gg_buddylist_import(gc);

	ic->flags |= OPT_DOES_HTML;
}
Esempio n. 18
0
File: set.c Progetto: Roger/bitlbee
int set_setstr(set_t **head, const char *key, char *value)
{
	set_t *s = set_find(head, key);
	char *nv = value;

	if (!s) {
		/*
		Used to do this, but it never really made sense.
		s = set_add( head, key, NULL, NULL, NULL );
		*/
		return 0;
	}

	if (value == NULL && (s->flags & SET_NULL_OK) == 0) {
		return 0;
	}

	/* Call the evaluator. For invalid values, evaluators should now
	   return SET_INVALID, but previously this was NULL. Try to handle
	   that too if NULL is not an allowed value for this setting. */
	if (s->eval && ((nv = s->eval(s, value)) == SET_INVALID ||
	                ((s->flags & SET_NULL_OK) == 0 && nv == NULL))) {
		return 0;
	}

	if (s->value) {
		g_free(s->value);
		s->value = NULL;
	}

	/* If there's a default setting and it's equal to what we're trying to
	   set, stick with s->value = NULL. Otherwise, remember the setting. */
	if (!s->def || (g_strcmp0(nv, s->def) != 0)) {
		s->value = g_strdup(nv);
	}

	if (nv != value) {
		g_free(nv);
	}

	return 1;
}
Esempio n. 19
0
static void handle_settings( struct xt_node *node, set_t **head )
{
	struct xt_node *c;
	
	for( c = node->children; ( c = xt_find_node( c, "setting" ) ); c = c->next )
	{
		char *name = xt_find_attr( c, "name" );
		
		if( !name )
			continue;
		
		if( strcmp( node->name, "account" ) == 0 )
		{
			set_t *s = set_find( head, name );
			if( s && ( s->flags & ACC_SET_ONLINE_ONLY ) )
				continue; /* U can't touch this! */
		}
		set_setstr( head, name, c->text );
	}
}
Esempio n. 20
0
File: set.c Progetto: Roger/bitlbee
set_t *set_add(set_t **head, const char *key, const char *def, set_eval eval, void *data)
{
	set_t *s = set_find(head, key);

	/* Possibly the setting already exists. If it doesn't exist yet,
	   we create it. If it does, we'll just change the default. */
	if (!s) {
		if ((s = *head)) {
			/* Sorted insertion. Special-case insertion at the start. */
			if (strcmp(key, s->key) < 0) {
				s = g_new0(set_t, 1);
				s->next = *head;
				*head = s;
			} else {
				while (s->next && strcmp(key, s->next->key) > 0) {
					s = s->next;
				}
				set_t *last_next = s->next;
				s->next = g_new0(set_t, 1);
				s = s->next;
				s->next = last_next;
			}
		} else {
			s = *head = g_new0(set_t, 1);
		}
		s->key = g_strdup(key);
	}

	if (s->def) {
		g_free(s->def);
		s->def = NULL;
	}
	if (def) {
		s->def = g_strdup(def);
	}

	s->eval = eval;
	s->data = data;

	return s;
}
Esempio n. 21
0
static void purple_sync_settings( account_t *acc, PurpleAccount *pa )
{
	PurplePlugin *prpl = purple_plugins_find_with_id( pa->protocol_id );
	PurplePluginProtocolInfo *pi = prpl->info->extra_info;
	GList *i;
	
	for( i = pi->protocol_options; i; i = i->next )
	{
		PurpleAccountOption *o = i->data;
		const char *name;
		set_t *s;
		
		name = purple_account_option_get_setting( o );
		s = set_find( &acc->set, name );
		if( s->value == NULL )
			continue;
		
		switch( purple_account_option_get_type( o ) )
		{
		case PURPLE_PREF_STRING:
		case PURPLE_PREF_STRING_LIST:
			purple_account_set_string( pa, name, set_getstr( &acc->set, name ) );
			break;
		
		case PURPLE_PREF_INT:
			purple_account_set_int( pa, name, set_getint( &acc->set, name ) );
			break;
		
		case PURPLE_PREF_BOOLEAN:
			purple_account_set_bool( pa, name, set_getbool( &acc->set, name ) );
			break;
		
		default:
			break;
		}
	}
	
	if( pi->options & OPT_PROTO_MAIL_CHECK )
		purple_account_set_check_mail( pa, set_getbool( &acc->set, "mail_notifications" ) );
}
Esempio n. 22
0
set_t		  *
set(char *setname_p, expr_t * exprlist_p)
{
	set_t		  *new_p;
	set_t		  *old_p;

	/* does this setname exist already? */
	old_p = set_find(setname_p);
	if (old_p)
		set_destroy(old_p);

	/* create a new set */
	new_p = new(set_t);
	queue_init(&new_p->qn);
	new_p->setname_p = setname_p;
	new_p->exprlist_p = exprlist_p;

	/* append the new set to the global list */
	(void) queue_append(&g_setlist, &new_p->qn);

	return (new_p);

}				/* end set */
Esempio n. 23
0
static void cmd_account(irc_t *irc, char **cmd)
{
	account_t *a;
	int len;

	if (global.conf->authmode == AUTHMODE_REGISTERED && !(irc->status & USTATUS_IDENTIFIED)) {
		irc_rootmsg(irc, "This server only accepts registered users");
		return;
	}

	len = strlen(cmd[1]);

	if (len >= 1 && g_strncasecmp(cmd[1], "add", len) == 0) {
		struct prpl *prpl;

		MIN_ARGS(3);

		if (!global.conf->allow_account_add) {
			irc_rootmsg(irc, "This server does not allow adding new accounts");
			return;
		}

		if (cmd[4] == NULL) {
			for (a = irc->b->accounts; a; a = a->next) {
				if (strcmp(a->pass, PASSWORD_PENDING) == 0) {
					irc_rootmsg(irc, "Enter password for account %s "
					            "first (use /OPER)", a->tag);
					return;
				}
			}

			irc->status |= OPER_HACK_ACCOUNT_PASSWORD;
		}

		prpl = find_protocol(cmd[2]);

		if (prpl == NULL) {
			if (is_protocol_disabled(cmd[2])) {
				irc_rootmsg(irc, "Protocol disabled in global config");
			} else {
				irc_rootmsg(irc, "Unknown protocol");
			}
			return;
		}

		for (a = irc->b->accounts; a; a = a->next) {
			if (a->prpl == prpl && prpl->handle_cmp(a->user, cmd[3]) == 0) {
				irc_rootmsg(irc, "Warning: You already have an account with "
				            "protocol `%s' and username `%s'. Are you accidentally "
				            "trying to add it twice?", prpl->name, cmd[3]);
			}
		}

		a = account_add(irc->b, prpl, cmd[3], cmd[4] ? cmd[4] : PASSWORD_PENDING);
		if (cmd[5]) {
			irc_rootmsg(irc, "Warning: Passing a servername/other flags to `account add' "
			            "is now deprecated. Use `account set' instead.");
			set_setstr(&a->set, "server", cmd[5]);
		}

		irc_rootmsg(irc, "Account successfully added with tag %s", a->tag);

		if (cmd[4] == NULL) {
			set_t *oauth = set_find(&a->set, "oauth");
			if (oauth && bool2int(set_value(oauth))) {
				*a->pass = '******';
				irc_rootmsg(irc, "No need to enter a password for this "
				            "account since it's using OAuth");
			} else {
				irc_rootmsg(irc, "You can now use the /OPER command to "
				            "enter the password");
				if (oauth) {
					irc_rootmsg(irc, "Alternatively, enable OAuth if "
					            "the account supports it: account %s "
					            "set oauth on", a->tag);
				}
			}
		}

		return;
	} else if (len >= 1 && g_strncasecmp(cmd[1], "list", len) == 0) {
		int i = 0;

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

		for (a = irc->b->accounts; a; a = a->next) {
			char *con;

			if (a->ic && (a->ic->flags & OPT_LOGGED_IN)) {
				con = " (connected)";
			} else if (a->ic) {
				con = " (connecting)";
			} else if (a->reconnect) {
				con = " (awaiting reconnect)";
			} else {
				con = "";
			}

			irc_rootmsg(irc, "%2d (%s): %s, %s%s", i, a->tag, a->prpl->name, a->user, con);

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

		return;
	} else if (cmd[2]) {
		/* Try the following two only if cmd[2] == NULL */
	} else if (len >= 2 && g_strncasecmp(cmd[1], "on", len) == 0) {
		if (irc->b->accounts) {
			irc_rootmsg(irc, "Trying to get all accounts connected...");

			for (a = irc->b->accounts; a; a = a->next) {
				if (!a->ic && a->auto_connect) {
					if (strcmp(a->pass, PASSWORD_PENDING) == 0) {
						irc_rootmsg(irc, "Enter password for account %s "
						            "first (use /OPER)", a->tag);
					} else {
						account_on(irc->b, a);
					}
				}
			}
		} else {
			irc_rootmsg(irc, "No accounts known. Use `account add' to add one.");
		}

		return;
	} else if (len >= 2 && g_strncasecmp(cmd[1], "off", len) == 0) {
		irc_rootmsg(irc, "Deactivating all active (re)connections...");

		for (a = irc->b->accounts; a; a = a->next) {
			if (a->ic) {
				account_off(irc->b, a);
			} else if (a->reconnect) {
				cancel_auto_reconnect(a);
			}
		}

		return;
	}

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

	/* At least right now, don't accept on/off/set/del as account IDs even
	   if they're a proper match, since people not familiar with the new
	   syntax yet may get a confusing/nasty surprise. */
	if (g_strcasecmp(cmd[1], "on") == 0 ||
	    g_strcasecmp(cmd[1], "off") == 0 ||
	    g_strcasecmp(cmd[1], "set") == 0 ||
	    g_strcasecmp(cmd[1], "del") == 0 ||
	    (a = account_get(irc->b, cmd[1])) == NULL) {
		irc_rootmsg(irc, "Could not find account `%s'.", cmd[1]);

		return;
	}

	if (len >= 1 && g_strncasecmp(cmd[2], "del", len) == 0) {
		if (a->flags & ACC_FLAG_LOCKED) {
			irc_rootmsg(irc, "Account is locked, can't delete");
		}
		else if (a->ic) {
			irc_rootmsg(irc, "Account is still logged in, can't delete");
		} else {
			account_del(irc->b, a);
			irc_rootmsg(irc, "Account deleted");
		}
	} else if (len >= 2 && g_strncasecmp(cmd[2], "on", len) == 0) {
		if (a->ic) {
			irc_rootmsg(irc, "Account already online");
		} else if (strcmp(a->pass, PASSWORD_PENDING) == 0) {
			irc_rootmsg(irc, "Enter password for account %s "
			            "first (use /OPER)", a->tag);
		} else {
			account_on(irc->b, a);
		}
	} else if (len >= 2 && g_strncasecmp(cmd[2], "off", len) == 0) {
		if (a->ic) {
			account_off(irc->b, a);
		} else if (a->reconnect) {
			cancel_auto_reconnect(a);
			irc_rootmsg(irc, "Reconnect cancelled");
		} else {
			irc_rootmsg(irc, "Account already offline");
		}
	} else if (len >= 1 && g_strncasecmp(cmd[2], "set", len) == 0) {
		cmd_set_real(irc, cmd + 2, &a->set, cmd_account_set_checkflags);
	} else {
		irc_rootmsg(irc,
		            "Unknown command: %s [...] %s. Please use \x02help commands\x02 to get a list of available commands.", "account",
		            cmd[2]);
	}
}
Esempio n. 24
0
bool set_union(int x, int y) { return set_link(set_find(x), set_find(y)); }
Esempio n. 25
0
int set_find(int x) {
  return father[x] = father[x] == x ? father[x] : set_find(father[x]);
}
Esempio n. 26
0
int lc_evpprintf(const lc_arg_env_t *env, lc_appendable_t *app, const char *fmt,
                 va_list args)
{
	int         res  = 0;
	const char *last = fmt + strlen(fmt);

	/* Find the first % */
	const char *s = strchr(fmt, '%');

	/* Emit the text before the first % was found */
	size_t addlen = (s ? s : last) - fmt;
	lc_appendable_snadd(app, fmt, addlen);
	res += addlen;

	while (s != NULL) {
		lc_arg_value_t val;

		/* We must be at a '%' */
		assert(*s == '%');

		/* Reset the occurrence structure */
		lc_arg_occ_t occ;
		memset(&occ, 0, sizeof(occ));

		/* Eat all flags and set the corresponding flags in the occ struct */
		for (++s; strchr("#0-+", *s); ++s) {
			switch (*s) {
				case '#':
					occ.flag_hash = 1;
					break;
				case '0':
					occ.flag_zero = 1;
					break;
				case '-':
					occ.flag_minus = 1;
					break;
				case '+':
					occ.flag_plus = 1;
					break;
				case ' ':
					occ.flag_space = 1;
					break;
			}
		}

		/* Read the width if given */
		s = read_int(s, &occ.width);

		occ.precision = -1;

		/* read the precision if given */
		if (*s == '.') {
			int precision;
			s = read_int(s + 1, &precision);

			/* Negative or lacking precision after a '.' is treated as
			 * precision 0. */
			occ.precision = MAX(0, precision);
		}

		/*
		 * Now, we can either have:
		 * - a named argument like {node}
		 * - some modifiers followed by a conversion specifier
		 * - or some other character, which ends this format invalidly
		 */
		char            ch  = *s;
		const lc_arg_t *arg = NULL;
		switch (ch) {
			case '%':
				s++;
				lc_appendable_chadd(app, '%');
				++res;
				break;
			case '{': {
				const char *named = ++s;

				/* Read until the closing brace or end of the string. */
				for (ch = *s; ch != '}' && ch != '\0'; ch = *++s) {
				}

				if (s - named) {
					size_t n = s - named;
					char *name;
					lc_arg_t tmp;

					name = (char*) malloc(sizeof(char) * (n + 1));
					MEMCPY(name, named, n);
					name[n] = '\0';
					tmp.name = name;

					arg = set_find(lc_arg_t, env->args, &tmp, sizeof(tmp), hash_str(named));
					occ.modifier = "";
					occ.modifier_length = 0;

					/* Set the conversion specifier of the occurrence to the
					 * letter specified in the argument description. */
					if (arg)
						occ.conversion = arg->letter;

					free(name);

					/* If we ended with a closing brace, move the current
					 * pointer after it, since it is not to be dumped. */
					if (ch == '}')
						s++;
				}
				break;
			}

			default: {
				const char *mod = s;

				/* Read, as long there are letters */
				while (isalpha((unsigned char)ch) && !arg) {
					int              base = 'a';
					lc_arg_t *const *map  = env->lower;

					/* If uppercase, select the uppercase map from the
					 * environment */
					if (isupper((unsigned char)ch)) {
						base = 'A';
						map = env->upper;
					}

					if (map[ch - base] != NULL) {
						occ.modifier = mod;
						occ.modifier_length = s - mod;
						occ.conversion = ch;
						arg = map[ch - base];
					}

					ch = *++s;
				}
			}
		}

		/* Call the handler if an argument was determined */
		if (arg != NULL && arg->handler != NULL) {
			const lc_arg_handler_t *handler = arg->handler;

			/* Let the handler determine the type of the argument based on the
			 * information gathered. */
			occ.lc_arg_type = handler->get_lc_arg_type(&occ);

			/* Store the value according to argument information */
			switch (occ.lc_arg_type) {
#define LC_ARG_TYPE(type,name,va_type) \
			case lc_arg_type_ ## name: val.v_ ## name = va_arg(args, va_type); break;
#include "lc_printf_arg_types.def"
#undef LC_ARG_TYPE
			}

			/* Finally, call the handler. */
			res += handler->emit(app, &occ, &val);
		}

		const char *old = s;
		s = strchr(s, '%');
		size_t addlen = (s ? s : last) - old;
		lc_appendable_snadd(app, old, addlen);
		res += addlen;
	}

	return res;
}
Esempio n. 27
0
	fail_unless(t->data == data);
	fail_unless(strcmp(t->def, "default") == 0);
END_TEST

START_TEST(test_set_add_existing)
	void *data = "data";
	set_t *s = NULL, *t;
	t = set_add(&s, "name", "default", NULL, data);
	t = set_add(&s, "name", "newdefault", NULL, data);
	fail_unless(s == t);
	fail_unless(strcmp(t->def, "newdefault") == 0);
END_TEST

START_TEST(test_set_find_unknown)
	set_t *s = NULL, *t;
	fail_unless (set_find(&s, "foo") == NULL);
END_TEST

START_TEST(test_set_find)
	void *data = "data";
	set_t *s = NULL, *t;
	t = set_add(&s, "name", "default", NULL, data);
	fail_unless(s == t);
	fail_unless(set_find(&s, "name") == t);
END_TEST

START_TEST(test_set_get_str_default)
	void *data = "data";
	set_t *s = NULL, *t;
	t = set_add(&s, "name", "default", NULL, data);
	fail_unless(s == t);
Esempio n. 28
0
File: irc.c Progetto: dgruss/bitlbee
irc_t *irc_new(int fd)
{
	irc_t *irc;
	struct sockaddr_storage sock;
	socklen_t socklen = sizeof(sock);
	char *host = NULL, *myhost = NULL;
	irc_user_t *iu;
	GSList *l;
	set_t *s;
	bee_t *b;

	irc = g_new0(irc_t, 1);

	irc->fd = fd;
	sock_make_nonblocking(irc->fd);

	irc->r_watch_source_id = b_input_add(irc->fd, B_EV_IO_READ, bitlbee_io_current_client_read, irc);

	irc->status = USTATUS_OFFLINE;
	irc->last_pong = gettime();

	irc->nick_user_hash = g_hash_table_new(g_str_hash, g_str_equal);
	irc->watches = g_hash_table_new(g_str_hash, g_str_equal);

	irc->iconv = (GIConv) - 1;
	irc->oconv = (GIConv) - 1;

	if (global.conf->hostname) {
		myhost = g_strdup(global.conf->hostname);
	} else if (getsockname(irc->fd, (struct sockaddr*) &sock, &socklen) == 0) {
		char buf[NI_MAXHOST + 1];

		if (getnameinfo((struct sockaddr *) &sock, socklen, buf,
		                NI_MAXHOST, NULL, 0, 0) == 0) {
			myhost = g_strdup(ipv6_unwrap(buf));
		}
	}

	if (getpeername(irc->fd, (struct sockaddr*) &sock, &socklen) == 0) {
		char buf[NI_MAXHOST + 1];

		if (getnameinfo((struct sockaddr *) &sock, socklen, buf,
		                NI_MAXHOST, NULL, 0, 0) == 0) {
			host = g_strdup(ipv6_unwrap(buf));
		}
	}

	if (host == NULL) {
		host = g_strdup("localhost.localdomain");
	}
	if (myhost == NULL) {
		myhost = g_strdup("localhost.localdomain");
	}

	if (global.conf->ping_interval > 0 && global.conf->ping_timeout > 0) {
		irc->ping_source_id = b_timeout_add(global.conf->ping_interval * 1000, irc_userping, irc);
	}

	irc_connection_list = g_slist_append(irc_connection_list, irc);

	b = irc->b = bee_new();
	b->ui_data = irc;
	b->ui = &irc_ui_funcs;

	s = set_add(&b->set, "allow_takeover", "true", set_eval_bool, irc);
	s = set_add(&b->set, "away_devoice", "true", set_eval_bw_compat, irc);
	s->flags |= SET_HIDDEN;
	s = set_add(&b->set, "away_reply_timeout", "3600", set_eval_int, irc);
	s = set_add(&b->set, "charset", "utf-8", set_eval_charset, irc);
	s = set_add(&b->set, "default_target", "root", NULL, irc);
	s = set_add(&b->set, "display_namechanges", "false", set_eval_bool, irc);
	s = set_add(&b->set, "display_timestamps", "true", set_eval_bool, irc);
	s = set_add(&b->set, "handle_unknown", "add_channel", NULL, irc);
	s = set_add(&b->set, "last_version", "0", NULL, irc);
	s->flags |= SET_HIDDEN;
	s = set_add(&b->set, "nick_format", "%-@nick", NULL, irc);
	s = set_add(&b->set, "nick_lowercase", "false", set_eval_bool, irc);
	s = set_add(&b->set, "nick_underscores", "false", set_eval_bool, irc);
	s = set_add(&b->set, "offline_user_quits", "true", set_eval_bool, irc);
	s = set_add(&b->set, "ops", "both", set_eval_irc_channel_ops, irc);
	s = set_add(&b->set, "paste_buffer", "false", set_eval_bool, irc);
	s->old_key = g_strdup("buddy_sendbuffer");
	s = set_add(&b->set, "paste_buffer_delay", "200", set_eval_int, irc);
	s->old_key = g_strdup("buddy_sendbuffer_delay");
	s = set_add(&b->set, "password", NULL, set_eval_password, irc);
	s->flags |= SET_NULL_OK | SET_PASSWORD;
	s = set_add(&b->set, "private", "true", set_eval_bool, irc);
	s = set_add(&b->set, "query_order", "lifo", NULL, irc);
	s = set_add(&b->set, "root_nick", ROOT_NICK, set_eval_root_nick, irc);
	s->flags |= SET_HIDDEN;
	s = set_add(&b->set, "show_offline", "false", set_eval_bw_compat, irc);
	s->flags |= SET_HIDDEN;
	s = set_add(&b->set, "self_messages", "true", set_eval_self_messages, irc);
	s = set_add(&b->set, "simulate_netsplit", "true", set_eval_bool, irc);
	s = set_add(&b->set, "timezone", "local", set_eval_timezone, irc);
	s = set_add(&b->set, "to_char", ": ", set_eval_to_char, irc);
	s = set_add(&b->set, "typing_notice", "false", set_eval_bool, irc);
	s = set_add(&b->set, "utf8_nicks", "false", set_eval_utf8_nicks, irc);

	irc->root = iu = irc_user_new(irc, ROOT_NICK);
	iu->host = g_strdup(myhost);
	iu->fullname = g_strdup(ROOT_FN);
	iu->f = &irc_user_root_funcs;

	iu = irc_user_new(irc, NS_NICK);
	iu->host = g_strdup(myhost);
	iu->fullname = g_strdup(ROOT_FN);
	iu->f = &irc_user_root_funcs;

	irc->user = g_new0(irc_user_t, 1);
	irc->user->host = g_strdup(host);

	conf_loaddefaults(irc);

	/* Evaluator sets the iconv/oconv structures. */
	set_eval_charset(set_find(&b->set, "charset"), set_getstr(&b->set, "charset"));

	irc_write(irc, ":%s NOTICE * :%s", irc->root->host, "BitlBee-IRCd initialized, please go on");
	if (isatty(irc->fd)) {
		irc_write(irc, ":%s NOTICE * :%s", irc->root->host,
		          "If you read this, you most likely accidentally "
		          "started BitlBee in inetd mode on the command line. "
		          "You probably want to run it in (Fork)Daemon mode. "
		          "See doc/README for more information.");
	}

	g_free(myhost);
	g_free(host);

	/* libpurple doesn't like fork()s after initializing itself, so this
	   is the right moment to initialize it. */
#ifdef WITH_PURPLE
	nogaim_init();
#endif

	/* SSL library initialization also should be done after the fork, to
	   avoid shared CSPRNG state. This is required by NSS, which refuses to
	   work if a fork is detected */
	ssl_init();

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

	return irc;
}
Esempio n. 29
0
/**
 * Processes a #SteamApiMsg.
 *
 * @param sata The #SteamData.
 * @param msg  The #SteamUserMsg.
 * @param time The timestamp (UTC) of the message, or 0 for now.
 **/
static void steam_user_msg(SteamData *sata, SteamUserMsg *msg, gint64 time)
{
    SteamUserInfo *info = msg->info;
    bee_user_t    *bu;
    gchar         *str;
    guint32        f;
    gchar          sid[STEAM_ID_STR_MAX];

    STEAM_ID_STR(info->id, sid);
    STEAM_UTIL_DEBUGLN("Incoming message from %s (Type: %u, Act: %u)",
                       sid, msg->type, info->act);

    switch (msg->type) {
    case STEAM_USER_MSG_TYPE_MY_EMOTE:
    case STEAM_USER_MSG_TYPE_MY_SAYTEXT:
        if (set_find(&sata->ic->bee->set, "self_messages") == NULL)
            return;

        if (msg->type == STEAM_USER_MSG_TYPE_MY_EMOTE)
            str = g_strconcat("/me ", msg->text, NULL);
        else
            str = g_strdup(msg->text);

        imcb_buddy_msg(sata->ic, sid, str, OPT_SELFMESSAGE, time);
        g_free(str);
        return;

    case STEAM_USER_MSG_TYPE_EMOTE:
    case STEAM_USER_MSG_TYPE_SAYTEXT:
        bu = imcb_buddy_by_handle(sata->ic, sid);

        if ((bu != NULL) && (bu->flags & OPT_TYPING))
            imcb_buddy_typing(sata->ic, sid, 0);

        if (msg->type == STEAM_USER_MSG_TYPE_EMOTE)
            str = g_strconcat("/me ", msg->text, NULL);
        else
            str = g_strdup(msg->text);

        imcb_buddy_msg(sata->ic, sid, str, 0, time);
        g_free(str);
        return;

    case STEAM_USER_MSG_TYPE_LEFT_CONV:
        imcb_buddy_typing(sata->ic, sid, 0);
        return;

    case STEAM_USER_MSG_TYPE_RELATIONSHIP:
        goto relationship;

    case STEAM_USER_MSG_TYPE_TYPING:
        bu = imcb_buddy_by_handle(sata->ic, sid);

        if (G_UNLIKELY(bu == NULL))
            return;

        f = (bu->flags & OPT_TYPING) ? 0 : OPT_TYPING;
        imcb_buddy_typing(sata->ic, sid, f);
        return;

    default:
        steam_user_status(sata, info, NULL);
        return;
    }

relationship:
    switch (info->act) {
    case STEAM_USER_ACT_REMOVE:
    case STEAM_USER_ACT_IGNORE:
        imcb_remove_buddy(sata->ic, sid, NULL);
        return;

    case STEAM_USER_ACT_REQUEST:
        imcb_ask_auth(sata->ic, sid, info->nick);
        return;

    case STEAM_USER_ACT_ADD:
        imcb_add_buddy(sata->ic, sid, NULL);
        imcb_buddy_nick_hint(sata->ic, sid, info->nick);
        imcb_rename_buddy(sata->ic, sid, info->fullname);
        steam_user_status(sata, info, NULL);
        return;

    default:
        return;
    }
}
Esempio n. 30
0
static void find_includes(llnode_t *head, llist_t *todo, llist_t *paths,
                   set_t *incl, map_t *deps)
{
    FILE *fp;
    llnode_t *tmp;
    char *buffer,*full,*ptr,*end;
    const char *file;

    buffer = (char *)malloc(4096);
    full = (char *)malloc(4096);

    tmp = head;
    while (tmp->next != NULL) {
        file = tmp->key;
        fp = fopen(file,"r");
        if (fp == NULL) {
            perror("Cannot read source");
            fprintf(stderr,"For file: %s\n",file);
            exit(EXIT_FAILURE);
        }

        /* read file line by line and look for #include "..." */
        while (!feof(fp) && !ferror(fp)) {
            if (fgets(buffer,4096,fp) == NULL) continue;
            ptr = buffer;
            while (*ptr == ' ' || *ptr == '\t') ++ptr;
            if (*ptr != '#') continue;
            while (*ptr == ' ' || *ptr == '\t') ++ptr;
            if (*++ptr != 'i') continue;
            if (*++ptr != 'n') continue;
            if (*++ptr != 'c') continue;
            if (*++ptr != 'l') continue;
            if (*++ptr != 'u') continue;
            if (*++ptr != 'd') continue;
            if (*++ptr != 'e') continue;
            ++ptr;
            while (*ptr == ' ' || *ptr == '\t') ++ptr;
            if (*ptr != '"') continue;
            ++ptr;
            end = ptr;
            while (*end != '"') {
                if (*end == '\0') {
                    fprintf(stderr,"Unmatched '\"': %s\n",buffer);
                    exit(EXIT_FAILURE);
                }
                ++end;
            }
            *end = '\0';

            /* get full path to include file */
            make_path(ptr,paths,full);
            /* skip, if not found or unreadable. */
            if (full[0] == '\0') continue;

            /* if this is a yet unknown include, add to the
             * todo list, if append is enabled */
            if (set_find(incl,full) == 0) {
                set_add(incl,full);
                llist_append(todo,full);
            }

            map_add(deps,file,full);
        }
        fclose(fp);
        tmp = tmp->next;
    }
    free(buffer);
    free(full);
}