diff options
Diffstat (limited to 'jacs/jacs.c')
-rw-r--r-- | jacs/jacs.c | 231 |
1 files changed, 231 insertions, 0 deletions
diff --git a/jacs/jacs.c b/jacs/jacs.c new file mode 100644 index 0000000..21f3139 --- /dev/null +++ b/jacs/jacs.c @@ -0,0 +1,231 @@ +/* + * Copy me if you can. + * by 20h + */ + +#include <u.h> +#include <libc.h> +#include <auth.h> +#include <mp.h> +#include <libsec.h> +#include "xmlpull.h" +#include "jacs.h" +#include "dat.h" +#include "roster.h" +#include "recv.h" + +#define NAME "jacs - Jabber Service Registry for Plan9" +#define VERSION "2nd ed" +#define OS "Plan9 4th ed" + +int +xmljacc(int sock) +{ + return fprint(sock, "<?xml version=\"1.0\"?>\n"); +} + +int +loginjacc(int sock, char *serv) +{ + return fprint(sock, "<stream:stream xmlns:stream=\"http://etherx.jabber.org/streams\"" + " xmlns=\"jabber:client\" to=\"%s\">\n", serv); +} + +int +userjacc(int sock, char *user, char *pass, char *res) +{ + return fprint(sock, "<iq type=\"set\" id=\"auth_1\">\n" + "<query xmlns=\"jabber:iq:auth\">\n" + "<username>%s</username>\n" + "<password>%s</password>\n" + "<resource>%s</resource>\n" + "</query>\n" + "</iq>\n", user, pass, res); +} + +int +presencejacc(int sock, char *stat, char *show, char *from, char *to) +{ + return fprint(sock, "<presence%s%s%s%s%s%s>\n" + "<show>%s</show>\n" + "<status>%s</status>\n" + "<priority>1</priority>\n" + "</presence>\n", (from != nil) ? " from=\"" : "", + (from != nil) ? from : "", + (from != nil) ? "\"" : "", + (to != nil) ? " to=\"" : "", + (to != nil) ? to : "", + (to != nil) ? "\"" : "", + (show != nil) ? show : "", + (stat != nil) ? stat : ""); +} + +int +versionjacc(int sock, char *from, char *to, char *id) +{ + return fprint(sock, "<iq from=\"%s\" type=\"result\" id=\"%s\" to=\"%s\">\n" + "<query xmlns=\"jabber:iq:version\">\n" + "<name>" NAME "</name>\n" + "<version>" VERSION "</version>\n" + "<os>" OS "</os>\n" + "</query>\n" + "</iq>\n", from, id, to); +} + +int +featuresjacc(int sock, char *from, char *to, char *id) +{ + return fprint(sock, "<iq from=\"%s\" type=\"result\" to=\"%s\" id=\"%s\">\n" + "<query xmlns=\"http://jabber.org/protocol/disco#info\">\n" + "</query>\n" + "</iq>\n", from, to, id); +} + +int +answersjacc(int sock, char *who, char *t, char *id, ilist *l) +{ + fprint(sock, "<iq type=\"set\" to=\"%s\" id=\"%s\">\n" + "<query xmlns=\"%s\">\n", who, id, t); + for(; l; l = l->n) + fprint(sock, "<%s>%s</%s>\n", l->name, l->val, l->name); + + return fprint(sock, "</query>\n" + "</iq>\n"); +} + +int +xmlnsjacc(int sock, char *who, char *t, char *id) +{ + return fprint(sock, "<iq type=\"get\" to=\"%s\" id=\"%s\">\n" + "<query xmlns=\"%s\"/>\n" + "</iq>\n", who, id, t); +} + +int +xmlnsnegjacc(int sock, char *who, char *t, char* id) +{ + return fprint(sock, "<iq type=\"set\" to=\"%s\" id=\"%s\">\n" + "<query xmlns=\"%s\">\n" + "<remove/>\n" + "</query>\n" + "</iq>\n", who, id, t); +} + +void +usage(void) +{ + print("usage: jacs [-dtu] [-e dest] [-s tosrv] [-r res] net!server!port\n"); + exits(0); +} + +int +main(int argc, char *argv[]) +{ + char *server, *user, *lbl, *b, *dest, *buf, *toserver; + int sock, ts, tls, debug, unreg; + UserPasswd *i; + TLSconn conn; + jabberc *me; + + tls = 0; + b = nil; + dest = nil; + unreg = 0; + debug = 0; + toserver = nil; + + ARGBEGIN { + case 't': + tls = 1; + break; + case 'r': + b = EARGF(usage()); + break; + case 'e': + dest = EARGF(usage()); + break; + case 'u': + unreg = 1; + break; + case 'd': + debug = 1; + break; + case 's': + toserver = EARGF(usage()); + break; + default: + usage(); + } ARGEND; + + if(argc < 1 || dest == nil) + usage(); + server = strdup(argv[0]); + + lbl = getwindowlbl(); + user = reallocj(nil, strlen(server) + 9, 2); + snprint(user, strlen(server) + 8, "jacs - %s", server); + setwindowlbl(user); + free(user); + + i = auth_getuserpasswd(auth_getkey, "proto=pass server=%s service=jabber", server); + if(i == nil) + sysfatal("auth_getuserpasswd: %r"); + + sock = dial(netmkaddr(server, "tcp", tls ? "5223" : "5222"), 0, 0, 0); + if(sock < 0) + sysfatal("dial: &r"); + + if(tls){ + ts = tlsClient(sock, &conn); + if(ts < 0) + sysfatal("tlsClient: %r"); + sock = ts; + + if(conn.cert != nil) + free(conn.cert); + } + + buf = strchr(server, '!'); + if(buf != nil) { + *buf++ = '\0'; + user = strchr(buf, '!'); + if(user != nil) + *user = '\0'; + user = strdup(buf); + free(server); + server = user; + } + + if(toserver == nil) + toserver = server; + + me = mkjabberc(); + me->dest = strdup(dest); + me->show = strdup("Online"); + me->stat = strdup("Online"); + me->name = strdup(i->user); + me->serv = strdup(toserver); + + if(b != nil) + me->reso = strdup(b); + else + me->reso = strdup("Plan9-Service"); + me->jid = printjid(me->name, me->serv, me->reso); + me->debug = debug; + me->unreg = unreg; + + free(buf); + + if(recvjacc(sock, me, i->passwd) < 0) + perror("recv_jacc"); + + if(lbl != nil){ + setwindowlbl(lbl); + lbl = nil; + free(lbl); + } + + freejabberc(me); + exits(0); + return 0; +} |