irc_ctcp_replace_variables (struct t_irc_server *server, const char *format)
{
    char *res, *temp, *username, *realname;
    const char *info;
    time_t now;
    struct tm *local_time;
    char buf[1024];
    struct utsname *buf_uname;

    /*
     * $clientinfo: supported CTCP, example:
     *   ACTION DCC CLIENTINFO FINGER PING SOURCE TIME USERINFO VERSION
     */
    temp = weechat_string_replace (
        format, "$clientinfo",
        "ACTION DCC CLIENTINFO FINGER PING SOURCE TIME USERINFO VERSION");
    if (!temp)
        return NULL;
    res = temp;

    /*
     * $git: git version (output of "git describe" for a development version
     * only, empty string if unknown), example:
     *   v0.3.9-104-g7eb5cc4
     */
    info = weechat_info_get ("version_git", "");
    temp = weechat_string_replace (res, "$git", info);
    free (res);
    if (!temp)
        return NULL;
    res = temp;

    /*
     * $versiongit: WeeChat version + git version (if known), examples:
     *   0.3.9
     *   0.4.0-dev
     *   0.4.0-dev (git: v0.3.9-104-g7eb5cc4)
     */
    info = weechat_info_get ("version_git", "");
    snprintf (buf, sizeof (buf), "%s%s%s%s",
              weechat_info_get ("version", ""),
              (info && info[0]) ? " (git: " : "",
              (info && info[0]) ? info : "",
              (info && info[0]) ? ")" : "");
    temp = weechat_string_replace (res, "$versiongit", buf);
    free (res);
    if (!temp)
        return NULL;
    res = temp;

    /*
     * $version: WeeChat version, examples:
     *   0.3.9
     *   0.4.0-dev
     */
    info = weechat_info_get ("version", "");
    temp = weechat_string_replace (res, "$version", info);
    free (res);
    if (!temp)
        return NULL;
    res = temp;

    /*
     * $compilation: compilation date, example:
     *   Dec 16 2012
     */
    info = weechat_info_get ("date", "");
    temp = weechat_string_replace (res, "$compilation", info);
    free (res);
    if (!temp)
        return NULL;
    res = temp;

    /*
     * $osinfo: info about OS, example:
     *   Linux 2.6.32-5-amd64 / x86_64
     */
    buf_uname = (struct utsname *)malloc (sizeof (struct utsname));
    if (buf_uname)
    {
        if (uname (buf_uname) >= 0)
        {
            snprintf (buf, sizeof (buf), "%s %s / %s",
                      buf_uname->sysname, buf_uname->release,
                      buf_uname->machine);
            temp = weechat_string_replace (res, "$osinfo", buf);
            free (res);
            if (!temp)
            {
                free (buf_uname);
                return NULL;
            }
            res = temp;
        }
        free (buf_uname);
    }

    /*
     * $site: WeeChat web site, example:
     *   https://weechat.org/
     */
    info = weechat_info_get ("weechat_site", "");
    temp = weechat_string_replace (res, "$site", info);
    free (res);
    if (!temp)
        return NULL;
    res = temp;

    /*
     * $download: WeeChat download page, example:
     *   https://weechat.org/download
     */
    info = weechat_info_get ("weechat_site_download", "");
    temp = weechat_string_replace (res, "$download", info);
    free (res);
    if (!temp)
        return NULL;
    res = temp;

    /*
     * $time: local date/time of user, example:
     *   Sun, 16 Dec 2012 10:40:48 +0100
     */
    now = time (NULL);
    local_time = localtime (&now);
    setlocale (LC_ALL, "C");
    strftime (buf, sizeof (buf),
              weechat_config_string (irc_config_look_ctcp_time_format),
              local_time);
    setlocale (LC_ALL, "");
    temp = weechat_string_replace (res, "$time", buf);
    free (res);
    if (!temp)
        return NULL;
    res = temp;

    /*
     * $username: user name, example:
     *   name
     */
    username = weechat_string_eval_expression (
        IRC_SERVER_OPTION_STRING(server, IRC_SERVER_OPTION_USERNAME),
        NULL, NULL, NULL);
    if (username)
    {
        temp = weechat_string_replace (res, "$username", username);
        free (res);
        if (!temp)
            return NULL;
        res = temp;
        free (username);
    }

    /*
     * $realname: real name, example:
     *   John doe
     */
    realname = weechat_string_eval_expression (
        IRC_SERVER_OPTION_STRING(server, IRC_SERVER_OPTION_REALNAME),
        NULL, NULL, NULL);
    if (realname)
    {
        temp = weechat_string_replace (res, "$realname", realname);
        free (res);
        if (!temp)
            return NULL;
        res = temp;
        free (realname);
    }

    /* return result */
    return res;
}