Now inbound_cap_ls() can enable extensions when a bouncer uses a namespace for the...
[rofl0r-ixchat.git] / src / common / text.c
blobc80a513e5f1348ff98bc22b3e11edf8a84f30450
1 /* X-Chat
2 * Copyright (C) 1998 Peter Zelezny.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <ctype.h>
24 #include <time.h>
25 #include <sys/types.h>
26 #include <fcntl.h>
27 #include <sys/stat.h>
28 #include <sys/mman.h>
30 #include "xchat.h"
31 #include <glib.h>
32 #include "cfgfiles.h"
33 #include "chanopt.h"
34 #include "plugin.h"
35 #include "fe.h"
36 #include "server.h"
37 #include "util.h"
38 #include "outbound.h"
39 #include "xchatc.h"
40 #include "text.h"
42 struct pevt_stage1
44 int len;
45 char *data;
46 struct pevt_stage1 *next;
50 static void mkdir_p (char *dir);
51 static char *log_create_filename (char *channame);
54 static char *
55 scrollback_get_filename (session *sess, char *buf, int max)
57 char *net, *chan;
59 net = server_get_network (sess->server, FALSE);
60 if (!net)
61 return NULL;
63 snprintf (buf, max, "%s/scrollback/%s/%s.txt", get_xdir_fs (), net, "");
64 mkdir_p (buf);
66 chan = log_create_filename (sess->channel);
67 snprintf (buf, max, "%s/scrollback/%s/%s.txt", get_xdir_fs (), net, chan);
68 free (chan);
70 return buf;
73 #if 0
75 static void
76 scrollback_unlock (session *sess)
78 char buf[1024];
80 if (scrollback_get_filename (sess, buf, sizeof (buf) - 6) == NULL)
81 return;
83 strcat (buf, ".lock");
84 unlink (buf);
87 static gboolean
88 scrollback_lock (session *sess)
90 char buf[1024];
91 int fh;
93 if (scrollback_get_filename (sess, buf, sizeof (buf) - 6) == NULL)
94 return FALSE;
96 strcat (buf, ".lock");
98 if (access (buf, F_OK) == 0)
99 return FALSE; /* can't get lock */
101 fh = open (buf, O_CREAT | O_TRUNC | O_APPEND | O_WRONLY, 0644);
102 if (fh == -1)
103 return FALSE;
105 return TRUE;
108 #endif
110 void
111 scrollback_close (session *sess)
113 if (sess->scrollfd != -1)
115 close (sess->scrollfd);
116 sess->scrollfd = -1;
120 static char *
121 file_to_buffer (char *file, int *len)
123 int fh;
124 char *buf;
125 struct stat st;
127 fh = open (file, O_RDONLY | OFLAGS);
128 if (fh == -1)
129 return NULL;
131 fstat (fh, &st);
133 buf = malloc (st.st_size);
134 if (!buf)
136 close (fh);
137 return NULL;
140 if (read (fh, buf, st.st_size) != st.st_size)
142 free (buf);
143 close (fh);
144 return NULL;
147 *len = st.st_size;
148 close (fh);
149 return buf;
152 /* shrink the file to roughly prefs.max_lines */
154 static void
155 scrollback_shrink (session *sess)
157 char file[1024];
158 char *buf;
159 int fh;
160 int lines;
161 int line;
162 int len;
163 char *p;
165 scrollback_close (sess);
166 sess->scrollwritten = 0;
167 lines = 0;
169 if (scrollback_get_filename (sess, file, sizeof (file)) == NULL)
170 return;
172 buf = file_to_buffer (file, &len);
173 if (!buf)
174 return;
176 /* count all lines */
177 p = buf;
178 while (p != buf + len)
180 if (*p == '\n')
181 lines++;
182 p++;
185 fh = open (file, O_CREAT | O_TRUNC | O_APPEND | O_WRONLY, 0644);
186 if (fh == -1)
188 free (buf);
189 return;
192 line = 0;
193 p = buf;
194 while (p != buf + len)
196 if (*p == '\n')
198 line++;
199 if (line >= lines - prefs.max_lines &&
200 p + 1 != buf + len)
202 p++;
203 write (fh, p, len - (p - buf));
204 break;
207 p++;
210 close (fh);
211 free (buf);
214 static void
215 scrollback_save (session *sess, char *text)
217 char buf[512 * 4];
218 time_t stamp;
219 int len;
221 if (sess->type == SESS_SERVER)
222 return;
224 if (sess->text_scrollback == SET_DEFAULT)
226 if (!prefs.text_replay)
227 return;
229 else
231 if (sess->text_scrollback != SET_ON)
232 return;
235 if (sess->scrollfd == -1)
237 if (scrollback_get_filename (sess, buf, sizeof (buf)) == NULL)
238 return;
240 sess->scrollfd = open (buf, O_CREAT | O_APPEND | O_WRONLY, 0644);
241 if (sess->scrollfd == -1)
242 return;
245 stamp = time (0);
246 if (sizeof (stamp) == 4) /* gcc will optimize one of these out */
247 write (sess->scrollfd, buf, snprintf (buf, sizeof (buf), "T %d ", (int)stamp));
248 else
249 write (sess->scrollfd, buf, snprintf (buf, sizeof (buf), "T %"G_GINT64_FORMAT" ", (gint64)stamp));
251 len = strlen (text);
252 write (sess->scrollfd, text, len);
253 if (len && text[len - 1] != '\n')
254 write (sess->scrollfd, "\n", 1);
256 sess->scrollwritten++;
258 if ((sess->scrollwritten * 2 > prefs.max_lines && prefs.max_lines > 0) ||
259 sess->scrollwritten > 32000)
260 scrollback_shrink (sess);
263 void
264 scrollback_load (session *sess)
266 int fh;
267 char buf[512 * 4];
268 char *text;
269 time_t stamp;
270 int lines;
271 char *map, *end_map;
272 struct stat statbuf;
273 const char *begin, *eol;
275 if (sess->text_scrollback == SET_DEFAULT)
277 if (!prefs.text_replay)
278 return;
280 else
282 if (sess->text_scrollback != SET_ON)
283 return;
286 if (scrollback_get_filename (sess, buf, sizeof (buf)) == NULL)
287 return;
289 fh = open (buf, O_RDONLY | OFLAGS);
290 if (fh == -1)
291 return;
293 if (fstat (fh, &statbuf) < 0)
294 return;
296 map = mmap (NULL, statbuf.st_size, PROT_READ, MAP_PRIVATE, fh, 0);
297 if (map == MAP_FAILED)
298 return;
300 end_map = map + statbuf.st_size;
302 lines = 0;
303 begin = map;
304 while (begin < end_map)
306 int n_bytes;
308 eol = memchr (begin, '\n', end_map - begin);
310 if (!eol)
311 eol = end_map;
313 n_bytes = MIN (eol - begin, sizeof (buf) - 1);
315 strncpy (buf, begin, n_bytes);
317 buf[n_bytes] = 0;
319 if (buf[0] == 'T')
321 if (sizeof (time_t) == 4)
322 stamp = strtoul (buf + 2, NULL, 10);
323 else
324 stamp = strtoull (buf + 2, NULL, 10); /* just incase time_t is 64 bits */
325 text = strchr (buf + 3, ' ');
326 if (text)
328 text = strip_color (text + 1, -1, STRIP_COLOR);
329 fe_print_text (sess, text, stamp);
330 g_free (text);
332 lines++;
335 begin = eol + 1;
338 sess->scrollwritten = lines;
340 if (lines)
342 text = ctime (&stamp);
343 text[24] = 0; /* get rid of the \n */
344 snprintf (buf, sizeof (buf), "\n*\t%s %s\n\n", _("Loaded log from"), text);
345 fe_print_text (sess, buf, 0);
346 /*EMIT_SIGNAL (XP_TE_GENMSG, sess, "*", buf, NULL, NULL, NULL, 0);*/
349 munmap (map, statbuf.st_size);
350 close (fh);
353 void
354 log_close (session *sess)
356 char obuf[512];
357 time_t currenttime;
359 if (sess->logfd != -1)
361 currenttime = time (NULL);
362 write (sess->logfd, obuf,
363 snprintf (obuf, sizeof (obuf) - 1, _("**** ENDING LOGGING AT %s\n"),
364 ctime (&currenttime)));
365 close (sess->logfd);
366 sess->logfd = -1;
370 static void
371 mkdir_p (char *dir) /* like "mkdir -p" from a shell, FS encoding */
373 char *start = dir;
375 /* the whole thing already exists? */
376 if (access (dir, F_OK) == 0)
377 return;
379 while (*dir)
381 if (dir != start && *dir == '/')
383 *dir = 0;
384 mkdir (start, S_IRUSR | S_IWUSR | S_IXUSR);
385 *dir = '/';
387 dir++;
391 static char *
392 log_create_filename (char *channame)
394 char *tmp, *ret;
395 int mbl;
397 ret = tmp = strdup (channame);
398 while (*tmp)
400 mbl = g_utf8_skip[((unsigned char *)tmp)[0]];
401 if (mbl == 1)
403 *tmp = rfc_tolower (*tmp);
404 if (*tmp == '/')
405 *tmp = '_';
407 tmp += mbl;
410 return ret;
413 /* like strcpy, but % turns into %% */
415 static char *
416 log_escape_strcpy (char *dest, char *src, char *end)
418 while (*src)
420 *dest = *src;
421 if (dest + 1 == end)
422 break;
423 dest++;
424 src++;
426 if (*src == '%')
428 if (dest + 1 == end)
429 break;
430 dest[0] = '%';
431 dest++;
435 dest[0] = 0;
436 return dest - 1;
439 /* substitutes %c %n %s into buffer */
441 static void
442 log_insert_vars (char *buf, int bufsize, char *fmt, char *c, char *n, char *s)
444 char *end = buf + bufsize;
446 while (1)
448 switch (fmt[0])
450 case 0:
451 buf[0] = 0;
452 return;
454 case '%':
455 fmt++;
456 switch (fmt[0])
458 case 'c':
459 buf = log_escape_strcpy (buf, c, end);
460 break;
461 case 'n':
462 buf = log_escape_strcpy (buf, n, end);
463 break;
464 case 's':
465 buf = log_escape_strcpy (buf, s, end);
466 break;
467 default:
468 buf[0] = '%';
469 buf++;
470 buf[0] = fmt[0];
471 break;
473 break;
475 default:
476 buf[0] = fmt[0];
478 fmt++;
479 buf++;
480 /* doesn't fit? */
481 if (buf == end)
483 buf[-1] = 0;
484 return;
489 static char *
490 log_create_pathname (char *servname, char *channame, char *netname)
492 char fname[384];
493 char fnametime[384];
494 char *fs;
495 struct tm *tm;
496 time_t now;
498 if (!netname)
499 netname = "NETWORK";
501 /* first, everything is in UTF-8 */
502 if (!rfc_casecmp (channame, servname))
503 channame = strdup ("server");
504 else
505 channame = log_create_filename (channame);
506 log_insert_vars (fname, sizeof (fname), prefs.logmask, channame, netname, servname);
507 free (channame);
509 /* insert time/date */
510 now = time (NULL);
511 tm = localtime (&now);
512 strftime (fnametime, sizeof (fnametime), fname, tm);
514 /* create final path/filename */
515 if (fnametime[0] == '/') /* is it fullpath already? */
516 snprintf (fname, sizeof (fname), "%s", fnametime);
517 else
518 snprintf (fname, sizeof (fname), "%s/xchatlogs/%s", get_xdir_utf8 (), fnametime);
520 /* now we need it in FileSystem encoding */
521 fs = xchat_filename_from_utf8 (fname, -1, 0, 0, 0);
523 /* create all the subdirectories */
524 if (fs)
525 mkdir_p (fs);
527 return fs;
530 static int
531 log_open_file (char *servname, char *channame, char *netname)
533 char buf[512];
534 int fd;
535 char *file;
536 time_t currenttime;
538 file = log_create_pathname (servname, channame, netname);
539 if (!file)
540 return -1;
542 fd = open (file, O_CREAT | O_APPEND | O_WRONLY, 0644);
543 g_free (file);
545 if (fd == -1)
546 return -1;
547 currenttime = time (NULL);
548 write (fd, buf,
549 snprintf (buf, sizeof (buf), _("**** BEGIN LOGGING AT %s\n"),
550 ctime (&currenttime)));
552 return fd;
555 static void
556 log_open (session *sess)
558 static gboolean log_error = FALSE;
560 log_close (sess);
561 sess->logfd = log_open_file (sess->server->servername, sess->channel,
562 server_get_network (sess->server, FALSE));
564 if (!log_error && sess->logfd == -1)
566 char message[512];
567 snprintf (message, sizeof (message),
568 _("* Can't open log file(s) for writing. Check the\n" \
569 " permissions on %s/xchatlogs"), get_xdir_utf8 ());
570 fe_message (message, FE_MSG_WAIT | FE_MSG_ERROR);
572 log_error = TRUE;
576 void
577 log_open_or_close (session *sess)
579 if (sess->text_logging == SET_DEFAULT)
581 if (prefs.logging)
582 log_open (sess);
583 else
584 log_close (sess);
586 else
588 if (sess->text_logging)
589 log_open (sess);
590 else
591 log_close (sess);
596 get_stamp_str (char *fmt, time_t tim, char **ret)
598 char *loc = NULL;
599 char dest[128];
600 gsize len;
602 /* strftime wants the format string in LOCALE! */
603 if (!prefs.utf8_locale)
605 const gchar *charset;
607 g_get_charset (&charset);
608 loc = g_convert_with_fallback (fmt, -1, charset, "UTF-8", "?", 0, 0, 0);
609 if (loc)
610 fmt = loc;
613 len = strftime (dest, sizeof (dest), fmt, localtime (&tim));
614 if (len)
616 if (prefs.utf8_locale)
617 *ret = g_strdup (dest);
618 else
619 *ret = g_locale_to_utf8 (dest, len, 0, &len, 0);
622 if (loc)
623 g_free (loc);
625 return len;
628 static void
629 log_write (session *sess, char *text)
631 char *temp;
632 char *stamp;
633 char *file;
634 int len;
636 if (sess->text_logging == SET_DEFAULT)
638 if (!prefs.logging)
639 return;
641 else
643 if (sess->text_logging != SET_ON)
644 return;
647 if (sess->logfd == -1)
648 log_open (sess);
650 /* change to a different log file? */
651 file = log_create_pathname (sess->server->servername, sess->channel,
652 server_get_network (sess->server, FALSE));
653 if (file)
655 if (access (file, F_OK) != 0)
657 close (sess->logfd);
658 sess->logfd = log_open_file (sess->server->servername, sess->channel,
659 server_get_network (sess->server, FALSE));
661 g_free (file);
664 if (prefs.timestamp_logs)
666 len = get_stamp_str (prefs.timestamp_log_format, time (0), &stamp);
667 if (len)
669 write (sess->logfd, stamp, len);
670 g_free (stamp);
673 temp = strip_color (text, -1, STRIP_ALL);
674 len = strlen (temp);
675 write (sess->logfd, temp, len);
676 /* lots of scripts/plugins print without a \n at the end */
677 if (temp[len - 1] != '\n')
678 write (sess->logfd, "\n", 1); /* emulate what xtext would display */
679 g_free (temp);
682 /* converts a CP1252/ISO-8859-1(5) hybrid to UTF-8 */
683 /* Features: 1. It never fails, all 00-FF chars are converted to valid UTF-8 */
684 /* 2. Uses CP1252 in the range 80-9f because ISO doesn't have any- */
685 /* thing useful in this range and it helps us receive from mIRC */
686 /* 3. The five undefined chars in CP1252 80-9f are replaced with */
687 /* ISO-8859-15 control codes. */
688 /* 4. Handles 0xa4 as a Euro symbol ala ISO-8859-15. */
689 /* 5. Uses ISO-8859-1 (which matches CP1252) for everything else. */
690 /* 6. This routine measured 3x faster than g_convert :) */
692 static unsigned char *
693 iso_8859_1_to_utf8 (unsigned char *text, int len, gsize *bytes_written)
695 unsigned int idx;
696 unsigned char *res, *output;
697 static const unsigned short lowtable[] = /* 74 byte table for 80-a4 */
699 /* compressed utf-8 table: if the first byte's 0x20 bit is set, it
700 indicates a 2-byte utf-8 sequence, otherwise prepend a 0xe2. */
701 0x82ac, /* 80 Euro. CP1252 from here on... */
702 0xe281, /* 81 NA */
703 0x809a, /* 82 */
704 0xe692, /* 83 */
705 0x809e, /* 84 */
706 0x80a6, /* 85 */
707 0x80a0, /* 86 */
708 0x80a1, /* 87 */
709 0xeb86, /* 88 */
710 0x80b0, /* 89 */
711 0xe5a0, /* 8a */
712 0x80b9, /* 8b */
713 0xe592, /* 8c */
714 0xe28d, /* 8d NA */
715 0xe5bd, /* 8e */
716 0xe28f, /* 8f NA */
717 0xe290, /* 90 NA */
718 0x8098, /* 91 */
719 0x8099, /* 92 */
720 0x809c, /* 93 */
721 0x809d, /* 94 */
722 0x80a2, /* 95 */
723 0x8093, /* 96 */
724 0x8094, /* 97 */
725 0xeb9c, /* 98 */
726 0x84a2, /* 99 */
727 0xe5a1, /* 9a */
728 0x80ba, /* 9b */
729 0xe593, /* 9c */
730 0xe29d, /* 9d NA */
731 0xe5be, /* 9e */
732 0xe5b8, /* 9f */
733 0xe2a0, /* a0 */
734 0xe2a1, /* a1 */
735 0xe2a2, /* a2 */
736 0xe2a3, /* a3 */
737 0x82ac /* a4 ISO-8859-15 Euro. */
740 if (len == -1)
741 len = strlen (text);
743 /* worst case scenario: every byte turns into 3 bytes */
744 res = output = g_malloc ((len * 3) + 1);
745 if (!output)
746 return NULL;
748 while (len)
750 if (G_LIKELY (*text < 0x80))
752 *output = *text; /* ascii maps directly */
754 else if (*text <= 0xa4) /* 80-a4 use a lookup table */
756 idx = *text - 0x80;
757 if (lowtable[idx] & 0x2000)
759 *output++ = (lowtable[idx] >> 8) & 0xdf; /* 2 byte utf-8 */
760 *output = lowtable[idx] & 0xff;
762 else
764 *output++ = 0xe2; /* 3 byte utf-8 */
765 *output++ = (lowtable[idx] >> 8) & 0xff;
766 *output = lowtable[idx] & 0xff;
769 else if (*text < 0xc0)
771 *output++ = 0xc2;
772 *output = *text;
774 else
776 *output++ = 0xc3;
777 *output = *text - 0x40;
779 output++;
780 text++;
781 len--;
783 *output = 0; /* terminate */
784 *bytes_written = output - res;
786 return res;
789 char *
790 text_validate (char **text, int *len)
792 char *utf;
793 gsize utf_len;
795 /* valid utf8? */
796 if (g_utf8_validate (*text, *len, 0))
797 return NULL;
799 if (prefs.utf8_locale)
800 /* fallback to iso-8859-1 */
801 utf = iso_8859_1_to_utf8 (*text, *len, &utf_len);
802 else
804 /* fallback to locale */
805 utf = g_locale_to_utf8 (*text, *len, 0, &utf_len, NULL);
806 if (!utf)
807 utf = iso_8859_1_to_utf8 (*text, *len, &utf_len);
810 if (!utf)
812 *text = g_strdup ("%INVALID%");
813 *len = 9;
814 } else
816 *text = utf;
817 *len = utf_len;
820 return utf;
823 void
824 PrintText (session *sess, char *text)
826 char *conv;
828 if (!sess)
830 if (!sess_list)
831 return;
832 sess = (session *) sess_list->data;
835 /* make sure it's valid utf8 */
836 if (text[0] == 0)
838 text = "\n";
839 conv = NULL;
840 } else
842 int len = -1;
843 conv = text_validate ((char **)&text, &len);
846 log_write (sess, text);
847 scrollback_save (sess, text);
848 fe_print_text (sess, text, 0);
850 if (conv)
851 g_free (conv);
854 void
855 PrintTextf (session *sess, char *format, ...)
857 va_list args;
858 char *buf;
860 va_start (args, format);
861 buf = g_strdup_vprintf (format, args);
862 va_end (args);
864 PrintText (sess, buf);
865 g_free (buf);
868 /* Print Events stuff here --AGL */
870 /* Consider the following a NOTES file:
872 The main upshot of this is:
873 * Plugins and Perl scripts (when I get round to signaling perl.c) can intercept text events and do what they like
874 * The default text engine can be config'ed
876 By default it should appear *exactly* the same (I'm working hard not to change the default style) but if you go into Settings->Edit Event Texts you can change the text's. The format is thus:
878 The normal %Cx (color) and %B (bold) etc work
880 $x is replaced with the data in var x (e.g. $1 is often the nick)
882 $axxx is replace with a single byte of value xxx (in base 10)
884 AGL (990507)
887 /* These lists are thus:
888 pntevts_text[] are the strings the user sees (WITH %x etc)
889 pntevts[] are the data strings with \000 etc
892 /* To add a new event:
894 Think up a name (like "Join")
895 Make up a pevt_name_help struct
896 Add an entry to textevents.in
897 Type: make textevents
900 /* Internals:
902 On startup ~/.xchat/printevents.conf is loaded if it doesn't exist the
903 defaults are loaded. Any missing events are filled from defaults.
904 Each event is parsed by pevt_build_string and a binary output is produced
905 which looks like:
907 (byte) value: 0 = {
908 (int) numbers of bytes
909 (char []) that number of byte to be memcpy'ed into the buffer
912 (byte) number of varable to insert
913 2 = end of buffer
915 Each XP_TE_* signal is hard coded to call text_emit which calls
916 display_event which decodes the data
918 This means that this system *should be faster* than snprintf because
919 it always 'knows' that format of the string (basically is preparses much
920 of the work)
922 --AGL
925 char *pntevts_text[NUM_XP];
926 char *pntevts[NUM_XP];
928 #define pevt_generic_none_help NULL
930 static char * const pevt_genmsg_help[] = {
931 N_("Left message"),
932 N_("Right message"),
935 static char * const pevt_join_help[] = {
936 N_("The nick of the joining person"),
937 N_("The channel being joined"),
938 N_("The host of the person"),
941 static char * const pevt_chanaction_help[] = {
942 N_("Nickname"),
943 N_("The action"),
944 N_("Mode char"),
945 N_("Identified text"),
948 static char * const pevt_chanmsg_help[] = {
949 N_("Nickname"),
950 N_("The text"),
951 N_("Mode char"),
952 N_("Identified text"),
955 static char * const pevt_privmsg_help[] = {
956 N_("Nickname"),
957 N_("The message"),
958 N_("Identified text")
961 static char * const pevt_capack_help[] = {
962 N_("Server Name"),
963 N_("Acknowledged Capabilities")
966 static char * const pevt_caplist_help[] = {
967 N_("Server Name"),
968 N_("Server Capabilities")
971 static char * const pevt_capreq_help[] = {
972 N_("Requested Capabilities")
975 static char * const pevt_changenick_help[] = {
976 N_("Old nickname"),
977 N_("New nickname"),
980 static char * const pevt_newtopic_help[] = {
981 N_("Nick of person who changed the topic"),
982 N_("Topic"),
983 N_("Channel"),
986 static char * const pevt_topic_help[] = {
987 N_("Channel"),
988 N_("Topic"),
991 static char * const pevt_kick_help[] = {
992 N_("The nickname of the kicker"),
993 N_("The person being kicked"),
994 N_("The channel"),
995 N_("The reason"),
998 static char * const pevt_part_help[] = {
999 N_("The nick of the person leaving"),
1000 N_("The host of the person"),
1001 N_("The channel"),
1004 static char * const pevt_chandate_help[] = {
1005 N_("The channel"),
1006 N_("The time"),
1009 static char * const pevt_topicdate_help[] = {
1010 N_("The channel"),
1011 N_("The creator"),
1012 N_("The time"),
1015 static char * const pevt_quit_help[] = {
1016 N_("Nick"),
1017 N_("Reason"),
1018 N_("Host"),
1021 static char * const pevt_pingrep_help[] = {
1022 N_("Who it's from"),
1023 N_("The time in x.x format (see below)"),
1026 static char * const pevt_notice_help[] = {
1027 N_("Who it's from"),
1028 N_("The message"),
1031 static char * const pevt_channotice_help[] = {
1032 N_("Who it's from"),
1033 N_("The Channel it's going to"),
1034 N_("The message"),
1037 static char * const pevt_uchangenick_help[] = {
1038 N_("Old nickname"),
1039 N_("New nickname"),
1042 static char * const pevt_ukick_help[] = {
1043 N_("The person being kicked"),
1044 N_("The channel"),
1045 N_("The nickname of the kicker"),
1046 N_("The reason"),
1049 static char * const pevt_partreason_help[] = {
1050 N_("The nick of the person leaving"),
1051 N_("The host of the person"),
1052 N_("The channel"),
1053 N_("The reason"),
1056 static char * const pevt_ctcpsnd_help[] = {
1057 N_("The sound"),
1058 N_("The nick of the person"),
1059 N_("The channel"),
1062 static char * const pevt_ctcpgen_help[] = {
1063 N_("The CTCP event"),
1064 N_("The nick of the person"),
1067 static char * const pevt_ctcpgenc_help[] = {
1068 N_("The CTCP event"),
1069 N_("The nick of the person"),
1070 N_("The Channel it's going to"),
1073 static char * const pevt_chansetkey_help[] = {
1074 N_("The nick of the person who set the key"),
1075 N_("The key"),
1078 static char * const pevt_chansetlimit_help[] = {
1079 N_("The nick of the person who set the limit"),
1080 N_("The limit"),
1083 static char * const pevt_chanop_help[] = {
1084 N_("The nick of the person who did the op'ing"),
1085 N_("The nick of the person who has been op'ed"),
1088 static char * const pevt_chanhop_help[] = {
1089 N_("The nick of the person who has been halfop'ed"),
1090 N_("The nick of the person who did the halfop'ing"),
1093 static char * const pevt_chanvoice_help[] = {
1094 N_("The nick of the person who did the voice'ing"),
1095 N_("The nick of the person who has been voice'ed"),
1098 static char * const pevt_chanban_help[] = {
1099 N_("The nick of the person who did the banning"),
1100 N_("The ban mask"),
1103 static char * const pevt_chanrmkey_help[] = {
1104 N_("The nick who removed the key"),
1107 static char * const pevt_chanrmlimit_help[] = {
1108 N_("The nick who removed the limit"),
1111 static char * const pevt_chandeop_help[] = {
1112 N_("The nick of the person of did the deop'ing"),
1113 N_("The nick of the person who has been deop'ed"),
1115 static char * const pevt_chandehop_help[] = {
1116 N_("The nick of the person of did the dehalfop'ing"),
1117 N_("The nick of the person who has been dehalfop'ed"),
1120 static char * const pevt_chandevoice_help[] = {
1121 N_("The nick of the person of did the devoice'ing"),
1122 N_("The nick of the person who has been devoice'ed"),
1125 static char * const pevt_chanunban_help[] = {
1126 N_("The nick of the person of did the unban'ing"),
1127 N_("The ban mask"),
1130 static char * const pevt_chanexempt_help[] = {
1131 N_("The nick of the person who did the exempt"),
1132 N_("The exempt mask"),
1135 static char * const pevt_chanrmexempt_help[] = {
1136 N_("The nick of the person removed the exempt"),
1137 N_("The exempt mask"),
1140 static char * const pevt_chaninvite_help[] = {
1141 N_("The nick of the person who did the invite"),
1142 N_("The invite mask"),
1145 static char * const pevt_chanrminvite_help[] = {
1146 N_("The nick of the person removed the invite"),
1147 N_("The invite mask"),
1150 static char * const pevt_chanmodegen_help[] = {
1151 N_("The nick of the person setting the mode"),
1152 N_("The mode's sign (+/-)"),
1153 N_("The mode letter"),
1154 N_("The channel it's being set on"),
1157 static char * const pevt_whois1_help[] = {
1158 N_("Nickname"),
1159 N_("Username"),
1160 N_("Host"),
1161 N_("Full name"),
1164 static char * const pevt_whois2_help[] = {
1165 N_("Nickname"),
1166 N_("Channel Membership/\"is an IRC operator\""),
1169 static char * const pevt_whois3_help[] = {
1170 N_("Nickname"),
1171 N_("Server Information"),
1174 static char * const pevt_whois4_help[] = {
1175 N_("Nickname"),
1176 N_("Idle time"),
1179 static char * const pevt_whois4t_help[] = {
1180 N_("Nickname"),
1181 N_("Idle time"),
1182 N_("Signon time"),
1185 static char * const pevt_whois5_help[] = {
1186 N_("Nickname"),
1187 N_("Away reason"),
1190 static char * const pevt_whois6_help[] = {
1191 N_("Nickname"),
1194 static char * const pevt_whoisid_help[] = {
1195 N_("Nickname"),
1196 N_("Message"),
1197 "Numeric"
1200 static char * const pevt_whoisauth_help[] = {
1201 N_("Nickname"),
1202 N_("Message"),
1203 N_("Account"),
1206 static char * const pevt_whoisrealhost_help[] = {
1207 N_("Nickname"),
1208 N_("Real user@host"),
1209 N_("Real IP"),
1210 N_("Message"),
1213 static char * const pevt_generic_channel_help[] = {
1214 N_("Channel Name"),
1217 static char * const pevt_saslauth_help[] = {
1218 N_("Username")
1221 static char * const pevt_saslresponse_help[] = {
1222 N_("Server Name"),
1223 N_("Raw Numeric or Identifier"),
1224 N_("Username"),
1225 N_("Message")
1228 static char * const pevt_servertext_help[] = {
1229 N_("Text"),
1230 N_("Server Name"),
1231 N_("Raw Numeric or Identifier")
1234 static char * const pevt_sslmessage_help[] = {
1235 N_("Text"),
1236 N_("Server Name")
1239 static char * const pevt_invited_help[] = {
1240 N_("Channel Name"),
1241 N_("Nick of person who invited you"),
1242 N_("Server Name"),
1245 static char * const pevt_usersonchan_help[] = {
1246 N_("Channel Name"),
1247 N_("Users"),
1250 static char * const pevt_nickclash_help[] = {
1251 N_("Nickname in use"),
1252 N_("Nick being tried"),
1255 static char * const pevt_connfail_help[] = {
1256 N_("Error"),
1259 static char * const pevt_connect_help[] = {
1260 N_("Host"),
1261 N_("IP"),
1262 N_("Port"),
1265 static char * const pevt_sconnect_help[] = {
1266 "PID"
1269 static char * const pevt_generic_nick_help[] = {
1270 N_("Nickname"),
1271 N_("Server Name"),
1272 N_("Network")
1275 static char * const pevt_chanmodes_help[] = {
1276 N_("Channel Name"),
1277 N_("Modes string"),
1280 static char * const pevt_rawmodes_help[] = {
1281 N_("Nickname"),
1282 N_("Modes string"),
1285 static char * const pevt_kill_help[] = {
1286 N_("Nickname"),
1287 N_("Reason"),
1290 static char * const pevt_dccchaterr_help[] = {
1291 N_("Nickname"),
1292 N_("IP address"),
1293 N_("Port"),
1294 N_("Error"),
1297 static char * const pevt_dccstall_help[] = {
1298 N_("DCC Type"),
1299 N_("Filename"),
1300 N_("Nickname"),
1303 static char * const pevt_generic_file_help[] = {
1304 N_("Filename"),
1305 N_("Error"),
1308 static char * const pevt_dccrecverr_help[] = {
1309 N_("Filename"),
1310 N_("Destination filename"),
1311 N_("Nickname"),
1312 N_("Error"),
1315 static char * const pevt_dccrecvcomp_help[] = {
1316 N_("Filename"),
1317 N_("Destination filename"),
1318 N_("Nickname"),
1319 N_("CPS"),
1322 static char * const pevt_dccconfail_help[] = {
1323 N_("DCC Type"),
1324 N_("Nickname"),
1325 N_("Error"),
1328 static char * const pevt_dccchatcon_help[] = {
1329 N_("Nickname"),
1330 N_("IP address"),
1333 static char * const pevt_dcccon_help[] = {
1334 N_("Nickname"),
1335 N_("IP address"),
1336 N_("Filename"),
1339 static char * const pevt_dccsendfail_help[] = {
1340 N_("Filename"),
1341 N_("Nickname"),
1342 N_("Error"),
1345 static char * const pevt_dccsendcomp_help[] = {
1346 N_("Filename"),
1347 N_("Nickname"),
1348 N_("CPS"),
1351 static char * const pevt_dccoffer_help[] = {
1352 N_("Filename"),
1353 N_("Nickname"),
1354 N_("Pathname"),
1357 static char * const pevt_dccfileabort_help[] = {
1358 N_("Nickname"),
1359 N_("Filename")
1362 static char * const pevt_dccchatabort_help[] = {
1363 N_("Nickname"),
1366 static char * const pevt_dccresumeoffer_help[] = {
1367 N_("Nickname"),
1368 N_("Filename"),
1369 N_("Position"),
1372 static char * const pevt_dccsendoffer_help[] = {
1373 N_("Nickname"),
1374 N_("Filename"),
1375 N_("Size"),
1376 N_("IP address"),
1379 static char * const pevt_dccgenericoffer_help[] = {
1380 N_("DCC String"),
1381 N_("Nickname"),
1384 static char * const pevt_notifynumber_help[] = {
1385 N_("Number of notify items"),
1388 static char * const pevt_serverlookup_help[] = {
1389 N_("Server Name"),
1392 static char * const pevt_servererror_help[] = {
1393 N_("Text"),
1396 static char * const pevt_foundip_help[] = {
1397 N_("IP"),
1400 static char * const pevt_dccrename_help[] = {
1401 N_("Old Filename"),
1402 N_("New Filename"),
1405 static char * const pevt_ctcpsend_help[] = {
1406 N_("Receiver"),
1407 N_("Message"),
1410 static char * const pevt_ignoreaddremove_help[] = {
1411 N_("Hostmask"),
1414 static char * const pevt_resolvinguser_help[] = {
1415 N_("Nickname"),
1416 N_("Hostname"),
1419 static char * const pevt_malformed_help[] = {
1420 N_("Nickname"),
1421 N_("The Packet"),
1424 static char * const pevt_pingtimeout_help[] = {
1425 N_("Seconds"),
1428 static char * const pevt_uinvite_help[] = {
1429 N_("Nick of person who have been invited"),
1430 N_("Channel Name"),
1431 N_("Server Name"),
1434 static char * const pevt_banlist_help[] = {
1435 N_("Channel"),
1436 N_("Banmask"),
1437 N_("Who set the ban"),
1438 N_("Ban time"),
1441 static char * const pevt_discon_help[] = {
1442 N_("Error"),
1445 #include "textevents.h"
1447 static void
1448 pevent_load_defaults ()
1450 int i;
1452 for (i = 0; i < NUM_XP; i++)
1454 if (pntevts_text[i])
1455 free (pntevts_text[i]);
1457 /* make-te.c sets this 128 flag (DON'T call gettext() flag) */
1458 if (te[i].num_args & 128)
1459 pntevts_text[i] = strdup (te[i].def);
1460 else
1461 pntevts_text[i] = strdup (_(te[i].def));
1465 void
1466 pevent_make_pntevts ()
1468 int i, m;
1469 char out[1024];
1471 for (i = 0; i < NUM_XP; i++)
1473 if (pntevts[i] != NULL)
1474 free (pntevts[i]);
1475 if (pevt_build_string (pntevts_text[i], &(pntevts[i]), &m) != 0)
1477 snprintf (out, sizeof (out),
1478 _("Error parsing event %s.\nLoading default."), te[i].name);
1479 fe_message (out, FE_MSG_WARN);
1480 free (pntevts_text[i]);
1481 /* make-te.c sets this 128 flag (DON'T call gettext() flag) */
1482 if (te[i].num_args & 128)
1483 pntevts_text[i] = strdup (te[i].def);
1484 else
1485 pntevts_text[i] = strdup (_(te[i].def));
1486 if (pevt_build_string (pntevts_text[i], &(pntevts[i]), &m) != 0)
1488 fprintf (stderr,
1489 "XChat CRITICAL *** default event text failed to build!\n");
1490 abort ();
1496 /* Loading happens at 2 levels:
1497 1) File is read into blocks
1498 2) Pe block is parsed and loaded
1500 --AGL */
1502 /* Better hope you pass good args.. --AGL */
1504 static void
1505 pevent_trigger_load (int *i_penum, char **i_text, char **i_snd)
1507 int penum = *i_penum, len;
1508 char *text = *i_text, *snd = *i_snd;
1510 if (penum != -1 && text != NULL)
1512 len = strlen (text) + 1;
1513 if (pntevts_text[penum])
1514 free (pntevts_text[penum]);
1515 pntevts_text[penum] = malloc (len);
1516 memcpy (pntevts_text[penum], text, len);
1519 if (text)
1520 free (text);
1521 if (snd)
1522 free (snd);
1523 *i_text = NULL;
1524 *i_snd = NULL;
1525 *i_penum = 0;
1528 static int
1529 pevent_find (char *name, int *i_i)
1531 int i = *i_i, j;
1533 j = i + 1;
1534 while (1)
1536 if (j == NUM_XP)
1537 j = 0;
1538 if (strcmp (te[j].name, name) == 0)
1540 *i_i = j;
1541 return j;
1543 if (j == i)
1544 return -1;
1545 j++;
1550 pevent_load (char *filename)
1552 /* AGL, I've changed this file and pevent_save, could you please take a look at
1553 * the changes and possibly modify them to suit you
1554 * //David H
1556 char *buf, *ibuf;
1557 int fd, i = 0, pnt = 0;
1558 struct stat st;
1559 char *text = NULL, *snd = NULL;
1560 int penum = 0;
1561 char *ofs;
1563 if (filename == NULL)
1564 fd = xchat_open_file ("pevents.conf", O_RDONLY, 0, 0);
1565 else
1566 fd = xchat_open_file (filename, O_RDONLY, 0, XOF_FULLPATH);
1568 if (fd == -1)
1569 return 1;
1570 if (fstat (fd, &st) != 0)
1571 return 1;
1572 ibuf = malloc (st.st_size);
1573 read (fd, ibuf, st.st_size);
1574 close (fd);
1576 while (buf_get_line (ibuf, &buf, &pnt, st.st_size))
1578 if (buf[0] == '#')
1579 continue;
1580 if (strlen (buf) == 0)
1581 continue;
1583 ofs = strchr (buf, '=');
1584 if (!ofs)
1585 continue;
1586 *ofs = 0;
1587 ofs++;
1588 /*if (*ofs == 0)
1589 continue;*/
1591 if (strcmp (buf, "event_name") == 0)
1593 if (penum >= 0)
1594 pevent_trigger_load (&penum, &text, &snd);
1595 penum = pevent_find (ofs, &i);
1596 continue;
1597 } else if (strcmp (buf, "event_text") == 0)
1599 if (text)
1600 free (text);
1602 #if 0
1603 /* This allows updating of old strings. We don't use new defaults
1604 if the user has customized the strings (.e.g a text theme).
1605 Hash of the old default is enough to identify and replace it.
1606 This only works in English. */
1608 switch (g_str_hash (ofs))
1610 case 0x526743a4:
1611 /* %C08,02 Hostmask PRIV NOTI CHAN CTCP INVI UNIG %O */
1612 text = strdup (te[XP_TE_IGNOREHEADER].def);
1613 break;
1615 case 0xe91bc9c2:
1616 /* %C08,02 %O */
1617 text = strdup (te[XP_TE_IGNOREFOOTER].def);
1618 break;
1620 case 0x1fbfdf22:
1621 /* -%C10-%C11-%O$tDCC RECV: Cannot open $1 for writing - aborting. */
1622 text = strdup (te[XP_TE_DCCFILEERR].def);
1623 break;
1625 default:
1626 text = strdup (ofs);
1628 #else
1629 text = strdup (ofs);
1630 #endif
1632 continue;
1633 }/* else if (strcmp (buf, "event_sound") == 0)
1635 if (snd)
1636 free (snd);
1637 snd = strdup (ofs);
1638 continue;
1641 continue;
1644 pevent_trigger_load (&penum, &text, &snd);
1645 free (ibuf);
1646 return 0;
1649 static void
1650 pevent_check_all_loaded ()
1652 int i;
1654 for (i = 0; i < NUM_XP; i++)
1656 if (pntevts_text[i] == NULL)
1658 /*printf ("%s\n", te[i].name);
1659 snprintf(out, sizeof(out), "The data for event %s failed to load. Reverting to defaults.\nThis may be because a new version of XChat is loading an old config file.\n\nCheck all print event texts are correct", evtnames[i]);
1660 gtkutil_simpledialog(out); */
1661 /* make-te.c sets this 128 flag (DON'T call gettext() flag) */
1662 if (te[i].num_args & 128)
1663 pntevts_text[i] = strdup (te[i].def);
1664 else
1665 pntevts_text[i] = strdup (_(te[i].def));
1670 void
1671 load_text_events ()
1673 memset (&pntevts_text, 0, sizeof (char *) * (NUM_XP));
1674 memset (&pntevts, 0, sizeof (char *) * (NUM_XP));
1676 if (pevent_load (NULL))
1677 pevent_load_defaults ();
1678 pevent_check_all_loaded ();
1679 pevent_make_pntevts ();
1683 CL: format_event now handles filtering of arguments:
1684 1) if prefs.stripcolor is set, filter all style control codes from arguments
1685 2) always strip \010 (ATTR_HIDDEN) from arguments: it is only for use in the format string itself
1687 #define ARG_FLAG(argn) (1 << (argn))
1689 void
1690 format_event (session *sess, int index, char **args, char *o, int sizeofo, unsigned int stripcolor_args)
1692 int len, oi, ii, numargs;
1693 char *i, *ar, d, a, done_all = FALSE;
1695 i = pntevts[index];
1696 numargs = te[index].num_args & 0x7f;
1698 oi = ii = len = d = a = 0;
1699 o[0] = 0;
1701 if (i == NULL)
1702 return;
1704 while (done_all == FALSE)
1706 d = i[ii++];
1707 switch (d)
1709 case 0:
1710 memcpy (&len, &(i[ii]), sizeof (int));
1711 ii += sizeof (int);
1712 if (oi + len > sizeofo)
1714 printf ("Overflow in display_event (%s)\n", i);
1715 o[0] = 0;
1716 return;
1718 memcpy (&(o[oi]), &(i[ii]), len);
1719 oi += len;
1720 ii += len;
1721 break;
1722 case 1:
1723 a = i[ii++];
1724 if (a > numargs)
1726 fprintf (stderr,
1727 "XChat DEBUG: display_event: arg > numargs (%d %d %s)\n",
1728 a, numargs, i);
1729 break;
1731 ar = args[(int) a + 1];
1732 if (ar == NULL)
1734 printf ("arg[%d] is NULL in print event\n", a + 1);
1735 } else
1737 if (stripcolor_args & ARG_FLAG(a + 1)) len = strip_color2 (ar, -1, &o[oi], STRIP_ALL);
1738 else len = strip_hidden_attribute (ar, &o[oi]);
1739 oi += len;
1741 break;
1742 case 2:
1743 o[oi++] = '\n';
1744 o[oi++] = 0;
1745 done_all = TRUE;
1746 continue;
1747 case 3:
1748 /* if (sess->type == SESS_DIALOG)
1750 if (prefs.dialog_indent_nicks)
1751 o[oi++] = '\t';
1752 else
1753 o[oi++] = ' ';
1754 } else
1756 if (prefs.indent_nicks)
1757 o[oi++] = '\t';
1758 else
1759 o[oi++] = ' ';
1760 /*}*/
1761 break;
1764 o[oi] = 0;
1765 if (*o == '\n')
1766 o[0] = 0;
1769 static void
1770 display_event (session *sess, int event, char **args, unsigned int stripcolor_args)
1772 char o[4096];
1773 format_event (sess, event, args, o, sizeof (o), stripcolor_args);
1774 if (o[0])
1775 PrintText (sess, o);
1779 pevt_build_string (const char *input, char **output, int *max_arg)
1781 struct pevt_stage1 *s = NULL, *base = NULL, *last = NULL, *next;
1782 int clen;
1783 char o[4096], d, *obuf, *i;
1784 int oi, ii, max = -1, len, x;
1786 len = strlen (input);
1787 i = malloc (len + 1);
1788 memcpy (i, input, len + 1);
1789 check_special_chars (i, TRUE);
1791 len = strlen (i);
1793 clen = oi = ii = 0;
1795 for (;;)
1797 if (ii == len)
1798 break;
1799 d = i[ii++];
1800 if (d != '$')
1802 o[oi++] = d;
1803 continue;
1805 if (i[ii] == '$')
1807 o[oi++] = '$';
1808 continue;
1810 if (oi > 0)
1812 s = (struct pevt_stage1 *) malloc (sizeof (struct pevt_stage1));
1813 if (base == NULL)
1814 base = s;
1815 if (last != NULL)
1816 last->next = s;
1817 last = s;
1818 s->next = NULL;
1819 s->data = malloc (oi + sizeof (int) + 1);
1820 s->len = oi + sizeof (int) + 1;
1821 clen += oi + sizeof (int) + 1;
1822 s->data[0] = 0;
1823 memcpy (&(s->data[1]), &oi, sizeof (int));
1824 memcpy (&(s->data[1 + sizeof (int)]), o, oi);
1825 oi = 0;
1827 if (ii == len)
1829 fe_message ("String ends with a $", FE_MSG_WARN);
1830 return 1;
1832 d = i[ii++];
1833 if (d == 'a')
1834 { /* Hex value */
1835 x = 0;
1836 if (ii == len)
1837 goto a_len_error;
1838 d = i[ii++];
1839 d -= '0';
1840 x = d * 100;
1841 if (ii == len)
1842 goto a_len_error;
1843 d = i[ii++];
1844 d -= '0';
1845 x += d * 10;
1846 if (ii == len)
1847 goto a_len_error;
1848 d = i[ii++];
1849 d -= '0';
1850 x += d;
1851 if (x > 255)
1852 goto a_range_error;
1853 o[oi++] = x;
1854 continue;
1856 a_len_error:
1857 fe_message ("String ends in $a", FE_MSG_WARN);
1858 return 1;
1859 a_range_error:
1860 fe_message ("$a value is greater than 255", FE_MSG_WARN);
1861 return 1;
1863 if (d == 't')
1865 /* Tab - if tabnicks is set then write '\t' else ' ' */
1866 s = (struct pevt_stage1 *) malloc (sizeof (struct pevt_stage1));
1867 if (base == NULL)
1868 base = s;
1869 if (last != NULL)
1870 last->next = s;
1871 last = s;
1872 s->next = NULL;
1873 s->data = malloc (1);
1874 s->len = 1;
1875 clen += 1;
1876 s->data[0] = 3;
1878 continue;
1880 if (d < '1' || d > '9')
1882 snprintf (o, sizeof (o), "Error, invalid argument $%c\n", d);
1883 fe_message (o, FE_MSG_WARN);
1884 return 1;
1886 d -= '0';
1887 if (max < d)
1888 max = d;
1889 s = (struct pevt_stage1 *) malloc (sizeof (struct pevt_stage1));
1890 if (base == NULL)
1891 base = s;
1892 if (last != NULL)
1893 last->next = s;
1894 last = s;
1895 s->next = NULL;
1896 s->data = malloc (2);
1897 s->len = 2;
1898 clen += 2;
1899 s->data[0] = 1;
1900 s->data[1] = d - 1;
1902 if (oi > 0)
1904 s = (struct pevt_stage1 *) malloc (sizeof (struct pevt_stage1));
1905 if (base == NULL)
1906 base = s;
1907 if (last != NULL)
1908 last->next = s;
1909 last = s;
1910 s->next = NULL;
1911 s->data = malloc (oi + sizeof (int) + 1);
1912 s->len = oi + sizeof (int) + 1;
1913 clen += oi + sizeof (int) + 1;
1914 s->data[0] = 0;
1915 memcpy (&(s->data[1]), &oi, sizeof (int));
1916 memcpy (&(s->data[1 + sizeof (int)]), o, oi);
1917 oi = 0;
1919 s = (struct pevt_stage1 *) malloc (sizeof (struct pevt_stage1));
1920 if (base == NULL)
1921 base = s;
1922 if (last != NULL)
1923 last->next = s;
1924 last = s;
1925 s->next = NULL;
1926 s->data = malloc (1);
1927 s->len = 1;
1928 clen += 1;
1929 s->data[0] = 2;
1931 oi = 0;
1932 s = base;
1933 obuf = malloc (clen);
1934 while (s)
1936 next = s->next;
1937 memcpy (&obuf[oi], s->data, s->len);
1938 oi += s->len;
1939 free (s->data);
1940 free (s);
1941 s = next;
1944 free (i);
1946 if (max_arg)
1947 *max_arg = max;
1948 if (output)
1949 *output = obuf;
1951 return 0;
1955 /* black n white(0/1) are bad colors for nicks, and we'll use color 2 for us */
1956 /* also light/dark gray (14/15) */
1957 /* 5,7,8 are all shades of yellow which happen to look dman near the same */
1959 static char rcolors[] = { 19, 20, 22, 24, 25, 26, 27, 28, 29 };
1961 static int
1962 color_of (char *name)
1964 int i = 0, sum = 0;
1966 while (name[i])
1967 sum += name[i++];
1968 sum %= sizeof (rcolors) / sizeof (char);
1969 return rcolors[sum];
1973 /* called by EMIT_SIGNAL macro */
1974 #include <stdio.h>
1975 void
1976 text_emit (int index, session *sess, char *a, char *b, char *c, char *d, char* file, int lineno)
1978 char *word[PDIWORDS];
1979 int i;
1980 unsigned int stripcolor_args = (prefs.stripcolor ? 0xFFFFFFFF : 0);
1981 char tbuf[NICKLEN + 4];
1983 if(getenv("XCHATDEBUG"))
1984 dprintf(2, "%s:%d %s %s %s %s\n", file, lineno, a ? a : "", b ? b : "", c ? c : "", d ? d : "");
1986 if (prefs.colorednicks && (index == XP_TE_CHANACTION || index == XP_TE_CHANMSG))
1988 snprintf (tbuf, sizeof (tbuf), "\003%d%s", color_of (a), a);
1989 a = tbuf;
1990 stripcolor_args &= ~ARG_FLAG(1); /* don't strip color from this argument */
1993 word[0] = te[index].name;
1994 word[1] = (a ? a : "\000");
1995 word[2] = (b ? b : "\000");
1996 word[3] = (c ? c : "\000");
1997 word[4] = (d ? d : "\000");
1998 for (i = 5; i < PDIWORDS; i++)
1999 word[i] = "\000";
2001 if (plugin_emit_print (sess, word))
2002 return;
2004 /* If a plugin's callback executes "/close", 'sess' may be invalid */
2005 if (!is_session (sess))
2006 return;
2008 switch (index)
2010 case XP_TE_JOIN:
2011 case XP_TE_PART:
2012 case XP_TE_PARTREASON:
2013 case XP_TE_QUIT:
2014 /* implement ConfMode / Hide Join and Part Messages */
2015 if (chanopt_is_set (prefs.confmode, sess->text_hidejoinpart))
2016 return;
2017 break;
2019 /* ===Private message=== */
2020 case XP_TE_PRIVMSG:
2021 case XP_TE_DPRIVMSG:
2022 case XP_TE_PRIVACTION:
2023 case XP_TE_DPRIVACTION:
2024 if (chanopt_is_set_a (prefs.input_beep_priv, sess->alert_beep))
2025 sound_beep (sess);
2026 if (chanopt_is_set_a (prefs.input_flash_priv, sess->alert_taskbar))
2027 fe_flash_window (sess);
2028 /* why is this one different? because of plugin-tray.c's hooks! ugly */
2029 if (sess->alert_tray == SET_ON)
2030 fe_tray_set_icon (FE_ICON_MESSAGE);
2031 break;
2033 /* ===Highlighted message=== */
2034 case XP_TE_HCHANACTION:
2035 case XP_TE_HCHANMSG:
2036 if (chanopt_is_set_a (prefs.input_beep_hilight, sess->alert_beep))
2037 sound_beep (sess);
2038 if (chanopt_is_set_a (prefs.input_flash_hilight, sess->alert_taskbar))
2039 fe_flash_window (sess);
2040 if (sess->alert_tray == SET_ON)
2041 fe_tray_set_icon (FE_ICON_MESSAGE);
2042 break;
2044 /* ===Channel message=== */
2045 case XP_TE_CHANACTION:
2046 case XP_TE_CHANMSG:
2047 if (chanopt_is_set_a (prefs.input_beep_chans, sess->alert_beep))
2048 sound_beep (sess);
2049 if (chanopt_is_set_a (prefs.input_flash_chans, sess->alert_taskbar))
2050 fe_flash_window (sess);
2051 if (sess->alert_tray == SET_ON)
2052 fe_tray_set_icon (FE_ICON_MESSAGE);
2053 break;
2054 case XP_TE_DISCON:
2055 /* do not spam log */
2056 if(!sess->total) return;
2059 sound_play_event (index);
2060 display_event (sess, index, word, stripcolor_args);
2063 char *
2064 text_find_format_string (char *name)
2066 int i = 0;
2068 i = pevent_find (name, &i);
2069 if (i >= 0)
2070 return pntevts_text[i];
2072 return NULL;
2076 text_emit_by_name (char *name, session *sess, char *a, char *b, char *c, char *d)
2078 int i = 0;
2080 i = pevent_find (name, &i);
2081 if (i >= 0)
2083 text_emit (i, sess, a, b, c, d, "", 0);
2084 return 1;
2087 return 0;
2090 void
2091 pevent_save (char *fn)
2093 int fd, i;
2094 char buf[1024];
2096 if (!fn)
2097 fd = xchat_open_file ("pevents.conf", O_CREAT | O_TRUNC | O_WRONLY,
2098 0x180, XOF_DOMODE);
2099 else
2100 fd = xchat_open_file (fn, O_CREAT | O_TRUNC | O_WRONLY, 0x180,
2101 XOF_FULLPATH | XOF_DOMODE);
2102 if (fd == -1)
2105 fe_message ("Error opening config file\n", FALSE);
2106 If we get here when X-Chat is closing the fe-message causes a nice & hard crash
2107 so we have to use perror which doesn't rely on GTK
2110 perror ("Error opening config file\n");
2111 return;
2114 for (i = 0; i < NUM_XP; i++)
2116 write (fd, buf, snprintf (buf, sizeof (buf),
2117 "event_name=%s\n", te[i].name));
2118 write (fd, buf, snprintf (buf, sizeof (buf),
2119 "event_text=%s\n\n", pntevts_text[i]));
2122 close (fd);
2125 /* =========================== */
2126 /* ========== SOUND ========== */
2127 /* =========================== */
2129 char *sound_files[NUM_XP];
2131 void
2132 sound_beep (session *sess)
2134 if (sound_files[XP_TE_BEEP] && sound_files[XP_TE_BEEP][0])
2135 /* user defined beep _file_ */
2136 sound_play_event (XP_TE_BEEP);
2137 else
2138 /* system beep */
2139 fe_beep ();
2142 static char *
2143 sound_find_command (void)
2145 /* some sensible unix players. You're bound to have one of them */
2146 static const char * const progs[] = {"aplay", "esdplay", "soxplay", "artsplay", NULL};
2147 char *cmd;
2148 int i = 0;
2150 if (prefs.soundcmd[0])
2151 return g_strdup (prefs.soundcmd);
2153 while (progs[i])
2155 cmd = g_find_program_in_path (progs[i]);
2156 if (cmd)
2157 return cmd;
2158 i++;
2161 return NULL;
2164 void
2165 sound_play (const char *file, gboolean quiet)
2167 char buf[512];
2168 char wavfile[512];
2169 char *file_fs;
2170 char *cmd;
2172 /* the pevents GUI editor triggers this after removing a soundfile */
2173 if (!file[0])
2174 return;
2176 if (file[0] != '/')
2178 snprintf (wavfile, sizeof (wavfile), "%s/%s", prefs.sounddir, file);
2179 } else
2181 strncpy (wavfile, file, sizeof (wavfile));
2183 wavfile[sizeof (wavfile) - 1] = 0; /* ensure termination */
2185 file_fs = xchat_filename_from_utf8 (wavfile, -1, 0, 0, 0);
2186 if (!file_fs)
2187 return;
2189 if (access (file_fs, R_OK) == 0)
2191 cmd = sound_find_command ();
2193 if (cmd)
2195 if (strchr (file_fs, ' '))
2196 snprintf (buf, sizeof (buf), "%s \"%s\"", cmd, file_fs);
2197 else
2198 snprintf (buf, sizeof (buf), "%s %s", cmd, file_fs);
2199 buf[sizeof (buf) - 1] = '\0';
2200 xchat_exec (buf);
2203 if (cmd)
2204 g_free (cmd);
2206 } else
2208 if (!quiet)
2210 snprintf (buf, sizeof (buf), _("Cannot read sound file:\n%s"), wavfile);
2211 fe_message (buf, FE_MSG_ERROR);
2215 g_free (file_fs);
2218 void
2219 sound_play_event (int i)
2221 if (sound_files[i])
2222 sound_play (sound_files[i], FALSE);
2225 static void
2226 sound_load_event (char *evt, char *file)
2228 int i = 0;
2230 if (file[0] && pevent_find (evt, &i) != -1)
2232 if (sound_files[i])
2233 free (sound_files[i]);
2234 sound_files[i] = strdup (file);
2238 void
2239 sound_load ()
2241 int fd;
2242 char buf[512];
2243 char evt[128];
2245 memset (&sound_files, 0, sizeof (char *) * (NUM_XP));
2247 fd = xchat_open_file ("sound.conf", O_RDONLY, 0, 0);
2248 if (fd == -1)
2249 return;
2251 evt[0] = 0;
2252 while (waitline (fd, buf, sizeof buf, FALSE) != -1)
2254 if (strncmp (buf, "event=", 6) == 0)
2256 safe_strcpy (evt, buf + 6, sizeof (evt));
2258 else if (strncmp (buf, "sound=", 6) == 0)
2260 if (evt[0] != 0)
2262 sound_load_event (evt, buf + 6);
2263 evt[0] = 0;
2268 close (fd);
2271 void
2272 sound_save ()
2274 int fd, i;
2275 char buf[512];
2277 fd = xchat_open_file ("sound.conf", O_CREAT | O_TRUNC | O_WRONLY, 0x180,
2278 XOF_DOMODE);
2279 if (fd == -1)
2280 return;
2282 for (i = 0; i < NUM_XP; i++)
2284 if (sound_files[i] && sound_files[i][0])
2286 write (fd, buf, snprintf (buf, sizeof (buf),
2287 "event=%s\n", te[i].name));
2288 write (fd, buf, snprintf (buf, sizeof (buf),
2289 "sound=%s\n\n", sound_files[i]));
2293 close (fd);