Mercurial > pidgin
annotate src/protocols/trepia/trepia.c @ 5731:7cb2bbf03db5
[gaim-migrate @ 6155]
Part of user profiles now appears in the tooltip and under the username.
committer: Tailor Script <tailor@pidgin.im>
author | Christian Hammond <chipx86@chipx86.com> |
---|---|
date | Wed, 04 Jun 2003 04:52:45 +0000 |
parents | 99ae9bd8b5fa |
children | 37810936887e |
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 |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
257 c = text + strlen(text); |
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 if (*c == '\n') |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
260 *c = '\0'; |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
261 |
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
262 return text; |
5730 | 263 } |
264 | |
265 static GList * | |
266 trepia_away_states(GaimConnection *gc) | |
267 { | |
268 GList *m = NULL; | |
269 | |
270 return m; | |
271 } | |
272 | |
273 static GList * | |
274 trepia_actions(GaimConnection *gc) | |
275 { | |
276 return NULL; | |
277 } | |
278 | |
279 static GList * | |
280 trepia_buddy_menu(GaimConnection *gc, const char *who) | |
281 { | |
282 return NULL; | |
283 } | |
284 | |
285 static void | |
286 __free_parser_data(gpointer user_data) | |
287 { | |
288 return; | |
289 | |
290 TrepiaParserData *data = user_data; | |
291 | |
292 if (data->buffer != NULL) | |
293 g_string_free(data->buffer, TRUE); | |
294 | |
295 if (data->tag != NULL) | |
296 g_free(data->tag); | |
297 | |
298 g_free(data); | |
299 } | |
300 | |
301 static void | |
302 __start_element_handler(GMarkupParseContext *context, | |
303 const gchar *element_name, | |
304 const gchar **attribute_names, | |
305 const gchar **attribute_values, | |
306 gpointer user_data, GError **error) | |
307 { | |
308 TrepiaParserData *data = user_data; | |
309 | |
310 if (data->buffer != NULL) { | |
311 g_string_free(data->buffer, TRUE); | |
312 data->buffer = NULL; | |
313 } | |
314 | |
315 if (*data->type == 0) { | |
316 *data->type = *element_name; | |
317 } | |
318 else { | |
319 data->tag = g_strdup(element_name); | |
320 } | |
321 } | |
322 | |
323 static void | |
324 __end_element_handler(GMarkupParseContext *context, const gchar *element_name, | |
325 gpointer user_data, GError **error) | |
326 { | |
327 TrepiaParserData *data = user_data; | |
328 gchar *buffer; | |
329 | |
330 if (*element_name == *data->type) | |
331 return; | |
332 | |
333 if (data->buffer == NULL || data->tag == NULL) { | |
334 data->tag = NULL; | |
335 return; | |
336 } | |
337 | |
338 buffer = g_string_free(data->buffer, FALSE); | |
339 data->buffer = NULL; | |
340 | |
341 g_hash_table_insert(data->keys, data->tag, buffer); | |
342 | |
343 data->tag = NULL; | |
344 } | |
345 | |
346 static void | |
347 __text_handler(GMarkupParseContext *context, const gchar *text, | |
348 gsize text_len, gpointer user_data, GError **error) | |
349 { | |
350 TrepiaParserData *data = user_data; | |
351 | |
352 if (data->buffer == NULL) | |
353 data->buffer = g_string_new_len(text, text_len); | |
354 else | |
355 g_string_append_len(data->buffer, text, text_len); | |
356 } | |
357 | |
358 static GMarkupParser accounts_parser = | |
359 { | |
360 __start_element_handler, | |
361 __end_element_handler, | |
362 __text_handler, | |
363 NULL, | |
364 NULL | |
365 }; | |
366 | |
367 static int | |
368 __parse_message(const char *buf, TrepiaMessageType *type, GHashTable **info) | |
369 { | |
370 TrepiaParserData *parser_data = g_new0(TrepiaParserData, 1); | |
371 GMarkupParseContext *context; | |
372 GHashTable *keys; | |
373 | |
374 keys = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); | |
375 parser_data->keys = keys; | |
376 parser_data->type = type; | |
377 | |
378 context = g_markup_parse_context_new(&accounts_parser, 0, | |
379 parser_data, __free_parser_data); | |
380 | |
381 if (!g_markup_parse_context_parse(context, buf, strlen(buf), NULL)) { | |
382 g_markup_parse_context_free(context); | |
383 g_free(parser_data); | |
384 g_hash_table_destroy(keys); | |
385 | |
386 return 1; | |
387 } | |
388 | |
389 if (!g_markup_parse_context_end_parse(context, NULL)) { | |
390 g_markup_parse_context_free(context); | |
391 g_free(parser_data); | |
392 g_hash_table_destroy(keys); | |
393 | |
394 return 1; | |
395 } | |
396 | |
397 g_markup_parse_context_free(context); | |
398 | |
399 *info = keys; | |
400 | |
401 return 0; | |
402 } | |
403 | |
404 static gboolean | |
405 __parse_data(TrepiaSession *session, char *buf) | |
406 { | |
407 GHashTable *info; | |
408 GaimAccount *account; | |
409 TrepiaMessageType type = 0; | |
410 TrepiaProfile *profile; | |
411 int ret; | |
412 char *buffer; | |
413 struct buddy *b; | |
414 const char *id = NULL; | |
415 const char *value; | |
416 | |
417 account = gaim_connection_get_account(session->gc); | |
418 | |
419 ret = __parse_message(buf, &type, &info); | |
420 | |
421 if (ret == 1) | |
422 return TRUE; | |
423 | |
424 gaim_debug(GAIM_DEBUG_INFO, "trepia", "Successful parse.\n"); | |
425 gaim_debug(GAIM_DEBUG_INFO, "trepia", "Message type: %c\n", | |
426 type); | |
427 | |
428 if (info != NULL) { | |
429 switch (type) { | |
430 case TREPIA_USER_LIST: | |
431 gaim_debug(GAIM_DEBUG_INFO, "trepia", | |
432 "Signon complete. Showing buddy list.\n"); | |
433 gaim_connection_set_state(session->gc, GAIM_CONNECTED); | |
434 serv_finish_login(session->gc); | |
435 break; | |
436 | |
437 case TREPIA_MSG_INCOMING: /* Incoming Message */ | |
438 gaim_debug(GAIM_DEBUG_INFO, "trepia", "Receiving message\n"); | |
439 serv_got_im(session->gc, | |
440 (char *)g_hash_table_lookup(info, "a"), | |
441 (char *)g_hash_table_lookup(info, "b"), | |
442 0, time(NULL), -1); | |
443 break; | |
444 | |
445 case TREPIA_MEMBER_UPDATE: | |
446 id = g_hash_table_lookup(info, "a"); | |
447 b = gaim_find_buddy(account, id); | |
448 | |
449 if (b == NULL) { | |
450 struct group *g; | |
451 | |
452 g = gaim_find_group(_("Local Users")); | |
453 | |
454 if (g == NULL) { | |
455 g = gaim_group_new(_("Local Users")); | |
456 gaim_blist_add_group(g, NULL); | |
457 } | |
458 | |
459 b = gaim_buddy_new(account, id, NULL); | |
460 | |
461 gaim_blist_add_buddy(b, g, NULL); | |
462 } | |
463 | |
464 b->proto_data = trepia_profile_new(); | |
465 | |
466 serv_got_update(session->gc, id, 1, 0, 0, 0, 0); | |
467 | |
468 buffer = g_strdup_printf( | |
469 "<D>\n" | |
470 "<a>%s</a>\n" | |
471 "<b>1</b>\n" | |
472 "</D>", | |
473 id); | |
474 | |
475 if (trepia_write(session->fd, buffer, strlen(buffer)) < 0) { | |
476 gaim_connection_error(session->gc, _("Write error")); | |
477 g_free(buffer); | |
478 return 1; | |
479 } | |
480 | |
481 buffer = g_strdup_printf( | |
482 "<D>\n" | |
483 "<a>%s</a>\n" | |
484 "<b>2</b>\n" | |
485 "</D>", | |
486 id); | |
487 | |
488 if (trepia_write(session->fd, buffer, strlen(buffer)) < 0) { | |
489 gaim_connection_error(session->gc, _("Write error")); | |
490 g_free(buffer); | |
491 return 1; | |
492 } | |
493 | |
494 g_free(buffer); | |
495 break; | |
496 | |
497 case TREPIA_MEMBER_PROFILE: | |
498 id = g_hash_table_lookup(info, "a"); | |
499 b = gaim_find_buddy(account, id); | |
500 | |
501 if (b == NULL) | |
502 break; | |
503 | |
504 profile = b->proto_data; | |
505 | |
506 /* ID */ | |
507 trepia_profile_set_id(profile, atoi(id)); | |
508 | |
509 /* Login Time */ | |
510 if ((value = g_hash_table_lookup(info, "b")) != NULL) | |
511 trepia_profile_set_login_time(profile, atoi(value)); | |
512 | |
513 /* Age */ | |
514 if ((value = g_hash_table_lookup(info, "m")) != NULL) | |
515 trepia_profile_set_age(profile, atoi(value)); | |
516 | |
517 /* ICQ */ | |
518 if ((value = g_hash_table_lookup(info, "i")) != NULL) | |
519 trepia_profile_set_icq(profile, atoi(value)); | |
520 | |
521 /* Sex */ | |
522 if ((value = g_hash_table_lookup(info, "n")) != NULL) | |
523 trepia_profile_set_sex(profile, *value); | |
524 | |
525 /* Location */ | |
526 trepia_profile_set_location(profile, | |
527 g_hash_table_lookup(info, "p")); | |
528 | |
529 /* First Name */ | |
530 trepia_profile_set_first_name(profile, | |
531 g_hash_table_lookup(info, "g")); | |
532 | |
533 /* Last Name */ | |
534 trepia_profile_set_last_name(profile, | |
535 g_hash_table_lookup(info, "h")); | |
536 | |
537 /* Profile */ | |
538 trepia_profile_set_profile(profile, | |
539 g_hash_table_lookup(info, "o")); | |
540 | |
541 /* E-mail */ | |
542 trepia_profile_set_email(profile, | |
543 g_hash_table_lookup(info, "e")); | |
544 | |
545 /* AIM */ | |
546 trepia_profile_set_aim(profile, | |
547 g_hash_table_lookup(info, "j")); | |
548 | |
549 /* MSN */ | |
550 trepia_profile_set_msn(profile, | |
551 g_hash_table_lookup(info, "k")); | |
552 | |
553 /* Yahoo */ | |
554 trepia_profile_set_yahoo(profile, | |
555 g_hash_table_lookup(info, "l")); | |
556 | |
557 /* Homepage */ | |
558 trepia_profile_set_homepage(profile, | |
559 g_hash_table_lookup(info, "f")); | |
560 | |
561 /* Country */ | |
562 trepia_profile_set_country(profile, | |
563 g_hash_table_lookup(info, "r")); | |
564 | |
565 /* State */ | |
566 trepia_profile_set_state(profile, | |
567 g_hash_table_lookup(info, "s")); | |
568 | |
569 /* City */ | |
570 trepia_profile_set_city(profile, | |
571 g_hash_table_lookup(info, "t")); | |
572 | |
573 /* Languages */ | |
574 trepia_profile_set_languages(profile, | |
575 g_hash_table_lookup(info, "u")); | |
576 | |
577 /* School */ | |
578 trepia_profile_set_school(profile, | |
579 g_hash_table_lookup(info, "v")); | |
580 | |
581 /* Company */ | |
582 trepia_profile_set_company(profile, | |
583 g_hash_table_lookup(info, "w")); | |
584 | |
585 /* Login Name */ | |
586 if ((value = g_hash_table_lookup(info, "d")) != NULL) { | |
587 serv_got_alias(session->gc, id, value); | |
588 trepia_profile_set_location(profile, value); | |
589 } | |
590 | |
591 /* Buddy Icon */ | |
592 if ((value = g_hash_table_lookup(info, "q")) != NULL) { | |
593 char *icon; | |
594 int icon_len; | |
595 | |
596 frombase64(value, &icon, &icon_len); | |
597 | |
598 set_icon_data(session->gc, id, icon, icon_len); | |
599 | |
600 g_free(icon); | |
601 } | |
602 break; | |
603 | |
604 case TREPIA_MEMBER_OFFLINE: | |
605 id = g_hash_table_lookup(info, "a"); | |
606 | |
607 b = gaim_find_buddy(account, id); | |
608 | |
609 if (b != NULL) | |
610 serv_got_update(session->gc, id, 0, 0, 0, 0, 0); | |
611 | |
612 gaim_blist_remove_buddy(b); | |
613 | |
614 break; | |
615 | |
616 default: | |
617 break; | |
618 } | |
619 | |
620 g_hash_table_destroy(info); | |
621 } | |
622 else { | |
623 gaim_debug(GAIM_DEBUG_WARNING, "trepia", | |
624 "Unknown data received. Possibly an image?\n"); | |
625 } | |
626 | |
627 return TRUE; | |
628 } | |
629 | |
630 static void | |
631 __data_cb(gpointer data, gint source, GaimInputCondition cond) | |
632 { | |
633 TrepiaSession *session = data; | |
634 int i = 0; | |
635 char buf[1025]; | |
636 gboolean cont = TRUE; | |
637 | |
638 i = read(session->fd, buf, 1024); | |
639 | |
640 if (i <= 0) { | |
641 gaim_connection_error(session->gc, _("Read error")); | |
642 return; | |
643 } | |
644 | |
645 buf[i] = '\0'; | |
646 | |
647 gaim_debug(GAIM_DEBUG_MISC, "trepia", "__data_cb\n"); | |
648 | |
649 if (session->rxqueue == NULL) | |
650 session->rxqueue = g_string_new(buf); | |
651 else | |
652 g_string_append(session->rxqueue, buf); | |
653 | |
654 while (cont) { | |
655 char end_tag[5] = "</ >"; | |
656 char *end_s; | |
657 | |
658 end_tag[2] = session->rxqueue->str[1]; | |
659 | |
660 end_s = strstr(session->rxqueue->str, end_tag); | |
661 | |
662 if (end_s != NULL) { | |
663 char *buffer; | |
664 size_t len; | |
665 int ret; | |
666 | |
667 end_s += 4; | |
668 | |
669 len = end_s - session->rxqueue->str; | |
670 buffer = g_new0(char, len + 1); | |
671 strncpy(buffer, session->rxqueue->str, len); | |
672 | |
673 g_string_erase(session->rxqueue, 0, len); | |
674 | |
675 if (*session->rxqueue->str == '\n') | |
676 g_string_erase(session->rxqueue, 0, 1); | |
677 | |
678 gaim_debug(GAIM_DEBUG_MISC, "trepia", "S: %s\n", buffer); | |
679 | |
680 ret = __parse_data(session, buffer); | |
681 | |
682 g_free(buffer); | |
683 } | |
684 else | |
685 break; | |
686 } | |
687 } | |
688 | |
689 static void | |
690 __login_cb(gpointer data, gint source, GaimInputCondition cond) | |
691 { | |
692 TrepiaSession *session = data; | |
693 GaimAccount *account; | |
694 const char *password; | |
695 char *buffer; | |
696 char *mac = "00:04:5A:50:31:DE"; | |
697 char *gateway_mac = "00:C0:F0:52:D0:A6"; | |
698 char buf[3]; | |
699 char md5_password[17]; | |
700 md5_state_t st; | |
701 md5_byte_t di[16]; | |
702 int i; | |
703 | |
704 #if 0 | |
705 mac = __get_mac_address(); | |
706 gateway_mac = mac; | |
707 #endif | |
708 | |
709 mac = g_strdup("01:02:03:04:05:06"); | |
710 | |
711 gaim_debug(GAIM_DEBUG_INFO, "trepia", "__login_cb\n"); | |
712 | |
713 if (source < 0) { | |
714 gaim_debug(GAIM_DEBUG_ERROR, "trepia", "Write error.\n"); | |
715 gaim_connection_error(session->gc, _("Write error")); | |
716 return; | |
717 } | |
718 | |
719 gaim_debug(GAIM_DEBUG_ERROR, "trepia", "Past the first stage.\n"); | |
720 | |
721 session->fd = source; | |
722 | |
723 account = gaim_connection_get_account(session->gc); | |
724 | |
725 password = gaim_account_get_password(account); | |
726 | |
727 md5_init(&st); | |
728 md5_append(&st, (const md5_byte_t *)password, strlen(password)); | |
729 md5_finish(&st, di); | |
730 | |
731 *md5_password = '\0'; | |
732 | |
733 for (i = 0; i < 16; i++) { | |
734 g_snprintf(buf, sizeof(buf), "%02x", di[i]); | |
735 strcat(md5_password, buf); | |
736 } | |
737 | |
738 buffer = g_strdup_printf( | |
739 "<C>\n" | |
740 "<a>%s</a>\n" | |
741 "<b1>%s</b1>\n" | |
742 "<c>%s</c>\n" | |
743 "<d>%s</d>\n" | |
744 "<e>2.0</e>\n" | |
745 "</C>", | |
746 mac, gateway_mac, gaim_account_get_username(account), | |
747 md5_password); | |
748 | |
749 g_free(mac); | |
750 | |
751 if (trepia_write(session->fd, buffer, strlen(buffer)) < 0) { | |
752 gaim_connection_error(session->gc, _("Write error")); | |
753 return; | |
754 } | |
755 | |
756 g_free(buffer); | |
757 | |
758 session->gc->inpa = gaim_input_add(session->fd, GAIM_INPUT_READ, | |
759 __data_cb, session); | |
760 } | |
761 | |
762 static void | |
763 trepia_login(GaimAccount *account) | |
764 { | |
765 GaimConnection *gc; | |
766 TrepiaSession *session; | |
767 const char *server; | |
768 int port; | |
769 int i; | |
770 | |
771 gaim_debug(GAIM_DEBUG_INFO, "trepia", "trepia_login\n"); | |
772 | |
773 server = gaim_account_get_string(account, "server", TREPIA_SERVER); | |
774 port = gaim_account_get_int(account, "port", TREPIA_PORT); | |
775 | |
776 gc = gaim_account_get_connection(account); | |
777 | |
778 session = g_new0(TrepiaSession, 1); | |
779 gc->proto_data = session; | |
780 session->gc = gc; | |
781 session->fd = -1; | |
782 | |
783 __clear_user_list(account); | |
784 | |
785 gaim_debug(GAIM_DEBUG_INFO, "trepia", "connecting to proxy\n"); | |
786 | |
787 i = gaim_proxy_connect(account, server, port, __login_cb, session); | |
788 | |
789 if (i != 0) | |
790 gaim_connection_error(gc, _("Unable to create socket")); | |
791 } | |
792 | |
793 static void | |
794 trepia_close(GaimConnection *gc) | |
795 { | |
796 __clear_user_list(gaim_connection_get_account(gc)); | |
797 | |
798 gc->proto_data = NULL; | |
799 } | |
800 | |
801 static int | |
802 trepia_send_im(GaimConnection *gc, const char *who, const char *message, | |
803 int len, int flags) | |
804 { | |
805 TrepiaSession *session = gc->proto_data; | |
806 char *buffer; | |
807 | |
808 buffer = g_strdup_printf( | |
809 "<F>\n" | |
810 "<a>%s</a>\n" | |
811 "<b>%s</b>\n" | |
812 "</F>", | |
813 who, message); | |
814 | |
815 if (trepia_write(session->fd, buffer, strlen(buffer)) < 0) { | |
816 gaim_connection_error(gc, _("Write error")); | |
817 g_free(buffer); | |
818 return 1; | |
819 } | |
820 | |
821 return 1; | |
822 } | |
823 | |
824 static void | |
825 trepia_add_buddy(GaimConnection *gc, const char *name) | |
826 { | |
827 } | |
828 | |
829 static void | |
830 trepia_rem_buddy(GaimConnection *gc, char *who, char *group) | |
831 { | |
832 } | |
833 | |
834 static void | |
835 trepia_buddy_free(struct buddy *b) | |
836 { | |
837 if (b->proto_data != NULL) { | |
838 trepia_profile_destroy(b->proto_data); | |
839 | |
840 b->proto_data = NULL; | |
841 } | |
842 } | |
843 | |
844 static void | |
845 trepia_register_user(GaimAccount *account) | |
846 { | |
847 #if 0 | |
848 char *buffer; | |
849 | |
850 buffer = g_strdup_printf( | |
851 "<J><a>%s</a><b1>%s</b1><c>2.0</c><d>%s</d><e>%s</e>" | |
852 "<f>%s</f><g></g><h></h><i></i><j></j><k></k><l></l>" | |
853 "<m></m></J>"); | |
854 | |
855 #endif | |
856 } | |
857 | |
858 static GaimPluginProtocolInfo prpl_info = | |
859 { | |
860 GAIM_PROTO_TREPIA, | |
861 OPT_PROTO_BUDDY_ICON, | |
862 NULL, | |
863 NULL, | |
864 trepia_list_icon, | |
865 trepia_list_emblems, | |
5731
7cb2bbf03db5
[gaim-migrate @ 6155]
Christian Hammond <chipx86@chipx86.com>
parents:
5730
diff
changeset
|
866 trepia_status_text, |
5730 | 867 trepia_tooltip_text, |
868 NULL, | |
869 NULL, | |
870 NULL, | |
871 NULL, | |
872 trepia_login, | |
873 trepia_close, | |
874 trepia_send_im, | |
875 NULL, | |
876 NULL, | |
877 NULL, | |
878 NULL, | |
879 NULL, | |
880 NULL, | |
881 NULL, | |
882 NULL, | |
883 NULL, | |
884 NULL, | |
885 trepia_add_buddy, | |
886 NULL, | |
887 trepia_rem_buddy, | |
888 NULL, | |
889 NULL, | |
890 NULL, | |
891 NULL, | |
892 NULL, | |
893 NULL, | |
894 NULL, | |
895 NULL, | |
896 NULL, | |
897 NULL, | |
898 NULL, | |
899 NULL, | |
900 NULL, | |
901 trepia_register_user, | |
902 NULL, | |
903 NULL, | |
904 NULL, | |
905 NULL, | |
906 NULL, | |
907 trepia_buddy_free, | |
908 NULL, | |
909 NULL | |
910 }; | |
911 | |
912 static GaimPluginInfo info = | |
913 { | |
914 2, /**< api_version */ | |
915 GAIM_PLUGIN_PROTOCOL, /**< type */ | |
916 NULL, /**< ui_requirement */ | |
917 0, /**< flags */ | |
918 NULL, /**< dependencies */ | |
919 GAIM_PRIORITY_DEFAULT, /**< priority */ | |
920 | |
921 "prpl-trepia", /**< id */ | |
922 "Trepia", /**< name */ | |
923 VERSION, /**< version */ | |
924 /** summary */ | |
925 N_("Trepia Protocol Plugin"), | |
926 /** description */ | |
927 N_("Trepia Protocol Plugin"), | |
928 "Christian Hammond <chipx86@gnupdate.org>", /**< author */ | |
929 WEBSITE, /**< homepage */ | |
930 | |
931 NULL, /**< load */ | |
932 NULL, /**< unload */ | |
933 NULL, /**< destroy */ | |
934 | |
935 NULL, /**< ui_info */ | |
936 &prpl_info /**< extra_info */ | |
937 }; | |
938 | |
939 static void | |
940 __init_plugin(GaimPlugin *plugin) | |
941 { | |
942 GaimAccountOption *option; | |
943 | |
944 option = gaim_account_option_string_new(_("Login server"), "server", | |
945 TREPIA_SERVER); | |
946 prpl_info.protocol_options = g_list_append(prpl_info.protocol_options, | |
947 option); | |
948 | |
949 option = gaim_account_option_int_new(_("Port"), "port", TREPIA_PORT); | |
950 prpl_info.protocol_options = g_list_append(prpl_info.protocol_options, | |
951 option); | |
952 | |
953 my_protocol = plugin; | |
954 } | |
955 | |
956 GAIM_INIT_PLUGIN(trepia, __init_plugin, info); |