00001
00002
00003 #include "libjwgc.h"
00004
00005 xode *contact_list = NULL;
00006 int num_contacts = 0;
00007
00008 xode *agent_list = NULL;
00009 int num_agents = 0;
00010
00011 void
00012 insert_resource_into_contact(int pos, char *resource, char *status)
00013 {
00014 int i, k;
00015 xode r, s;
00016
00017 if (!resource) {
00018 return;
00019 }
00020
00021 r = xode_get_tag(contact_list[pos], "resources");
00022 s = xode_insert_tag(r, "resource");
00023 xode_put_attrib(s, "name", resource);
00024 if (status) {
00025 xode_put_attrib(s, "status", status);
00026 }
00027 }
00028
00029 void
00030 insert_into_contact_list(char *contact, char *status, char *resource)
00031 {
00032 int i, k;
00033
00034 num_contacts++;
00035 contact_list = realloc(contact_list, sizeof(xode) * (num_contacts));
00036 contact_list[num_contacts - 1] = malloc(sizeof(xode));
00037
00038 for (i = 0; i < (num_contacts - 1) &&
00039 (strcasecmp(contact, xode_get_attrib(contact_list[i], "jid")) > 0);
00040 i++);
00041
00042 for (k = (num_contacts - 1); k > i; k--) {
00043 contact_list[k] = contact_list[k - 1];
00044 }
00045
00046 contact_list[k] = xode_new("contact");
00047 xode_put_attrib(contact_list[k], "jid", contact);
00048 xode_insert_tag(contact_list[k], "resources");
00049
00050 insert_resource_into_contact(i, resource, status);
00051
00052 dprintf(dExecution, "Inserted %s into contact list at position %d\n", contact, i);
00053 }
00054
00055 void
00056 remove_from_contact_list(char *contact)
00057 {
00058 int i;
00059 int k;
00060
00061 for (i = 0; i < num_contacts &&
00062 (strcasecmp(contact, xode_get_attrib(contact_list[i], "jid")) != 0);
00063 i++);
00064
00065 if (i == num_contacts) {
00066 return;
00067 }
00068
00069 free(contact_list[i]);
00070 for (k = i; k < (num_contacts - 1); k++) {
00071 contact_list[k] = contact_list[k + 1];
00072 }
00073 num_contacts--;
00074
00075 dprintf(dExecution, "Removed %s from contact list at position %d\n", contact, i);
00076 }
00077
00078 void
00079 insert_into_agent_list(char *jid, char *name, char *service, int flags)
00080 {
00081 int i, k;
00082
00083 if (!jid) {
00084 return;
00085 }
00086
00087 num_agents++;
00088 agent_list = realloc(agent_list, sizeof(xode) * (num_agents));
00089 agent_list[num_agents - 1] = malloc(sizeof(xode));
00090
00091 for (i = 0; i < (num_agents - 1) &&
00092 (strcasecmp(jid, xode_get_attrib(agent_list[i], "jid")) > 0);
00093 i++);
00094
00095 for (k = (num_agents - 1); k > i; k--) {
00096 agent_list[k] = agent_list[k - 1];
00097 }
00098
00099 agent_list[k] = xode_new("agent");
00100 xode_put_attrib(agent_list[k], "jid", jid);
00101 if (name) {
00102 xode_put_attrib(agent_list[k], "name", name);
00103 }
00104 if (service) {
00105 xode_put_attrib(agent_list[k], "service", service);
00106 }
00107 if (flags & AGENT_TRANSPORT) {
00108 xode_insert_tag(agent_list[k], "transport");
00109 }
00110 if (flags & AGENT_GROUPCHAT) {
00111 xode_insert_tag(agent_list[k], "groupchat");
00112 }
00113 if (flags & AGENT_REGISTER) {
00114 xode_insert_tag(agent_list[k], "register");
00115 }
00116 if (flags & AGENT_SEARCH) {
00117 xode_insert_tag(agent_list[k], "search");
00118 }
00119
00120 dprintf(dExecution, "Inserted %s into agent list at positiion %d\n", jid, i);
00121 }
00122
00123 int
00124 update_contact_status(char *jid, char *status, char *resource)
00125 {
00126 int contactfound = 0;
00127 int resourcefound = 0;
00128 int i;
00129 char *pos;
00130 xode x, y;
00131 int ret = 0;
00132
00133 dprintf(dExecution, "Updating %s/%s status to %s.\n",
00134 jid,
00135 resource ? resource : "NULL",
00136 status ? status : "NULL");
00137
00138 for (i = 0; i < num_contacts; i++) {
00139 if (!strcasecmp(xode_get_attrib(contact_list[i], "jid"),
00140 jid)) {
00141 contactfound = 1;
00142 if (!resource) {
00143 break;
00144 }
00145 x = xode_get_tag(contact_list[i], "resources");
00146 y = xode_get_firstchild(x);
00147 while (y) {
00148 if (!strcmp(xode_get_attrib(y, "name"),
00149 resource)) {
00150 char *curstatus;
00151 resourcefound = 1;
00152
00153 curstatus = xode_get_attrib(y,
00154 "status");
00155 if (curstatus && !status) {
00156 xode_hide_attrib(y, "status");
00157 ret = 1;
00158 }
00159 else if (status && (!curstatus ||
00160 strcmp(curstatus, status))) {
00161 xode_put_attrib(y,
00162 "status",
00163 status);
00164 ret = 1;
00165 }
00166 break;
00167 }
00168 y = xode_get_nextsibling(y);
00169 }
00170
00171 if (!resourcefound) {
00172 insert_resource_into_contact(i,
00173 resource, status);
00174 ret = 1;
00175 }
00176
00177 break;
00178 }
00179 }
00180
00181 if (!contactfound) {
00182 insert_into_contact_list(jid, status, resource);
00183 ret = 1;
00184 }
00185
00186 return ret;
00187 }
00188
00189 int
00190 contact_status_change(jabpacket packet)
00191 {
00192 char *temp, *pos;
00193 xode x;
00194 char *from = xode_get_attrib(packet->x, "from");
00195 char *resource = "NULL";
00196 int ret = 0;
00197 if (!from) {
00198 return;
00199 }
00200
00201 pos = (char *) strchr(from, '/');
00202 if (pos) {
00203 *pos = '\0';
00204 resource = pos + 1;
00205 }
00206
00207 switch (packet->subtype) {
00208 case JABPACKET__AVAILABLE:
00209 x = xode_get_tag(packet->x, "show");
00210 if (x) {
00211 temp = xode_get_data(x);
00212 update_contact_status(from, temp, resource);
00213 ret = 1;
00214 }
00215 else {
00216 update_contact_status(from, "available", resource);
00217 ret = 1;
00218 }
00219
00220 dprintf(dExecution, "%s is available\n", from);
00221 break;
00222
00223 case JABPACKET__UNAVAILABLE:
00224 dprintf(dExecution, "%s is unavailable\n", from);
00225 update_contact_status(from, "unavailable", resource);
00226 ret = 1;
00227 break;
00228
00229 default:
00230 dprintf(dExecution, "%s is unknown(?) -> %d\n", from, packet->subtype);
00231 update_contact_status(from, "unknown", resource);
00232 ret = 1;
00233 break;
00234 }
00235 }
00236
00237 void
00238 list_contacts(jwg, matchstr, strictmatch, skipnotavail)
00239 jwgconn jwg;
00240 char *matchstr;
00241 int strictmatch;
00242 int skipnotavail;
00243 {
00244 int i, k;
00245 xode x, y, ny, z, r, nr;
00246
00247 x = xode_new("contacts");
00248 for (i = 0; i < num_contacts; i++) {
00249 y = xode_dup(contact_list[i]);
00250 xode_insert_node(x, y);
00251 }
00252
00253 if (matchstr) {
00254 ny = y = xode_get_firstchild(x);
00255 while (ny) {
00256 ny = xode_get_nextsibling(ny);
00257 if (!test_match(matchstr, y, strictmatch)) {
00258 dprintf(dXML, "HIDE-A:\n%s\n",
00259 xode_to_prettystr(y));
00260 xode_hide(y);
00261 }
00262 y = ny;
00263 }
00264 }
00265
00266 if (skipnotavail) {
00267 ny = y = xode_get_firstchild(x);
00268 while (ny) {
00269 z = xode_get_tag(y, "resources");
00270 nr = r = xode_get_firstchild(z);
00271 while (nr) {
00272 char *curstatus;
00273 nr = xode_get_nextsibling(nr);
00274 curstatus = xode_get_attrib(r, "status");
00275 if (!curstatus || !strcmp(curstatus,
00276 "unavailable")) {
00277 dprintf(dXML, "HIDE-B:\n%s\n",
00278 xode_to_prettystr(r));
00279 xode_hide(r);
00280 }
00281 r = nr;
00282 }
00283 ny = xode_get_nextsibling(ny);
00284 if (!xode_has_children(z)) {
00285 dprintf(dXML, "HIDE-C:\n%s\n",
00286 xode_to_prettystr(y));
00287 xode_hide(y);
00288 }
00289 y = ny;
00290 }
00291 }
00292
00293 jwg_servsend(jwg, x);
00294 xode_free(x);
00295 }
00296
00297 xode
00298 find_contact_group(base, target)
00299 xode base;
00300 char *target;
00301 {
00302 xode x;
00303 char *name;
00304
00305 if (!target) {
00306 return NULL;
00307 }
00308
00309 x = xode_get_tag(base, "group");
00310 while (x) {
00311 name = xode_get_attrib(x, "name");
00312 if (name && !strcmp(name, target)) {
00313 return x;
00314 }
00315 x = xode_get_nextsibling(x);
00316 }
00317
00318 return NULL;
00319 }
00320
00321 void
00322 list_contacts_bygroup(jwg, matchstr, strictmatch, skipnotavail)
00323 jwgconn jwg;
00324 char *matchstr;
00325 int strictmatch;
00326 int skipnotavail;
00327 {
00328 int i, k;
00329 xode x, y, ny, z, r, nr, g, ng;
00330 char *group;
00331
00332 x = xode_new("contacts");
00333 for (i = 0; i < num_contacts; i++) {
00334 y = xode_dup(contact_list[i]);
00335 group = xode_get_attrib(y, "group");
00336 if (!group) {
00337 group = "Unknown";
00338 }
00339 z = find_contact_group(x, group);
00340 if (!z) {
00341 z = xode_insert_tag(x, "group");
00342 xode_put_attrib(z, "name", group);
00343 }
00344 xode_hide_attrib(y, "group");
00345 xode_insert_node(z, y);
00346 }
00347
00348 if (matchstr) {
00349 ng = g = xode_get_firstchild(x);
00350 while (ng) {
00351 ny = y = xode_get_firstchild(g);
00352 while (ny) {
00353 ny = xode_get_nextsibling(y);
00354 if (!test_match(matchstr, y, strictmatch)) {
00355 dprintf(dXML,
00356 "HIDE-D:\n%s\n",
00357 xode_to_prettystr(y));
00358 xode_hide(y);
00359 }
00360 y = ny;
00361 }
00362
00363 ng = xode_get_nextsibling(ng);
00364 if (!xode_has_children(g)) {
00365 dprintf(dXML, "HIDE-E:\n%s\n",
00366 xode_to_prettystr(g));
00367 xode_hide(g);
00368 }
00369 g = ng;
00370 }
00371 }
00372
00373 if (skipnotavail) {
00374 ng = g = xode_get_firstchild(x);
00375 while (ng) {
00376 ny = y = xode_get_firstchild(g);
00377 while (ny) {
00378 z = xode_get_tag(y, "resources");
00379 nr = r = xode_get_firstchild(z);
00380 while (nr) {
00381 char *curstatus;
00382 nr = xode_get_nextsibling(nr);
00383 curstatus = xode_get_attrib(r,
00384 "status");
00385 if (!curstatus || !strcmp(curstatus,
00386 "unavailable")) {
00387 dprintf(dXML,
00388 "HIDE-F:\n%s\n",
00389 xode_to_prettystr(r));
00390 xode_hide(r);
00391 }
00392 r = nr;
00393 }
00394
00395 ny = xode_get_nextsibling(ny);
00396 if (!xode_has_children(z)) {
00397 dprintf(dXML,
00398 "HIDE-G:\n%s\n",
00399 xode_to_prettystr(y));
00400 xode_hide(y);
00401 }
00402 y = ny;
00403 }
00404
00405 ng = xode_get_nextsibling(ng);
00406 if (!xode_has_children(g)) {
00407 dprintf(dXML, "HIDE-H:\n%s\n",
00408 xode_to_prettystr(g));
00409 xode_hide(g);
00410 }
00411 g = ng;
00412 }
00413 }
00414
00415 jwg_servsend(jwg, x);
00416 xode_free(x);
00417 }
00418
00419 void
00420 list_agents(xode x)
00421 {
00422 int i;
00423 xode y, z;
00424
00425 y = xode_insert_tag(x, "agents");
00426 for (i = 0; i < num_agents; i++) {
00427 z = xode_dup(agent_list[i]);
00428 xode_insert_node(y, z);
00429 }
00430 }
00431
00432 void
00433 update_nickname(char *target, char *nickname)
00434 {
00435 int i;
00436
00437 for (i = 0; i < num_contacts; i++) {
00438 if (!strcasecmp(xode_get_attrib(contact_list[i], "jid"),
00439 target)) {
00440 xode_put_attrib(contact_list[i], "nick", nickname);
00441 return;
00442 }
00443 }
00444 }
00445
00446 void
00447 update_group(char *target, char *group)
00448 {
00449 int i;
00450
00451 for (i = 0; i < num_contacts; i++) {
00452 if (!strcasecmp(xode_get_attrib(contact_list[i], "jid"),
00453 target)) {
00454 xode_put_attrib(contact_list[i], "group", group);
00455 return;
00456 }
00457 }
00458 }
00459
00460 char *
00461 find_jid_from_nickname(char *nickname)
00462 {
00463 int i;
00464 char *name;
00465
00466 for (i = 0; i < num_contacts; i++) {
00467 name = xode_get_attrib(contact_list[i], "nick");
00468 if (name && !strcasecmp(name, nickname)) {
00469 return strdup(xode_get_attrib(contact_list[i], "jid"));
00470 }
00471 }
00472
00473 return NULL;
00474 }
00475
00476 char *
00477 find_nickname_from_jid(char *jid)
00478 {
00479 int i;
00480 char *name, *nick;
00481
00482 for (i = 0; i < num_contacts; i++) {
00483 name = xode_get_attrib(contact_list[i], "jid");
00484 if (name && !strcasecmp(name, jid)) {
00485 nick = xode_get_attrib(contact_list[i], "nick");
00486 if (nick) {
00487 return strdup(nick);
00488 }
00489 else {
00490 return strdup("");
00491 }
00492 }
00493 }
00494
00495 return NULL;
00496 }
00497
00498 int
00499 test_match(matchstr, contact, exactmatch)
00500 char *matchstr;
00501 xode contact;
00502 int exactmatch;
00503 {
00504 char *nick;
00505
00506 if (!strcasecmp(xode_get_attrib(contact, "jid"), matchstr)) {
00507 return 1;
00508 }
00509
00510 if ((nick = xode_get_attrib(contact, "nick")) != NULL &&
00511 !strcasecmp(nick, matchstr)) {
00512 return 1;
00513 }
00514
00515 if (!exactmatch && !strncasecmp(xode_get_attrib(contact, "jid"),
00516 matchstr, strlen(matchstr))) {
00517 return 1;
00518 }
00519
00520 return 0;
00521 }
00522
00523 char *
00524 find_match(char *searchstr)
00525 {
00526 int i;
00527 char *jid, *nick;
00528
00529
00530 for (i = 0; i < num_contacts; i++) {
00531 jid = xode_get_attrib(contact_list[i], "jid");
00532 dprintf(dMatch, "Exact Match: %s = %s\n", searchstr, jid);
00533 if (!strcasecmp(jid, searchstr)) {
00534 return strdup(jid);
00535 }
00536 }
00537
00538
00539 for (i = 0; i < num_contacts; i++) {
00540 jid = xode_get_attrib(contact_list[i], "jid");
00541 nick = xode_get_attrib(contact_list[i], "nick");
00542 if (!nick) {
00543 continue;
00544 }
00545 dprintf(dMatch, "Nick Match: %s = %s\n", searchstr, nick);
00546 if (!strcasecmp(nick, searchstr)) {
00547 return strdup(jid);
00548 }
00549 }
00550
00551
00552 for (i = 0; i < num_contacts; i++) {
00553 jid = xode_get_attrib(contact_list[i], "jid");
00554 dprintf(dMatch, "Part Match: %s = %s\n", searchstr, jid);
00555 if (!strncasecmp(jid, searchstr, strlen(searchstr))) {
00556 return strdup(jid);
00557 }
00558 }
00559
00560
00561 return NULL;
00562 }