00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef lint
00021 static const char rcsid[] = "$Id: events.c,v 1.14 2007-11-06 00:05:40 sven Exp $";
00022 #endif
00023
00024 #include <eggdrop/eggdrop.h>
00025
00026 #include "oldbotnet.h"
00027
00028
00029 static int on_bcast(void *client_data, botnet_entity_t *src, const char *text, int len);
00030 static int on_privmsg(void *client_data, botnet_entity_t *src, partymember_t *dst, const char *text, int len);
00031 static int on_nick(void *client_data, partymember_t *src, const char *oldnick);
00032 static int on_quit(void *client_data, partymember_t *src, const char *text, int len);
00033 static int on_chanmsg(void *client_data, partychan_t *chan, botnet_entity_t *src, const char *text, int len);
00034 static int on_join(void *client_data, partychan_t *chan, partymember_t *src, int linking);
00035 static int on_part(void *client_data, partychan_t *chan, partymember_t *src, const char *reason, int len);
00036 static int on_new_bot(void *client_data, botnet_bot_t *bot, int linking);
00037 static int on_lost_bot(void *client_data, botnet_bot_t *bot, const char *reason);
00038 static int on_link(void *client_data, botnet_entity_t *from, botnet_bot_t *dst, const char *target);
00039 static int on_unlink(void *client_data, botnet_entity_t *from, botnet_bot_t *bot, const char *reason);
00040 static int on_botmsg(void *client_data, botnet_bot_t *src, botnet_bot_t *dst, const char *command, const char *text, int len);
00041 static int on_botbroadcast(void *client_data, botnet_bot_t *src, const char *command, const char *text, int len);
00042 static int on_extension(void *client_data, botnet_entity_t *src, botnet_bot_t *dst, const char *cmd, const char *text, int len);
00043
00044 botnet_handler_t bothandler = {
00045 NULL, on_bcast, on_privmsg, on_nick, on_quit,
00046 on_chanmsg, on_join, on_part,
00047 on_new_bot, on_lost_bot, on_link, on_unlink, on_botmsg, on_botbroadcast, on_extension
00048 };
00049
00050 static const char *filter(const char *text)
00051 {
00052 char *p;
00053 static char buf[512] = {0};
00054
00055 if (!strchr(text, '\n')) return text;
00056
00057 strncpy(buf, text, 511);
00058 p = strchr(buf, '\n');
00059 if (p) *p = 0;
00060 return buf;
00061 }
00062
00077 static int on_bcast(void *client_data, botnet_entity_t *src, const char *text, int len)
00078 {
00079 const char *name;
00080 oldbotnet_t *obot = client_data;
00081
00082 if (src->what == ENTITY_BOT) name = entity_full_name(src);
00083 else if (src->user->bot) name = src->user->bot->name;
00084 else name = botnet_get_name();
00085
00086 egg_iprintf(obot->idx, "ct %s %s\n", name, filter(text));
00087
00088 return 0;
00089 }
00090
00091 static int on_privmsg(void *client_data, botnet_entity_t *src, partymember_t *dst, const char *text, int len)
00092 {
00093 oldbotnet_t *obot = client_data;
00094
00095 egg_iprintf(obot->idx, "p %s %s %s\n", entity_full_id_name(src), dst->full_id_name, filter(text));
00096
00097 return 0;
00098 }
00099
00100 static int on_quit(void *client_data, partymember_t *src, const char *text, int len)
00101 {
00102 oldbotnet_t *obot = client_data;
00103
00104 egg_iprintf(obot->idx, "pt %s %s %s\n", src->bot ? src->bot->name : botnet_get_name(), src->nick, b64enc_int(src->id));
00105
00106 return 0;
00107 }
00108
00109 static int on_nick(void *client_data, partymember_t *src, const char *oldnick)
00110 {
00111 oldbotnet_t *obot = client_data;
00112
00113 egg_iprintf(obot->idx, "nc %s %s %s\n", src->bot ? src->bot->name : botnet_get_name(), b64enc_int(src->id), src->nick);
00114
00115 return 0;
00116 }
00117
00118 static int on_chanmsg(void *client_data, partychan_t *chan, botnet_entity_t *src, const char *t, int len)
00119 {
00120 const char *text = filter(t);
00121 oldbotnet_t *obot = client_data;
00122
00123 if (len >= 9 && !strncasecmp(text, "\1ACTION ", 8) && text[len - 1] == 1) {
00124 egg_iprintf(obot->idx, "a %s %s %.*s\n", entity_full_name(src), b64enc_int(assoc_get_id(chan->name)), len - 9, text + 8);
00125 } else {
00126 egg_iprintf(obot->idx, "c %s %s %s\n", entity_full_name(src), b64enc_int(assoc_get_id(chan->name)), text);
00127 }
00128 return 0;
00129 }
00130
00131 static int on_join(void *client_data, partychan_t *chan, partymember_t *src, int linking)
00132 {
00133 char *cid;
00134 oldbotnet_t *obot = client_data;
00135
00136 cid = strdup(b64enc_int(assoc_get_id(chan->name)));
00137 egg_iprintf(obot->idx, "j %s%s %s %s %c%s %s@%s\n", linking ? "!" : "", src->bot ? src->bot->name : botnet_get_name(), src->nick, cid, '*', b64enc_int(src->id), src->ident, src->host);
00138 free(cid);
00139
00140 return 0;
00141 }
00142
00143 static int on_part(void *client_data, partychan_t *chan, partymember_t *src, const char *reason, int len)
00144 {
00145 oldbotnet_t *obot = client_data;
00146
00147 if (!src->nchannels) {
00148 if (reason && *reason) egg_iprintf(obot->idx, "pt %s %s %s %s\n", src->bot ? src->bot->name : botnet_get_name(), src->nick, b64enc_int(src->id), filter(reason));
00149 else egg_iprintf(obot->idx, "pt %s %s %s\n", src->bot ? src->bot->name : botnet_get_name(), src->nick, b64enc_int(src->id));
00150 } else {
00151 char *cid;
00152
00153 cid = strdup(b64enc_int(assoc_get_id(src->channels[src->nchannels - 1]->name)));
00154 egg_iprintf(obot->idx, "j %s %s %s %c%s %s@%s\n", src->bot ? src->bot->name : botnet_get_name(), src->nick, cid, '*', b64enc_int(src->id), src->ident, src->host);
00155 free(cid);
00156 }
00157
00158 return 0;
00159 }
00160
00161 static int on_new_bot(void *client_data, botnet_bot_t *bot, int linking)
00162 {
00163 int version = 1090000;
00164 const char *verstr;
00165 oldbotnet_t *obot = client_data;
00166
00167 verstr = botnet_get_info(bot, "numversion");
00168 if (verstr) version = atoi(verstr);
00169 egg_iprintf(obot->idx, "n %s %s %c%s\n", bot->name, bot->uplink ? bot->uplink->name : botnet_get_name(), linking ? '-' : '!', b64enc_int(version));
00170
00171 return 0;
00172 }
00173
00174 static int on_lost_bot(void *client_data, botnet_bot_t *bot, const char *reason)
00175 {
00176 oldbotnet_t *obot = client_data;
00177
00178 if (bot == obot->bot) egg_iprintf(obot->idx, "bye %s\n", filter(reason));
00179 else egg_iprintf(obot->idx, "un %s %s\n", bot->name, filter(reason));
00180
00181 return 0;
00182 }
00183
00184 static int on_link(void *client_data, botnet_entity_t *from, struct botnet_bot *dst, const char *target)
00185 {
00186 oldbotnet_t *obot = client_data;
00187
00188 egg_iprintf(obot->idx, "l %s %s %s\n", entity_full_id_name(from), dst->name, target);
00189
00190 return 0;
00191 }
00192
00193 static int on_unlink(void *client_data, botnet_entity_t *from, struct botnet_bot *bot, const char *reason)
00194 {
00195 oldbotnet_t *obot = client_data;
00196
00197 egg_iprintf(obot->idx, "ul %s %s %s %s\n", entity_full_id_name(from), bot->uplink->name, bot->name, filter(reason));
00198
00199 return 0;
00200 }
00201
00202 static int on_botbroadcast(void *client_data, botnet_bot_t *src, const char *command, const char *text, int len)
00203 {
00204 const char *srcname = src ? src->name : botnet_get_name();
00205 oldbotnet_t *obot = client_data;
00206
00207 if (text && len > 0) egg_iprintf(obot->idx, "z %s %s %s\n", srcname, command, filter(text));
00208 else egg_iprintf(obot->idx, "z %s %s\n", srcname, command);
00209
00210 return 0;
00211 }
00212
00213 static int on_botmsg(void *client_data, botnet_bot_t *src, botnet_bot_t *dst, const char *command, const char *text, int len)
00214 {
00215 const char *srcname = src ? src->name : botnet_get_name();
00216 oldbotnet_t *obot = client_data;
00217
00218 if (text && len > 0) egg_iprintf(obot->idx, "zb %s %s %s %s\n", srcname, dst->name, command, filter(text));
00219 else egg_iprintf(obot->idx, "zb %s %s %s\n", srcname, dst->name, command);
00220
00221 return 0;
00222 }
00223
00224 static int on_extension(void *client_data, botnet_entity_t *src, botnet_bot_t *dst, const char *cmd, const char *t, int len)
00225 {
00226 const char *text = filter(t);
00227 oldbotnet_t *obot = client_data;
00228
00229 if (!strcmp(cmd, "note")) {
00230 egg_iprintf(obot->idx, "p %s %s\n", entity_full_id_name(src), text);
00231 } else if (!strcmp(cmd, "who")) {
00232 egg_iprintf(obot->idx, "w %s %s %s\n", entity_full_id_name(src), dst->name, text);
00233 } else if (!strcmp(cmd, "motd")) {
00234 egg_iprintf(obot->idx, "m %s %s\n", entity_full_id_name(src), dst->name);
00235 } else if (!strcmp(cmd, "versions") && src->what == ENTITY_PARTYMEMBER) {
00236 egg_iprintf(obot->idx, "v %s %s %d:%s\n", src->user->bot->name, dst->name, src->user->id, src->user->nick);
00237 } else if (!strcmp(cmd, "away") && src->what == ENTITY_PARTYMEMBER) {
00238 if (*text) egg_iprintf(obot->idx, "aw %s %s %s\n", src->user->bot ? src->user->bot->name : botnet_get_name(), b64enc_int(src->user->id), text);
00239 else egg_iprintf(obot->idx, "aw %s %s\n", src->user->bot ? src->user->bot->name : botnet_get_name(), b64enc_int(src->user->id));
00240 }
00241
00242 return 0;
00243 }