Example #1
0
void s_itoa (char *buf, long val, int radix, int len)
{
	unsigned char i, c, r, sgn = 0, pad = ' ';
	unsigned v;

	buf[0] = 0;

	if (radix < 0) {
		radix = -radix;
		if (val < 0) {
			val = -val;
			sgn = '-';
		}
	}

	v = val;
	r = radix;
	if (len < 0) {
		len = -len;
		pad = '0';
	}

	if (len > 20)
		return;

	i = 0;
	do {
		c = (unsigned char)(v % r);
		if (c < 10)
			c += '0';
		else
			c += 'a' - 10;
		buf[i++] = c;
		v /= r;
	} while (v);

	while (i < len)
		buf[i++] = pad;

	if (sgn)
		buf[i++] = sgn;

	buf[i] = 0;

	StrReverse(buf);
}
Example #2
0
void db_removenick_nt(char *nick, char *reason)
{
    char *sqlreason;
    int nickid = db_getnick(nick);

    User *u;
#ifdef USE_MYSQL
    MYSQL_RES *mysql_res;
    MYSQL_RES *mysql_res2;
#endif
    char *username, *host, *queryhost;
    char *newnick;
    char *olduser = (char *) "";
    char *newuser = (char *) "";

    u = user_find(nick);


    SET_SEGV_LOCATION();

    if (!denora->do_sql) {
        return;
    }

    if (nickid == 0) {
        alog(LOG_DEBUG, "nickid 0");
        return;
    }
    SET_SEGV_LOCATION();

    db_removefromchans(nickid);
    if (UserCacheTime) {
        sqlreason = rdb_escape(reason);
        if (u) {
            username = rdb_escape(u->username);
            host = rdb_escape(u->host);
            queryhost =
                (myNumToken(host, '.') >= 2) ? strchr(host, '.') : host;
            queryhost = StrReverse(queryhost);
            rdb_query(QUERY_HIGH,
                      "SELECT nick FROM %s WHERE username=\'%s\' AND REVERSE(hostname) LIKE \'%s%%\' AND online=\'Y\' AND nick != \'%s\' ORDER BY connecttime DESC",
                      UserTable, username, queryhost, u->sqlnick);
#ifdef USE_MYSQL
            mysql_res = mysql_store_result(mysql);
            if (mysql_res) {
                if (mysql_num_rows(mysql_res) > 0) {
                    /* Getting nickname of user we found */
                    mysql_row = mysql_fetch_row(mysql_res);
                    newnick = rdb_escape(mysql_row[0]);
                    mysql_free_result(mysql_res);

                    /* Deleting user as we got one with a similar mask which is still online */
                    alog(LOG_DEBUG,
                         "db_removenick_nt(%s): There is %s with similar mask online, so we will delete %s.",
                         nick, newnick, nick);
                    rdb_query(QUERY_LOW, "DELETE FROM %s WHERE nickid=%d",
                              UserTable, nickid);

                    /* Getting uname of the old and new users */
                    rdb_query(QUERY_LOW,
                              "SELECT nick, uname FROM %s WHERE nick=\'%s\' OR nick=\'%s\'",
                              AliasesTable, newnick, u->sqlnick);
                    mysql_res2 = mysql_store_result(mysql);
                    if (mysql_res2) {
                        while ((mysql_row =
                                mysql_fetch_row(mysql_res2)) != NULL) {
                            if (stricmp(mysql_row[0], newnick) == 0) {
                                newuser = rdb_escape(mysql_row[1]);
                            }
                            if (stricmp(mysql_row[0], u->sqlnick) == 0) {
                                olduser = rdb_escape(mysql_row[1]);
                            }
                        }
                        mysql_free_result(mysql_res2);
                    }
                    /* Summing old user to new user, if they differ */
                    /*if ((!BadPtr(newuser)) && (!BadPtr(olduser))
                        && (stricmp(newuser, olduser) != 0)) {
                        alog(LOG_DEBUG,
                             "db_removenick_nt(%s): summing old user %s and new user %s.",
                             nick, olduser, newuser);
                        sumuser(u, olduser, newuser);
                        if (olduser) {
                            free(olduser);
                        }
                        if (newuser) {
                            free(newuser);
                        }
                    }*/
                    if (newnick) {
                        free(newnick);
                    }

                } else {
                    mysql_free_result(mysql_res);
                    rdb_query(QUERY_LOW,
                              "UPDATE %s SET online=\'N\', lastquit=NOW(), lastquitmsg=\'%s\', servid=0 WHERE nickid=%d",
                              UserTable, sqlreason, nickid);
                }
            }
            if (username) {
                free(username);
            }
            if (host) {
                free(host);
            }
            SET_SEGV_LOCATION();
#endif
        } else {
            rdb_query(QUERY_LOW,
                      "UPDATE %s SET online=\'N\', lastquit=NOW(), lastquitmsg=\'%s\', servid=0 WHERE nickid=%d",
                      UserTable, sqlreason, nickid);
        }
        if (sqlreason) {
            free(sqlreason);
        }
    } else {
        rdb_query(QUERY_LOW, "DELETE FROM %s WHERE nickid=%d",
                  UserTable, nickid);
    }


    SET_SEGV_LOCATION();
}
Example #3
0
/* -1 if nick not found, nickid else */
int db_checknick_nt(char *nick)
{
    int nickid = -1;
    User *u;
#ifdef USE_MYSQL
    MYSQL_RES *mysql_res;
    MYSQL_RES *mysql_res2;
#endif
    char *username, *host, *queryhost, *olduname;

    SET_SEGV_LOCATION();

    u = user_find(nick);
    if (u && u->sqlid) {
        return u->sqlid;
    }

    if (!denora->do_sql) {
        return -1;
    }

    if (u) {
        username = rdb_escape(u->username);
        host = rdb_escape(u->host);
        queryhost =
            (myNumToken(host, '.') >= 2) ? strchr(host, '.') : host;
        queryhost = StrReverse(queryhost);
        rdb_query(QUERY_HIGH,
                  "SELECT nickid,nick FROM %s WHERE (username=\'%s\' AND REVERSE(hostname) LIKE \'%s%%\' AND online=\'N\') OR nick=\'%s\' ORDER BY connecttime DESC",
                  UserTable, username, queryhost, u->sqlnick);

#ifdef USE_MYSQL
        mysql_res = mysql_store_result(mysql);
        if (mysql_res) {
            if (mysql_num_rows(mysql_res)) {
                mysql_row = mysql_fetch_row(mysql_res);
                nickid = strtol(mysql_row[0], NULL, 10);
                if (stricmp(mysql_row[1], u->sqlnick) != 0) {
                    /* Removing old user to avoid duplicate on update, which will happen shortly */
                    rdb_query(QUERY_HIGH,
                              "DELETE FROM %s WHERE nick=\'%s\' ",
                              UserTable, u->sqlnick);
                    /* Getting uname from alias table with old nick */
                    rdb_query(QUERY_HIGH,
                              "SELECT uname FROM %s WHERE nick=\'%s\' ",
                              AliasesTable, u->sqlnick);
                    mysql_res2 = mysql_store_result(mysql);
                    if (mysql_res2) {
                        if (mysql_num_rows(mysql_res2)) {
                            mysql_row = mysql_fetch_row(mysql_res2);
                            olduname = rdb_escape(mysql_row[0]);
                            /* Adding alias entry with new nick and old uname to avoid creation of new uname */
                            rdb_query(QUERY_HIGH,
                                      "INSERT INTO %s (nick, uname) VALUES (\'%s\', \'%s\') ON DUPLICATE KEY UPDATE uname=\'%s\'",
                                      AliasesTable, u->sqlnick, olduname,
                                      olduname);
                            free(olduname);
                        }
                        mysql_free_result(mysql_res2);
                    }
                }
                mysql_free_result(mysql_res);
            }
            SET_SEGV_LOCATION();
        } else {
            nickid = -1;
        }
        if (username) {
            free(username);
        }
        if (host) {
            free(host);
        }
#endif
        return nickid;
    } else {
        rdb_query(QUERY_HIGH, "SELECT nickid FROM %s WHERE nick=\'%s\'",
                  UserTable, nick);
#ifdef USE_MYSQL
        mysql_res = mysql_store_result(mysql);
        if (mysql_res) {
            if (mysql_num_rows(mysql_res)) {
                mysql_row = mysql_fetch_row(mysql_res);
                nickid = strtol(mysql_row[0], NULL, 10);
            }
            SET_SEGV_LOCATION();
            mysql_free_result(mysql_res);
        } else {
            nickid = -1;
        }
#endif
        return nickid;
    }
}