Mercurial > pidgin
annotate src/protocols/trepia/trepia.c @ 5732:37810936887e
[gaim-migrate @ 6156]
Fixed truncation on the tooltip for Trepia.
committer: Tailor Script <tailor@pidgin.im>
author | Christian Hammond <chipx86@chipx86.com> |
---|---|
date | Wed, 04 Jun 2003 04:54:01 +0000 |
parents | 7cb2bbf03db5 |
children | a80fcb009d81 |
rev | line source |
---|---|
5730 | 1 /** |
2 * @file trepia.c The Trepia protocol plugin | |
3 * | |
4 * gaim | |
5 * | |
6 * Copyright (C) 2003 Christian Hammond <chipx86@gnupdate.org> | |
7 * | |
8 * This program is free software; you can redistribute it and/or modify | |
9 * it under the terms of the GNU General Public License as published by | |
10 * the Free Software Foundation; either version 2 of the License, or | |
11 * (at your option) any later version. | |
12 * | |
13 * This program is distributed in the hope that it will be useful, | |
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 * GNU General Public License for more details. | |
17 * | |
18 * You should have received a copy of the GNU General Public License | |
19 * along with this program; if not, write to the Free Software | |
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
21 */ | |
22 #include "gaim.h" | |
23 #include "account.h" | |
24 #include "accountopt.h" | |
25 #include "md5.h" | |
26 #include "profile.h" | |
27 #include <string.h> | |
28 #include <stdlib.h> | |
29 #include <unistd.h> | |
30 | |
31 #ifndef _WIN32 | |
32 # include <sys/socket.h> | |
33 # include <sys/ioctl.h> | |
34 # include <netinet/in.h> | |
35 # include <arpa/inet.h> | |
36 # include <net/if_arp.h> | |
37 #endif | |
38 | |
39 static GaimPlugin *my_protocol = NULL; | |
40 | |
41 typedef enum | |
42 { | |
43 TREPIA_LOGIN = 'C', | |
44 TREPIA_PROFILE_REQ = 'D', | |
45 TREPIA_MSG_OUTGOING = 'F', | |
46 TREPIA_REGISTER = 'J', | |
47 TREPIA_USER_LIST = 'L', | |
48 TREPIA_MEMBER_UPDATE = 'M', | |
49 TREPIA_MEMBER_OFFLINE = 'N', | |
50 TREPIA_MEMBER_PROFILE = 'O', | |
51 TREPIA_MSG_INCOMING = 'Q' | |
52 | |
53 } TrepiaMessageType; | |
54 | |
55 typedef struct | |
56 { | |
57 GaimConnection *gc; | |
58 | |
59 int inpa; | |
60 int fd; | |
61 | |
62 GString *rxqueue; | |
63 | |
64 } TrepiaSession; | |
65 | |
66 typedef struct | |
67 { | |
68 TrepiaMessageType *type; | |
69 char *tag; | |
70 | |
71 GHashTable *keys; | |
72 | |
73 GString *buffer; | |
74 | |
75 } TrepiaParserData; | |
76 | |
77 #define TREPIA_SERVER "trepia.com" | |
78 #define TREPIA_PORT 8201 | |
79 #define TREPIA_REG_PORT 8209 | |
80 | |
81 static int | |
82 trepia_write(int fd, const char *data, size_t len) | |
83 { | |
84 gaim_debug(GAIM_DEBUG_MISC, "trepia", "C: %s%c", data, | |
85 (data[strlen(data) - 1] == '\n' ? '\0' : '\n')); | |
86 | |
87 return write(fd, data, len); | |
88 } | |
89 | |
90 static void | |
91 __clear_user_list(GaimAccount *account) | |
92 { | |
93 struct gaim_buddy_list *blist; | |
94 GaimBlistNode *group, *buddy, *next_buddy; | |
95 | |
96 blist = gaim_get_blist(); | |
97 | |
98 for (group = blist->root; group != NULL; group = group->next) { | |
99 for (buddy = group->child; buddy != NULL; buddy = next_buddy) { | |
100 struct buddy *b = (struct buddy *)buddy; | |
101 | |
102 next_buddy = buddy->next; | |
103 | |
104 if (b->account == account) | |
105 gaim_blist_remove_buddy(b); | |
106 } | |
107 } | |
108 } | |
109 | |
110 static char * | |
111 __get_mac_address(const char *ip) | |
112 { | |
113 char *mac = NULL; | |
114 #ifndef _WIN32 | |
115 struct sockaddr_in sin = { 0 }; | |
116 struct arpreq myarp = { { 0 } }; | |
117 int sockfd; | |
118 unsigned char *ptr; | |
119 | |
120 sin.sin_family = AF_INET; | |
121 | |
122 if (inet_aton(ip, &sin.sin_addr) == 0) { | |
123 gaim_debug(GAIM_DEBUG_ERROR, "trepia", "Invalid IP address %s\n", ip); | |
124 return NULL; | |
125 } | |
126 | |
127 memcpy(&myarp.arp_pa, &sin, sizeof(myarp.arp_pa)); | |
128 strcpy(myarp.arp_dev, "eth0"); | |
129 | |
130 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { | |
131 gaim_debug(GAIM_DEBUG_ERROR, "trepia", | |
132 "Cannot open socket for retrieving MAC address.\n"); | |
133 return NULL; | |
134 } | |
135 | |
136 if (ioctl(sockfd, SIOCGARP, &myarp) == -1) { | |
137 gaim_debug(GAIM_DEBUG_ERROR, "trepia", | |
138 "No entry in in arp_cache for %s\n", ip); | |
139 return NULL; | |
140 } | |
141 | |
142 ptr = &myarp.arp_ha.sa_data[0]; | |
143 | |
144 mac = g_strdup_printf("%x:%x:%x:%x:%x:%x", | |
145 ptr[0], ptr[1], ptr[2], ptr[3], ptr[4], ptr[5]); | |
146 #else | |
147 #endif | |
148 | |
149 return mac; | |
150 } | |
151 | |
152 /************************************************************************** | |
153 * Protocol Plugin ops | |
154 **************************************************************************/ | |
155 | |
156 static const char * | |
157 trepia_list_icon(GaimAccount *a, struct buddy *b) | |
158 { | |
159 return "trepia"; | |
160 } | |
161 | |
162 static void | |
163 trepia_list_emblems(struct buddy *b, char **se, char **sw, | |
164 char **nw, char **ne) | |
165 { | |
166 TrepiaProfile *profile = (TrepiaProfile *)b->proto_data; | |
167 | |
168 if (trepia_profile_get_sex(profile) == 'M') | |
169 *sw = "male"; | |
170 else if (trepia_profile_get_sex(profile)) | |
171 *sw = "female"; | |
172 } | |
173 | |
174 static char * | |
175 trepia_status_text(struct buddy *b) | |
176 { | |
5731
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
177 TrepiaProfile *profile = (TrepiaProfile *)b->proto_data; |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
178 |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
179 if (trepia_profile_get_profile(profile) != NULL) |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
180 return g_strdup(trepia_profile_get_profile(profile)); |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
181 |
5730 | 182 return NULL; |
183 } | |
184 | |
185 static char * | |
186 trepia_tooltip_text(struct buddy *b) | |
187 { | |
5731
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
188 TrepiaProfile *profile = (TrepiaProfile *)b->proto_data; |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
189 const char *value; |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
190 const char *first_name, *last_name; |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
191 int int_value; |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
192 char *text = NULL; |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
193 char *tmp, *tmp2; |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
194 char *c; |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
195 |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
196 text = g_strdup(""); |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
197 |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
198 first_name = trepia_profile_get_first_name(profile); |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
199 last_name = trepia_profile_get_last_name(profile); |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
200 |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
201 if (first_name != NULL || last_name != NULL) { |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
202 tmp = g_strdup_printf("<b>Name:</b> %s%s%s\n", |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
203 (first_name == NULL ? "" : first_name), |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
204 (first_name == NULL ? "" : " "), |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
205 (last_name == NULL ? "" : last_name)); |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
206 |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
207 tmp2 = g_strconcat(text, tmp, NULL); |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
208 g_free(tmp); |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
209 g_free(text); |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
210 text = tmp2; |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
211 } |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
212 |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
213 if ((int_value = trepia_profile_get_age(profile)) != 0) { |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
214 tmp = g_strdup_printf("<b>Age:</b> %d\n", int_value); |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
215 |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
216 tmp2 = g_strconcat(text, tmp, NULL); |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
217 g_free(tmp); |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
218 g_free(text); |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
219 text = tmp2; |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
220 } |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
221 |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
222 tmp = g_strdup_printf("<b>Gender:</b> %s\n", |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
223 (trepia_profile_get_sex(profile) == 'F' ? "Female" : "Male")); |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
224 |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
225 tmp2 = g_strconcat(text, tmp, NULL); |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
226 g_free(tmp); |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
227 g_free(text); |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
228 text = tmp2; |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
229 |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
230 if ((value = trepia_profile_get_city(profile)) != NULL) { |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
231 tmp = g_strdup_printf("<b>City:</b> %s\n", value); |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
232 |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
233 tmp2 = g_strconcat(text, tmp, NULL); |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
234 g_free(tmp); |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
235 g_free(text); |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
236 text = tmp2; |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
237 } |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
238 |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
239 if ((value = trepia_profile_get_state(profile)) != NULL) { |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
240 tmp = g_strdup_printf("<b>State:</b> %s\n", value); |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
241 |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
242 tmp2 = g_strconcat(text, tmp, NULL); |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
243 g_free(tmp); |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
244 g_free(text); |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
245 text = tmp2; |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
246 } |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
247 |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
248 if ((value = trepia_profile_get_country(profile)) != NULL) { |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
249 tmp = g_strdup_printf("<b>Country:</b> %s\n", value); |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
250 |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
251 tmp2 = g_strconcat(text, tmp, NULL); |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
252 g_free(tmp); |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
253 g_free(text); |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
254 text = tmp2; |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
255 } |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
256 |
5732
37810936887e
[gaim-migrate @ 6156]
Christian Hammond <chipx86@chipx86.com>
parents:
5731
diff
changeset
|
257 text[strlen(text) - 1] = '\0'; |
5731
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
258 |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
259 return text; |
5730 | 260 } |
261 | |
262 static void | |
263 __free_parser_data(gpointer user_data) | |
264 { | |
265 return; | |
266 | |
267 TrepiaParserData *data = user_data; | |
268 | |
269 if (data->buffer != NULL) | |
270 g_string_free(data->buffer, TRUE); | |
271 | |
272 if (data->tag != NULL) | |
273 g_free(data->tag); | |
274 | |
275 g_free(data); | |
276 } | |
277 | |
278 static void | |
279 __start_element_handler(GMarkupParseContext *context, | |
280 const gchar *element_name, | |
281 const gchar **attribute_names, | |
282 const gchar **attribute_values, | |
283 gpointer user_data, GError **error) | |
284 { | |
285 TrepiaParserData *data = user_data; | |
286 | |
287 if (data->buffer != NULL) { | |
288 g_string_free(data->buffer, TRUE); | |
289 data->buffer = NULL; | |
290 } | |
291 | |
292 if (*data->type == 0) { | |
293 *data->type = *element_name; | |
294 } | |
295 else { | |
296 data->tag = g_strdup(element_name); | |
297 } | |
298 } | |
299 | |
300 static void | |
301 __end_element_handler(GMarkupParseContext *context, const gchar *element_name, | |
302 gpointer user_data, GError **error) | |
303 { | |
304 TrepiaParserData *data = user_data; | |
305 gchar *buffer; | |
306 | |
307 if (*element_name == *data->type) | |
308 return; | |
309 | |
310 if (data->buffer == NULL || data->tag == NULL) { | |
311 data->tag = NULL; | |
312 return; | |
313 } | |
314 | |
315 buffer = g_string_free(data->buffer, FALSE); | |
316 data->buffer = NULL; | |
317 | |
318 g_hash_table_insert(data->keys, data->tag, buffer); | |
319 | |
320 data->tag = NULL; | |
321 } | |
322 | |
323 static void | |
324 __text_handler(GMarkupParseContext *context, const gchar *text, | |
325 gsize text_len, gpointer user_data, GError **error) | |
326 { | |
327 TrepiaParserData *data = user_data; | |
328 | |
329 if (data->buffer == NULL) | |
330 data->buffer = g_string_new_len(text, text_len); | |
331 else | |
332 g_string_append_len(data->buffer, text, text_len); | |
333 } | |
334 | |
335 static GMarkupParser accounts_parser = | |
336 { | |
337 __start_element_handler, | |
338 __end_element_handler, | |
339 __text_handler, | |
340 NULL, | |
341 NULL | |
342 }; | |
343 | |
344 static int | |
345 __parse_message(const char *buf, TrepiaMessageType *type, GHashTable **info) | |
346 { | |
347 TrepiaParserData *parser_data = g_new0(TrepiaParserData, 1); | |
348 GMarkupParseContext *context; | |
349 GHashTable *keys; | |
350 | |
351 keys = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); | |
352 parser_data->keys = keys; | |
353 parser_data->type = type; | |
354 | |
355 context = g_markup_parse_context_new(&accounts_parser, 0, | |
356 parser_data, __free_parser_data); | |
357 | |
358 if (!g_markup_parse_context_parse(context, buf, strlen(buf), NULL)) { | |
359 g_markup_parse_context_free(context); | |
360 g_free(parser_data); | |
361 g_hash_table_destroy(keys); | |
362 | |
363 return 1; | |
364 } | |
365 | |
366 if (!g_markup_parse_context_end_parse(context, NULL)) { | |
367 g_markup_parse_context_free(context); | |
368 g_free(parser_data); | |
369 g_hash_table_destroy(keys); | |
370 | |
371 return 1; | |
372 } | |
373 | |
374 g_markup_parse_context_free(context); | |
375 | |
376 *info = keys; | |
377 | |
378 return 0; | |
379 } | |
380 | |
381 static gboolean | |
382 __parse_data(TrepiaSession *session, char *buf) | |
383 { | |
384 GHashTable *info; | |
385 GaimAccount *account; | |
386 TrepiaMessageType type = 0; | |
387 TrepiaProfile *profile; | |
388 int ret; | |
389 char *buffer; | |
390 struct buddy *b; | |
391 const char *id = NULL; | |
392 const char *value; | |
393 | |
394 account = gaim_connection_get_account(session->gc); | |
395 | |
396 ret = __parse_message(buf, &type, &info); | |
397 | |
398 if (ret == 1) | |
399 return TRUE; | |
400 | |
401 gaim_debug(GAIM_DEBUG_INFO, "trepia", "Successful parse.\n"); | |
402 gaim_debug(GAIM_DEBUG_INFO, "trepia", "Message type: %c\n", | |
403 type); | |
404 | |
405 if (info != NULL) { | |
406 switch (type) { | |
407 case TREPIA_USER_LIST: | |
408 gaim_debug(GAIM_DEBUG_INFO, "trepia", | |
409 "Signon complete. Showing buddy list.\n"); | |
410 gaim_connection_set_state(session->gc, GAIM_CONNECTED); | |
411 serv_finish_login(session->gc); | |
412 break; | |
413 | |
414 case TREPIA_MSG_INCOMING: /* Incoming Message */ | |
415 gaim_debug(GAIM_DEBUG_INFO, "trepia", "Receiving message\n"); | |
416 serv_got_im(session->gc, | |
417 (char *)g_hash_table_lookup(info, "a"), | |
418 (char *)g_hash_table_lookup(info, "b"), | |
419 0, time(NULL), -1); | |
420 break; | |
421 | |
422 case TREPIA_MEMBER_UPDATE: | |
423 id = g_hash_table_lookup(info, "a"); | |
424 b = gaim_find_buddy(account, id); | |
425 | |
426 if (b == NULL) { | |
427 struct group *g; | |
428 | |
429 g = gaim_find_group(_("Local Users")); | |
430 | |
431 if (g == NULL) { | |
432 g = gaim_group_new(_("Local Users")); | |
433 gaim_blist_add_group(g, NULL); | |
434 } | |
435 | |
436 b = gaim_buddy_new(account, id, NULL); | |
437 | |
438 gaim_blist_add_buddy(b, g, NULL); | |
439 } | |
440 | |
441 b->proto_data = trepia_profile_new(); | |
442 | |
443 serv_got_update(session->gc, id, 1, 0, 0, 0, 0); | |
444 | |
445 buffer = g_strdup_printf( | |
446 "<D>\n" | |
447 "<a>%s</a>\n" | |
448 "<b>1</b>\n" | |
449 "</D>", | |
450 id); | |
451 | |
452 if (trepia_write(session->fd, buffer, strlen(buffer)) < 0) { | |
453 gaim_connection_error(session->gc, _("Write error")); | |
454 g_free(buffer); | |
455 return 1; | |
456 } | |
457 | |
458 buffer = g_strdup_printf( | |
459 "<D>\n" | |
460 "<a>%s</a>\n" | |
461 "<b>2</b>\n" | |
462 "</D>", | |
463 id); | |
464 | |
465 if (trepia_write(session->fd, buffer, strlen(buffer)) < 0) { | |
466 gaim_connection_error(session->gc, _("Write error")); | |
467 g_free(buffer); | |
468 return 1; | |
469 } | |
470 | |
471 g_free(buffer); | |
472 break; | |
473 | |
474 case TREPIA_MEMBER_PROFILE: | |
475 id = g_hash_table_lookup(info, "a"); | |
476 b = gaim_find_buddy(account, id); | |
477 | |
478 if (b == NULL) | |
479 break; | |
480 | |
481 profile = b->proto_data; | |
482 | |
483 /* ID */ | |
484 trepia_profile_set_id(profile, atoi(id)); | |
485 | |
486 /* Login Time */ | |
487 if ((value = g_hash_table_lookup(info, "b")) != NULL) | |
488 trepia_profile_set_login_time(profile, atoi(value)); | |
489 | |
490 /* Age */ | |
491 if ((value = g_hash_table_lookup(info, "m")) != NULL) | |
492 trepia_profile_set_age(profile, atoi(value)); | |
493 | |
494 /* ICQ */ | |
495 if ((value = g_hash_table_lookup(info, "i")) != NULL) | |
496 trepia_profile_set_icq(profile, atoi(value)); | |
497 | |
498 /* Sex */ | |
499 if ((value = g_hash_table_lookup(info, "n")) != NULL) | |
500 trepia_profile_set_sex(profile, *value); | |
501 | |
502 /* Location */ | |
503 trepia_profile_set_location(profile, | |
504 g_hash_table_lookup(info, "p")); | |
505 | |
506 /* First Name */ | |
507 trepia_profile_set_first_name(profile, | |
508 g_hash_table_lookup(info, "g")); | |
509 | |
510 /* Last Name */ | |
511 trepia_profile_set_last_name(profile, | |
512 g_hash_table_lookup(info, "h")); | |
513 | |
514 /* Profile */ | |
515 trepia_profile_set_profile(profile, | |
516 g_hash_table_lookup(info, "o")); | |
517 | |
518 /* E-mail */ | |
519 trepia_profile_set_email(profile, | |
520 g_hash_table_lookup(info, "e")); | |
521 | |
522 /* AIM */ | |
523 trepia_profile_set_aim(profile, | |
524 g_hash_table_lookup(info, "j")); | |
525 | |
526 /* MSN */ | |
527 trepia_profile_set_msn(profile, | |
528 g_hash_table_lookup(info, "k")); | |
529 | |
530 /* Yahoo */ | |
531 trepia_profile_set_yahoo(profile, | |
532 g_hash_table_lookup(info, "l")); | |
533 | |
534 /* Homepage */ | |
535 trepia_profile_set_homepage(profile, | |
536 g_hash_table_lookup(info, "f")); | |
537 | |
538 /* Country */ | |
539 trepia_profile_set_country(profile, | |
540 g_hash_table_lookup(info, "r")); | |
541 | |
542 /* State */ | |
543 trepia_profile_set_state(profile, | |
544 g_hash_table_lookup(info, "s")); | |
545 | |
546 /* City */ | |
547 trepia_profile_set_city(profile, | |
548 g_hash_table_lookup(info, "t")); | |
549 | |
550 /* Languages */ | |
551 trepia_profile_set_languages(profile, | |
552 g_hash_table_lookup(info, "u")); | |
553 | |
554 /* School */ | |
555 trepia_profile_set_school(profile, | |
556 g_hash_table_lookup(info, "v")); | |
557 | |
558 /* Company */ | |
559 trepia_profile_set_company(profile, | |
560 g_hash_table_lookup(info, "w")); | |
561 | |
562 /* Login Name */ | |
563 if ((value = g_hash_table_lookup(info, "d")) != NULL) { | |
564 serv_got_alias(session->gc, id, value); | |
565 trepia_profile_set_location(profile, value); | |
566 } | |
567 | |
568 /* Buddy Icon */ | |
569 if ((value = g_hash_table_lookup(info, "q")) != NULL) { | |
570 char *icon; | |
571 int icon_len; | |
572 | |
573 frombase64(value, &icon, &icon_len); | |
574 | |
575 set_icon_data(session->gc, id, icon, icon_len); | |
576 | |
577 g_free(icon); | |
578 } | |
579 break; | |
580 | |
581 case TREPIA_MEMBER_OFFLINE: | |
582 id = g_hash_table_lookup(info, "a"); | |
583 | |
584 b = gaim_find_buddy(account, id); | |
585 | |
586 if (b != NULL) | |
587 serv_got_update(session->gc, id, 0, 0, 0, 0, 0); | |
588 | |
589 gaim_blist_remove_buddy(b); | |
590 | |
591 break; | |
592 | |
593 default: | |
594 break; | |
595 } | |
596 | |
597 g_hash_table_destroy(info); | |
598 } | |
599 else { | |
600 gaim_debug(GAIM_DEBUG_WARNING, "trepia", | |
601 "Unknown data received. Possibly an image?\n"); | |
602 } | |
603 | |
604 return TRUE; | |
605 } | |
606 | |
607 static void | |
608 __data_cb(gpointer data, gint source, GaimInputCondition cond) | |
609 { | |
610 TrepiaSession *session = data; | |
611 int i = 0; | |
612 char buf[1025]; | |
613 gboolean cont = TRUE; | |
614 | |
615 i = read(session->fd, buf, 1024); | |
616 | |
617 if (i <= 0) { | |
618 gaim_connection_error(session->gc, _("Read error")); | |
619 return; | |
620 } | |
621 | |
622 buf[i] = '\0'; | |
623 | |
624 gaim_debug(GAIM_DEBUG_MISC, "trepia", "__data_cb\n"); | |
625 | |
626 if (session->rxqueue == NULL) | |
627 session->rxqueue = g_string_new(buf); | |
628 else | |
629 g_string_append(session->rxqueue, buf); | |
630 | |
631 while (cont) { | |
632 char end_tag[5] = "</ >"; | |
633 char *end_s; | |
634 | |
635 end_tag[2] = session->rxqueue->str[1]; | |
636 | |
637 end_s = strstr(session->rxqueue->str, end_tag); | |
638 | |
639 if (end_s != NULL) { | |
640 char *buffer; | |
641 size_t len; | |
642 int ret; | |
643 | |
644 end_s += 4; | |
645 | |
646 len = end_s - session->rxqueue->str; | |
647 buffer = g_new0(char, len + 1); | |
648 strncpy(buffer, session->rxqueue->str, len); | |
649 | |
650 g_string_erase(session->rxqueue, 0, len); | |
651 | |
652 if (*session->rxqueue->str == '\n') | |
653 g_string_erase(session->rxqueue, 0, 1); | |
654 | |
655 gaim_debug(GAIM_DEBUG_MISC, "trepia", "S: %s\n", buffer); | |
656 | |
657 ret = __parse_data(session, buffer); | |
658 | |
659 g_free(buffer); | |
660 } | |
661 else | |
662 break; | |
663 } | |
664 } | |
665 | |
666 static void | |
667 __login_cb(gpointer data, gint source, GaimInputCondition cond) | |
668 { | |
669 TrepiaSession *session = data; | |
670 GaimAccount *account; | |
671 const char *password; | |
672 char *buffer; | |
673 char *mac = "00:04:5A:50:31:DE"; | |
674 char *gateway_mac = "00:C0:F0:52:D0:A6"; | |
675 char buf[3]; | |
676 char md5_password[17]; | |
677 md5_state_t st; | |
678 md5_byte_t di[16]; | |
679 int i; | |
680 | |
681 #if 0 | |
682 mac = __get_mac_address(); | |
683 gateway_mac = mac; | |
684 #endif | |
685 | |
686 mac = g_strdup("01:02:03:04:05:06"); | |
687 | |
688 gaim_debug(GAIM_DEBUG_INFO, "trepia", "__login_cb\n"); | |
689 | |
690 if (source < 0) { | |
691 gaim_debug(GAIM_DEBUG_ERROR, "trepia", "Write error.\n"); | |
692 gaim_connection_error(session->gc, _("Write error")); | |
693 return; | |
694 } | |
695 | |
696 gaim_debug(GAIM_DEBUG_ERROR, "trepia", "Past the first stage.\n"); | |
697 | |
698 session->fd = source; | |
699 | |
700 account = gaim_connection_get_account(session->gc); | |
701 | |
702 password = gaim_account_get_password(account); | |
703 | |
704 md5_init(&st); | |
705 md5_append(&st, (const md5_byte_t *)password, strlen(password)); | |
706 md5_finish(&st, di); | |
707 | |
708 *md5_password = '\0'; | |
709 | |
710 for (i = 0; i < 16; i++) { | |
711 g_snprintf(buf, sizeof(buf), "%02x", di[i]); | |
712 strcat(md5_password, buf); | |
713 } | |
714 | |
715 buffer = g_strdup_printf( | |
716 "<C>\n" | |
717 "<a>%s</a>\n" | |
718 "<b1>%s</b1>\n" | |
719 "<c>%s</c>\n" | |
720 "<d>%s</d>\n" | |
721 "<e>2.0</e>\n" | |
722 "</C>", | |
723 mac, gateway_mac, gaim_account_get_username(account), | |
724 md5_password); | |
725 | |
726 g_free(mac); | |
727 | |
728 if (trepia_write(session->fd, buffer, strlen(buffer)) < 0) { | |
729 gaim_connection_error(session->gc, _("Write error")); | |
730 return; | |
731 } | |
732 | |
733 g_free(buffer); | |
734 | |
735 session->gc->inpa = gaim_input_add(session->fd, GAIM_INPUT_READ, | |
736 __data_cb, session); | |
737 } | |
738 | |
739 static void | |
740 trepia_login(GaimAccount *account) | |
741 { | |
742 GaimConnection *gc; | |
743 TrepiaSession *session; | |
744 const char *server; | |
745 int port; | |
746 int i; | |
747 | |
748 gaim_debug(GAIM_DEBUG_INFO, "trepia", "trepia_login\n"); | |
749 | |
750 server = gaim_account_get_string(account, "server", TREPIA_SERVER); | |
751 port = gaim_account_get_int(account, "port", TREPIA_PORT); | |
752 | |
753 gc = gaim_account_get_connection(account); | |
754 | |
755 session = g_new0(TrepiaSession, 1); | |
756 gc->proto_data = session; | |
757 session->gc = gc; | |
758 session->fd = -1; | |
759 | |
760 __clear_user_list(account); | |
761 | |
762 gaim_debug(GAIM_DEBUG_INFO, "trepia", "connecting to proxy\n"); | |
763 | |
764 i = gaim_proxy_connect(account, server, port, __login_cb, session); | |
765 | |
766 if (i != 0) | |
767 gaim_connection_error(gc, _("Unable to create socket")); | |
768 } | |
769 | |
770 static void | |
771 trepia_close(GaimConnection *gc) | |
772 { | |
773 __clear_user_list(gaim_connection_get_account(gc)); | |
774 | |
775 gc->proto_data = NULL; | |
776 } | |
777 | |
778 static int | |
779 trepia_send_im(GaimConnection *gc, const char *who, const char *message, | |
780 int len, int flags) | |
781 { | |
782 TrepiaSession *session = gc->proto_data; | |
783 char *buffer; | |
784 | |
785 buffer = g_strdup_printf( | |
786 "<F>\n" | |
787 "<a>%s</a>\n" | |
788 "<b>%s</b>\n" | |
789 "</F>", | |
790 who, message); | |
791 | |
792 if (trepia_write(session->fd, buffer, strlen(buffer)) < 0) { | |
793 gaim_connection_error(gc, _("Write error")); | |
794 g_free(buffer); | |
795 return 1; | |
796 } | |
797 | |
798 return 1; | |
799 } | |
800 | |
801 static void | |
802 trepia_add_buddy(GaimConnection *gc, const char *name) | |
803 { | |
804 } | |
805 | |
806 static void | |
807 trepia_rem_buddy(GaimConnection *gc, char *who, char *group) | |
808 { | |
809 } | |
810 | |
811 static void | |
812 trepia_buddy_free(struct buddy *b) | |
813 { | |
814 if (b->proto_data != NULL) { | |
815 trepia_profile_destroy(b->proto_data); | |
816 | |
817 b->proto_data = NULL; | |
818 } | |
819 } | |
820 | |
821 static void | |
822 trepia_register_user(GaimAccount *account) | |
823 { | |
824 #if 0 | |
825 char *buffer; | |
826 | |
827 buffer = g_strdup_printf( | |
828 "<J><a>%s</a><b1>%s</b1><c>2.0</c><d>%s</d><e>%s</e>" | |
829 "<f>%s</f><g></g><h></h><i></i><j></j><k></k><l></l>" | |
830 "<m></m></J>"); | |
831 | |
832 #endif | |
833 } | |
834 | |
835 static GaimPluginProtocolInfo prpl_info = | |
836 { | |
837 GAIM_PROTO_TREPIA, | |
838 OPT_PROTO_BUDDY_ICON, | |
839 NULL, | |
840 NULL, | |
841 trepia_list_icon, | |
842 trepia_list_emblems, | |
5731
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
843 trepia_status_text, |
5730 | 844 trepia_tooltip_text, |
845 NULL, | |
846 NULL, | |
847 NULL, | |
848 NULL, | |
849 trepia_login, | |
850 trepia_close, | |
851 trepia_send_im, | |
852 NULL, | |
853 NULL, | |
854 NULL, | |
855 NULL, | |
856 NULL, | |
857 NULL, | |
858 NULL, | |
859 NULL, | |
860 NULL, | |
861 NULL, | |
862 trepia_add_buddy, | |
863 NULL, | |
864 trepia_rem_buddy, | |
865 NULL, | |
866 NULL, | |
867 NULL, | |
868 NULL, | |
869 NULL, | |
870 NULL, | |
871 NULL, | |
872 NULL, | |
873 NULL, | |
874 NULL, | |
875 NULL, | |
876 NULL, | |
877 NULL, | |
878 trepia_register_user, | |
879 NULL, | |
880 NULL, | |
881 NULL, | |
882 NULL, | |
883 NULL, | |
884 trepia_buddy_free, | |
885 NULL, | |
886 NULL | |
887 }; | |
888 | |
889 static GaimPluginInfo info = | |
890 { | |
891 2, /**< api_version */ | |
892 GAIM_PLUGIN_PROTOCOL, /**< type */ | |
893 NULL, /**< ui_requirement */ | |
894 0, /**< flags */ | |
895 NULL, /**< dependencies */ | |
896 GAIM_PRIORITY_DEFAULT, /**< priority */ | |
897 | |
898 "prpl-trepia", /**< id */ | |
899 "Trepia", /**< name */ | |
900 VERSION, /**< version */ | |
901 /** summary */ | |
902 N_("Trepia Protocol Plugin"), | |
903 /** description */ | |
904 N_("Trepia Protocol Plugin"), | |
905 "Christian Hammond <chipx86@gnupdate.org>", /**< author */ | |
906 WEBSITE, /**< homepage */ | |
907 | |
908 NULL, /**< load */ | |
909 NULL, /**< unload */ | |
910 NULL, /**< destroy */ | |
911 | |
912 NULL, /**< ui_info */ | |
913 &prpl_info /**< extra_info */ | |
914 }; | |
915 | |
916 static void | |
917 __init_plugin(GaimPlugin *plugin) | |
918 { | |
919 GaimAccountOption *option; | |
920 | |
921 option = gaim_account_option_string_new(_("Login server"), "server", | |
922 TREPIA_SERVER); | |
923 prpl_info.protocol_options = g_list_append(prpl_info.protocol_options, | |
924 option); | |
925 | |
926 option = gaim_account_option_int_new(_("Port"), "port", TREPIA_PORT); | |
927 prpl_info.protocol_options = g_list_append(prpl_info.protocol_options, | |
928 option); | |
929 | |
930 my_protocol = plugin; | |
931 } | |
932 | |
933 GAIM_INIT_PLUGIN(trepia, __init_plugin, info); |