Fri, 02 Jan 2004 15:34:04 +0000
[gaim-migrate @ 8651]
jabber /part support (with /part messages!)
the list of the cool jabber chat stuff I can do w/o certain extras
from people who know who they are is getting shorter and shorter
| 7014 | 1 | /* |
| 2 | * gaim - Jabber Protocol Plugin | |
| 3127 | 3 | * |
| 7014 | 4 | * Copyright (C) 2003, Nathan Walp <faceprint@faceprint.com> |
| 3127 | 5 | * |
| 7014 | 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by | |
| 8 | * the Free Software Foundation; either version 2 of the License, or | |
| 9 | * (at your option) any later version. | |
| 3127 | 10 | * |
| 7014 | 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 14 | * GNU General Public License for more details. | |
| 15 | * | |
| 16 | * You should have received a copy of the GNU General Public License | |
| 17 | * along with this program; if not, write to the Free Software | |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 3127 | 19 | * |
| 7014 | 20 | */ |
| 21 | #include "internal.h" | |
| 22 | #include "server.h" | |
| 23 | ||
| 7322 | 24 | #include "chat.h" |
| 7014 | 25 | #include "presence.h" |
| 26 | #include "jutil.h" | |
| 3127 | 27 | |
| 7014 | 28 | time_t str_to_time(const char *timestamp) |
| 29 | { | |
| 30 | struct tm t; | |
| 31 | time_t retval = 0; | |
| 32 | char buf[32]; | |
| 33 | char *c; | |
| 34 | int tzoff = 0; | |
| 2086 | 35 | |
| 7014 | 36 | time(&retval); |
| 37 | localtime_r(&retval, &t); | |
| 38 | ||
| 39 | snprintf(buf, sizeof(buf), "%s", timestamp); | |
| 40 | c = buf; | |
|
3717
2fc0789e04e8
[gaim-migrate @ 3850]
Herman Bloggs <herman@bluedigits.com>
parents:
3127
diff
changeset
|
41 | |
| 7014 | 42 | /* 4 digit year */ |
| 43 | if(!sscanf(c, "%04d", &t.tm_year)) return 0; | |
| 44 | c+=4; | |
| 45 | if(*c == '-') | |
| 46 | c++; | |
| 2086 | 47 | |
| 7014 | 48 | t.tm_year -= 1900; |
| 49 | ||
| 50 | /* 2 digit month */ | |
| 51 | if(!sscanf(c, "%02d", &t.tm_mon)) return 0; | |
| 52 | c+=2; | |
| 53 | if(*c == '-') | |
| 54 | c++; | |
| 55 | ||
| 56 | t.tm_mon -= 1; | |
| 2086 | 57 | |
| 7014 | 58 | /* 2 digit day */ |
| 59 | if(!sscanf(c, "%02d", &t.tm_mday)) return 0; | |
| 60 | c+=2; | |
| 61 | if(*c == 'T') { /* we have more than a date, keep going */ | |
| 62 | c++; /* skip the "T" */ | |
| 2086 | 63 | |
| 7014 | 64 | /* 2 digit hour */ |
| 65 | if(sscanf(c, "%02d:%02d:%02d", &t.tm_hour, &t.tm_min, &t.tm_sec)) { | |
| 66 | int tzhrs, tzmins; | |
| 67 | c+=8; | |
| 68 | if(*c == '.') /* dealing with precision we don't care about */ | |
| 69 | c += 4; | |
| 2086 | 70 | |
| 7014 | 71 | if((*c == '+' || *c == '-') && |
| 72 | sscanf(c+1, "%02d:%02d", &tzhrs, &tzmins)) { | |
| 73 | tzoff = tzhrs*60*60 + tzmins*60; | |
| 74 | if(*c == '+') | |
| 75 | tzoff *= -1; | |
| 76 | } | |
| 2086 | 77 | |
| 7014 | 78 | #ifdef HAVE_TM_GMTOFF |
| 79 | tzoff += t.tm_gmtoff; | |
| 80 | #else | |
| 81 | # ifdef HAVE_TIMEZONE | |
| 82 | tzset(); /* making sure */ | |
| 83 | tzoff -= timezone; | |
| 84 | # endif | |
| 85 | #endif | |
| 86 | } | |
| 2086 | 87 | } |
| 7014 | 88 | retval = mktime(&t); |
| 2086 | 89 | |
| 7014 | 90 | retval += tzoff; |
| 2086 | 91 | |
| 7014 | 92 | return retval; |
| 2086 | 93 | } |
| 94 | ||
| 7014 | 95 | const char *jabber_get_state_string(int s) { |
| 96 | switch(s) { | |
| 97 | case JABBER_STATE_AWAY: | |
| 98 | return _("Away"); | |
| 99 | break; | |
| 100 | case JABBER_STATE_CHAT: | |
| 101 | return _("Chatty"); | |
| 102 | break; | |
| 103 | case JABBER_STATE_XA: | |
| 104 | return _("Extended Away"); | |
| 105 | break; | |
| 106 | case JABBER_STATE_DND: | |
| 107 | return _("Do Not Disturb"); | |
| 108 | break; | |
| 109 | default: | |
| 110 | return _("Available"); | |
| 111 | break; | |
| 112 | } | |
| 2086 | 113 | } |
| 114 | ||
| 7310 | 115 | gboolean jabber_nodeprep_validate(const char *str) |
| 116 | { | |
| 117 | const char *c; | |
| 118 | ||
| 119 | if(!str) | |
| 120 | return TRUE; | |
| 121 | ||
| 122 | if(strlen(str) > 1023) | |
| 123 | return FALSE; | |
| 124 | ||
| 125 | c = str; | |
| 126 | while(c && *c) { | |
| 127 | gunichar ch = g_utf8_get_char(c); | |
| 128 | if(ch == '\"' || ch == '&' || ch == '\'' || ch == '/' || ch == ':' || | |
| 129 | ch == '<' || ch == '>' || ch == '@' || !g_unichar_isgraph(ch)) { | |
| 130 | return FALSE; | |
| 131 | } | |
| 132 | c = g_utf8_next_char(c); | |
| 133 | } | |
| 134 | ||
| 135 | return TRUE; | |
| 136 | } | |
| 137 | ||
| 138 | gboolean jabber_nameprep_validate(const char *str) | |
| 139 | { | |
| 140 | const char *c; | |
| 141 | ||
| 142 | if(!str) | |
| 143 | return TRUE; | |
| 144 | ||
| 145 | if(strlen(str) > 1023) | |
| 146 | return FALSE; | |
| 147 | ||
| 148 | c = str; | |
| 149 | while(c && *c) { | |
| 150 | gunichar ch = g_utf8_get_char(c); | |
| 151 | if(!g_unichar_isgraph(ch)) | |
| 152 | return FALSE; | |
| 153 | ||
| 154 | c = g_utf8_next_char(c); | |
| 155 | } | |
| 156 | ||
| 157 | ||
| 158 | return TRUE; | |
| 159 | } | |
| 160 | ||
| 161 | gboolean jabber_resourceprep_validate(const char *str) | |
| 162 | { | |
| 163 | const char *c; | |
| 164 | ||
| 165 | if(!str) | |
| 166 | return TRUE; | |
| 167 | ||
| 168 | if(strlen(str) > 1023) | |
| 169 | return FALSE; | |
| 170 | ||
| 171 | c = str; | |
| 172 | while(c && *c) { | |
| 173 | gunichar ch = g_utf8_get_char(c); | |
| 7419 | 174 | if(!g_unichar_isgraph(ch) && ch != ' ') |
| 7310 | 175 | return FALSE; |
| 176 | ||
| 177 | c = g_utf8_next_char(c); | |
| 178 | } | |
| 179 | ||
| 180 | return TRUE; | |
| 181 | } | |
| 182 | ||
| 183 | ||
| 7014 | 184 | JabberID* |
| 185 | jabber_id_new(const char *str) | |
| 2086 | 186 | { |
| 7014 | 187 | char *at; |
| 188 | char *slash; | |
| 189 | JabberID *jid; | |
| 2086 | 190 | |
| 7306 | 191 | if(!str || !g_utf8_validate(str, -1, NULL)) |
| 7014 | 192 | return NULL; |
| 2086 | 193 | |
| 7014 | 194 | jid = g_new0(JabberID, 1); |
| 195 | ||
| 7306 | 196 | at = g_utf8_strchr(str, -1, '@'); |
| 197 | slash = g_utf8_strchr(str, -1, '/'); | |
| 2086 | 198 | |
| 7014 | 199 | if(at) { |
| 7306 | 200 | jid->node = g_utf8_normalize(str, at-str, G_NORMALIZE_NFKC); |
| 7014 | 201 | if(slash) { |
| 7306 | 202 | jid->domain = g_utf8_normalize(at+1, slash-(at+1), G_NORMALIZE_NFKC); |
| 203 | jid->resource = g_utf8_normalize(slash+1, -1, G_NORMALIZE_NFKC); | |
| 7014 | 204 | } else { |
| 7306 | 205 | jid->domain = g_utf8_normalize(at+1, -1, G_NORMALIZE_NFKC); |
| 7014 | 206 | } |
| 207 | } else { | |
| 208 | if(slash) { | |
| 7306 | 209 | jid->domain = g_utf8_normalize(str, slash-str, G_NORMALIZE_NFKC); |
| 210 | jid->resource = g_utf8_normalize(slash+1, -1, G_NORMALIZE_NFKC); | |
| 7014 | 211 | } else { |
| 7306 | 212 | jid->domain = g_utf8_normalize(str, -1, G_NORMALIZE_NFKC); |
| 213 | } | |
| 214 | } | |
| 215 | ||
| 216 | ||
| 7310 | 217 | if(!jabber_nodeprep_validate(jid->node) || |
| 218 | !jabber_nameprep_validate(jid->domain) || | |
| 219 | !jabber_resourceprep_validate(jid->resource)) { | |
| 7306 | 220 | jabber_id_free(jid); |
| 221 | return NULL; | |
| 222 | } | |
| 223 | ||
| 7014 | 224 | return jid; |
| 2086 | 225 | } |
| 226 | ||
| 7014 | 227 | void |
| 228 | jabber_id_free(JabberID *jid) | |
| 2086 | 229 | { |
| 7014 | 230 | if(jid) { |
| 231 | if(jid->node) | |
| 232 | g_free(jid->node); | |
| 233 | if(jid->domain) | |
| 234 | g_free(jid->domain); | |
| 235 | if(jid->resource) | |
| 236 | g_free(jid->resource); | |
| 237 | g_free(jid); | |
| 238 | } | |
| 2086 | 239 | } |
| 240 | ||
| 7014 | 241 | |
| 7306 | 242 | char *jabber_get_resource(const char *in) |
| 7014 | 243 | { |
| 7306 | 244 | JabberID *jid = jabber_id_new(in); |
| 245 | char *out; | |
| 7014 | 246 | |
| 7306 | 247 | if(!jid) |
| 7014 | 248 | return NULL; |
| 7306 | 249 | |
| 250 | out = g_strdup(jid->resource); | |
| 251 | jabber_id_free(jid); | |
| 252 | ||
| 253 | return out; | |
| 7014 | 254 | } |
| 255 | ||
| 7306 | 256 | char *jabber_get_bare_jid(const char *in) |
| 7014 | 257 | { |
| 7306 | 258 | JabberID *jid = jabber_id_new(in); |
| 259 | char *out; | |
| 7014 | 260 | |
| 7306 | 261 | if(!jid) |
| 262 | return NULL; | |
| 263 | ||
| 7322 | 264 | out = g_strdup_printf("%s%s%s", jid->node ? jid->node : "", |
| 265 | jid->node ? "@" : "", jid->domain); | |
| 7306 | 266 | jabber_id_free(jid); |
| 267 | ||
| 268 | return out; | |
| 7014 | 269 | } |
| 7261 | 270 | |
| 7322 | 271 | const char *jabber_normalize(const GaimAccount *account, const char *in) |
| 7261 | 272 | { |
| 7322 | 273 | GaimConnection *gc = account ? account->gc : NULL; |
| 274 | JabberStream *js = gc ? gc->proto_data : NULL; | |
| 275 | static char buf[3072]; /* maximum legal length of a jabber jid */ | |
| 276 | JabberID *jid; | |
| 7445 | 277 | char *node, *domain; |
| 7261 | 278 | |
| 7322 | 279 | jid = jabber_id_new(in); |
| 7310 | 280 | |
| 7322 | 281 | if(!jid) |
| 7310 | 282 | return NULL; |
| 283 | ||
| 7445 | 284 | node = jid->node ? g_utf8_strdown(jid->node, -1) : NULL; |
| 285 | domain = g_utf8_strdown(jid->domain, -1); | |
| 286 | ||
| 287 | ||
| 288 | if(js && node && jid->resource && | |
| 289 | jabber_chat_find(js, node, domain)) | |
| 290 | g_snprintf(buf, sizeof(buf), "%s@%s/%s", node, domain, | |
| 7322 | 291 | jid->resource); |
| 292 | else | |
| 7445 | 293 | g_snprintf(buf, sizeof(buf), "%s%s%s", node ? node : "", |
| 294 | node ? "@" : "", domain); | |
| 7322 | 295 | |
| 7429 | 296 | jabber_id_free(jid); |
| 7445 | 297 | g_free(node); |
| 298 | g_free(domain); | |
| 7429 | 299 | |
| 7261 | 300 | return buf; |
| 301 | } | |
| 7306 | 302 |