summaryrefslogtreecommitdiffstats
path: root/doc/iksemel.texi
blob: fd683e2685ddeb31c1098507dd611bd6adf64fde (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
\input texinfo @c -*-texinfo-*-
@c %**start of header
@setfilename iksemel
@setcontentsaftertitlepage
@settitle Iksemel Programmers Manual
@set VERSION 1.2
@c %**end of header

@titlepage
@title iksemel programmers manual
@subtitle A tutorial and API reference for the iksemel library @value{VERSION}
@page
@vskip 0pt plus 1filll
Copyright @copyright{} 2001-2003 G@"urer @"Ozen

This is a free document; 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, or
(at your option) any later version.You may obtain a copy of the
GNU General Public License from the Free Software Foundation
by visiting their Web site or by writing to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA.

@end titlepage

@ifinfo
@node Top, , , (dir)
@top iksemel Programmers Manual

Copyright @copyright{} 2001-2003 G@"urer @"Ozen

This is a free document; 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, or
(at your option) any later version.You may obtain a copy of the
GNU General Public License from the Free Software Foundation
by visiting their Web site or by writing to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA.

@menu
* Introduction::

* Tutorials::

* Development::

* Datatype Index::

* Function Index::
@end menu
@end ifinfo

@node Introduction, Tutorials, ,Top
@chapter Introduction

iksemel is an XML (eXtensible Markup Language) parser library
designed for Jabber applications. It is coded in ANSI C for POSIX
compatible environments, thus highly portable. It is free software
released under the GNU Lesser General Public License.

The purprose of this manual is to tell you how to use the facilities
of the iksemel library. Manual is written with the assumption that you
are familiar with the C programming language, basic programming
concepts, XML and Jabber protocol.

@section Compiling the Library

You need to install MinGW (@url{http://mingw.org}) under Windows to be able
to compile iksemel. Although not tested by the author, Cygwin should
work equally well.

Library can be built with:

@example
./configure
make
@end example

If you want to make a self test:

@example
make test
@end example

Now you can install it with:

@example
make install
@end example


@section Using iksemel in Applications

You need to include @file{iksemel.h} file in your source to access library API.
You can do this with:

@code{#include "iksemel.h"}

Now you can use iksemel functions and compile your source. In able to link
your compiled object files and generate your executable program, you have to
link with iksemel library. This can be done with:

@example
gcc -o myprg src1.o src2.o src3.o -liksemel
@end example

iksemel registers itself with pkg-config while installing, so if you are using
autotools in your program, you can simply check the availability of iksemel
and configure your build process accordingly with:

@example
PKG_CHECK_MODULES(IKSEMEL,iksemel,,exit)
@end example

This would result in IKSEMEL_LIBS and IKSEMEL_CFLAGS substitution variables
set to correct values.

@node Tutorials,Development,Introduction,Top
@chapter Tutorials

@ifinfo
@menu
* Parsing an XML Document::

* Working with XML Trees::

* XML Streams::

* Writing a Jabber Client::

* Utility Functions::
@end menu
@end ifinfo


@comment ============================================================
@node Parsing an XML Document,Working with XML Trees,,Tutorials
@section Parsing an XML Document

iksemel parser sequentally processes the XML document. Each encountered XML
element (i.e. tags, character data, comments, processing instructions, etc.)
is reported to your application by calling the hook functions you have provided.
This type of interface is called SAX (serial access) interface.

@tindex iksparser
Parser stores its state in a small structure. This structure is referenced by
@code{iksparser} type, and managed with following functions:

@deftypefun iksparser* iks_sax_new (void* @var{user_data}, iksTagHook* @var{tagHook}, iksCDataHook* @var{cdataHook});
This function allocates and initializes a parser structure. If allocation fails,
NULL value is returned. @var{user_data} is passed directly to hook functions.
@end deftypefun

@deftp Typedef iksTagHook
int iksTagHook (void* @var{user_data}, char* @var{name}, char** @var{atts}, int @var{type});

This function is called when a tag parsed. @var{name} is the name of the tag. If tag has
no attributes @var{atts} is NULL, otherwise it contains a null terminated list of
pointers to tag's attributes and their values. If return value isn't @code{IKS_OK},
it is passed immediately to the caller of the @code{iks_parse}.

@var{type} is one of the following:
@table @code
@item IKS_OPEN
Opening tag, i.e. <tag attr='value'>
@item IKS_CLOSE
Closing tag, i.e. </tag>
@item IKS_SINGLE
Standalone tag, i.e. <tag attr='value'/>
@end table
@end deftp

@deftp Typedef iksCDataHook
int iksCDataHook (void* @var{user_data}, char* @var{data}, size_t @var{len});

@var{data} is a pointer to the character data. Encoding is UTF-8 and it isn't terminated
with a null character. Size of the data is given with @var{len} in bytes. This function
can be called several times with smaller sized data for a single string. If
return value isn't @code{IKS_OK}, it is passed immediately to the caller of the
@code{iks_parse}.
@end deftp

@deftypefun int iks_parse (iksparser* @var{prs}, char *@var{data}, size_t @var{len}, int @var{finish});
You give XML document to the parser with this function. @var{data}
is a @var{len} bytes string. If @var{len} is zero, data must be a null
terminated string.

If @var{finish} value is zero, parser waits for more data later. If you
want to finish parsing without giving data, call it like:
@example
iks_parse (my_parser, NULL, 0, 1);
@end example

You should check the return value for following conditions:
@table @code
@item IKS_OK
There isn't any problem.
@item IKS_NOMEM
Not enough memory.
@item IKS_BADXML
Document is not well-formed.
@item IKS_HOOK
Your hook decided that there is an error.
@end table
@end deftypefun

@deftypefun void iks_parser_delete (iksparser* @var{prs});
This function frees parser structure and associated data.
@end deftypefun

Now we have learned how to create and use a sax parser. Lets parse a simple
XML document. Write following code into a @file{test.c} file.

@smallexample
#include <stdio.h>
#include <iksemel.h>

int pr_tag (void *udata, char *name, char **atts, int type)
@{
    switch (type) @{
        case IKS_OPEN:
            printf ("TAG <%s>\n", name);
            break;
        case IKS_CLOSE:
            printf ("TAG </%s>\n", name);
            break;
        case IKS_SINGLE:
            printf ("TAG <%s/>\n", name);
            break;
    @}
    if (atts) @{
        int i = 0;
        while (atts[i]) @{
            printf ("  ATTRIB %s='%s'\n", atts[i], atts[i+1]);
            i += 2;
        @}
    @}
    return IKS_OK;
@}

enum ikserror pr_cdata (void *udata, char *data, size_t len)
@{
    int i;
    printf ("CDATA [");
    for (i = 0; i < len; i++)
        putchar (data[i]);
    printf ("]\n");
    return IKS_OK;
@}

int main (int argc, char *argv[])
@{
    iksparser *p;
    p = iks_sax_new (NULL, pr_tag, pr_cdata);
    switch (iks_parse (p, argv[1], 0, 1)) @{
        case IKS_OK:
            puts ("OK");
            break;
        case IKS_NOMEM:
            puts ("Not enough memory");
            exit (1);
        case IKS_BADXML:
            puts ("XML document is not well-formed");
            exit (2);
        case IKS_HOOK:
            puts ("Our hooks didn't like something");
            exit (2);
    @}
    iks_parser_delete (p);
    return 0;
@}
@end smallexample

Now compile and test it with:

@example
gcc -o test test.c -liksemel
./test "<test>Hello<br/>World!</test>"
./test "<lala a='12' b='42'/>"
@end example

@heading Error Handling

XML standart states that once an error is detected, the processor must not continue
normal processing (i.e. it must not pass character data or markup information to
the application). So iksemel stops processing immediately when it encounters a
syntax error, or one of your hook functions return any one value than @code{IKS_OK},
and @code{iks_parse} function returns with the error code.

Since it is useful for debugging, iksemel provides functions to get position of
the error. Position is usually at the starting character for syntax errors. Since
your hooks are called after whole element (i.e. markup or character data) is
passed, position is at the end of the erroneous element for @code{IKS_HOOK} errors.

@deftypefun {unsigned long} iks_nr_bytes (iksparser* @var{prs});
Returns how many number of bytes parsed.
@end deftypefun

@deftypefun {unsigned long} iks_nr_lines (iksparser* @var{prs});
Returns how many number of lines parsed.
@end deftypefun

If you want to parse another document with your parser again, you should use
the following function to reset your parser.

@deftypefun void iks_parser_reset (iksparser* @var{prs});
Resets the parser's internal state.
@end deftypefun


@comment ============================================================
@node Working with XML Trees,XML Streams,Parsing an XML Document,Tutorials
@section Working with XML Trees

SAX interface uses very little memory, but it forces you to access XML
documents sequentally. In many cases you want to keep a tree like
representation of XML document in memory and want to access and
modify its content randomly.

iksemel provides functions for efficiently creating such trees either
from documents or programmaticaly. You can access and modify this
tree and can easily generate a new XML document from the tree.

This is called DOM (Document Object Model) interface.

@ifinfo
@menu
* Memory Management::

* Creating a Tree::

* Accessing the Tree::

* Converting a Tree into an XML Document::

* Parsing an XML Document into a Tree::
@end menu
@end ifinfo


@comment ============================================================
@node Memory Management,Creating a Tree,,Working with XML Trees
@subsection Memory Management

Since keeping whole document content uses a lot of memory and requires
many calls to OS's memory allocation layer, iksemel uses a simple object
stack system for minimizing calls to the @code{malloc} function and releasing
all the memory associated with a tree in a single step.

A parsed XML tree contains following objects:
@table @samp
@item Nodes
These are basic blocks of document. They can contain a tag, attribute pair
of a tag, or character data. Tag nodes can also contain other nodes as
children. Node structure has a small fixed size depending on the node type.
@item Names
Names of tags and attributes. They are utf-8 encoded small strings.
@item Character Data
They are similar to names but usually much bigger.
@end table

iksemel's object stack has two separate areas for keeping these data objects.
Meta chunk contains all the structures and aligned data, while the data chunk
contains strings. Each chunk starts with a choosen size memory block, then
when necessary more blocks allocated for providing space. Unless there is a big
request, each block is double the size of the previous block, thus real memory
needs are quickly reached without allocating too many blocks, or wasting
memory with too big blocks.

@deftp Typedef ikstack
This is a structure defining the object stack. Its fields are private
and subject to change with new iksemel releases.
@end deftp

@deftypefun {ikstack *} iks_stack_new (size_t @var{meta_chunk}, size_t @var{data_chunk});
Creates an object stack. @var{meta_chunk} is the initial size of the
data block used for structures and aligned data. @var{data_chunk} is
the initial size of the data block used for strings. They are both in byte units.

These two initial chunks and a small object stack structure is allocated in
one @code{malloc} call for optimization purproses.
@end deftypefun

@deftypefun {void *} iks_stack_alloc (ikstack * @var{stack}, size_t @var{size});
Allocates @var{size} bytes of space from the object stack's meta chunk.
Allocated space is aligned on platform's default alignment boundary and
isn't initialized. Returns a pointer to the space, or NULL if there isn't enough
space available and allocating a new block fails.
@end deftypefun

@deftypefun {void *} iks_stack_strdup (ikstack * @var{stack}, const char * @var{src}, size_t @var{len});
Copies given string @var{src} into the object stack's data chunk. Returns a
pointer to the new string, or NULL if there isn't enough space in the stack.
If @var{len} is zero string must be null terminated.
@end deftypefun

@deftypefun void iks_stack_delete (ikstack * @var{stack});
Gives all memory associated with object stack to the system.
@end deftypefun

Since character data sections are usually parsed in separate blocks,
a growable string implementation is necessary for saving memory.

@deftypefun {char *} iks_stack_strcat (ikstack *@var{stack}, char *@var{old}, size_t @var{old_len}, const char *@var{src}, size_t @var{src_len});
This function appends the string @var{src} to the string @var{old} in the
stack's data chunk. If  @var{old} is NULL it behaves like @code{iks_stack_strdup}.
Otherwise @var{old} has to be a string created with @code{iks_stack_strdup}
or @code{iks_stack_strcat} functions.

If @var{old_len} or @var{src_len} is zero, corresponding string must be null
terminated.

Since string can be moved into another block of the data chunk, you must use the
returned value for new string, and must not reference to @var{old} anymore.
Return value can be NULL if there isn't enough space in stack, and allocating a
new block fails.
@end deftypefun


@comment ============================================================
@node Creating a Tree,Accessing the Tree,Memory Management,Working with XML Trees
@subsection Creating a Tree

@deftp Typedef iks
This is a structure defining a XML node. Its fields are private and only
accessed by following functions.
@end deftp

@deftypefun iks* iks_new (const char *@var{name});
Creates an object stack and creates a IKS_TAG type of node with given
tag name inside the stack. Tag name is also copied into the stack.
Returns the node pointer, or NULL if there isn't enough memory.
@end deftypefun

@deftypefun iks* iks_new_within (const char *@var{name}, ikstack* @var{stack});
Creates a IKS_TAG type of node with the given tag name. Node and tag
name is allocated inside the given object stack. Returns the node
pointer, or NULL if there isn't enough memory.
@end deftypefun

@deftypefun iks* iks_insert (iks *@var{x}, const char *@var{name});
Creates a IKS_TAG type of node with the given tag name. Node and tag
name is allocated inside the @var{x} node's object stack and linked
to @var{x} as a child node. Returns the node pointer, or NULL if there
isn't enough memory.
@end deftypefun

@deftypefun iks* iks_insert_cdata (iks* @var{x}, const char* @var{data}, size_t @var{len});
Creates a IKS_CDATA type of node with given character data. Node is
allocated inside the @var{x} node's object stack and linked to @var{x}
as a child node. Data is copied as well. If @var{len} is zero data must
be a null terminated string. Returns the node pointer, or NULL if
there isn't enough memory.
@end deftypefun

@deftypefun iks* iks_insert_attrib (iks* @var{x}, const char* @var{name}, const char* @var{value});
Creates a IKS_ATTRIBUTE type of node with given attribute name and the
value. Node is allocated inside the @var{x} node's object stack and
linked to @var{x} as an attribute node. Attribute name and value is
copied as well. Returns the node pointer, or NULL if there isn't
enough memory.

Reinserting another value with same attribute name changes an attribute's
value. If @var{value} is NULL, attribute is removed from the tag.
@end deftypefun

@deftypefun iks* iks_insert_node (iks* @var{x}, iks* @var{y});
Links node @var{y} to node @var{x} as a child node. Nodes are not copied
between object stacks, be careful.
@end deftypefun

@deftypefun void iks_hide (iks *@var{x});
Changes the links of the other nodes so that @var{x} becomes invisible.
It stays in the same object stack with neighbour nodes, be careful.
@end deftypefun

@deftypefun void iks_delete (iks *@var{x});
Frees the object stack of the node @var{x}.
@end deftypefun

Now lets create a tree representation of following XML document:

@example
<message type='chat' from='bob@@bd.com'>
<subject>song lyric</subject><priority>high</priority>
<body>
<em style='underline'>here is the correct version:</em>
i just don't see why i should even care
it's not dark yet, but it's getting there
</body>
</message>
@end example

here is the code:

@example
iks *x, *y, *z;

x = iks_new ("message");
iks_insert_attrib (x, "type", "chat");
iks_insert_attrib (x, "from", "bob@@bd.com");
iks_insert_cdata (x, "\n", 1);
iks_insert_cdata (iks_insert (x, "subject"), "song lyric", 10);
iks_insert_cdata (iks_insert (x, "priority"), "high", 4);
iks_insert_cdata (x, "\n", 1);
y = iks_insert (x, "body");
iks_insert_cdata (y, "\n", 1);
z = iks_insert (y, "em");
iks_insert_attrib (z, "style", "underline");
iks_insert_cdata (z, "here is the correct version", 0);
iks_insert_cdata (y, "\n", 1);
iks_insert_cdata (y, "i just don't see why", 0);
iks_insert_cdata (y, "i should even care\n", 0);
iks_insert_cdata (y, "it's not dark yet,", 0);
iks_insert_cdata (y, "but it's getting there\n", 0);
iks_insert_cdata (x, "\n", 1);
@end example

Notice how newlines are inserted for proper formatting of document. They aren't
necessary for representing data, but they make it easier to read document for
humans.

Also notice how @code{iks_insert} and @code{iks_insert_cdata} chained.

There are also functions for duplicating xml trees. They are:

@deftypefun {iks *} iks_copy (iks* @var{x});
Creates a full copy of the tree in a newly created object stack.
@end deftypefun

@deftypefun {iks *} iks_copy_within (iks* @var{x}, ikstack *@var{s});
Creates a full copy of the tree in given object stack.
@end deftypefun

@comment ============================================================
@node Accessing the Tree,Converting a Tree into an XML Document,Creating a Tree,Working with XML Trees
@subsection Accessing a Tree

Basic access functions allow you to move on the tree:

@deftypefun iks* iks_next (iks* @var{x});
@end deftypefun
@deftypefun iks* iks_prev (iks* @var{x});
@end deftypefun
@deftypefun iks* iks_parent (iks* @var{x});
@end deftypefun
@deftypefun iks* iks_child (iks* @var{x});
@end deftypefun
@deftypefun iks* iks_attrib (iks* @var{x});
@end deftypefun

These functions return a pointer to the next, previous, parent, first child,
and first attribute node of the given node @var{x}. If that node doesn't
exist or @var{x} is NULL, a NULL value is returned.

@deftypefun {iks *} iks_root (iks *@var{x});
Returns the topmost parent node of the @var{x}.
@end deftypefun

@deftypefun iks* iks_next_tag (iks* @var{x});
@end deftypefun
@deftypefun iks* iks_prev_tag (iks* @var{x});
@end deftypefun
@deftypefun iks* iks_first_tag (iks* @var{x});
@end deftypefun

These functions return a pointer to the next, previous, first child node
of the given node @var{x}. Only tag nodes are considered, other type
of the nodes are skipped. If such a node doesn't exist or @var{x} is NULL,
a NULL value is returned.

Another group of functions allow you to access specific information and
content of the nodes:

@deftypefun ikstack* iks_stack (iks* @var{x});
Returns the object stack which node @var{x} stays.
@end deftypefun

@deftypefun {enum ikstype} iks_type (iks* @var{x});
Returns the type of the node.

@tindex ikstype
@table @code
@item IKS_TAG
Node is a tag and can contain child nodes and attributes.
@item IKS_CDATA
Node contains character data.
@item IKS_ATTRIBUTE
Node contains an attribute and its value.
@end table
@end deftypefun

@deftypefun char* iks_name (iks* @var{x});
Returns the name of the tag for nodes with the type @var{IKS_TAG}.
Returns an attribute's name for nodes of type IKS_ATTRIBUTE.
@end deftypefun

@deftypefun char* iks_cdata (iks* @var{x});
Returns a pointer to node's character data if available, NULL otherwise.
Returns an attribute's value for nodes of type IKS_ATTRIBUTE.
@end deftypefun

@deftypefun size_t iks_cdata_size (iks *@var{x});
Returns the size of the node's character data in bytes.
@end deftypefun

@deftypefun int iks_has_children (iks *@var{x});
Returns a non-zero value if node @var{x} has a child node.
@end deftypefun

@deftypefun int iks_has_attribs (iks *@var{x});
Returns a non-zero value if node @var{x} has attributes.
@end deftypefun

Last group of the functions simplifies finding and accessing the content
of a specific node:

@deftypefun iks* iks_find (iks *@var{x}, const char *@var{name});
Searches a IKS_TAG type of node with @var{name} as tag name in child
nodes of @var{x}. Returns a pointer to the node if found, NULL otherwise.
@end deftypefun

@deftypefun char* iks_find_cdata (iks* @var{x}, const char* @var{name});
Searches a IKS_TAG type of node with @var{name} as tag name in child
nodes of @var{x}. Returns a pointer to the character data of the node's
first child node if found, NULL otherwise.
@end deftypefun

@deftypefun char* iks_find_attrib (iks* @var{x}, const char* @var{name});
Searches an attribute with given name in attributes of the @var{x}.
Returns a pointer to attribute value if found, NULL otherwise.
@end deftypefun

@deftypefun {iks *} iks_find_with_attrib (iks *@var{x}, const char *@var{tagname}, const char *@var{attrname}, const char *@var{value});
Searches for a child tag of @var{x} which has an attribute with name
@var{attrname} and value @var{value}. If @var{tagname} isn't NULL,
name of the tag must also match. Returns a pointer to the node if found,
NULL otherwise.
@end deftypefun

Here is an example which demonstrates accessing file names in a fictitious
XML playlist file:

@example
<playlist>
    <item type='mpg'>
        <name>/home/madcat/download/matrix_rev_trailer.mpg</name>
        <duration>1:17</duration>
    </item>
    <item type='rm'>
        <name>/home/madcat/anim/clementine_ep1.rm</name>
        <duration>22:00</duration>
    </item>
    <item type='avi'>
        <name>/home/madcat/anim/futurama/ep101.avi</name>
        <subtitle>/home/madcat/subs/futurama/ep101.txt</subtitle>
        <duration>30:00</duration>
    </item>
    <repeat/>
    <fullscreen/>
    <noui/>
</playlist>
@end example

and here is the code:

@example
#include <stdio.h>
#include <iksemel.h>

int main (int argc, char *argv[])
@{
    iks *x, *y;
    int e;

    if (argc < 2) @{
        printf ("usage: %s <playlistfile>", argv[0]);
        return 0;
    @}
    e = iks_load (argv[1], &x);
    if (e != IKS_OK) @{
    	printf ("parse error %d\n", e);
        return 1;
    @}
    if (iks_find (x, "repeat")) puts ("repeat mode enabled");
    y = iks_child (x);
    while (y) @{
        if (iks_type (y) == IKS_TAG
            && strcmp (iks_name (y), "item") == 0) @{
	        printf ("Filename: [%s]\n", iks_find_cdata (y, "name"));
        @}
        y = iks_next (y);
     @}
    iks_delete (x);
    return 0;
@}
@end example


@comment ============================================================
@node Converting a Tree into an XML Document,Parsing an XML Document into a Tree,Accessing the Tree,Working with XML Trees
@subsection Converting a Tree to an XML Document

There is a function for converting given XML tree into a null terminated string.

@deftypefun {char *} iks_string (ikstack* @var{stack}, iks* @var{x});
Converts given tree into a string. String is created inside the given object
stack. Returns a pointer to the string, or NULL if there isn't enough memory
available.

If @var{stack} is NULL, string is created inside an @code{iks_malloc}ed buffer.
You can free it later with @code{iks_free} function.
@end deftypefun

Here is an example which builds a tree and print it.

@example
iks *x;
char *t;

x = iks_new ("test");
iks_insert_cdata (iks_insert (x, "a"), "1234", 4);
iks_insert (x, "br");
iks_insert_cdata (x, "1234", 4);
t = iks_string (iks_stack (x), x);
puts (t);
iks_delete (x);
@end example


@comment ============================================================
@node Parsing an XML Document into a Tree,,Converting a Tree into an XML Document,Working with XML Trees
@subsection Parsing a Document into a Tree

If you want to automatically convert an XML document into a tree, you can use
iksemel's DOM parser. It is created with following function:

@deftypefun iksparser* iks_dom_new (iks **@var{iksptr});
Creates a DOM parser. A pointer to the created XML tree is put into the
variable pointed by @var{iksptr}. Returns a pointer to the parser, or NULL
is there isn't enough memory.
@end deftypefun

Usage is same as SAX parser. You feed the data with @code{iks_parse}, and if
there isn't an error, you can access to your tree from variable @code{*iksptr}.

Here is a simple example:

@example
iks *x;
iksparser *p;

p = iks_dom_new (&x);
if (IKS_OK != iks_parse (p, "<a>bcd</a>", 9, 1)) @{
    puts ("parse error");
@}
/* x is useable after that point */

/* this will print 'bcd' */
printf ("%s\n", iks_cdata (iks_child (x)));
@end example

If you know the size of the file ahead, or you have an approximate idea,
you can tell this to the dom parser for choosing a better memory allocation
strategy. Here is the function for this.

@deftypefun void iks_set_size_hint (iksparser *@var{prs}, size_t @var{approx_size});
Parser @var{prs} must be a dom type parser. @var{approx_size} is the
expected size of the xml document. Parser chooses its chunk size
based on this information. Helps performance while processing big files.
@end deftypefun

If you already have your XML document in memory, you can simply parse
it with:

@deftypefun {iks *} iks_tree (const char *@var{xml_str}, size_t @var{len}, int *@var{err});
This function parses the buffer pointed by @var{xml_str}. If @var{len} is zero
buffer is considered as a null terminated utf8 string. Returns the parsed tree,
or NULL if there is an error. If @var{err} is not NULL, actual error code (returned
by iks_parse) is put there.
@end deftypefun

Most of the times you want to load your configuration (or similar) files directly
into trees. iksemel provides two functions to greatly simplify this:

@deftypefun int iks_load (const char *@var{fname}, iks **@var{xptr});
Loads the XML file. Tree is placed into the variable pointed by @var{xptr}.
@end deftypefun

@deftypefun int iks_save (const char *@var{fname}, iks *@var{x});
Converts tree @var{x} into a string and saves to the file.
@end deftypefun

Both functions return same error codes as @code{iks_parse}. Some additional
error codes are defined for indicating file problems. They are:

@table @code
@item IKS_FILE_NOFILE
A file with the given name doesn't exist.
@item IKS_FILE_NOACCESS
Cannot open file. Possibly a permission problem.
@item IKS_FILE_RWERR
Read or write operation failed.
@end table

Here is a simple example which parses a file and saves it into another:

@example
iks *x;

if (IKS_OK != iks_load ("file1.xml", &x)) @{
    puts ("loading error");
@}
if (IKS_OK != iks_save ("file2.xml", x)) @{
    puts ("saving error");
@}
@end example


@comment ============================================================
@node XML Streams,Writing a Jabber Client,Working with XML Trees,Tutorials
@section XML Streams

XML streams function as containers for any XML chunks sent asynchronously
between network endpoints. They are used for asyncronously exchanging
relatively small payload of structured information between entities.

A stream is initiated by one of hosts connecting to the other, and sending a
<stream:stream> tag. Receiving entity replies with a second XML stream
back to the initiating entity within the same connection. Each unit of
information is send as a direct child tag of the <stream:stream> tag.
Stream is closed with </stream:stream>.

XML streams use a subset of XML. Specifically they should not contain
processing instructions, non-predefined entities, comments, or DTDs.

Jabber protocol uses XML streams for exchanging messages, presence
information, and other information like authorization, search, time and
version queries, protocol extensions.

iksemel provides you a stream parser, which automatically handles connection
to the server, and calls your hook function with incoming information
parsed and converted to an XML tree.

You can create such a parser with:

@deftypefun iksparser* iks_stream_new (char* @var{name_space}, void* @var{user_data}, iksStreamHook* @var{streamHook});
Allocates and initalizes a stream parser. @var{name_space} indicates the
stream type, jabber clients use "jabber:client" namespace. @var{user_data}
is passed directly to your hook function.
@end deftypefun

@deftp Typedef iksStreamHook
int iksStreamHook (void* @var{user_data}, int @var{type}, iks* @var{node});

Depending on the value of the @var{type}, @var{node} contains:
@table @code
@item IKS_NODE_START
Got the <stream:stream> tag, namespace, stream id and other information
is contained in the @var{node}.
@item IKS_NODE_NORMAL
A first level child of the <stream:stream> tag is received. @var{node} contains
the parsed tag. If you are connected to a jabber server, you can get <message>,
<presence>, or <iq> tags.
@item IKS_NODE_ERROR
Got a <stream:error> tag, details can be accessed from @var{node}.
@item IKS_NODE_STOP
</stream:stream> tag is received or connection is closed, @var{node} is @code{NULL}.
@end table

Freeing the node with @code{iks_delete} is up to you.
@end deftp

You can manually feed this parser with @code{iks_parse} function, but using
iksemel's connection facilities is easier for most of the cases.

This functions return @code{IKS_OK} for success. Error codes of @code{iks_parse}
are used in same manner. Following additional codes are defined for
network related problems:

@table @code
@item IKS_NET_NODNS
Hostname lookup failed. Possible reasons: hostname is incorrect,
you are not online, your dns server isn't accessible.
@item IKS_NET_NOSOCK
Socket cannot created.
@item IKS_NET_NOCONN
Connection attemp failed. Possible reasons: host is not an XML stream
server, port number is wrong, server is busy or closed for the moment.
@item IKS_NET_RWERR
@code{send} or @code{recv} call is failed when attempting to exchange
the data with the server. You should close the connection with @code{iks_disconnect}
after getting this error from data transfer functions.
@end table

@deftypefun int iks_connect_tcp (iksparser *@var{prs}, const char *@var{server}, int @var{port});
This function connects the parser to a server and sends stream header for you.
@var{server} is the host name of the server and @var{port} is the tcp port
number which server is listening to. You can use @code{IKS_JABBER_PORT}
macro for the default jabber client port (5222).
@end deftypefun

@deftypefun int iks_connect_fd (iksparser *@var{prs}, int @var{fd});
Attaches parser to an already opened connection. @var{fd} is the socket
descriptor. Note that @code{iks_disconnect} doesn't close the socket
for this kind of connection, opening and closing of the socket is up to your
application. Stream header is not sent automatically. You can use
@code{iks_send_header} function for sending it.
@end deftypefun

@deftypefun void iks_disconnect (iksparser *@var{prs});
Closes connection to the server, and frees connection resources.
@end deftypefun

After successfully connecting to a server, you can use following functions
for exchanging information with server.

@deftypefun int iks_recv (iksparser* @var{prs}, int @var{timeout});
If @var{timeout} is @code{-1}, waits until some data arrives from server,
and process the data. Your stream hook can be called if a complete
chunk is arrived.

If @var{timeout} is a positive integer, @code{iks_recv} returns if no data
arrives for @var{timeout} seconds.

If @var{timeout} is zero, @code{iks_recv} checks if there is any data
waiting at the network buffer, and returns without waiting for data.
@end deftypefun

@deftypefun int iks_fd (iksparser* @var{prs});
Returns the file descriptor of the connected socket. You can use this in
your @code{select} function or some other input loop to act whenever
some data from the server arrives. This value of only valid between
a successful @code{iks_connect_tcp} and @code{iks_disconnect}.
@end deftypefun

@deftypefun int iks_send (iksparser* @var{prs}, iks* @var{x});
Converts the tree given in @var{x} to a string, and sends to the server.
String is created inside the object stack of @var{x}.
@end deftypefun

@deftypefun int iks_send_raw (iksparser* @var{prs}, char* @var{xmlstr});
Sends the string given in @var{xmlstr} to the server.
@end deftypefun

@deftypefun int iks_send_header (iksparser *@var{prs}, char *@var{to});
Sends the stream header. @var{to} is the name of the server.
Normally @code{iks_connect_tcp} function calls this for you. This
is only useful if you are using @code{iks_connect_fd}.
@end deftypefun

Sometimes it is useful to log incoming and outgoing data to your parser
for debugging your applications. iksemel provides a logging facility for you.

@deftypefun void iks_set_log_hook (iksparser* @var{prs}, iksLogHook* @var{logHook});
Sets the log function for your stream parser. You can't use this function
on any other type of parser.
@end deftypefun

@deftp Typedef iksLogHook
void iksLogHook (void* @var{user_data}, const char* @var{data}, size_t @var{size}, int @var{is_incoming});

@var{user_data} is same value which you give with @code{iks_stream_new}.
@var{data} is @var{size} bytes of data. Be very careful that this data may be
coming from other side of the connection and can contain malicius bytes. It isn't
checked by iksemel yet, so you should check it yourself before displaying or
passing to other systems in your application or computer. If @var{is_incoming}
is a non-zero value, data is incoming from server, otherwise it is outgoing to the
server.
@end deftp


@comment ============================================================
@node Writing a Jabber Client,Utility Functions,XML Streams,Tutorials
@section Writing a Jabber Client

@ifinfo
@menu
* Security::

* Packets::

* Packet Filter::

* Creating Common Packets::

@end menu
@end ifinfo

@comment ============================================================
@node Security,Packets,,Writing a Jabber Client
@subsection Security

iksemel supports TLS protocol for encrypted communication and SASL
protocol for authentication. TLS is handled by gnutls library.

@deftypefun int iks_has_tls (void);
If iksemel is compiled with gnutls library, this function returns a non-zero
value indicating you can try encrypted connection with the server.
@end deftypefun

@deftypefun int iks_start_tls (iksparser* @var{prs});
Starts a TLS handshake over already connected parser. Returns IKS_OK or
one of the IKS_NET_ errors. If handshake succeeds you'll get another
stream header from server.
@end deftypefun

@deftypefun int iks_is_secure (iksparser* @var{prs});
Returns a non-zero value if a secure connection is fully established
between server.
@end deftypefun

@deftypefun int iks_start_sasl (iksparser* @var{prs}, enum ikssasltype @var{type}, char* @var{username}, char* @var{pass});
Starts SASL operation.
@end deftypefun

See tools/iksroster.c for a good example.

@comment ============================================================
@node Packets,Packet Filter,Security,Writing a Jabber Client
@subsection Packets

iksemel can parse a jabber XML node and provide you a public packet
structure which contains information like node type and subtype, id,
namespace, sender's jabber id, etc.

This handles a lot of node parsing for you. Packets are also used in
the packet filter subsystem.

@deftypefun {ikspak *} iks_packet (iks *@var{x});
Takes a node from stream and extracts information from it to a packet structure.
Structure is allocated inside the node's object stack.
@end deftypefun

@tindex ikspak
@code{ikspak} structure has following fields:

@table @code
@item iks *x;
This is a pointer to the node.
@item iksid *from;
Sender's jabber id in parsed form. See below for @code{iksid} structure.
@item iks *query;
A pointer to the <query> tag for IQ nodes.
@item char *ns;
Namespace of the content for IQ nodes.
@item char *id;
ID of the node.
@item enum ikspaktype type;
Type of the node. Possible types are:

@table @code
@item IKS_PAK_NONE
Unknown node.
@item IKS_PAK_MESSAGE
Message node.
@item IKS_PAK_PRESENCE
Presence node with presence publishing operation.
@item IKS_PAK_S10N
Presence node with subscription operation.
@item IKS_PAK_IQ
IQ node.
@end table
@item enum iksubtype subtype;
Sub type of the node. Sub types for message nodes:

@table @code
@item IKS_TYPE_NONE
A normal message.
@item IKS_TYPE_CHAT
Private chat message.
@item IKS_TYPE_GROUPCHAT
Multi user chat message.
@item IKS_TYPE_HEADLINE
Message from a news source.
@item IKS_TYPE_ERROR
Message error.
@end table

Sub types for IQ nodes:

@table @code
@item IKS_TYPE_GET
Asks for some information.
@item IKS_TYPE_SET
Request for changing information.
@item IKS_TYPE_RESULT
Reply to get and set requests.
@item IKS_TYPE_ERROR
IQ error.
@end table

Sub types for subscription nodes:

@table @code
@item IKS_TYPE_SUBSCRIBE,
Asks for subscribing to the presence.
@item IKS_TYPE_SUBSCRIBED,
Grants subscription.
@item IKS_TYPE_UNSUBSCRIBE,
Asks for unsubscribing to the presence.
@item IKS_TYPE_UNSUBSCRIBED,
Cancels subscription.
@item IKS_TYPE_ERROR
Presence error.
@end table

Sub types for presence nodes:

@table @code
@item IKS_TYPE_PROBE,
Asks presence status.
@item IKS_TYPE_AVAILABLE,
Publishes entity as available. More information can be found in @code{show} field.
@item IKS_TYPE_UNAVAILABLE
Publishes entity as unavailable. More information can be found in @code{show} field.
@end table
@item enum ikshowtype show;
Presence state for the presence nodes.

@table @code
@item IKS_SHOW_UNAVAILABLE
Entity is unavailable.
@item IKS_SHOW_AVAILABLE
Entity is available.
@item IKS_SHOW_CHAT
Entity is free for chat.
@item IKS_SHOW_AWAY
Entity is away for a short time.
@item IKS_SHOW_XA
Entity is away for a long time.
@item IKS_SHOW_DND
Entity doesn't want to be disturbed.
@end table
@end table

iksemel has two functions to parse and compare jabber IDs.

@deftypefun {iksid *} iks_id_new (ikstack *@var{s}, const char *@var{jid});
Parses a jabber id into its parts. @code{iksid} structure is created inside
the @var{s} object stack.
@end deftypefun

@tindex iksid
@code{iksid} structure has following fields:

@table @code
@item char *user;
User name.
@item char *server;
Server name.
@item char *resource;
Resource.
@item char *partial;
User name and server name.
@item char *full;
User name, server name and resource.
@end table

You can access this fields and read their values. Comparing two parsed jabber
ids can be done with:

@deftypefun int iks_id_cmp (iksid *@var{a}, iksid *@var{b}, int @var{parts});
Compares @var{parts} of @var{a} and @var{b}. Part values are:

@table @code
@item IKS_ID_USER
@item IKS_ID_SERVER
@item IKS_ID_RESOURCE
@end table

@sp 1
You can combine this values with @code{or} operator. Some common combinations
are predefined for you:

@table @code
@item IKS_ID_PARTIAL
@code{IKS_ID_USER | IKS_ID_SERVER}
@item IKS_ID_FULL
@code{IKS_ID_USER | IKS_ID_SERVER | IKS_ID_RESOURCE}
@end table

Return value is @code{0} for equality. If entities are not equal a combination of
part values showing different parts is returned.
@end deftypefun


@comment ============================================================
@node Packet Filter,Creating Common Packets,Packets,Writing a Jabber Client
@subsection Packet Filter

Packet filter handles routing incoming packets to related functions.

@tindex iksfilter
@deftypefun {iksfilter *} iks_filter_new (void);
Creates a new packet filter.
@end deftypefun

@deftypefun void iks_filter_packet (iksfilter *@var{f}, ikspak *@var{pak});
Feeds the filter with given packet. Packet is compared to registered rules and
hook functions of the matching rules are called in most matched to least
matched order.
@end deftypefun

@deftypefun void iks_filter_delete (iksfilter *@var{f});
Frees filter and rules.
@end deftypefun

Rules are created with following function:

@tindex iksrule
@deftypefun {iksrule *} iks_filter_add_rule (iksfilter *@var{f}, iksFilterHook *@var{filterHook}, void *@var{user_data}, @dots{});
Adds a rule to the filter @var{f}. @var{user_data} is passed directly to your
hook function @var{filterHook}.

A rule consist of one or more type and value pairs. Possible types:
@table @code
@item IKS_RULE_ID
Compares @code{char *} value to packet ids.
@item IKS_RULE_FROM
Compares @code{char *} value to packet senders.
@item IKS_RULE_FROM_PARTIAL
Compares @code{char *} value to packet sender. Ignores resource part of jabber id.
@item IKS_RULE_NS
Compares @code{char *} value to namespace of iq packets.
@item IKS_RULE_TYPE
Compares @code{int} value to packet types.
@item IKS_RULE_SUBTYPE
Compares @code{int} value to packet sub types.
@item IKS_RULE_DONE
Terminates the rule pairs.
@end table
@end deftypefun

Here is an example which creates a filter and adds three rules:
@example
iksfilter *f;

f = iks_filter_new ();
iks_filter_add_rule (f, on_msg, NULL,
                     IKS_RULE_TYPE, IKS_PAK_MESSAGE,
		     IKS_RULE_DONE);
iks_filter_add_rule (f, on_auth_result, NULL,
                     IKS_RULE_TYPE, IKS_PAK_IQ,
		     IKS_RULE_SUBTYPE, IKS_TYPE_RESULT,
		     IKS_RULE_ID, "auth",
		     IKS_RULE_DONE);
iks_filter_add_rule (f, on_roster_push, NULL,
                     IKS_RULE_TYPE, IKS_PAK_IQ,
		     IKS_RULE_SUBTYPE, IKS_TYPE_SET,
		     IKS_RULE_NS, "jabber:iq:roster",
		     IKS_RULE_DONE);
@end example

@deftp Typedef iksFilterHook
int iksFilterHook (void *user_data, ikspak *pak);

Your hook is called with your @var{user_data} and matching packet @var{pak}.
You can return two different values from your hook:
@table @code
@item IKS_FILTER_PASS
Packet is forwarded to least matching rules.
@item IKS_FILTER_EAT
Filtering process for the packet ends.
@end table
@end deftp

You can remove the rules with following functions:

@deftypefun void iks_filter_remove_rule (iksfilter *@var{f}, iksrule *@var{rule});
Removes the rule from filter.
@end deftypefun

@deftypefun void iks_filter_remove_hook (iksfilter *@var{f}, iksFilterHook *@var{filterHook});
Remove the rules using @var{filterHook} function from filter.
@end deftypefun


@comment ============================================================
@node Creating Common Packets,,Packet Filter,Writing a Jabber Client
@subsection Creating Common Packets

A usual jabber network traffic contains many similar XML constructs. iksemel
provides several utility functions for creating them. They all generate an XML
tree, so you can add or modify some parts of the tree, and send to server then.

@deftypefun {iks *} iks_make_auth (iksid *@var{id}, const char *@var{pass}, const char *@var{sid});
Creates an authorization packet. @var{id} is your parsed jabber id, and @var{pass}
is your password.

If stream id @var{sid} isn't NULL, SHA1 authentication is used, otherwise password
is attached in plain text. You can learn stream id from @code{IKS_STREAM_START}
packet in your stream hook like this:

@example
char *sid;

if (type == IKS_STREAM_START) @{
    sid = iks_find_attrib (node, "id");
@}
@end example
@end deftypefun

@deftypefun {iks *} iks_make_msg (enum iksubtype @var{type}, const char *@var{to}, const char *@var{body});
Creates a message packet. @var{type} is the message type, @var{to} is jabber id
of the recipient, @var{body} is the message.
@end deftypefun

@deftypefun {iks *} iks_make_s10n (enum iksubtype @var{type}, const char *@var{to}, const char *@var{msg});
Creates a presence packet for subscription operations. @var{type} is operation,
@var{to} is jabber id of the recipient, @var{msg} is a small message for
introducing yourself, or explaning the reason of why you are subscribing or
unsubscribing.
@end deftypefun

@deftypefun {iks *} iks_make_pres (enum ikshowtype @var{show}, const char *@var{status});
Creates a presence packet for publishing your presence. @var{show} is your
presence state and @var{status} is a message explaining why you are not
available at the moment, or what you are doing now.
@end deftypefun

@deftypefun {iks *} iks_make_iq (enum iksubtype @var{type}, const char *@var{xmlns});
Creates an IQ packet. @var{type} is operation type and @var{xmlns} is the
namespace of the content. You usually have to add real content to the <query>
tag before sending this packet.
@end deftypefun


@comment ============================================================
@node Utility Functions,,Writing a Jabber Client,Tutorials
@section Utility Functions

@subsection Memory Utilities

@deftypefun {void *} iks_malloc (size_t @var{size});
@end deftypefun
@deftypefun void iks_free (void *@var{ptr});
@end deftypefun

These are wrappers around ANSI malloc and free functions used by the
iksemel library itself. You can free the output of iks_string (only if you
passed it a NULL stack) with iks_free for example. That is important
if you are using a malloc debugger in your application but not in iksemel
or vice versa.

@comment ============================================================
@subsection String Utilities

@deftypefun {char *} iks_strdup (const char *@var{src});
@end deftypefun
@deftypefun int iks_strcmp (const char *@var{a}, const char *@var{b});
@end deftypefun
@deftypefun int iks_strcasecmp (const char *@var{a}, const char *@var{b});
@end deftypefun
@deftypefun int iks_strncmp (const char *@var{a}, const char *@var{b}, size_t @var{n});
@end deftypefun
@deftypefun int iks_strncasecmp (const char *@var{a}, const char *@var{b}, size_t @var{n});
@end deftypefun
@deftypefun size_t iks_strlen (const char *@var{src});
@end deftypefun

These functions work exactly like their ANSI equivalents except that they allow
NULL values for string pointers. If @var{src} is NULL, iks_strdup and iks_strlen
returns zero. If @var{a} or @var{b} is NULL in string comparisation functions
they return -1.

Their usefulness comes from the fact that they can chained with DOM traversing
functions like this:

@smallexample
if (iks_strcmp (iks_find_attrib (x, "id"), "x1") == 0) count++;
@end smallexample

That example works even x doesn't have an 'id' attribute and iks_find_attrib
returns NULL. So you don't need to use temporary variables in such
situations.

@comment ============================================================
@subsection SHA1 Hash

Secure Hash Algorithm (SHA1) is used in the Jabber authentication
protocol for encoding your password when sending to the server.
This is normally handled by iks_make_auth() function, but if you
want to handle it manually, or if you need a good hash function
for other purproses you can use these functions.

@deftypefun iksha* iks_sha_new (void);
Allocates a structure for keeping calculation values and the state.
@end deftypefun

@deftypefun void iks_sha_reset (iksha *@var{sha});
Resets the state of the calculation.
@end deftypefun

@deftypefun void iks_sha_hash (iksha *@var{sha}, const unsigned char *@var{data}, int @var{len}, int @var{finish});
Calculates the hash value of the given data. If @var{finish} is non
zero, applies the last step of the calculation.
@end deftypefun

@deftypefun void iks_sha_print (iksha *@var{sha}, char *@var{hash});
Prints the result of a finished calculation into the buffer pointed by @var{hash}
in hexadecimal string form. Buffer must be at least 40 bytes long. String
is not null terminated.
@end deftypefun

@deftypefun void iks_sha (const char *@var{data}, char *@var{hash});
Calculates the hash value of @var{data} and prints into @var{hash}.
This is a helper function for simple hash calculations. It calls
other functions for the actual work.
@end deftypefun


@comment ============================================================


@node Development,Datatype Index,Tutorials,Top
@chapter Development

This chapter contains information on plan, procedure and standarts of
iksemel development.

@section Roadmap

There are three main functions iksemel tries to provide to applications:
@itemize @bullet
@item
A generic XML parser with SAX and DOM interfaces.
@item
XML stream client and server functionality.
@item
Utilities for Jabber clients.
@end itemize

Goal of the iksemel is providing these functions while supporting embedded
environments, keeping usage simple, and having a robust implementation.

Some decisions are made to reach this goal:

Code is written in ANSI C with a single dependency on C library. Instead of
using expat or libxml, a simple built-in parser is used. Similarly glib and
gnu only features of glibc (like object stacks) are avoided and built-in
memory and string utilities are used. This may seem like code duplication
but since they are optimized for iksemel and only a few kb in size,
it isn't a big disadvantage.

Code is placed files in a modular fashion, and different modules don't depend
on others' internal details. This allows taking unneeded functionality out when
building for low resource situations.

It is tried to give functions names which are consistent, clear and short.

API is documented with texinfo for high quality printed output and info file
output for fast and simple access during application development. Instead
of using an autogenerated system or simply listing function descriptions,
a task oriented tutorial approach is used.

@section Coding Style

Here is a short list describing preferred coding style for iksemel.
Please keep in mind when sending patches.

@itemize @bullet
@item
Indentation is done with tabs. Aligning is done with spaces.
@item
Placement of braces is K&R style.
@item
Function names are put at the start of line.
@item
Function names are lowercase.
@item
Words of the function names are separated with underscore character.
@item
Structure and variable names are lowercase.
@item
Macro and enumarations names are uppercase.
@item
Exported library API is contained in the single iksemel.h file.
@item
Exported function names start with iks_
@item
Exported structure and type names start with iks
@item
Exported macro and enumaration names start with IKS_
@end itemize

Here is an example:

@smallexample
int
iks_new_func (char *text)
@{
    int i;

    i = an_internal_func (text);
    if (IKS_SOME_VALUE == i) @{
        iks_some_func (text);
        i++;
    @}
    return i;
@}
@end smallexample

@section Resources

@itemize @bullet
@item
RFC 2279, UTF-8 format @url{http://www.ietf.org/rfc/rfc2279.txt}
@item
W3C Recommendation, Extensible Markup Language 1.0 @url{http://www.w3.org/TR/REC-xml}
@item
Annotated XML Specification @url{http://www.xml.com/axml/testaxml.htm}
@item
Jabber Protocol Documents @url{http://www.jabber.org/protocol/}
@end itemize


@comment ============================================================


@node Datatype Index,Function Index,Development,Top
@unnumbered Datatype Index
@printindex tp


@node Function Index,,Datatype Index,Top
@unnumbered Function Index
@printindex fn


@contents
@bye