comparison libgaim/protocols/irc/msgs.c @ 14192:60b1bc8dbf37

[gaim-migrate @ 16863] Renamed 'core' to 'libgaim' committer: Tailor Script <tailor@pidgin.im>
author Evan Schoenberg <evan.s@dreskin.net>
date Sat, 19 Aug 2006 01:50:10 +0000
parents
children 9d669dc232ad
comparison
equal deleted inserted replaced
14191:009db0b357b5 14192:60b1bc8dbf37
1 /**
2 * @file msgs.c
3 *
4 * gaim
5 *
6 * Copyright (C) 2003, Ethan Blanton <eblanton@cs.purdue.edu>
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
23 #include "internal.h"
24
25 #include "conversation.h"
26 #include "blist.h"
27 #include "notify.h"
28 #include "util.h"
29 #include "debug.h"
30 #include "irc.h"
31
32 #include <stdio.h>
33
34 static char *irc_mask_nick(const char *mask);
35 static char *irc_mask_userhost(const char *mask);
36 static void irc_chat_remove_buddy(GaimConversation *convo, char *data[2]);
37 static void irc_buddy_status(char *name, struct irc_buddy *ib, struct irc_conn *irc);
38
39 static void irc_msg_handle_privmsg(struct irc_conn *irc, const char *name,
40 const char *from, const char *to,
41 const char *rawmsg, gboolean notice);
42
43 static char *irc_mask_nick(const char *mask)
44 {
45 char *end, *buf;
46
47 end = strchr(mask, '!');
48 if (!end)
49 buf = g_strdup(mask);
50 else
51 buf = g_strndup(mask, end - mask);
52
53 return buf;
54 }
55
56 static char *irc_mask_userhost(const char *mask)
57 {
58 return g_strdup(strchr(mask, '!') + 1);
59 }
60
61 static void irc_chat_remove_buddy(GaimConversation *convo, char *data[2])
62 {
63 char *message;
64
65 message = g_strdup_printf("quit: %s", data[1]);
66
67 if (gaim_conv_chat_find_user(GAIM_CONV_CHAT(convo), data[0]))
68 gaim_conv_chat_remove_user(GAIM_CONV_CHAT(convo), data[0], message);
69
70 g_free(message);
71 }
72
73 void irc_msg_default(struct irc_conn *irc, const char *name, const char *from, char **args)
74 {
75 gaim_debug(GAIM_DEBUG_INFO, "irc", "Unrecognized message: %s\n", args[0]);
76 }
77
78 void irc_msg_away(struct irc_conn *irc, const char *name, const char *from, char **args)
79 {
80 GaimConnection *gc;
81 char *msg;
82
83 if (!args || !args[1])
84 return;
85
86 if (irc->whois.nick && !gaim_utf8_strcasecmp(irc->whois.nick, args[1])) {
87 /* We're doing a whois, show this in the whois dialog */
88 irc_msg_whois(irc, name, from, args);
89 return;
90 }
91
92 gc = gaim_account_get_connection(irc->account);
93 if (gc) {
94 msg = g_markup_escape_text(args[2], -1);
95 serv_got_im(gc, args[1], msg, GAIM_MESSAGE_AUTO_RESP, time(NULL));
96 g_free(msg);
97 }
98 }
99
100 void irc_msg_badmode(struct irc_conn *irc, const char *name, const char *from, char **args)
101 {
102 GaimConnection *gc = gaim_account_get_connection(irc->account);
103
104 if (!args || !args[1] || !gc)
105 return;
106
107 gaim_notify_error(gc, NULL, _("Bad mode"), args[1]);
108 }
109
110 void irc_msg_banned(struct irc_conn *irc, const char *name, const char *from, char **args)
111 {
112 GaimConnection *gc = gaim_account_get_connection(irc->account);
113 char *buf;
114
115 if (!args || !args[1] || !gc)
116 return;
117
118 buf = g_strdup_printf(_("You are banned from %s."), args[1]);
119 gaim_notify_error(gc, _("Banned"), _("Banned"), buf);
120 g_free(buf);
121 }
122
123 void irc_msg_banfull(struct irc_conn *irc, const char *name, const char *from, char **args)
124 {
125 GaimConversation *convo;
126 char *buf, *nick;
127
128 if (!args || !args[0] || !args[1] || !args[2])
129 return;
130
131 convo = gaim_find_conversation_with_account(GAIM_CONV_TYPE_CHAT, args[1], irc->account);
132 if (!convo)
133 return;
134
135 nick = g_markup_escape_text(args[2], -1);
136 buf = g_strdup_printf(_("Cannot ban %s: banlist is full"), nick);
137 g_free(nick);
138 gaim_conv_chat_write(GAIM_CONV_CHAT(convo), "", buf,
139 GAIM_MESSAGE_SYSTEM|GAIM_MESSAGE_NO_LOG,
140 time(NULL));
141 g_free(buf);
142 }
143
144 void irc_msg_chanmode(struct irc_conn *irc, const char *name, const char *from, char **args)
145 {
146 GaimConversation *convo;
147 char *buf, *escaped;
148
149 if (!args || !args[1] || !args[2])
150 return;
151
152 convo = gaim_find_conversation_with_account(GAIM_CONV_TYPE_CHAT, args[1], irc->account);
153 if (!convo) /* XXX punt on channels we are not in for now */
154 return;
155
156 escaped = (args[3] != NULL) ? g_markup_escape_text(args[3], -1) : NULL;
157 buf = g_strdup_printf("mode for %s: %s %s", args[1], args[2], escaped ? escaped : "");
158 gaim_conv_chat_write(GAIM_CONV_CHAT(convo), "", buf, GAIM_MESSAGE_SYSTEM, time(NULL));
159 g_free(escaped);
160 g_free(buf);
161
162 return;
163 }
164
165 void irc_msg_whois(struct irc_conn *irc, const char *name, const char *from, char **args)
166 {
167 if (!irc->whois.nick) {
168 gaim_debug(GAIM_DEBUG_WARNING, "irc", "Unexpected WHOIS reply for %s\n", args[1]);
169 return;
170 }
171
172 if (gaim_utf8_strcasecmp(irc->whois.nick, args[1])) {
173 gaim_debug(GAIM_DEBUG_WARNING, "irc", "Got WHOIS reply for %s while waiting for %s\n", args[1], irc->whois.nick);
174 return;
175 }
176
177 if (!strcmp(name, "301")) {
178 irc->whois.away = g_strdup(args[2]);
179 } else if (!strcmp(name, "311")) {
180 irc->whois.userhost = g_strdup_printf("%s@%s", args[2], args[3]);
181 irc->whois.name = g_strdup(args[5]);
182 } else if (!strcmp(name, "312")) {
183 irc->whois.server = g_strdup(args[2]);
184 irc->whois.serverinfo = g_strdup(args[3]);
185 } else if (!strcmp(name, "313")) {
186 irc->whois.ircop = 1;
187 } else if (!strcmp(name, "317")) {
188 irc->whois.idle = atoi(args[2]);
189 if (args[3])
190 irc->whois.signon = (time_t)atoi(args[3]);
191 } else if (!strcmp(name, "319")) {
192 irc->whois.channels = g_strdup(args[2]);
193 } else if (!strcmp(name, "320")) {
194 irc->whois.identified = 1;
195 }
196 }
197
198 void irc_msg_endwhois(struct irc_conn *irc, const char *name, const char *from, char **args)
199 {
200 GaimConnection *gc;
201 GString *info;
202 char *str, *tmp;
203
204 if (!irc->whois.nick) {
205 gaim_debug(GAIM_DEBUG_WARNING, "irc", "Unexpected End of WHOIS for %s\n", args[1]);
206 return;
207 }
208 if (gaim_utf8_strcasecmp(irc->whois.nick, args[1])) {
209 gaim_debug(GAIM_DEBUG_WARNING, "irc", "Received end of WHOIS for %s, expecting %s\n", args[1], irc->whois.nick);
210 return;
211 }
212
213 info = g_string_new("");
214 tmp = g_markup_escape_text(args[1], -1);
215 g_string_append_printf(info, _("<b>%s:</b> %s"), _("Nick"), tmp);
216 g_free(tmp);
217 g_string_append_printf(info, "%s%s<br>",
218 irc->whois.ircop ? _(" <i>(ircop)</i>") : "",
219 irc->whois.identified ? _(" <i>(identified)</i>") : "");
220 if (irc->whois.away) {
221 tmp = g_markup_escape_text(irc->whois.away, strlen(irc->whois.away));
222 g_free(irc->whois.away);
223 g_string_append_printf(info, _("<b>%s:</b> %s<br>"), _("Away"), tmp);
224 g_free(tmp);
225 }
226 if (irc->whois.userhost) {
227 tmp = g_markup_escape_text(irc->whois.name, strlen(irc->whois.name));
228 g_free(irc->whois.name);
229 g_string_append_printf(info, _("<b>%s:</b> %s<br>"), _("Username"), irc->whois.userhost);
230 g_string_append_printf(info, _("<b>%s:</b> %s<br>"), _("Real name"), tmp);
231 g_free(irc->whois.userhost);
232 g_free(tmp);
233 }
234 if (irc->whois.server) {
235 g_string_append_printf(info, _("<b>%s:</b> %s"), _("Server"), irc->whois.server);
236 g_string_append_printf(info, " (%s)<br>", irc->whois.serverinfo);
237 g_free(irc->whois.server);
238 g_free(irc->whois.serverinfo);
239 }
240 if (irc->whois.channels) {
241 g_string_append_printf(info, _("<b>%s:</b> %s<br>"), _("Currently on"), irc->whois.channels);
242 g_free(irc->whois.channels);
243 }
244 if (irc->whois.idle) {
245 gchar *timex = gaim_str_seconds_to_string(irc->whois.idle);
246 g_string_append_printf(info, _("<b>Idle for:</b> %s<br>"), timex);
247 g_free(timex);
248 g_string_append_printf(info, _("<b>%s:</b> %s"), _("Online since"),
249 gaim_date_format_full(localtime(&irc->whois.signon)));
250 }
251 if (!strcmp(irc->whois.nick, "Paco-Paco")) {
252 g_string_append_printf(info, _("<br><b>Defining adjective:</b> Glorious<br>"));
253 }
254
255 gc = gaim_account_get_connection(irc->account);
256 str = g_string_free(info, FALSE);
257
258 gaim_notify_userinfo(gc, irc->whois.nick, str, NULL, NULL);
259
260 g_free(irc->whois.nick);
261 g_free(str);
262 memset(&irc->whois, 0, sizeof(irc->whois));
263 }
264
265 void irc_msg_list(struct irc_conn *irc, const char *name, const char *from, char **args)
266 {
267 if (!irc->roomlist)
268 return;
269
270 if (!strcmp(name, "321")) {
271 gaim_roomlist_set_in_progress(irc->roomlist, TRUE);
272 return;
273 }
274
275 if (!strcmp(name, "323")) {
276 gaim_roomlist_set_in_progress(irc->roomlist, FALSE);
277 gaim_roomlist_unref(irc->roomlist);
278 irc->roomlist = NULL;
279 return;
280 }
281
282 if (!strcmp(name, "322")) {
283 GaimRoomlistRoom *room;
284
285 if (!args[0] || !args[1] || !args[2] || !args[3])
286 return;
287
288 room = gaim_roomlist_room_new(GAIM_ROOMLIST_ROOMTYPE_ROOM, args[1], NULL);
289 gaim_roomlist_room_add_field(irc->roomlist, room, args[1]);
290 gaim_roomlist_room_add_field(irc->roomlist, room, GINT_TO_POINTER(strtol(args[2], NULL, 10)));
291 gaim_roomlist_room_add_field(irc->roomlist, room, args[3]);
292 gaim_roomlist_room_add(irc->roomlist, room);
293 }
294 }
295
296 void irc_msg_topic(struct irc_conn *irc, const char *name, const char *from, char **args)
297 {
298 char *chan, *topic, *msg, *nick, *tmp, *tmp2;
299 GaimConversation *convo;
300
301 if (!strcmp(name, "topic")) {
302 chan = args[0];
303 topic = irc_mirc2txt (args[1]);
304 } else {
305 chan = args[1];
306 topic = irc_mirc2txt (args[2]);
307 }
308
309 convo = gaim_find_conversation_with_account(GAIM_CONV_TYPE_CHAT, chan, irc->account);
310 if (!convo) {
311 gaim_debug(GAIM_DEBUG_ERROR, "irc", "Got a topic for %s, which doesn't exist\n", chan);
312 g_free(topic);
313 return;
314 }
315
316 /* If this is an interactive update, print it out */
317 tmp = g_markup_escape_text(topic, -1);
318 tmp2 = gaim_markup_linkify(tmp);
319 g_free(tmp);
320 if (!strcmp(name, "topic")) {
321 const char *current_topic = gaim_conv_chat_get_topic(GAIM_CONV_CHAT(convo));
322 if (!(current_topic != NULL && strcmp(tmp2, current_topic) == 0))
323 {
324 char *nick_esc;
325 nick = irc_mask_nick(from);
326 nick_esc = g_markup_escape_text(nick, -1);
327 gaim_conv_chat_set_topic(GAIM_CONV_CHAT(convo), nick, topic);
328 if (*tmp2)
329 msg = g_strdup_printf(_("%s has changed the topic to: %s"), nick_esc, tmp2);
330 else
331 msg = g_strdup_printf(_("%s has cleared the topic."), nick_esc);
332 g_free(nick_esc);
333 g_free(nick);
334 gaim_conv_chat_write(GAIM_CONV_CHAT(convo), from, msg, GAIM_MESSAGE_SYSTEM, time(NULL));
335 g_free(msg);
336 }
337 } else {
338 char *chan_esc = g_markup_escape_text(chan, -1);
339 msg = g_strdup_printf(_("The topic for %s is: %s"), chan_esc, tmp2);
340 g_free(chan_esc);
341 gaim_conv_chat_set_topic(GAIM_CONV_CHAT(convo), NULL, topic);
342 gaim_conv_chat_write(GAIM_CONV_CHAT(convo), "", msg, GAIM_MESSAGE_SYSTEM, time(NULL));
343 g_free(msg);
344 }
345 g_free(tmp2);
346 g_free(topic);
347 }
348
349 void irc_msg_unknown(struct irc_conn *irc, const char *name, const char *from, char **args)
350 {
351 GaimConnection *gc = gaim_account_get_connection(irc->account);
352 char *buf;
353
354 if (!args || !args[1] || !gc)
355 return;
356
357 buf = g_strdup_printf(_("Unknown message '%s'"), args[1]);
358 gaim_notify_error(gc, _("Unknown message"), buf, _("Gaim has sent a message the IRC server did not understand."));
359 g_free(buf);
360 }
361
362 void irc_msg_names(struct irc_conn *irc, const char *name, const char *from, char **args)
363 {
364 char *names, *cur, *end, *tmp, *msg;
365 GaimConversation *convo;
366
367 if (!strcmp(name, "366")) {
368 convo = gaim_find_conversation_with_account(GAIM_CONV_TYPE_ANY, irc->nameconv ? irc->nameconv : args[1], irc->account);
369 if (!convo) {
370 gaim_debug(GAIM_DEBUG_ERROR, "irc", "Got a NAMES list for %s, which doesn't exist\n", args[1]);
371 g_string_free(irc->names, TRUE);
372 irc->names = NULL;
373 g_free(irc->nameconv);
374 irc->nameconv = NULL;
375 return;
376 }
377
378 names = cur = g_string_free(irc->names, FALSE);
379 irc->names = NULL;
380 if (irc->nameconv) {
381 msg = g_strdup_printf(_("Users on %s: %s"), args[1], names ? names : "");
382 if (gaim_conversation_get_type(convo) == GAIM_CONV_TYPE_CHAT)
383 gaim_conv_chat_write(GAIM_CONV_CHAT(convo), "", msg, GAIM_MESSAGE_SYSTEM|GAIM_MESSAGE_NO_LOG, time(NULL));
384 else
385 gaim_conv_im_write(GAIM_CONV_IM(convo), "", msg, GAIM_MESSAGE_SYSTEM|GAIM_MESSAGE_NO_LOG, time(NULL));
386 g_free(msg);
387 g_free(irc->nameconv);
388 irc->nameconv = NULL;
389 } else {
390 GList *users = NULL;
391 GList *flags = NULL;
392
393 while (*cur) {
394 GaimConvChatBuddyFlags f = GAIM_CBFLAGS_NONE;
395 end = strchr(cur, ' ');
396 if (!end)
397 end = cur + strlen(cur);
398 if (*cur == '@') {
399 f = GAIM_CBFLAGS_OP;
400 cur++;
401 } else if (*cur == '%') {
402 f = GAIM_CBFLAGS_HALFOP;
403 cur++;
404 } else if(*cur == '+') {
405 f = GAIM_CBFLAGS_VOICE;
406 cur++;
407 }
408 tmp = g_strndup(cur, end - cur);
409 users = g_list_prepend(users, tmp);
410 flags = g_list_prepend(flags, GINT_TO_POINTER(f));
411 cur = end;
412 if (*cur)
413 cur++;
414 }
415
416 if (users != NULL) {
417 GList *l;
418
419 gaim_conv_chat_add_users(GAIM_CONV_CHAT(convo), users, NULL, flags, FALSE);
420
421 for (l = users; l != NULL; l = l->next)
422 g_free(l->data);
423
424 g_list_free(users);
425 g_list_free(flags);
426 }
427 }
428 g_free(names);
429 } else {
430 if (!irc->names)
431 irc->names = g_string_new("");
432
433 irc->names = g_string_append(irc->names, args[3]);
434 }
435 }
436
437 void irc_msg_motd(struct irc_conn *irc, const char *name, const char *from, char **args)
438 {
439 GaimConnection *gc;
440 char *escaped;
441 if (!strcmp(name, "375")) {
442 gc = gaim_account_get_connection(irc->account);
443 if (gc)
444 gaim_connection_set_display_name(gc, args[0]);
445 }
446
447 if (!irc->motd)
448 irc->motd = g_string_new("");
449
450 escaped = g_markup_escape_text(args[1], -1);
451 g_string_append_printf(irc->motd, "%s<br>", escaped);
452 g_free(escaped);
453 }
454
455 void irc_msg_endmotd(struct irc_conn *irc, const char *name, const char *from, char **args)
456 {
457 GaimConnection *gc;
458 GaimStatus *status;
459 GaimBlistNode *gnode, *cnode, *bnode;
460
461 gc = gaim_account_get_connection(irc->account);
462 if (!gc)
463 return;
464
465 gaim_connection_set_state(gc, GAIM_CONNECTED);
466
467 /* If we're away then set our away message */
468 status = gaim_account_get_active_status(irc->account);
469 if (!gaim_status_get_type(status) != GAIM_STATUS_AVAILABLE)
470 {
471 GaimPluginProtocolInfo *prpl_info = GAIM_PLUGIN_PROTOCOL_INFO(gc->prpl);
472 prpl_info->set_status(irc->account, status);
473 }
474
475 /* this used to be in the core, but it's not now */
476 for (gnode = gaim_get_blist()->root; gnode; gnode = gnode->next) {
477 if(!GAIM_BLIST_NODE_IS_GROUP(gnode))
478 continue;
479 for(cnode = gnode->child; cnode; cnode = cnode->next) {
480 if(!GAIM_BLIST_NODE_IS_CONTACT(cnode))
481 continue;
482 for(bnode = cnode->child; bnode; bnode = bnode->next) {
483 GaimBuddy *b;
484 if(!GAIM_BLIST_NODE_IS_BUDDY(bnode))
485 continue;
486 b = (GaimBuddy *)bnode;
487 if(b->account == gc->account) {
488 struct irc_buddy *ib = g_new0(struct irc_buddy, 1);
489 ib->name = g_strdup(b->name);
490 g_hash_table_insert(irc->buddies, ib->name, ib);
491 }
492 }
493 }
494 }
495
496 irc_blist_timeout(irc);
497 if (!irc->timer)
498 irc->timer = gaim_timeout_add(45000, (GSourceFunc)irc_blist_timeout, (gpointer)irc);
499 }
500
501 void irc_msg_time(struct irc_conn *irc, const char *name, const char *from, char **args)
502 {
503 GaimConnection *gc;
504
505 gc = gaim_account_get_connection(irc->account);
506 if (gc == NULL || args == NULL || args[2] == NULL)
507 return;
508
509 gaim_notify_message(gc, GAIM_NOTIFY_MSG_INFO, _("Time Response"),
510 _("The IRC server's local time is:"),
511 args[2], NULL, NULL);
512 }
513
514 void irc_msg_nochan(struct irc_conn *irc, const char *name, const char *from, char **args)
515 {
516 GaimConnection *gc = gaim_account_get_connection(irc->account);
517
518 if (gc == NULL || args == NULL || args[1] == NULL)
519 return;
520
521 gaim_notify_error(gc, NULL, _("No such channel"), args[1]);
522 }
523
524 void irc_msg_nonick(struct irc_conn *irc, const char *name, const char *from, char **args)
525 {
526 GaimConnection *gc;
527 GaimConversation *convo;
528
529 convo = gaim_find_conversation_with_account(GAIM_CONV_TYPE_ANY, args[1], irc->account);
530 if (convo) {
531 if (gaim_conversation_get_type(convo) == GAIM_CONV_TYPE_CHAT) /* does this happen? */
532 gaim_conv_chat_write(GAIM_CONV_CHAT(convo), args[1], _("no such channel"),
533 GAIM_MESSAGE_SYSTEM|GAIM_MESSAGE_NO_LOG, time(NULL));
534 else
535 gaim_conv_im_write(GAIM_CONV_IM(convo), args[1], _("User is not logged in"),
536 GAIM_MESSAGE_SYSTEM|GAIM_MESSAGE_NO_LOG, time(NULL));
537 } else {
538 if ((gc = gaim_account_get_connection(irc->account)) == NULL)
539 return;
540 gaim_notify_error(gc, NULL, _("No such nick or channel"), args[1]);
541 }
542
543 if (irc->whois.nick && !gaim_utf8_strcasecmp(irc->whois.nick, args[1])) {
544 g_free(irc->whois.nick);
545 irc->whois.nick = NULL;
546 }
547 }
548
549 void irc_msg_nosend(struct irc_conn *irc, const char *name, const char *from, char **args)
550 {
551 GaimConnection *gc;
552 GaimConversation *convo;
553
554 convo = gaim_find_conversation_with_account(GAIM_CONV_TYPE_CHAT, args[1], irc->account);
555 if (convo) {
556 gaim_conv_chat_write(GAIM_CONV_CHAT(convo), args[1], args[2], GAIM_MESSAGE_SYSTEM|GAIM_MESSAGE_NO_LOG, time(NULL));
557 } else {
558 if ((gc = gaim_account_get_connection(irc->account)) == NULL)
559 return;
560 gaim_notify_error(gc, NULL, _("Could not send"), args[2]);
561 }
562 }
563
564 void irc_msg_notinchan(struct irc_conn *irc, const char *name, const char *from, char **args)
565 {
566 GaimConversation *convo = gaim_find_conversation_with_account(GAIM_CONV_TYPE_CHAT, args[1], irc->account);
567
568 gaim_debug(GAIM_DEBUG_INFO, "irc", "We're apparently not in %s, but tried to use it\n", args[1]);
569 if (convo) {
570 /*g_slist_remove(irc->gc->buddy_chats, convo);
571 gaim_conversation_set_account(convo, NULL);*/
572 gaim_conv_chat_write(GAIM_CONV_CHAT(convo), args[1], args[2], GAIM_MESSAGE_SYSTEM|GAIM_MESSAGE_NO_LOG, time(NULL));
573 }
574 }
575
576 void irc_msg_notop(struct irc_conn *irc, const char *name, const char *from, char **args)
577 {
578 GaimConversation *convo;
579
580 if (!args || !args[1] || !args[2])
581 return;
582
583 convo = gaim_find_conversation_with_account(GAIM_CONV_TYPE_CHAT, args[1], irc->account);
584 if (!convo)
585 return;
586
587 gaim_conv_chat_write(GAIM_CONV_CHAT(convo), "", args[2], GAIM_MESSAGE_SYSTEM, time(NULL));
588 }
589
590 void irc_msg_invite(struct irc_conn *irc, const char *name, const char *from, char **args)
591 {
592 GaimConnection *gc = gaim_account_get_connection(irc->account);
593 GHashTable *components = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
594 char *nick = irc_mask_nick(from);
595
596 if (!args || !args[1] || !gc) {
597 g_free(nick);
598 g_hash_table_destroy(components);
599 return;
600 }
601
602 g_hash_table_insert(components, strdup("channel"), strdup(args[1]));
603
604 serv_got_chat_invite(gc, args[1], nick, NULL, components);
605 g_free(nick);
606 }
607
608 void irc_msg_inviteonly(struct irc_conn *irc, const char *name, const char *from, char **args)
609 {
610 GaimConnection *gc = gaim_account_get_connection(irc->account);
611 char *buf;
612
613 if (!args || !args[1] || !gc)
614 return;
615
616 buf = g_strdup_printf(_("Joining %s requires an invitation."), args[1]);
617 gaim_notify_error(gc, _("Invitation only"), _("Invitation only"), buf);
618 g_free(buf);
619 }
620
621 void irc_msg_ison(struct irc_conn *irc, const char *name, const char *from, char **args)
622 {
623 char **nicks;
624 struct irc_buddy *ib;
625 int i;
626
627 if (!args || !args[1])
628 return;
629
630 nicks = g_strsplit(args[1], " ", -1);
631
632 for (i = 0; nicks[i]; i++) {
633 if ((ib = g_hash_table_lookup(irc->buddies, (gconstpointer)nicks[i])) == NULL) {
634 continue;
635 }
636 ib->flag = TRUE;
637 }
638
639 g_strfreev(nicks);
640
641 g_hash_table_foreach(irc->buddies, (GHFunc)irc_buddy_status, (gpointer)irc);
642 }
643
644 static void irc_buddy_status(char *name, struct irc_buddy *ib, struct irc_conn *irc)
645 {
646 GaimConnection *gc = gaim_account_get_connection(irc->account);
647 GaimBuddy *buddy = gaim_find_buddy(irc->account, name);
648
649 if (!gc || !buddy)
650 return;
651
652 if (ib->online && !ib->flag) {
653 gaim_prpl_got_user_status(irc->account, name, "offline", NULL);
654 ib->online = FALSE;
655 } else if (!ib->online && ib->flag) {
656 gaim_prpl_got_user_status(irc->account, name, "available", NULL);
657 ib->online = TRUE;
658 }
659 }
660
661 void irc_msg_join(struct irc_conn *irc, const char *name, const char *from, char **args)
662 {
663 GaimConnection *gc = gaim_account_get_connection(irc->account);
664 GaimConversation *convo;
665 char *nick = irc_mask_nick(from), *userhost;
666 struct irc_buddy *ib;
667 static int id = 1;
668
669 if (!gc) {
670 g_free(nick);
671 return;
672 }
673
674 if (!gaim_utf8_strcasecmp(nick, gaim_connection_get_display_name(gc))) {
675 /* We are joining a channel for the first time */
676 serv_got_joined_chat(gc, id++, args[0]);
677 g_free(nick);
678 convo = gaim_find_conversation_with_account(GAIM_CONV_TYPE_CHAT,
679 args[0],
680 irc->account);
681 if (convo == NULL) {
682 gaim_debug_error("irc", "tried to join %s but couldn't\n", args[0]);
683 return;
684 }
685 gaim_conversation_present(convo);
686 return;
687 }
688
689 convo = gaim_find_conversation_with_account(GAIM_CONV_TYPE_CHAT, args[0], irc->account);
690 if (convo == NULL) {
691 gaim_debug(GAIM_DEBUG_ERROR, "irc", "JOIN for %s failed\n", args[0]);
692 g_free(nick);
693 return;
694 }
695
696 userhost = irc_mask_userhost(from);
697 gaim_conv_chat_add_user(GAIM_CONV_CHAT(convo), nick, userhost, GAIM_CBFLAGS_NONE, TRUE);
698
699 if ((ib = g_hash_table_lookup(irc->buddies, nick)) != NULL) {
700 ib->flag = TRUE;
701 irc_buddy_status(nick, ib, irc);
702 }
703
704 g_free(userhost);
705 g_free(nick);
706 }
707
708 void irc_msg_kick(struct irc_conn *irc, const char *name, const char *from, char **args)
709 {
710 GaimConnection *gc = gaim_account_get_connection(irc->account);
711 GaimConversation *convo = gaim_find_conversation_with_account(GAIM_CONV_TYPE_CHAT, args[0], irc->account);
712 char *nick = irc_mask_nick(from), *buf;
713
714 if (!gc) {
715 g_free(nick);
716 return;
717 }
718
719 if (!convo) {
720 gaim_debug(GAIM_DEBUG_ERROR, "irc", "Recieved a KICK for unknown channel %s\n", args[0]);
721 g_free(nick);
722 return;
723 }
724
725 if (!gaim_utf8_strcasecmp(gaim_connection_get_display_name(gc), args[1])) {
726 buf = g_strdup_printf(_("You have been kicked by %s: (%s)"), nick, args[2]);
727 gaim_conv_chat_write(GAIM_CONV_CHAT(convo), args[0], buf, GAIM_MESSAGE_SYSTEM, time(NULL));
728 g_free(buf);
729 serv_got_chat_left(gc, gaim_conv_chat_get_id(GAIM_CONV_CHAT(convo)));
730 } else {
731 buf = g_strdup_printf(_("Kicked by %s (%s)"), nick, args[2]);
732 gaim_conv_chat_remove_user(GAIM_CONV_CHAT(convo), args[1], buf);
733 g_free(buf);
734 }
735
736 g_free(nick);
737 return;
738 }
739
740 void irc_msg_mode(struct irc_conn *irc, const char *name, const char *from, char **args)
741 {
742 GaimConversation *convo;
743 char *nick = irc_mask_nick(from), *buf;
744
745 if (*args[0] == '#' || *args[0] == '&') { /* Channel */
746 char *escaped;
747 convo = gaim_find_conversation_with_account(GAIM_CONV_TYPE_CHAT, args[0], irc->account);
748 if (!convo) {
749 gaim_debug(GAIM_DEBUG_ERROR, "irc", "MODE received for %s, which we are not in\n", args[0]);
750 g_free(nick);
751 return;
752 }
753 escaped = (args[2] != NULL) ? g_markup_escape_text(args[2], -1) : NULL;
754 buf = g_strdup_printf(_("mode (%s %s) by %s"), args[1], escaped ? escaped : "", nick);
755 gaim_conv_chat_write(GAIM_CONV_CHAT(convo), args[0], buf, GAIM_MESSAGE_SYSTEM, time(NULL));
756 g_free(escaped);
757 g_free(buf);
758 if(args[2]) {
759 GaimConvChatBuddyFlags newflag, flags;
760 char *mcur, *cur, *end, *user;
761 gboolean add = FALSE;
762 mcur = args[1];
763 cur = args[2];
764 while (*cur && *mcur) {
765 if ((*mcur == '+') || (*mcur == '-')) {
766 add = (*mcur == '+') ? TRUE : FALSE;
767 mcur++;
768 continue;
769 }
770 end = strchr(cur, ' ');
771 if (!end)
772 end = cur + strlen(cur);
773 user = g_strndup(cur, end - cur);
774 flags = gaim_conv_chat_user_get_flags(GAIM_CONV_CHAT(convo), user);
775 newflag = GAIM_CBFLAGS_NONE;
776 if (*mcur == 'o')
777 newflag = GAIM_CBFLAGS_OP;
778 else if (*mcur =='h')
779 newflag = GAIM_CBFLAGS_HALFOP;
780 else if (*mcur == 'v')
781 newflag = GAIM_CBFLAGS_VOICE;
782 if (newflag) {
783 if (add)
784 flags |= newflag;
785 else
786 flags &= ~newflag;
787 gaim_conv_chat_user_set_flags(GAIM_CONV_CHAT(convo), user, flags);
788 }
789 g_free(user);
790 cur = end;
791 if (*cur)
792 cur++;
793 if (*mcur)
794 mcur++;
795 }
796 }
797 } else { /* User */
798 }
799 g_free(nick);
800 }
801
802 void irc_msg_nick(struct irc_conn *irc, const char *name, const char *from, char **args)
803 {
804 GaimConnection *gc = gaim_account_get_connection(irc->account);
805 GaimConversation *conv;
806 GSList *chats;
807 char *nick = irc_mask_nick(from);
808
809 if (!gc) {
810 g_free(nick);
811 return;
812 }
813 chats = gc->buddy_chats;
814
815 if (!gaim_utf8_strcasecmp(nick, gaim_connection_get_display_name(gc))) {
816 gaim_connection_set_display_name(gc, args[0]);
817 }
818
819 while (chats) {
820 GaimConvChat *chat = GAIM_CONV_CHAT(chats->data);
821 /* This is ugly ... */
822 if (gaim_conv_chat_find_user(chat, nick))
823 gaim_conv_chat_rename_user(chat, nick, args[0]);
824 chats = chats->next;
825 }
826
827 conv = gaim_find_conversation_with_account(GAIM_CONV_TYPE_IM, nick,
828 irc->account);
829 if (conv != NULL)
830 gaim_conversation_set_name(conv, args[0]);
831
832 g_free(nick);
833 }
834
835 void irc_msg_badnick(struct irc_conn *irc, const char *name, const char *from, char **args)
836 {
837 GaimConnection *gc = gaim_account_get_connection(irc->account);
838 if (gaim_connection_get_state(gc) == GAIM_CONNECTED) {
839 gaim_notify_error(gc, _("Invalid nickname"),
840 _("Invalid nickname"),
841 _("Your selected nickname was rejected by the server. It probably contains invalid characters."));
842
843 } else {
844 gaim_connection_error(gaim_account_get_connection(irc->account),
845 _("Your selected account name was rejected by the server. It probably contains invalid characters."));
846 }
847 }
848
849 void irc_msg_nickused(struct irc_conn *irc, const char *name, const char *from, char **args)
850 {
851 char *newnick, *buf, *end;
852
853 if (!args || !args[1])
854 return;
855
856 newnick = strdup(args[1]);
857 end = newnick + strlen(newnick) - 1;
858 /* try fallbacks */
859 if((*end < '9') && (*end >= '1')) {
860 *end = *end + 1;
861 } else *end = '1';
862
863 buf = irc_format(irc, "vn", "NICK", newnick);
864 irc_send(irc, buf);
865 g_free(buf);
866 g_free(newnick);
867 }
868
869 void irc_msg_notice(struct irc_conn *irc, const char *name, const char *from, char **args)
870 {
871 if (!args || !args[0] || !args[1])
872 return;
873
874 irc_msg_handle_privmsg(irc, name, from, args[0], args[1], TRUE);
875 }
876
877 void irc_msg_nochangenick(struct irc_conn *irc, const char *name, const char *from, char **args)
878 {
879 GaimConnection *gc = gaim_account_get_connection(irc->account);
880
881 if (!args || !args[2] || !gc)
882 return;
883
884 gaim_notify_error(gc, _("Cannot change nick"), _("Could not change nick"), args[2]);
885 }
886
887 void irc_msg_part(struct irc_conn *irc, const char *name, const char *from, char **args)
888 {
889 GaimConnection *gc = gaim_account_get_connection(irc->account);
890 GaimConversation *convo;
891 char *nick, *msg;
892
893 if (!args || !args[0] || !gc)
894 return;
895
896 convo = gaim_find_conversation_with_account(GAIM_CONV_TYPE_CHAT, args[0], irc->account);
897 if (!convo) {
898 gaim_debug(GAIM_DEBUG_INFO, "irc", "Got a PART on %s, which doesn't exist -- probably closed\n", args[0]);
899 return;
900 }
901
902 nick = irc_mask_nick(from);
903 if (!gaim_utf8_strcasecmp(nick, gaim_connection_get_display_name(gc))) {
904 char *escaped = g_markup_escape_text(args[1], -1);
905 msg = g_strdup_printf(_("You have parted the channel%s%s"),
906 (args[1] && *args[1]) ? ": " : "",
907 (escaped && *escaped) ? escaped : "");
908 g_free(escaped);
909 gaim_conv_chat_write(GAIM_CONV_CHAT(convo), args[0], msg, GAIM_MESSAGE_SYSTEM, time(NULL));
910 g_free(msg);
911 serv_got_chat_left(gc, gaim_conv_chat_get_id(GAIM_CONV_CHAT(convo)));
912 } else {
913 gaim_conv_chat_remove_user(GAIM_CONV_CHAT(convo), nick, args[1]);
914 }
915 g_free(nick);
916 }
917
918 void irc_msg_ping(struct irc_conn *irc, const char *name, const char *from, char **args)
919 {
920 char *buf;
921 if (!args || !args[0])
922 return;
923
924 buf = irc_format(irc, "v:", "PONG", args[0]);
925 irc_send(irc, buf);
926 g_free(buf);
927 }
928
929 void irc_msg_pong(struct irc_conn *irc, const char *name, const char *from, char **args)
930 {
931 GaimConversation *convo;
932 GaimConnection *gc;
933 char **parts, *msg;
934 time_t oldstamp;
935
936 if (!args || !args[1])
937 return;
938
939 parts = g_strsplit(args[1], " ", 2);
940
941 if (!parts[0] || !parts[1]) {
942 g_strfreev(parts);
943 return;
944 }
945
946 if (sscanf(parts[1], "%lu", &oldstamp) != 1) {
947 msg = g_strdup(_("Error: invalid PONG from server"));
948 } else {
949 msg = g_strdup_printf(_("PING reply -- Lag: %lu seconds"), time(NULL) - oldstamp);
950 }
951
952 convo = gaim_find_conversation_with_account(GAIM_CONV_TYPE_ANY, parts[0], irc->account);
953 g_strfreev(parts);
954 if (convo) {
955 if (gaim_conversation_get_type (convo) == GAIM_CONV_TYPE_CHAT)
956 gaim_conv_chat_write(GAIM_CONV_CHAT(convo), "PONG", msg, GAIM_MESSAGE_SYSTEM|GAIM_MESSAGE_NO_LOG, time(NULL));
957 else
958 gaim_conv_im_write(GAIM_CONV_IM(convo), "PONG", msg, GAIM_MESSAGE_SYSTEM|GAIM_MESSAGE_NO_LOG, time(NULL));
959 } else {
960 gc = gaim_account_get_connection(irc->account);
961 if (!gc) {
962 g_free(msg);
963 return;
964 }
965 gaim_notify_info(gc, NULL, "PONG", msg);
966 }
967 g_free(msg);
968 }
969
970 void irc_msg_privmsg(struct irc_conn *irc, const char *name, const char *from, char **args)
971 {
972 if (!args || !args[0] || !args[1])
973 return;
974
975 irc_msg_handle_privmsg(irc, name, from, args[0], args[1], FALSE);
976 }
977
978 static void irc_msg_handle_privmsg(struct irc_conn *irc, const char *name, const char *from, const char *to, const char *rawmsg, gboolean notice)
979 {
980 GaimConnection *gc = gaim_account_get_connection(irc->account);
981 GaimConversation *convo;
982 char *tmp;
983 char *msg;
984 char *nick;
985
986 if (!gc)
987 return;
988
989 nick = irc_mask_nick(from);
990 tmp = irc_parse_ctcp(irc, nick, to, rawmsg, notice);
991 if (!tmp) {
992 g_free(nick);
993 return;
994 }
995
996 msg = g_markup_escape_text(tmp, -1);
997 g_free(tmp);
998
999 tmp = irc_mirc2html(msg);
1000 g_free(msg);
1001 msg = tmp;
1002 if (notice) {
1003 tmp = g_strdup_printf("(notice) %s", msg);
1004 g_free(msg);
1005 msg = tmp;
1006 }
1007
1008 if (!gaim_utf8_strcasecmp(to, gaim_connection_get_display_name(gc))) {
1009 serv_got_im(gc, nick, msg, 0, time(NULL));
1010 } else {
1011 convo = gaim_find_conversation_with_account(GAIM_CONV_TYPE_CHAT, to, irc->account);
1012 if (convo)
1013 serv_got_chat_in(gc, gaim_conv_chat_get_id(GAIM_CONV_CHAT(convo)), nick, 0, msg, time(NULL));
1014 else
1015 gaim_debug_error("irc", "Got a %s on %s, which does not exist\n",
1016 notice ? "NOTICE" : "PRIVMSG", to);
1017 }
1018 g_free(msg);
1019 g_free(nick);
1020 }
1021
1022 void irc_msg_regonly(struct irc_conn *irc, const char *name, const char *from, char **args)
1023 {
1024 GaimConnection *gc = gaim_account_get_connection(irc->account);
1025 char *msg;
1026
1027 if (!args || !args[1] || !args[2] || !gc)
1028 return;
1029
1030 msg = g_strdup_printf(_("Cannot join %s:"), args[1]);
1031 gaim_notify_error(gc, _("Cannot join channel"), msg, args[2]);
1032 g_free(msg);
1033 }
1034
1035 void irc_msg_quit(struct irc_conn *irc, const char *name, const char *from, char **args)
1036 {
1037 GaimConnection *gc = gaim_account_get_connection(irc->account);
1038 struct irc_buddy *ib;
1039 char *data[2];
1040
1041 if (!args || !args[0] || !gc)
1042 return;
1043
1044 data[0] = irc_mask_nick(from);
1045 data[1] = args[0];
1046 /* XXX this should have an API, I shouldn't grab this directly */
1047 g_slist_foreach(gc->buddy_chats, (GFunc)irc_chat_remove_buddy, data);
1048
1049 if ((ib = g_hash_table_lookup(irc->buddies, data[0])) != NULL) {
1050 ib->flag = FALSE;
1051 irc_buddy_status(data[0], ib, irc);
1052 }
1053 g_free(data[0]);
1054
1055 return;
1056 }
1057
1058 void irc_msg_unavailable(struct irc_conn *irc, const char *name, const char *from, char **args)
1059 {
1060 GaimConnection *gc = gaim_account_get_connection(irc->account);
1061
1062 if (!args || !args[1])
1063 return;
1064
1065 gaim_notify_error(gc, NULL, _("Nick or channel is temporarily unavailable."), args[1]);
1066 }
1067
1068 void irc_msg_wallops(struct irc_conn *irc, const char *name, const char *from, char **args)
1069 {
1070 GaimConnection *gc = gaim_account_get_connection(irc->account);
1071 char *nick, *msg;
1072
1073 if (!args || !args[0] || !gc)
1074 return;
1075
1076 nick = irc_mask_nick(from);
1077 msg = g_strdup_printf (_("Wallops from %s"), nick);
1078 g_free(nick);
1079 gaim_notify_info(gc, NULL, msg, args[0]);
1080 g_free(msg);
1081 }
1082
1083 void irc_msg_ignore(struct irc_conn *irc, const char *name, const char *from, char **args)
1084 {
1085 return;
1086 }