# HG changeset patch # User Eric Warmenhoven # Date 955325541 0 # Node ID c5a1a1b05c266a3ce6a5aa1b43685f1856b600ab # Parent 8c301530b2a30aec8eb8becf3f44ad911059aa39 [gaim-migrate @ 113] Added a spell check plugin. Shows proof-of-concept, mostly. committer: Tailor Script diff -r 8c301530b2a3 -r c5a1a1b05c26 plugins/Makefile --- a/plugins/Makefile Sun Apr 09 23:06:23 2000 +0000 +++ b/plugins/Makefile Mon Apr 10 00:12:21 2000 +0000 @@ -2,7 +2,7 @@ CFLAGS = -Wall `gnome-config --cflags gtk` -I../src LDFLAGS = -ggdb `gnome-config --libs gtk` -shared -all: simple.so gaiminc.so autorecon.so +all: simple.so gaiminc.so autorecon.so spellchk.so .SUFFIXES: .c .so diff -r 8c301530b2a3 -r c5a1a1b05c26 plugins/SIGNALS --- a/plugins/SIGNALS Sun Apr 09 23:06:23 2000 +0000 +++ b/plugins/SIGNALS Mon Apr 10 00:12:21 2000 +0000 @@ -55,13 +55,13 @@ check that they are not NULL, and try not to leave them as NULL. event_im_send: - char **who, char **text + char *who, char **text 'who' is the username of the person you're sending the message to. 'text' is the actual strict text (with HTML tags and all) of the message you're sending. - Note that you can modify these values. (You are _not_ encouraged to + Note that you can modify outgoing text. (You are _not_ encouraged to do so ;-) .) event_buddy_signon: diff -r 8c301530b2a3 -r c5a1a1b05c26 plugins/spellchk.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/plugins/spellchk.c Mon Apr 10 00:12:21 2000 +0000 @@ -0,0 +1,162 @@ +#define GAIM_PLUGINS +#include "gaim.h" + +#include +#include +#include + +struct replace_words { + char *bad; + char *good; +}; + +GList *words = NULL; + +int num_words(char *); +int get_word(char *, int); +char *have_word(char *, int); +void substitute(char **, int, int, char *); + +void spell_check(char *who, char **message, void *m) { + int i, l; + int word; + GList *w; + char *tmp; + + if (message == NULL || *message == NULL) + return; + + l = num_words(*message); + for (i = 0; i < l; i++) { + word = get_word(*message, i); + w = words; + while (w) { + struct replace_words *r; + r = (struct replace_words *)(w->data); + tmp = have_word(*message, word); + if (!strcmp(have_word(*message, word), r->bad)) + substitute(message, word, strlen(r->bad), + r->good); + free(tmp); + w = w->next; + } + } +} + +void gaim_plugin_init(void *handle) { + struct replace_words *p; + + p = malloc(sizeof *p); + p->bad = "definately"; + p->good = "definitely"; + words = g_list_append(words, p); + + p = malloc(sizeof *p); + p->bad = "u"; + p->good = "you"; + words = g_list_append(words, p); + + p = malloc(sizeof *p); + p->bad = "r"; + p->good = "are"; + words = g_list_append(words, p); + + p = malloc(sizeof *p); + p->bad = "teh"; + p->good = "the"; + words = g_list_append(words, p); + + gaim_signal_connect(handle, event_im_send, spell_check, NULL); +} + +char *name() { + return "IM Spell Check"; +} + +char *description() { + return "Watches outgoing IM text and corrects common spelling errors."; +} + +int num_words(char *m) { + int count = 0; + int pos; + int state = 0; + + for (pos = 0; pos < strlen(m); pos++) { + switch (state) { + case 0: /* expecting word */ + if (isalnum(m[pos])) { + count++; + state = 1; + } else if (m[pos] == '<') + state = 2; + break; + case 1: /* inside word */ + if (isspace(m[pos])) + state = 0; + break; + case 2: /* inside HTML tag */ + if (m[pos] == '>') + state = 0; + break; + } + } + return count; +} + +int get_word(char *m, int word) { + int count = 0; + int pos = 0; + int state = 0; + + for (pos = 0; pos < strlen(m) && count <= word; pos++) { + switch (state) { + case 0: + if (isalnum(m[pos])) { + count++; + state = 1; + } else if (m[pos] == '<') + state = 2; + break; + case 1: + if (isspace(m[pos])) + state = 0; + break; + case 2: + if (m[pos] == '>') + state = 0; + break; + } + } + return pos - 1; +} + +char *have_word(char *m, int pos) { + char *tmp = strpbrk(&m[pos], "' \t\f\r\n"); + int len = (int)(tmp - &m[pos]); + + if (tmp == NULL) { + tmp = strdup(&m[pos]); + return tmp; + } + + tmp = malloc(len + 1); + tmp[0] = 0; + strncat(tmp, &m[pos], len); + + return tmp; +} + +void substitute(char **mes, int pos, int m, char *text) { + char *new = malloc(strlen(*mes) + strlen(text) + 1); + char *tmp; + new[0] = 0; + + strncat(new, *mes, pos); + strcat(new, text); + + strcat(new, &(*mes)[pos + m]); + tmp = *mes; + *mes = new; + free(tmp); +}