modules/up/UP_subject.c
/* [<][>][^][v][top][bottom][index][help] */
FUNCTIONS
This source file includes following functions.
- up_classify_word
- UP_subject_process
1 /***************************************
2 $Revision: 1.2 $
3
4 UP subject line parsing
5
6 Status: NOT REVIEWED, TESTED
7
8 Author(s): Engin Gunduz
9
10 ******************/ /******************
11 Modification History:
12 engin (19/03/2001) Created.
13 ******************/ /******************
14 Copyright (c) 2001 RIPE NCC
15
16 All Rights Reserved
17
18 Permission to use, copy, modify, and distribute this software and its
19 documentation for any purpose and without fee is hereby granted,
20 provided that the above copyright notice appear in all copies and that
21 both that copyright notice and this permission notice appear in
22 supporting documentation, and that the name of the author not be
23 used in advertising or publicity pertaining to distribution of the
24 software without specific, written prior permission.
25
26 THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
27 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS; IN NO EVENT SHALL
28 AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
29 DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
30 AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
31 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
32 ***************************************/
33
34
35 #include "UP_subject.h"
36 #include "glib.h"
37
38
39 /*
40 Classifies the given word. The word can be HELP, HOWTO or NEW
41 up_classify_word may return UP_KEYWORD_HELP (for HELP & HOWTO),
42 UP_KEYWORD_NEW (for NEW) or UP_KEYWORD_UNRECOG (for all others).
43 Comparisons are case insensitive.
44 */
45 int up_classify_word(const char * word){
/* [<][>][^][v][top][bottom][index][help] */
46
47 if(word == NULL){
48 return UP_KEYWORD_UNRECOG;
49 }
50
51 if( strcasecmp(word, "HOWTO") == 0 || strcasecmp(word, "HELP") == 0){
52
53 return UP_KEYWORD_HELP;
54
55 }else if( strcasecmp(word, "NEW") == 0){
56
57 return UP_KEYWORD_NEW;
58
59 }else{
60
61 return UP_KEYWORD_UNRECOG;
62
63 }
64
65 }
66
67
68
69
70 /* UP_subject_process function gets the "Subject:" line of a
71 update message and processes it. It tries to see if the
72 words in the subject line are keywords that are recognised ones.
73 The recognised ones are: HELP, HOWTO and NEW. The possible
74 return values are UP_SUBJ_HELP_REQ, UP_SUBJ_NEW_ENFORCED,
75 UP_SUBJ_UNRECOG and UP_SUBJ_ALL_UNRECOG.
76
77 UP_SUBJ_HELP_REQ means only HELP keyword is existent in the subject line
78 UP_SUBJ_NEW_ENFORCED means only NEW keyword is existent in the subject line
79 UP_SUBJ_UNRECOG means some unrecognised words found in subj line as well as
80 at least one recognised one.
81 UP_SUBJ_ALL_UNRECOG means all words were unrecognised ones (or subject
82 line was empty or non-existent)
83 UP_SUBJ_INVALID_COMB means an invalid combination in given in the subject line.
84 */
85 up_subject_struct UP_subject_process(const char *arg){
/* [<][>][^][v][top][bottom][index][help] */
86
87 up_subject_struct return_struct;
88 char ** temp;
89 int i;
90 char * list_of_nonkeywords = NULL;
91 char * subject_line = NULL;
92
93 subject_line = strdup(arg);
94
95
96 /* initialize the struct */
97 return_struct.result = UP_SUBJ_INIT;
98
99 list_of_nonkeywords = strdup("");
100
101 /* convert \t, \r, \n chars to white spaces */
102 for(i=0; i < (strlen(subject_line)) ; i++){
103
104 if( subject_line[i] == '\t' || subject_line[i] == '\n'
105 || subject_line[i] == '\r' ){
106 subject_line[i] = ' ';
107 }
108 }
109
110 /* split the string into lines */
111 temp = g_strsplit (subject_line, " ", 0);
112
113 for(i=0; temp[i] != NULL; i++){
114 if(strlen(temp[i]) > 0){
115
116 switch(up_classify_word(temp[i])){
117 case UP_KEYWORD_HELP:
118 switch(return_struct.result){
119
120 case UP_SUBJ_INIT:
121 return_struct.result = UP_SUBJ_HELP_REQ; break;
122 case UP_SUBJ_HELP_REQ: break;
123 case UP_SUBJ_NEW_ENFORCED:
124 return_struct.result = UP_SUBJ_UNRECOG; break;
125 case UP_SUBJ_UNRECOG: break;
126 case UP_SUBJ_ALL_UNRECOG:
127 return_struct.result = UP_SUBJ_UNRECOG; break;
128 case UP_SUBJ_INVALID_COMB: break;
129 default: ;
130 }; break;
131
132
133 case UP_KEYWORD_NEW:
134 switch(return_struct.result){
135
136 case UP_SUBJ_INIT:
137 return_struct.result = UP_SUBJ_NEW_ENFORCED; break;
138 case UP_SUBJ_HELP_REQ:
139 return_struct.result = UP_SUBJ_INVALID_COMB; break;
140 case UP_SUBJ_NEW_ENFORCED: break;
141 case UP_SUBJ_UNRECOG: break;
142 case UP_SUBJ_ALL_UNRECOG:
143 return_struct.result = UP_SUBJ_UNRECOG; break;
144 case UP_SUBJ_INVALID_COMB: break;
145 default: ;
146 }; break;
147
148 case UP_KEYWORD_UNRECOG:
149
150 if(strlen(list_of_nonkeywords) == 0){
151 list_of_nonkeywords = (char *)realloc(list_of_nonkeywords,
152 strlen(temp[i]) + 1);
153 list_of_nonkeywords = strcat(list_of_nonkeywords, temp[i]);
154 }else{
155 list_of_nonkeywords = (char *)realloc(list_of_nonkeywords,
156 strlen(list_of_nonkeywords) + strlen(temp[i]) + 2);
157 list_of_nonkeywords = strcat(list_of_nonkeywords, " ");
158 list_of_nonkeywords = strcat(list_of_nonkeywords, temp[i]);
159 };
160
161 switch(return_struct.result){
162
163 case UP_SUBJ_INIT:
164 return_struct.result = UP_SUBJ_ALL_UNRECOG; break;
165 case UP_SUBJ_HELP_REQ:
166 return_struct.result = UP_SUBJ_UNRECOG; break;
167 case UP_SUBJ_NEW_ENFORCED:
168 return_struct.result = UP_SUBJ_UNRECOG; break;
169 case UP_SUBJ_UNRECOG: break;
170 case UP_SUBJ_ALL_UNRECOG: break;
171 case UP_SUBJ_INVALID_COMB:
172 return_struct.result = UP_SUBJ_UNRECOG; break;
173 default: ;
174
175 }; break;
176 default: ;
177 }
178 }
179 }
180
181 if(return_struct.result == UP_SUBJ_INIT){/* There were no words in the subject */
182 return_struct.result = UP_SUBJ_ALL_UNRECOG;
183 }
184
185 return_struct.word_list = list_of_nonkeywords;
186
187 free(subject_line);
188
189 return return_struct;
190 }