1    | /***************************************
2    |   $Revision: 1.5 $
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,2002                         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   | extern int help_requested;
39   | extern int enforced_new;
40   | 
41   | /*
42   |    Classifies the given word. The word can be HELP, HOWTO or NEW
43   |    up_classify_word may return UP_KEYWORD_HELP (for HELP & HOWTO), 
44   |    UP_KEYWORD_NEW (for NEW) or UP_KEYWORD_UNRECOG (for all others).
45   |    Comparisons are case insensitive. 
46   | */
47   | int up_classify_word(const char * word){
48   | 
49   |   if(word == NULL){
50   |     return UP_KEYWORD_UNRECOG;
51   |   }
52   |   
53   |   if( strcasecmp(word, "HOWTO") == 0 || strcasecmp(word, "HELP") == 0){
54   | 
55   |     return UP_KEYWORD_HELP;
56   | 
57   |   }else if( strcasecmp(word, "NEW") == 0){
58   | 
59   |     return UP_KEYWORD_NEW;
60   | 
61   |   }else{
62   |  
63   |     return UP_KEYWORD_UNRECOG;
64   | 
65   |   }
66   | 
67   | }
68   | 
69   | 
70   | 
71   | 
72   | /* UP_subject_process function gets the "Subject:" line of a 
73   |    update message and processes it. It tries to see if the
74   |    words in the subject line are keywords that are recognised ones.
75   |    The recognised ones are: HELP, HOWTO and NEW. The possible 
76   |    return values are UP_SUBJ_HELP_REQ, UP_SUBJ_NEW_ENFORCED,
77   |    UP_SUBJ_UNRECOG and UP_SUBJ_ALL_UNRECOG.
78   | 
79   |      UP_SUBJ_HELP_REQ means only HELP keyword is existent in the subject line
80   |      UP_SUBJ_NEW_ENFORCED means only NEW keyword is existent in the subject line
81   |      UP_SUBJ_UNRECOG means some unrecognised words found in subj line as well as
82   |                at least one recognised one.
83   |      UP_SUBJ_ALL_UNRECOG means all words were unrecognised ones (or subject
84   |                             line was empty or non-existent)
85   |      UP_SUBJ_INVALID_COMB means an invalid combination in given in the subject line. 
86   |    */
87   | up_subject_struct UP_subject_process(const char *arg){
88   | 
89   |   up_subject_struct return_struct; 
90   |   char ** temp; 
91   |   int i;
92   |   char * list_of_nonkeywords = NULL;
93   |   char * subject_line = NULL;
94   | 
95   |   subject_line = strdup(arg);
96   | 
97   | 
98   |   /* initialize the struct */
99   |   return_struct.result = UP_SUBJ_INIT;
100  | 
101  |   list_of_nonkeywords = strdup("");
102  | 
103  |   /* convert \t, \r, \n chars to white spaces */
104  |   for(i=0; i < (strlen(subject_line)) ; i++){
105  | 
106  |     if(   subject_line[i] == '\t' || subject_line[i] == '\n'
107  |        || subject_line[i] == '\r' ){
108  |       subject_line[i] = ' ';
109  |     }
110  |   }
111  | 
112  |   /* split the string into lines */
113  |   temp = g_strsplit (subject_line, " ", 0);
114  | 
115  |   for(i=0; temp[i] != NULL; i++){
116  |     if(strlen(temp[i]) > 0){
117  | 
118  |       switch(up_classify_word(temp[i])){
119  |         case UP_KEYWORD_HELP:
120  |           switch(return_struct.result){
121  | 
122  |             case UP_SUBJ_INIT: 
123  |               return_struct.result = UP_SUBJ_HELP_REQ; break;
124  |             case UP_SUBJ_HELP_REQ: break;
125  |             case UP_SUBJ_NEW_ENFORCED: 
126  |               return_struct.result = UP_SUBJ_INVALID_COMB; break;
127  |             case UP_SUBJ_UNRECOG: break;
128  |             case UP_SUBJ_ALL_UNRECOG:
129  |               return_struct.result = UP_SUBJ_UNRECOG;  break;
130  |             case UP_SUBJ_INVALID_COMB: break;
131  |             default: ;
132  |           }; break;
133  |               
134  | 
135  |         case UP_KEYWORD_NEW:
136  |           switch(return_struct.result){
137  | 
138  |             case UP_SUBJ_INIT: 
139  |               return_struct.result = UP_SUBJ_NEW_ENFORCED; break;
140  |             case UP_SUBJ_HELP_REQ:
141  |               return_struct.result = UP_SUBJ_INVALID_COMB; break;
142  |             case UP_SUBJ_NEW_ENFORCED: break;
143  |             case UP_SUBJ_UNRECOG: break;
144  |             case UP_SUBJ_ALL_UNRECOG:
145  |               return_struct.result = UP_SUBJ_UNRECOG;  break;
146  |             case UP_SUBJ_INVALID_COMB: break;
147  |             default: ;
148  |           }; break;
149  | 
150  |         case UP_KEYWORD_UNRECOG:
151  | 
152  |           if(strlen(list_of_nonkeywords) == 0){
153  |             list_of_nonkeywords = (char *)realloc(list_of_nonkeywords,
154  |                   strlen(temp[i]) + 1);
155  |             list_of_nonkeywords = strcat(list_of_nonkeywords, temp[i]);
156  |           }else{
157  |              list_of_nonkeywords = (char *)realloc(list_of_nonkeywords, 
158  |                   strlen(list_of_nonkeywords) + strlen(temp[i]) + 2);
159  |              list_of_nonkeywords = strcat(list_of_nonkeywords, " ");
160  |              list_of_nonkeywords = strcat(list_of_nonkeywords, temp[i]);
161  |           };
162  | 
163  |           switch(return_struct.result){
164  | 
165  |             case UP_SUBJ_INIT:
166  |               return_struct.result = UP_SUBJ_ALL_UNRECOG; break;
167  |             case UP_SUBJ_HELP_REQ:
168  |               return_struct.result = UP_SUBJ_UNRECOG; break;
169  |             case UP_SUBJ_NEW_ENFORCED:
170  |               return_struct.result = UP_SUBJ_UNRECOG; break;
171  |             case UP_SUBJ_UNRECOG: break;
172  |             case UP_SUBJ_ALL_UNRECOG: break;
173  |             case UP_SUBJ_INVALID_COMB:
174  |               return_struct.result = UP_SUBJ_UNRECOG; break;
175  |             default: ;
176  | 
177  |           }; break;
178  |         default: ;
179  |       }
180  |     }
181  |   }
182  | 
183  |   if(return_struct.result == UP_SUBJ_INIT){/* There were no words in the subject */
184  |     return_struct.result = UP_SUBJ_ALL_UNRECOG;
185  |   }
186  | 
187  |   return_struct.word_list = list_of_nonkeywords;
188  | 
189  |   free(subject_line);
190  | 
191  |   return return_struct;
192  | }
193  | 
194  | 
195  | 
196  | /* UP_subject_flags function gets the "Subject" type flags
197  |    and processes them. 
198  |    The flags are to represent: HELP and NEW. The possible 
199  |    return values are UP_SUBJ_HELP_REQ, UP_SUBJ_NEW_ENFORCED,
200  |    UP_SUBJ_ALL_UNRECOG, UP_SUBJ_INVALID_COMB..
201  | 
202  |      UP_SUBJ_HELP_REQ means only HELP flag is set.
203  |      UP_SUBJ_NEW_ENFORCED means only NEW flag is set.
204  |      UP_SUBJ_ALL_UNRECOG means no flags set.
205  |      UP_SUBJ_INVALID_COMB means an invalid combination of flags set. 
206  |    */
207  | up_subject_struct UP_subject_flags(){
208  | 
209  |   up_subject_struct return_struct; 
210  |   char * list_of_nonkeywords = NULL;
211  | 
212  |   /* initialize the struct */
213  |   return_struct.result = UP_SUBJ_INIT;
214  | 
215  |   list_of_nonkeywords = strdup("");
216  | 
217  |   if ( help_requested && enforced_new ) 
218  |     return_struct.result = UP_SUBJ_INVALID_COMB;
219  |   else if ( help_requested )
220  |     return_struct.result = UP_SUBJ_HELP_REQ;
221  |   else if ( enforced_new )
222  |     return_struct.result = UP_SUBJ_NEW_ENFORCED;
223  | 
224  |   if ( return_struct.result == UP_SUBJ_INIT ){
225  |     /* There were no flags set */
226  |     return_struct.result = UP_SUBJ_ALL_UNRECOG;
227  |   }
228  | 
229  |   return_struct.word_list = list_of_nonkeywords;
230  | 
231  |   return return_struct;
232  | }