comparison plugins/yay/yay.c @ 1054:0b0b4cb53c17

[gaim-migrate @ 1064] yahoo prpl committer: Tailor Script <tailor@pidgin.im>
author Eric Warmenhoven <eric@warmenhoven.org>
date Fri, 03 Nov 2000 10:03:53 +0000
parents
children f0f5c10cce63
comparison
equal deleted inserted replaced
1053:864f4aae0b60 1054:0b0b4cb53c17
1 /*
2 * gaim
3 *
4 * Some code copyright (C) 1998-1999, Mark Spencer <markster@marko.net>
5 * libfaim code copyright 1998, 1999 Adam Fritzler <afritz@auk.cx>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include "../config.h"
25 #endif
26
27
28 #include <netdb.h>
29 #include <gtk/gtk.h>
30 #include <unistd.h>
31 #include <errno.h>
32 #include <netinet/in.h>
33 #include <arpa/inet.h>
34 #include <string.h>
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <time.h>
38 #include <sys/socket.h>
39 #include <sys/stat.h>
40 #include "multi.h"
41 #include "prpl.h"
42 #include "gaim.h"
43 #include "libyahoo.h"
44
45 struct yahoo_data {
46 struct yahoo_context *ctxt;
47 };
48
49 static char *yahoo_name() {
50 return "Yahoo";
51 }
52
53 char *name() {
54 return "Yahoo";
55 }
56
57 char *description() {
58 return "Allows gaim to use the Yahoo protocol";
59 }
60
61 static void process_packet_status(struct gaim_connection *gc, struct yahoo_packet *pkt) {
62 struct yahoo_data *yd = (struct yahoo_data *)gc->proto_data;
63 int i;
64
65 if (pkt->service == YAHOO_SERVICE_LOGOFF && !strcasecmp(pkt->active_id, gc->username)) {
66 signoff(gc);
67 return;
68 }
69
70 for (i = 0; i < pkt->idstatus_count; i++) {
71 struct group *g;
72 struct buddy *b;
73 struct yahoo_idstatus *rec = pkt->idstatus[i];
74
75 b = find_buddy(gc, rec->id);
76 if (!b) {
77 struct yahoo_buddy **buddy;
78 for (buddy = yd->ctxt->buddies; *buddy; buddy++) {
79 struct yahoo_buddy *bud = *buddy;
80
81 if (!strcasecmp(rec->id, bud->id))
82 b = add_buddy(gc, bud->group, bud->id, bud->id);
83 }
84 }
85 if (pkt->service == YAHOO_SERVICE_LOGOFF)
86 serv_got_update(gc, b->name, 0, 0, 0, 0, 0, 0);
87 else
88 serv_got_update(gc, b->name, 1, 0, 0, 0, rec->status, 0);
89 }
90 }
91
92 static void process_packet_message(struct gaim_connection *gc, struct yahoo_packet *pkt) {
93 struct yahoo_data *yd = (struct yahoo_data *)gc->proto_data;
94
95 if (pkt->msg) {
96 if (pkt->msgtype == YAHOO_MSGTYPE_BOUNCE)
97 do_error_dialog("Your message did not get received.", "Error");
98 else
99 serv_got_im(gc, pkt->msg_id, pkt->msg, pkt->msg_timestamp ? 1 : 0);
100 }
101 }
102
103 static void process_packet_conf_invite(struct gaim_connection *gc, struct yahoo_packet *pkt) {
104 struct yahoo_data *yd = (struct yahoo_data *)gc->proto_data;
105 }
106
107 static void process_packet_conf_add_invite(struct gaim_connection *gc, struct yahoo_packet *pkt) {
108 struct yahoo_data *yd = (struct yahoo_data *)gc->proto_data;
109 }
110
111 static void process_packet_conf_msg(struct gaim_connection *gc, struct yahoo_packet *pkt) {
112 struct yahoo_data *yd = (struct yahoo_data *)gc->proto_data;
113 }
114
115 static void process_packet_conf_logon(struct gaim_connection *gc, struct yahoo_packet *pkt) {
116 struct yahoo_data *yd = (struct yahoo_data *)gc->proto_data;
117 }
118
119 static void process_packet_conf_logoff(struct gaim_connection *gc, struct yahoo_packet *pkt) {
120 struct yahoo_data *yd = (struct yahoo_data *)gc->proto_data;
121 }
122
123 static void process_packet_newmail(struct gaim_connection *gc, struct yahoo_packet *pkt) {
124 struct yahoo_data *yd = (struct yahoo_data *)gc->proto_data;
125 char buf[2048];
126
127 if (pkt->mail_status) {
128 if (pkt->service == YAHOO_SERVICE_NEWMAIL)
129 g_snprintf(buf, sizeof buf, "%s has %d new message%s on Yahoo Mail.",
130 gc->username, pkt->mail_status,
131 pkt->mail_status == 1 ? "" : "s");
132 else
133 g_snprintf(buf, sizeof buf, "%s has %d new personal message%s on Yahoo Mail.",
134 gc->username, pkt->mail_status,
135 pkt->mail_status == 1 ? "" : "s");
136 do_error_dialog(buf, "New Mail!");
137 }
138 }
139
140 static void yahoo_callback(gpointer data, gint source, GdkInputCondition condition) {
141 struct gaim_connection *gc = (struct gaim_connection *)data;
142 struct yahoo_data *yd = (struct yahoo_data *)gc->proto_data;
143
144 struct yahoo_rawpacket *rawpkt;
145 struct yahoo_packet *pkt;
146
147 if (!yahoo_getdata(yd->ctxt)) {
148 signoff(gc);
149 }
150
151 while ((rawpkt = yahoo_getpacket(yd->ctxt)) != NULL) {
152 pkt = yahoo_parsepacket(yd->ctxt, rawpkt);
153
154 switch (pkt->service) {
155 case YAHOO_SERVICE_USERSTAT:
156 case YAHOO_SERVICE_CHATLOGON:
157 case YAHOO_SERVICE_CHATLOGOFF:
158 case YAHOO_SERVICE_LOGON:
159 case YAHOO_SERVICE_LOGOFF:
160 case YAHOO_SERVICE_ISAWAY:
161 case YAHOO_SERVICE_ISBACK:
162 process_packet_status(gc, pkt);
163 break;
164 case YAHOO_SERVICE_MESSAGE:
165 case YAHOO_SERVICE_CHATMSG:
166 case YAHOO_SERVICE_SYSMESSAGE:
167 process_packet_message(gc, pkt);
168 break;
169 case YAHOO_SERVICE_CONFINVITE:
170 process_packet_conf_invite(gc, pkt);
171 break;
172 case YAHOO_SERVICE_CONFADDINVITE:
173 process_packet_conf_add_invite(gc, pkt);
174 break;
175 case YAHOO_SERVICE_CONFMSG:
176 process_packet_conf_msg(gc, pkt);
177 break;
178 case YAHOO_SERVICE_CONFLOGON:
179 process_packet_conf_logon(gc, pkt);
180 break;
181 case YAHOO_SERVICE_CONFLOGOFF:
182 process_packet_conf_logoff(gc, pkt);
183 break;
184 case YAHOO_SERVICE_NEWMAIL:
185 case YAHOO_SERVICE_NEWPERSONALMAIL:
186 process_packet_newmail(gc, pkt);
187 break;
188 default:
189 debug_printf("Unhandled packet type %s\n",
190 yahoo_get_service_string(pkt->service));
191 break;
192 }
193 yahoo_free_packet(pkt);
194 yahoo_free_rawpacket(rawpkt);
195 }
196 }
197
198 static void yahoo_login(struct aim_user *user) {
199 struct gaim_connection *gc = new_gaim_conn(PROTO_YAHOO, user->username, user->password);
200 struct yahoo_data *yd = gc->proto_data = g_new0(struct yahoo_data, 1);
201 int i;
202
203 struct yahoo_options opt;
204 struct yahoo_context *ctxt;
205 opt.connect_mode = YAHOO_CONNECT_NORMAL;
206 opt.proxy_host = NULL;
207 ctxt = yahoo_init(user->username, user->password, &opt);
208 yd->ctxt = ctxt;
209
210 set_login_progress(gc, 1, "Connecting");
211 while (gtk_events_pending())
212 gtk_main_iteration();
213
214 if (!ctxt || !yahoo_connect(ctxt)) {
215 debug_printf("Yahoo: Unable to connect\n");
216 hide_login_progress(gc, "Unable to connect");
217 destroy_gaim_conn(gc);
218 return;
219 }
220
221 debug_printf("Yahoo: connected\n");
222
223 set_login_progress(gc, 3, "Getting Config");
224 while (gtk_events_pending())
225 gtk_main_iteration();
226
227 yahoo_get_config(ctxt);
228
229 if (yahoo_cmd_logon(ctxt, YAHOO_STATUS_AVAILABLE)) {
230 debug_printf("Yahoo: Unable to login\n");
231 hide_login_progress(gc, "Unable to login");
232 destroy_gaim_conn(gc);
233 return;
234 }
235
236 if (ctxt->buddies) {
237 struct yahoo_buddy **buddies;
238
239 for (buddies = ctxt->buddies; *buddies; buddies++) {
240 struct yahoo_buddy *bud = *buddies;
241 struct buddy *b;
242 struct group *g;
243
244 b = find_buddy(gc, bud->id);
245 if (!b) add_buddy(gc, bud->group, bud->id, bud->id);
246 }
247 }
248
249 debug_printf("Yahoo: logged in %s\n", gc->username);
250 account_online(user, gc);
251 serv_finish_login(gc);
252
253 gc->inpa = gdk_input_add(ctxt->sockfd, GDK_INPUT_READ | GDK_INPUT_EXCEPTION,
254 yahoo_callback, gc);
255 }
256
257 static void yahoo_close(struct gaim_connection *gc) {
258 struct yahoo_data *yd = (struct yahoo_data *)gc->proto_data;
259 if (gc->inpa)
260 gdk_input_remove(gc->inpa);
261 gc->inpa = -1;
262 yahoo_cmd_logoff(yd->ctxt);
263 g_free(yd);
264 }
265
266 static void yahoo_send_im(struct gaim_connection *gc, char *who, char *message, int away) {
267 struct yahoo_data *yd = (struct yahoo_data *)gc->proto_data;
268
269 yahoo_cmd_msg(yd->ctxt, gc->username, who, message);
270 }
271
272 static void yahoo_set_idle(struct gaim_connection *gc, int idle) {
273 struct yahoo_data *yd = (struct yahoo_data *)gc->proto_data;
274
275 if (idle)
276 yahoo_cmd_set_away_mode(yd->ctxt, YAHOO_STATUS_IDLE, NULL);
277 else
278 yahoo_cmd_set_back_mode(yd->ctxt, YAHOO_STATUS_AVAILABLE, NULL);
279 }
280
281 static void yahoo_keepalive(struct gaim_connection *gc) {
282 yahoo_cmd_ping(((struct yahoo_data *)gc->proto_data)->ctxt);
283 }
284
285 static void gyahoo_add_buddy(struct gaim_connection *gc, char *name) {
286 struct yahoo_data *yd = (struct yahoo_data *)gc->proto_data;
287 struct yahoo_buddy *tmpbuddy;
288 struct group *g = find_group_by_buddy(gc, name);
289 char *group = NULL;
290
291 if (g) {
292 group = g->name;
293 } else if (yd->ctxt && yd->ctxt->buddies[0]) {
294 tmpbuddy = yd->ctxt->buddies[0];
295 group = tmpbuddy->group;
296 }
297
298 if (group)
299 yahoo_add_buddy(yd->ctxt, name, gc->username, group, "");
300 }
301
302 static struct prpl *my_protocol = NULL;
303
304 void Yahoo_init(struct prpl *ret) {
305 /* the NULL's aren't required but they're nice to have */
306 ret->protocol = PROTO_YAHOO;
307 ret->name = yahoo_name;
308 ret->list_icon = NULL;
309 ret->action_menu = NULL;
310 ret->login = yahoo_login;
311 ret->close = yahoo_close;
312 ret->send_im = yahoo_send_im;
313 ret->set_info = NULL;
314 ret->get_info = NULL;
315 ret->set_away = NULL;
316 ret->get_away_msg = NULL;
317 ret->set_dir = NULL;
318 ret->get_dir = NULL;
319 ret->dir_search = NULL;
320 ret->set_idle = yahoo_set_idle;
321 ret->change_passwd = NULL;
322 ret->add_buddy = gyahoo_add_buddy;
323 ret->add_buddies = NULL;
324 ret->remove_buddy = NULL;
325 ret->add_permit = NULL;
326 ret->add_deny = NULL;
327 ret->rem_permit = NULL;
328 ret->rem_deny = NULL;
329 ret->set_permit_deny = NULL;
330 ret->warn = NULL;
331 ret->accept_chat = NULL;
332 ret->join_chat = NULL;
333 ret->chat_invite = NULL;
334 ret->chat_leave = NULL;
335 ret->chat_whisper = NULL;
336 ret->chat_send = NULL;
337 ret->keepalive = yahoo_keepalive;
338
339 my_protocol = ret;
340 }
341
342 char *gaim_plugin_init(GModule *handle) {
343 load_protocol(Yahoo_init);
344 return NULL;
345 }
346
347 void gaim_plugin_remove() {
348 struct prpl *p = find_prpl(PROTO_YAHOO);
349 if (p == my_protocol)
350 unload_protocol(p);
351 }