int message_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata) { xmpp_stanza_t *reply, *body, *text; char *intext, *replytext; xmpp_ctx_t *ctx = (xmpp_ctx_t*)userdata; if(!xmpp_stanza_get_child_by_name(stanza, "body")) return 1; if(!strcmp(xmpp_stanza_get_attribute(stanza, "type"), "error")) return 1; intext = xmpp_stanza_get_text(xmpp_stanza_get_child_by_name(stanza, "body")); printf("Incoming message from %s: %s\n", xmpp_stanza_get_attribute(stanza, "from"), intext); reply = xmpp_stanza_new(ctx); xmpp_stanza_set_name(reply, "message"); xmpp_stanza_set_type(reply, xmpp_stanza_get_type(stanza)?xmpp_stanza_get_type(stanza):"chat"); xmpp_stanza_set_attribute(reply, "to", xmpp_stanza_get_attribute(stanza, "from")); body = xmpp_stanza_new(ctx); xmpp_stanza_set_name(body, "body"); //printf("intext char: %c\n", *intext); /* reply section */ if(*intext == '!') { intext++; char command[32]; int idx=0; while(idx<31 && *intext != '\0' && *intext != ' ') { command[idx] = *intext; idx++; intext++; } command[idx] = '\0'; while(*intext == ' ' && *intext != '\0') intext++; char *args = intext; if(command[0]=='\0') { replytext = strdup("you're barking mad!"); } else { // good command and args // if(!strcmp("google", command)) { char *res = google(args); replytext = malloc(snprintf(NULL, 0 , "result: %s", res)+1); sprintf(replytext, "result: %s", res); free(res); } else if(!strcmp("d20", command)) { srand(time(NULL)); int rand_int = (rand() % 20) + 1; if(rand_int == 20) { replytext = malloc(snprintf(NULL, 0, "d20: %d CRITICAL!!!", rand_int)+1); sprintf(replytext, "d20: %d CRITICAL!!!", rand_int); } else { replytext = malloc(snprintf(NULL, 0, "d20: %d", rand_int)+1); sprintf(replytext, "d20: %d", rand_int); } } else { replytext = malloc(snprintf(NULL, 0 , "command: %s args: %s", command, args)+1); sprintf(replytext, "command: %s args: %s", command, args); } } } else { if (strcmp(intext, "boob")==0) { replytext = malloc(strlen("Boobies!")+1); replytext = strcpy(replytext, "Boobies!"); } else { replytext = malloc(strlen(" to you too!") + strlen(intext) + 1); strcpy(replytext, intext); strcat(replytext, " to you too!"); } } /* end reply logic */ text = xmpp_stanza_new(ctx); xmpp_stanza_set_text(text, replytext); xmpp_stanza_add_child(body, text); xmpp_stanza_add_child(reply, body); xmpp_send(conn, reply); xmpp_stanza_release(reply); free(replytext); return 1; }
static int google_hook(int argc, char *argv[]) { google(argv[1]); return 0; }