comparison mcabber/src/jab_iq.c @ 683:c5e0d8c3f00c

Introduce new IQ system
author Mikael Berthe <mikael@lilotux.net>
date Mon, 06 Feb 2006 19:57:00 +0100
parents 44ddf9bec3a5
children 3282276e7413
comparison
equal deleted inserted replaced
682:f3945593432e 683:c5e0d8c3f00c
30 #include "roster.h" 30 #include "roster.h"
31 #include "utils.h" 31 #include "utils.h"
32 #include "screen.h" 32 #include "screen.h"
33 #include "settings.h" 33 #include "settings.h"
34 34
35
35 int s_id; // XXX 36 int s_id; // XXX
37
38 static GSList *iqs_list;
39
40
41 // iqs_new(type, namespace, prefix, timeout)
42 // Create a query (GET, SET) IQ structure. This function should not be used
43 // for RESULT packets.
44 iqs *iqs_new(guint8 type, const char *ns, const char *prefix, time_t timeout)
45 {
46 static guint iqs_idn;
47 iqs *new_iqs;
48 time_t now_t;
49
50 iqs_idn++;
51
52 new_iqs = g_new0(iqs, 1);
53 time(&now_t);
54 new_iqs->ts_create = now_t;
55 if (timeout)
56 new_iqs->ts_expire = now_t + timeout;
57 new_iqs->type = type;
58 new_iqs->xmldata = jutil_iqnew(type, (char*)ns);
59 if (prefix)
60 new_iqs->id = g_strdup_printf("%s%d", prefix, iqs_idn);
61 else
62 new_iqs->id = g_strdup_printf("%d", iqs_idn);
63 xmlnode_put_attrib(new_iqs->xmldata, "id", new_iqs->id);
64
65 iqs_list = g_slist_append(iqs_list, new_iqs);
66 return new_iqs;
67 }
68
69 int iqs_del(const char *iqid)
70 {
71 GSList *p;
72 iqs *i;
73
74 if (!iqid) return 1;
75
76 for (p = iqs_list; p; p = g_slist_next(p)) {
77 i = p->data;
78 if (!strcmp(iqid, i->id))
79 break;
80 }
81 if (p) {
82 g_free(i->id);
83 if (i->xmldata) xmlnode_free(i->xmldata);
84 // XXX Should we free i->data?
85 g_free(i);
86 iqs_list = g_slist_remove(iqs_list, p->data);
87 return 0; // Ok, deleted
88 }
89 return -1; // Not found
90 }
91
92 static iqs *iqs_find(const char *iqid)
93 {
94 GSList *p;
95 iqs *i;
96
97 if (!iqid) return NULL;
98
99 for (p = iqs_list; p; p = g_slist_next(p)) {
100 i = p->data;
101 if (!strcmp(iqid, i->id))
102 return i;
103 }
104 return NULL;
105 }
106
107 // iqs_callback(iqid, xml_result)
108 // Callback processing for the iqid message.
109 // If we've received an answer, xml_result should point to the xmldata packet.
110 // If this is a timeout, xml_result should be NULL.
111 // Return 0 in case of success, -1 if the iqid hasn't been found.
112 int iqs_callback(const char *iqid, xmlnode xml_result)
113 {
114 iqs *i;
115
116 i = iqs_find(iqid);
117 if (!i) return -1;
118
119 // IQ processing
120 // Note: If xml_result is NULL, this is a timeout
121 if (i->callback)
122 (*i->callback)(i, xml_result);
123
124 iqs_del(iqid);
125 return 0;
126 }
127
128 void iqs_check_timeout(void)
129 {
130 GSList *p;
131 iqs *i;
132 time_t now_t;
133
134 time(&now_t);
135
136 for (p = iqs_list; p; p = g_slist_next(p)) {
137 i = p->data;
138 if ((!i->ts_expire && now_t > i->ts_create + IQS_MAX_TIMEOUT) ||
139 (i->ts_expire && now_t > i->ts_expire)) {
140 iqs_callback(i->id, NULL);
141 }
142 }
143 }
36 144
37 static void request_roster(void) 145 static void request_roster(void)
38 { 146 {
39 xmlnode x = jutil_iqnew(JPACKET__GET, NS_ROSTER); 147 xmlnode x = jutil_iqnew(JPACKET__GET, NS_ROSTER);
40 xmlnode_put_attrib(x, "id", "Roster1"); // XXX 148 xmlnode_put_attrib(x, "id", "Roster1"); // XXX