char * logger_get_filename (struct t_gui_buffer *buffer) { char *res, *mask_expanded, *file_path; const char *mask; const char *dir_separator, *weechat_dir; int length; res = NULL; mask_expanded = NULL; file_path = NULL; dir_separator = weechat_info_get ("dir_separator", ""); if (!dir_separator) return NULL; weechat_dir = weechat_info_get ("weechat_dir", ""); if (!weechat_dir) return NULL; /* get filename mask for buffer */ mask = logger_get_mask_for_buffer (buffer); if (!mask) { weechat_printf_tags (NULL, "no_log", _("%s%s: unable to find filename mask for buffer " "\"%s\", logging is disabled for this buffer"), weechat_prefix ("error"), LOGGER_PLUGIN_NAME, weechat_buffer_get_string (buffer, "name")); return NULL; } mask_expanded = logger_get_mask_expanded (buffer, mask); if (!mask_expanded) goto end; file_path = logger_get_file_path (); if (!file_path) goto end; /* build string with path + mask */ length = strlen (file_path) + strlen (dir_separator) + strlen (mask_expanded) + 1; res = malloc (length); if (res) { snprintf (res, length, "%s%s%s", file_path, (file_path[strlen (file_path) - 1] == dir_separator[0]) ? "" : dir_separator, mask_expanded); } end: if (mask_expanded) free (mask_expanded); if (file_path) free (file_path); return res; }
char * script_config_get_dir () { const char *weechat_home; char *path, *path2; weechat_home = weechat_info_get ("weechat_dir", NULL); path = weechat_string_expand_home (weechat_config_string (script_config_scripts_dir)); path2 = weechat_string_replace ((path) ? path : weechat_config_string (script_config_scripts_dir), "%h", weechat_home); if (path && path2) { free (path); path = NULL; } if (path2) return path2; if (path) return path; return strdup (weechat_config_string (script_config_scripts_dir)); }
char * irc_color_decode_ansi (const char *string, int keep_colors) { struct t_irc_color_ansi_state ansi_state; /* allocate/compile regex if needed (first call) */ if (!irc_color_regex_ansi) { irc_color_regex_ansi = malloc (sizeof (*irc_color_regex_ansi)); if (!irc_color_regex_ansi) return NULL; if (weechat_string_regcomp (irc_color_regex_ansi, weechat_info_get ("color_ansi_regex", NULL), REG_EXTENDED) != 0) { free (irc_color_regex_ansi); irc_color_regex_ansi = NULL; return NULL; } } ansi_state.keep_colors = keep_colors; ansi_state.bold = 0; ansi_state.underline = 0; ansi_state.italic = 0; return weechat_string_replace_regex (string, irc_color_regex_ansi, "$0", '$', &irc_color_decode_ansi_cb, &ansi_state); }
int irc_color_convert_rgb2irc (int rgb) { char str_color[64], *error; const char *info_color; long number; snprintf (str_color, sizeof (str_color), "%d,%d", rgb, IRC_COLOR_TERM2IRC_NUM_COLORS); info_color = weechat_info_get ("color_rgb2term", str_color); if (!info_color || !info_color[0]) return -1; error = NULL; number = strtol (info_color, &error, 10); if (!error || error[0] || (number < 0) || (number >= IRC_COLOR_TERM2IRC_NUM_COLORS)) { return -1; } return irc_color_term2irc[number]; }
int weechat_plugin_init (struct t_weechat_plugin *plugin, int argc, char *argv[]) { /* make C compiler happy */ (void) argc; (void) argv; weechat_plugin = plugin; /* get terminal & internal charsets */ charset_terminal = weechat_info_get ("charset_terminal", ""); charset_internal = weechat_info_get ("charset_internal", ""); /* display message */ if (weechat_charset_plugin->debug >= 1) charset_display_charsets (); if (!charset_config_init ()) { weechat_printf (NULL, _("%s%s: error creating configuration file"), weechat_prefix("error"), CHARSET_PLUGIN_NAME); return WEECHAT_RC_OK; } charset_config_read (); /* /charset command */ weechat_hook_command ("charset", N_("change charset for current buffer"), N_("decode|encode <charset>" " || reset"), N_(" decode: change decoding charset\n" " encode: change encoding charset\n" "charset: new charset for current buffer\n" " reset: reset charsets for current buffer"), "decode|encode|reset", &charset_command_cb, NULL); /* modifiers hooks */ weechat_hook_modifier ("charset_decode", &charset_decode_cb, NULL); weechat_hook_modifier ("charset_encode", &charset_encode_cb, NULL); return WEECHAT_RC_OK; }
void relay_network_set_ssl_cert_key (int verbose) { #ifdef HAVE_GNUTLS char *certkey_path, *certkey_path2; int ret; gnutls_certificate_free_credentials (relay_gnutls_x509_cred); gnutls_certificate_allocate_credentials (&relay_gnutls_x509_cred); relay_network_init_ssl_cert_key_ok = 0; certkey_path = weechat_string_expand_home (weechat_config_string (relay_config_network_ssl_cert_key)); if (certkey_path) { certkey_path2 = weechat_string_replace (certkey_path, "%h", weechat_info_get ("weechat_dir", NULL)); if (certkey_path2) { ret = gnutls_certificate_set_x509_key_file (relay_gnutls_x509_cred, certkey_path2, certkey_path2, GNUTLS_X509_FMT_PEM); if (ret >= 0) { relay_network_init_ssl_cert_key_ok = 1; if (verbose) { weechat_printf (NULL, _("%s: SSL certificate and key have been " "set"), RELAY_PLUGIN_NAME); } } else { if (verbose) { weechat_printf (NULL, _("%s%s: warning: no SSL certificate/key " "found (option relay.network.ssl_cert_key)"), weechat_prefix ("error"), RELAY_PLUGIN_NAME); } } free (certkey_path2); } free (certkey_path); } #else /* make C compiler happy */ (void) verbose; #endif }
void logger_set_log_filename (struct t_logger_buffer *logger_buffer) { char *log_filename, *pos_last_sep; const char *dir_separator; struct t_logger_buffer *ptr_logger_buffer; /* get log filename for buffer */ log_filename = logger_get_filename (logger_buffer->buffer); if (!log_filename) { weechat_printf_tags (NULL, "no_log", _("%s%s: not enough memory"), weechat_prefix ("error"), LOGGER_PLUGIN_NAME); return; } /* log file is already used by another buffer? */ ptr_logger_buffer = logger_buffer_search_log_filename (log_filename); if (ptr_logger_buffer) { weechat_printf_tags (NULL, "no_log", _("%s%s: unable to start logging for buffer " "\"%s\": filename \"%s\" is already used by " "another buffer (check your log settings)"), weechat_prefix ("error"), LOGGER_PLUGIN_NAME, weechat_buffer_get_string (logger_buffer->buffer, "name"), log_filename); free (log_filename); return; } /* create directory for path in "log_filename" */ dir_separator = weechat_info_get ("dir_separator", ""); if (dir_separator) { pos_last_sep = strrchr (log_filename, dir_separator[0]); if (pos_last_sep) { pos_last_sep[0] = '\0'; weechat_mkdir_parents (log_filename, 0700); pos_last_sep[0] = dir_separator[0]; } } /* set log filename */ logger_buffer->log_filename = log_filename; }
const char * script_config_get_diff_command () { const char *diff_command; char *dir_separator; static char result[64]; struct stat st; char *path, **paths, bin[4096]; int num_paths, i, rc; diff_command = weechat_config_string (script_config_look_diff_command); if (!diff_command || !diff_command[0]) return NULL; if (strcmp (diff_command, "auto") == 0) { dir_separator = weechat_info_get ("dir_separator", ""); path = getenv ("PATH"); result[0] = '\0'; if (dir_separator && path) { paths = weechat_string_split (path, ":", WEECHAT_STRING_SPLIT_STRIP_LEFT | WEECHAT_STRING_SPLIT_STRIP_RIGHT | WEECHAT_STRING_SPLIT_COLLAPSE_SEPS, 0, &num_paths); if (paths) { for (i = 0; i < num_paths; i++) { snprintf (bin, sizeof (bin), "%s%s%s", paths[i], dir_separator, "git"); rc = stat (bin, &st); if ((rc == 0) && (S_ISREG(st.st_mode))) { snprintf (result, sizeof (result), "git diff --no-index"); break; } } weechat_string_free_split (paths); } } if (dir_separator) free (dir_separator); if (!result[0]) snprintf (result, sizeof (result), "diff"); return result; } return diff_command; }
char * weechat_python_get_python2_bin () { const char *dir_separator; char *py2_bin, *path, **paths, bin[4096]; char *versions[] = { "2.7", "2.6", "2.5", "2.4", "2.3", "2.2", "2", NULL }; int num_paths, i, j, rc; struct stat stat_buf; py2_bin = NULL; dir_separator = weechat_info_get ("dir_separator", ""); path = getenv ("PATH"); if (dir_separator && path) { paths = weechat_string_split (path, ":", 0, 0, &num_paths); if (paths) { for (i = 0; i < num_paths; i++) { for (j = 0; versions[j]; j++) { snprintf (bin, sizeof (bin), "%s%s%s%s", paths[i], dir_separator, "python", versions[j]); rc = stat (bin, &stat_buf); if ((rc == 0) && (S_ISREG(stat_buf.st_mode))) { py2_bin = strdup (bin); break; } } if (py2_bin) break; } weechat_string_free_split (paths); } } if (!py2_bin) py2_bin = strdup ("python"); return py2_bin; }
int irc_color_convert_term2irc (int color) { char str_color[64], *error; const char *info_color; long number; snprintf (str_color, sizeof (str_color), "%d", color); info_color = weechat_info_get ("color_term2rgb", str_color); if (!info_color || !info_color[0]) return -1; error = NULL; number = strtol (info_color, &error, 10); if (!error || error[0] || (number < 0) || (number > 0xFFFFFF)) return -1; return irc_color_convert_rgb2irc (number); }
char * irc_sasl_get_key_content (struct t_irc_server *server, const char *sasl_key) { const char *weechat_dir; char *key_path1, *key_path2, *content; if (!sasl_key) return NULL; content = NULL; weechat_dir = weechat_info_get ("weechat_dir", ""); key_path1 = weechat_string_replace (sasl_key, "%h", weechat_dir); key_path2 = (key_path1) ? weechat_string_expand_home (key_path1) : NULL; if (key_path2) content = weechat_file_get_content (key_path2); if (!content) { weechat_printf ( server->buffer, _("%s%s: unable to read private key in file \"%s\""), weechat_prefix ("error"), IRC_PLUGIN_NAME, (key_path2) ? key_path2 : ((key_path1) ? key_path1 : sasl_key)); } if (key_path1) free (key_path1); if (key_path2) free (key_path2); return content; }
void logger_backlog (struct t_gui_buffer *buffer, const char *filename, int lines) { const char *charset; struct t_logger_line *last_lines, *ptr_lines; char *pos_message, *pos_tab, *error, *message; time_t datetime, time_now; struct tm tm_line; int num_lines; charset = weechat_info_get ("charset_terminal", ""); weechat_buffer_set (buffer, "print_hooks_enabled", "0"); num_lines = 0; last_lines = logger_tail_file (filename, lines); ptr_lines = last_lines; while (ptr_lines) { datetime = 0; pos_message = strchr (ptr_lines->data, '\t'); if (pos_message) { /* initialize structure, because strptime does not do it */ memset (&tm_line, 0, sizeof (struct tm)); /* * we get current time to initialize daylight saving time in * structure tm_line, otherwise printed time will be shifted * and will not use DST used on machine */ time_now = time (NULL); localtime_r (&time_now, &tm_line); pos_message[0] = '\0'; error = strptime (ptr_lines->data, weechat_config_string (logger_config_file_time_format), &tm_line); if (error && !error[0] && (tm_line.tm_year > 0)) datetime = mktime (&tm_line); pos_message[0] = '\t'; } pos_message = (pos_message && (datetime != 0)) ? pos_message + 1 : ptr_lines->data; message = (charset) ? weechat_iconv_to_internal (charset, pos_message) : strdup (pos_message); if (message) { pos_tab = strchr (message, '\t'); if (pos_tab) pos_tab[0] = '\0'; weechat_printf_date_tags (buffer, datetime, "no_highlight,notify_none,logger_backlog", "%s%s%s%s%s", weechat_color (weechat_config_string (logger_config_color_backlog_line)), message, (pos_tab) ? "\t" : "", (pos_tab) ? weechat_color (weechat_config_string (logger_config_color_backlog_line)) : "", (pos_tab) ? pos_tab + 1 : ""); if (pos_tab) pos_tab[0] = '\t'; free (message); } num_lines++; ptr_lines = ptr_lines->next_line; } if (last_lines) logger_tail_free (last_lines); if (num_lines > 0) { weechat_printf_date_tags (buffer, datetime, "no_highlight,notify_none,logger_backlog_end", _("%s===\t%s========== End of backlog (%d lines) =========="), weechat_color (weechat_config_string (logger_config_color_backlog_end)), weechat_color (weechat_config_string (logger_config_color_backlog_end)), num_lines); weechat_buffer_set (buffer, "unread", ""); } weechat_buffer_set (buffer, "print_hooks_enabled", "1"); }
void logger_write_line (struct t_logger_buffer *logger_buffer, const char *format, ...) { char *message, buf_time[256], buf_beginning[1024]; const char *charset; time_t seconds; struct tm *date_tmp; int log_level; charset = weechat_info_get ("charset_terminal", ""); if (!logger_buffer->log_file) { log_level = logger_get_level_for_buffer (logger_buffer->buffer); if (log_level == 0) { logger_buffer_free (logger_buffer); return; } if (!logger_create_directory ()) { weechat_printf_tags (NULL, "no_log", _("%s%s: unable to create directory for logs " "(\"%s\")"), weechat_prefix ("error"), LOGGER_PLUGIN_NAME, weechat_config_string (logger_config_file_path)); logger_buffer_free (logger_buffer); return; } if (!logger_buffer->log_filename) logger_set_log_filename (logger_buffer); if (!logger_buffer->log_filename) { logger_buffer_free (logger_buffer); return; } logger_buffer->log_file = fopen (logger_buffer->log_filename, "a"); if (!logger_buffer->log_file) { weechat_printf_tags ( NULL, "no_log", _("%s%s: unable to write log file \"%s\": %s"), weechat_prefix ("error"), LOGGER_PLUGIN_NAME, logger_buffer->log_filename, strerror (errno)); logger_buffer_free (logger_buffer); return; } if (weechat_config_boolean (logger_config_file_info_lines) && logger_buffer->write_start_info_line) { buf_time[0] = '\0'; seconds = time (NULL); date_tmp = localtime (&seconds); if (date_tmp) { strftime (buf_time, sizeof (buf_time) - 1, weechat_config_string (logger_config_file_time_format), date_tmp); } snprintf (buf_beginning, sizeof (buf_beginning), _("%s\t**** Beginning of log ****"), buf_time); message = (charset) ? weechat_iconv_from_internal (charset, buf_beginning) : NULL; fprintf (logger_buffer->log_file, "%s\n", (message) ? message : buf_beginning); if (message) free (message); logger_buffer->flush_needed = 1; } logger_buffer->write_start_info_line = 0; } weechat_va_format (format); if (vbuffer) { message = (charset) ? weechat_iconv_from_internal (charset, vbuffer) : NULL; fprintf (logger_buffer->log_file, "%s\n", (message) ? message : vbuffer); if (message) free (message); logger_buffer->flush_needed = 1; if (!logger_timer) { fflush (logger_buffer->log_file); logger_buffer->flush_needed = 0; } free (vbuffer); } }
char * logger_get_mask_expanded (struct t_gui_buffer *buffer, const char *mask) { char *mask2, *mask_decoded, *mask_decoded2, *mask_decoded3, *mask_decoded4; char *mask_decoded5; const char *dir_separator; int length; time_t seconds; struct tm *date_tmp; mask2 = NULL; mask_decoded = NULL; mask_decoded2 = NULL; mask_decoded3 = NULL; mask_decoded4 = NULL; mask_decoded5 = NULL; dir_separator = weechat_info_get ("dir_separator", ""); if (!dir_separator) return NULL; /* * we first replace directory separator (commonly '/') by \01 because * buffer mask can contain this char, and will be replaced by replacement * char ('_' by default) */ mask2 = weechat_string_replace (mask, dir_separator, "\01"); if (!mask2) goto end; mask_decoded = weechat_buffer_string_replace_local_var (buffer, mask2); if (!mask_decoded) goto end; mask_decoded2 = weechat_string_replace (mask_decoded, dir_separator, weechat_config_string (logger_config_file_replacement_char)); if (!mask_decoded2) goto end; #ifdef __CYGWIN__ mask_decoded3 = weechat_string_replace (mask_decoded2, "\\", weechat_config_string (logger_config_file_replacement_char)); #else mask_decoded3 = strdup (mask_decoded2); #endif /* __CYGWIN__ */ if (!mask_decoded3) goto end; /* restore directory separator */ mask_decoded4 = weechat_string_replace (mask_decoded3, "\01", dir_separator); if (!mask_decoded4) goto end; /* replace date/time specifiers in mask */ length = strlen (mask_decoded4) + 256 + 1; mask_decoded5 = malloc (length); if (!mask_decoded5) goto end; seconds = time (NULL); date_tmp = localtime (&seconds); mask_decoded5[0] = '\0'; strftime (mask_decoded5, length - 1, mask_decoded4, date_tmp); /* convert to lower case? */ if (weechat_config_boolean (logger_config_file_name_lower_case)) weechat_string_tolower (mask_decoded5); if (weechat_logger_plugin->debug) { weechat_printf_tags (NULL, "no_log", "%s: buffer = \"%s\", mask = \"%s\", " "decoded mask = \"%s\"", LOGGER_PLUGIN_NAME, weechat_buffer_get_string (buffer, "name"), mask, mask_decoded5); } end: if (mask2) free (mask2); if (mask_decoded) free (mask_decoded); if (mask_decoded2) free (mask_decoded2); if (mask_decoded3) free (mask_decoded3); if (mask_decoded4) free (mask_decoded4); return mask_decoded5; }
int weechat_python_load (const char *filename) { char *argv[] = { "__weechat_plugin__" , NULL }; #if PY_MAJOR_VERSION >= 3 wchar_t *wargv[] = { NULL, NULL }; #endif FILE *fp; PyObject *weechat_outputs, *python_path, *path; const char *weechat_home; char *str_home; int len; if ((fp = fopen (filename, "r")) == NULL) { weechat_printf (NULL, weechat_gettext ("%s%s: script \"%s\" not found"), weechat_prefix ("error"), PYTHON_PLUGIN_NAME, filename); return 0; } if ((weechat_python_plugin->debug >= 2) || !python_quiet) { weechat_printf (NULL, weechat_gettext ("%s: loading script \"%s\""), PYTHON_PLUGIN_NAME, filename); } python_current_script = NULL; python_registered_script = NULL; /* PyEval_AcquireLock (); */ python_current_interpreter = Py_NewInterpreter (); #if PY_MAJOR_VERSION >= 3 /* python >= 3.x */ len = mbstowcs (NULL, argv[0], 0) + 1; wargv[0] = malloc ((len + 1) * sizeof (wargv[0][0])); if (wargv[0]) { if (mbstowcs (wargv[0], argv[0], len) == (size_t)(-1)) { free (wargv[0]); wargv[0] = NULL; } PySys_SetArgv(1, wargv); if (wargv[0]) free (wargv[0]); } #else /* python <= 2.x */ PySys_SetArgv(1, argv); #endif if (!python_current_interpreter) { weechat_printf (NULL, weechat_gettext ("%s%s: unable to create new " "sub-interpreter"), weechat_prefix ("error"), PYTHON_PLUGIN_NAME); fclose (fp); /* PyEval_ReleaseLock (); */ return 0; } PyThreadState_Swap (python_current_interpreter); /* adding $weechat_dir/python in $PYTHONPATH */ python_path = PySys_GetObject ("path"); weechat_home = weechat_info_get ("weechat_dir", ""); if (weechat_home) { len = strlen (weechat_home) + 1 + strlen (PYTHON_PLUGIN_NAME) + 1; str_home = malloc (len); if (str_home) { snprintf (str_home, len, "%s/python", weechat_home); #if PY_MAJOR_VERSION >= 3 /* python >= 3.x */ path = PyUnicode_FromString(str_home); #else /* python <= 2.x */ path = PyBytes_FromString (str_home); #endif if (path != NULL) { PyList_Insert (python_path, 0, path); Py_DECREF (path); } free (str_home); } } #if PY_MAJOR_VERSION >= 3 /* python >= 3.x */ weechat_outputs = PyModule_Create (&moduleDefOutputs); #else /* python <= 2.x */ weechat_outputs = Py_InitModule("weechatOutputs", weechat_python_output_funcs); #endif if (!weechat_outputs) { weechat_printf (NULL, weechat_gettext ("%s%s: unable to redirect stdout and " "stderr"), weechat_prefix ("error"), PYTHON_PLUGIN_NAME); } else { if (PySys_SetObject("stdout", weechat_outputs) == -1) { weechat_printf (NULL, weechat_gettext ("%s%s: unable to redirect stdout"), weechat_prefix ("error"), PYTHON_PLUGIN_NAME); } if (PySys_SetObject("stderr", weechat_outputs) == -1) { weechat_printf (NULL, weechat_gettext ("%s%s: unable to redirect stderr"), weechat_prefix ("error"), PYTHON_PLUGIN_NAME); } } python_current_script_filename = filename; if (PyRun_SimpleFile (fp, filename) != 0) { weechat_printf (NULL, weechat_gettext ("%s%s: unable to parse file \"%s\""), weechat_prefix ("error"), PYTHON_PLUGIN_NAME, filename); fclose (fp); if (PyErr_Occurred ()) PyErr_Print (); /* if script was registered, remove it from list */ if (python_current_script != NULL) { plugin_script_remove (weechat_python_plugin, &python_scripts, &last_python_script, python_current_script); } Py_EndInterpreter (python_current_interpreter); /* PyEval_ReleaseLock (); */ return 0; } if (PyErr_Occurred ()) PyErr_Print (); fclose (fp); if (!python_registered_script) { weechat_printf (NULL, weechat_gettext ("%s%s: function \"register\" not " "found (or failed) in file \"%s\""), weechat_prefix ("error"), PYTHON_PLUGIN_NAME, filename); if (PyErr_Occurred ()) PyErr_Print (); Py_EndInterpreter (python_current_interpreter); /* PyEval_ReleaseLock (); */ return 0; } python_current_script = python_registered_script; /* PyEval_ReleaseThread (python_current_script->interpreter); */ /* * set input/close callbacks for buffers created by this script * (to restore callbacks after upgrade) */ plugin_script_set_buffer_callbacks (weechat_python_plugin, python_scripts, python_current_script, &weechat_python_api_buffer_input_data_cb, &weechat_python_api_buffer_close_cb); (void) weechat_hook_signal_send ("python_script_loaded", WEECHAT_HOOK_SIGNAL_STRING, python_current_script->filename); return 1; }
/** * Return the full path to our SQLite database file. Must be freed. */ char * twc_sqlite_db_path() { const char *weechat_dir = weechat_info_get("weechat_dir", NULL); return weechat_string_replace("%h/tox/data.db", "%h", weechat_dir); }
char * irc_ctcp_replace_variables (struct t_irc_server *server, const char *format) { char *res, *temp; const char *info; time_t now; char buf[1024]; struct utsname *buf_uname; /* clientinfo */ temp = weechat_string_replace (format, "$clientinfo", "ACTION DCC CLIENTINFO FINGER PING SOURCE " "TIME USERINFO VERSION"); if (!temp) return NULL; res = temp; /* version */ info = weechat_info_get ("version", ""); temp = weechat_string_replace (res, "$version", info); free (res); if (!temp) return NULL; res = temp; /* compilation date */ info = weechat_info_get ("date", ""); temp = weechat_string_replace (res, "$compilation", info); free (res); if (!temp) return NULL; res = temp; /* info about OS */ buf_uname = (struct utsname *)malloc (sizeof (struct utsname)); if (buf_uname && (uname (buf_uname) >= 0)) { snprintf (buf, sizeof (buf), "%s %s / %s", buf_uname->sysname, buf_uname->release, buf_uname->machine); free (buf_uname); temp = weechat_string_replace (res, "$osinfo", buf); free (res); if (!temp) return NULL; res = temp; } /* site */ info = weechat_info_get ("weechat_site", ""); temp = weechat_string_replace (res, "$site", info); free (res); if (!temp) return NULL; res = temp; /* site (download page) */ info = weechat_info_get ("weechat_site_download", ""); temp = weechat_string_replace (res, "$download", info); free (res); if (!temp) return NULL; res = temp; /* time */ now = time (NULL); snprintf (buf, sizeof (buf), "%s", ctime (&now)); buf[strlen (buf) - 1] = '\0'; temp = weechat_string_replace (res, "$time", buf); free (res); if (!temp) return NULL; res = temp; /* username */ temp = weechat_string_replace (res, "$username", IRC_SERVER_OPTION_STRING(server, IRC_SERVER_OPTION_USERNAME)); free (res); if (!temp) return NULL; res = temp; /* realname */ temp = weechat_string_replace (res, "$realname", IRC_SERVER_OPTION_STRING(server, IRC_SERVER_OPTION_REALNAME)); free (res); if (!temp) return NULL; res = temp; /* return result */ return res; }
char * irc_ctcp_replace_variables (struct t_irc_server *server, const char *format) { char *res, *temp; 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 && (uname (buf_uname) >= 0)) { snprintf (buf, sizeof (buf), "%s %s / %s", buf_uname->sysname, buf_uname->release, buf_uname->machine); free (buf_uname); temp = weechat_string_replace (res, "$osinfo", buf); free (res); if (!temp) return NULL; res = temp; } /* * $site: WeeChat web site, example: * http://www.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: * http://www.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 */ temp = weechat_string_replace (res, "$username", IRC_SERVER_OPTION_STRING(server, IRC_SERVER_OPTION_USERNAME)); free (res); if (!temp) return NULL; res = temp; /* * $realname: real name, example: * John doe */ temp = weechat_string_replace (res, "$realname", IRC_SERVER_OPTION_STRING(server, IRC_SERVER_OPTION_REALNAME)); free (res); if (!temp) return NULL; res = temp; /* return result */ return res; }
void xfer_file_find_filename (struct t_xfer *xfer) { const char *dir_separator; char *path, *filename2; int length; if (!XFER_IS_FILE(xfer->type)) return; path = weechat_string_eval_path_home ( weechat_config_string (xfer_config_file_download_path), NULL, NULL, NULL); if (!path) return; xfer->local_filename = malloc (strlen (path) + strlen (xfer->remote_nick) + strlen (xfer->filename) + 4); if (!xfer->local_filename) { free (path); return; } strcpy (xfer->local_filename, path); dir_separator = weechat_info_get("dir_separator", ""); if (dir_separator && (xfer->local_filename[strlen (xfer->local_filename) - 1] != dir_separator[0])) strcat (xfer->local_filename, dir_separator); if (weechat_config_boolean (xfer_config_file_use_nick_in_filename)) { strcat (xfer->local_filename, xfer->remote_nick); strcat (xfer->local_filename, "."); } strcat (xfer->local_filename, xfer->filename); free (path); /* file already exists? */ if (access (xfer->local_filename, F_OK) == 0) { if (xfer_file_resume (xfer, xfer->local_filename)) return; /* if auto rename is not set, then abort xfer */ if (!xfer_config_file_auto_rename) { xfer_close (xfer, XFER_STATUS_FAILED); xfer_buffer_refresh (WEECHAT_HOTLIST_MESSAGE); return; } length = strlen (xfer->local_filename) + 16; filename2 = malloc (length); if (!filename2) { xfer_close (xfer, XFER_STATUS_FAILED); xfer_buffer_refresh (WEECHAT_HOTLIST_MESSAGE); return; } xfer->filename_suffix = 0; do { xfer->filename_suffix++; snprintf (filename2, length, "%s.%d", xfer->local_filename, xfer->filename_suffix); if (access (filename2, F_OK) == 0) { if (xfer_file_resume (xfer, filename2)) break; } else break; } while (1); free (xfer->local_filename); xfer->local_filename = strdup (filename2); free (filename2); } }
char * logger_get_file_path () { char *file_path, *file_path2, *file_path3; const char *weechat_dir; int length; time_t seconds; struct tm *date_tmp; file_path = NULL; file_path2 = NULL; file_path3 = NULL; weechat_dir = weechat_info_get ("weechat_dir", ""); if (!weechat_dir) goto end; /* replace "~" with user home */ file_path = weechat_string_expand_home (weechat_config_string (logger_config_file_path)); if (!file_path) goto end; /* replace "%h" with WeeChat home (at beginning of string only) */ if (strncmp (file_path, "%h", 2) == 0) { length = strlen (weechat_dir) + strlen (file_path + 2) + 1; file_path2 = malloc (length); if (file_path2) snprintf (file_path2, length, "%s%s", weechat_dir, file_path + 2); } else file_path2 = strdup (file_path); if (!file_path2) goto end; /* replace date/time specifiers in path */ length = strlen (file_path2) + 256 + 1; file_path3 = malloc (length); if (!file_path3) goto end; seconds = time (NULL); date_tmp = localtime (&seconds); file_path3[0] = '\0'; strftime (file_path3, length - 1, file_path2, date_tmp); if (weechat_logger_plugin->debug) { weechat_printf_tags (NULL, "no_log", "%s: file path = \"%s\"", LOGGER_PLUGIN_NAME, file_path3); } end: if (file_path) free (file_path); if (file_path2) free (file_path2); return file_path3; }