00001 #include <stdio.h>
00002 #include <string.h>
00003 #include <libxml/xmlmemory.h>
00004 #include <libxml/parser.h>
00005 #include <glib.h>
00006
00007 #include "lpbot.h"
00008
00017 static int parseOptions(xmlDoc *doc, xmlNode *cur)
00018 {
00019 xmlChar *key;
00020 cur = cur->xmlChildrenNode;
00021
00022
00023 while (cur)
00024 {
00025 key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
00026 if ((!xmlStrcmp(cur->name, (const xmlChar *)"ident_method")))
00027 {
00028 if(!strcmp((char*)key, "pass"))
00029 config->ident_method = LP_IDENT_PASS;
00030 else if(!strcmp((char*)key, "services"))
00031 config->ident_method = LP_IDENT_SERVICES;
00032 }
00033 else if ((!xmlStrcmp(cur->name, (const xmlChar *)"rss_interval")))
00034 config->rss_interval = atoi((char*)key);
00035 xmlFree(key);
00036 cur = cur->next;
00037 }
00038 return(0);
00039 }
00044 int parseConfig(char *docname)
00045 {
00046 xmlDocPtr doc;
00047 xmlNodePtr cur;
00048 xmlChar *key;
00049
00050 doc = xmlParseFile(docname);
00051
00052 if(doc == NULL)
00053 {
00054 fprintf(stderr, "document not parsed successfully\n");
00055 return(1);
00056 }
00057
00058 cur = xmlDocGetRootElement(doc);
00059
00060 if(cur == NULL)
00061 {
00062 fprintf(stderr, "empty document\n");
00063 xmlFreeDoc(doc);
00064 return(1);
00065 }
00066
00067 if(xmlStrcmp(cur->name, (const xmlChar *)"config"))
00068 {
00069 fprintf(stderr, "document of the wrong type, root node != config");
00070 xmlFreeDoc(doc);
00071 return(1);
00072 }
00073
00074 cur = cur->xmlChildrenNode;
00075 while(cur)
00076 {
00077 key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
00078 if((!xmlStrcmp(cur->name, (const xmlChar *)"server")))
00079 {
00080 if(parseServer(doc, cur)<0)
00081 return -1;
00082 }
00083 else if((!xmlStrcmp(cur->name, (const xmlChar *)"user")))
00084 {
00085 if(parseUser(doc, cur)<0)
00086 return -1;
00087 }
00088 else if((!xmlStrcmp(cur->name, (const xmlChar *)"rss")))
00089 {
00090 if(parseRss(doc, cur)<0)
00091 return -1;
00092 }
00093 else if((!xmlStrcmp(cur->name, (const xmlChar *)"options")))
00094 {
00095 if(parseOptions(doc, cur)<0)
00096 return -1;
00097 }
00098 xmlFree(key);
00099 cur = cur->next;
00100 }
00101 xmlFreeDoc(doc);
00102 return(0);
00103 }
00107 void lp_config_free(lp_config *cfg)
00108 {
00109 int i;
00110 for(i=0;i<g_list_length(cfg->servers);i++)
00111 lp_server_free(g_list_nth_data(cfg->servers, i));
00112 g_list_free(cfg->servers);
00113 for(i=0;i<g_list_length(cfg->users);i++)
00114 lp_user_free(g_list_nth_data(cfg->users, i));
00115 g_list_free(cfg->users);
00116 for(i=0;i<g_list_length(cfg->records);i++)
00117 lp_record_free(g_list_nth_data(cfg->records, i));
00118 g_list_free(cfg->records);
00119 for(i=0;i<g_list_length(cfg->rsslist);i++)
00120 lp_rss_free(g_list_nth_data(cfg->rsslist, i));
00121 g_list_free(cfg->rsslist);
00122 if(cfg->ident_to)
00123 free(cfg->ident_to);
00124 }
00125