-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathircmsg.js
146 lines (131 loc) · 2.8 KB
/
ircmsg.js
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
/* ircmsg.js */
/* Send a message (from stdin or the command-line) to an IRC channel */
/* $Id: ircmsg.js,v 1.34 2015/11/25 09:05:26 deuce Exp $ */
load("irclib.js"); // Thanks Cyan!
const REVISION = "$Revision: 1.34 $".split(' ')[1];
var server="irc.synchro.net";
var channel="#channel";
var port=6667;
var nick="nick";
var msg;
var join=false;
var exclude=new Array();
var quiet=false;
function mylog(msg)
{
if(!quiet)
log(LOG_INFO,"ircmsg: " + msg);
}
for(i=0;i<argc;i++) {
switch(argv[i]) {
case "-s":
server=argv[++i];
break;
case "-j":
join=true;
break;
case "-c":
channel=argv[++i];
break;
case "-p":
port=argv[++i];
break;
case "-n":
nick=argv[++i];
break;
case "-q":
quiet=true;
break;
case "-m":
msg=argv[++i];
if(msg==undefined || msg.search(/^[\r\n]*$/)!=-1) {
alert("-m specified with blank message... aborting");
exit();
}
break;
case "-x":
exclude.push(RegExp(argv[++i],"i"));
break;
}
}
mylog("Using nick: " + nick);
mylog("Connecting to: " +server+ " port " + port);
my_server = IRC_client_connect(server,nick,undefined,undefined,port);
if(!my_server) {
alert("!Couldn't connect to " + server);
exit();
}
var done=0;
while(!done) {
while(!done && (response=my_server.recvline())) {
var resp=response.split(/\s+/);
if(resp[1]=='433') {
mylog(response);
/* Nick in use... */
nick+='_';
mylog("Using nick: " + nick);
my_server.send("NICK " + nick + "\r\n");
}
if(resp[1]=='422' || resp[1]=='376')
done=1;
//mylog(response);
}
if(!my_server.is_connected) {
alert("Disconnected");
exit();
}
}
if(join) {
mylog("Joining: " + channel);
my_server.send("JOIN " + channel + "\r\n");
while(my_server.poll(5) && (response=my_server.recvline()))
mylog(response);
}
var delay=0;
if(msg)
send(msg, delay);
else while((msg=readln())!=undefined) { /* read from stdin */
var send_count = 0;
if (send(msg, delay)) {
send_count++;
if (send_count > 4)
delay = 2050;
}
}
while(my_server.poll(0) && (response=my_server.recvline()))
mylog(response);
IRC_quit(my_server);
mylog("Exiting");
exit();
function send(msg, delay)
{
if(msg==undefined || msg.search(/^[\r\n]*$/)!=-1) {
mylog("Not sending blank message");
return false;
}
for(i in exclude)
if(msg.search(exclude[i])>=0)
return false;
if (delay)
mswait(delay); // Cyan: IRCd throttles clients that send text to the server too quickly
mylog("Sending: " + msg);
if(!my_server.send("PRIVMSG "+channel+" :"+expand_tabs(msg)+"\r\n")) {
alert("send failure");
return false;
}
return true;
}
function expand_tabs(msg)
{
if(msg!=undefined) {
var s=msg;
do {
msg=s;
s=msg.replace(/\t/,
function(str,offset,s) {
return(' '.substr(0,8 - offset % 8));
});
} while(s!=msg);
}
return(msg);
}