-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathirc.c
214 lines (177 loc) · 6.23 KB
/
irc.c
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
/* Copyright 2011 Tom Jollans <[email protected]>
* This code is under the Chicken Dance License v0.1 */
#include "command.h"
#include <libircclient.h>
#include <libirc_rfcnumeric.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void on_connect (irc_session_t *session, const char *event, const char *origin, const char **params, unsigned count);
void on_privmsg (irc_session_t *session, const char *event, const char *origin, const char **params, unsigned count);
void on_chanmsg (irc_session_t *session, const char *event, const char *origin, const char **params, unsigned count);
void on_invite (irc_session_t *session, const char *event, const char *origin, const char **params, unsigned count);
void on_generic (irc_session_t *session, const char *event, const char *origin, const char **params, unsigned count);
void on_numeric (irc_session_t *session, unsigned event, const char *origin, const char **params, unsigned count);
static char *irc_nick;
struct channel_game *channel_games_list = NULL;
/* might want different nicks on different servers some day. */
const char *
get_irc_nick (irc_session_t *session)
{
return irc_nick;
}
int
main (int argc, char **argv)
{
/* Basic single-server single-thread IRC bot. */
irc_callbacks_t callbacks = {
.event_nick = &on_generic,
.event_quit = &on_generic,
.event_join = &on_generic,
.event_part = &on_generic,
.event_mode = &on_generic,
.event_umode = &on_generic,
.event_topic = &on_generic,
.event_kick = &on_generic,
.event_notice = &on_generic,
/* .event_channel_notice = &on_generic, */
.event_ctcp_req = &on_generic,
.event_ctcp_rep = &on_generic,
.event_ctcp_action = &on_generic,
.event_unknown = &on_generic,
.event_numeric = &on_numeric,
.event_connect = &on_connect,
.event_privmsg = &on_privmsg,
.event_channel = &on_chanmsg,
.event_invite = &on_invite
};
irc_session_t *session;
char *server, *nick;
int port;
if (argc != 4) {
fprintf(stderr, "usage: ircpoker [server] [port] [nick]\n");
return 2;
}
/* TODO: configuration file */
server = argv[1];
port = atoi(argv[2]); /* TODO: value checking */
nick = argv[3];
irc_nick = strdup(nick);
session = irc_create_session(&callbacks);
if (!session) {
fprintf(stderr, "ERROR creating session handle. Quitting.\n");
return 1;
}
printf("nick: '%s'\n", nick);
if (irc_connect(session, server, port, NULL,
nick, nick, "IRC Poker")
!= 0) {
fprintf(stderr, "ERROR connecting to server %s on port %d: %s\n",
server, port, irc_strerror(irc_errno(session)));
return 1;
}
/* error checking might be nice */
irc_run(session);
irc_destroy_session(session);
return 0;
}
void
on_connect (irc_session_t *session, const char *event,
const char *origin, const char **params, unsigned count)
{
printf("*** CONNECTION ESTABLISHED. ***\n");
}
void
on_privmsg (irc_session_t *session, const char *event,
const char *origin, const char **params, unsigned count)
{
const char *dest = params[0];
/* process_cmd needs to be able to modify the cmd string */
char *msg = strdup(params[1]);
printf("privmsg: %s --> %s: \"%s\"\n", origin, dest, msg);
/* private messages are always seen as commands. */
process_cmd(session, origin, NULL, msg);
free(msg);
}
void
on_chanmsg (irc_session_t *session, const char *event,
const char *origin, const char **params, unsigned count)
{
int len;
const char *dest = params[0];
/* process_cmd needs to be able to modify the cmd string */
char *msg = strdup(params[1]);
char *firstword;
if (strcmp(dest, get_irc_nick(session)) == 0) {
/* the lib can get confused when the server silently changes the nick) */
on_privmsg(session, event, origin, params, count);
free(msg);
return;
}
printf("%s in %s: \"%s\"\n", origin, dest, msg);
/* treat messages prefixed with my name as commands: */
firstword = first_word(msg);
len = strlen(firstword);
/* name may be postfixed with colon or comma */
switch(firstword[len-1]) {
case ':':
case ',':
firstword[len-1] = '\0';
}
if (strcmp(firstword, get_irc_nick(session)) == 0) {
process_cmd(session, origin, dest, &msg[len+1]);
} else {
/* is there a game on the channel? If so, check all messages for
* betting-round commands */
game_tp game;
if ((game = get_channel_game(session, dest))) {
process_bet_cmd(session, origin, dest, game, msg);
}
}
free(firstword);
free(msg);
}
void
on_invite (irc_session_t *session, const char *event,
const char *origin, const char **params, unsigned count)
{
/* possibly check if this is me? probably not necessary. */
const char *invitee = params[0];
const char *channel = params[1];
printf("invitation for %s to join %s, from %s. Vamos!\n", invitee, channel, origin);
irc_cmd_join(session, channel, NULL);
}
void
on_generic (irc_session_t *session, const char *event, const char *origin, const char **params, unsigned count)
{
unsigned i;
printf("-- [%s] -- (%s) -- ", event, origin);
for (i=0; i<count; ++i)
printf("%s -- ", params[i]);
printf("\n");
}
void
on_numeric (irc_session_t *session, unsigned event, const char *origin, const char **params, unsigned count)
{
unsigned i;
printf("-- [%d] -- (%s) -- ", event, origin);
for (i=0; i<count; ++i)
printf("%s -- ", params[i]);
printf("\n");
if (event == LIBIRC_RFC_RPL_WELCOME && count == 2) {
/* get the actual nick (in case the server changed it) */
char *old_irc_nick = irc_nick;
irc_nick = strdup(params[0]);
free(old_irc_nick);
}
}
game_tp
get_channel_game (irc_session_t *session, const char *channel)
{
struct channel_game *cg;
if (!channel) return NULL;
for (cg = channel_games_list; cg; cg = cg->next)
if (cg->session == session && !strcasecmp(cg->channel, channel))
return cg->game;
return NULL;
}