2 * @file util.h Utility Functions
8 * Purple is the legal property of its developers, whose names are too numerous
9 * to list here. Please refer to the COPYRIGHT file distributed with this
10 * source distribution.
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
26 * @todo Rename the functions so that they live somewhere in the purple
29 #ifndef _PURPLE_UTIL_H_
30 #define _PURPLE_UTIL_H_
34 typedef struct _PurpleUtilFetchUrlData PurpleUtilFetchUrlData;
35 typedef struct _PurpleMenuAction PurpleMenuAction;
36 typedef struct _PurpleKeyValuePair PurpleKeyValuePair;
47 struct _PurpleMenuAction
50 PurpleCallback callback;
55 typedef char *(*PurpleInfoFieldFormatCallback)(const char *field, size_t len);
60 * This is used by, among other things, purple_gtk_combo* functions to pass in a
61 * list of key-value pairs so it can display a user-friendly value.
63 struct _PurpleKeyValuePair
71 * Creates a new PurpleMenuAction.
73 * @param label The text label to display for this action.
74 * @param callback The function to be called when the action is used on
76 * @param data Additional data to be passed to the callback.
77 * @param children A GList of PurpleMenuActions to be added as a submenu
79 * @return The PurpleMenuAction.
81 PurpleMenuAction *purple_menu_action_new(const char *label, PurpleCallback callback,
82 gpointer data, GList *children);
85 * Frees a PurpleMenuAction
87 * @param act The PurpleMenuAction to free.
89 void purple_menu_action_free(PurpleMenuAction *act);
92 * Set the appropriate presence values for the currently playing song.
94 * @param title The title of the song, @c NULL to unset the value.
95 * @param artist The artist of the song, can be @c NULL.
96 * @param album The album of the song, can be @c NULL.
99 void purple_util_set_current_song(const char *title, const char *artist,
103 * Format song information.
105 * @param title The title of the song, @c NULL to unset the value.
106 * @param artist The artist of the song, can be @c NULL.
107 * @param album The album of the song, can be @c NULL.
108 * @param unused Currently unused, must be @c NULL.
110 * @return The formatted string. The caller must #g_free the returned string.
113 char * purple_util_format_song_info(const char *title, const char *artist,
114 const char *album, gpointer unused);
116 /**************************************************************************/
117 /** @name Utility Subsystem */
118 /**************************************************************************/
122 * Initializes the utility subsystem.
126 void purple_util_init(void);
129 * Uninitializes the util subsystem.
133 void purple_util_uninit(void);
137 /**************************************************************************/
138 /** @name Base16 Functions */
139 /**************************************************************************/
143 * Converts a chunk of binary data to its base-16 equivalent.
145 * @param data The data to convert.
146 * @param len The length of the data.
148 * @return The base-16 string in the ASCII encoding. Must be
149 * g_free'd when no longer needed.
151 * @see purple_base16_decode()
153 gchar *purple_base16_encode(const guchar *data, gsize len);
156 * Converts an ASCII string of base-16 encoded data to
157 * the binary equivalent.
159 * @param str The base-16 string to convert to raw data.
160 * @param ret_len The length of the returned data. You can
161 * pass in NULL if you're sure that you know
162 * the length of the decoded data, or if you
163 * know you'll be able to use strlen to
164 * determine the length, etc.
166 * @return The raw data. Must be g_free'd when no longer needed.
168 * @see purple_base16_encode()
170 guchar *purple_base16_decode(const char *str, gsize *ret_len);
173 * Converts a chunk of binary data to a chunked base-16 representation
174 * (handy for key fingerprints)
176 * Example output: 01:23:45:67:89:AB:CD:EF
178 * @param data The data to convert.
179 * @param len The length of the data.
181 * @return The base-16 string in the ASCII chunked encoding. Must be
182 * g_free'd when no longer needed.
184 gchar *purple_base16_encode_chunked(const guchar *data, gsize len);
189 /**************************************************************************/
190 /** @name Base64 Functions */
191 /**************************************************************************/
195 * Converts a chunk of binary data to its base-64 equivalent.
197 * @param data The data to convert.
198 * @param len The length of the data.
200 * @return The base-64 string in the ASCII encoding. Must be
201 * g_free'd when no longer needed.
203 * @see purple_base64_decode()
205 gchar *purple_base64_encode(const guchar *data, gsize len);
208 * Converts an ASCII string of base-64 encoded data to
209 * the binary equivalent.
211 * @param str The base-64 string to convert to raw data.
212 * @param ret_len The length of the returned data. You can
213 * pass in NULL if you're sure that you know
214 * the length of the decoded data, or if you
215 * know you'll be able to use strlen to
216 * determine the length, etc.
218 * @return The raw data. Must be g_free'd when no longer needed.
220 * @see purple_base64_encode()
222 guchar *purple_base64_decode(const char *str, gsize *ret_len);
226 /**************************************************************************/
227 /** @name Quoted Printable Functions */
228 /**************************************************************************/
232 * Converts a quoted printable string back to its readable equivalent.
233 * What is a quoted printable string, you ask? It's an encoding used
234 * to transmit binary data as ASCII. It's intended purpose is to send
235 * emails containing non-ASCII characters. Wikipedia has a pretty good
236 * explanation. Also see RFC 2045.
238 * @param str The quoted printable ASCII string to convert to raw data.
239 * @param ret_len The length of the returned data.
241 * @return The readable string. Must be g_free'd when no longer needed.
243 guchar *purple_quotedp_decode(const char *str, gsize *ret_len);
247 /**************************************************************************/
248 /** @name MIME Functions */
249 /**************************************************************************/
253 * Converts a MIME header field string back to its readable equivalent
254 * according to RFC 2047. Basically, a header is plain ASCII and can
255 * contain any number of sections called "encoded-words." The format
256 * of an encoded word is =?ISO-8859-1?Q?Keld_J=F8rn_Simonsen?=
257 * =? designates the beginning of the encoded-word
258 * ?= designates the end of the encoded-word
260 * An encoded word is segmented into three pieces by the use of a
261 * question mark. The first piece is the character set, the second
262 * piece is the encoding, and the third piece is the encoded text.
264 * @param str The ASCII string, possibly containing any number of
265 * encoded-word sections.
267 * @return The string, with any encoded-word sections decoded and
268 * converted to UTF-8. Must be g_free'd when no longer
271 char *purple_mime_decode_field(const char *str);
276 /**************************************************************************/
277 /** @name Date/Time Functions */
278 /**************************************************************************/
282 * Formats a time into the specified format.
284 * This is essentially strftime(), but it has a static buffer
285 * and handles the UTF-8 conversion for the caller.
287 * This function also provides the GNU %z formatter if the underlying C
288 * library doesn't. However, the format string parser is very naive, which
289 * means that conversions specifiers to %z cannot be guaranteed. The GNU
290 * strftime(3) man page describes %z as: 'The time-zone as hour offset from
291 * GMT. Required to emit RFC822-conformant dates
292 * (using "%a, %d %b %Y %H:%M:%S %z"). (GNU)'
294 * On Windows, this function also converts the results for %Z from a timezone
295 * name (as returned by the system strftime() %Z format string) to a timezone
296 * abbreviation (as is the case on Unix). As with %z, conversion specifiers
297 * should not be used.
299 * @param format The format string, in UTF-8
300 * @param tm The time to format, or @c NULL to use the current local time
302 * @return The formatted time, in UTF-8.
304 * @note @a format is required to be in UTF-8. This differs from strftime(),
305 * where the format is provided in the locale charset.
307 const char *purple_utf8_strftime(const char *format, const struct tm *tm);
310 * Gets a string representation of the local timezone offset
312 * @param tm The time to get the timezone for
313 * @param iso TRUE to format the offset according to ISO-8601, FALSE to
314 * not substitute 'Z' for 0 offset, and to not separate
315 * hours and minutes with a colon.
317 const char *purple_get_tzoff_str(const struct tm *tm, gboolean iso);
320 * Formats a time into the user's preferred short date format.
322 * The returned string is stored in a static buffer, so the result
323 * should be g_strdup()'d if it's going to be kept.
325 * @param tm The time to format, or @c NULL to use the current local time
327 * @return The date, formatted as per the user's settings.
329 const char *purple_date_format_short(const struct tm *tm);
332 * Formats a time into the user's preferred short date plus time format.
334 * The returned string is stored in a static buffer, so the result
335 * should be g_strdup()'d if it's going to be kept.
337 * @param tm The time to format, or @c NULL to use the current local time
339 * @return The timestamp, formatted as per the user's settings.
341 const char *purple_date_format_long(const struct tm *tm);
344 * Formats a time into the user's preferred full date and time format.
346 * The returned string is stored in a static buffer, so the result
347 * should be g_strdup()'d if it's going to be kept.
349 * @param tm The time to format, or @c NULL to use the current local time
351 * @return The date and time, formatted as per the user's settings.
353 const char *purple_date_format_full(const struct tm *tm);
356 * Formats a time into the user's preferred time format.
358 * The returned string is stored in a static buffer, so the result
359 * should be g_strdup()'d if it's going to be kept.
361 * @param tm The time to format, or @c NULL to use the current local time
363 * @return The time, formatted as per the user's settings.
365 const char *purple_time_format(const struct tm *tm);
368 * Builds a time_t from the supplied information.
370 * @param year The year.
371 * @param month The month.
372 * @param day The day.
373 * @param hour The hour.
374 * @param min The minute.
375 * @param sec The second.
379 time_t purple_time_build(int year, int month, int day, int hour,
382 /** Used by purple_str_to_time to indicate no timezone offset was
383 * specified in the timestamp string. */
384 #define PURPLE_NO_TZ_OFF -500000
387 * Parses a timestamp in jabber, ISO8601, or MM/DD/YYYY format and returns
390 * @param timestamp The timestamp
391 * @param utc Assume UTC if no timezone specified
392 * @param tm If not @c NULL, the caller can get a copy of the
393 * struct tm used to calculate the time_t return value.
394 * @param tz_off If not @c NULL, the caller can get a copy of the
395 * timezone offset (from UTC) used to calculate the time_t
396 * return value. Note: Zero is a valid offset. As such,
397 * the value of the macro @c PURPLE_NO_TZ_OFF indicates no
398 * offset was specified (which means that the local
399 * timezone was used in the calculation).
400 * @param rest If not @c NULL, the caller can get a pointer to the
401 * part of @a timestamp left over after parsing is
402 * completed, if it's not the end of @a timestamp.
406 time_t purple_str_to_time(const char *timestamp, gboolean utc,
407 struct tm *tm, long *tz_off, const char **rest);
412 /**************************************************************************/
413 /** @name Markup Functions */
414 /**************************************************************************/
418 * Escapes special characters in a plain-text string so they display
419 * correctly as HTML. For example, & is replaced with & and < is
422 * This is exactly the same as g_markup_escape_text(), except that it
423 * does not change ' to ' because ' is not a valid HTML 4 entity,
424 * and is displayed literally in IE7.
428 gchar *purple_markup_escape_text(const gchar *text, gssize length);
431 * Finds an HTML tag matching the given name.
433 * This locates an HTML tag's start and end, and stores its attributes
434 * in a GData hash table. The names of the attributes are lower-cased
435 * in the hash table, and the name of the tag is case insensitive.
437 * @param needle The name of the tag
438 * @param haystack The null-delimited string to search in
439 * @param start A pointer to the start of the tag if found
440 * @param end A pointer to the end of the tag if found
441 * @param attributes The attributes, if the tag was found. This should
442 * be freed with g_datalist_clear().
443 * @return TRUE if the tag was found
445 gboolean purple_markup_find_tag(const char *needle, const char *haystack,
446 const char **start, const char **end,
450 * Extracts a field of data from HTML.
452 * This is a scary function. See protocols/msn/msn.c and
453 * protocols/yahoo/yahoo_profile.c for example usage.
455 * @param str The string to parse.
456 * @param len The size of str.
457 * @param user_info The destination PurpleNotifyUserInfo to which the new
458 * field info should be added.
459 * @param start_token The beginning token.
460 * @param skip The number of characters to skip after the
462 * @param end_token The ending token.
463 * @param check_value The value that the last character must meet.
464 * @param no_value_token The token indicating no value is given.
465 * @param display_name The short descriptive name to display for this token.
466 * @param is_link TRUE if this should be a link, or FALSE otherwise.
467 * @param link_prefix The prefix for the link.
468 * @param format_cb A callback to format the value before adding it.
470 * @return TRUE if successful, or FALSE otherwise.
472 gboolean purple_markup_extract_info_field(const char *str, int len, PurpleNotifyUserInfo *user_info,
473 const char *start_token, int skip,
474 const char *end_token, char check_value,
475 const char *no_value_token,
476 const char *display_name, gboolean is_link,
477 const char *link_prefix,
478 PurpleInfoFieldFormatCallback format_cb);
481 * Converts HTML markup to XHTML.
483 * @param html The HTML markup.
484 * @param dest_xhtml The destination XHTML output.
485 * @param dest_plain The destination plain-text output.
487 void purple_markup_html_to_xhtml(const char *html, char **dest_xhtml,
491 * Strips HTML tags from a string.
493 * @param str The string to strip HTML from.
495 * @return The new string without HTML. You must g_free this string
496 * when finished with it.
498 char *purple_markup_strip_html(const char *str);
501 * Adds the necessary HTML code to turn URIs into HTML links in a string.
503 * @param str The string to linkify.
505 * @return The new string with all URIs surrounded in standard
506 * HTML <a href="whatever"></a> tags. You must g_free this
507 * string when finished with it.
509 char *purple_markup_linkify(const char *str);
512 * Unescapes HTML entities to their literal characters. Also translates
514 * For example "&" is replaced by '&' and so on.
515 * Actually only "&", """, "<" and ">" are currently
518 * @param html The string in which to unescape any HTML entities
520 * @return The text with HTML entities literalized. You must g_free
521 * this string when finished with it.
523 char *purple_unescape_html(const char *html);
526 * Returns a newly allocated substring of the HTML UTF-8 string "str".
527 * The markup is preserved such that the substring will have the same
528 * formatting as original string, even though some tags may have been
529 * opened before "x", or may close after "y". All open tags are closed
530 * at the end of the returned string, in the proper order.
532 * Note that x and y are in character offsets, not byte offsets, and
533 * are offsets into an unformatted version of str. Because of this,
534 * this function may be sensitive to changes in GtkIMHtml and may break
535 * when used with other UI's. libpurple users are encouraged to report and
536 * work out any problems encountered.
538 * @param str The input NUL terminated, HTML, UTF-8 (or ASCII) string.
539 * @param x The character offset into an unformatted version of str to
541 * @param y The character offset (into an unformatted vesion of str) of
542 * one past the last character to include in the slice.
544 * @return The HTML slice of string, with all formatting retained.
546 char *purple_markup_slice(const char *str, guint x, guint y);
549 * Returns a newly allocated string containing the name of the tag
550 * located at "tag". Tag is expected to point to a '<', and contain
551 * a '>' sometime after that. If there is no '>' and the string is
552 * not NUL terminated, this function can be expected to segfault.
554 * @param tag The string starting a HTML tag.
555 * @return A string containing the name of the tag.
557 char *purple_markup_get_tag_name(const char *tag);
560 * Returns a constant string of the character representation of the HTML
561 * entity pointed to by @a text. For example, purple_markup_unescape_entity("&")
562 * will return "&". The @a text variable is expected to point to an '&',
563 * the first character of the entity. If given an unrecognized entity, the function
566 * Note that this function, unlike purple_unescape_html(), does not search
567 * the string for the entity, does not replace the entity, and does not
568 * return a newly allocated string.
570 * @param text A string containing an HTML entity.
571 * @param length If not @c NULL, the string length of the entity is stored in this location.
573 * @return A constant string containing the character representation of the given entity.
575 const char * purple_markup_unescape_entity(const char *text, int *length);
578 * Returns a newly allocated string containing the value of the CSS property specified
579 * in opt. The @a style argument is expected to point to a HTML inline CSS.
580 * The function will seek for the CSS property and return its value.
582 * For example, purple_markup_get_css_property("direction:rtl;color:#dc4d1b;",
583 * "color") would return "#dc4d1b".
585 * On error or if the requested property was not found, the function returns
588 * @param style A string containing the inline CSS text.
589 * @param opt The requested CSS property.
591 * @return The value of the requested CSS property.
593 char * purple_markup_get_css_property(const gchar *style, const gchar *opt);
596 * Check if the given HTML contains RTL text.
598 * @param html The HTML text.
600 * @return TRUE if the text contains RTL text, FALSE otherwise.
604 gboolean purple_markup_is_rtl(const char *html);
609 /**************************************************************************/
610 /** @name Path/Filename Functions */
611 /**************************************************************************/
615 * Returns the user's home directory.
617 * @return The user's home directory.
619 * @see purple_user_dir()
621 const gchar *purple_home_dir(void);
624 * Returns the purple settings directory in the user's home directory.
625 * This is usually ~/.purple
627 * @return The purple settings directory.
629 * @see purple_home_dir()
631 const char *purple_user_dir(void);
634 * Define a custom purple settings directory, overriding the default (user's home directory/.purple)
635 * @param dir The custom settings directory
637 void purple_util_set_user_dir(const char *dir);
640 * Builds a complete path from the root, making any directories along
641 * the path which do not already exist.
643 * @param path The path you wish to create. Note that it must start
644 * from the root or this function will fail.
645 * @param mode Unix-style permissions for this directory.
647 * @return 0 for success, nonzero on any error.
649 int purple_build_dir(const char *path, int mode);
652 * Write a string of data to a file of the given name in the Purple
653 * user directory ($HOME/.purple by default). The data is typically
654 * a serialized version of one of Purple's config files, such as
655 * prefs.xml, accounts.xml, etc. And the string is typically
656 * obtained using xmlnode_to_formatted_str. However, this function
657 * should work fine for saving binary files as well.
659 * @param filename The basename of the file to write in the purple_user_dir.
660 * @param data A null-terminated string of data to write.
661 * @param size The size of the data to save. If data is
662 * null-terminated you can pass in -1.
664 * @return TRUE if the file was written successfully. FALSE otherwise.
666 gboolean purple_util_write_data_to_file(const char *filename, const char *data,
670 * Write data to a file using the absolute path.
672 * This exists for Glib backwards compatibility reasons.
674 * @param filename_full Filename to write to
675 * @param data A null-terminated string of data to write.
676 * @param size The size of the data to save. If data is
677 * null-terminated you can pass in -1.
679 * @return TRUE if the file was written successfully. FALSE otherwise.
681 * @todo Remove this function (use g_file_set_contents instead) when 3.0.0
683 * @see purple_util_write_data_to_file()
687 purple_util_write_data_to_file_absolute(const char *filename_full, const char *data, gssize size);
690 * Read the contents of a given file and parse the results into an
691 * xmlnode tree structure. This is intended to be used to read
692 * Purple's configuration xml files (prefs.xml, pounces.xml, etc.)
694 * @param filename The basename of the file to open in the purple_user_dir.
695 * @param description A very short description of the contents of this
696 * file. This is used in error messages shown to the
697 * user when the file can not be opened. For example,
698 * "preferences," or "buddy pounces."
700 * @return An xmlnode tree of the contents of the given file. Or NULL, if
701 * the file does not exist or there was an error reading the file.
703 xmlnode *purple_util_read_xml_from_file(const char *filename,
704 const char *description);
707 * Creates a temporary file and returns a file pointer to it.
709 * This is like mkstemp(), but returns a file pointer and uses a
710 * pre-set template. It uses the semantics of tempnam() for the
711 * directory to use and allocates the space for the file path.
713 * The caller is responsible for closing the file and removing it when
714 * done, as well as freeing the space pointed to by @a path with
717 * @param path The returned path to the temp file.
718 * @param binary Text or binary, for platforms where it matters.
720 * @return A file pointer to the temporary file, or @c NULL on failure.
722 FILE *purple_mkstemp(char **path, gboolean binary);
725 * Returns an extension corresponding to the image data's file type.
727 * @param data A pointer to the image data
728 * @param len The length of the image data
730 * @return The appropriate extension, or "icon" if unknown.
733 purple_util_get_image_extension(gconstpointer data, size_t len);
736 * Returns a SHA-1 hash string of the data passed in.
738 char *purple_util_get_image_checksum(gconstpointer image_data, size_t image_len);
741 * @return A hex encoded version of the SHA-1 hash of the data passed
742 * in with the correct file extention appended. The file
743 * extension is determined by calling
744 * purple_util_get_image_extension(). This return value must
745 * be g_freed by the caller.
747 char *purple_util_get_image_filename(gconstpointer image_data, size_t image_len);
752 /**************************************************************************/
753 /** @name Environment Detection Functions */
754 /**************************************************************************/
758 * Checks if the given program name is valid and executable.
760 * @param program The file name of the application.
762 * @return TRUE if the program is runable.
764 gboolean purple_program_is_valid(const char *program);
767 * Check if running GNOME.
769 * @return TRUE if running GNOME, FALSE otherwise.
771 gboolean purple_running_gnome(void);
774 * Check if running KDE.
776 * @return TRUE if running KDE, FALSE otherwise.
778 gboolean purple_running_kde(void);
781 * Check if running OS X.
783 * @return TRUE if running OS X, FALSE otherwise.
785 gboolean purple_running_osx(void);
788 * Returns the IP address from a socket file descriptor.
790 * @param fd The socket file descriptor.
792 * @return The IP address, or @c NULL on error.
794 char *purple_fd_get_ip(int fd);
799 /**************************************************************************/
800 /** @name String Functions */
801 /**************************************************************************/
805 * Tests two strings for equality.
807 * Unlike strcmp(), this function will not crash if one or both of the
808 * strings are @c NULL.
810 * @param left A string
811 * @param right A string to compare with left
813 * @return @c TRUE if the strings are the same, else @c FALSE.
817 gboolean purple_strequal(const gchar *left, const gchar *right);
820 * Normalizes a string, so that it is suitable for comparison.
822 * The returned string will point to a static buffer, so if the
823 * string is intended to be kept long-term, you <i>must</i>
824 * g_strdup() it. Also, calling normalize() twice in the same line
825 * will lead to problems.
827 * @param account The account the string belongs to, or NULL if you do
828 * not know the account. If you use NULL, the string
829 * will still be normalized, but if the PRPL uses a
830 * custom normalization function then the string may
831 * not be normalized correctly.
832 * @param str The string to normalize.
834 * @return A pointer to the normalized version stored in a static buffer.
836 const char *purple_normalize(const PurpleAccount *account, const char *str);
839 * Normalizes a string, so that it is suitable for comparison.
841 * This is one possible implementation for the PRPL callback
842 * function "normalize." It returns a lowercase and UTF-8
843 * normalized version of the string.
845 * @param account The account the string belongs to.
846 * @param str The string to normalize.
848 * @return A pointer to the normalized version stored in a static buffer.
850 const char *purple_normalize_nocase(const PurpleAccount *account, const char *str);
853 * Compares two strings to see if the first contains the second as
856 * @param s The string to check.
857 * @param p The prefix in question.
859 * @return TRUE if p is a prefix of s, otherwise FALSE.
861 gboolean purple_str_has_prefix(const char *s, const char *p);
864 * Compares two strings to see if the second is a proper suffix
867 * @param s The string to check.
868 * @param x The suffix in question.
870 * @return TRUE if x is a a suffix of s, otherwise FALSE.
872 gboolean purple_str_has_suffix(const char *s, const char *x);
875 * Duplicates a string and replaces all newline characters from the
876 * source string with HTML linebreaks.
878 * @param src The source string.
880 * @return The new string. Must be g_free'd by the caller.
882 gchar *purple_strdup_withhtml(const gchar *src);
885 * Ensures that all linefeeds have a matching carriage return.
887 * @param str The source string.
889 * @return The string with carriage returns.
891 char *purple_str_add_cr(const char *str);
894 * Strips all instances of the given character from the
895 * given string. The string is modified in place. This
896 * is useful for stripping new line characters, for example.
899 * purple_str_strip_char(my_dumb_string, '\n');
901 * @param str The string to strip characters from.
902 * @param thechar The character to strip from the given string.
904 void purple_str_strip_char(char *str, char thechar);
907 * Given a string, this replaces all instances of one character
908 * with another. This happens inline (the original string IS
911 * @param string The string from which to replace stuff.
912 * @param delimiter The character you want replaced.
913 * @param replacement The character you want inserted in place
914 * of the delimiting character.
916 void purple_util_chrreplace(char *string, char delimiter,
920 * Given a string, this replaces one substring with another
921 * and returns a newly allocated string.
923 * @param string The string from which to replace stuff.
924 * @param delimiter The substring you want replaced.
925 * @param replacement The substring you want inserted in place
926 * of the delimiting substring.
928 * @return A new string, after performing the substitution.
929 * free this with g_free().
931 gchar *purple_strreplace(const char *string, const char *delimiter,
932 const char *replacement);
936 * Given a string, this replaces any utf-8 substrings in that string with
937 * the corresponding numerical character reference, and returns a newly
940 * @param in The string which might contain utf-8 substrings
942 * @return A new string, with utf-8 replaced with numerical character
943 * references, free this with g_free()
945 char *purple_utf8_ncr_encode(const char *in);
949 * Given a string, this replaces any numerical character references
950 * in that string with the corresponding actual utf-8 substrings,
951 * and returns a newly allocated string.
953 * @param in The string which might contain numerical character references.
955 * @return A new string, with numerical character references
956 * replaced with actual utf-8, free this with g_free().
958 char *purple_utf8_ncr_decode(const char *in);
962 * Given a string, this replaces one substring with another
963 * ignoring case and returns a newly allocated string.
965 * @param string The string from which to replace stuff.
966 * @param delimiter The substring you want replaced.
967 * @param replacement The substring you want inserted in place
968 * of the delimiting substring.
970 * @return A new string, after performing the substitution.
971 * free this with g_free().
973 gchar *purple_strcasereplace(const char *string, const char *delimiter,
974 const char *replacement);
977 * This is like strstr, except that it ignores ASCII case in
978 * searching for the substring.
980 * @param haystack The string to search in.
981 * @param needle The substring to find.
983 * @return the location of the substring if found, or NULL if not
985 const char *purple_strcasestr(const char *haystack, const char *needle);
988 * Returns a string representing a filesize in the appropriate
989 * units (MB, KB, GB, etc.)
991 * @param size The size
993 * @return The string in units form. This must be freed.
995 char *purple_str_size_to_units(size_t size);
998 * Converts seconds into a human-readable form.
1000 * @param sec The seconds.
1002 * @return A human-readable form, containing days, hours, minutes, and
1005 char *purple_str_seconds_to_string(guint sec);
1008 * Converts a binary string into a NUL terminated ascii string,
1009 * replacing nonascii characters and characters below SPACE (including
1010 * NUL) into \\xyy, where yy are two hex digits. Also backslashes are
1011 * changed into two backslashes (\\\\). The returned, newly allocated
1012 * string can be outputted to the console, and must be g_free()d.
1014 * @param binary A string of random data, possibly with embedded NULs
1016 * @param len The length in bytes of the input string. Must not be 0.
1018 * @return A newly allocated ASCIIZ string.
1020 char *purple_str_binary_to_ascii(const unsigned char *binary, guint len);
1024 /**************************************************************************/
1025 /** @name URI/URL Functions */
1026 /**************************************************************************/
1029 void purple_got_protocol_handler_uri(const char *uri);
1032 * Parses a URL, returning its host, port, file path, username and password.
1034 * The returned data must be freed.
1036 * @param url The URL to parse.
1037 * @param ret_host The returned host.
1038 * @param ret_port The returned port.
1039 * @param ret_path The returned path.
1040 * @param ret_user The returned username.
1041 * @param ret_passwd The returned password.
1043 gboolean purple_url_parse(const char *url, char **ret_host, int *ret_port,
1044 char **ret_path, char **ret_user, char **ret_passwd);
1047 * This is the signature used for functions that act as the callback
1048 * to purple_util_fetch_url() or purple_util_fetch_url_request().
1050 * @param url_data The same value that was returned when you called
1051 * purple_fetch_url() or purple_fetch_url_request().
1052 * @param user_data The user data that your code passed into either
1053 * purple_util_fetch_url() or purple_util_fetch_url_request().
1054 * @param url_text This will be NULL on error. Otherwise this
1055 * will contain the contents of the URL.
1056 * @param len 0 on error, otherwise this is the length of buf.
1057 * @param error_message If something went wrong then this will contain
1058 * a descriptive error message, and buf will be
1059 * NULL and len will be 0.
1061 typedef void (*PurpleUtilFetchUrlCallback)(PurpleUtilFetchUrlData *url_data, gpointer user_data, const gchar *url_text, gsize len, const gchar *error_message);
1064 * Fetches the data from a URL, and passes it to a callback function.
1066 * @param url The URL.
1067 * @param full TRUE if this is the full URL, or FALSE if it's a
1069 * @param user_agent The user agent field to use, or NULL.
1070 * @param http11 TRUE if HTTP/1.1 should be used to download the file.
1071 * @param cb The callback function.
1072 * @param data The user data to pass to the callback function.
1074 #define purple_util_fetch_url(url, full, user_agent, http11, cb, data) \
1075 purple_util_fetch_url_request(url, full, user_agent, http11, NULL, \
1079 * Fetches the data from a URL, and passes it to a callback function.
1081 * @param url The URL.
1082 * @param full TRUE if this is the full URL, or FALSE if it's a
1084 * @param user_agent The user agent field to use, or NULL.
1085 * @param http11 TRUE if HTTP/1.1 should be used to download the file.
1086 * @param max_len The maximum number of bytes to retrieve (-1 for unlimited)
1087 * @param cb The callback function.
1088 * @param data The user data to pass to the callback function.
1089 * @deprecated In 3.0.0, we'll rename this to "purple_util_fetch_url" and get rid of the old one
1091 #define purple_util_fetch_url_len(url, full, user_agent, http11, max_len, cb, data) \
1092 purple_util_fetch_url_request_len(url, full, user_agent, http11, NULL, \
1093 FALSE, max_len, cb, data);
1096 * Fetches the data from a URL, and passes it to a callback function.
1098 * @param url The URL.
1099 * @param full TRUE if this is the full URL, or FALSE if it's a
1101 * @param user_agent The user agent field to use, or NULL.
1102 * @param http11 TRUE if HTTP/1.1 should be used to download the file.
1103 * @param request A HTTP request to send to the server instead of the
1105 * @param include_headers
1106 * If TRUE, include the HTTP headers in the response.
1107 * @param callback The callback function.
1108 * @param data The user data to pass to the callback function.
1110 PurpleUtilFetchUrlData *purple_util_fetch_url_request(const gchar *url,
1111 gboolean full, const gchar *user_agent, gboolean http11,
1112 const gchar *request, gboolean include_headers,
1113 PurpleUtilFetchUrlCallback callback, gpointer data);
1116 * Fetches the data from a URL, and passes it to a callback function.
1118 * @param url The URL.
1119 * @param full TRUE if this is the full URL, or FALSE if it's a
1121 * @param user_agent The user agent field to use, or NULL.
1122 * @param http11 TRUE if HTTP/1.1 should be used to download the file.
1123 * @param request A HTTP request to send to the server instead of the
1125 * @param include_headers
1126 * If TRUE, include the HTTP headers in the response.
1127 * @param max_len The maximum number of bytes to retrieve (-1 for unlimited)
1128 * @param callback The callback function.
1129 * @param data The user data to pass to the callback function.
1130 * @deprecated In 3.0.0, this will go away.
1132 PurpleUtilFetchUrlData *purple_util_fetch_url_request_len(const gchar *url,
1133 gboolean full, const gchar *user_agent, gboolean http11,
1134 const gchar *request, gboolean include_headers, gssize max_len,
1135 PurpleUtilFetchUrlCallback callback, gpointer data);
1138 * Fetches the data from a URL, and passes it to a callback function.
1140 * @param account The account for which the request is needed, or NULL.
1141 * @param url The URL.
1142 * @param full TRUE if this is the full URL, or FALSE if it's a
1144 * @param user_agent The user agent field to use, or NULL.
1145 * @param http11 TRUE if HTTP/1.1 should be used to download the file.
1146 * @param request A HTTP request to send to the server instead of the
1148 * @param include_headers
1149 * If TRUE, include the HTTP headers in the response.
1150 * @param max_len The maximum number of bytes to retrieve (-1 for unlimited)
1151 * @param callback The callback function.
1152 * @param data The user data to pass to the callback function.
1153 * @deprecated In 3.0.0, we'll rename this to "purple_util_fetch_url_request" and get rid of the old one
1155 PurpleUtilFetchUrlData *purple_util_fetch_url_request_len_with_account(
1156 PurpleAccount *account, const gchar *url,
1157 gboolean full, const gchar *user_agent, gboolean http11,
1158 const gchar *request, gboolean include_headers, gssize max_len,
1159 PurpleUtilFetchUrlCallback callback, gpointer data);
1162 * Cancel a pending URL request started with either
1163 * purple_util_fetch_url_request() or purple_util_fetch_url().
1165 * @param url_data The data returned when you initiated the URL fetch.
1167 void purple_util_fetch_url_cancel(PurpleUtilFetchUrlData *url_data);
1170 * Decodes a URL into a plain string.
1172 * This will change hex codes and such to their ascii equivalents.
1174 * @param str The string to translate.
1176 * @return The resulting string.
1178 const char *purple_url_decode(const char *str);
1181 * Encodes a URL into an escaped string.
1183 * This will change non-alphanumeric characters to hex codes.
1185 * @param str The string to translate.
1187 * @return The resulting string.
1189 const char *purple_url_encode(const char *str);
1192 * Checks if the given email address is syntactically valid.
1194 * @param address The email address to validate.
1196 * @return True if the email address is syntactically correct.
1198 gboolean purple_email_is_valid(const char *address);
1201 * Checks if the given IP address is a syntactically valid IPv4 address.
1203 * @param ip The IP address to validate.
1205 * @return True if the IP address is syntactically correct.
1206 * @deprecated This function will be replaced with one that validates
1207 * as either IPv4 or IPv6 in 3.0.0. If you don't want this,
1208 * behavior, use one of the more specific functions.
1210 gboolean purple_ip_address_is_valid(const char *ip);
1213 * Checks if the given IP address is a syntactically valid IPv4 address.
1215 * @param ip The IP address to validate.
1217 * @return True if the IP address is syntactically correct.
1220 gboolean purple_ipv4_address_is_valid(const char *ip);
1223 * Checks if the given IP address is a syntactically valid IPv6 address.
1225 * @param ip The IP address to validate.
1227 * @return True if the IP address is syntactically correct.
1230 gboolean purple_ipv6_address_is_valid(const char *ip);
1233 * This function extracts a list of URIs from the a "text/uri-list"
1234 * string. It was "borrowed" from gnome_uri_list_extract_uris
1236 * @param uri_list An uri-list in the standard format.
1238 * @return A GList containing strings allocated with g_malloc
1239 * that have been splitted from uri-list.
1241 GList *purple_uri_list_extract_uris(const gchar *uri_list);
1244 * This function extracts a list of filenames from a
1245 * "text/uri-list" string. It was "borrowed" from
1246 * gnome_uri_list_extract_filenames
1248 * @param uri_list A uri-list in the standard format.
1250 * @return A GList containing strings allocated with g_malloc that
1251 * contain the filenames in the uri-list. Note that unlike
1252 * purple_uri_list_extract_uris() function, this will discard
1253 * any non-file uri from the result value.
1255 GList *purple_uri_list_extract_filenames(const gchar *uri_list);
1259 /**************************************************************************
1260 * UTF8 String Functions
1261 **************************************************************************/
1265 * Attempts to convert a string to UTF-8 from an unknown encoding.
1267 * This function checks the locale and tries sane defaults.
1269 * @param str The source string.
1271 * @return The UTF-8 string, or @c NULL if it could not be converted.
1273 gchar *purple_utf8_try_convert(const char *str);
1276 * Salvages the valid UTF-8 characters from a string, replacing any
1277 * invalid characters with a filler character (currently hardcoded to
1280 * @param str The source string.
1282 * @return A valid UTF-8 string.
1284 gchar *purple_utf8_salvage(const char *str);
1287 * Removes unprintable characters from a UTF-8 string. These characters
1288 * (in particular low-ASCII characters) are invalid in XML 1.0 and thus
1289 * are not allowed in XMPP and are rejected by libxml2 by default. This
1290 * function uses g_unichar_isprint to determine what characters should
1291 * be stripped. The returned string must be freed by the caller.
1293 * @param str A valid UTF-8 string.
1295 * @return A newly allocated UTF-8 string without the unprintable characters.
1298 * @see g_unichar_isprint
1300 gchar *purple_utf8_strip_unprintables(const gchar *str);
1303 * Return the UTF-8 version of gai_strerror(). It calls gai_strerror()
1304 * then converts the result to UTF-8. This function is analogous to
1307 * @param errnum The error code.
1309 * @return The UTF-8 error message.
1312 G_CONST_RETURN gchar *purple_gai_strerror(gint errnum);
1315 * Compares two UTF-8 strings case-insensitively. This comparison is
1316 * more expensive than a simple g_utf8_collate() comparison because
1317 * it calls g_utf8_casefold() on each string, which allocates new
1320 * @param a The first string.
1321 * @param b The second string.
1323 * @return -1 if @a is less than @a b.
1324 * 0 if @a is equal to @a b.
1325 * 1 if @a is greater than @a b.
1327 int purple_utf8_strcasecmp(const char *a, const char *b);
1330 * Case insensitive search for a word in a string. The needle string
1331 * must be contained in the haystack string and not be immediately
1332 * preceded or immediately followed by another alpha-numeric character.
1334 * @param haystack The string to search in.
1335 * @param needle The substring to find.
1337 * @return TRUE if haystack has the word, otherwise FALSE
1339 gboolean purple_utf8_has_word(const char *haystack, const char *needle);
1342 * Prints a UTF-8 message to the given file stream. The function
1343 * tries to convert the UTF-8 message to user's locale. If this
1344 * is not possible, the original UTF-8 text will be printed.
1346 * @param filestream The file stream (e.g. STDOUT or STDERR)
1347 * @param message The message to print.
1349 void purple_print_utf8_to_console(FILE *filestream, char *message);
1352 * Checks for messages starting (post-HTML) with "/me ", including the space.
1354 * @param message The message to check
1355 * @param len The message length, or -1
1357 * @return TRUE if it starts with "/me ", and it has been removed, otherwise
1360 gboolean purple_message_meify(char *message, gssize len);
1363 * Removes the underscore characters from a string used identify the mnemonic
1366 * @param in The string to strip
1368 * @return The stripped string
1370 char *purple_text_strip_mnemonic(const char *in);
1375 * Adds 8 to something.
1379 * @param x The number to add 8 to.
1383 #define purple_add_eight(x) ((x)+8)
1386 * Does the reverse of purple_escape_filename
1388 * This will change hex codes and such to their ascii equivalents.
1390 * @param str The string to translate.
1392 * @return The resulting string.
1394 const char *purple_unescape_filename(const char *str);
1397 * Escapes filesystem-unfriendly characters from a filename
1399 * @param str The string to translate.
1401 * @return The resulting string.
1403 const char *purple_escape_filename(const char *str);
1406 * This is added temporarily to assist the split of oscar into aim and icq.
1407 * This should not be used by plugins.
1409 const char *_purple_oscar_convert(const char *act, const char *protocol);
1412 * Restore default signal handlers for signals which might reasonably have
1413 * handlers. This should be called by a fork()'d child process, since child processes
1414 * inherit the handlers of the parent.
1416 void purple_restore_default_signal_handlers(void);
1419 * Gets the host name of the machine. If it not possible to determine the
1420 * host name, "localhost" is returned
1422 * @constreturn The hostname
1424 const gchar *purple_get_host_name(void);
1430 #endif /* _PURPLE_UTIL_H_ */