* check for a valid foldername
[modest] / src / modest-text-utils.c
index 6b67214..aaa025d 100644 (file)
  */
 
 
  */
 
 
+
+#ifndef _GNU_SOURCE
+#define _GNU_SOURCE
+#endif /*_GNU_SOURCE*/
+#include <string.h> /* for strcasestr */
+
+
 #include <glib.h>
 #include <glib.h>
-#include <string.h>
 #include <stdlib.h>
 #include <glib/gi18n.h>
 #include <regex.h>
 #include <stdlib.h>
 #include <glib/gi18n.h>
 #include <regex.h>
@@ -79,7 +85,7 @@ struct _url_match_t {
        { "(file|rtsp|http|ftp|https)://[-A-Za-z0-9_$.+!*(),;:@%&=?/~#]+[-A-Za-z0-9_$%&=?/~#]",\
          NULL, NULL },\
        { "www\\.[-a-z0-9.]+[-a-z0-9](:[0-9]*)?(/[-A-Za-z0-9_$.+!*(),;:@%&=?/~#]*[^]}\\),?!;:\"]?)?",\
        { "(file|rtsp|http|ftp|https)://[-A-Za-z0-9_$.+!*(),;:@%&=?/~#]+[-A-Za-z0-9_$%&=?/~#]",\
          NULL, NULL },\
        { "www\\.[-a-z0-9.]+[-a-z0-9](:[0-9]*)?(/[-A-Za-z0-9_$.+!*(),;:@%&=?/~#]*[^]}\\),?!;:\"]?)?",\
-         NULL, "http://" },\
+                       NULL, "http://" },                              \
        { "ftp\\.[-a-z0-9.]+[-a-z0-9](:[0-9]*)?(/[-A-Za-z0-9_$.+!*(),;:@%&=?/~#]*[^]}\\),?!;:\"]?)?",\
          NULL, "ftp://" },\
        { "(voipto|callto|chatto|jabberto|xmpp):[-_a-z@0-9.\\+]+", \
        { "ftp\\.[-a-z0-9.]+[-a-z0-9](:[0-9]*)?(/[-A-Za-z0-9_$.+!*(),;:@%&=?/~#]*[^]}\\),?!;:\"]?)?",\
          NULL, "ftp://" },\
        { "(voipto|callto|chatto|jabberto|xmpp):[-_a-z@0-9.\\+]+", \
@@ -1069,6 +1075,79 @@ modest_text_utils_get_display_date (time_t date)
        return g_strdup(date_buf);
 }
 
        return g_strdup(date_buf);
 }
 
+
+gboolean
+modest_text_utils_validate_folder_name (const gchar *folder_name)
+{
+       /* based on http://msdn2.microsoft.com/en-us/library/aa365247.aspx,
+        * with some extras */
+       
+       guint len;
+       const gchar **cursor;
+       const gchar *forbidden_chars[] = {
+               "<", ">", ":", "\"", "/", "\\", "|", "?", "*", "^", "%", "$", NULL
+       };
+       const gchar *forbidden_names[] = { /* windows does not like these */
+               "CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6",
+               "COM7", "COM8", "COM9", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9",
+               ".", "..", NULL
+       };
+       
+       /* cannot be NULL */
+       if (!folder_name) 
+               return FALSE;
+
+       /* cannot be empty */
+       len = strlen(folder_name);
+       if (len == 0)
+               return FALSE;
+       
+       /* cannot start or end with a space */
+       if (g_ascii_isspace(folder_name[0]) || g_ascii_isspace(folder_name[len - 1]))
+               return FALSE; 
+
+       /* cannot contain a forbidden char */
+       for (cursor = forbidden_chars; cursor && *cursor; ++cursor)
+               if (strstr(folder_name, *cursor) != NULL)
+                       return FALSE;
+       
+       /* cannot contain a forbidden word */
+       if (len <= 4) {
+               for (cursor = forbidden_names; cursor && *cursor; ++cursor) {
+                       if (g_ascii_strcasecmp (folder_name, *cursor) == 0)
+                               return FALSE;
+               }
+       }
+       return TRUE; /* it's valid! */
+}
+
+
+
+gboolean
+modest_text_utils_validate_domain_name (const gchar *domain)
+{
+       gboolean valid = FALSE;
+       regex_t rx;
+       const gchar* domain_regex = "^[a-z0-9]([.]?[a-z0-9-])*[a-z0-9]$";
+
+       if (!domain)
+               return FALSE;
+       
+       /* domain name: all alphanum or '-' or '.',
+        * but beginning/ending in alphanum */  
+       if (regcomp (&rx, domain_regex, REG_ICASE|REG_EXTENDED|REG_NOSUB)) {
+               g_warning ("BUG: error in regexp");
+               return FALSE;
+       }
+       
+       valid = (regexec (&rx, domain, 1, NULL, 0) == 0);
+       regfree (&rx);
+               
+       return valid;
+}
+
+
+
 gboolean
 modest_text_utils_validate_email_address (const gchar *email_address, const gchar **invalid_char_position)
 {
 gboolean
 modest_text_utils_validate_email_address (const gchar *email_address, const gchar **invalid_char_position)
 {
@@ -1119,7 +1198,7 @@ modest_text_utils_validate_email_address (const gchar *email_address, const gcha
                return FALSE;
        do {
                if (*c == '.') {
                return FALSE;
        do {
                if (*c == '.') {
-                       if (c == domain || *(c - 1) == '.') 
+                       if (c == domain || *(c - 1) == '.' || *(c + 1) == '\0') 
                                return FALSE;
                        count++;
                }
                                return FALSE;
                        count++;
                }