HTMLify
ChatBot
Views: 10 | Author: abh
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "ui.h"
#include "chatbot.h"
char *mirror_chat(char *message) {
return message;
}
char *word_counter(char *message) {
char command[1024];
snprintf(command, sizeof(command), "printf %%s \"%s\" | wc", message);
FILE *fp = popen(command, "r");
if (!fp) return NULL;
size_t size = 32;
char *result = malloc(size), *command_output = malloc(size);
int words, chars;
result[0] = '\0';
fgets(command_output, size, fp);
sscanf(command_output, "%d %d %d", &words, &words, &chars);
sprintf(result, "Characters: %d, Words: %d", chars, words);
pclose(fp);
return result;
}
char *wttr_chat(char *city) {
char command[256];
snprintf(command, sizeof(command), "curl -s wttr.in/%s?format=\"%%l:+%%t,%%C\"", city);
FILE *fp = popen(command, "r");
if (!fp) return NULL;
size_t size = 32;
char *result = malloc(size);
result[0] = '\0';
size_t len = 0;
char temp[256];
while (fgets(temp, sizeof(temp), fp)) {
size_t tlen = strlen(temp);
if (len + tlen + 1 > size) {
size *= 2;
result = realloc(result, size);
}
strcpy(result + len, temp);
len += tlen;
}
pclose(fp);
return result;
}
char *define_word(char *word) {
char command[512];
snprintf(command, sizeof(command), "dict '%s' 2>/dev/null", word);
FILE *fp = popen(command, "r");
if (!fp) return NULL;
size_t size = 2048;
char *result = malloc(size);
result[0] = '\0';
char buffer[256];
size_t len = 0;
while (fgets(buffer, sizeof(buffer), fp)) {
size_t blen = strlen(buffer);
if (len + blen + 1 > size) {
size *= 2;
result = realloc(result, size);
}
strcpy(result + len, buffer);
len += blen;
}
for (int i=0; result[i]; i++)
if (result[i] == '\n')
result[i] = ' ';
pclose(fp);
if (strlen(result) == 0) {
strcpy(result, "No definition found.");
}
return result;
}
char *file_search(char *filename) {
char command[512];
snprintf(command, sizeof(command), "find . -type f -name '%s' 2>/dev/null | head -n 5", filename);
FILE *fp = popen(command, "r");
if (!fp) return NULL;
size_t size = 4096;
char *result = malloc(size);
result[0] = '\0';
char buffer[256];
size_t len = 0;
while (fgets(buffer, sizeof(buffer), fp)) {
size_t blen = strlen(buffer);
if (len + blen + 1 > size) {
size *= 2;
result = realloc(result, size);
}
strcpy(result + len, buffer);
len += blen;
}
pclose(fp);
if (strlen(result) == 0) {
strcpy(result, "File not found.");
return result;
}
for (size_t i = 0; result[i]; i++) {
if (result[i] == '\n')
result[i] = ' ';
}
return result;
}
char *fortune(char *message) {
FILE *fp = popen("fortune", "r");
if (!fp) return NULL;
size_t size = 4096;
char *result = malloc(size);
result[0] = '\0';
char buffer[256];
size_t len = 0;
while (fgets(buffer, sizeof(buffer), fp)) {
size_t blen = strlen(buffer);
if (len + blen + 1 > size) {
size *= 2;
result = realloc(result, size); // assume success
}
strcpy(result + len, buffer);
len += blen;
}
pclose(fp);
if (strlen(result) == 0) {
strcpy(result, "No fortune available.");
return result;
}
for (size_t i = 0; result[i]; i++) {
if (result[i] == '\n')
result[i] = ' ';
}
return result;
}
#define cbc 6
int main() {
ChatBot chatbots[cbc] = {
{ "MirrorBot", mirror_chat, 0, NULL},
{ "Word Counter", word_counter, 0, NULL},
{ "WTTR", wttr_chat, 0, NULL},
{ "DictBot", define_word, 0, NULL},
{ "File Finder", file_search, 0, NULL},
{ "Fortune Bot", fortune, 0, NULL},
};
ui(cbc, chatbots);;
return 0;
}