| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
#include "ggtrans.h" |
|---|
| 21 |
#include <stdio.h> |
|---|
| 22 |
#include <libxode.h> |
|---|
| 23 |
#include <libgadu.h> |
|---|
| 24 |
#include <time.h> |
|---|
| 25 |
#include <message.h> |
|---|
| 26 |
#include "requests.h" |
|---|
| 27 |
#include "search.h" |
|---|
| 28 |
#include "sessions.h" |
|---|
| 29 |
#include "stream.h" |
|---|
| 30 |
#include "register.h" |
|---|
| 31 |
#include "iq.h" |
|---|
| 32 |
#include "debug.h" |
|---|
| 33 |
|
|---|
| 34 |
static GList *requests=NULL; |
|---|
| 35 |
static GHashTable *lookups=NULL; |
|---|
| 36 |
static int id_counter=0; |
|---|
| 37 |
|
|---|
| 38 |
int requests_init(){ |
|---|
| 39 |
|
|---|
| 40 |
id_counter=time(NULL); |
|---|
| 41 |
lookups=g_hash_table_new(g_int_hash, g_int_equal); |
|---|
| 42 |
if(!lookups) |
|---|
| 43 |
return 1; |
|---|
| 44 |
return 0; |
|---|
| 45 |
} |
|---|
| 46 |
|
|---|
| 47 |
void request_response_search(struct gg_event *data){ |
|---|
| 48 |
int hash; |
|---|
| 49 |
Request *r; |
|---|
| 50 |
|
|---|
| 51 |
hash=gg_pubdir50_seq(data->event.pubdir50); |
|---|
| 52 |
r=g_hash_table_lookup(lookups, &hash); |
|---|
| 53 |
g_hash_table_remove(lookups, &hash); |
|---|
| 54 |
|
|---|
| 55 |
if (r){ |
|---|
| 56 |
switch(r->type){ |
|---|
| 57 |
case RT_VCARD: |
|---|
| 58 |
vcard_done(r, data->event.pubdir50); |
|---|
| 59 |
remove_request(r); |
|---|
| 60 |
return; |
|---|
| 61 |
case RT_SEARCH: |
|---|
| 62 |
search_done(r, data->event.pubdir50); |
|---|
| 63 |
remove_request(r); |
|---|
| 64 |
return; |
|---|
| 65 |
default: |
|---|
| 66 |
break; |
|---|
| 67 |
} |
|---|
| 68 |
} |
|---|
| 69 |
|
|---|
| 70 |
} |
|---|
| 71 |
|
|---|
| 72 |
void request_response_write(struct gg_event *data){ |
|---|
| 73 |
int hash; |
|---|
| 74 |
Request *r; |
|---|
| 75 |
|
|---|
| 76 |
hash=gg_pubdir50_seq(data->event.pubdir50); |
|---|
| 77 |
debug("got public directory write response (id: %i)",hash); |
|---|
| 78 |
r=g_hash_table_lookup(lookups, &hash); |
|---|
| 79 |
if (r==NULL){ |
|---|
| 80 |
debug("no associated request found"); |
|---|
| 81 |
return; |
|---|
| 82 |
} |
|---|
| 83 |
g_hash_table_remove(lookups, &hash); |
|---|
| 84 |
|
|---|
| 85 |
if (r->type!=RT_CHANGE){ |
|---|
| 86 |
if (r->from!=NULL && r->to!=NULL && r->id!=NULL) |
|---|
| 87 |
jabber_iq_send_error(r->stream,r->from,r->to,r->id, |
|---|
| 88 |
500,_("Got public directory change result for unknown request type.")); |
|---|
| 89 |
return; |
|---|
| 90 |
} |
|---|
| 91 |
if (r->from!=NULL && r->to!=NULL && r->id!=NULL && r->query!=NULL){ |
|---|
| 92 |
debug("Query defined in request - sending result."); |
|---|
| 93 |
jabber_iq_send_result(r->stream,r->from,r->to,r->id,NULL); |
|---|
| 94 |
} |
|---|
| 95 |
else |
|---|
| 96 |
debug("Query not defined in request - not sending result."); |
|---|
| 97 |
} |
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 |
Request * add_request(RequestType type,const char *from,const char *to, |
|---|
| 101 |
const char *id,xmlnode query, void *data, |
|---|
| 102 |
Stream *stream){ |
|---|
| 103 |
Request *r; |
|---|
| 104 |
Session *s; |
|---|
| 105 |
|
|---|
| 106 |
r=g_new0(Request,1); |
|---|
| 107 |
r->type=type; |
|---|
| 108 |
r->id=g_strdup(id); |
|---|
| 109 |
r->from=g_strdup(from); |
|---|
| 110 |
if (to) r->to=g_strdup(to); |
|---|
| 111 |
else r->to=NULL; |
|---|
| 112 |
if (query) r->query=xmlnode_dup(query); |
|---|
| 113 |
else r->query=NULL; |
|---|
| 114 |
r->stream=stream; |
|---|
| 115 |
|
|---|
| 116 |
if (type==RT_VCARD || type==RT_SEARCH || type==RT_CHANGE) { |
|---|
| 117 |
s=session_get_by_jid(from, stream,0); |
|---|
| 118 |
if (s==NULL) return NULL; |
|---|
| 119 |
|
|---|
| 120 |
r->hash=id_counter++; |
|---|
| 121 |
gg_pubdir50_seq_set((gg_pubdir50_t)data, r->hash); |
|---|
| 122 |
gg_pubdir50(s->ggs, (gg_pubdir50_t)data); |
|---|
| 123 |
g_hash_table_insert(lookups, &r->hash, r); |
|---|
| 124 |
} |
|---|
| 125 |
requests=g_list_append(requests,r); |
|---|
| 126 |
return r; |
|---|
| 127 |
} |
|---|
| 128 |
|
|---|
| 129 |
int remove_request(Request *r){ |
|---|
| 130 |
|
|---|
| 131 |
if (!r) return -1; |
|---|
| 132 |
requests=g_list_remove(requests,r); |
|---|
| 133 |
if (r->from) g_free(r->from); |
|---|
| 134 |
if (r->to) g_free(r->to); |
|---|
| 135 |
if (r->id) g_free(r->id); |
|---|
| 136 |
if (r->query) xmlnode_free(r->query); |
|---|
| 137 |
if (r->gghttp){ |
|---|
| 138 |
if (r->gghttp->destroy) |
|---|
| 139 |
r->gghttp->destroy(r->gghttp); |
|---|
| 140 |
else |
|---|
| 141 |
gg_http_free(r->gghttp); |
|---|
| 142 |
} |
|---|
| 143 |
|
|---|
| 144 |
g_free(r); |
|---|
| 145 |
return 0; |
|---|
| 146 |
} |
|---|
| 147 |
|
|---|
| 148 |
void requests_done(){ |
|---|
| 149 |
|
|---|
| 150 |
while(requests) remove_request((Request *)requests->data); |
|---|
| 151 |
g_hash_table_destroy(lookups); |
|---|
| 152 |
} |
|---|