Remove useless comparison
[pidgin-git.git] / libpurple / util.h
blobede673e19e8ea57beba74868bee7da701d4440de
1 /**
2 * @file util.h Utility Functions
3 * @ingroup core
4 */
6 /* purple
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
27 * namespace.
29 #ifndef _PURPLE_UTIL_H_
30 #define _PURPLE_UTIL_H_
32 #include <stdio.h>
34 /**
35 * An opaque structure representing a URL request. Can be used to cancel
36 * the request.
38 typedef struct _PurpleUtilFetchUrlData PurpleUtilFetchUrlData;
39 /** @copydoc _PurpleMenuAction */
40 typedef struct _PurpleMenuAction PurpleMenuAction;
41 /** @copydoc _PurpleKeyValuePair */
42 typedef struct _PurpleKeyValuePair PurpleKeyValuePair;
44 #include "account.h"
45 #include "signals.h"
46 #include "xmlnode.h"
47 #include "notify.h"
50 #ifdef __cplusplus
51 extern "C" {
52 #endif
54 struct _PurpleMenuAction
56 char *label;
57 PurpleCallback callback;
58 gpointer data;
59 GList *children;
62 typedef char *(*PurpleInfoFieldFormatCallback)(const char *field, size_t len);
64 /**
65 * A key-value pair.
67 * This is used by, among other things, purple_gtk_combo* functions to pass in a
68 * list of key-value pairs so it can display a user-friendly value.
70 struct _PurpleKeyValuePair
72 gchar *key;
73 void *value;
77 /**
78 * Creates a new PurpleMenuAction.
80 * @param label The text label to display for this action.
81 * @param callback The function to be called when the action is used on
82 * the selected item.
83 * @param data Additional data to be passed to the callback.
84 * @param children A GList of PurpleMenuActions to be added as a submenu
85 * of the action.
86 * @return The PurpleMenuAction.
88 PurpleMenuAction *purple_menu_action_new(const char *label, PurpleCallback callback,
89 gpointer data, GList *children);
91 /**
92 * Frees a PurpleMenuAction
94 * @param act The PurpleMenuAction to free.
96 void purple_menu_action_free(PurpleMenuAction *act);
98 /**
99 * Set the appropriate presence values for the currently playing song.
101 * @param title The title of the song, @c NULL to unset the value.
102 * @param artist The artist of the song, can be @c NULL.
103 * @param album The album of the song, can be @c NULL.
104 * @since 2.4.0
106 void purple_util_set_current_song(const char *title, const char *artist,
107 const char *album);
110 * Format song information.
112 * @param title The title of the song, @c NULL to unset the value.
113 * @param artist The artist of the song, can be @c NULL.
114 * @param album The album of the song, can be @c NULL.
115 * @param unused Currently unused, must be @c NULL.
117 * @return The formatted string. The caller must g_free the returned string.
118 * @since 2.4.0
120 char * purple_util_format_song_info(const char *title, const char *artist,
121 const char *album, gpointer unused);
123 /**************************************************************************/
124 /** @name Utility Subsystem */
125 /**************************************************************************/
126 /*@{*/
129 * Initializes the utility subsystem.
131 * @since 2.3.0
133 void purple_util_init(void);
136 * Uninitializes the util subsystem.
138 * @since 2.3.0
140 void purple_util_uninit(void);
142 /*@}*/
144 /**************************************************************************/
145 /** @name Base16 Functions */
146 /**************************************************************************/
147 /*@{*/
150 * Converts a chunk of binary data to its base-16 equivalent.
152 * @param data The data to convert.
153 * @param len The length of the data.
155 * @return The base-16 string in the ASCII encoding. Must be
156 * g_free'd when no longer needed.
158 * @see purple_base16_decode()
160 gchar *purple_base16_encode(const guchar *data, gsize len);
163 * Converts an ASCII string of base-16 encoded data to
164 * the binary equivalent.
166 * @param str The base-16 string to convert to raw data.
167 * @param ret_len The length of the returned data. You can
168 * pass in NULL if you're sure that you know
169 * the length of the decoded data, or if you
170 * know you'll be able to use strlen to
171 * determine the length, etc.
173 * @return The raw data. Must be g_free'd when no longer needed.
175 * @see purple_base16_encode()
177 guchar *purple_base16_decode(const char *str, gsize *ret_len);
180 * Converts a chunk of binary data to a chunked base-16 representation
181 * (handy for key fingerprints)
183 * Example output: 01:23:45:67:89:AB:CD:EF
185 * @param data The data to convert.
186 * @param len The length of the data.
188 * @return The base-16 string in the ASCII chunked encoding. Must be
189 * g_free'd when no longer needed.
191 gchar *purple_base16_encode_chunked(const guchar *data, gsize len);
194 /*@}*/
196 /**************************************************************************/
197 /** @name Base64 Functions */
198 /**************************************************************************/
199 /*@{*/
202 * Converts a chunk of binary data to its base-64 equivalent.
204 * @param data The data to convert.
205 * @param len The length of the data.
207 * @return The base-64 string in the ASCII encoding. Must be
208 * g_free'd when no longer needed.
210 * @see purple_base64_decode()
212 gchar *purple_base64_encode(const guchar *data, gsize len);
215 * Converts an ASCII string of base-64 encoded data to
216 * the binary equivalent.
218 * @param str The base-64 string to convert to raw data.
219 * @param ret_len The length of the returned data. You can
220 * pass in NULL if you're sure that you know
221 * the length of the decoded data, or if you
222 * know you'll be able to use strlen to
223 * determine the length, etc.
225 * @return The raw data. Must be g_free'd when no longer needed.
227 * @see purple_base64_encode()
229 guchar *purple_base64_decode(const char *str, gsize *ret_len);
231 /*@}*/
233 /**************************************************************************/
234 /** @name Quoted Printable Functions */
235 /**************************************************************************/
236 /*@{*/
239 * Converts a quoted printable string back to its readable equivalent.
240 * What is a quoted printable string, you ask? It's an encoding used
241 * to transmit binary data as ASCII. It's intended purpose is to send
242 * emails containing non-ASCII characters. Wikipedia has a pretty good
243 * explanation. Also see RFC 2045.
245 * @param str The quoted printable ASCII string to convert to raw data.
246 * @param ret_len The length of the returned data.
248 * @return The readable string. Must be g_free'd when no longer needed.
250 guchar *purple_quotedp_decode(const char *str, gsize *ret_len);
252 /*@}*/
254 /**************************************************************************/
255 /** @name MIME Functions */
256 /**************************************************************************/
257 /*@{*/
260 * Converts a MIME header field string back to its readable equivalent
261 * according to RFC 2047. Basically, a header is plain ASCII and can
262 * contain any number of sections called "encoded-words." The format
263 * of an encoded word is =?ISO-8859-1?Q?Keld_J=F8rn_Simonsen?=
264 * =? designates the beginning of the encoded-word
265 * ?= designates the end of the encoded-word
267 * An encoded word is segmented into three pieces by the use of a
268 * question mark. The first piece is the character set, the second
269 * piece is the encoding, and the third piece is the encoded text.
271 * @param str The ASCII string, possibly containing any number of
272 * encoded-word sections.
274 * @return The string, with any encoded-word sections decoded and
275 * converted to UTF-8. Must be g_free'd when no longer
276 * needed.
278 char *purple_mime_decode_field(const char *str);
280 /*@}*/
283 /**************************************************************************/
284 /** @name Date/Time Functions */
285 /**************************************************************************/
286 /*@{*/
289 * Formats a time into the specified format.
291 * This is essentially strftime(), but it has a static buffer
292 * and handles the UTF-8 conversion for the caller.
294 * This function also provides the GNU %z formatter if the underlying C
295 * library doesn't. However, the format string parser is very naive, which
296 * means that conversions specifiers to %z cannot be guaranteed. The GNU
297 * strftime(3) man page describes %z as: 'The time-zone as hour offset from
298 * GMT. Required to emit RFC822-conformant dates
299 * (using "%a, %d %b %Y %H:%M:%S %z"). (GNU)'
301 * On Windows, this function also converts the results for %Z from a timezone
302 * name (as returned by the system strftime() %Z format string) to a timezone
303 * abbreviation (as is the case on Unix). As with %z, conversion specifiers
304 * should not be used.
306 * @param format The format string, in UTF-8
307 * @param tm The time to format, or @c NULL to use the current local time
309 * @return The formatted time, in UTF-8.
311 * @note @a format is required to be in UTF-8. This differs from strftime(),
312 * where the format is provided in the locale charset.
314 const char *purple_utf8_strftime(const char *format, const struct tm *tm);
317 * Gets a string representation of the local timezone offset
319 * @param tm The time to get the timezone for
320 * @param iso TRUE to format the offset according to ISO-8601, FALSE to
321 * not substitute 'Z' for 0 offset, and to not separate
322 * hours and minutes with a colon.
324 const char *purple_get_tzoff_str(const struct tm *tm, gboolean iso);
327 * Formats a time into the user's preferred short date format.
329 * The returned string is stored in a static buffer, so the result
330 * should be g_strdup()'d if it's going to be kept.
332 * @param tm The time to format, or @c NULL to use the current local time
334 * @return The date, formatted as per the user's settings.
336 const char *purple_date_format_short(const struct tm *tm);
339 * Formats a time into the user's preferred short date plus time format.
341 * The returned string is stored in a static buffer, so the result
342 * should be g_strdup()'d if it's going to be kept.
344 * @param tm The time to format, or @c NULL to use the current local time
346 * @return The timestamp, formatted as per the user's settings.
348 const char *purple_date_format_long(const struct tm *tm);
351 * Formats a time into the user's preferred full date and time format.
353 * The returned string is stored in a static buffer, so the result
354 * should be g_strdup()'d if it's going to be kept.
356 * @param tm The time to format, or @c NULL to use the current local time
358 * @return The date and time, formatted as per the user's settings.
360 const char *purple_date_format_full(const struct tm *tm);
363 * Formats a time into the user's preferred time format.
365 * The returned string is stored in a static buffer, so the result
366 * should be g_strdup()'d if it's going to be kept.
368 * @param tm The time to format, or @c NULL to use the current local time
370 * @return The time, formatted as per the user's settings.
372 const char *purple_time_format(const struct tm *tm);
375 * Builds a time_t from the supplied information.
377 * @param year The year.
378 * @param month The month.
379 * @param day The day.
380 * @param hour The hour.
381 * @param min The minute.
382 * @param sec The second.
384 * @return A time_t.
386 time_t purple_time_build(int year, int month, int day, int hour,
387 int min, int sec);
389 /** Used by purple_str_to_time to indicate no timezone offset was
390 * specified in the timestamp string. */
391 #define PURPLE_NO_TZ_OFF -500000
394 * Parses a timestamp in jabber, ISO8601, or MM/DD/YYYY format and returns
395 * a time_t.
397 * @param timestamp The timestamp
398 * @param utc Assume UTC if no timezone specified
399 * @param tm If not @c NULL, the caller can get a copy of the
400 * struct tm used to calculate the time_t return value.
401 * @param tz_off If not @c NULL, the caller can get a copy of the
402 * timezone offset (from UTC) used to calculate the time_t
403 * return value. Note: Zero is a valid offset. As such,
404 * the value of the macro @c PURPLE_NO_TZ_OFF indicates no
405 * offset was specified (which means that the local
406 * timezone was used in the calculation).
407 * @param rest If not @c NULL, the caller can get a pointer to the
408 * part of @a timestamp left over after parsing is
409 * completed, if it's not the end of @a timestamp.
411 * @return A time_t.
413 time_t purple_str_to_time(const char *timestamp, gboolean utc,
414 struct tm *tm, long *tz_off, const char **rest);
416 /*@}*/
419 /**************************************************************************/
420 /** @name Markup Functions */
421 /**************************************************************************/
422 /*@{*/
425 * Escapes special characters in a plain-text string so they display
426 * correctly as HTML. For example, & is replaced with &amp; and < is
427 * replaced with &lt;
429 * This is exactly the same as g_markup_escape_text(), except that it
430 * does not change ' to &apos; because &apos; is not a valid HTML 4 entity,
431 * and is displayed literally in IE7.
433 * @since 2.6.0
435 gchar *purple_markup_escape_text(const gchar *text, gssize length);
438 * Finds an HTML tag matching the given name.
440 * This locates an HTML tag's start and end, and stores its attributes
441 * in a GData hash table. The names of the attributes are lower-cased
442 * in the hash table, and the name of the tag is case insensitive.
444 * @param needle The name of the tag
445 * @param haystack The null-delimited string to search in
446 * @param start A pointer to the start of the tag if found
447 * @param end A pointer to the end of the tag if found
448 * @param attributes The attributes, if the tag was found. This should
449 * be freed with g_datalist_clear().
450 * @return TRUE if the tag was found
452 gboolean purple_markup_find_tag(const char *needle, const char *haystack,
453 const char **start, const char **end,
454 GData **attributes);
457 * Extracts a field of data from HTML.
459 * This is a scary function. It used to be used for MSN and Yahoo prpls,
460 * but since those prpls have been removed, this is now deprecated.
462 * @param str The string to parse.
463 * @param len The size of str.
464 * @param user_info The destination PurpleNotifyUserInfo to which the new
465 * field info should be added.
466 * @param start_token The beginning token.
467 * @param skip The number of characters to skip after the
468 * start token.
469 * @param end_token The ending token.
470 * @param check_value The value that the last character must meet.
471 * @param no_value_token The token indicating no value is given.
472 * @param display_name The short descriptive name to display for this token.
473 * @param is_link TRUE if this should be a link, or FALSE otherwise.
474 * @param link_prefix The prefix for the link.
475 * @param format_cb A callback to format the value before adding it.
477 * @return TRUE if successful, or FALSE otherwise.
479 gboolean purple_markup_extract_info_field(const char *str, int len, PurpleNotifyUserInfo *user_info,
480 const char *start_token, int skip,
481 const char *end_token, char check_value,
482 const char *no_value_token,
483 const char *display_name, gboolean is_link,
484 const char *link_prefix,
485 PurpleInfoFieldFormatCallback format_cb);
488 * Converts HTML markup to XHTML.
490 * @param html The HTML markup.
491 * @param dest_xhtml The destination XHTML output.
492 * @param dest_plain The destination plain-text output.
494 void purple_markup_html_to_xhtml(const char *html, char **dest_xhtml,
495 char **dest_plain);
498 * Strips HTML tags from a string.
500 * @param str The string to strip HTML from.
502 * @return The new string without HTML. You must g_free this string
503 * when finished with it.
505 char *purple_markup_strip_html(const char *str);
508 * Adds the necessary HTML code to turn URIs into HTML links in a string.
510 * @param str The string to linkify.
512 * @return The new string with all URIs surrounded in standard
513 * HTML <a href="whatever"></a> tags. You must g_free this
514 * string when finished with it.
516 char *purple_markup_linkify(const char *str);
519 * Unescapes HTML entities to their literal characters in the text.
520 * For example "&amp;" is replaced by '&' and so on. Also converts
521 * numerical entities (e.g. "&#38;" is also '&').
523 * This function currently supports the following named entities:
524 * "&amp;", "&lt;", "&gt;", "&copy;", "&quot;", "&reg;", "&apos;"
526 * purple_unescape_html() is similar, but also converts "<br>" into "\n".
528 * @param text The string in which to unescape any HTML entities
530 * @return The text with HTML entities literalized. You must g_free
531 * this string when finished with it.
533 * @see purple_unescape_html()
534 * @since 2.7.0
536 char *purple_unescape_text(const char *text);
539 * Unescapes HTML entities to their literal characters and converts
540 * "<br>" to "\n". See purple_unescape_text() for more details.
542 * @param html The string in which to unescape any HTML entities
544 * @return The text with HTML entities literalized. You must g_free
545 * this string when finished with it.
547 * @see purple_unescape_text()
549 char *purple_unescape_html(const char *html);
552 * Returns a newly allocated substring of the HTML UTF-8 string "str".
553 * The markup is preserved such that the substring will have the same
554 * formatting as original string, even though some tags may have been
555 * opened before "x", or may close after "y". All open tags are closed
556 * at the end of the returned string, in the proper order.
558 * Note that x and y are in character offsets, not byte offsets, and
559 * are offsets into an unformatted version of str. Because of this,
560 * this function may be sensitive to changes in GtkIMHtml and may break
561 * when used with other UI's. libpurple users are encouraged to report and
562 * work out any problems encountered.
564 * @param str The input NUL terminated, HTML, UTF-8 (or ASCII) string.
565 * @param x The character offset into an unformatted version of str to
566 * begin at.
567 * @param y The character offset (into an unformatted vesion of str) of
568 * one past the last character to include in the slice.
570 * @return The HTML slice of string, with all formatting retained.
572 char *purple_markup_slice(const char *str, guint x, guint y);
575 * Returns a newly allocated string containing the name of the tag
576 * located at "tag". Tag is expected to point to a '<', and contain
577 * a '>' sometime after that. If there is no '>' and the string is
578 * not NUL terminated, this function can be expected to segfault.
580 * @param tag The string starting a HTML tag.
581 * @return A string containing the name of the tag.
583 char *purple_markup_get_tag_name(const char *tag);
586 * Returns a constant string of the character representation of the HTML
587 * entity pointed to by @a text. For example, purple_markup_unescape_entity("&amp;")
588 * will return "&". The @a text variable is expected to point to an '&',
589 * the first character of the entity. If given an unrecognized entity, the function
590 * returns @c NULL.
592 * Note that this function, unlike purple_unescape_html(), does not search
593 * the string for the entity, does not replace the entity, and does not
594 * return a newly allocated string.
596 * @param text A string containing an HTML entity.
597 * @param length If not @c NULL, the string length of the entity is stored in this location.
599 * @return A constant string containing the character representation of the given entity.
601 const char * purple_markup_unescape_entity(const char *text, int *length);
604 * Returns a newly allocated string containing the value of the CSS property specified
605 * in opt. The @a style argument is expected to point to a HTML inline CSS.
606 * The function will seek for the CSS property and return its value.
608 * For example, purple_markup_get_css_property("direction:rtl;color:#dc4d1b;",
609 * "color") would return "#dc4d1b".
611 * On error or if the requested property was not found, the function returns
612 * @c NULL.
614 * @param style A string containing the inline CSS text.
615 * @param opt The requested CSS property.
617 * @return The value of the requested CSS property.
619 char * purple_markup_get_css_property(const gchar *style, const gchar *opt);
622 * Check if the given HTML contains RTL text.
624 * @param html The HTML text.
626 * @return TRUE if the text contains RTL text, FALSE otherwise.
628 * @since 2.6.0
630 gboolean purple_markup_is_rtl(const char *html);
632 /*@}*/
635 /**************************************************************************/
636 /** @name Path/Filename Functions */
637 /**************************************************************************/
638 /*@{*/
641 * Returns the user's home directory.
643 * @return The user's home directory.
645 * @see purple_user_dir()
647 const gchar *purple_home_dir(void);
650 * Returns the purple settings directory in the user's home directory.
651 * This is usually ~/.purple
653 * @return The purple settings directory.
655 * @see purple_home_dir()
657 const char *purple_user_dir(void);
660 * Define a custom purple settings directory, overriding the default (user's home directory/.purple)
661 * @param dir The custom settings directory
663 void purple_util_set_user_dir(const char *dir);
666 * Builds a complete path from the root, making any directories along
667 * the path which do not already exist.
669 * @param path The path you wish to create. Note that it must start
670 * from the root or this function will fail.
671 * @param mode Unix-style permissions for this directory.
673 * @return 0 for success, nonzero on any error.
675 int purple_build_dir(const char *path, int mode);
678 * Write a string of data to a file of the given name in the Purple
679 * user directory ($HOME/.purple by default). The data is typically
680 * a serialized version of one of Purple's config files, such as
681 * prefs.xml, accounts.xml, etc. And the string is typically
682 * obtained using xmlnode_to_formatted_str. However, this function
683 * should work fine for saving binary files as well.
685 * @param filename The basename of the file to write in the purple_user_dir.
686 * @param data A null-terminated string of data to write.
687 * @param size The size of the data to save. If data is
688 * null-terminated you can pass in -1.
690 * @return TRUE if the file was written successfully. FALSE otherwise.
692 gboolean purple_util_write_data_to_file(const char *filename, const char *data,
693 gssize size);
696 * Write data to a file using the absolute path.
698 * This exists for Glib backwards compatibility reasons.
700 * @param filename_full Filename to write to
701 * @param data A null-terminated string of data to write.
702 * @param size The size of the data to save. If data is
703 * null-terminated you can pass in -1.
705 * @return TRUE if the file was written successfully. FALSE otherwise.
707 * @todo Remove this function (use g_file_set_contents instead) when 3.0.0
708 * rolls around.
709 * @see purple_util_write_data_to_file()
712 gboolean
713 purple_util_write_data_to_file_absolute(const char *filename_full, const char *data, gssize size);
716 * Read the contents of a given file and parse the results into an
717 * xmlnode tree structure. This is intended to be used to read
718 * Purple's configuration xml files (prefs.xml, pounces.xml, etc.)
720 * @param filename The basename of the file to open in the purple_user_dir.
721 * @param description A very short description of the contents of this
722 * file. This is used in error messages shown to the
723 * user when the file can not be opened. For example,
724 * "preferences," or "buddy pounces."
726 * @return An xmlnode tree of the contents of the given file. Or NULL, if
727 * the file does not exist or there was an error reading the file.
729 xmlnode *purple_util_read_xml_from_file(const char *filename,
730 const char *description);
733 * Creates a temporary file and returns a file pointer to it.
735 * This is like mkstemp(), but returns a file pointer and uses a
736 * pre-set template. It uses the semantics of tempnam() for the
737 * directory to use and allocates the space for the file path.
739 * The caller is responsible for closing the file and removing it when
740 * done, as well as freeing the space pointed to by @a path with
741 * g_free().
743 * @param path The returned path to the temp file.
744 * @param binary Text or binary, for platforms where it matters.
746 * @return A file pointer to the temporary file, or @c NULL on failure.
748 FILE *purple_mkstemp(char **path, gboolean binary);
751 * Returns an extension corresponding to the image data's file type.
753 * @param data A pointer to the image data
754 * @param len The length of the image data
756 * @return The appropriate extension, or "icon" if unknown.
758 const char *
759 purple_util_get_image_extension(gconstpointer data, size_t len);
762 * Returns a SHA-1 hash string of the data passed in.
764 char *purple_util_get_image_checksum(gconstpointer image_data, size_t image_len);
767 * @return A hex encoded version of the SHA-1 hash of the data passed
768 * in with the correct file extention appended. The file
769 * extension is determined by calling
770 * purple_util_get_image_extension(). This return value must
771 * be g_freed by the caller.
773 char *purple_util_get_image_filename(gconstpointer image_data, size_t image_len);
775 /*@}*/
778 /**************************************************************************/
779 /** @name Environment Detection Functions */
780 /**************************************************************************/
781 /*@{*/
784 * Checks if the given program name is valid and executable.
786 * @param program The file name of the application.
788 * @return TRUE if the program is runable.
790 gboolean purple_program_is_valid(const char *program);
793 * Check if running GNOME.
795 * @return TRUE if running GNOME, FALSE otherwise.
797 gboolean purple_running_gnome(void);
800 * Check if running KDE.
802 * @return TRUE if running KDE, FALSE otherwise.
804 gboolean purple_running_kde(void);
807 * Check if running OS X.
809 * @return TRUE if running OS X, FALSE otherwise.
811 gboolean purple_running_osx(void);
814 * Returns the IP address from a socket file descriptor.
816 * @param fd The socket file descriptor.
818 * @return The IP address, or @c NULL on error.
820 char *purple_fd_get_ip(int fd);
823 * Returns the address family of a socket.
825 * @param fd The socket file descriptor.
827 * @return The address family of the socket (AF_INET, AF_INET6, etc) or -1
828 * on error.
829 * @since 2.7.0
831 int purple_socket_get_family(int fd);
834 * Returns TRUE if a socket is capable of speaking IPv4.
836 * This is the case for IPv4 sockets and, on some systems, IPv6 sockets
837 * (due to the IPv4-mapped address functionality).
839 * @param fd The socket file descriptor
840 * @return TRUE if a socket can speak IPv4.
841 * @since 2.7.0
843 gboolean purple_socket_speaks_ipv4(int fd);
845 /*@}*/
848 /**************************************************************************/
849 /** @name String Functions */
850 /**************************************************************************/
851 /*@{*/
854 * Tests two strings for equality.
856 * Unlike strcmp(), this function will not crash if one or both of the
857 * strings are @c NULL.
859 * @param left A string
860 * @param right A string to compare with left
862 * @return @c TRUE if the strings are the same, else @c FALSE.
864 * @since 2.6.0
866 gboolean purple_strequal(const gchar *left, const gchar *right);
869 * Normalizes a string, so that it is suitable for comparison.
871 * The returned string will point to a static buffer, so if the
872 * string is intended to be kept long-term, you <i>must</i>
873 * g_strdup() it. Also, calling normalize() twice in the same line
874 * will lead to problems.
876 * @param account The account the string belongs to, or NULL if you do
877 * not know the account. If you use NULL, the string
878 * will still be normalized, but if the PRPL uses a
879 * custom normalization function then the string may
880 * not be normalized correctly.
881 * @param str The string to normalize.
883 * @return A pointer to the normalized version stored in a static buffer.
885 const char *purple_normalize(const PurpleAccount *account, const char *str);
888 * Normalizes a string, so that it is suitable for comparison.
890 * This is one possible implementation for the PRPL callback
891 * function "normalize." It returns a lowercase and UTF-8
892 * normalized version of the string.
894 * @param account The account the string belongs to.
895 * @param str The string to normalize.
897 * @return A pointer to the normalized version stored in a static buffer.
899 const char *purple_normalize_nocase(const PurpleAccount *account, const char *str);
902 * Compares two strings to see if the first contains the second as
903 * a proper prefix.
905 * @param s The string to check.
906 * @param p The prefix in question.
908 * @return TRUE if p is a prefix of s, otherwise FALSE.
910 gboolean purple_str_has_prefix(const char *s, const char *p);
913 * Compares two strings to see if the second is a proper suffix
914 * of the first.
916 * @param s The string to check.
917 * @param x The suffix in question.
919 * @return TRUE if x is a a suffix of s, otherwise FALSE.
921 gboolean purple_str_has_suffix(const char *s, const char *x);
924 * Duplicates a string and replaces all newline characters from the
925 * source string with HTML linebreaks.
927 * @param src The source string.
929 * @return The new string. Must be g_free'd by the caller.
931 gchar *purple_strdup_withhtml(const gchar *src);
934 * Ensures that all linefeeds have a matching carriage return.
936 * @param str The source string.
938 * @return The string with carriage returns.
940 char *purple_str_add_cr(const char *str);
943 * Strips all instances of the given character from the
944 * given string. The string is modified in place. This
945 * is useful for stripping new line characters, for example.
947 * Example usage:
948 * purple_str_strip_char(my_dumb_string, '\n');
950 * @param str The string to strip characters from.
951 * @param thechar The character to strip from the given string.
953 void purple_str_strip_char(char *str, char thechar);
956 * Given a string, this replaces all instances of one character
957 * with another. This happens inline (the original string IS
958 * modified).
960 * @param string The string from which to replace stuff.
961 * @param delimiter The character you want replaced.
962 * @param replacement The character you want inserted in place
963 * of the delimiting character.
965 void purple_util_chrreplace(char *string, char delimiter,
966 char replacement);
969 * Given a string, this replaces one substring with another
970 * and returns a newly allocated string.
972 * @param string The string from which to replace stuff.
973 * @param delimiter The substring you want replaced.
974 * @param replacement The substring you want inserted in place
975 * of the delimiting substring.
977 * @return A new string, after performing the substitution.
978 * free this with g_free().
980 gchar *purple_strreplace(const char *string, const char *delimiter,
981 const char *replacement);
985 * Given a string, this replaces any utf-8 substrings in that string with
986 * the corresponding numerical character reference, and returns a newly
987 * allocated string.
989 * @param in The string which might contain utf-8 substrings
991 * @return A new string, with utf-8 replaced with numerical character
992 * references, free this with g_free()
994 char *purple_utf8_ncr_encode(const char *in);
998 * Given a string, this replaces any numerical character references
999 * in that string with the corresponding actual utf-8 substrings,
1000 * and returns a newly allocated string.
1002 * @param in The string which might contain numerical character references.
1004 * @return A new string, with numerical character references
1005 * replaced with actual utf-8, free this with g_free().
1007 char *purple_utf8_ncr_decode(const char *in);
1011 * Given a string, this replaces one substring with another
1012 * ignoring case and returns a newly allocated string.
1014 * @param string The string from which to replace stuff.
1015 * @param delimiter The substring you want replaced.
1016 * @param replacement The substring you want inserted in place
1017 * of the delimiting substring.
1019 * @return A new string, after performing the substitution.
1020 * free this with g_free().
1022 gchar *purple_strcasereplace(const char *string, const char *delimiter,
1023 const char *replacement);
1026 * This is like strstr, except that it ignores ASCII case in
1027 * searching for the substring.
1029 * @param haystack The string to search in.
1030 * @param needle The substring to find.
1032 * @return the location of the substring if found, or NULL if not
1034 const char *purple_strcasestr(const char *haystack, const char *needle);
1037 * Returns a string representing a filesize in the appropriate
1038 * units (MB, KB, GB, etc.)
1040 * @param size The size
1042 * @return The string in units form. This must be freed.
1044 char *purple_str_size_to_units(size_t size);
1047 * Converts seconds into a human-readable form.
1049 * @param sec The seconds.
1051 * @return A human-readable form, containing days, hours, minutes, and
1052 * seconds.
1054 char *purple_str_seconds_to_string(guint sec);
1057 * Converts a binary string into a NUL terminated ascii string,
1058 * replacing nonascii characters and characters below SPACE (including
1059 * NUL) into \\xyy, where yy are two hex digits. Also backslashes are
1060 * changed into two backslashes (\\\\). The returned, newly allocated
1061 * string can be outputted to the console, and must be g_free()d.
1063 * @param binary A string of random data, possibly with embedded NULs
1064 * and such.
1065 * @param len The length in bytes of the input string. Must not be 0.
1067 * @return A newly allocated ASCIIZ string.
1069 char *purple_str_binary_to_ascii(const unsigned char *binary, guint len);
1070 /*@}*/
1073 /**************************************************************************/
1074 /** @name URI/URL Functions */
1075 /**************************************************************************/
1076 /*@{*/
1078 void purple_got_protocol_handler_uri(const char *uri);
1081 * Parses a URL, returning its host, port, file path, username and password.
1083 * The returned data must be freed.
1085 * @param url The URL to parse.
1086 * @param ret_host The returned host.
1087 * @param ret_port The returned port.
1088 * @param ret_path The returned path.
1089 * @param ret_user The returned username.
1090 * @param ret_passwd The returned password.
1092 gboolean purple_url_parse(const char *url, char **ret_host, int *ret_port,
1093 char **ret_path, char **ret_user, char **ret_passwd);
1096 * This is the signature used for functions that act as the callback
1097 * to purple_util_fetch_url() or purple_util_fetch_url_request().
1099 * @param url_data The same value that was returned when you called
1100 * purple_fetch_url() or purple_fetch_url_request().
1101 * @param user_data The user data that your code passed into either
1102 * purple_util_fetch_url() or purple_util_fetch_url_request().
1103 * @param url_text This will be NULL on error. Otherwise this
1104 * will contain the contents of the URL.
1105 * @param len 0 on error, otherwise this is the length of buf.
1106 * @param error_message If something went wrong then this will contain
1107 * a descriptive error message, and buf will be
1108 * NULL and len will be 0.
1110 typedef void (*PurpleUtilFetchUrlCallback)(PurpleUtilFetchUrlData *url_data, gpointer user_data, const gchar *url_text, gsize len, const gchar *error_message);
1113 * Fetches the data from a URL, and passes it to a callback function.
1115 * @param url The URL.
1116 * @param full TRUE if this is the full URL, or FALSE if it's a
1117 * partial URL.
1118 * @param user_agent The user agent field to use, or NULL.
1119 * @param http11 TRUE if HTTP/1.1 should be used to download the file.
1120 * @param cb The callback function.
1121 * @param data The user data to pass to the callback function.
1123 #define purple_util_fetch_url(url, full, user_agent, http11, cb, data) \
1124 purple_util_fetch_url_request(url, full, user_agent, http11, NULL, \
1125 FALSE, cb, data);
1128 * Fetches the data from a URL, and passes it to a callback function.
1130 * @param url The URL.
1131 * @param full TRUE if this is the full URL, or FALSE if it's a
1132 * partial URL.
1133 * @param user_agent The user agent field to use, or NULL.
1134 * @param http11 TRUE if HTTP/1.1 should be used to download the file.
1135 * @param max_len The maximum number of bytes to retrieve (-1 for unlimited)
1136 * @param cb The callback function.
1137 * @param data The user data to pass to the callback function.
1138 * @deprecated In 3.0.0, we'll rename this to "purple_util_fetch_url" and get rid of the old one
1140 #define purple_util_fetch_url_len(url, full, user_agent, http11, max_len, cb, data) \
1141 purple_util_fetch_url_request_len(url, full, user_agent, http11, NULL, \
1142 FALSE, max_len, cb, data);
1145 * Fetches the data from a URL, and passes it to a callback function.
1147 * @param url The URL.
1148 * @param full TRUE if this is the full URL, or FALSE if it's a
1149 * partial URL.
1150 * @param user_agent The user agent field to use, or NULL.
1151 * @param http11 TRUE if HTTP/1.1 should be used to download the file.
1152 * @param request A HTTP request to send to the server instead of the
1153 * standard GET
1154 * @param include_headers
1155 * If TRUE, include the HTTP headers in the response.
1156 * @param callback The callback function.
1157 * @param data The user data to pass to the callback function.
1159 PurpleUtilFetchUrlData *purple_util_fetch_url_request(const gchar *url,
1160 gboolean full, const gchar *user_agent, gboolean http11,
1161 const gchar *request, gboolean include_headers,
1162 PurpleUtilFetchUrlCallback callback, gpointer data);
1165 * Fetches the data from a URL, and passes it to a callback function.
1167 * @param url The URL.
1168 * @param full TRUE if this is the full URL, or FALSE if it's a
1169 * partial URL.
1170 * @param user_agent The user agent field to use, or NULL.
1171 * @param http11 TRUE if HTTP/1.1 should be used to download the file.
1172 * @param request A HTTP request to send to the server instead of the
1173 * standard GET
1174 * @param include_headers
1175 * If TRUE, include the HTTP headers in the response.
1176 * @param max_len The maximum number of bytes to retrieve (-1 for unlimited)
1177 * @param callback The callback function.
1178 * @param data The user data to pass to the callback function.
1179 * @deprecated In 3.0.0, this will go away.
1181 PurpleUtilFetchUrlData *purple_util_fetch_url_request_len(const gchar *url,
1182 gboolean full, const gchar *user_agent, gboolean http11,
1183 const gchar *request, gboolean include_headers, gssize max_len,
1184 PurpleUtilFetchUrlCallback callback, gpointer data);
1187 * Fetches the data from a URL, and passes it to a callback function.
1189 * @param account The account for which the request is needed, or NULL.
1190 * @param url The URL.
1191 * @param full TRUE if this is the full URL, or FALSE if it's a
1192 * partial URL.
1193 * @param user_agent The user agent field to use, or NULL.
1194 * @param http11 TRUE if HTTP/1.1 should be used to download the file.
1195 * @param request A HTTP request to send to the server instead of the
1196 * standard GET
1197 * @param include_headers
1198 * If TRUE, include the HTTP headers in the response.
1199 * @param max_len The maximum number of bytes to retrieve, or a negative
1200 * number to use the default max of 512 KiB.
1201 * @param callback The callback function.
1202 * @param data The user data to pass to the callback function.
1203 * @deprecated In 3.0.0, we'll rename this to "purple_util_fetch_url_request" and get rid of the old one
1205 PurpleUtilFetchUrlData *purple_util_fetch_url_request_len_with_account(
1206 PurpleAccount *account, const gchar *url,
1207 gboolean full, const gchar *user_agent, gboolean http11,
1208 const gchar *request, gboolean include_headers, gssize max_len,
1209 PurpleUtilFetchUrlCallback callback, gpointer data);
1212 * Fetches the data from a URL, and passes it to a callback function.
1214 * @param account The account for which the request is needed, or NULL.
1215 * @param url The URL.
1216 * @param full TRUE if this is the full URL, or FALSE if it's a
1217 * partial URL.
1218 * @param user_agent The user agent field to use, or NULL.
1219 * @param http11 TRUE if HTTP/1.1 should be used to download the file.
1220 * @param request A HTTP request to send to the server instead of the
1221 * standard GET
1222 * @param request_len
1223 * Then length of the request being sent
1224 * @param include_headers
1225 * If TRUE, include the HTTP headers in the response.
1226 * @param max_len The maximum number of bytes to retrieve, or a negative
1227 * number to use the default max of 512 KiB.
1228 * @param callback The callback function.
1229 * @param data The user data to pass to the callback function.
1230 * @deprecated In 3.0.0, we'll rename this to "purple_util_fetch_url_request" and get rid of the old one
1232 PurpleUtilFetchUrlData *
1233 purple_util_fetch_url_request_data_len_with_account(PurpleAccount *account,
1234 const char *url, gboolean full, const char *user_agent, gboolean http11,
1235 const char *request, gsize request_len, gboolean include_headers, gssize max_len,
1236 PurpleUtilFetchUrlCallback callback, void *user_data);
1238 * Cancel a pending URL request started with either
1239 * purple_util_fetch_url_request() or purple_util_fetch_url().
1241 * @param url_data The data returned when you initiated the URL fetch.
1243 void purple_util_fetch_url_cancel(PurpleUtilFetchUrlData *url_data);
1246 * Decodes a URL into a plain string.
1248 * This will change hex codes and such to their ascii equivalents.
1250 * @param str The string to translate.
1252 * @return The resulting string.
1254 const char *purple_url_decode(const char *str);
1257 * Encodes a URL into an escaped string.
1259 * This will change non-alphanumeric characters to hex codes.
1261 * @param str The string to translate.
1263 * @return The resulting string.
1265 const char *purple_url_encode(const char *str);
1268 * Checks if the given email address is syntactically valid.
1270 * @param address The email address to validate.
1272 * @return True if the email address is syntactically correct.
1274 gboolean purple_email_is_valid(const char *address);
1277 * Checks if the given IP address is a syntactically valid IPv4 address.
1279 * @param ip The IP address to validate.
1281 * @return True if the IP address is syntactically correct.
1282 * @deprecated This function will be replaced with one that validates
1283 * as either IPv4 or IPv6 in 3.0.0. If you don't want this,
1284 * behavior, use one of the more specific functions.
1286 gboolean purple_ip_address_is_valid(const char *ip);
1289 * Checks if the given IP address is a syntactically valid IPv4 address.
1291 * @param ip The IP address to validate.
1293 * @return True if the IP address is syntactically correct.
1294 * @since 2.6.0
1296 gboolean purple_ipv4_address_is_valid(const char *ip);
1299 * Checks if the given IP address is a syntactically valid IPv6 address.
1301 * @param ip The IP address to validate.
1303 * @return True if the IP address is syntactically correct.
1304 * @since 2.6.0
1306 gboolean purple_ipv6_address_is_valid(const char *ip);
1309 * This function extracts a list of URIs from the a "text/uri-list"
1310 * string. It was "borrowed" from gnome_uri_list_extract_uris
1312 * @param uri_list An uri-list in the standard format.
1314 * @return A GList containing strings allocated with g_malloc
1315 * that have been splitted from uri-list.
1317 GList *purple_uri_list_extract_uris(const gchar *uri_list);
1320 * This function extracts a list of filenames from a
1321 * "text/uri-list" string. It was "borrowed" from
1322 * gnome_uri_list_extract_filenames
1324 * @param uri_list A uri-list in the standard format.
1326 * @return A GList containing strings allocated with g_malloc that
1327 * contain the filenames in the uri-list. Note that unlike
1328 * purple_uri_list_extract_uris() function, this will discard
1329 * any non-file uri from the result value.
1331 GList *purple_uri_list_extract_filenames(const gchar *uri_list);
1333 /*@}*/
1335 /**************************************************************************
1336 * UTF8 String Functions
1337 **************************************************************************/
1338 /*@{*/
1341 * Attempts to convert a string to UTF-8 from an unknown encoding.
1343 * This function checks the locale and tries sane defaults.
1345 * @param str The source string.
1347 * @return The UTF-8 string, or @c NULL if it could not be converted.
1349 gchar *purple_utf8_try_convert(const char *str);
1352 * Salvages the valid UTF-8 characters from a string, replacing any
1353 * invalid characters with a filler character (currently hardcoded to
1354 * '?').
1356 * @param str The source string.
1358 * @return A valid UTF-8 string.
1360 gchar *purple_utf8_salvage(const char *str);
1363 * Removes unprintable characters from a UTF-8 string. These characters
1364 * (in particular low-ASCII characters) are invalid in XML 1.0 and thus
1365 * are not allowed in XMPP and are rejected by libxml2 by default.
1367 * The returned string must be freed by the caller.
1369 * @param str A valid UTF-8 string.
1371 * @return A newly allocated UTF-8 string without the unprintable characters.
1372 * @since 2.6.0
1374 gchar *purple_utf8_strip_unprintables(const gchar *str);
1377 * Return the UTF-8 version of gai_strerror(). It calls gai_strerror()
1378 * then converts the result to UTF-8. This function is analogous to
1379 * g_strerror().
1381 * @param errnum The error code.
1383 * @return The UTF-8 error message.
1384 * @since 2.4.0
1386 G_CONST_RETURN gchar *purple_gai_strerror(gint errnum);
1389 * Compares two UTF-8 strings case-insensitively. This comparison is
1390 * more expensive than a simple g_utf8_collate() comparison because
1391 * it calls g_utf8_casefold() on each string, which allocates new
1392 * strings.
1394 * @param a The first string.
1395 * @param b The second string.
1397 * @return -1 if @a is less than @a b.
1398 * 0 if @a is equal to @a b.
1399 * 1 if @a is greater than @a b.
1401 int purple_utf8_strcasecmp(const char *a, const char *b);
1404 * Case insensitive search for a word in a string. The needle string
1405 * must be contained in the haystack string and not be immediately
1406 * preceded or immediately followed by another alpha-numeric character.
1408 * @param haystack The string to search in.
1409 * @param needle The substring to find.
1411 * @return TRUE if haystack has the word, otherwise FALSE
1413 gboolean purple_utf8_has_word(const char *haystack, const char *needle);
1416 * Prints a UTF-8 message to the given file stream. The function
1417 * tries to convert the UTF-8 message to user's locale. If this
1418 * is not possible, the original UTF-8 text will be printed.
1420 * @param filestream The file stream (e.g. STDOUT or STDERR)
1421 * @param message The message to print.
1423 void purple_print_utf8_to_console(FILE *filestream, char *message);
1426 * Checks for messages starting (post-HTML) with "/me ", including the space.
1428 * @param message The message to check
1429 * @param len The message length, or -1
1431 * @return TRUE if it starts with "/me ", and it has been removed, otherwise
1432 * FALSE
1434 gboolean purple_message_meify(char *message, gssize len);
1437 * Removes the underscore characters from a string used identify the mnemonic
1438 * character.
1440 * @param in The string to strip
1442 * @return The stripped string
1444 char *purple_text_strip_mnemonic(const char *in);
1446 /*@}*/
1449 * Adds 8 to something.
1451 * Blame SimGuy.
1453 * @param x The number to add 8 to.
1455 * @return x + 8
1457 #define purple_add_eight(x) ((x)+8)
1460 * Does the reverse of purple_escape_filename
1462 * This will change hex codes and such to their ascii equivalents.
1464 * @param str The string to translate.
1466 * @return The resulting string.
1468 const char *purple_unescape_filename(const char *str);
1471 * Escapes filesystem-unfriendly characters from a filename
1473 * @param str The string to translate.
1475 * @return The resulting string.
1477 const char *purple_escape_filename(const char *str);
1480 * This is added temporarily to assist the split of oscar into aim and icq.
1481 * This should not be used by plugins.
1483 * @deprecated This function should not be used in new code and should be
1484 * removed in 3.0.0. The aim/icq prpl split happened a long
1485 * time ago, and we don't need to keep migrating old data.
1487 const char *_purple_oscar_convert(const char *act, const char *protocol);
1490 * Restore default signal handlers for signals which might reasonably have
1491 * handlers. This should be called by a fork()'d child process, since child processes
1492 * inherit the handlers of the parent.
1494 void purple_restore_default_signal_handlers(void);
1497 * Gets the host name of the machine. If it not possible to determine the
1498 * host name, "localhost" is returned
1500 * @constreturn The hostname
1502 const gchar *purple_get_host_name(void);
1505 * Returns a type 4 (random) UUID
1507 * @return A UUID, caller is responsible for freeing it
1508 * @since 2.7.0
1510 gchar *purple_uuid_random(void);
1512 #ifdef __cplusplus
1514 #endif
1516 #endif /* _PURPLE_UTIL_H_ */