Update Debian Vcs-* fields to point to git repository
[onak.git] / wordlist.c
1 /*
2  * wordlist.c - Routines for manipulating word lists
3  *
4  * Copyright 2004 Jonathan McDowell <noodles@earth.li>
5  *
6  * This program is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the Free
8  * Software Foundation; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program; if not, write to the Free Software Foundation, Inc., 51
17  * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19
20 #include <ctype.h>
21 #include <stdio.h>
22 #include <string.h>
23
24 #include "ll.h"
25 #include "decodekey.h"
26 #include "log.h"
27 #include "wordlist.h"
28
29 /**
30  *      makewordlist - Takes a string and splits it into a set of unique words.
31  *      @wordlist: The current word list.
32  *      @words: The string to split and add.
33  *
34  *      We take words and split it on non alpha numeric characters. These get
35  *      added to the word list if they're not already present. If the wordlist
36  *      is NULL then we start a new list, otherwise it's search for already
37  *      added words. Note that words is modified in the process of scanning.
38  *
39  *      Returns the new word list.
40  */
41 struct ll *makewordlist(struct ll *wordlist, char *word)
42 {
43         char *start = NULL;
44         char *end = NULL;
45
46         /*
47          * Walk through the words string, spliting on non alphanumerics and
48          * then checking if the word already exists in the list. If not then
49          * we add it.
50          */
51         end = word;
52         while (end != NULL && *end != 0) {
53                 start = end;
54                 while (*start != 0 && (ispunct(*start) || isspace (*start))) {
55                         start++;
56                 }
57                 end = start;
58                 while (*end != 0 && (!ispunct(*end) && !isspace (*end))) {
59                         *end = tolower(*end);
60                         end++;
61                 }
62                 if (end - start > 1) {
63                         if (*end != 0) {
64                                 *end = 0;
65                                 end++;
66                         }
67
68                         if (llfind(wordlist, start, 
69                                 (int (*)(const void *, const void *)) strcmp
70                                         ) == NULL) {
71                                 wordlist = lladdend(wordlist, start);
72                         }
73                 }
74         }
75         return wordlist;
76 }
77
78 /**
79  *      makewordlistfromkey - Takes a public key and splits it into a set of 
80  *                     unique words.
81  *      @wordlist: The current word list.
82  *      @key: The key to return the words from.
83  *
84  *      We take words and split it on non alpha numeric characters. These get
85  *      added to the word list if they're not already present. If the wordlist
86  *      is NULL then we start a new list, otherwise it's search for already
87  *      added words. Note that words is modified in the process of scanning.
88  *
89  *      Returns the new word list.
90  */
91 struct ll *makewordlistfromkey(struct ll *wordlist,
92                                struct openpgp_publickey *key)
93 {
94         char      **uids;
95         int         i;
96         struct ll  *words = NULL;
97         struct ll  *wl = NULL;
98
99         uids = keyuids(key, NULL);
100         for (i = 0; uids[i] != NULL; ++i) {
101                 words = makewordlist(NULL, uids[i]);
102                 for (wl = words; wl != NULL; wl = wl->next) {
103                         if (llfind(wordlist, wl->object, 
104                                 (int (*)(const void *, const void *)) strcmp
105                                                 ) == NULL) {
106                                 wordlist = lladd(wordlist, strdup(wl->object));
107                         }
108                 }
109                 free(uids[i]);
110                 uids[i] = NULL;
111         }
112         free(uids);
113
114         return wordlist;
115 }