2
0

iksemel.texi 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534
  1. \input texinfo @c -*-texinfo-*-
  2. @c %**start of header
  3. @setfilename iksemel
  4. @setcontentsaftertitlepage
  5. @settitle Iksemel Programmers Manual
  6. @set VERSION 1.2
  7. @c %**end of header
  8. @titlepage
  9. @title iksemel programmers manual
  10. @subtitle A tutorial and API reference for the iksemel library @value{VERSION}
  11. @page
  12. @vskip 0pt plus 1filll
  13. Copyright @copyright{} 2001-2003 G@"urer @"Ozen
  14. This is a free document; you can redistribute it and/or
  15. modify it under the terms of the GNU General Public License as
  16. published by the Free Software Foundation; either version 2, or
  17. (at your option) any later version.You may obtain a copy of the
  18. GNU General Public License from the Free Software Foundation
  19. by visiting their Web site or by writing to the Free Software
  20. Foundation, Inc., 59 Temple Place - Suite 330, Boston,
  21. MA 02111-1307, USA.
  22. @end titlepage
  23. @ifinfo
  24. @node Top, , , (dir)
  25. @top iksemel Programmers Manual
  26. Copyright @copyright{} 2001-2003 G@"urer @"Ozen
  27. This is a free document; you can redistribute it and/or
  28. modify it under the terms of the GNU General Public License as
  29. published by the Free Software Foundation; either version 2, or
  30. (at your option) any later version.You may obtain a copy of the
  31. GNU General Public License from the Free Software Foundation
  32. by visiting their Web site or by writing to the Free Software
  33. Foundation, Inc., 59 Temple Place - Suite 330, Boston,
  34. MA 02111-1307, USA.
  35. @menu
  36. * Introduction::
  37. * Tutorials::
  38. * Development::
  39. * Datatype Index::
  40. * Function Index::
  41. @end menu
  42. @end ifinfo
  43. @node Introduction, Tutorials, ,Top
  44. @chapter Introduction
  45. iksemel is an XML (eXtensible Markup Language) parser library
  46. designed for Jabber applications. It is coded in ANSI C for POSIX
  47. compatible environments, thus highly portable. It is free software
  48. released under the GNU Lesser General Public License.
  49. The purprose of this manual is to tell you how to use the facilities
  50. of the iksemel library. Manual is written with the assumption that you
  51. are familiar with the C programming language, basic programming
  52. concepts, XML and Jabber protocol.
  53. @section Compiling the Library
  54. You need to install MinGW (@url{http://mingw.org}) under Windows to be able
  55. to compile iksemel. Although not tested by the author, Cygwin should
  56. work equally well.
  57. Library can be built with:
  58. @example
  59. ./configure
  60. make
  61. @end example
  62. If you want to make a self test:
  63. @example
  64. make test
  65. @end example
  66. Now you can install it with:
  67. @example
  68. make install
  69. @end example
  70. @section Using iksemel in Applications
  71. You need to include @file{iksemel.h} file in your source to access library API.
  72. You can do this with:
  73. @code{#include "iksemel.h"}
  74. Now you can use iksemel functions and compile your source. In able to link
  75. your compiled object files and generate your executable program, you have to
  76. link with iksemel library. This can be done with:
  77. @example
  78. gcc -o myprg src1.o src2.o src3.o -liksemel
  79. @end example
  80. iksemel registers itself with pkg-config while installing, so if you are using
  81. autotools in your program, you can simply check the availability of iksemel
  82. and configure your build process accordingly with:
  83. @example
  84. PKG_CHECK_MODULES(IKSEMEL,iksemel,,exit)
  85. @end example
  86. This would result in IKSEMEL_LIBS and IKSEMEL_CFLAGS substitution variables
  87. set to correct values.
  88. @node Tutorials,Development,Introduction,Top
  89. @chapter Tutorials
  90. @ifinfo
  91. @menu
  92. * Parsing an XML Document::
  93. * Working with XML Trees::
  94. * XML Streams::
  95. * Writing a Jabber Client::
  96. * Utility Functions::
  97. @end menu
  98. @end ifinfo
  99. @comment ============================================================
  100. @node Parsing an XML Document,Working with XML Trees,,Tutorials
  101. @section Parsing an XML Document
  102. iksemel parser sequentally processes the XML document. Each encountered XML
  103. element (i.e. tags, character data, comments, processing instructions, etc.)
  104. is reported to your application by calling the hook functions you have provided.
  105. This type of interface is called SAX (serial access) interface.
  106. @tindex iksparser
  107. Parser stores its state in a small structure. This structure is referenced by
  108. @code{iksparser} type, and managed with following functions:
  109. @deftypefun iksparser* iks_sax_new (void* @var{user_data}, iksTagHook* @var{tagHook}, iksCDataHook* @var{cdataHook});
  110. This function allocates and initializes a parser structure. If allocation fails,
  111. NULL value is returned. @var{user_data} is passed directly to hook functions.
  112. @end deftypefun
  113. @deftp Typedef iksTagHook
  114. int iksTagHook (void* @var{user_data}, char* @var{name}, char** @var{atts}, int @var{type});
  115. This function is called when a tag parsed. @var{name} is the name of the tag. If tag has
  116. no attributes @var{atts} is NULL, otherwise it contains a null terminated list of
  117. pointers to tag's attributes and their values. If return value isn't @code{IKS_OK},
  118. it is passed immediately to the caller of the @code{iks_parse}.
  119. @var{type} is one of the following:
  120. @table @code
  121. @item IKS_OPEN
  122. Opening tag, i.e. <tag attr='value'>
  123. @item IKS_CLOSE
  124. Closing tag, i.e. </tag>
  125. @item IKS_SINGLE
  126. Standalone tag, i.e. <tag attr='value'/>
  127. @end table
  128. @end deftp
  129. @deftp Typedef iksCDataHook
  130. int iksCDataHook (void* @var{user_data}, char* @var{data}, size_t @var{len});
  131. @var{data} is a pointer to the character data. Encoding is UTF-8 and it isn't terminated
  132. with a null character. Size of the data is given with @var{len} in bytes. This function
  133. can be called several times with smaller sized data for a single string. If
  134. return value isn't @code{IKS_OK}, it is passed immediately to the caller of the
  135. @code{iks_parse}.
  136. @end deftp
  137. @deftypefun int iks_parse (iksparser* @var{prs}, char *@var{data}, size_t @var{len}, int @var{finish});
  138. You give XML document to the parser with this function. @var{data}
  139. is a @var{len} bytes string. If @var{len} is zero, data must be a null
  140. terminated string.
  141. If @var{finish} value is zero, parser waits for more data later. If you
  142. want to finish parsing without giving data, call it like:
  143. @example
  144. iks_parse (my_parser, NULL, 0, 1);
  145. @end example
  146. You should check the return value for following conditions:
  147. @table @code
  148. @item IKS_OK
  149. There isn't any problem.
  150. @item IKS_NOMEM
  151. Not enough memory.
  152. @item IKS_BADXML
  153. Document is not well-formed.
  154. @item IKS_HOOK
  155. Your hook decided that there is an error.
  156. @end table
  157. @end deftypefun
  158. @deftypefun void iks_parser_delete (iksparser* @var{prs});
  159. This function frees parser structure and associated data.
  160. @end deftypefun
  161. Now we have learned how to create and use a sax parser. Lets parse a simple
  162. XML document. Write following code into a @file{test.c} file.
  163. @smallexample
  164. #include <stdio.h>
  165. #include <iksemel.h>
  166. int pr_tag (void *udata, char *name, char **atts, int type)
  167. @{
  168. switch (type) @{
  169. case IKS_OPEN:
  170. printf ("TAG <%s>\n", name);
  171. break;
  172. case IKS_CLOSE:
  173. printf ("TAG </%s>\n", name);
  174. break;
  175. case IKS_SINGLE:
  176. printf ("TAG <%s/>\n", name);
  177. break;
  178. @}
  179. if (atts) @{
  180. int i = 0;
  181. while (atts[i]) @{
  182. printf (" ATTRIB %s='%s'\n", atts[i], atts[i+1]);
  183. i += 2;
  184. @}
  185. @}
  186. return IKS_OK;
  187. @}
  188. enum ikserror pr_cdata (void *udata, char *data, size_t len)
  189. @{
  190. int i;
  191. printf ("CDATA [");
  192. for (i = 0; i < len; i++)
  193. putchar (data[i]);
  194. printf ("]\n");
  195. return IKS_OK;
  196. @}
  197. int main (int argc, char *argv[])
  198. @{
  199. iksparser *p;
  200. p = iks_sax_new (NULL, pr_tag, pr_cdata);
  201. switch (iks_parse (p, argv[1], 0, 1)) @{
  202. case IKS_OK:
  203. puts ("OK");
  204. break;
  205. case IKS_NOMEM:
  206. puts ("Not enough memory");
  207. exit (1);
  208. case IKS_BADXML:
  209. puts ("XML document is not well-formed");
  210. exit (2);
  211. case IKS_HOOK:
  212. puts ("Our hooks didn't like something");
  213. exit (2);
  214. @}
  215. iks_parser_delete (p);
  216. return 0;
  217. @}
  218. @end smallexample
  219. Now compile and test it with:
  220. @example
  221. gcc -o test test.c -liksemel
  222. ./test "<test>Hello<br/>World!</test>"
  223. ./test "<lala a='12' b='42'/>"
  224. @end example
  225. @heading Error Handling
  226. XML standart states that once an error is detected, the processor must not continue
  227. normal processing (i.e. it must not pass character data or markup information to
  228. the application). So iksemel stops processing immediately when it encounters a
  229. syntax error, or one of your hook functions return any one value than @code{IKS_OK},
  230. and @code{iks_parse} function returns with the error code.
  231. Since it is useful for debugging, iksemel provides functions to get position of
  232. the error. Position is usually at the starting character for syntax errors. Since
  233. your hooks are called after whole element (i.e. markup or character data) is
  234. passed, position is at the end of the erroneous element for @code{IKS_HOOK} errors.
  235. @deftypefun {unsigned long} iks_nr_bytes (iksparser* @var{prs});
  236. Returns how many number of bytes parsed.
  237. @end deftypefun
  238. @deftypefun {unsigned long} iks_nr_lines (iksparser* @var{prs});
  239. Returns how many number of lines parsed.
  240. @end deftypefun
  241. If you want to parse another document with your parser again, you should use
  242. the following function to reset your parser.
  243. @deftypefun void iks_parser_reset (iksparser* @var{prs});
  244. Resets the parser's internal state.
  245. @end deftypefun
  246. @comment ============================================================
  247. @node Working with XML Trees,XML Streams,Parsing an XML Document,Tutorials
  248. @section Working with XML Trees
  249. SAX interface uses very little memory, but it forces you to access XML
  250. documents sequentally. In many cases you want to keep a tree like
  251. representation of XML document in memory and want to access and
  252. modify its content randomly.
  253. iksemel provides functions for efficiently creating such trees either
  254. from documents or programmaticaly. You can access and modify this
  255. tree and can easily generate a new XML document from the tree.
  256. This is called DOM (Document Object Model) interface.
  257. @ifinfo
  258. @menu
  259. * Memory Management::
  260. * Creating a Tree::
  261. * Accessing the Tree::
  262. * Converting a Tree into an XML Document::
  263. * Parsing an XML Document into a Tree::
  264. @end menu
  265. @end ifinfo
  266. @comment ============================================================
  267. @node Memory Management,Creating a Tree,,Working with XML Trees
  268. @subsection Memory Management
  269. Since keeping whole document content uses a lot of memory and requires
  270. many calls to OS's memory allocation layer, iksemel uses a simple object
  271. stack system for minimizing calls to the @code{malloc} function and releasing
  272. all the memory associated with a tree in a single step.
  273. A parsed XML tree contains following objects:
  274. @table @samp
  275. @item Nodes
  276. These are basic blocks of document. They can contain a tag, attribute pair
  277. of a tag, or character data. Tag nodes can also contain other nodes as
  278. children. Node structure has a small fixed size depending on the node type.
  279. @item Names
  280. Names of tags and attributes. They are utf-8 encoded small strings.
  281. @item Character Data
  282. They are similar to names but usually much bigger.
  283. @end table
  284. iksemel's object stack has two separate areas for keeping these data objects.
  285. Meta chunk contains all the structures and aligned data, while the data chunk
  286. contains strings. Each chunk starts with a choosen size memory block, then
  287. when necessary more blocks allocated for providing space. Unless there is a big
  288. request, each block is double the size of the previous block, thus real memory
  289. needs are quickly reached without allocating too many blocks, or wasting
  290. memory with too big blocks.
  291. @deftp Typedef ikstack
  292. This is a structure defining the object stack. Its fields are private
  293. and subject to change with new iksemel releases.
  294. @end deftp
  295. @deftypefun {ikstack *} iks_stack_new (size_t @var{meta_chunk}, size_t @var{data_chunk});
  296. Creates an object stack. @var{meta_chunk} is the initial size of the
  297. data block used for structures and aligned data. @var{data_chunk} is
  298. the initial size of the data block used for strings. They are both in byte units.
  299. These two initial chunks and a small object stack structure is allocated in
  300. one @code{malloc} call for optimization purproses.
  301. @end deftypefun
  302. @deftypefun {void *} iks_stack_alloc (ikstack * @var{stack}, size_t @var{size});
  303. Allocates @var{size} bytes of space from the object stack's meta chunk.
  304. Allocated space is aligned on platform's default alignment boundary and
  305. isn't initialized. Returns a pointer to the space, or NULL if there isn't enough
  306. space available and allocating a new block fails.
  307. @end deftypefun
  308. @deftypefun {void *} iks_stack_strdup (ikstack * @var{stack}, const char * @var{src}, size_t @var{len});
  309. Copies given string @var{src} into the object stack's data chunk. Returns a
  310. pointer to the new string, or NULL if there isn't enough space in the stack.
  311. If @var{len} is zero string must be null terminated.
  312. @end deftypefun
  313. @deftypefun void iks_stack_delete (ikstack * @var{stack});
  314. Gives all memory associated with object stack to the system.
  315. @end deftypefun
  316. Since character data sections are usually parsed in separate blocks,
  317. a growable string implementation is necessary for saving memory.
  318. @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});
  319. This function appends the string @var{src} to the string @var{old} in the
  320. stack's data chunk. If @var{old} is NULL it behaves like @code{iks_stack_strdup}.
  321. Otherwise @var{old} has to be a string created with @code{iks_stack_strdup}
  322. or @code{iks_stack_strcat} functions.
  323. If @var{old_len} or @var{src_len} is zero, corresponding string must be null
  324. terminated.
  325. Since string can be moved into another block of the data chunk, you must use the
  326. returned value for new string, and must not reference to @var{old} anymore.
  327. Return value can be NULL if there isn't enough space in stack, and allocating a
  328. new block fails.
  329. @end deftypefun
  330. @comment ============================================================
  331. @node Creating a Tree,Accessing the Tree,Memory Management,Working with XML Trees
  332. @subsection Creating a Tree
  333. @deftp Typedef iks
  334. This is a structure defining a XML node. Its fields are private and only
  335. accessed by following functions.
  336. @end deftp
  337. @deftypefun iks* iks_new (const char *@var{name});
  338. Creates an object stack and creates a IKS_TAG type of node with given
  339. tag name inside the stack. Tag name is also copied into the stack.
  340. Returns the node pointer, or NULL if there isn't enough memory.
  341. @end deftypefun
  342. @deftypefun iks* iks_new_within (const char *@var{name}, ikstack* @var{stack});
  343. Creates a IKS_TAG type of node with the given tag name. Node and tag
  344. name is allocated inside the given object stack. Returns the node
  345. pointer, or NULL if there isn't enough memory.
  346. @end deftypefun
  347. @deftypefun iks* iks_insert (iks *@var{x}, const char *@var{name});
  348. Creates a IKS_TAG type of node with the given tag name. Node and tag
  349. name is allocated inside the @var{x} node's object stack and linked
  350. to @var{x} as a child node. Returns the node pointer, or NULL if there
  351. isn't enough memory.
  352. @end deftypefun
  353. @deftypefun iks* iks_insert_cdata (iks* @var{x}, const char* @var{data}, size_t @var{len});
  354. Creates a IKS_CDATA type of node with given character data. Node is
  355. allocated inside the @var{x} node's object stack and linked to @var{x}
  356. as a child node. Data is copied as well. If @var{len} is zero data must
  357. be a null terminated string. Returns the node pointer, or NULL if
  358. there isn't enough memory.
  359. @end deftypefun
  360. @deftypefun iks* iks_insert_attrib (iks* @var{x}, const char* @var{name}, const char* @var{value});
  361. Creates a IKS_ATTRIBUTE type of node with given attribute name and the
  362. value. Node is allocated inside the @var{x} node's object stack and
  363. linked to @var{x} as an attribute node. Attribute name and value is
  364. copied as well. Returns the node pointer, or NULL if there isn't
  365. enough memory.
  366. Reinserting another value with same attribute name changes an attribute's
  367. value. If @var{value} is NULL, attribute is removed from the tag.
  368. @end deftypefun
  369. @deftypefun iks* iks_insert_node (iks* @var{x}, iks* @var{y});
  370. Links node @var{y} to node @var{x} as a child node. Nodes are not copied
  371. between object stacks, be careful.
  372. @end deftypefun
  373. @deftypefun void iks_hide (iks *@var{x});
  374. Changes the links of the other nodes so that @var{x} becomes invisible.
  375. It stays in the same object stack with neighbour nodes, be careful.
  376. @end deftypefun
  377. @deftypefun void iks_delete (iks *@var{x});
  378. Frees the object stack of the node @var{x}.
  379. @end deftypefun
  380. Now lets create a tree representation of following XML document:
  381. @example
  382. <message type='chat' from='bob@@bd.com'>
  383. <subject>song lyric</subject><priority>high</priority>
  384. <body>
  385. <em style='underline'>here is the correct version:</em>
  386. i just don't see why i should even care
  387. it's not dark yet, but it's getting there
  388. </body>
  389. </message>
  390. @end example
  391. here is the code:
  392. @example
  393. iks *x, *y, *z;
  394. x = iks_new ("message");
  395. iks_insert_attrib (x, "type", "chat");
  396. iks_insert_attrib (x, "from", "bob@@bd.com");
  397. iks_insert_cdata (x, "\n", 1);
  398. iks_insert_cdata (iks_insert (x, "subject"), "song lyric", 10);
  399. iks_insert_cdata (iks_insert (x, "priority"), "high", 4);
  400. iks_insert_cdata (x, "\n", 1);
  401. y = iks_insert (x, "body");
  402. iks_insert_cdata (y, "\n", 1);
  403. z = iks_insert (y, "em");
  404. iks_insert_attrib (z, "style", "underline");
  405. iks_insert_cdata (z, "here is the correct version", 0);
  406. iks_insert_cdata (y, "\n", 1);
  407. iks_insert_cdata (y, "i just don't see why", 0);
  408. iks_insert_cdata (y, "i should even care\n", 0);
  409. iks_insert_cdata (y, "it's not dark yet,", 0);
  410. iks_insert_cdata (y, "but it's getting there\n", 0);
  411. iks_insert_cdata (x, "\n", 1);
  412. @end example
  413. Notice how newlines are inserted for proper formatting of document. They aren't
  414. necessary for representing data, but they make it easier to read document for
  415. humans.
  416. Also notice how @code{iks_insert} and @code{iks_insert_cdata} chained.
  417. There are also functions for duplicating xml trees. They are:
  418. @deftypefun {iks *} iks_copy (iks* @var{x});
  419. Creates a full copy of the tree in a newly created object stack.
  420. @end deftypefun
  421. @deftypefun {iks *} iks_copy_within (iks* @var{x}, ikstack *@var{s});
  422. Creates a full copy of the tree in given object stack.
  423. @end deftypefun
  424. @comment ============================================================
  425. @node Accessing the Tree,Converting a Tree into an XML Document,Creating a Tree,Working with XML Trees
  426. @subsection Accessing a Tree
  427. Basic access functions allow you to move on the tree:
  428. @deftypefun iks* iks_next (iks* @var{x});
  429. @end deftypefun
  430. @deftypefun iks* iks_prev (iks* @var{x});
  431. @end deftypefun
  432. @deftypefun iks* iks_parent (iks* @var{x});
  433. @end deftypefun
  434. @deftypefun iks* iks_child (iks* @var{x});
  435. @end deftypefun
  436. @deftypefun iks* iks_attrib (iks* @var{x});
  437. @end deftypefun
  438. These functions return a pointer to the next, previous, parent, first child,
  439. and first attribute node of the given node @var{x}. If that node doesn't
  440. exist or @var{x} is NULL, a NULL value is returned.
  441. @deftypefun {iks *} iks_root (iks *@var{x});
  442. Returns the topmost parent node of the @var{x}.
  443. @end deftypefun
  444. @deftypefun iks* iks_next_tag (iks* @var{x});
  445. @end deftypefun
  446. @deftypefun iks* iks_prev_tag (iks* @var{x});
  447. @end deftypefun
  448. @deftypefun iks* iks_first_tag (iks* @var{x});
  449. @end deftypefun
  450. These functions return a pointer to the next, previous, first child node
  451. of the given node @var{x}. Only tag nodes are considered, other type
  452. of the nodes are skipped. If such a node doesn't exist or @var{x} is NULL,
  453. a NULL value is returned.
  454. Another group of functions allow you to access specific information and
  455. content of the nodes:
  456. @deftypefun ikstack* iks_stack (iks* @var{x});
  457. Returns the object stack which node @var{x} stays.
  458. @end deftypefun
  459. @deftypefun {enum ikstype} iks_type (iks* @var{x});
  460. Returns the type of the node.
  461. @tindex ikstype
  462. @table @code
  463. @item IKS_TAG
  464. Node is a tag and can contain child nodes and attributes.
  465. @item IKS_CDATA
  466. Node contains character data.
  467. @item IKS_ATTRIBUTE
  468. Node contains an attribute and its value.
  469. @end table
  470. @end deftypefun
  471. @deftypefun char* iks_name (iks* @var{x});
  472. Returns the name of the tag for nodes with the type @var{IKS_TAG}.
  473. Returns an attribute's name for nodes of type IKS_ATTRIBUTE.
  474. @end deftypefun
  475. @deftypefun char* iks_cdata (iks* @var{x});
  476. Returns a pointer to node's character data if available, NULL otherwise.
  477. Returns an attribute's value for nodes of type IKS_ATTRIBUTE.
  478. @end deftypefun
  479. @deftypefun size_t iks_cdata_size (iks *@var{x});
  480. Returns the size of the node's character data in bytes.
  481. @end deftypefun
  482. @deftypefun int iks_has_children (iks *@var{x});
  483. Returns a non-zero value if node @var{x} has a child node.
  484. @end deftypefun
  485. @deftypefun int iks_has_attribs (iks *@var{x});
  486. Returns a non-zero value if node @var{x} has attributes.
  487. @end deftypefun
  488. Last group of the functions simplifies finding and accessing the content
  489. of a specific node:
  490. @deftypefun iks* iks_find (iks *@var{x}, const char *@var{name});
  491. Searches a IKS_TAG type of node with @var{name} as tag name in child
  492. nodes of @var{x}. Returns a pointer to the node if found, NULL otherwise.
  493. @end deftypefun
  494. @deftypefun char* iks_find_cdata (iks* @var{x}, const char* @var{name});
  495. Searches a IKS_TAG type of node with @var{name} as tag name in child
  496. nodes of @var{x}. Returns a pointer to the character data of the node's
  497. first child node if found, NULL otherwise.
  498. @end deftypefun
  499. @deftypefun char* iks_find_attrib (iks* @var{x}, const char* @var{name});
  500. Searches an attribute with given name in attributes of the @var{x}.
  501. Returns a pointer to attribute value if found, NULL otherwise.
  502. @end deftypefun
  503. @deftypefun {iks *} iks_find_with_attrib (iks *@var{x}, const char *@var{tagname}, const char *@var{attrname}, const char *@var{value});
  504. Searches for a child tag of @var{x} which has an attribute with name
  505. @var{attrname} and value @var{value}. If @var{tagname} isn't NULL,
  506. name of the tag must also match. Returns a pointer to the node if found,
  507. NULL otherwise.
  508. @end deftypefun
  509. Here is an example which demonstrates accessing file names in a fictitious
  510. XML playlist file:
  511. @example
  512. <playlist>
  513. <item type='mpg'>
  514. <name>/home/madcat/download/matrix_rev_trailer.mpg</name>
  515. <duration>1:17</duration>
  516. </item>
  517. <item type='rm'>
  518. <name>/home/madcat/anim/clementine_ep1.rm</name>
  519. <duration>22:00</duration>
  520. </item>
  521. <item type='avi'>
  522. <name>/home/madcat/anim/futurama/ep101.avi</name>
  523. <subtitle>/home/madcat/subs/futurama/ep101.txt</subtitle>
  524. <duration>30:00</duration>
  525. </item>
  526. <repeat/>
  527. <fullscreen/>
  528. <noui/>
  529. </playlist>
  530. @end example
  531. and here is the code:
  532. @example
  533. #include <stdio.h>
  534. #include <iksemel.h>
  535. int main (int argc, char *argv[])
  536. @{
  537. iks *x, *y;
  538. int e;
  539. if (argc < 2) @{
  540. printf ("usage: %s <playlistfile>", argv[0]);
  541. return 0;
  542. @}
  543. e = iks_load (argv[1], &x);
  544. if (e != IKS_OK) @{
  545. printf ("parse error %d\n", e);
  546. return 1;
  547. @}
  548. if (iks_find (x, "repeat")) puts ("repeat mode enabled");
  549. y = iks_child (x);
  550. while (y) @{
  551. if (iks_type (y) == IKS_TAG
  552. && strcmp (iks_name (y), "item") == 0) @{
  553. printf ("Filename: [%s]\n", iks_find_cdata (y, "name"));
  554. @}
  555. y = iks_next (y);
  556. @}
  557. iks_delete (x);
  558. return 0;
  559. @}
  560. @end example
  561. @comment ============================================================
  562. @node Converting a Tree into an XML Document,Parsing an XML Document into a Tree,Accessing the Tree,Working with XML Trees
  563. @subsection Converting a Tree to an XML Document
  564. There is a function for converting given XML tree into a null terminated string.
  565. @deftypefun {char *} iks_string (ikstack* @var{stack}, iks* @var{x});
  566. Converts given tree into a string. String is created inside the given object
  567. stack. Returns a pointer to the string, or NULL if there isn't enough memory
  568. available.
  569. If @var{stack} is NULL, string is created inside an @code{iks_malloc}ed buffer.
  570. You can free it later with @code{iks_free} function.
  571. @end deftypefun
  572. Here is an example which builds a tree and print it.
  573. @example
  574. iks *x;
  575. char *t;
  576. x = iks_new ("test");
  577. iks_insert_cdata (iks_insert (x, "a"), "1234", 4);
  578. iks_insert (x, "br");
  579. iks_insert_cdata (x, "1234", 4);
  580. t = iks_string (iks_stack (x), x);
  581. puts (t);
  582. iks_delete (x);
  583. @end example
  584. @comment ============================================================
  585. @node Parsing an XML Document into a Tree,,Converting a Tree into an XML Document,Working with XML Trees
  586. @subsection Parsing a Document into a Tree
  587. If you want to automatically convert an XML document into a tree, you can use
  588. iksemel's DOM parser. It is created with following function:
  589. @deftypefun iksparser* iks_dom_new (iks **@var{iksptr});
  590. Creates a DOM parser. A pointer to the created XML tree is put into the
  591. variable pointed by @var{iksptr}. Returns a pointer to the parser, or NULL
  592. is there isn't enough memory.
  593. @end deftypefun
  594. Usage is same as SAX parser. You feed the data with @code{iks_parse}, and if
  595. there isn't an error, you can access to your tree from variable @code{*iksptr}.
  596. Here is a simple example:
  597. @example
  598. iks *x;
  599. iksparser *p;
  600. p = iks_dom_new (&x);
  601. if (IKS_OK != iks_parse (p, "<a>bcd</a>", 9, 1)) @{
  602. puts ("parse error");
  603. @}
  604. /* x is useable after that point */
  605. /* this will print 'bcd' */
  606. printf ("%s\n", iks_cdata (iks_child (x)));
  607. @end example
  608. If you know the size of the file ahead, or you have an approximate idea,
  609. you can tell this to the dom parser for choosing a better memory allocation
  610. strategy. Here is the function for this.
  611. @deftypefun void iks_set_size_hint (iksparser *@var{prs}, size_t @var{approx_size});
  612. Parser @var{prs} must be a dom type parser. @var{approx_size} is the
  613. expected size of the xml document. Parser chooses its chunk size
  614. based on this information. Helps performance while processing big files.
  615. @end deftypefun
  616. If you already have your XML document in memory, you can simply parse
  617. it with:
  618. @deftypefun {iks *} iks_tree (const char *@var{xml_str}, size_t @var{len}, int *@var{err});
  619. This function parses the buffer pointed by @var{xml_str}. If @var{len} is zero
  620. buffer is considered as a null terminated utf8 string. Returns the parsed tree,
  621. or NULL if there is an error. If @var{err} is not NULL, actual error code (returned
  622. by iks_parse) is put there.
  623. @end deftypefun
  624. Most of the times you want to load your configuration (or similar) files directly
  625. into trees. iksemel provides two functions to greatly simplify this:
  626. @deftypefun int iks_load (const char *@var{fname}, iks **@var{xptr});
  627. Loads the XML file. Tree is placed into the variable pointed by @var{xptr}.
  628. @end deftypefun
  629. @deftypefun int iks_save (const char *@var{fname}, iks *@var{x});
  630. Converts tree @var{x} into a string and saves to the file.
  631. @end deftypefun
  632. Both functions return same error codes as @code{iks_parse}. Some additional
  633. error codes are defined for indicating file problems. They are:
  634. @table @code
  635. @item IKS_FILE_NOFILE
  636. A file with the given name doesn't exist.
  637. @item IKS_FILE_NOACCESS
  638. Cannot open file. Possibly a permission problem.
  639. @item IKS_FILE_RWERR
  640. Read or write operation failed.
  641. @end table
  642. Here is a simple example which parses a file and saves it into another:
  643. @example
  644. iks *x;
  645. if (IKS_OK != iks_load ("file1.xml", &x)) @{
  646. puts ("loading error");
  647. @}
  648. if (IKS_OK != iks_save ("file2.xml", x)) @{
  649. puts ("saving error");
  650. @}
  651. @end example
  652. @comment ============================================================
  653. @node XML Streams,Writing a Jabber Client,Working with XML Trees,Tutorials
  654. @section XML Streams
  655. XML streams function as containers for any XML chunks sent asynchronously
  656. between network endpoints. They are used for asyncronously exchanging
  657. relatively small payload of structured information between entities.
  658. A stream is initiated by one of hosts connecting to the other, and sending a
  659. <stream:stream> tag. Receiving entity replies with a second XML stream
  660. back to the initiating entity within the same connection. Each unit of
  661. information is send as a direct child tag of the <stream:stream> tag.
  662. Stream is closed with </stream:stream>.
  663. XML streams use a subset of XML. Specifically they should not contain
  664. processing instructions, non-predefined entities, comments, or DTDs.
  665. Jabber protocol uses XML streams for exchanging messages, presence
  666. information, and other information like authorization, search, time and
  667. version queries, protocol extensions.
  668. iksemel provides you a stream parser, which automatically handles connection
  669. to the server, and calls your hook function with incoming information
  670. parsed and converted to an XML tree.
  671. You can create such a parser with:
  672. @deftypefun iksparser* iks_stream_new (char* @var{name_space}, void* @var{user_data}, iksStreamHook* @var{streamHook});
  673. Allocates and initalizes a stream parser. @var{name_space} indicates the
  674. stream type, jabber clients use "jabber:client" namespace. @var{user_data}
  675. is passed directly to your hook function.
  676. @end deftypefun
  677. @deftp Typedef iksStreamHook
  678. int iksStreamHook (void* @var{user_data}, int @var{type}, iks* @var{node});
  679. Depending on the value of the @var{type}, @var{node} contains:
  680. @table @code
  681. @item IKS_NODE_START
  682. Got the <stream:stream> tag, namespace, stream id and other information
  683. is contained in the @var{node}.
  684. @item IKS_NODE_NORMAL
  685. A first level child of the <stream:stream> tag is received. @var{node} contains
  686. the parsed tag. If you are connected to a jabber server, you can get <message>,
  687. <presence>, or <iq> tags.
  688. @item IKS_NODE_ERROR
  689. Got a <stream:error> tag, details can be accessed from @var{node}.
  690. @item IKS_NODE_STOP
  691. </stream:stream> tag is received or connection is closed, @var{node} is @code{NULL}.
  692. @end table
  693. Freeing the node with @code{iks_delete} is up to you.
  694. @end deftp
  695. You can manually feed this parser with @code{iks_parse} function, but using
  696. iksemel's connection facilities is easier for most of the cases.
  697. This functions return @code{IKS_OK} for success. Error codes of @code{iks_parse}
  698. are used in same manner. Following additional codes are defined for
  699. network related problems:
  700. @table @code
  701. @item IKS_NET_NODNS
  702. Hostname lookup failed. Possible reasons: hostname is incorrect,
  703. you are not online, your dns server isn't accessible.
  704. @item IKS_NET_NOSOCK
  705. Socket cannot created.
  706. @item IKS_NET_NOCONN
  707. Connection attemp failed. Possible reasons: host is not an XML stream
  708. server, port number is wrong, server is busy or closed for the moment.
  709. @item IKS_NET_RWERR
  710. @code{send} or @code{recv} call is failed when attempting to exchange
  711. the data with the server. You should close the connection with @code{iks_disconnect}
  712. after getting this error from data transfer functions.
  713. @end table
  714. @deftypefun int iks_connect_tcp (iksparser *@var{prs}, const char *@var{server}, int @var{port});
  715. This function connects the parser to a server and sends stream header for you.
  716. @var{server} is the host name of the server and @var{port} is the tcp port
  717. number which server is listening to. You can use @code{IKS_JABBER_PORT}
  718. macro for the default jabber client port (5222).
  719. @end deftypefun
  720. @deftypefun int iks_connect_fd (iksparser *@var{prs}, int @var{fd});
  721. Attaches parser to an already opened connection. @var{fd} is the socket
  722. descriptor. Note that @code{iks_disconnect} doesn't close the socket
  723. for this kind of connection, opening and closing of the socket is up to your
  724. application. Stream header is not sent automatically. You can use
  725. @code{iks_send_header} function for sending it.
  726. @end deftypefun
  727. @deftypefun void iks_disconnect (iksparser *@var{prs});
  728. Closes connection to the server, and frees connection resources.
  729. @end deftypefun
  730. After successfully connecting to a server, you can use following functions
  731. for exchanging information with server.
  732. @deftypefun int iks_recv (iksparser* @var{prs}, int @var{timeout});
  733. If @var{timeout} is @code{-1}, waits until some data arrives from server,
  734. and process the data. Your stream hook can be called if a complete
  735. chunk is arrived.
  736. If @var{timeout} is a positive integer, @code{iks_recv} returns if no data
  737. arrives for @var{timeout} seconds.
  738. If @var{timeout} is zero, @code{iks_recv} checks if there is any data
  739. waiting at the network buffer, and returns without waiting for data.
  740. @end deftypefun
  741. @deftypefun int iks_fd (iksparser* @var{prs});
  742. Returns the file descriptor of the connected socket. You can use this in
  743. your @code{select} function or some other input loop to act whenever
  744. some data from the server arrives. This value of only valid between
  745. a successful @code{iks_connect_tcp} and @code{iks_disconnect}.
  746. @end deftypefun
  747. @deftypefun int iks_send (iksparser* @var{prs}, iks* @var{x});
  748. Converts the tree given in @var{x} to a string, and sends to the server.
  749. String is created inside the object stack of @var{x}.
  750. @end deftypefun
  751. @deftypefun int iks_send_raw (iksparser* @var{prs}, char* @var{xmlstr});
  752. Sends the string given in @var{xmlstr} to the server.
  753. @end deftypefun
  754. @deftypefun int iks_send_header (iksparser *@var{prs}, char *@var{to});
  755. Sends the stream header. @var{to} is the name of the server.
  756. Normally @code{iks_connect_tcp} function calls this for you. This
  757. is only useful if you are using @code{iks_connect_fd}.
  758. @end deftypefun
  759. Sometimes it is useful to log incoming and outgoing data to your parser
  760. for debugging your applications. iksemel provides a logging facility for you.
  761. @deftypefun void iks_set_log_hook (iksparser* @var{prs}, iksLogHook* @var{logHook});
  762. Sets the log function for your stream parser. You can't use this function
  763. on any other type of parser.
  764. @end deftypefun
  765. @deftp Typedef iksLogHook
  766. void iksLogHook (void* @var{user_data}, const char* @var{data}, size_t @var{size}, int @var{is_incoming});
  767. @var{user_data} is same value which you give with @code{iks_stream_new}.
  768. @var{data} is @var{size} bytes of data. Be very careful that this data may be
  769. coming from other side of the connection and can contain malicius bytes. It isn't
  770. checked by iksemel yet, so you should check it yourself before displaying or
  771. passing to other systems in your application or computer. If @var{is_incoming}
  772. is a non-zero value, data is incoming from server, otherwise it is outgoing to the
  773. server.
  774. @end deftp
  775. @comment ============================================================
  776. @node Writing a Jabber Client,Utility Functions,XML Streams,Tutorials
  777. @section Writing a Jabber Client
  778. @ifinfo
  779. @menu
  780. * Security::
  781. * Packets::
  782. * Packet Filter::
  783. * Creating Common Packets::
  784. @end menu
  785. @end ifinfo
  786. @comment ============================================================
  787. @node Security,Packets,,Writing a Jabber Client
  788. @subsection Security
  789. iksemel supports TLS protocol for encrypted communication and SASL
  790. protocol for authentication. TLS is handled by gnutls library.
  791. @deftypefun int iks_has_tls (void);
  792. If iksemel is compiled with gnutls library, this function returns a non-zero
  793. value indicating you can try encrypted connection with the server.
  794. @end deftypefun
  795. @deftypefun int iks_start_tls (iksparser* @var{prs});
  796. Starts a TLS handshake over already connected parser. Returns IKS_OK or
  797. one of the IKS_NET_ errors. If handshake succeeds you'll get another
  798. stream header from server.
  799. @end deftypefun
  800. @deftypefun int iks_is_secure (iksparser* @var{prs});
  801. Returns a non-zero value if a secure connection is fully established
  802. between server.
  803. @end deftypefun
  804. @deftypefun int iks_start_sasl (iksparser* @var{prs}, enum ikssasltype @var{type}, char* @var{username}, char* @var{pass});
  805. Starts SASL operation.
  806. @end deftypefun
  807. See tools/iksroster.c for a good example.
  808. @comment ============================================================
  809. @node Packets,Packet Filter,Security,Writing a Jabber Client
  810. @subsection Packets
  811. iksemel can parse a jabber XML node and provide you a public packet
  812. structure which contains information like node type and subtype, id,
  813. namespace, sender's jabber id, etc.
  814. This handles a lot of node parsing for you. Packets are also used in
  815. the packet filter subsystem.
  816. @deftypefun {ikspak *} iks_packet (iks *@var{x});
  817. Takes a node from stream and extracts information from it to a packet structure.
  818. Structure is allocated inside the node's object stack.
  819. @end deftypefun
  820. @tindex ikspak
  821. @code{ikspak} structure has following fields:
  822. @table @code
  823. @item iks *x;
  824. This is a pointer to the node.
  825. @item iksid *from;
  826. Sender's jabber id in parsed form. See below for @code{iksid} structure.
  827. @item iks *query;
  828. A pointer to the <query> tag for IQ nodes.
  829. @item char *ns;
  830. Namespace of the content for IQ nodes.
  831. @item char *id;
  832. ID of the node.
  833. @item enum ikspaktype type;
  834. Type of the node. Possible types are:
  835. @table @code
  836. @item IKS_PAK_NONE
  837. Unknown node.
  838. @item IKS_PAK_MESSAGE
  839. Message node.
  840. @item IKS_PAK_PRESENCE
  841. Presence node with presence publishing operation.
  842. @item IKS_PAK_S10N
  843. Presence node with subscription operation.
  844. @item IKS_PAK_IQ
  845. IQ node.
  846. @end table
  847. @item enum iksubtype subtype;
  848. Sub type of the node. Sub types for message nodes:
  849. @table @code
  850. @item IKS_TYPE_NONE
  851. A normal message.
  852. @item IKS_TYPE_CHAT
  853. Private chat message.
  854. @item IKS_TYPE_GROUPCHAT
  855. Multi user chat message.
  856. @item IKS_TYPE_HEADLINE
  857. Message from a news source.
  858. @item IKS_TYPE_ERROR
  859. Message error.
  860. @end table
  861. Sub types for IQ nodes:
  862. @table @code
  863. @item IKS_TYPE_GET
  864. Asks for some information.
  865. @item IKS_TYPE_SET
  866. Request for changing information.
  867. @item IKS_TYPE_RESULT
  868. Reply to get and set requests.
  869. @item IKS_TYPE_ERROR
  870. IQ error.
  871. @end table
  872. Sub types for subscription nodes:
  873. @table @code
  874. @item IKS_TYPE_SUBSCRIBE,
  875. Asks for subscribing to the presence.
  876. @item IKS_TYPE_SUBSCRIBED,
  877. Grants subscription.
  878. @item IKS_TYPE_UNSUBSCRIBE,
  879. Asks for unsubscribing to the presence.
  880. @item IKS_TYPE_UNSUBSCRIBED,
  881. Cancels subscription.
  882. @item IKS_TYPE_ERROR
  883. Presence error.
  884. @end table
  885. Sub types for presence nodes:
  886. @table @code
  887. @item IKS_TYPE_PROBE,
  888. Asks presence status.
  889. @item IKS_TYPE_AVAILABLE,
  890. Publishes entity as available. More information can be found in @code{show} field.
  891. @item IKS_TYPE_UNAVAILABLE
  892. Publishes entity as unavailable. More information can be found in @code{show} field.
  893. @end table
  894. @item enum ikshowtype show;
  895. Presence state for the presence nodes.
  896. @table @code
  897. @item IKS_SHOW_UNAVAILABLE
  898. Entity is unavailable.
  899. @item IKS_SHOW_AVAILABLE
  900. Entity is available.
  901. @item IKS_SHOW_CHAT
  902. Entity is free for chat.
  903. @item IKS_SHOW_AWAY
  904. Entity is away for a short time.
  905. @item IKS_SHOW_XA
  906. Entity is away for a long time.
  907. @item IKS_SHOW_DND
  908. Entity doesn't want to be disturbed.
  909. @end table
  910. @end table
  911. iksemel has two functions to parse and compare jabber IDs.
  912. @deftypefun {iksid *} iks_id_new (ikstack *@var{s}, const char *@var{jid});
  913. Parses a jabber id into its parts. @code{iksid} structure is created inside
  914. the @var{s} object stack.
  915. @end deftypefun
  916. @tindex iksid
  917. @code{iksid} structure has following fields:
  918. @table @code
  919. @item char *user;
  920. User name.
  921. @item char *server;
  922. Server name.
  923. @item char *resource;
  924. Resource.
  925. @item char *partial;
  926. User name and server name.
  927. @item char *full;
  928. User name, server name and resource.
  929. @end table
  930. You can access this fields and read their values. Comparing two parsed jabber
  931. ids can be done with:
  932. @deftypefun int iks_id_cmp (iksid *@var{a}, iksid *@var{b}, int @var{parts});
  933. Compares @var{parts} of @var{a} and @var{b}. Part values are:
  934. @table @code
  935. @item IKS_ID_USER
  936. @item IKS_ID_SERVER
  937. @item IKS_ID_RESOURCE
  938. @end table
  939. @sp 1
  940. You can combine this values with @code{or} operator. Some common combinations
  941. are predefined for you:
  942. @table @code
  943. @item IKS_ID_PARTIAL
  944. @code{IKS_ID_USER | IKS_ID_SERVER}
  945. @item IKS_ID_FULL
  946. @code{IKS_ID_USER | IKS_ID_SERVER | IKS_ID_RESOURCE}
  947. @end table
  948. Return value is @code{0} for equality. If entities are not equal a combination of
  949. part values showing different parts is returned.
  950. @end deftypefun
  951. @comment ============================================================
  952. @node Packet Filter,Creating Common Packets,Packets,Writing a Jabber Client
  953. @subsection Packet Filter
  954. Packet filter handles routing incoming packets to related functions.
  955. @tindex iksfilter
  956. @deftypefun {iksfilter *} iks_filter_new (void);
  957. Creates a new packet filter.
  958. @end deftypefun
  959. @deftypefun void iks_filter_packet (iksfilter *@var{f}, ikspak *@var{pak});
  960. Feeds the filter with given packet. Packet is compared to registered rules and
  961. hook functions of the matching rules are called in most matched to least
  962. matched order.
  963. @end deftypefun
  964. @deftypefun void iks_filter_delete (iksfilter *@var{f});
  965. Frees filter and rules.
  966. @end deftypefun
  967. Rules are created with following function:
  968. @tindex iksrule
  969. @deftypefun {iksrule *} iks_filter_add_rule (iksfilter *@var{f}, iksFilterHook *@var{filterHook}, void *@var{user_data}, @dots{});
  970. Adds a rule to the filter @var{f}. @var{user_data} is passed directly to your
  971. hook function @var{filterHook}.
  972. A rule consist of one or more type and value pairs. Possible types:
  973. @table @code
  974. @item IKS_RULE_ID
  975. Compares @code{char *} value to packet ids.
  976. @item IKS_RULE_FROM
  977. Compares @code{char *} value to packet senders.
  978. @item IKS_RULE_FROM_PARTIAL
  979. Compares @code{char *} value to packet sender. Ignores resource part of jabber id.
  980. @item IKS_RULE_NS
  981. Compares @code{char *} value to namespace of iq packets.
  982. @item IKS_RULE_TYPE
  983. Compares @code{int} value to packet types.
  984. @item IKS_RULE_SUBTYPE
  985. Compares @code{int} value to packet sub types.
  986. @item IKS_RULE_DONE
  987. Terminates the rule pairs.
  988. @end table
  989. @end deftypefun
  990. Here is an example which creates a filter and adds three rules:
  991. @example
  992. iksfilter *f;
  993. f = iks_filter_new ();
  994. iks_filter_add_rule (f, on_msg, NULL,
  995. IKS_RULE_TYPE, IKS_PAK_MESSAGE,
  996. IKS_RULE_DONE);
  997. iks_filter_add_rule (f, on_auth_result, NULL,
  998. IKS_RULE_TYPE, IKS_PAK_IQ,
  999. IKS_RULE_SUBTYPE, IKS_TYPE_RESULT,
  1000. IKS_RULE_ID, "auth",
  1001. IKS_RULE_DONE);
  1002. iks_filter_add_rule (f, on_roster_push, NULL,
  1003. IKS_RULE_TYPE, IKS_PAK_IQ,
  1004. IKS_RULE_SUBTYPE, IKS_TYPE_SET,
  1005. IKS_RULE_NS, "jabber:iq:roster",
  1006. IKS_RULE_DONE);
  1007. @end example
  1008. @deftp Typedef iksFilterHook
  1009. int iksFilterHook (void *user_data, ikspak *pak);
  1010. Your hook is called with your @var{user_data} and matching packet @var{pak}.
  1011. You can return two different values from your hook:
  1012. @table @code
  1013. @item IKS_FILTER_PASS
  1014. Packet is forwarded to least matching rules.
  1015. @item IKS_FILTER_EAT
  1016. Filtering process for the packet ends.
  1017. @end table
  1018. @end deftp
  1019. You can remove the rules with following functions:
  1020. @deftypefun void iks_filter_remove_rule (iksfilter *@var{f}, iksrule *@var{rule});
  1021. Removes the rule from filter.
  1022. @end deftypefun
  1023. @deftypefun void iks_filter_remove_hook (iksfilter *@var{f}, iksFilterHook *@var{filterHook});
  1024. Remove the rules using @var{filterHook} function from filter.
  1025. @end deftypefun
  1026. @comment ============================================================
  1027. @node Creating Common Packets,,Packet Filter,Writing a Jabber Client
  1028. @subsection Creating Common Packets
  1029. A usual jabber network traffic contains many similar XML constructs. iksemel
  1030. provides several utility functions for creating them. They all generate an XML
  1031. tree, so you can add or modify some parts of the tree, and send to server then.
  1032. @deftypefun {iks *} iks_make_auth (iksid *@var{id}, const char *@var{pass}, const char *@var{sid});
  1033. Creates an authorization packet. @var{id} is your parsed jabber id, and @var{pass}
  1034. is your password.
  1035. If stream id @var{sid} isn't NULL, SHA1 authentication is used, otherwise password
  1036. is attached in plain text. You can learn stream id from @code{IKS_STREAM_START}
  1037. packet in your stream hook like this:
  1038. @example
  1039. char *sid;
  1040. if (type == IKS_STREAM_START) @{
  1041. sid = iks_find_attrib (node, "id");
  1042. @}
  1043. @end example
  1044. @end deftypefun
  1045. @deftypefun {iks *} iks_make_msg (enum iksubtype @var{type}, const char *@var{to}, const char *@var{body});
  1046. Creates a message packet. @var{type} is the message type, @var{to} is jabber id
  1047. of the recipient, @var{body} is the message.
  1048. @end deftypefun
  1049. @deftypefun {iks *} iks_make_s10n (enum iksubtype @var{type}, const char *@var{to}, const char *@var{msg});
  1050. Creates a presence packet for subscription operations. @var{type} is operation,
  1051. @var{to} is jabber id of the recipient, @var{msg} is a small message for
  1052. introducing yourself, or explaning the reason of why you are subscribing or
  1053. unsubscribing.
  1054. @end deftypefun
  1055. @deftypefun {iks *} iks_make_pres (enum ikshowtype @var{show}, const char *@var{status});
  1056. Creates a presence packet for publishing your presence. @var{show} is your
  1057. presence state and @var{status} is a message explaining why you are not
  1058. available at the moment, or what you are doing now.
  1059. @end deftypefun
  1060. @deftypefun {iks *} iks_make_iq (enum iksubtype @var{type}, const char *@var{xmlns});
  1061. Creates an IQ packet. @var{type} is operation type and @var{xmlns} is the
  1062. namespace of the content. You usually have to add real content to the <query>
  1063. tag before sending this packet.
  1064. @end deftypefun
  1065. @comment ============================================================
  1066. @node Utility Functions,,Writing a Jabber Client,Tutorials
  1067. @section Utility Functions
  1068. @subsection Memory Utilities
  1069. @deftypefun {void *} iks_malloc (size_t @var{size});
  1070. @end deftypefun
  1071. @deftypefun void iks_free (void *@var{ptr});
  1072. @end deftypefun
  1073. These are wrappers around ANSI malloc and free functions used by the
  1074. iksemel library itself. You can free the output of iks_string (only if you
  1075. passed it a NULL stack) with iks_free for example. That is important
  1076. if you are using a malloc debugger in your application but not in iksemel
  1077. or vice versa.
  1078. @comment ============================================================
  1079. @subsection String Utilities
  1080. @deftypefun {char *} iks_strdup (const char *@var{src});
  1081. @end deftypefun
  1082. @deftypefun int iks_strcmp (const char *@var{a}, const char *@var{b});
  1083. @end deftypefun
  1084. @deftypefun int iks_strcasecmp (const char *@var{a}, const char *@var{b});
  1085. @end deftypefun
  1086. @deftypefun int iks_strncmp (const char *@var{a}, const char *@var{b}, size_t @var{n});
  1087. @end deftypefun
  1088. @deftypefun int iks_strncasecmp (const char *@var{a}, const char *@var{b}, size_t @var{n});
  1089. @end deftypefun
  1090. @deftypefun size_t iks_strlen (const char *@var{src});
  1091. @end deftypefun
  1092. These functions work exactly like their ANSI equivalents except that they allow
  1093. NULL values for string pointers. If @var{src} is NULL, iks_strdup and iks_strlen
  1094. returns zero. If @var{a} or @var{b} is NULL in string comparisation functions
  1095. they return -1.
  1096. Their usefulness comes from the fact that they can chained with DOM traversing
  1097. functions like this:
  1098. @smallexample
  1099. if (iks_strcmp (iks_find_attrib (x, "id"), "x1") == 0) count++;
  1100. @end smallexample
  1101. That example works even x doesn't have an 'id' attribute and iks_find_attrib
  1102. returns NULL. So you don't need to use temporary variables in such
  1103. situations.
  1104. @comment ============================================================
  1105. @subsection SHA1 Hash
  1106. Secure Hash Algorithm (SHA1) is used in the Jabber authentication
  1107. protocol for encoding your password when sending to the server.
  1108. This is normally handled by iks_make_auth() function, but if you
  1109. want to handle it manually, or if you need a good hash function
  1110. for other purproses you can use these functions.
  1111. @deftypefun iksha* iks_sha_new (void);
  1112. Allocates a structure for keeping calculation values and the state.
  1113. @end deftypefun
  1114. @deftypefun void iks_sha_reset (iksha *@var{sha});
  1115. Resets the state of the calculation.
  1116. @end deftypefun
  1117. @deftypefun void iks_sha_hash (iksha *@var{sha}, const unsigned char *@var{data}, int @var{len}, int @var{finish});
  1118. Calculates the hash value of the given data. If @var{finish} is non
  1119. zero, applies the last step of the calculation.
  1120. @end deftypefun
  1121. @deftypefun void iks_sha_print (iksha *@var{sha}, char *@var{hash});
  1122. Prints the result of a finished calculation into the buffer pointed by @var{hash}
  1123. in hexadecimal string form. Buffer must be at least 40 bytes long. String
  1124. is not null terminated.
  1125. @end deftypefun
  1126. @deftypefun void iks_sha (const char *@var{data}, char *@var{hash});
  1127. Calculates the hash value of @var{data} and prints into @var{hash}.
  1128. This is a helper function for simple hash calculations. It calls
  1129. other functions for the actual work.
  1130. @end deftypefun
  1131. @comment ============================================================
  1132. @node Development,Datatype Index,Tutorials,Top
  1133. @chapter Development
  1134. This chapter contains information on plan, procedure and standarts of
  1135. iksemel development.
  1136. @section Roadmap
  1137. There are three main functions iksemel tries to provide to applications:
  1138. @itemize @bullet
  1139. @item
  1140. A generic XML parser with SAX and DOM interfaces.
  1141. @item
  1142. XML stream client and server functionality.
  1143. @item
  1144. Utilities for Jabber clients.
  1145. @end itemize
  1146. Goal of the iksemel is providing these functions while supporting embedded
  1147. environments, keeping usage simple, and having a robust implementation.
  1148. Some decisions are made to reach this goal:
  1149. Code is written in ANSI C with a single dependency on C library. Instead of
  1150. using expat or libxml, a simple built-in parser is used. Similarly glib and
  1151. gnu only features of glibc (like object stacks) are avoided and built-in
  1152. memory and string utilities are used. This may seem like code duplication
  1153. but since they are optimized for iksemel and only a few kb in size,
  1154. it isn't a big disadvantage.
  1155. Code is placed files in a modular fashion, and different modules don't depend
  1156. on others' internal details. This allows taking unneeded functionality out when
  1157. building for low resource situations.
  1158. It is tried to give functions names which are consistent, clear and short.
  1159. API is documented with texinfo for high quality printed output and info file
  1160. output for fast and simple access during application development. Instead
  1161. of using an autogenerated system or simply listing function descriptions,
  1162. a task oriented tutorial approach is used.
  1163. @section Coding Style
  1164. Here is a short list describing preferred coding style for iksemel.
  1165. Please keep in mind when sending patches.
  1166. @itemize @bullet
  1167. @item
  1168. Indentation is done with tabs. Aligning is done with spaces.
  1169. @item
  1170. Placement of braces is K&R style.
  1171. @item
  1172. Function names are put at the start of line.
  1173. @item
  1174. Function names are lowercase.
  1175. @item
  1176. Words of the function names are separated with underscore character.
  1177. @item
  1178. Structure and variable names are lowercase.
  1179. @item
  1180. Macro and enumarations names are uppercase.
  1181. @item
  1182. Exported library API is contained in the single iksemel.h file.
  1183. @item
  1184. Exported function names start with iks_
  1185. @item
  1186. Exported structure and type names start with iks
  1187. @item
  1188. Exported macro and enumaration names start with IKS_
  1189. @end itemize
  1190. Here is an example:
  1191. @smallexample
  1192. int
  1193. iks_new_func (char *text)
  1194. @{
  1195. int i;
  1196. i = an_internal_func (text);
  1197. if (IKS_SOME_VALUE == i) @{
  1198. iks_some_func (text);
  1199. i++;
  1200. @}
  1201. return i;
  1202. @}
  1203. @end smallexample
  1204. @section Resources
  1205. @itemize @bullet
  1206. @item
  1207. RFC 2279, UTF-8 format @url{http://www.ietf.org/rfc/rfc2279.txt}
  1208. @item
  1209. W3C Recommendation, Extensible Markup Language 1.0 @url{http://www.w3.org/TR/REC-xml}
  1210. @item
  1211. Annotated XML Specification @url{http://www.xml.com/axml/testaxml.htm}
  1212. @item
  1213. Jabber Protocol Documents @url{http://www.jabber.org/protocol/}
  1214. @end itemize
  1215. @comment ============================================================
  1216. @node Datatype Index,Function Index,Development,Top
  1217. @unnumbered Datatype Index
  1218. @printindex tp
  1219. @node Function Index,,Datatype Index,Top
  1220. @unnumbered Function Index
  1221. @printindex fn
  1222. @contents
  1223. @bye