What's xmpp??
XMPP originally named Jabber is an open communication protocol designed for instant messaging.
Based on XML(Extensible Markup Language), It enables the near real time exchange of structured data between two or more network entities alice and bob. It was designed to be extensible, the protocol offers a many applications beyond traditional IM in the broader realm of message oriented middleware including signaling for VoIP,video,file transfer,gaming and other use cases.
I assume you have knowledge of fundamentals of C.
Remember C is the language of cave men now let's proceed onto the next stage.
First grab libxml2 for parsing xml because xmpp loves wasting cycles.
sudo apt-get install gcc
libxml2 -dev
We start by importing various libraries
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <sys/types.h>
#include <conio.h>
#include <winsock.h>
#include <poll.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.>
// Just use libstrophe. Seriously.
#include <strophe.h>
// (But you won’t, because you hate yourself.)
#define Unix 0 // set to 0 for Unix, 1 for Windows
#define CLIENT_NAME "Cvck"
#define CLIENT_VERSION "not used at this time"
#define REMOTEPORT 5222
#define PROXY "proxy.eu.jabber.org"
#define STDIN 0 // file descriptor for standard input
#define ENCODING "UTF-8"
#define START_DEBUGMODE 0
/// Connect to the xmpp server (pray theres no TLS bug)//
/* Socket Variables */
int sock;
struct sockaddr_in client_addr;
struct sockaddr_in server_addr;
struct hostent *hostname;
int sin_size;
int si_sock;
struct sockaddr_in si_client_addr;
struct sockaddr_in si_server_addr;
struct hostent *si_hostname;
/* send/recv prototypes */
char proxy[128];
char *host,int size);
void connectsock(void);
void getin(void);
void fd_getin(void);
/* send/recv variables */
int buflen;
fd_set onrecv_set;
struct timeval tv;
unsigned short sequence_number_client;
char message_out[3000];
/* Here is how to send login stanza*/
void send_xmpp_auth(int sockfd, const char *user, const char *pass) {
char buffer[1024];
snprintf(buffer, sizeof(buffer),
"<stream:stream to='%s' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams' version='1.0'>",
"example.com");
send(sockfd, buffer, strlen(buffer), 0);
// Wait for server response (you’ll need to parse it)//
// Pro tip: Use libxml2 or suffer regex nightmares.
}
/* vars */
char client[32];
char user[127];
char userjid[384];
void userlogin(void);
int main(int argc, char *argv[])
{
printf("\n" " %s, a Xmpp cli \n"" version: %s\n" " xmppid: terzibajian@draugr.de , website: http://x64x2.neocities.com\n" "\n",CLIENT_NAME,__FILE__);
if (argc > 0 && ((argv[1]== NULL) || (argv[2]== NULL)))
{
printf(" Usage: %s <xmppid>[/ressource] <pass> [server] [port]\n\n", argv[0]);
exit(0);
}
strcpy(client,argv[0]);
strcpy(user,argv[1]);
if (strstr(user,"@") != NULL){
strcpy(nick,user);
if (strstr(user,"/") != NULL)
sscanf(nick,"%[^@]@%[^/]/%s",user,domain,ressource);
else
{sscanf(nick,"%[^@]@%s",user,domain);
strcpy(ressource,CLIENT_NAME);}
}
strcpy(pass,argv[2]);
if (argv[3] != NULL)
strcpy(remotehost,argv[3]);
else
strcpy(remotehost,domain);
if (domain==NULL)
strcpy(domain,remotehost);
//if (argv[4] != NULL)
if ((argv[4] != NULL) && (argv[3] != NULL))
strcpy(remoteport,argv[4]);
else
strcpy(remoteport,"5222");
//printf("\n\tCONNECTED as %s@%s on %s:%s!\n\n",user,domain,remotehost,remoteport);
connectsock();
userlogin();
flapon();
printf("get stream...\n");
toc_signon();
printf("server signon...\n");
toc_init_done();
}
/* Functions*/
void commands_interpreter(void)
{
if (strstr(message_out,"quit:") != NULL)
{
strcpy(option,"");
sscanf(message_out,"quit: %[^\n]",option);
sprintf(message_out,"<presence type='unavailable'><status>%s</status></presence>",option);
sendout(message_out);
sprintf(message_out,"</stream:stream>");
printf("%s\n",message_out);
exitclient();
}
if (strcmp(message_out,"debug") == 0)
{
debugmode=debugmode^1;
if (debugmode==1)
printf("debug mode is now ON\n");
else
printf("debug mode is now OFF\n");
}
if (strcmp(message_out,"notif") == 0)
{
notifmode=notifmode^1;
if (notifmode==1)
printf("presence notification is now ON\n");
else
printf("presence notification is now OFF\n");
}
if (strcmp(message_out,"antiflood") == 0)
{
antifloodmode=antifloodmode^1;
if (antifloodmode==1)
printf("presence antiflood is now ON\n");
else
printf("presence antiflood is now OFF\n");
}
if (strcmp(message_out,"autoresult") == 0)
{
autoresult=autoresult^1;
if (autoresult==1)
printf("autoresult mode is now ON\n");
else
printf("autoresult mode is now OFF\n");
}
if (strstr(message_out,"prior:") != NULL)
{
sscanf(message_out,"prior: %s",prior);
sprintf(message_out,"<presence><priority>%s</priority></presence>",prior);
}
if (strstr(message_out,"status:") != NULL)
{
sscanf(message_out,"status: %[^\n]",option);
sprintf(message_out,
"<presence><status>%s</status><priority>%s</priority></presence>"
"<presence to='%s'><status>%s</status><priority>%s</priority></presence>"
,option,prior,roomname,option,prior);
}
}
void getin(void)
{
char sockbuf[12000];
/* memset sockbuf*/
if((buflen=recv(sock,sockbuf, sizeof(sockbuf),0)) == -1) /* Lire nouvelle co */
{
printf(" brk recv in - buflen = %i\n",buflen); /* Si erreur lecture */
perror("recv");
exit(1);}
printf("recv in - buflen = %i (%i)\n",buflen, strlen(sockbuf)); /* Result buffer */
if ((debugmode==1) || (strstr(ressource,"debug") != NULL))
/*printf("[%i bytes]\n",buflen);*\
/*printbuf(sockbuf,buflen);*\
}
void connectsock(void)
{
/*create socket*/
if ( (sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{ perror("opening steam socket in");
exit(1);
}
/*printf("create socket %i\n", sock);*/
/*name socket using wildcards*/
hostname=gethostbyname(remotehost);
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr( inet_ntoa(*((struct in_addr *)hostname->h_addr)) );
server_addr.sin_port = htons((unsigned short int) atoi(remoteport)); /* Use specified port */
memset(server_addr.sin_zero, 0, 8);
/*resolve DNS and connecting socket*/
printf("connecting to: %s port %s\n", inet_ntoa(server_addr.sin_addr),remoteport);
sin_size=sizeof(struct sockaddr_in);
if(connect(sock,(struct sockaddr*)&server_addr, sin_size) == -1) {
perror("connect");
exit(1);}
}
void userlogin(void)
{
printf("Login with %s ...\n",user);
strcpy(prior,"0");
strcpy(proxy,PROXY);
autoresult=1;
sprintf(userjid,"%s@%s/%s",user,domain,ressource);
}
void exitclient(void)
{
closesocket(sock);
shutdown(sock,SHUT_RDWR);
exit(0);
}
Top comments (0)