root/trunk/src/requests.c

Revision 680, 3.6 kB (checked in by jajcus, 3 years ago)

- dates in the copyright header updated

Line 
1 /* $Id: requests.c,v 1.37 2004/03/16 19:30:25 mmazur Exp $ */
2
3 /*
4  *  (C) Copyright 2002-2006 Jacek Konieczny [jajcus(a)jajcus,net]
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License Version 2 as
8  *  published by the Free Software Foundation.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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 }
Note: See TracBrowser for help on using the browser.