| 1 | /* |
|---|
| 2 | * Purple's oscar protocol plugin |
|---|
| 3 | * This file is the legal property of its developers. |
|---|
| 4 | * Please see the AUTHORS file distributed alongside this file. |
|---|
| 5 | * |
|---|
| 6 | * This library is free software; you can redistribute it and/or |
|---|
| 7 | * modify it under the terms of the GNU Lesser General Public |
|---|
| 8 | * License as published by the Free Software Foundation; either |
|---|
| 9 | * version 2 of the License, or (at your option) any later version. |
|---|
| 10 | * |
|---|
| 11 | * This library 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 GNU |
|---|
| 14 | * Lesser General Public License for more details. |
|---|
| 15 | * |
|---|
| 16 | * You should have received a copy of the GNU Lesser General Public |
|---|
| 17 | * License along with this library; if not, write to the Free Software |
|---|
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA |
|---|
| 19 | */ |
|---|
| 20 | |
|---|
| 21 | /* |
|---|
| 22 | * A little bit of this |
|---|
| 23 | * A little bit of that |
|---|
| 24 | * It started with a kiss |
|---|
| 25 | * Now we're up to bat |
|---|
| 26 | */ |
|---|
| 27 | |
|---|
| 28 | #include "oscar.h" |
|---|
| 29 | #include <ctype.h> |
|---|
| 30 | |
|---|
| 31 | #ifdef _WIN32 |
|---|
| 32 | #include "win32dep.h" |
|---|
| 33 | #endif |
|---|
| 34 | |
|---|
| 35 | /* |
|---|
| 36 | * Tokenizing functions. Used to portably replace strtok/sep. |
|---|
| 37 | * -- DMP. |
|---|
| 38 | * |
|---|
| 39 | */ |
|---|
| 40 | /* TODO: Get rid of this and use glib functions */ |
|---|
| 41 | int |
|---|
| 42 | aimutil_tokslen(char *toSearch, int theindex, char dl) |
|---|
| 43 | { |
|---|
| 44 | int curCount = 1; |
|---|
| 45 | char *next; |
|---|
| 46 | char *last; |
|---|
| 47 | int toReturn; |
|---|
| 48 | |
|---|
| 49 | last = toSearch; |
|---|
| 50 | next = strchr(toSearch, dl); |
|---|
| 51 | |
|---|
| 52 | while(curCount < theindex && next != NULL) { |
|---|
| 53 | curCount++; |
|---|
| 54 | last = next + 1; |
|---|
| 55 | next = strchr(last, dl); |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | if ((curCount < theindex) || (next == NULL)) |
|---|
| 59 | toReturn = strlen(toSearch) - (curCount - 1); |
|---|
| 60 | else |
|---|
| 61 | toReturn = next - toSearch - (curCount - 1); |
|---|
| 62 | |
|---|
| 63 | return toReturn; |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | int |
|---|
| 67 | aimutil_itemcnt(char *toSearch, char dl) |
|---|
| 68 | { |
|---|
| 69 | int curCount; |
|---|
| 70 | char *next; |
|---|
| 71 | |
|---|
| 72 | curCount = 1; |
|---|
| 73 | |
|---|
| 74 | next = strchr(toSearch, dl); |
|---|
| 75 | |
|---|
| 76 | while(next != NULL) { |
|---|
| 77 | curCount++; |
|---|
| 78 | next = strchr(next + 1, dl); |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | return curCount; |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | char * |
|---|
| 85 | aimutil_itemindex(char *toSearch, int theindex, char dl) |
|---|
| 86 | { |
|---|
| 87 | int curCount; |
|---|
| 88 | char *next; |
|---|
| 89 | char *last; |
|---|
| 90 | char *toReturn; |
|---|
| 91 | |
|---|
| 92 | curCount = 0; |
|---|
| 93 | |
|---|
| 94 | last = toSearch; |
|---|
| 95 | next = strchr(toSearch, dl); |
|---|
| 96 | |
|---|
| 97 | while (curCount < theindex && next != NULL) { |
|---|
| 98 | curCount++; |
|---|
| 99 | last = next + 1; |
|---|
| 100 | next = strchr(last, dl); |
|---|
| 101 | } |
|---|
| 102 | next = strchr(last, dl); |
|---|
| 103 | |
|---|
| 104 | if (curCount < theindex) { |
|---|
| 105 | toReturn = g_malloc(sizeof(char)); |
|---|
| 106 | *toReturn = '\0'; |
|---|
| 107 | } else { |
|---|
| 108 | if (next == NULL) { |
|---|
| 109 | toReturn = g_malloc((strlen(last) + 1) * sizeof(char)); |
|---|
| 110 | strcpy(toReturn, last); |
|---|
| 111 | } else { |
|---|
| 112 | toReturn = g_malloc((next - last + 1) * sizeof(char)); |
|---|
| 113 | memcpy(toReturn, last, (next - last)); |
|---|
| 114 | toReturn[next - last] = '\0'; |
|---|
| 115 | } |
|---|
| 116 | } |
|---|
| 117 | return toReturn; |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | /** |
|---|
| 121 | * Calculate the checksum of a given icon. |
|---|
| 122 | */ |
|---|
| 123 | guint16 |
|---|
| 124 | aimutil_iconsum(const guint8 *buf, int buflen) |
|---|
| 125 | { |
|---|
| 126 | guint32 sum; |
|---|
| 127 | int i; |
|---|
| 128 | |
|---|
| 129 | for (i=0, sum=0; i+1<buflen; i+=2) |
|---|
| 130 | sum += (buf[i+1] << 8) + buf[i]; |
|---|
| 131 | if (i < buflen) |
|---|
| 132 | sum += buf[i]; |
|---|
| 133 | sum = ((sum & 0xffff0000) >> 16) + (sum & 0x0000ffff); |
|---|
| 134 | |
|---|
| 135 | return sum; |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | /** |
|---|
| 139 | * Check if the given screen name is a valid AIM screen name. |
|---|
| 140 | * Example: BobDole |
|---|
| 141 | * Example: Henry_Ford@mac.com |
|---|
| 142 | * Example: 1KrazyKat@example.com |
|---|
| 143 | * |
|---|
| 144 | * @return TRUE if the screen name is valid, FALSE if not. |
|---|
| 145 | */ |
|---|
| 146 | static gboolean |
|---|
| 147 | aim_snvalid_aim(const char *sn) |
|---|
| 148 | { |
|---|
| 149 | int i; |
|---|
| 150 | |
|---|
| 151 | if (purple_email_is_valid(sn)) |
|---|
| 152 | return TRUE; |
|---|
| 153 | |
|---|
| 154 | /* Normal AIM screen names can't start with a number */ |
|---|
| 155 | if (isdigit(sn[0])) |
|---|
| 156 | return FALSE; |
|---|
| 157 | |
|---|
| 158 | for (i = 0; sn[i] != '\0'; i++) { |
|---|
| 159 | if (!isalnum(sn[i]) && (sn[i] != ' ')) |
|---|
| 160 | return FALSE; |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | return TRUE; |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | /** |
|---|
| 167 | * Check if the given screen name is a valid ICQ screen name. |
|---|
| 168 | * Example: 1234567 |
|---|
| 169 | * |
|---|
| 170 | * @return TRUE if the screen name is valid, FALSE if not. |
|---|
| 171 | */ |
|---|
| 172 | gboolean |
|---|
| 173 | aim_snvalid_icq(const char *sn) |
|---|
| 174 | { |
|---|
| 175 | int i; |
|---|
| 176 | |
|---|
| 177 | for (i = 0; sn[i] != '\0'; i++) { |
|---|
| 178 | if (!isdigit(sn[i])) |
|---|
| 179 | return FALSE; |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | return TRUE; |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | /** |
|---|
| 186 | * Check if the given screen name is a valid SMS screen name. |
|---|
| 187 | * Example: +19195551234 |
|---|
| 188 | * |
|---|
| 189 | * @return TRUE if the screen name is valid, FALSE if not. |
|---|
| 190 | */ |
|---|
| 191 | gboolean |
|---|
| 192 | aim_snvalid_sms(const char *sn) |
|---|
| 193 | { |
|---|
| 194 | int i; |
|---|
| 195 | |
|---|
| 196 | if (sn[0] != '+') |
|---|
| 197 | return FALSE; |
|---|
| 198 | |
|---|
| 199 | for (i = 1; sn[i] != '\0'; i++) { |
|---|
| 200 | if (!isdigit(sn[i])) |
|---|
| 201 | return FALSE; |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | return TRUE; |
|---|
| 205 | } |
|---|
| 206 | |
|---|
| 207 | /** |
|---|
| 208 | * Check if the given screen name is a valid oscar screen name. |
|---|
| 209 | * |
|---|
| 210 | * @return TRUE if the screen name is valid, FALSE if not. |
|---|
| 211 | */ |
|---|
| 212 | gboolean |
|---|
| 213 | aim_snvalid(const char *sn) |
|---|
| 214 | { |
|---|
| 215 | if ((sn == NULL) || (*sn == '\0')) |
|---|
| 216 | return FALSE; |
|---|
| 217 | |
|---|
| 218 | return aim_snvalid_icq(sn) || aim_snvalid_sms(sn) || aim_snvalid_aim(sn); |
|---|
| 219 | } |
|---|
| 220 | |
|---|
| 221 | /** |
|---|
| 222 | * This takes two screen names and compares them using the rules |
|---|
| 223 | * on screen names for AIM/AOL. Mainly, this means case and space |
|---|
| 224 | * insensitivity (all case differences and spacing differences are |
|---|
| 225 | * ignored, with the exception that screen names can not start with |
|---|
| 226 | * a space). |
|---|
| 227 | * |
|---|
| 228 | * @return 0 if equal, non-0 if different |
|---|
| 229 | */ |
|---|
| 230 | /* TODO: Do something different for email addresses. */ |
|---|
| 231 | int |
|---|
| 232 | aim_sncmp(const char *sn1, const char *sn2) |
|---|
| 233 | { |
|---|
| 234 | |
|---|
| 235 | if ((sn1 == NULL) || (sn2 == NULL)) |
|---|
| 236 | return -1; |
|---|
| 237 | |
|---|
| 238 | do { |
|---|
| 239 | while (*sn2 == ' ') |
|---|
| 240 | sn2++; |
|---|
| 241 | while (*sn1 == ' ') |
|---|
| 242 | sn1++; |
|---|
| 243 | if (toupper(*sn1) != toupper(*sn2)) |
|---|
| 244 | return 1; |
|---|
| 245 | } while ((*sn1 != '\0') && sn1++ && sn2++); |
|---|
| 246 | |
|---|
| 247 | return 0; |
|---|
| 248 | } |
|---|