| 1 |
/* |
| 2 |
* Copyright (C) 2007 Colin DIDIER |
| 3 |
* |
| 4 |
* This program is free software; you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU General Public License version 2 as |
| 6 |
* published by the Free Software Foundation. |
| 7 |
* |
| 8 |
* This program is distributed in the hope that it will be useful, |
| 9 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
* GNU General Public License for more details. |
| 12 |
* |
| 13 |
* You should have received a copy of the GNU General Public License along |
| 14 |
* with this program; if not, write to the Free Software Foundation, Inc., |
| 15 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 16 |
*/ |
| 17 |
|
| 18 |
#include <string.h> |
| 19 |
|
| 20 |
#include "module.h" |
| 21 |
#include "recode.h" |
| 22 |
#include "settings.h" |
| 23 |
#include "signals.h" |
| 24 |
|
| 25 |
#define XMPP_PRIORITY_MIN -128 |
| 26 |
#define XMPP_PRIORITY_MAX 127 |
| 27 |
|
| 28 |
static const char *utf8_charset = "UTF-8"; |
| 29 |
|
| 30 |
static gboolean |
| 31 |
xmpp_get_local_charset(const char **charset) |
| 32 |
{ |
| 33 |
*charset = settings_get_str("term_charset"); |
| 34 |
if (is_valid_charset(*charset)) |
| 35 |
return (g_ascii_strcasecmp(*charset, utf8_charset) == 0); |
| 36 |
return g_get_charset(charset); |
| 37 |
} |
| 38 |
|
| 39 |
char * |
| 40 |
xmpp_recode_out(const char *str) |
| 41 |
{ |
| 42 |
const char *charset; |
| 43 |
char *recoded, *stripped; |
| 44 |
|
| 45 |
if (str == NULL || *str == '\0') |
| 46 |
return NULL; |
| 47 |
recoded = stripped = NULL; |
| 48 |
signal_emit("xmpp formats strip codes", 2, str, &stripped); |
| 49 |
if (stripped != NULL) |
| 50 |
str = stripped; |
| 51 |
if (!xmpp_get_local_charset(&charset) && charset != NULL) |
| 52 |
recoded = g_convert_with_fallback(str, -1, utf8_charset, |
| 53 |
charset, NULL, NULL, NULL, NULL); |
| 54 |
recoded = recoded != NULL ? recoded : g_strdup(str); |
| 55 |
g_free(stripped); |
| 56 |
return recoded; |
| 57 |
} |
| 58 |
|
| 59 |
char * |
| 60 |
xmpp_recode_in(const char *str) |
| 61 |
{ |
| 62 |
const char *charset; |
| 63 |
char *recoded, *to = NULL; |
| 64 |
|
| 65 |
if (str == NULL || *str == '\0') |
| 66 |
return NULL; |
| 67 |
if (xmpp_get_local_charset(&charset) || charset == NULL) |
| 68 |
return g_strdup(str); |
| 69 |
if (settings_get_bool("recode_transliterate") && |
| 70 |
g_ascii_strcasecmp(charset, "//TRANSLIT") != 0) |
| 71 |
charset = to = g_strconcat(charset ,"//TRANSLIT", (void *)NULL); |
| 72 |
recoded = g_convert_with_fallback(str, -1, charset, utf8_charset, NULL, |
| 73 |
NULL, NULL, NULL); |
| 74 |
g_free(to); |
| 75 |
return (recoded != NULL) ? recoded : g_strdup(str); |
| 76 |
} |
| 77 |
|
| 78 |
char * |
| 79 |
xmpp_find_resource_sep(const char *jid) |
| 80 |
{ |
| 81 |
return jid == NULL ? NULL : g_utf8_strchr(jid, -1, '/'); |
| 82 |
} |
| 83 |
|
| 84 |
char * |
| 85 |
xmpp_extract_resource(const char *jid) |
| 86 |
{ |
| 87 |
char *pos; |
| 88 |
|
| 89 |
g_return_val_if_fail(jid != NULL, NULL); |
| 90 |
pos = xmpp_find_resource_sep(jid); |
| 91 |
return (pos != NULL) ? g_strdup(pos + 1) : NULL; |
| 92 |
} |
| 93 |
|
| 94 |
char * |
| 95 |
xmpp_strip_resource(const char *jid) |
| 96 |
{ |
| 97 |
char *pos; |
| 98 |
|
| 99 |
g_return_val_if_fail(jid != NULL, NULL); |
| 100 |
pos = xmpp_find_resource_sep(jid); |
| 101 |
return (pos != NULL) ? g_strndup(jid, pos - jid) : g_strdup(jid); |
| 102 |
} |
| 103 |
|
| 104 |
char * |
| 105 |
xmpp_extract_user(const char *jid) |
| 106 |
{ |
| 107 |
char *pos; |
| 108 |
|
| 109 |
g_return_val_if_fail(jid != NULL, NULL); |
| 110 |
pos = g_utf8_strchr(jid, -1, '@'); |
| 111 |
return (pos != NULL) ? g_strndup(jid, pos - jid) : |
| 112 |
xmpp_strip_resource(jid); |
| 113 |
} |
| 114 |
|
| 115 |
char * |
| 116 |
xmpp_extract_domain(const char *jid) |
| 117 |
{ |
| 118 |
char *pos1, *pos2; |
| 119 |
|
| 120 |
pos1 = g_utf8_strchr(jid, -1, '@'); |
| 121 |
pos2 = xmpp_find_resource_sep(jid); |
| 122 |
if (pos1 == NULL) |
| 123 |
return NULL; |
| 124 |
if (pos2 != NULL && pos2 < pos1) |
| 125 |
return g_strdup(pos1 + 1); |
| 126 |
return (pos2 != NULL) ? |
| 127 |
g_strndup(pos1 + 1, pos2 - pos1 - 1) : g_strdup(pos1 + 1); |
| 128 |
} |
| 129 |
|
| 130 |
gboolean |
| 131 |
xmpp_have_domain(const char *jid) |
| 132 |
{ |
| 133 |
char *pos; |
| 134 |
|
| 135 |
g_return_val_if_fail(jid != NULL, FALSE); |
| 136 |
pos = g_utf8_strchr(jid, -1, '@'); |
| 137 |
return (pos != NULL && *(pos+1) != '\0'); |
| 138 |
} |
| 139 |
|
| 140 |
gboolean |
| 141 |
xmpp_have_resource(const char *jid) |
| 142 |
{ |
| 143 |
char *pos; |
| 144 |
|
| 145 |
g_return_val_if_fail(jid != NULL, FALSE); |
| 146 |
pos = xmpp_find_resource_sep(jid); |
| 147 |
return (pos != NULL && *(pos+1) != '\0'); |
| 148 |
} |
| 149 |
|
| 150 |
gboolean |
| 151 |
xmpp_priority_out_of_bound(const int priority) |
| 152 |
{ |
| 153 |
return (XMPP_PRIORITY_MIN <= priority |
| 154 |
&& priority <= XMPP_PRIORITY_MAX) ? FALSE : TRUE; |
| 155 |
} |
| 156 |
|
| 157 |
gboolean |
| 158 |
xmpp_presence_changed(const int show, const int old_show, const char *status, |
| 159 |
const char *old_status, const int priority, const int old_priority) |
| 160 |
{ |
| 161 |
return (show != old_show) |
| 162 |
|| (status == NULL && old_status != NULL) |
| 163 |
|| (status != NULL && old_status == NULL) |
| 164 |
|| (status != NULL && old_status != NULL |
| 165 |
&& strcmp(status, old_status) != 0) |
| 166 |
|| (priority != old_priority); |
| 167 |
} |