]> git.xonotic.org Git - xonotic/darkplaces.git/blob - irc.c
fixed bug with empty irc_watched_channels
[xonotic/darkplaces.git] / irc.c
1 //      irc.c
2 //      
3 //      Copyright 2011 Akari <Akari` @ irc.quakenet.org>
4 //      
5 //      This program is free software; you can redistribute it and/or modify
6 //      it under the terms of the GNU General Public License as published by
7 //      the Free Software Foundation; either version 2 of the License, or
8 //      (at your option) any later version.
9 //      
10 //      This program is distributed in the hope that it will be useful,
11 //      but WITHOUT ANY WARRANTY; without even the implied warranty of
12 //      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 //      GNU General Public License for more details.
14
15 #include <pthread.h>
16 #include <string.h>
17
18 #include "quakedef.h"
19 #include "irc.h"
20 #include "cvar.h"
21
22 cvar_t irc_server = {CVAR_SAVE, "irc_server", "", "IRC server to connect to"};
23 cvar_t irc_port = {CVAR_SAVE, "irc_port", "6667", "Port of the IRC server"};
24 cvar_t irc_password = {CVAR_SAVE, "irc_password", "", "IRC server password"};
25 cvar_t irc_nick = {CVAR_SAVE, "irc_nick", "", "Your nickname to use on IRC. Note: this cvar only defines your prefered nick, do NOT use this to change your nickname while connected, use irc_chnick instead"};
26 cvar_t irc_connected = {CVAR_READONLY, "irc_connected", "0", "IRC connection state (0 = not connected, 1 = connecting, 2 = connected)"};
27 cvar_t irc_msgprefix = {CVAR_SAVE, "irc_msgprefix", "^5IRC^0|^7", "What all IRC events will be prefixed with when printed to the console"};
28 cvar_t irc_chatwindow = {CVAR_SAVE, "irc_chatwindow", "2", "0 = IRC messages will be printed in the console only, 1 = IRC messages will go to the chat window, 2 = Only hilights and private messages will appear in the chat window"};
29 cvar_t irc_numeric_errorsonly = {CVAR_SAVE, "irc_numeric_errorsonly", "0", "If 1, any numeric event below 400 won't be printed'"};
30 cvar_t irc_save_target = {CVAR_SAVE, "irc_save_target", "1", "Whether to save target for irc_messagemode or not"};
31 cvar_t irc_current_nick = {CVAR_READONLY, "irc_current_nick", "", "Holds your current IRC nick"};
32 cvar_t irc_hilights = {CVAR_SAVE, "irc_hilights", "", "Space separated list of words to hilight"};
33 cvar_t irc_autochangetarget = {CVAR_SAVE, "irc_autochangetarget", "0", "if 1, will automatically change target for irc_messagemode when a hilight or a private message is recieved, so you can reply instantly. Requires irc_save_target"};
34 cvar_t irc_watched_channels = {CVAR_SAVE, "irc_watched_channels", "", "Space separated list of watched channels. PRIVMSGs from those channels will be always printed to the chat area, regardless of irc_chatwindow setting"};
35
36 irc_session_t *irc_session_global = NULL;
37 int irc_msgmode;
38 char last_channel[MAX_INPUTLINE];
39
40 //
41 //  Initialization
42 //
43
44 void CL_Irc_Init(void)
45 {
46     Cvar_RegisterVariable(&irc_server);
47     Cvar_RegisterVariable(&irc_port);
48     Cvar_RegisterVariable(&irc_password);
49     Cvar_RegisterVariable(&irc_nick);
50     Cvar_RegisterVariable(&irc_connected);
51     Cvar_RegisterVariable(&irc_msgprefix);
52     Cvar_RegisterVariable(&irc_chatwindow);
53     Cvar_RegisterVariable(&irc_numeric_errorsonly);
54     Cvar_RegisterVariable(&irc_save_target);
55     Cvar_RegisterVariable(&irc_current_nick);
56     Cvar_RegisterVariable(&irc_hilights);
57     Cvar_RegisterVariable(&irc_autochangetarget);
58     Cvar_RegisterVariable(&irc_watched_channels);
59     
60     Cmd_AddCommand ("irc_connect", CL_Irc_Connect_f, "Connects you to the IRC server");
61     Cmd_AddCommand ("irc_disconnect", CL_Irc_Disconnect_f, "Disconnects you from the IRC server");
62     Cmd_AddCommand ("irc_say", CL_Irc_Say_f, "Sends a privmsg to a channel or nick");
63     Cmd_AddCommand ("irc_notice", CL_Irc_Notice_f, "Sends a notice to a channel or nick");
64     Cmd_AddCommand ("irc_me", CL_Irc_Me_f, "Sends a CTCP ACTION (/me) to a channel or nick");
65     Cmd_AddCommand ("irc_join", CL_Irc_Join_f, "Joins an IRC channel");
66     Cmd_AddCommand ("irc_part", CL_Irc_Part_f, "Parts an IRC channel");
67     Cmd_AddCommand ("irc_names", CL_Irc_Names_f, "Lists users of an IRC channel");
68     Cmd_AddCommand ("irc_messagemode", CL_Irc_MessageMode_f, "Interactive prompt for sending IRC messages. Meant to be bound to a key");
69     Cmd_AddCommand ("irc_chnick", CL_Irc_ChNick_f, "Changes your nick");
70     Cmd_AddCommand ("irc_raw", CL_Irc_Raw_f, "Sends a raw string to the IRC server");
71 }
72
73 //
74 //  Function that starts IRC main loop in a thread
75 //
76
77 static void IRC_Thread(void *p)
78 {
79     if(irc_run(irc_session_global))
80     {
81         Con_Printf("%s^1Error: ^7%s\n",
82             irc_msgprefix.string,
83             irc_strerror(irc_errno(irc_session_global))
84         );
85         
86         CL_Irc_Disconnect_f();
87         return;
88     }
89 }
90
91 //
92 //  irc_ console commands callbacks
93 //
94
95 static void CL_Irc_Connect_f(void)
96 {
97     irc_callbacks_t cb;
98     pthread_t irc_thread;
99     
100     switch(irc_connected.integer)
101     {
102         case 1:
103             Con_Printf("%sAlready connecting to %s:%i\n",
104                 irc_msgprefix.string, 
105                 irc_server.string,
106                 irc_port.integer
107             );
108             return;
109         
110         case 2:
111             Con_Printf("%sAlready connected to %s:%i.\n",
112                 irc_msgprefix.string,
113                 irc_server.string,
114                 irc_port.integer
115             );
116             return;
117     }
118     
119     if(NOTSET(irc_server))
120     {
121         Con_Printf("Please set the irc_server variable\n");
122         return;
123     }
124     
125     if(NOTSET(irc_nick))
126     {
127         Con_Printf("Please set the irc_nick variable\n");
128         return;
129     }
130     
131     if(NOTSET(irc_port))
132     {
133         Con_Printf("Please set the irc_port variable\n");
134         return;
135     }
136     
137     Cvar_SetQuick(&irc_connected, "1");
138     
139     memset(&cb, 0, sizeof(cb));
140     cb.event_connect = event_connect;
141     cb.event_numeric = event_numeric;
142     cb.event_privmsg = event_privmsg;
143     cb.event_channel = event_channel;
144     cb.event_nick    = event_nick;
145     cb.event_quit    = event_quit;
146     cb.event_join    = event_join;
147     cb.event_part    = event_part;
148     cb.event_mode    = event_mode;
149     cb.event_umode   = event_umode;
150     cb.event_topic   = event_topic;
151     cb.event_kick    = event_kick;
152     cb.event_notice  = event_notice;
153     cb.event_invite  = event_invite;
154
155     Cvar_SetQuick(&irc_current_nick, irc_nick.string);
156
157     irc_session_global = irc_create_session(&cb);
158     irc_option_set(irc_session_global, LIBIRC_OPTION_STRIPNICKS);
159     if(irc_connect(
160         irc_session_global,
161         irc_server.string,
162         irc_port.integer,
163         NOTSET(irc_password)? NULL : irc_password.string,
164         irc_nick.string,
165         "dpirc", "DPIRC user"
166     ))
167     {   //Connection failed
168         Con_Printf("%s^1Connection failed: ^7%s\n",
169             irc_msgprefix.string,
170             irc_strerror(irc_errno(irc_session_global))
171         );
172         
173         CL_Irc_Disconnect_f();
174         return;
175     }
176     
177     pthread_create (&irc_thread, NULL, (void *) &IRC_Thread, NULL);
178 }
179
180 static void CL_Irc_Disconnect_f(void)
181 {
182     if(!irc_connected.integer)
183     {
184         Con_Printf("%sNot connected\n",
185             irc_msgprefix.string
186         );
187     }
188     
189     Con_Printf("^1Disconnected from the IRC server\n");
190     irc_cmd_quit(irc_session_global, "Disconnected");
191     irc_destroy_session(irc_session_global);
192     Cvar_SetQuick(&irc_connected, "0");
193 }
194
195 static void CL_Irc_Say_Universal_f(void)
196 {
197     int cmdlen, i, j, space;
198     const char *cmd, *dest;
199     char message[MAX_INPUTLINE];
200     qboolean watched;
201
202     if(Cmd_Argc() < 3) switch(irc_msgmode)
203     {
204         case MSGMODE_PRIVMSG:
205             Con_Printf("Usage: irc_say channel_or_nick message\n");
206             return;
207         case MSGMODE_NOTICE:
208             Con_Printf("Usage: irc_notice channel_or_nick message\n");
209             return;
210         case MSGMODE_ACTION:
211             Con_Printf("Usage: irc_me channel_or_nick message\n");
212             return;
213     }
214     
215     cmd = Cmd_Args();
216     cmdlen = strlen(cmd);
217     
218     space = 0;
219     for(i = 0, j = 0; i < cmdlen; ++i)
220     {
221         if(space)
222         {
223             message[j++] = cmd[i];
224         }
225         else if(cmd[i] == ' ') ++space;
226     }
227     
228     message[j] = '\0';
229     
230     dest = Cmd_Argv(1);
231     
232     switch(irc_msgmode)
233     {
234         case MSGMODE_PRIVMSG:
235             irc_cmd_msg(irc_session_global, dest, message);
236             watched = Irc_IsWatchedChannel(dest);
237             
238             if(ISCHANNEL(dest)) Con_Printf("%s%s^3%s^0|^7<^2%s^7> %s\n",
239                 watched? "\001" : CHATWINDOW,
240                 irc_msgprefix.string,
241                 dest,
242                 irc_current_nick.string,
243                 message
244             ); else Con_Printf("%s%s^1Privmsg to ^2%s^7: ^3%s\n",
245                 CHATWINDOW_URGENT,
246                 irc_msgprefix.string,
247                 dest,
248                 message
249             );
250             
251             break;
252         
253         case MSGMODE_NOTICE:
254             irc_cmd_notice(irc_session_global, dest, message);
255             
256             if(!ISCHANNEL(dest)) Con_Printf("%s%sNotice to ^2%s^7: ^9%s\n",
257                 CHATWINDOW,
258                 irc_msgprefix.string,
259                 dest,
260                 message
261             ); else Con_Printf("%s%s^3%s^0|^9-^2%s^9- %s\n",
262                 CHATWINDOW,
263                 irc_msgprefix.string,
264                 dest,
265                 irc_current_nick.string,
266                 message
267             );
268             
269             break;
270         
271         case MSGMODE_ACTION: //to-do
272             irc_cmd_me(irc_session_global, dest, message);
273             break;
274     }
275 }
276
277 static void CL_Irc_Raw_f(void)
278 {
279     if(Cmd_Argc() < 2)
280     {
281         Con_Printf("Usage: irc_raw command\n");
282         return;
283     }
284     
285     irc_send_raw(irc_session_global, "%s", Cmd_Args());
286 }
287
288 static void CL_Irc_Say_f(void)
289 {
290     MUSTCONNECT
291     irc_msgmode = MSGMODE_PRIVMSG;
292     CL_Irc_Say_Universal_f();
293 }
294
295 static void CL_Irc_Notice_f(void)
296 {
297     MUSTCONNECT
298     irc_msgmode = MSGMODE_NOTICE;
299     CL_Irc_Say_Universal_f();
300 }
301
302 static void CL_Irc_Me_f(void)
303 {
304     MUSTCONNECT
305     irc_msgmode = MSGMODE_ACTION;
306     CL_Irc_Say_Universal_f();
307 }
308
309 static void CL_Irc_Join_f(void)
310 {
311     int argc = Cmd_Argc();
312     const char *channel;
313     
314     MUSTCONNECT
315     
316     if(argc < 2)
317     {
318         Con_Printf("Usage: irc_join channel [key]\n");
319         return;
320     }
321     
322     channel = Cmd_Argv(1);
323     
324     if(argc > 2)
325         irc_cmd_join(irc_session_global, channel, Cmd_Argv(2));
326     else
327         irc_cmd_join(irc_session_global, channel, NULL);
328 }
329
330 static void CL_Irc_Part_f(void)
331 {
332     MUSTCONNECT
333     
334     if(Cmd_Argc() < 2)
335     {
336         Con_Printf("Usage: irc_part channel\n");
337         return;
338     }
339     
340     irc_cmd_part(irc_session_global, Cmd_Argv(1));
341 }
342
343 static void CL_Irc_Names_f(void)
344 {
345     MUSTCONNECT
346     
347     if(Cmd_Argc() < 2)
348     {
349         Con_Printf("Usage: irc_names channel\n");
350         return;
351     }
352     
353     irc_cmd_names(irc_session_global, Cmd_Argv(1));
354 }
355
356 void CL_Irc_MessageMode_f(void)
357 {
358     const char *tmp;
359     
360     key_dest = key_message;
361     chat_mode = 2;
362     chat_bufferlen = 0;
363     chat_buffer[0] = 0;
364     
365     if(irc_save_target.integer)
366     {
367         tmp = Irc_GetLastChannel();
368         strlcpy(chat_buffer, tmp, sizeof(chat_buffer));
369         chat_bufferlen = strlen(chat_buffer);
370     }
371 }
372
373 void CL_Irc_ChNick_f(void)
374 {
375     MUSTCONNECT
376     
377     if(Cmd_Argc() < 2)
378     {
379         Con_Printf("Usage: irc_chnick nick\n");
380         return;
381     }
382     
383     irc_cmd_nick(irc_session_global, Cmd_Argv(1));
384 }
385
386
387 void Irc_SetLastChannel(const char *value)
388 {
389     strlcpy(last_channel, value, sizeof(last_channel));
390 }
391
392 const char* Irc_GetLastChannel(void)
393 {
394     return last_channel;
395 }
396
397
398 void Irc_SendMessage(const char *message)
399 {
400     const char* dest = Irc_GetLastChannel();
401     qboolean watched = Irc_IsWatchedChannel(dest);
402     
403     MUSTCONNECT
404     
405     irc_cmd_msg(irc_session_global, dest, message);
406             
407     if(ISCHANNEL(dest)) Con_Printf("%s%s^3%s^0|^7<^2%s^7> %s\n",
408         watched? "\001" : CHATWINDOW,
409         irc_msgprefix.string,
410         dest,
411         irc_current_nick.string,
412         message
413     ); else Con_Printf("%s%s^1Privmsg to ^2%s^7: ^3%s\n",
414         CHATWINDOW_URGENT,
415         irc_msgprefix.string,
416         dest,
417         message
418     );
419 }
420
421 //
422 //  IRC events
423 //
424
425 IRCEVENT(event_connect)
426 {
427     Cvar_SetQuick(&irc_connected, "2");
428     Con_Printf("%sConnected to %s:%i\n",
429         irc_msgprefix.string, 
430         irc_server.string,
431         irc_port.integer
432     );
433 }
434
435 IRCNUMEVENT(event_numeric)
436 {
437     //Get our initial nick from the welcome message
438     if(event == 001)
439         Cvar_SetQuick(&irc_current_nick, params[0]);
440     
441     if (!irc_numeric_errorsonly.integer || event > 400)
442     {
443         Con_Printf("%s^3%d ^2%s ^1%s^7 %s %s %s\n",
444             irc_msgprefix.string,
445             event,
446             origin ? origin : "^8(unknown)",
447             params[0],
448             count > 1 ? params[1] : "",
449             count > 2 ? params[2] : "",
450             count > 3 ? params[3] : ""
451         );
452     }
453 }
454
455 IRCEVENT(event_privmsg)
456 {
457     char* msgstr = "";
458     
459     if(count > 1)
460         msgstr = irc_color_strip_from_mirc(params[1]);
461     
462     UPDATETARGET(origin)
463     
464     Con_Printf("%s%s^1Privmsg from ^2%s^7: ^3%s\n",
465         CHATWINDOW_URGENT,
466         irc_msgprefix.string,
467         origin,
468         msgstr
469     );
470     
471     if(count > 1) free(msgstr);
472 }
473
474
475 IRCEVENT(event_channel)
476 {
477     char* msgstr = "";
478     qboolean watched;
479     
480     //weird shit
481     if(!ISCHANNEL(params[0]))
482         return event_privmsg(session, event, origin, params, count);
483     
484     if(count > 1)
485         msgstr = irc_color_strip_from_mirc(params[1]);
486     
487     watched = Irc_IsWatchedChannel(params[0]);
488     
489     if(Irc_CheckHilight(msgstr))
490     {   
491         UPDATETARGET(params[0])
492         
493         Con_Printf("%s%s^3%s^0|^7<^2%s^7> ^1%s\n",
494             watched? "\001" : CHATWINDOW_URGENT,
495             irc_msgprefix.string,
496             params[0],
497             origin,
498             msgstr
499         );
500     }
501     else Con_Printf("%s%s^3%s^0|^7<^2%s^7> %s\n",
502         watched? "\001" : CHATWINDOW,
503         irc_msgprefix.string,
504         params[0],
505         origin,
506         msgstr
507     );
508     
509     if(count > 1) free(msgstr);
510 }
511
512 IRCEVENT(event_nick)
513 {
514     //printf("%sorigin -> %s (%s, %s)\n", origin, params[0], irc_current_nick.string, irc_nick.string);
515     //crash!
516     //cvar_t dumb;
517     //Cvar_SetQuick(&dumb, NULL);
518     
519     if(!strncmp(origin, irc_current_nick.string, 512)) //Our nick is changed
520     {
521         Cvar_SetQuick(&irc_current_nick, params[0]);
522         
523         Con_Printf("%s%s^7Your nick is now ^2%s\n",
524             CHATWINDOW,
525             irc_msgprefix.string,
526             params[0]
527         );
528         
529         return;
530     }
531     
532     Con_Printf("%s%s^2%s^7 is now known as ^2%s\n",
533         CHATWINDOW,
534         irc_msgprefix.string,
535         origin,
536         params[0]
537     );
538 }
539
540 IRCEVENT(event_quit)
541 {
542     Con_Printf("%s%s^2%s^7 has quit IRC: ^2%s\n",
543         CHATWINDOW,
544         irc_msgprefix.string,
545         origin,
546         count > 1? params[0] : "^8(quit message missing)"
547     );
548 }
549
550 IRCEVENT(event_join)
551 {
552     Con_Printf("%s%s^2%s^7 has joined ^3%s\n",
553         CHATWINDOW,
554         irc_msgprefix.string,
555         origin,
556         params[0]
557     );
558 }
559
560 IRCEVENT(event_part)
561 {
562     Con_Printf("%s%s^2%s^7 has left ^3%s^7: %s\n",
563         CHATWINDOW,
564         irc_msgprefix.string,
565         origin,
566         params[0],
567         count > 1? params[1] : "^8(part message missing)"
568     );
569 }
570
571 IRCEVENT(event_mode)
572 {
573     char paramstring[MAX_INPUTLINE] = "";
574     unsigned int i;
575     
576     for(i = 2; i < count; i++)
577     {
578         strlcat(paramstring, params[i], sizeof(paramstring));
579         strlcat(paramstring, " ", sizeof(paramstring));
580     }
581     
582     Con_Printf("%s%s^2%s^7 set mode on ^3%s^7: %s %s\n",
583         CHATWINDOW,
584         irc_msgprefix.string,
585         origin,
586         params[0],
587         params[1],
588         paramstring
589     );
590 }
591
592 IRCEVENT(event_umode)
593 {
594     Con_Printf("%sUsermode changed for ^2%s^7: %s\n",
595         irc_msgprefix.string,
596         origin,
597         params[0]
598     );
599 }
600
601 IRCEVENT(event_topic)
602 {
603     if(count > 1) Con_Printf("%s%s^2%s^7 changed the topic of ^3%s^7: %s\n",
604         CHATWINDOW,
605         irc_msgprefix.string,
606         origin,
607         params[0],
608         params[1]
609     );
610 }
611
612 IRCEVENT(event_kick)
613 {
614     Con_Printf("%s%s^2%s^7 has kicked ^2%s^7 out of ^3%s^7: %s\n",
615         CHATWINDOW,
616         irc_msgprefix.string,
617         origin,
618         count > 1? params[1] : "^8(nick missing)", //libircclient documentation says params[1] is optinal.
619         params[0],
620         count > 2? params[2] : "^8(random kick victim! yay!)"
621     );
622 }
623
624 IRCEVENT(event_notice)
625 {
626     char* msgstr = "";
627     
628     if(count > 1)
629         msgstr = irc_color_strip_from_mirc(params[1]);
630     
631     if(!ISCHANNEL(params[0])) Con_Printf("%s%sNotice from ^2%s^7: ^9%s\n",
632         CHATWINDOW,
633         irc_msgprefix.string,
634         origin,
635         msgstr
636     ); else Con_Printf("%s%s^3%s^0|^9-^2%s^9- %s\n",
637         CHATWINDOW,
638         irc_msgprefix.string,
639         params[0],
640         origin,
641         msgstr
642     );
643     
644     if(count > 1) free(msgstr);
645 }
646
647 IRCEVENT(event_invite)
648 {
649     Con_Printf("%s%s^2%s^7 invites you to ^3%s\n",
650         CHATWINDOW,
651         irc_msgprefix.string,
652         origin,
653         params[1]
654     );
655 }
656
657 //
658 //  Functions that checks if a message contains hilights
659 //
660
661 qboolean Irc_CheckHilight(const char *msg)
662 {
663     int start, idx, len;
664     char buffer[512];
665     
666     if(strcasestr(msg, irc_current_nick.string))
667         return TRUE; //Contains our nick
668     
669     len = strlen(irc_hilights.string);
670     start = 0;
671     
672     for(idx = 0; idx < len; ++idx)
673     {
674         if(irc_hilights.string[idx] == ' ')
675         {
676             strlcpy(buffer, irc_hilights.string+start, idx+1);
677             if(strcasestr(msg, buffer))
678                 return TRUE; //Contains a word from hilight list
679                 
680             start = idx+1;
681         }
682     }
683     
684     //Catch the final word
685     strlcpy(buffer, irc_hilights.string+start, idx+1);
686     if(strcasestr(msg, buffer))
687         return TRUE; //Contains a word from hilight list
688     
689     return FALSE;
690 }
691
692 //
693 //  Checks if channel is watched
694 //
695
696 qboolean Irc_IsWatchedChannel(const char* chan)
697 {
698     int start, idx, len;
699     char buffer[512];
700     
701     if(NOTSET(irc_watched_channels))
702         return FALSE;
703     
704     len = strlen(irc_watched_channels.string);
705     start = 0;
706     
707     for(idx = 0; idx < len; ++idx)
708     {
709         if(irc_watched_channels.string[idx] == ' ')
710         {
711             strlcpy(buffer, irc_watched_channels.string+start, idx+1);
712             if(strcasestr(chan, buffer))
713                 return TRUE;
714                 
715             start = idx+1;
716         }
717     }
718     
719     //Catch the final channel
720     strlcpy(buffer, irc_watched_channels.string+start, idx+1);
721     if(strcasestr(chan, buffer))
722         return TRUE;
723     
724     return FALSE;
725 }
726
727 //
728 //  Functions that should have been in libircclient
729 //
730
731 int irc_cmd_nick(irc_session_t * session, const char *nick)
732 {
733     if(!nick)
734     {
735         //session->lasterror = LIBIRC_ERR_STATE; Won't compile
736         return 1;
737     }
738
739     return irc_send_raw(session, "NICK %s", nick);
740 }