summaryrefslogtreecommitdiff
path: root/libpurple/util.h
blob: f73a785763a16e64afb28f4a618c2668796cefb1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
/* purple
 *
 * Purple is the legal property of its developers, whose names are too numerous
 * to list here.  Please refer to the COPYRIGHT file distributed with this
 * source distribution.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
 *
 * TODO Rename the functions so that they live somewhere in the purple
 *      namespace.
 */

#ifndef _PURPLE_UTIL_H_
#define _PURPLE_UTIL_H_
/**
 * SECTION:util
 * @section_id: libpurple-util
 * @short_description: <filename>util.h</filename>
 * @title: Utility Functions
 */

#include <stdio.h>

/**
 * PurpleKeyValuePair:
 * @key: The key
 * @value: The value
 *
 * A key-value pair.
 *
 * This is used by, among other things, purple_gtk_combo* functions to pass in a
 * list of key-value pairs so it can display a user-friendly value.
 */
typedef struct _PurpleKeyValuePair PurpleKeyValuePair;

#include "account.h"
#include "signals.h"
#include "xmlnode.h"
#include "notify.h"
#include "protocols.h"


typedef char *(*PurpleInfoFieldFormatCallback)(const char *field, size_t len);

struct _PurpleKeyValuePair
{
	gchar *key;
	void *value;

};

G_BEGIN_DECLS

/**
 * purple_util_set_current_song:
 * @title:     The title of the song, %NULL to unset the value.
 * @artist:    The artist of the song, can be %NULL.
 * @album:     The album of the song, can be %NULL.
 *
 * Set the appropriate presence values for the currently playing song.
 */
void purple_util_set_current_song(const char *title, const char *artist,
		const char *album);

/**
 * purple_util_format_song_info:
 * @title:     The title of the song, %NULL to unset the value.
 * @artist:    The artist of the song, can be %NULL.
 * @album:     The album of the song, can be %NULL.
 * @unused:    Currently unused, must be %NULL.
 *
 * Format song information.
 *
 * Returns:   The formatted string. The caller must g_free the returned string.
 */
char * purple_util_format_song_info(const char *title, const char *artist,
		const char *album, gpointer unused);

/**************************************************************************/
/* Utility Subsystem                                                      */
/**************************************************************************/

/**
 * purple_util_init:
 *
 * Initializes the utility subsystem.
 */
void purple_util_init(void);

/**
 * purple_util_uninit:
 *
 * Uninitializes the util subsystem.
 */
void purple_util_uninit(void);

/**************************************************************************/
/* Base16 Functions                                                       */
/**************************************************************************/

/**
 * purple_base16_encode:
 * @data: The data to convert.
 * @len:  The length of the data.
 *
 * Converts a chunk of binary data to its base-16 equivalent.
 *
 *  See purple_base16_decode()
 *
 * Returns: The base-16 string in the ASCII encoding.  Must be
 *         g_free'd when no longer needed.
 */
gchar *purple_base16_encode(const guchar *data, gsize len);

/**
 * purple_base16_decode:
 * @str:     The base-16 string to convert to raw data.
 * @ret_len: The length of the returned data.  You can
 *                pass in NULL if you're sure that you know
 *                the length of the decoded data, or if you
 *                know you'll be able to use strlen to
 *                determine the length, etc.
 *
 * Converts an ASCII string of base-16 encoded data to
 * the binary equivalent.
 *
 *  See purple_base16_encode()
 *
 * Returns: The raw data.  Must be g_free'd when no longer needed.
 */
guchar *purple_base16_decode(const char *str, gsize *ret_len);

/**
 * purple_base16_encode_chunked:
 * @data: The data to convert.
 * @len:  The length of the data.
 *
 * Converts a chunk of binary data to a chunked base-16 representation
 * (handy for key fingerprints)
 *
 * Example output: 01:23:45:67:89:AB:CD:EF
 *
 * Returns: The base-16 string in the ASCII chunked encoding.  Must be
 *         g_free'd when no longer needed.
 */
gchar *purple_base16_encode_chunked(const guchar *data, gsize len);

/**************************************************************************/
/* Quoted Printable Functions                                             */
/**************************************************************************/

/**
 * purple_quotedp_decode:
 * @str:     The quoted printable ASCII string to convert to raw data.
 * @ret_len: The length of the returned data.
 *
 * Converts a quoted printable string back to its readable equivalent.
 * What is a quoted printable string, you ask?  It's an encoding used
 * to transmit binary data as ASCII.  It's intended purpose is to send
 * emails containing non-ASCII characters.  Wikipedia has a pretty good
 * explanation.  Also see RFC 2045.
 *
 * Returns: The readable string.  Must be g_free'd when no longer needed.
 */
guchar *purple_quotedp_decode(const char *str, gsize *ret_len);

/**************************************************************************/
/* MIME Functions                                                         */
/**************************************************************************/

/**
 * purple_mime_decode_field:
 * @str: The ASCII string, possibly containing any number of
 *            encoded-word sections.
 *
 * Converts a MIME header field string back to its readable equivalent
 * according to RFC 2047.  Basically, a header is plain ASCII and can
 * contain any number of sections called "encoded-words."  The format
 * of an encoded word is =?ISO-8859-1?Q?Keld_J=F8rn_Simonsen?=
 * =? designates the beginning of the encoded-word
 * ?= designates the end of the encoded-word
 *
 * An encoded word is segmented into three pieces by the use of a
 * question mark.  The first piece is the character set, the second
 * piece is the encoding, and the third piece is the encoded text.
 *
 * Returns: The string, with any encoded-word sections decoded and
 *         converted to UTF-8.  Must be g_free'd when no longer
 *         needed.
 */
char *purple_mime_decode_field(const char *str);


/**************************************************************************/
/* Date/Time Functions                                                    */
/**************************************************************************/

/**
 * purple_utf8_strftime:
 * @format: The format string, in UTF-8
 * @tm:     The time to format, or %NULL to use the current local time
 *
 * Formats a time into the specified format.
 *
 * This is essentially strftime(), but it has a static buffer
 * and handles the UTF-8 conversion for the caller.
 *
 * This function also provides the GNU \%z formatter if the underlying C
 * library doesn't.  However, the format string parser is very naive, which
 * means that conversions specifiers to \%z cannot be guaranteed.  The GNU
 * strftime(3) man page describes \%z as: 'The time-zone as hour offset from
 * GMT.  Required to emit RFC822-conformant dates
 * (using "\%a, \%d \%b \%Y \%H:\%M:\%S \%z"). (GNU)'
 *
 * On Windows, this function also converts the results for \%Z from a timezone
 * name (as returned by the system strftime() \%Z format string) to a timezone
 * abbreviation (as is the case on Unix).  As with \%z, conversion specifiers
 * should not be used.
 *
 * Note: @format is required to be in UTF-8.  This differs from strftime(),
 *       where the format is provided in the locale charset.
 *
 * Returns: The formatted time, in UTF-8.
 */
const char *purple_utf8_strftime(const char *format, const struct tm *tm);

/**
 * purple_date_format_short:
 * @tm: The time to format, or %NULL to use the current local time
 *
 * Formats a time into the user's preferred short date format.
 *
 * The returned string is stored in a static buffer, so the result
 * should be g_strdup()'d if it's going to be kept.
 *
 * Returns: The date, formatted as per the user's settings.  In the USA this
 *         is something like "02/18/13"
 */
const char *purple_date_format_short(const struct tm *tm);

/**
 * purple_date_format_long:
 * @tm: The time to format, or %NULL to use the current local time
 *
 * Formats a time into the user's preferred short date plus time format.
 *
 * The returned string is stored in a static buffer, so the result
 * should be g_strdup()'d if it's going to be kept.
 *
 * Returns: The timestamp, formatted as per the user's settings.  In the USA
 *         this is something like "02/18/13 15:26:44"
 */
const char *purple_date_format_long(const struct tm *tm);

/**
 * purple_date_format_full:
 * @tm: The time to format, or %NULL to use the current local time
 *
 * Formats a time into the user's preferred full date and time format.
 *
 * The returned string is stored in a static buffer, so the result
 * should be g_strdup()'d if it's going to be kept.
 *
 * Returns: The date and time, formatted as per the user's settings.  In the
 *         USA this is something like "Mon Feb 18 15:26:44 2013"
 */
const char *purple_date_format_full(const struct tm *tm);

/**
 * purple_time_format:
 * @tm: The time to format, or %NULL to use the current local time
 *
 * Formats a time into the user's preferred time format.
 *
 * The returned string is stored in a static buffer, so the result
 * should be g_strdup()'d if it's going to be kept.
 *
 * Returns: The time, formatted as per the user's settings.  In the USA this
 *         is something like "15:26:44"
 */
const char *purple_time_format(const struct tm *tm);

/**
 * purple_time_build:
 * @year:  The year.
 * @month: The month.
 * @day:   The day.
 * @hour:  The hour.
 * @min:   The minute.
 * @sec:   The second.
 *
 * Builds a time_t from the supplied information.
 *
 * Returns: A time_t.
 */
time_t purple_time_build(int year, int month, int day, int hour,
					   int min, int sec);

/**
 * PURPLE_NO_TZ_OFF:
 *
 * Used by purple_str_to_time to indicate no timezone offset was
 * specified in the timestamp string.
 */
#define PURPLE_NO_TZ_OFF -500000

/**
 * purple_str_to_time:
 * @timestamp: The timestamp
 * @utc:       Assume UTC if no timezone specified
 * @tm:        If not %NULL, the caller can get a copy of the
 *                  struct tm used to calculate the time_t return value.
 * @tz_off:    If not %NULL, the caller can get a copy of the
 *                  timezone offset (from UTC) used to calculate the time_t
 *                  return value. Note: Zero is a valid offset. As such,
 *                  the value of the macro PURPLE_NO_TZ_OFF indicates no
 *                  offset was specified (which means that the local
 *                  timezone was used in the calculation).
 * @rest:      If not %NULL, the caller can get a pointer to the
 *                  part of @timestamp left over after parsing is
 *                  completed, if it's not the end of @timestamp.
 *
 * Parses a timestamp in jabber, ISO8601, or MM/DD/YYYY format and returns
 * a time_t.
 *
 * Returns: A time_t.
 */
time_t purple_str_to_time(const char *timestamp, gboolean utc,
                        struct tm *tm, long *tz_off, const char **rest);

/**
 * purple_str_to_date_time:
 * @timestamp: The timestamp
 * @utc:       Assume UTC if no timezone specified
 *
 * Parses a timestamp in jabber, ISO8601, or MM/DD/YYYY format and returns
 * a GDateTime.
 *
 * Returns: (transfer full): A GDateTime.
 */
GDateTime *purple_str_to_date_time(const char *timestamp, gboolean utc);

/**
 * purple_uts35_to_str:
 * @format: The formatting string, according to UTS \#35
 *               See http://unicode.org/reports/tr35/
 *               (NOTE: not all formats are supported)
 * @len:    The length of the formatting string
 * @tm:     The time to format, or %NULL to use the current local time
 *
 * Formats a datetime according to a UTS-35 Date Format Pattern.
 *
 * Returns: The time, formatted as per the user's settings.
 */
char *purple_uts35_to_str(const char *format, size_t len, struct tm *tm);


/**************************************************************************/
/* Markup Functions                                                       */
/**************************************************************************/

/**
 * purple_markup_escape_text:
 * @text: The text to escape
 * @length: The length of the text, or -1 if #NULL terminated
 *
 * Escapes special characters in a plain-text string so they display
 * correctly as HTML.  For example, &amp; is replaced with &amp;amp; and &lt; is
 * replaced with &amp;lt;
 *
 * This is exactly the same as g_markup_escape_text(), except that it
 * does not change ' to &amp;apos; because &amp;apos; is not a valid HTML 4 entity,
 * and is displayed literally in IE7.
 */
gchar *purple_markup_escape_text(const gchar *text, gssize length);

/**
 * purple_markup_find_tag:
 * @needle:	  The name of the tag
 * @haystack:	  The null-delimited string to search in
 * @start:		  A pointer to the start of the tag if found
 * @end:		  A pointer to the end of the tag if found
 * @attributes:  The attributes, if the tag was found.  This should
 *                    be freed with g_datalist_clear().
 *
 * Finds an HTML tag matching the given name.
 *
 * This locates an HTML tag's start and end, and stores its attributes
 * in a GData hash table. The names of the attributes are lower-cased
 * in the hash table, and the name of the tag is case insensitive.
 *
 * Returns: TRUE if the tag was found
 */
gboolean purple_markup_find_tag(const char *needle, const char *haystack,
							  const char **start, const char **end,
							  GData **attributes);

/**
 * purple_markup_extract_info_field:
 * @str:            The string to parse.
 * @len:            The size of str.
 * @user_info:      The destination PurpleNotifyUserInfo to which the new
 *                       field info should be added.
 * @start_token:    The beginning token.
 * @skip:           The number of characters to skip after the
 *                       start token.
 * @end_token:      The ending token.
 * @check_value:    The value that the last character must meet.
 * @no_value_token: The token indicating no value is given.
 * @display_name:   The short descriptive name to display for this token.
 * @is_link:        TRUE if this should be a link, or FALSE otherwise.
 * @link_prefix:    The prefix for the link.
 * @format_cb:      A callback to format the value before adding it.
 *
 * Extracts a field of data from HTML.
 *
 * This is a scary function. It used to be used for MSN and Yahoo prpls,
 * but since those prpls have been removed, this is now deprecated.
 *
 * Returns: TRUE if successful, or FALSE otherwise.
 */
gboolean purple_markup_extract_info_field(const char *str, int len, PurpleNotifyUserInfo *user_info,
                                        const char *start_token, int skip,
                                        const char *end_token, char check_value,
                                        const char *no_value_token,
                                        const char *display_name, gboolean is_link,
                                        const char *link_prefix,
					PurpleInfoFieldFormatCallback format_cb);

/**
 * purple_markup_html_to_xhtml:
 * @html:       The HTML markup.
 * @dest_xhtml: The destination XHTML output.
 * @dest_plain: The destination plain-text output.
 *
 * Converts HTML markup to XHTML.
 */
void purple_markup_html_to_xhtml(const char *html, char **dest_xhtml,
							   char **dest_plain);

/**
 * purple_markup_strip_html:
 * @str: The string to strip HTML from.
 *
 * Strips HTML tags from a string.
 *
 * Returns: The new string without HTML.  You must g_free this string
 *         when finished with it.
 */
char *purple_markup_strip_html(const char *str);

/**
 * purple_markup_linkify:
 * @str: The string to linkify.
 *
 * Adds the necessary HTML code to turn URIs into HTML links in a string.
 *
 * Returns: The new string with all URIs surrounded in standard
 *          HTML &lt;a href="whatever"&gt;&lt;/a&gt; tags. You must g_free()
 *          this string when finished with it.
 */
char *purple_markup_linkify(const char *str);

/**
 * purple_unescape_text:
 * @text: The string in which to unescape any HTML entities
 *
 * Unescapes HTML entities to their literal characters in the text.
 * For example "&amp;amp;" is replaced by '&amp;' and so on.  Also converts
 * numerical entities (e.g. "&amp;\#38;" is also '&amp;').
 *
 * This function currently supports the following named entities:
 *     "&amp;amp;", "&amp;lt;", "&amp;gt;", "&amp;copy;", "&amp;quot;",
 *     "&amp;reg;", "&amp;apos;"
 *
 * purple_unescape_html() is similar, but also converts "&lt;br&gt;" into "\n".
 *
 *  See purple_unescape_html()
 *
 * Returns: The text with HTML entities literalized.  You must g_free
 *         this string when finished with it.
 */
char *purple_unescape_text(const char *text);

/**
 * purple_unescape_html:
 * @html: The string in which to unescape any HTML entities
 *
 * Unescapes HTML entities to their literal characters and converts
 * "&lt;br&gt;" to "\n".  See purple_unescape_text() for more details.
 *
 *  See purple_unescape_text()
 *
 * Returns: The text with HTML entities literalized.  You must g_free
 *         this string when finished with it.
 */
char *purple_unescape_html(const char *html);

/**
 * purple_markup_slice:
 * @str: The input NUL terminated, HTML, UTF-8 (or ASCII) string.
 * @x: The character offset into an unformatted version of str to
 *          begin at.
 * @y: The character offset (into an unformatted vesion of str) of
 *          one past the last character to include in the slice.
 *
 * Returns a newly allocated substring of the HTML UTF-8 string "str".
 * The markup is preserved such that the substring will have the same
 * formatting as original string, even though some tags may have been
 * opened before "x", or may close after "y". All open tags are closed
 * at the end of the returned string, in the proper order.
 *
 * Note that x and y are in character offsets, not byte offsets, and
 * are offsets into an unformatted version of str. Because of this,
 * this function may be sensitive to changes in GtkIMHtml and may break
 * when used with other UI's. libpurple users are encouraged to report and
 * work out any problems encountered.
 *
 * Returns: The HTML slice of string, with all formatting retained.
 */
char *purple_markup_slice(const char *str, guint x, guint y);

/**
 * purple_markup_get_tag_name:
 * @tag: The string starting a HTML tag.
 *
 * Returns a newly allocated string containing the name of the tag
 * located at "tag". Tag is expected to point to a '<', and contain
 * a '>' sometime after that. If there is no '>' and the string is
 * not NUL terminated, this function can be expected to segfault.
 *
 * Returns: A string containing the name of the tag.
 */
char *purple_markup_get_tag_name(const char *tag);

/**
 * purple_markup_unescape_entity:
 * @text:   A string containing an HTML entity.
 * @length: If not %NULL, the string length of the entity is stored in this location.
 *
 * Returns a constant string of the character representation of the HTML
 * entity pointed to by @text. For example, purple_markup_unescape_entity("&amp;amp;")
 * will return "&amp;". The @text variable is expected to point to an '&amp;',
 * the first character of the entity. If given an unrecognized entity, the function
 * returns %NULL.
 *
 * Note that this function, unlike purple_unescape_html(), does not search
 * the string for the entity, does not replace the entity, and does not
 * return a newly allocated string.
 *
 * Returns: A constant string containing the character representation of the given entity.
 */
const char * purple_markup_unescape_entity(const char *text, int *length);

/**
 * purple_markup_get_css_property:
 * @style: A string containing the inline CSS text.
 * @opt:   The requested CSS property.
 *
 * Returns a newly allocated string containing the value of the CSS property specified
 * in opt. The @style argument is expected to point to a HTML inline CSS.
 * The function will seek for the CSS property and return its value.
 *
 * For example, purple_markup_get_css_property("direction:rtl;color:#dc4d1b;",
 * "color") would return "#dc4d1b".
 *
 * On error or if the requested property was not found, the function returns
 * %NULL.
 *
 * Returns: The value of the requested CSS property.
 */
char * purple_markup_get_css_property(const gchar *style, const gchar *opt);

/**
 * purple_markup_is_rtl:
 * @html:  The HTML text.
 *
 * Check if the given HTML contains RTL text.
 *
 * Returns:  TRUE if the text contains RTL text, FALSE otherwise.
 */
gboolean purple_markup_is_rtl(const char *html);


/**************************************************************************/
/* Path/Filename Functions                                                */
/**************************************************************************/

/**
 * purple_home_dir:
 *
 * Returns the user's home directory.
 *
 *  See purple_user_dir()
 *
 * Returns: The user's home directory.
 */
const gchar *purple_home_dir(void);

/**
 * purple_user_dir:
 *
 * Returns the purple settings directory in the user's home directory.
 * This is usually $HOME/.purple
 *
 *  See purple_home_dir()
 *
 * Returns: The purple settings directory.
 * 
 * Deprecated: Use purple_cache_dir(), purple_config_dir() or
 *             purple_data_dir() instead.
 */
G_DEPRECATED_FOR(purple_cache_dir' or 'purple_config_dir' or 'purple_data_dir)
const char *purple_user_dir(void);

/**
 * purple_cache_dir:
 *
 * Returns the purple cache directory according to XDG Base Directory Specification.
 * This is usually $HOME/.cache/purple.
 * If custom user dir was specified then this is cache
 * sub-directory of DIR argument passed to -c option.
 *
 * Returns: The purple cache directory.
 */
const gchar *purple_cache_dir(void);

/**
 * purple_config_dir:
 *
 * Returns the purple configuration directory according to XDG Base Directory Specification.
 * This is usually $HOME/.config/purple.
 * If custom user dir was specified then this is config
 * sub-directory of DIR argument passed to -c option.
 *
 * Returns: The purple configuration directory.
 */
const gchar *purple_config_dir(void);

/**
 * purple_data_dir:
 *
 * Returns the purple data directory according to XDG Base Directory Specification.
 * This is usually $HOME/.local/share/purple.
 * If custom user dir was specified then this is data
 * sub-directory of DIR argument passed to -c option.
 *
 * Returns: The purple data directory.
 */
const gchar *purple_data_dir(void);

/**
 * purple_move_to_xdg_base_dir:
 * @purple_xdg_dir: The path to cache, config or data dir.
 *                  Use respective function
 * @path:           File or directory in purple_user_dir
 * 
 * Moves file or directory from legacy user dir to XDG
 * based dir.
 * 
 * Returns: TRUE if moved successfully, FALSE otherwise
 */
gboolean
purple_move_to_xdg_base_dir(const char *purple_xdg_dir, char *path);

/**
 * purple_util_set_user_dir:
 * @dir: The custom settings directory
 *
 * Define a custom purple settings directory, overriding the default (user's home directory/.purple)
 */
void purple_util_set_user_dir(const char *dir);

/**
 * purple_build_dir:
 * @path: The path you wish to create.  Note that it must start
 *        from the root or this function will fail.
 * @mode: Unix-style permissions for this directory.
 *
 * Builds a complete path from the root, making any directories along
 * the path which do not already exist.
 *
 * Returns: 0 for success, nonzero on any error.
 */
int purple_build_dir(const char *path, int mode);

/**
 * purple_util_write_data_to_file:
 * @filename: The basename of the file to write in the purple_user_dir.
 * @data:     A string of data to write.
 * @size:     The size of the data to save.  If data is
 *                 null-terminated you can pass in -1.
 *
 * Write a string of data to a file of the given name in the Purple
 * user directory ($HOME/.purple by default).  The data is typically
 * a serialized version of one of Purple's config files, such as
 * prefs.xml, accounts.xml, etc.  And the string is typically
 * obtained using purple_xmlnode_to_formatted_str.  However, this function
 * should work fine for saving binary files as well.
 *
 * Returns: TRUE if the file was written successfully.  FALSE otherwise.
 * 
 * Deprecated: Use purple_util_write_data_to_cache_file(),
 *             purple_util_write_data_to_config_file() or
 *             purple_util_write_data_to_data_file() instead.
 */
G_DEPRECATED_FOR(purple_util_write_data_to_cache_file' or 'purple_util_write_data_to_config_file' or 'purple_util_write_data_to_data_file)
gboolean purple_util_write_data_to_file(const char *filename, const char *data,
									  gssize size);

/**
 * purple_util_write_data_to_cache_file:
 * @filename: The basename of the file to write in the purple_cache_dir.
 * @data:     A string of data to write.
 * @size:     The size of the data to save.  If data is
 *                 null-terminated you can pass in -1.
 *
 * Write a string of data to a file of the given name in the Purple
 * cache directory ($HOME/.cache/purple by default).
 * 
 *  See purple_util_write_data_to_file()
 *
 * Returns: TRUE if the file was written successfully.  FALSE otherwise.
 */
gboolean
purple_util_write_data_to_cache_file(const char *filename, const char *data, gssize size);

/**
 * purple_util_write_data_to_config_file:
 * @filename: The basename of the file to write in the purple_config_dir.
 * @data:     A string of data to write.
 * @size:     The size of the data to save.  If data is
 *                 null-terminated you can pass in -1.
 *
 * Write a string of data to a file of the given name in the Purple
 * config directory ($HOME/.config/purple by default).
 *
 *  See purple_util_write_data_to_file()
 *
 * Returns: TRUE if the file was written successfully.  FALSE otherwise.
 */
gboolean
purple_util_write_data_to_config_file(const char *filename, const char *data, gssize size);

/**
 * purple_util_write_data_to_data_file:
 * @filename: The basename of the file to write in the purple_data_dir.
 * @data:     A string of data to write.
 * @size:     The size of the data to save.  If data is
 *                 null-terminated you can pass in -1.
 *
 * Write a string of data to a file of the given name in the Purple
 * data directory ($HOME/.local/share/purple by default).
 *
 *  See purple_util_write_data_to_file()
 *
 * Returns: TRUE if the file was written successfully.  FALSE otherwise.
 */
gboolean
purple_util_write_data_to_data_file(const char *filename, const char *data, gssize size);

/**
 * purple_util_write_data_to_file_absolute:
 * @filename_full: Filename to write to
 * @data:          A string of data to write.
 * @size:          The size of the data to save.  If data is
 *                      null-terminated you can pass in -1.
 *
 * Write data to a file using the absolute path.
 *
 * This exists for Glib backwards compatibility reasons.
 *
 *  See purple_util_write_data_to_file()
 *
 * Returns: TRUE if the file was written successfully.  FALSE otherwise.
 */
/* TODO: Remove this function (use g_file_set_contents instead) when 3.0.0
 *       rolls around. */
gboolean
purple_util_write_data_to_file_absolute(const char *filename_full, const char *data, gssize size);

/**
 * purple_util_read_xml_from_file:
 * @filename:    The basename of the file to open in the purple_user_dir.
 * @description: A very short description of the contents of this
 *                    file.  This is used in error messages shown to the
 *                    user when the file can not be opened.  For example,
 *                    "preferences," or "buddy pounces."
 *
 * Read the contents of a given file and parse the results into an
 * PurpleXmlNode tree structure.  This is intended to be used to read
 * Purple's configuration xml files (prefs.xml, pounces.xml, etc.)
 *
 * Returns: An PurpleXmlNode tree of the contents of the given file.  Or NULL, if
 *         the file does not exist or there was an error reading the file.
 * 
 * Deprecated: Use purple_util_read_xml_from_cache_file(),
 *             purple_util_read_xml_from_config_file() or
 *             purple_util_read_xml_from_data_file() instead.
 */
G_DEPRECATED_FOR(purple_util_read_xml_from_cache_file' or 'purple_util_read_xml_from_config_file' or 'purple_util_read_xml_from_data_file)
PurpleXmlNode *purple_util_read_xml_from_file(const char *filename,
									  const char *description);

/**
 * purple_util_read_xml_from_cache_file:
 * @filename:    The basename of the file to open in the purple_cache_dir.
 * @description: A very short description of the contents of this
 *                    file.  This is used in error messages shown to the
 *                    user when the file can not be opened.  For example,
 *                    "preferences," or "buddy pounces."
 *
 * Read the contents of a given file and parse the results into an
 * PurpleXmlNode tree structure.  This is intended to be used to read
 * Purple's cache xml files (xmpp-caps.xml, etc.)
 *
 * Returns: An PurpleXmlNode tree of the contents of the given file.  Or NULL, if
 *         the file does not exist or there was an error reading the file.
 */
PurpleXmlNode *
purple_util_read_xml_from_cache_file(const char *filename, const char *description);

/**
 * purple_util_read_xml_from_config_file:
 * @filename:    The basename of the file to open in the purple_config_dir.
 * @description: A very short description of the contents of this
 *                    file.  This is used in error messages shown to the
 *                    user when the file can not be opened.  For example,
 *                    "preferences," or "buddy pounces."
 *
 * Read the contents of a given file and parse the results into an
 * PurpleXmlNode tree structure.  This is intended to be used to read
 * Purple's config xml files (prefs.xml, pounces.xml, etc.)
 *
 * Returns: An PurpleXmlNode tree of the contents of the given file.  Or NULL, if
 *         the file does not exist or there was an error reading the file.
 */
PurpleXmlNode *
purple_util_read_xml_from_config_file(const char *filename, const char *description);

/**
 * purple_util_read_xml_from_data_file:
 * @filename:    The basename of the file to open in the purple_data_dir.
 * @description: A very short description of the contents of this
 *                    file.  This is used in error messages shown to the
 *                    user when the file can not be opened.  For example,
 *                    "preferences," or "buddy pounces."
 *
 * Read the contents of a given file and parse the results into an
 * PurpleXmlNode tree structure.  This is intended to be used to read
 * Purple's cache xml files (accounts.xml, etc.)
 *
 * Returns: An PurpleXmlNode tree of the contents of the given file.  Or NULL, if
 *         the file does not exist or there was an error reading the file.
 */
PurpleXmlNode *
purple_util_read_xml_from_data_file(const char *filename, const char *description);

/**
 * purple_mkstemp:
 * @path:   The returned path to the temp file.
 * @binary: Text or binary, for platforms where it matters.
 *
 * Creates a temporary file and returns a file pointer to it.
 *
 * This is like mkstemp(), but returns a file pointer and uses a
 * pre-set template. It uses the semantics of tempnam() for the
 * directory to use and allocates the space for the file path.
 *
 * The caller is responsible for closing the file and removing it when
 * done, as well as freeing the space pointed to by @path with
 * g_free().
 *
 * Returns: A file pointer to the temporary file, or %NULL on failure.
 */
FILE *purple_mkstemp(char **path, gboolean binary);


/**************************************************************************/
/* Environment Detection Functions                                        */
/**************************************************************************/

/**
 * purple_program_is_valid:
 * @program: The file name of the application.
 *
 * Checks if the given program name is valid and executable.
 *
 * Returns: TRUE if the program is runable.
 */
gboolean purple_program_is_valid(const char *program);

/**
 * purple_running_gnome:
 *
 * Check if running GNOME.
 *
 * Returns: TRUE if running GNOME, FALSE otherwise.
 */
gboolean purple_running_gnome(void);

/**
 * purple_running_kde:
 *
 * Check if running KDE.
 *
 * Returns: TRUE if running KDE, FALSE otherwise.
 */
gboolean purple_running_kde(void);

/**
 * purple_running_osx:
 *
 * Check if running OS X.
 *
 * Returns: TRUE if running OS X, FALSE otherwise.
 */
gboolean purple_running_osx(void);

/**
 * purple_fd_get_ip:
 * @fd: The socket file descriptor.
 *
 * Returns the IP address from a socket file descriptor.
 *
 * Returns: The IP address, or %NULL on error.
 */
char *purple_fd_get_ip(int fd);

/**
 * purple_socket_get_family:
 * @fd: The socket file descriptor.
 *
 * Returns the address family of a socket.
 *
 * Returns: The address family of the socket (AF_INET, AF_INET6, etc) or -1
 *         on error.
 */
int purple_socket_get_family(int fd);

/**
 * purple_socket_speaks_ipv4:
 * @fd: The socket file descriptor
 *
 * Returns TRUE if a socket is capable of speaking IPv4.
 *
 * This is the case for IPv4 sockets and, on some systems, IPv6 sockets
 * (due to the IPv4-mapped address functionality).
 *
 * Returns: TRUE if a socket can speak IPv4.
 */
gboolean purple_socket_speaks_ipv4(int fd);


/**************************************************************************/
/* String Functions                                                       */
/**************************************************************************/

/**
 * purple_strequal:
 * @left:	A string
 * @right: A string to compare with left
 *
 * Tests two strings for equality.
 *
 * Unlike strcmp(), this function will not crash if one or both of the
 * strings are %NULL.
 *
 * Returns: %TRUE if the strings are the same, else %FALSE.
 */
static inline gboolean
purple_strequal(const gchar *left, const gchar *right)
{
	return (g_strcmp0(left, right) == 0);
}

/**
 * purple_normalize:
 * @account:  The account the string belongs to, or NULL if you do
 *                 not know the account.  If you use NULL, the string
 *                 will still be normalized, but if the protocol uses a
 *                 custom normalization function then the string may
 *                 not be normalized correctly.
 * @str:      The string to normalize.
 *
 * Normalizes a string, so that it is suitable for comparison.
 *
 * The returned string will point to a static buffer, so if the
 * string is intended to be kept long-term, you <emphasis>must</emphasis>
 * g_strdup() it. Also, calling normalize() twice in the same line
 * will lead to problems.
 *
 * Returns: A pointer to the normalized version stored in a static buffer.
 */
const char *purple_normalize(const PurpleAccount *account, const char *str);

/**
 * purple_normalize_nocase:
 * @account:  The account the string belongs to.
 * @str:      The string to normalize.
 *
 * Normalizes a string, so that it is suitable for comparison.
 *
 * This is one possible implementation for the protocol callback
 * function "normalize."  It returns a lowercase and UTF-8
 * normalized version of the string.
 *
 * Returns: A pointer to the normalized version stored in a static buffer.
 */
const char *purple_normalize_nocase(const PurpleAccount *account, const char *str);

/**
 * purple_validate:
 * @protocol: The protocol the string belongs to.
 * @str:      The string to validate.
 *
 * Checks, if a string is valid.
 *
 * Returns: TRUE, if string is valid, otherwise FALSE.
 */
gboolean purple_validate(const PurpleProtocol *protocol, const char *str);

/**
 * purple_str_has_prefix:
 * @s:  The string to check.
 * @p:  The prefix in question.
 *
 * Compares two strings to see if the first contains the second as
 * a proper prefix.
 *
 * Returns:   TRUE if p is a prefix of s, otherwise FALSE.
 */
gboolean purple_str_has_prefix(const char *s, const char *p);

/**
 * purple_str_has_caseprefix:
 * @s: The string to check.
 * @p: The prefix in question.
 *
 * Compares two strings to see if the first contains the second as
 * a proper case-insensitive prefix.
 *
 * Returns: %TRUE if @p is a prefix of @s, otherwise %FALSE.
 */
gboolean
purple_str_has_caseprefix(const gchar *s, const gchar *p);

/**
 * purple_str_has_suffix:
 * @s:  The string to check.
 * @x:  The suffix in question.
 *
 * Compares two strings to see if the second is a proper suffix
 * of the first.
 *
 * Returns:   TRUE if x is a a suffix of s, otherwise FALSE.
 */
gboolean purple_str_has_suffix(const char *s, const char *x);

/**
 * purple_strdup_withhtml:
 * @src: The source string.
 *
 * Duplicates a string and replaces all newline characters from the
 * source string with HTML linebreaks.
 *
 * Returns: The new string.  Must be g_free'd by the caller.
 */
gchar *purple_strdup_withhtml(const gchar *src);

/**
 * purple_str_add_cr:
 * @str: The source string.
 *
 * Ensures that all linefeeds have a matching carriage return.
 *
 * Returns: The string with carriage returns.
 */
char *purple_str_add_cr(const char *str);

/**
 * purple_str_strip_char:
 * @str:     The string to strip characters from.
 * @thechar: The character to strip from the given string.
 *
 * Strips all instances of the given character from the
 * given string.  The string is modified in place.  This
 * is useful for stripping new line characters, for example.
 *
 * Example usage:
 * purple_str_strip_char(my_dumb_string, '\n');
 */
void purple_str_strip_char(char *str, char thechar);

/**
 * purple_util_chrreplace:
 * @string: The string from which to replace stuff.
 * @delimiter: The character you want replaced.
 * @replacement: The character you want inserted in place
 *        of the delimiting character.
 *
 * Given a string, this replaces all instances of one character
 * with another.  This happens inline (the original string IS
 * modified).
 */
void purple_util_chrreplace(char *string, char delimiter,
						  char replacement);

/**
 * purple_strreplace:
 * @string: The string from which to replace stuff.
 * @delimiter: The substring you want replaced.
 * @replacement: The substring you want inserted in place
 *        of the delimiting substring.
 *
 * Given a string, this replaces one substring with another
 * and returns a newly allocated string.
 *
 * Returns: A new string, after performing the substitution.
 *         free this with g_free().
 */
gchar *purple_strreplace(const char *string, const char *delimiter,
					   const char *replacement);


/**
 * purple_utf8_ncr_encode:
 * @in: The string which might contain utf-8 substrings
 *
 * Given a string, this replaces any utf-8 substrings in that string with
 * the corresponding numerical character reference, and returns a newly
 * allocated string.
 *
 * Returns: A new string, with utf-8 replaced with numerical character
 *         references, free this with g_free()
*/
char *purple_utf8_ncr_encode(const char *in);


/**
 * purple_utf8_ncr_decode:
 * @in: The string which might contain numerical character references.
 *
 * Given a string, this replaces any numerical character references
 * in that string with the corresponding actual utf-8 substrings,
 * and returns a newly allocated string.
 *
 * Returns: A new string, with numerical character references
 *         replaced with actual utf-8, free this with g_free().
 */
char *purple_utf8_ncr_decode(const char *in);


/**
 * purple_strcasereplace:
 * @string: The string from which to replace stuff.
 * @delimiter: The substring you want replaced.
 * @replacement: The substring you want inserted in place
 *        of the delimiting substring.
 *
 * Given a string, this replaces one substring with another
 * ignoring case and returns a newly allocated string.
 *
 * Returns: A new string, after performing the substitution.
 *         free this with g_free().
 */
gchar *purple_strcasereplace(const char *string, const char *delimiter,
						   const char *replacement);

/**
 * purple_strcasestr:
 * @haystack: The string to search in.
 * @needle:   The substring to find.
 *
 * This is like strstr, except that it ignores ASCII case in
 * searching for the substring.
 *
 * Returns: the location of the substring if found, or NULL if not
 */
const char *purple_strcasestr(const char *haystack, const char *needle);

/**
 * purple_str_size_to_units:
 * @size: The size
 *
 * Returns a string representing a filesize in the appropriate
 * units (MB, KB, GB, etc.)
 *
 * Returns: The string in units form. This must be freed.
 */
char *purple_str_size_to_units(goffset size);

/**
 * purple_str_seconds_to_string:
 * @sec: The seconds.
 *
 * Converts seconds into a human-readable form.
 *
 * Returns: A human-readable form, containing days, hours, minutes, and
 *         seconds.
 */
char *purple_str_seconds_to_string(guint sec);

/**
 * purple_str_binary_to_ascii:
 * @binary: A string of random data, possibly with embedded NULs
 *               and such.
 * @len: The length in bytes of the input string. Must not be 0.
 *
 * Converts a binary string into a NUL terminated ascii string,
 * replacing nonascii characters and characters below SPACE (including
 * NUL) into \\xyy, where yy are two hex digits. Also backslashes are
 * changed into two backslashes (\\\\). The returned, newly allocated
 * string can be outputted to the console, and must be g_free()d.
 *
 * Returns: A newly allocated ASCIIZ string.
 */
char *purple_str_binary_to_ascii(const unsigned char *binary, guint len);

/**
 * purple_utf16_size:
 * @str: String to check.
 *
 * Calculates UTF-16 string size (in bytes).
 *
 * Returns:    Number of bytes (including NUL character) that string occupies.
 */
size_t purple_utf16_size(const gunichar2 *str);

/**
 * purple_str_wipe:
 * @str: A NUL-terminated string to free, or a NULL-pointer.
 *
 * Fills a NUL-terminated string with zeros and frees it.
 *
 * It should be used to free sensitive data, like passwords.
 */
void purple_str_wipe(gchar *str);

/**
 * purple_utf16_wipe:
 * @str: A NUL-terminated string to free, or a NULL-pointer.
 *
 * Fills a NUL-terminated UTF-16 string with zeros and frees it.
 *
 * It should be used to free sensitive data, like passwords.
 */
void purple_utf16_wipe(gunichar2 *str);


/**************************************************************************/
/* URI/URL Functions                                                      */
/**************************************************************************/

void purple_got_protocol_handler_uri(const char *uri);

/**
 * purple_url_decode:
 * @str: The string to translate.
 *
 * Decodes a URL into a plain string.
 *
 * This will change hex codes and such to their ascii equivalents.
 *
 * Returns: The resulting string.
 */
const char *purple_url_decode(const char *str);

/**
 * purple_url_encode:
 * @str: The string to translate.
 *
 * Encodes a URL into an escaped string.
 *
 * This will change non-alphanumeric characters to hex codes.
 *
 * Returns: The resulting string.
 */
const char *purple_url_encode(const char *str);

/**
 * purple_email_is_valid:
 * @address: The email address to validate.
 *
 * Checks if the given email address is syntactically valid.
 *
 * Returns: True if the email address is syntactically correct.
 */
gboolean purple_email_is_valid(const char *address);

/**
 * purple_ip_address_is_valid:
 * @ip: The IP address to validate.
 *
 * Checks if the given IP address is a syntactically valid IPv4 or
 * IPv6 address.
 * If you specifically want to check for an IPv4 address use
 * purple_ipv4_address_is_valid(), or for an IPv6 address use
 * purple_ipv6_address_is_valid().
 *
 * Returns: True if the IP address is syntactically correct.
 */
gboolean purple_ip_address_is_valid(const char *ip);

/**
 * purple_ipv4_address_is_valid:
 * @ip: The IP address to validate.
 *
 * Checks if the given IP address is a syntactically valid IPv4 address.
 *
 * Returns: True if the IP address is syntactically correct.
 */
gboolean purple_ipv4_address_is_valid(const char *ip);

/**
 * purple_ipv6_address_is_valid:
 * @ip: The IP address to validate.
 *
 * Checks if the given IP address is a syntactically valid IPv6 address.
 *
 * Returns: True if the IP address is syntactically correct.
 */
gboolean purple_ipv6_address_is_valid(const char *ip);

/**
 * purple_uri_list_extract_uris:
 * @uri_list: An uri-list in the standard format.
 *
 * This function extracts a list of URIs from the a "text/uri-list"
 * string.  It was "borrowed" from gnome_uri_list_extract_uris
 *
 * Returns: (element-type utf8): A GList containing strings allocated with
 *          g_malloc that have been split from uri-list.
 */
GList *purple_uri_list_extract_uris(const gchar *uri_list);

/**
 * purple_uri_list_extract_filenames:
 * @uri_list: A uri-list in the standard format.
 *
 * This function extracts a list of filenames from a
 * "text/uri-list" string.  It was "borrowed" from
 * gnome_uri_list_extract_filenames
 *
 * Returns: (element-type utf8): A GList containing strings allocated with
 *          g_malloc that contain the filenames in the uri-list. Note that
 *          unlike the purple_uri_list_extract_uris() function, this will
 *          discard any non-file uri from the result value.
 */
GList *purple_uri_list_extract_filenames(const gchar *uri_list);

/**
 * purple_uri_escape_for_open:
 * @unescaped: The unescaped URI.
 *
 * This function escapes any characters that might be interpreted by the shell
 * when executing a program to open a URI on some systems.
 *
 * Returns: A newly allocated string with any shell metacharacters replaced
 *          with their escaped equivalents.
 */
char *purple_uri_escape_for_open(const char *unescaped);

/**************************************************************************
 * UTF8 String Functions
 **************************************************************************/

/**
 * purple_utf8_try_convert:
 * @str: The source string.
 *
 * Attempts to convert a string to UTF-8 from an unknown encoding.
 *
 * This function checks the locale and tries sane defaults.
 *
 * Returns: The UTF-8 string, or %NULL if it could not be converted.
 */
gchar *purple_utf8_try_convert(const char *str);

/**
 * purple_utf8_salvage:
 * @str: The source string.
 *
 * Salvages the valid UTF-8 characters from a string, replacing any
 * invalid characters with a filler character (currently hardcoded to
 * '?').
 *
 * Returns: A valid UTF-8 string.
 */
gchar *purple_utf8_salvage(const char *str);

/**
 * purple_utf8_strip_unprintables:
 * @str: A valid UTF-8 string.
 *
 * Removes unprintable characters from a UTF-8 string. These characters
 * (in particular low-ASCII characters) are invalid in XML 1.0 and thus
 * are not allowed in XMPP and are rejected by libxml2 by default.
 *
 * The returned string must be freed by the caller.
 *
 * Returns: A newly allocated UTF-8 string without the unprintable characters.
 */
gchar *purple_utf8_strip_unprintables(const gchar *str);

/**
 * purple_gai_strerror:
 * @errnum: The error code.
 *
 * Return the UTF-8 version of #gai_strerror. It calls #gai_strerror
 * then converts the result to UTF-8. This function is analogous to
 * g_strerror().
 *
 * Returns: The UTF-8 error message.
 */
const gchar *purple_gai_strerror(gint errnum);

/**
 * purple_utf8_strcasecmp:
 * @a: The first string.
 * @b: The second string.
 *
 * Compares two UTF-8 strings case-insensitively.  This comparison is
 * more expensive than a simple g_utf8_collate() comparison because
 * it calls g_utf8_casefold() on each string, which allocates new
 * strings.
 *
 * Returns: -1 if @a is less than @b.
 *           0 if @a is equal to @b.
 *           1 if @a is greater than @b.
 */
int purple_utf8_strcasecmp(const char *a, const char *b);

/**
 * purple_utf8_has_word:
 * @haystack: The string to search in.
 * @needle:   The substring to find.
 *
 * Case insensitive search for a word in a string. The needle string
 * must be contained in the haystack string and not be immediately
 * preceded or immediately followed by another alpha-numeric character.
 *
 * Returns: TRUE if haystack has the word, otherwise FALSE
 */
gboolean purple_utf8_has_word(const char *haystack, const char *needle);

/**
 * purple_message_meify:
 * @message: The message to check
 * @len:     The message length, or -1
 *
 * Checks for messages starting (post-HTML) with "/me ", including the space.
 *
 * Returns: TRUE if it starts with "/me ", and it has been removed, otherwise
 *         FALSE
 */
gboolean purple_message_meify(char *message, gssize len);

/**
 * purple_text_strip_mnemonic:
 * @in:  The string to strip
 *
 * Removes the underscore characters from a string used identify the mnemonic
 * character.
 *
 * Returns: The stripped string
 */
char *purple_text_strip_mnemonic(const char *in);

/**
 * purple_unescape_filename:
 * @str: The string to translate.
 *
 * Does the reverse of purple_escape_filename
 *
 * This will change hex codes and such to their ascii equivalents.
 *
 * Returns: The resulting string.
 */
const char *purple_unescape_filename(const char *str);

/**
 * purple_escape_filename:
 * @str: The string to translate.
 *
 * Escapes filesystem-unfriendly characters from a filename
 *
 * Returns: The resulting string.
 */
const char *purple_escape_filename(const char *str);

/**
 * purple_escape_js:
 * @str: The string to escape.
 *
 * Escapes javascript-unfriendly substrings from a string.
 *
 * Returns: The javascript-safe string (must be g_free'd after use).
 */
gchar * purple_escape_js(const gchar *str);

/**
 * purple_restore_default_signal_handlers:
 *
 * Restore default signal handlers for signals which might reasonably have
 * handlers. This should be called by a fork()'d child process, since child processes
 * inherit the handlers of the parent.
 */
void purple_restore_default_signal_handlers(void);

/**
 * purple_get_host_name:
 *
 * Gets the host name of the machine. If it not possible to determine the
 * host name, "localhost" is returned
 *
 * Returns: The hostname
 */
const gchar *purple_get_host_name(void);

/**
 * purple_uuid_random:
 *
 * Returns a type 4 (random) UUID
 *
 * Returns: A UUID, caller is responsible for freeing it
 */
gchar *purple_uuid_random(void);

/**
 * purple_callback_set_zero:
 * @data: A pointer to variable, which should be set to NULL.
 *
 * Sets given pointer to NULL.
 *
 * Function designed to be used as a GDestroyNotify callback.
 */
void purple_callback_set_zero(gpointer data);

/**
 * purple_value_new:
 * @type:  The type of data to be held by the GValue
 *
 * Creates a new GValue of the specified type.
 *
 * Returns:  The created GValue
 */
GValue *purple_value_new(GType type);

/**
 * purple_value_dup:
 * @value:  The GValue to duplicate
 *
 * Duplicates a GValue.
 *
 * Returns:  The duplicated GValue
 */
GValue *purple_value_dup(GValue *value);

/**
 * purple_value_free:
 * @value:  The GValue to free.
 *
 * Frees a GValue.
 */
void purple_value_free(GValue *value);

/**
 * purple_http_digest_calculate_session_key:
 * @algorithm:    The hash algorithm to use
 * @username:     The username provided by the user
 * @realm:        The authentication realm provided by the server
 * @password:     The password provided by the user
 * @nonce:        The nonce provided by the server
 * @client_nonce: The nonce provided by the client
 *
 * Calculates a session key for HTTP Digest authentation
 *
 * See RFC 2617 for more information.
 *
 * Returns: The session key, or %NULL if an error occurred.
 */
gchar *purple_http_digest_calculate_session_key(
		const gchar *algorithm, const gchar *username,
		const gchar *realm, const gchar *password,
		const gchar *nonce, const gchar *client_nonce);

/**
 * purple_http_digest_calculate_response:
 * @algorithm:         The hash algorithm to use
 * @method:            The HTTP method in use
 * @digest_uri:        The URI from the initial request
 * @qop:               The "quality of protection"
 * @entity:            The entity body
 * @nonce:             The nonce provided by the server
 * @nonce_count:       The nonce count
 * @client_nonce:      The nonce provided by the client
 * @session_key:       The session key from purple_http_digest_calculate_session_key()
 *
 * Calculate a response for HTTP Digest authentication
 *
 * See RFC 2617 for more information.
 *
 * Returns: The hashed response, or %NULL if an error occurred.
 */
gchar *purple_http_digest_calculate_response(
		const gchar *algorithm, const gchar *method,
		const gchar *digest_uri, const gchar *qop,
		const gchar *entity, const gchar *nonce,
		const gchar *nonce_count, const gchar *client_nonce,
		const gchar *session_key);

G_END_DECLS

#endif /* _PURPLE_UTIL_H_ */