ks_bquery.c

Go to the documentation of this file.
00001 /*
00002  * libkarmaclient - A C Library to the Karmasphere Reputation Server
00003  * Copyright (C) 2006 Karmasphere <http://www.karmasphere.com/>
00004  *  - Shevek <shevek@karmasphere.com>
00005  *  - Dave Stewart <dave.stewart@karmasphere.com>
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
00020  */
00021 
00022 #include "ks_config.h"
00023 #include "ks_bquery.h"
00024 #include "ks_malloc.h"
00025 
00026 #ifdef HAVE_STDIO_H
00027 #include <stdio.h>
00028 #endif
00029 
00030 #ifdef HAVE_STRING_H
00031 #include <string.h>
00032 #endif
00033 
00039 static int      idseq = 0;
00040 
00051 static void
00052 ks_bquery_bappend(ks_string_t *r, ks_base_t *b)
00053 {
00054         ks_bquery_t     *q = KS_CAST(ks_bquery, b);
00055 
00056         ks_string_append(r, "d", 1);
00057 
00058         /* _, auth, combiners, composites, feeds, flags, ids */
00059 
00060         ks_string_append(r, "1:_", 3);
00061         ks_bappend(r, (ks_base_t *)ks_bquery_id_get(q));
00062 
00063         /* See ks_bquery_set_auth() for the magic that ensures that
00064          * q->credentials is correct */
00065         if (q->principal) {
00066                 ks_string_append(r, "1:al", 4);
00067                 ks_bappend(r, (ks_base_t *)q->principal);
00068                 ks_bappend(r, (ks_base_t *)q->credentials);
00069                 ks_string_append(r, "e", 1);
00070         }
00071 
00072         if (ks_array_length(&(q->combiners)) > 0) {
00073                 // ks_string_append(r, "9:combiners", 11);
00074                 ks_string_append(r, "1:c", 3);
00075                 ks_bappend(r, (ks_base_t *)&(q->combiners));
00076         }
00077 
00078         if (ks_array_length(&(q->feeds)) > 0) {
00079                 // ks_string_append(r, "5:feeds", 7);
00080                 ks_string_append(r, "1:f", 3);
00081                 ks_bappend(r, (ks_base_t *)&(q->feeds));
00082         }
00083 
00084         if (ks_number_get(&(q->flags)) != 0) {
00085                 // ks_string_append(r, "5:flags", 7);
00086                 ks_string_append(r, "2:fl", 4);
00087                 ks_bappend(r, (ks_base_t *)&(q->flags));
00088         }
00089 
00090         if (ks_array_length(&(q->identities)) > 0) {
00091                 // ks_string_append(r, "3:ids", 5);
00092                 ks_string_append(r, "1:i", 3);
00093                 ks_bappend(r, (ks_base_t *)&(q->identities));
00094         }
00095 
00096         if (ks_array_length(&(q->composites)) > 0) {
00097                 // ks_string_append(r, "10:composites", 13);
00098                 ks_string_append(r, "1:s", 3);
00099                 ks_bappend(r, (ks_base_t *)&(q->composites));
00100         }
00101 
00102         ks_string_append(r, "e", 1);
00103 }
00104 
00108 static ks_type_t ks_type_bquery = {
00109         "ks_bquery",                     
00110         (ks_destroy_t)ks_bquery_free,    
00111         (ks_bappend_t)ks_bquery_bappend  
00112 };
00113 
00119 ks_type_t *
00120 ks_bquery_type(void)
00121 {
00122         return &ks_type_bquery;
00123 }
00124 
00134 ks_bquery_t *
00135 ks_bquery_new(void)
00136 {
00137         ks_bquery_t     *q = (ks_bquery_t *)ks_malloc(sizeof(ks_bquery_t));
00138         q->base.type = &ks_type_bquery;
00139         ks_array_init(&(q->feeds));
00140         ks_array_init(&(q->composites));
00141         ks_array_init(&(q->combiners));
00142         ks_array_init(&(q->identities));
00143         ks_number_init(&(q->flags), 0);
00144         q->principal = NULL;
00145         q->credentials = NULL;
00146         q->id = NULL;
00147         return q;
00148 }
00149 
00158 void
00159 ks_bquery_free(ks_bquery_t *q)
00160 {
00161         ks_array_fini(&(q->feeds));
00162         ks_array_fini(&(q->composites));
00163         ks_array_fini(&(q->combiners));
00164         ks_array_fini(&(q->identities));
00165         ks_number_fini(&(q->flags));
00166         if (q->id)
00167                 ks_string_free(q->id);
00168         if (q->principal)
00169                 ks_string_free(q->principal);
00170         if (q->credentials)
00171                 ks_string_free(q->credentials);
00172         ks_free(q);
00173 }
00174 
00188 void
00189 ks_bquery_id_set(ks_bquery_t *q, const char *id, size_t len)
00190 {
00191         if (q->id)
00192                 ks_string_set(q->id, id, len);
00193         else
00194                 q->id = ks_string_new(id, len);
00195 }
00196 
00209 ks_string_t *
00210 ks_bquery_id_get(ks_bquery_t *q)
00211 {
00212         char             idbuf[64];
00213         if (q->id == NULL) {
00214                 sprintf(idbuf, "lkc%d", idseq++);
00215                 q->id = ks_string_new(idbuf, strlen(idbuf));
00216         }
00217         return q->id;
00218 }
00219 
00228 void
00229 ks_bquery_feed_add(ks_bquery_t *q, const char *id)
00230 {
00231         ks_string_t     *r = ks_string_new(id, strlen(id));
00232         ks_array_add(&(q->feeds), (ks_base_t *)r);
00233 }
00234 
00244 void
00245 ks_bquery_composite_add(ks_bquery_t *q, const char *id)
00246 {
00247         ks_string_t     *r = ks_string_new(id, strlen(id));
00248         ks_array_add(&(q->composites), (ks_base_t *)r);
00249 }
00250 
00259 void
00260 ks_bquery_combiner_add(ks_bquery_t *q, const char *id)
00261 {
00262         ks_string_t     *r = ks_string_new(id, strlen(id));
00263         ks_array_add(&(q->combiners), (ks_base_t *)r);
00264 }
00265 
00283 static ks_array_t *
00284 ks_bquery_identity_add_ret(ks_bquery_t *q, const char *id, int idt)
00285 {
00286         ks_array_t      *a = ks_array_new();
00287         ks_array_add(a, (ks_base_t *)ks_string_new(id, strlen(id)));
00288         ks_array_add(a, (ks_base_t *)ks_number_new(idt));
00289         ks_array_add(&(q->identities), (ks_base_t *)a);
00290         return a;
00291 }
00292 
00313 void
00314 ks_bquery_identity_add(ks_bquery_t *q, const char *id, int idt)
00315 {
00316         ks_bquery_identity_add_ret(q, id, idt);
00317 }
00318 
00337 void
00338 ks_bquery_identity_add_tagged(ks_bquery_t *q,
00339                                 const char *id, int idt, const char *tag)
00340 {
00341         ks_array_t      *a = ks_bquery_identity_add_ret(q, id, idt);
00342         ks_array_add(a, (ks_base_t *)ks_string_new(tag, strlen(tag)));
00343 }
00344 
00354 void
00355 ks_bquery_flags_set(ks_bquery_t *q, long flags)
00356 {
00357         ks_number_set(&(q->flags), flags);
00358 }
00359 
00369 ks_string_t *
00370 ks_bquery_packet(ks_bquery_t *q)
00371 {
00372         ks_string_t     *r = ks_string_new("", 0);
00373         ks_bappend(r, (ks_base_t *)q);
00374         return r;
00375 }
00376 
00385 int
00386 ks_bquery_has_auth(ks_bquery_t *q)
00387 {
00388         return q->principal != NULL;
00389 }
00390 
00406 void
00407 ks_bquery_set_auth(ks_bquery_t *q, const char *p, const char *c)
00408 {
00409         const char      *creds;
00410         creds = c ? c : "";
00411 
00412         if (p != NULL) {
00413                 if (q->principal != NULL)
00414                         ks_string_set(q->principal, p, strlen(p));
00415                 else
00416                         q->principal = ks_string_new(p, strlen(p));
00417                 if (q->credentials != NULL)
00418                         ks_string_set(q->credentials, creds, strlen(creds));
00419                 else
00420                         q->credentials = ks_string_new(creds, strlen(creds));
00421         }
00422         else {
00423                 if (q->principal != NULL) {
00424                         ks_string_free(q->principal);
00425                         q->principal = NULL;
00426                 }
00427                 if (q->credentials != NULL) {
00428                         ks_string_free(q->credentials);
00429                         q->credentials = NULL;
00430                 }
00431         }
00432 }

Generated on Wed May 9 01:01:56 2007 for libkarmaclient by  doxygen 1.5.1