00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044 #include "libjabber.h"
00045 #include "libjwgc.h"
00046 #include "libxode.h"
00047
00048
00049 xode
00050 jabutil_presnew(int type, char *to, char *status, int priority)
00051 {
00052 xode pres;
00053 char pribuff[32];
00054
00055 pres = xode_new("presence");
00056 switch (type) {
00057 case JABPACKET__SUBSCRIBE:
00058 xode_put_attrib(pres, "type", "subscribe");
00059 break;
00060 case JABPACKET__UNSUBSCRIBE:
00061 xode_put_attrib(pres, "type", "unsubscribe");
00062 break;
00063 case JABPACKET__SUBSCRIBED:
00064 xode_put_attrib(pres, "type", "subscribed");
00065 break;
00066 case JABPACKET__UNSUBSCRIBED:
00067 xode_put_attrib(pres, "type", "unsubscribed");
00068 break;
00069 case JABPACKET__PROBE:
00070 xode_put_attrib(pres, "type", "probe");
00071 break;
00072 case JABPACKET__UNAVAILABLE:
00073 xode_put_attrib(pres, "type", "unavailable");
00074 break;
00075 case JABPACKET__INVISIBLE:
00076 xode_put_attrib(pres, "type", "invisible");
00077 break;
00078 }
00079 if (to != NULL)
00080 xode_put_attrib(pres, "to", to);
00081 #ifdef USE_GPGME
00082 if (*(int *)jVars_get(jVarUseGPG)) {
00083 char *signature;
00084 char *tmpstatus;
00085
00086 if (status == NULL) {
00087 tmpstatus = "online";
00088 }
00089 else {
00090 tmpstatus = status;
00091 }
00092
00093 signature = JTrimPGPMessage(JSign(tmpstatus));
00094 if (signature) {
00095 xode x;
00096
00097 dprintf(dExecution, "Adding status signature.\n");
00098 x = xode_insert_tag(pres, "x");
00099 xode_put_attrib(x, "xmlns", NS_SIGNED);
00100 xode_insert_cdata(x, signature, strlen(signature));
00101 status = tmpstatus;
00102 }
00103 }
00104 #endif
00105 if (status != NULL)
00106 xode_insert_cdata(xode_insert_tag(pres, "status"), status, strlen(status));
00107 if (priority >= 0) {
00108 snprintf(pribuff, sizeof(pribuff), "%d", priority);
00109 xode_insert_cdata(xode_insert_tag(pres, "priority"), pribuff, strlen(pribuff));
00110 }
00111
00112 return pres;
00113 }
00114
00115
00116 xode
00117 jabutil_iqnew(int type, char *ns)
00118 {
00119 xode iq;
00120
00121 iq = xode_new("iq");
00122 switch (type) {
00123 case JABPACKET__GET:
00124 xode_put_attrib(iq, "type", "get");
00125 break;
00126 case JABPACKET__SET:
00127 xode_put_attrib(iq, "type", "set");
00128 break;
00129 case JABPACKET__RESULT:
00130 xode_put_attrib(iq, "type", "result");
00131 break;
00132 case JABPACKET__ERROR:
00133 xode_put_attrib(iq, "type", "error");
00134 break;
00135 }
00136 xode_put_attrib(xode_insert_tag(iq, "query"), "xmlns", ns);
00137
00138 return iq;
00139 }
00140
00141
00142 xode
00143 jabutil_msgnew(char *type, char *to, char *subj, char *body, char *encrypt)
00144 {
00145 xode msg;
00146
00147 msg = xode_new("message");
00148 xode_put_attrib(msg, "type", type);
00149 xode_put_attrib(msg, "to", to);
00150
00151 if (subj) {
00152 xode_insert_cdata(xode_insert_tag(msg, "subject"), subj, strlen(subj));
00153 }
00154
00155 if (encrypt) {
00156 xode x;
00157
00158 x = xode_insert_tag(msg, "x");
00159 xode_put_attrib(x, "xmlns", NS_ENCRYPTED);
00160 xode_insert_cdata(x, encrypt, strlen(encrypt));
00161 }
00162
00163 {
00164 xode x;
00165
00166 x = xode_insert_tag(msg, "x");
00167 xode_put_attrib(x, "xmlns", NS_EVENT);
00168 xode_insert_tag(x, "composing");
00169 }
00170
00171 if (body) {
00172 xode_insert_cdata(xode_insert_tag(msg, "body"), body, strlen(body));
00173 }
00174
00175 return msg;
00176 }
00177
00178
00179 xode
00180 jabutil_pingnew(char *type, char *to)
00181 {
00182 xode msg;
00183 xode x, y;
00184
00185 msg = xode_new("message");
00186 xode_put_attrib(msg, "type", type);
00187 xode_put_attrib(msg, "to", to);
00188
00189 x = xode_insert_tag(msg, "x");
00190 xode_put_attrib(x, "xmlns", NS_EVENT);
00191 xode_insert_tag(x, "composing");
00192
00193 return msg;
00194 }
00195
00196
00197 xode
00198 jabutil_header(char *xmlns, char *server)
00199 {
00200 xode result;
00201 if ((xmlns == NULL) || (server == NULL))
00202 return NULL;
00203 result = xode_new("stream:stream");
00204 xode_put_attrib(result, "xmlns:stream", "http://etherx.jabber.org/streams");
00205 xode_put_attrib(result, "xmlns", xmlns);
00206 xode_put_attrib(result, "to", server);
00207
00208 return result;
00209 }
00210
00211
00212 int
00213 jabutil_priority(xode x)
00214 {
00215 char *str;
00216 int p;
00217
00218 if (x == NULL)
00219 return -1;
00220
00221 if (xode_get_attrib(x, "type") != NULL)
00222 return -1;
00223
00224 x = xode_get_tag(x, "priority");
00225 if (x == NULL)
00226 return 0;
00227
00228 str = xode_get_data((x));
00229 if (str == NULL)
00230 return 0;
00231
00232 p = atoi(str);
00233 if (p >= 0)
00234 return p;
00235 else
00236 return 0;
00237 }
00238
00239 void
00240 jabutil_tofrom(xode x)
00241 {
00242 char *to, *from;
00243
00244 to = xode_get_attrib(x, "to");
00245 from = xode_get_attrib(x, "from");
00246 xode_put_attrib(x, "from", to);
00247 xode_put_attrib(x, "to", from);
00248 }
00249
00250 xode
00251 jabutil_iqresult(xode x)
00252 {
00253 xode cur;
00254
00255 jabutil_tofrom(x);
00256
00257 xode_put_attrib(x, "type", "result");
00258
00259
00260 for (cur = xode_get_firstchild(x); cur != NULL; cur = xode_get_nextsibling(cur))
00261 xode_hide(cur);
00262
00263 return x;
00264 }
00265
00266 char *
00267 jabutil_timestamp(void)
00268 {
00269 time_t t;
00270 struct tm *new_time;
00271 static char timestamp[18];
00272 int ret;
00273
00274 t = time(NULL);
00275
00276 if (t == (time_t) - 1)
00277 return NULL;
00278 new_time = gmtime(&t);
00279
00280 ret = snprintf(timestamp, 18, "%d%02d%02dT%02d:%02d:%02d", 1900 + new_time->tm_year,
00281 new_time->tm_mon + 1, new_time->tm_mday, new_time->tm_hour,
00282 new_time->tm_min, new_time->tm_sec);
00283
00284 if (ret == -1)
00285 return NULL;
00286
00287 return timestamp;
00288 }
00289
00290 void
00291 jabutil_error(xode x, terror E)
00292 {
00293 xode err;
00294 char code[4];
00295
00296 xode_put_attrib(x, "type", "error");
00297 err = xode_insert_tag(x, "error");
00298
00299 snprintf(code, 4, "%d", E.code);
00300 xode_put_attrib(err, "code", code);
00301 if (E.msg != NULL)
00302 xode_insert_cdata(err, E.msg, strlen(E.msg));
00303
00304 jabutil_tofrom(x);
00305 }
00306
00307 void
00308 jabutil_delay(xode msg, char *reason)
00309 {
00310 xode delay;
00311
00312 delay = xode_insert_tag(msg, "x");
00313 xode_put_attrib(delay, "xmlns", NS_DELAY);
00314 xode_put_attrib(delay, "from", xode_get_attrib(msg, "to"));
00315 xode_put_attrib(delay, "stamp", jabutil_timestamp());
00316 if (reason != NULL)
00317 xode_insert_cdata(delay, reason, strlen(reason));
00318 }
00319
00320 #define KEYBUF 100
00321
00322 char *
00323 jabutil_regkey(char *key, char *seed)
00324 {
00325 static char keydb[KEYBUF][41];
00326 static char seeddb[KEYBUF][41];
00327 static int last = -1;
00328 char *str, strint[32];
00329 int i;
00330
00331
00332 if (last == -1) {
00333 last = 0;
00334 memset(&keydb, 0, KEYBUF * 41);
00335 memset(&seeddb, 0, KEYBUF * 41);
00336 srand(time(NULL));
00337 }
00338
00339
00340 if (key == NULL && seed != NULL) {
00341
00342 sprintf(strint, "%d", rand());
00343 strcpy(keydb[last], j_shahash(strint));
00344
00345
00346 strcpy(seeddb[last], j_shahash(seed));
00347
00348
00349 str = keydb[last];
00350 last++;
00351 if (last == KEYBUF)
00352 last = 0;
00353 return str;
00354 }
00355
00356
00357 str = j_shahash(seed);
00358 for (i = 0; i < KEYBUF; i++)
00359 if (j_strcmp(keydb[i], key) == 0 && j_strcmp(seeddb[i], str) == 0) {
00360 seeddb[i][0] = '\0';
00361 return keydb[i];
00362 }
00363
00364 return NULL;
00365 }
00366
00367 char *
00368 jab_type_to_ascii(j_type)
00369 int j_type;
00370 {
00371 char *result;
00372
00373 switch (j_type) {
00374 case JABPACKET_UNKNOWN:
00375 result = "unknown";
00376 break;
00377
00378 case JABPACKET_MESSAGE:
00379 result = "message";
00380 break;
00381
00382 case JABPACKET_PRESENCE:
00383 result = "presence";
00384 break;
00385
00386 case JABPACKET_IQ:
00387 result = "iq";
00388 break;
00389
00390 default:
00391 result = "<unknown type>";
00392 break;
00393 }
00394
00395 return (strdup(result));
00396 }
00397
00398 char *
00399 jab_subtype_to_ascii(j_subtype)
00400 int j_subtype;
00401 {
00402 char *result;
00403
00404 switch (j_subtype) {
00405 case JABPACKET__UNKNOWN:
00406 result = "unknown";
00407 break;
00408
00409 case JABPACKET__NONE:
00410 result = "none";
00411 break;
00412
00413 case JABPACKET__ERROR:
00414 result = "error";
00415 break;
00416
00417 case JABPACKET__CHAT:
00418 result = "chat";
00419 break;
00420
00421 case JABPACKET__GROUPCHAT:
00422 result = "groupchat";
00423 break;
00424
00425 case JABPACKET__INTERNAL:
00426 result = "internal";
00427 break;
00428
00429 case JABPACKET__GET:
00430 result = "get";
00431 break;
00432
00433 case JABPACKET__SET:
00434 result = "set";
00435 break;
00436
00437 case JABPACKET__RESULT:
00438 result = "result";
00439 break;
00440
00441 case JABPACKET__SUBSCRIBE:
00442 result = "subscribe";
00443 break;
00444
00445 case JABPACKET__UNSUBSCRIBE:
00446 result = "unsubscribe";
00447 break;
00448
00449 case JABPACKET__UNSUBSCRIBED:
00450 result = "unsubscribed";
00451 break;
00452
00453 case JABPACKET__AVAILABLE:
00454 result = "available";
00455 break;
00456
00457 case JABPACKET__UNAVAILABLE:
00458 result = "unavailable";
00459 break;
00460
00461 case JABPACKET__PROBE:
00462 result = "probe";
00463 break;
00464
00465 case JABPACKET__HEADLINE:
00466 result = "headline";
00467 break;
00468
00469 default:
00470 result = "<unknown subtype>";
00471 break;
00472 }
00473
00474 return (strdup(result));
00475 }
00476
00477 char *
00478 jab_contype_to_ascii(j_contype)
00479 int j_contype;
00480 {
00481 char *result;
00482
00483 switch (j_contype) {
00484 case JABCONN_STATE_OFF:
00485 result = "off";
00486 break;
00487
00488 case JABCONN_STATE_CONNECTED:
00489 result = "connected";
00490 break;
00491
00492 case JABCONN_STATE_ON:
00493 result = "on";
00494 break;
00495
00496 case JABCONN_STATE_AUTH:
00497 result = "auth";
00498 break;
00499
00500 default:
00501 result = "<unknown connection type>";
00502 break;
00503 }
00504
00505 return (strdup(result));
00506 }