44 printf(" --ssl / -S use SSL\n"); |
40 printf(" --ssl / -S use SSL\n"); |
45 printf(" --log-database database connection string for logging\n"); |
41 printf(" --log-database database connection string for logging\n"); |
46 printf(" --log-channel channel to log\n"); |
42 printf(" --log-channel channel to log\n"); |
47 } |
43 } |
48 |
44 |
49 /** |
|
50 * Context for async nexus operation |
|
51 */ |
|
52 struct nexus_ctx { |
|
53 /** The libevent base */ |
|
54 struct event_base *ev_base; |
|
55 |
|
56 /** The one IRC network */ |
|
57 struct irc_net *net; |
|
58 }; |
|
59 |
|
60 void on_sigint (evutil_socket_t sig, short what, void *arg) |
45 void on_sigint (evutil_socket_t sig, short what, void *arg) |
61 { |
46 { |
62 struct nexus_ctx *ctx = arg; |
47 struct nexus_ctx *ctx = arg; |
63 |
48 |
64 (void) sig; |
49 (void) sig; |
65 (void) what; |
50 (void) what; |
66 |
51 |
67 if (ctx->net && ctx->net->conn && !ctx->net->conn->quitting) { |
52 if (ctx->client && !ctx->client->quitting) { |
68 log_info("Quitting..."); |
53 log_info("Quitting..."); |
69 |
54 |
70 // quit it |
55 // quit it |
71 irc_net_quit(ctx->net, "Goodbye, cruel world ;("); |
56 irc_client_quit(ctx->client, "Goodbye, cruel world ;("); |
72 |
57 |
73 } else { |
58 } else { |
74 log_error("Aborting"); |
59 log_error("Aborting"); |
75 |
60 |
76 // die |
61 // die |
77 if (ctx->net) { |
62 if (ctx->client) { |
78 irc_net_destroy(ctx->net); |
63 irc_client_destroy(ctx->client); |
79 ctx->net = NULL; |
64 ctx->client = NULL; |
80 } |
65 } |
81 |
66 |
82 // exit |
67 // exit |
83 event_base_loopexit(ctx->ev_base, NULL); |
68 event_base_loopexit(ctx->ev_base, NULL); |
84 } |
69 } |
85 } |
70 } |
86 |
71 |
87 int main (int argc, char **argv) |
72 int main (int argc, char **argv) |
88 { |
73 { |
89 int opt, option_index; |
74 int opt, option_index; |
|
75 struct signals *signals; |
90 struct nexus_ctx ctx; |
76 struct nexus_ctx ctx; |
91 struct signals *signals; |
77 struct irc_net *net; |
92 struct error_info err; |
78 struct error_info err; |
93 |
79 |
94 struct irc_net_info net_info = { |
80 struct irc_net_info net_info = { |
95 .network = NULL, |
81 .network = NULL, |
96 .hostname = DEFAULT_HOST, |
82 .hostname = DEFAULT_HOST, |
162 |
148 |
163 // initialize sock module |
149 // initialize sock module |
164 if (sock_init(ctx.ev_base, &err)) |
150 if (sock_init(ctx.ev_base, &err)) |
165 FATAL_ERROR(&err, "sock_init"); |
151 FATAL_ERROR(&err, "sock_init"); |
166 |
152 |
|
153 // the IRC client |
|
154 if (irc_client_create(&ctx.client, &err)) |
|
155 FATAL_ERROR(&err, "irc_client_create"); |
|
156 |
167 // the IRC network |
157 // the IRC network |
168 if (irc_net_create(&ctx.net, &net_info, &err)) |
158 if (irc_client_add_net(ctx.client, &net, &net_info)) |
169 FATAL_ERROR(&err, "irc_net_create"); |
159 FATAL_ERR(ERROR_CODE(&err), "irc_client_add_net"); |
170 |
160 |
171 // add our signal handlers |
161 // add our signal handlers |
172 if ( |
162 if ( |
173 (ERROR_CODE(&err) = signals_add(signals, SIGPIPE, &signals_ignore, signals)) |
163 (ERROR_CODE(&err) = signals_add(signals, SIGPIPE, &signals_ignore, signals)) |
174 || (ERROR_CODE(&err) = signals_add(signals, SIGINT, &on_sigint, &ctx)) |
164 || (ERROR_CODE(&err) = signals_add(signals, SIGINT, &on_sigint, &ctx)) |
176 FATAL_ERROR(&err, "signals_add"); |
166 FATAL_ERROR(&err, "signals_add"); |
177 |
167 |
178 // logging? |
168 // logging? |
179 if (log_info.db_info || log_chan_info.channel) { |
169 if (log_info.db_info || log_chan_info.channel) { |
180 // get the channel |
170 // get the channel |
181 if (log_chan_info.channel && (log_info.channel = irc_net_add_chan(ctx.net, &log_chan_info)) == NULL) |
171 if (log_chan_info.channel && (log_info.channel = irc_net_add_chan(net, &log_chan_info)) == NULL) |
182 FATAL("irc_net_add_chan"); |
172 FATAL("irc_net_add_chan"); |
183 |
173 |
184 // init the irc_log module |
174 // init the irc_log module |
185 if ((ERROR_CODE(&err) = irc_log_init(ctx.ev_base, &log_info))) |
175 if ((ERROR_CODE(&err) = irc_log_init(ctx.ev_base, &log_info))) |
186 FATAL_ERROR(&err, "irc_log_init"); |
176 FATAL_ERROR(&err, "irc_log_init"); |