summaryrefslogtreecommitdiff
path: root/kanotix-irc/kanotix-irc
blob: b61b6202b3aa03f93b0649bfb4e207fcb47465c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
#!/bin/sh
# Copyright (C) 2003 Jörg Schirottke <master@kanotix.com>
# Copyright (C) 2005 Michiel Lieuwe de Boer
# Copyright (C) 2005-2006 Stefan Lippers-Hollmann <s.l-h@gmx.de>

# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# override tool behaviour through distro-defaults
FLL_IRC_SERVER="irc.freenode.net"
FLL_IRC_PORT="6667"
FLL_IRC_CHANNEL="#kanotix"
FLL_DISTRO_NAME="Kanotix"
[ -r /etc/default/distro ] && . /etc/default/distro

# define user settings
NICK="Web-${FLL_DISTRO_NAME}$(mawk 'BEGIN{print int(1000 * rand())}')"
FALLBACK_USER="$(getent passwd 1000 | cut -d\: -f1)"
XTERM_OPT="-e"


# determine system status
DIALOG="$(which dialog)"			|| DIALOG="/usr/bin/dialog"
KDIALOG="$(which kdialog)"			|| KDIALOG="/usr/bin/kdialog"
XDIALOG="$(which Xdialog)"			|| XDIALOG="/usr/bin/Xdialog"
ZENITY="$(which zenity)"			|| ZENITY="/usr/bin/zenity"
YAD="$(which yad)"				|| YAD="/usr/bin/yad"
MKTEMP="$(which mktemp)"			|| XTERMINAL="/bin/mktemp"
SU="$(which su)"				|| SU="/bin/su"
XTERMINAL="$(which x-terminal-emulator)"	|| XTERMINAL="/usr/bin/x-terminal-emulator"

BITCHX="$(which bitchx)"			|| BITCHX="/usr/bin/bitchx"
IRSSI="$(which irssi)"				|| IRSSI="/usr/bin/irssi"
KSIRC="$(which ksirc)"				|| KSIRC="/usr/bin/ksirc"
KONVERSATION="$(which konversation)"		|| KONVERSATION="/usr/bin/konversation"
WEECHAT="$(which weechat-curses)"		|| WEECHAT="/usr/bin/weechat-curses"
XCHAT="$(which xchat)"				|| XCHAT="/usr/bin/xchat"
HEXCHAT="$(which hexchat)"			|| HEXCHAT="/usr/bin/hexchat"

# internal functions
inputbox()
{
	# inputbox title text width height [init]
	TITLE="$1"
	TEXT="$2"
	WIDTH="$3"
	HEIGHT="$4"
	INIT="$5"
	
	if [ -n "$DISPLAY" ]; then
		if [ -x "$KDIALOG" ]; then
			"$KDIALOG" --title "$TITLE" --inputbox "$TEXT" "$INIT"
		elif [ -x "$XDIALOG" ]; then
			"$XDIALOG" --title "$TITLE" --inputbox "$TEXT" $HEIGHT $WIDTH "$INIT"
		elif [ -x "$ZENITY" ]; then
			"$ZENITY" --title="$TITLE" --entry --text="$TEXT" --entry-text="$INIT"
		elif [ -x "$YAD" ]; then
			"$YAD" --center --title="$TITLE" --entry --text="$TEXT" --entry-text="$INIT"
		fi		
	else
		"$DIALOG" --stdout --title "$TITLE" --inputbox "$TEXT" $HEIGHT $WIDTH "$INIT"
	fi
}

kanotix_irssi()
{
	TERM_TYPE="utf-8"
	#TERM_TYPE="8bit"
	IRSSICFG="$($MKTEMP -dp /tmp/ .irssi-XXXXXXXXXX)"

	CTCPS=0
	if [ -e /etc/kanotix-version ]; then
		QUITMSG="$(< /etc/kanotix-version) | http://www.kanotix.com"
	else
		QUITMSG="http://www.kanotix.com"
	fi

	cat <<_EOF_ >"$IRSSICFG/config"
servers = (
  {
    address = "$FLL_IRC_SERVER";
    chatnet = "freenode";
    port = "$FLL_IRC_PORT";
    autoconnect = "yes";
_EOF_
	# if ssl port configured then add ssl options
	if [ $FLL_IRC_PORT = "7000" ]; then
		cat <<_EOF_ >>"$IRSSICFG/config"
    use_ssl = "yes";
    ssl_verify = "yes";
    ssl_capath = "/etc/ssl/certs";
_EOF_
	fi
	cat <<_EOF_ >>"$IRSSICFG/config"
  }
);

chatnets = {
  freenode = {
    type = "IRC";
    max_kicks = "4";
    max_modes = "4";
    max_msgs = "1";
    max_whois = "1";
  };
};

channels = (
  { name = "#kanotix"; chatnet = "freenode"; autojoin = "yes"; }
);
_EOF_

	# See also special kanotix aliases in here, like "/IBS", "/IBV", "/IB", "/GFX", "/SND", and "/X"
	cat <<"_EOF_" >>"$IRSSICFG/config"
aliases = {
  J = "join";
  WJOIN = "join -window";
  WQUERY = "query -window";
  LEAVE = "part";
  BYE = "quit";
  EXIT = "quit";
  SIGNOFF = "quit";
  DESCRIBE = "action";
  DATE = "time";
  HOST = "userhost";
  LAST = "lastlog";
  SAY = "msg *";
  WI = "whois";
  WII = "whois $0 $0";
  WW = "whowas";
  W = "who";
  N = "names";
  M = "msg";
  T = "topic";
  C = "clear";
  CL = "clear";
  K = "kick";
  KB = "kickban";
  KN = "knockout";
  BANS = "ban";
  B = "ban";
  MUB = "unban *";
  UB = "unban";
  IG = "ignore";
  UNIG = "unignore";
  SB = "scrollback";
  UMODE = "mode $N";
  WC = "window close";
  WN = "window new hide";
  SV = "say Irssi $J ($V) - http://irssi.org/";
  GOTO = "sb goto";
  CHAT = "dcc chat";
  RUN = "SCRIPT LOAD";
  UPTIME = "eval exec - expr `date +%s` - \\$F | awk '{print \"Irssi uptime: \"int(\\\\\\$1/3600/24)\"d \"int(\\\\\\$1/3600%24)\"h \"int(\\\\\\$1/60%60)\"m \"int(\\\\\\$1%60)\"s\" }'";
  CALC = "exec - if which bc &>/dev/null\\; then echo '$*' | bc | awk '{print \"$*=\"$$1}'\\; else echo bc was not found\\; fi";
  SBAR = "STATUSBAR";
  INVITELIST = "mode $C +I";
  Q = "QUERY";
  QUIT = "foreach window /part;/quit";
  INFOBASH = "/exec -o infobash $*";
  X = "QUIT";
};

statusbar = {
  # formats:
  # when using {templates}, the template is shown only if it's argument isn't
  # empty unless no argument is given. for example {sb} is printed always,
  # but {sb $T} is printed only if $T isn't empty.

  items = {
    # start/end text in statusbars
    barstart = "{sbstart}";
    barend = "{sbend}";

    topicbarstart = "{topicsbstart}";
    topicbarend = "{topicsbend}";

    # treated "normally", you could change the time/user name to whatever
    time = "{sb $Z}";
    user = "{sb {sbnickmode $cumode}$N{sbmode $usermode}{sbaway $A}}";

    # treated specially .. window is printed with non-empty windows,
    # window_empty is printed with empty windows
    window = "{sb $winref:$itemname{sbmode $M}}";
    window_empty = "{sb $winref{sbservertag $tag}}";
    prompt = "{prompt $[.15]itemname}";
    prompt_empty = "{prompt $winname}";
    topic = " $topic";
    topic_empty = " Irssi v$J - http://irssi.org/help/";

    # all of these treated specially, they're only displayed when needed
    lag = "{sb Lag: $0-}";
    act = "{sb Act: $0-}";
    more = "-- more --";
  };

  # there's two type of statusbars. root statusbars are either at the top
  # of the screen or at the bottom of the screen. window statusbars are at
  # the top/bottom of each split window in screen.
  default = {
    # the "default statusbar" to be displayed at the bottom of the window.
    # contains all the normal items.
    window = {
      disabled = "no";

      # window, root
      type = "window";
      # top, bottom
      placement = "bottom";
      # number
      position = "1";
      # active, inactive, always
      visible = "active";

      # list of items in statusbar in the display order
      items = {
        barstart = { priority = "100"; };
        time = { };
        user = { };
        window = { };
        window_empty = { };
        lag = { priority = "-1"; };
        act = { priority = "10"; };
        more = { priority = "-1"; alignment = "right"; };
        barend = { priority = "100"; alignment = "right"; };
      };
    };

    # statusbar to use in inactive split windows
    window_inact = {
      type = "window";
      placement = "bottom";
      position = "1";
      visible = "inactive";
      items = {
        barstart = { priority = "100"; };
        window = { };
        window_empty = { };
        more = { priority = "-1"; alignment = "right"; };
        barend = { priority = "100"; alignment = "right"; };
      };
    };

    # we treat input line as yet another statusbar :) It's possible to
    # add other items before or after the input line item.
    prompt = {
      type = "root";
      placement = "bottom";
      # we want to be at the bottom always
      position = "100";
      visible = "always";
      items = {
        prompt = { priority = "-1"; };
        prompt_empty = { priority = "-1"; };
        # treated specially, this is the real input line.
        input = { priority = "10"; };
      };
    };

    # topicbar
    topic = {
      type = "root";
      placement = "top";
      position = "1";
      visible = "always";
      items = {
        topicbarstart = { priority = "100"; };
        topic = { };
        topic_empty = { };
        topicbarend = { priority = "100"; alignment = "right"; };
      };
    };
  };
};
_EOF_

	cat <<_EOF_ >>"$IRSSICFG/config"
settings = {
  core = {
    real_name = "Kanotix user";
    user_name = "kanotix";
    nick = "$NICK";
    quit_message = "$QUITMSG";
  };

  "irc/core" = { part_message = "$QUITMSG"; };
  
  "irc/dcc" = {
    dcc_upload_path = "/tmp";
    dcc_download_path = "/tmp";
    dcc_autoget = "no";
    dcc_autochat_masks = "*";
    dcc_autoget_masks = "= *";
  };
  "fe-text" = { term_type = "$TERM_TYPE"; };
  "fe-common/core" = { completion_char = ","; };
  
};
logs = { };

windows = {
  2 = {
    items = (
      {
        type = "CHANNEL";
        chat_type = "IRC";
        name = "$FLL_IRC_CHANNEL";
        tag = "freenode";
      }
    );
  };
  1 = { immortal = "yes"; name = "(status)"; level = "ALL"; };
};
mainwindows = { 2 = { first_line = "1"; lines = "47"; }; };
_EOF_

	[ "$CTCPS" -eq 0 ] && echo 'ignores = ( { level = "CTCPS"; } );' >>"$IRSSICFG/config"

	"$IRSSI" --home="$IRSSICFG"
	RETC="$?"
	rm -rf "$IRSSICFG"
	[ "$RETC" -ne 0 ] && return 127 || exit 0
}

kanotix_xchat()
{
	XCHATCFG="$($MKTEMP -dp /tmp/ .xchat2-XXXXXXXXXX)"

        if [ -e ~/.xchat2/ ]; then
		cp -r ~/.xchat2/* "$XCHATCFG/"
		# delete improper lines
	sed -i '/gui_join_dialog/d;
		/gui_slist_skip/d;
		/gui_slist_select/d;
		/irc_nick1/d;
		/irc_nick2/d;
		/irc_nick3/d;
		/irc_user_name/d;
		/irc_real_name/d' "$XCHATCFG/xchat.conf"
	fi
	cat <<_EOF_ >>"$XCHATCFG/xchat.conf"
gui_join_dialog = 0
gui_slist_select = 0
gui_slist_skip = 1
irc_nick1 = $NICK
irc_nick2 = ${NICK}_
irc_nick3 = ${NICK}__
_EOF_


	cat <<_EOF_ >"$XCHATCFG/servlist_.conf"
N=FreeNode
J=#kanotix
E=UTF-8 (Unicode)
F=63
_EOF_
	# if ssl port configured then add ssl options
	if [ $FLL_IRC_PORT = "7000" ]; then
		cat <<_EOF_ >>"$XCHATCFG/servlist_.conf"
D=1
_EOF_
	else
		cat <<_EOF_ >>"$XCHATCFG/servlist_.conf"
D=0
_EOF_
	fi
	cat <<_EOF_ >>"$XCHATCFG/servlist_.conf"
S=irc.freenode.net/$FLL_IRC_PORT
S=irc.freenode.net/+7000
_EOF_

	cat <<_EOF_ >"$XCHATCFG/commands.conf"
NAME cmd
CMD exec -o &2
_EOF_

	"$XCHAT" -d "$XCHATCFG"
	RETC="$?"
	rm -rf "$XCHATCFG"
	[ "$RETC" -ne 0 ] && return 127 || exit 0
}

kanotix_hexchat()
{
	HEXCHATCFG="$($MKTEMP -dp /tmp/ .hexchat-XXXXXXXXXX)"

        if [ -e ~/.config/hexchat/ ]; then
		cp -r ~/.config/hexchat/* "$HEXCHATCFG/"
		# delete improper lines
	sed -i '/gui_join_dialog/d;
		/gui_slist_skip/d;
		/gui_slist_select/d;
		/irc_nick1/d;
		/irc_nick2/d;
		/irc_nick3/d;
		/irc_user_name/d;
		/irc_real_name/d' "$HEXCHATCFG/hexchat.conf"
	fi
	cat <<_EOF_ >>"$HEXCHATCFG/hexchat.conf"
gui_join_dialog = 0
gui_slist_select = 0
gui_slist_skip = 1
irc_nick1 = $NICK
irc_nick2 = ${NICK}_
irc_nick3 = ${NICK}__
_EOF_


	cat <<_EOF_ >"$HEXCHATCFG/servlist.conf"
N=FreeNode
J=#kanotix
E=UTF-8 (Unicode)
F=63
_EOF_
	# if ssl port configured then add ssl options
	if [ $FLL_IRC_PORT = "7000" ]; then
		cat <<_EOF_ >>"$HEXCHATCFG/servlist.conf"
D=1
_EOF_
	else
		cat <<_EOF_ >>"$HEXCHATCFG/servlist.conf"
D=0
_EOF_
	fi
	cat <<_EOF_ >>"$HEXCHATCFG/servlist.conf"
S=irc.freenode.net/$FLL_IRC_PORT
S=irc.freenode.net/+7000
_EOF_

	cat <<_EOF_ >"$HEXCHATCFG/commands.conf"
NAME cmd
CMD exec -o &2
_EOF_

	"$HEXCHAT" -d "$HEXCHATCFG"
	RETC="$?"
	rm -rf "$HEXCHATCFG"
	[ "$RETC" -ne 0 ] && return 127 || exit 0
}



##############################################################################
#                               int main(void);                              #
##############################################################################

# yes, this stuff is elementary
if [ -z "$DISPLAY" ]; then
	for i in "$DIALOG" "$SU"; do
		if [ ! -x "$i" ]; then
			echo "$i: missing, terminate abnormally"
			exit 999
		fi
	done
else
	for i in "$DIALOG" "$XTERMINAL" "$SU"; do
		if [ ! -x "$i" ]; then
			echo "$i: missing, terminate abnormally"
			exit 998
		fi
	done
	if ! [ -x "$XDIALOG" -o -x "$KDIALOG" -o -x "$ZENITY" -o -x "$YAD" ]; then
		echo "$XDIALOG, $KDIALOG, $ZENITY and $YAD are missing, terminate abnormally"
		exit 998
	fi
fi

if [ "$1" = "--tty" ]; then
	DISPLAY=""
fi

# are you insane?
if [ "`id -u`" = "0" ]; then
	echo "ROOOOOOOOOOOOT has landed!"
	echo ""
	echo "Don't ever call any IRC client as root"

	if [ -z "$FALLBACK" ]; then
		# yes, you deserve it - and I'm to lazy to enhance kanotix-su's features ;)
		echo "Employ fallback and start IRC as $FALLBACK_USER."
		if [ -n "$DISPLAY" ]; then
			exec $XTERMINAL $XTERM_OPT $SU -c "$0 --tty" "$FALLBACK_USER"
		else
			exec $SU -c "$0 --tty" "$FALLBACK_USER"
		fi
	else
		exit 996
	fi

	if [ "`id -u`" = "0" ]; then
		echo "you had your chance, I'm not going to start any IRC client as root"
		exit 997
	fi
fi

# choose your nick
NICK=$(inputbox "${FLL_DISTRO_NAME} IRC Chat" "Choose your nick" 50 10 "$NICK") || exit 0

# try to start your irc client
if [ -z "$DISPLAY" ]; then
	# tty
	if [ -x "$IRSSI" ]; then
		kanotix_irssi
		exit "$?"
	fi

	if [ -x "$WEECHAT" ]; then
	    [ -d ~/.weechat ] || cp -r /etc/skel/.weechat ~
	    grep -qs "ssl_dhkey_size = 2048" ~/.weechat/irc.conf && sed -i 's/\(ssl_dhkey_size =\).*/\1 1024/' ~/.weechat/irc.conf
	    [ -r ~/.weechat/irc.conf ] || cp /etc/skel/.weechat/irc.conf ~/.weechat
	    exec $WEECHAT -a "ircs://$NICK@$FLL_IRC_SERVER:$FLL_IRC_PORT/$FLL_IRC_CHANNEL"
	fi

	[ -x "$BITCHX" ] && \
		exec "$BITCHX" -c "$FLL_IRC_CHANNEL" -n "$NICK"
else
	# X11
	if [ -x "$KONVERSATION" ]; then
		if [ $FLL_IRC_PORT = "7000" ]; then
			exec "$KONVERSATION" --server "$FLL_IRC_SERVER" --ssl --port "$FLL_IRC_PORT" --channel "$FLL_IRC_CHANNEL" --nick "$NICK"
		else
			exec "$KONVERSATION" --server "$FLL_IRC_SERVER" --port "$FLL_IRC_PORT" --channel "$FLL_IRC_CHANNEL" --nick "$NICK"
		fi
	fi

	[ -x "$KSIRC" ] && \
		exec "$KSIRC" --server "$FLL_IRC_SERVER" --channel "$FLL_IRC_CHANNEL" --nick "$NICK"

	if [ -x "$HEXCHAT" ]; then
		kanotix_hexchat
		exit "$?"
	fi

	if [ -x "$XCHAT" ]; then
		kanotix_xchat
		exit "$?"
	fi

	# nasty fallback, but well - it works ;)
	exec $XTERMINAL $XTERM_OPT $0 --tty
fi

# Oops...
echo "ERROR: failed to start any suitable IRC Client"
for i in "$DIALOG" "$XDIALOG" "$BITCHX" "$IRSSI" "$KSIRC" "$KONVERSATION" "$WEECHAT" "$XCHAT" "$HEXCHAT" ; do
	if [ -x "$i" ]; then
		echo "$i: found."
	else
		echo "$i: NOT found."
	fi
done