Merge branch 'development'
[NeonServV5.git] / src / lang.c
index e50e752741e1a2a54e6812c6c96b2d09b16c2d05..7d1532bf424bd7003cca2560c4da19ef26e1f0c2 100644 (file)
@@ -1,7 +1,25 @@
+/* lang.c - NeonServ v5.6
+ * Copyright (C) 2011-2012  Philipp Kreil (pk910)
+ * 
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License 
+ * along with this program. If not, see <http://www.gnu.org/licenses/>. 
+ */
 #include "lang.h"
 #include "UserNode.h"
 #include "DBHelper.h"
 #include "mysqlConn.h"
+#include "tools.h"
+#include "log.h"
 
 #define DEFAULT_LANG_TAG "EN"
 #define DEFAULT_LANG_NAME "English"
@@ -37,7 +55,7 @@ static struct language* add_language(char *langtag, char *langname) {
     if(cindex == MAXLANGUAGES) return NULL;
     struct language *lang = malloc(sizeof(*lang));
     if (!lang) {
-        perror("malloc() failed");
+        printf_log("main", LOG_ERROR, "%s:%d malloc() failed", __FILE__, __LINE__);
         return NULL;
     }
     lang->langtag = strdup(langtag);
@@ -119,13 +137,21 @@ void register_default_language_table(const struct default_language_entry *msgtab
 
 void register_language_string(struct language *lang, char *ident, char *text) {
     int cindex = get_entry_index(ident);
-    struct language_table *lang_entry = malloc(sizeof(*lang_entry));
-    if (!lang_entry) {
-        perror("malloc() failed");
-        return;
+    struct language_table *lang_entry;
+    for(lang_entry = lang->entrys[cindex]; lang_entry; lang_entry = lang_entry->next) {
+        if(!strcmp(lang_entry->ident, ident)) break;
     }
-    
-    lang_entry->ident = strdup(ident);
+    if(!lang_entry) {
+        lang_entry = malloc(sizeof(*lang_entry));
+        if (!lang_entry) {
+            printf_log("main", LOG_ERROR, "%s:%d malloc() failed", __FILE__, __LINE__);
+            return;
+        }
+        lang_entry->ident = strdup(ident);
+        lang_entry->next = lang->entrys[cindex];
+        lang->entrys[cindex] = lang_entry;
+    } else
+        free(lang_entry->text); //free old text (new one will be set below)
     //replace all:
     //$b to \002
     //$k to \003
@@ -141,10 +167,13 @@ void register_language_string(struct language *lang, char *ident, char *text) {
         if(a) {
             switch(a[1]) {
                 case 'b':
-                    tmp[tmppos++] = '\002';
+                    tmp[tmppos++] = 2;
                     break;
                 case 'k':
-                    tmp[tmppos++] = '\003';
+                    tmp[tmppos++] = 3;
+                    break;
+                case 'u':
+                    tmp[tmppos++] = 31;
                     break;
                 default:
                     //unknown - just write it
@@ -154,13 +183,11 @@ void register_language_string(struct language *lang, char *ident, char *text) {
         }
     } while(a);
     lang_entry->text = strdup(tmp);
-    lang_entry->next = lang->entrys[cindex];
-    lang->entrys[cindex] = lang_entry;
 }
 
 char *get_language_string(struct UserNode *user, const char* msg_ident) {
     struct language* lang;
-    if((user->flags & USERFLAG_ISAUTHED)) {
+    if(user && (user->flags & USERFLAG_ISAUTHED)) {
         loadUserSettings(user);
         lang = user->language;
     } else
@@ -185,7 +212,7 @@ char *build_language_string(struct UserNode *user, char *buffer, const char *msg
     if(buffer == NULL) {
         buffer = (char *)malloc((MAXLEN+1) * sizeof(char));
         if (!buffer) {
-            perror("malloc() failed");
+            printf_log("main", LOG_ERROR, "%s:%d malloc() failed", __FILE__, __LINE__);
             return NULL;
         }
     }