Add -1 to Debian package version
[onak.git] / wordlist.c
1 /*
2  * wordlist.c - Routines for manipulating word lists
3  *
4  * Jonathan McDowell <noodles@earth.li>
5  *
6  * Copyright 2004 Project Purple
7  */
8
9 #include <ctype.h>
10 #include <stdio.h>
11 #include <string.h>
12
13 #include "ll.h"
14 #include "decodekey.h"
15 #include "log.h"
16 #include "wordlist.h"
17
18 /**
19  *      makewordlist - Takes a string and splits it into a set of unique words.
20  *      @wordlist: The current word list.
21  *      @words: The string to split and add.
22  *
23  *      We take words and split it on non alpha numeric characters. These get
24  *      added to the word list if they're not already present. If the wordlist
25  *      is NULL then we start a new list, otherwise it's search for already
26  *      added words. Note that words is modified in the process of scanning.
27  *
28  *      Returns the new word list.
29  */
30 struct ll *makewordlist(struct ll *wordlist, char *word)
31 {
32         char *start = NULL;
33         char *end = NULL;
34
35         /*
36          * Walk through the words string, spliting on non alphanumerics and
37          * then checking if the word already exists in the list. If not then
38          * we add it.
39          */
40         end = word;
41         while (end != NULL && *end != 0) {
42                 start = end;
43                 while (*start != 0 && (ispunct(*start) || isspace (*start))) {
44                         start++;
45                 }
46                 end = start;
47                 while (*end != 0 && (!ispunct(*end) && !isspace (*end))) {
48                         *end = tolower(*end);
49                         end++;
50                 }
51                 if (end - start > 1) {
52                         if (*end != 0) {
53                                 *end = 0;
54                                 end++;
55                         }
56
57                         if (llfind(wordlist, start, 
58                                 (int (*)(const void *, const void *)) strcmp
59                                         ) == NULL) {
60                                 wordlist = lladdend(wordlist, start);
61                         }
62                 }
63         }
64         return wordlist;
65 }
66
67 /**
68  *      makewordlistfromkey - Takes a public key and splits it into a set of 
69  *                     unique words.
70  *      @wordlist: The current word list.
71  *      @key: The key to return the words from.
72  *
73  *      We take words and split it on non alpha numeric characters. These get
74  *      added to the word list if they're not already present. If the wordlist
75  *      is NULL then we start a new list, otherwise it's search for already
76  *      added words. Note that words is modified in the process of scanning.
77  *
78  *      Returns the new word list.
79  */
80 struct ll *makewordlistfromkey(struct ll *wordlist,
81                                struct openpgp_publickey *key)
82 {
83         char      **uids;
84         int         i;
85         struct ll  *words = NULL;
86         struct ll  *wl = NULL;
87
88         uids = keyuids(key, NULL);
89         for (i = 0; uids[i] != NULL; ++i) {
90                 words = makewordlist(NULL, uids[i]);
91                 for (wl = words; wl != NULL; wl = wl->next) {
92                         if (llfind(wordlist, wl->object, 
93                                 (int (*)(const void *, const void *)) strcmp
94                                                 ) == NULL) {
95                                 wordlist = lladd(wordlist, strdup(wl->object));
96                         }
97                 }
98                 free(uids[i]);
99                 uids[i] = NULL;
100         }
101         free(uids);
102
103         return wordlist;
104 }