void subuser_restor(subuser *sub, acetables *g_ape) { CHANLIST *chanl; CHANNEL *chan; json_item *jlist; RAW *newraw; USERS *user = sub->user; userslist *ulist; chanl = user->chan_foot; while (chanl != NULL) { chan = chanl->chaninfo; /* * quiet channel won't be posted on subuser_restor */ if (!(chan->flags & CHANNEL_QUIET)) { jlist = json_new_object(); if ( !(chan->flags & CHANNEL_NONINTERACTIVE) && chan->head != NULL ) { json_item *user_list = json_new_array(); ulist = chan->head; while (ulist != NULL) { json_item *juser = get_json_object_user(ulist->userinfo); if (ulist->userinfo != user) { //make_link(user, ulist->userinfo); } json_set_property_intN(juser, "level", 5, ulist->level); json_set_element_obj(user_list, juser); ulist = ulist->next; } json_set_property_objN(jlist, "users", 5, user_list); } json_set_property_objN(jlist, "pipe", 4, get_json_object_channel(chan)); newraw = forge_raw(RAW_CHANNEL, jlist); newraw->priority = RAW_PRI_HI; post_raw_sub(newraw, sub, g_ape); POSTRAW_DONE(newraw); } chanl = chanl->next; } jlist = json_new_object(); json_set_property_objN(jlist, "user", 4, get_json_object_user(user)); newraw = forge_raw("IDENT", jlist); newraw->priority = RAW_PRI_HI; post_raw_sub(newraw, sub, g_ape); POSTRAW_DONE(newraw); }
void ban(CHANNEL *chan, USERS *banner, const char *ip, char *reason, unsigned int expire, acetables *g_ape) // Ban IP { userslist *uTmp, *tUtmp; RAW *newraw; json_item *jlist; BANNED *blist, *bTmp; unsigned int isban = 0; long int nextime = (expire * 60)+time(NULL); // NOW ! if (chan == NULL) { return; } uTmp = chan->head; bTmp = chan->banned; while (uTmp != NULL) { if (strcmp(ip, uTmp->userinfo->ip) == 0) { // We find somebody with the same IP jlist = json_new_object(); json_set_property_strZ(jlist, "reason", reason); json_set_property_objN(jlist, "banner", 6, get_json_object_user(banner)); json_set_property_objN(jlist, "pipe", 4, get_json_object_channel(chan)); newraw = forge_raw(RAW_BAN, jlist); post_raw(newraw, uTmp->userinfo, g_ape); if (isban == 0) { blist = xmalloc(sizeof(*blist)); memset(blist->reason, 0, 256); strncpy(blist->ip, ip, 16); strncpy(blist->reason, reason, 255); blist->expire = nextime; blist->next = bTmp; chan->banned = blist; isban = 1; } tUtmp = uTmp->next; left(uTmp->userinfo, chan, g_ape); // if the user is the last : "chan" is free (rmchan()) uTmp = tUtmp; continue; } uTmp = uTmp->next; } }
RAW *forge_raw(const char *raw, json_item *jlist) { RAW *new_raw; char unixtime[16]; struct jsontring *string; json_item *jstruct = NULL; sprintf(unixtime, "%li", time(NULL)); jstruct = json_new_object(); json_set_property_strN(jstruct, "time", 4, unixtime, strlen(unixtime)); json_set_property_strN(jstruct, "raw", 3, raw, strlen(raw)); json_set_property_objN(jstruct, "data", 4, jlist); string = json_to_string(jstruct, NULL, 1); new_raw = xmalloc(sizeof(*new_raw)); new_raw->len = string->len; new_raw->next = NULL; new_raw->priority = RAW_PRI_LO; new_raw->refcount = 0; new_raw->data = string->jstring; free(string); return new_raw; }
unsigned int cmd_connect(callbackp *callbacki) { USERS *nuser; RAW *newraw; json_item *jstr = NULL; nuser = adduser(NULL, NULL, NULL, callbacki->call_user, callbacki->g_ape); callbacki->call_user = nuser; jstr = json_new_object(); json_set_property_objN(jstr, "user", 4, get_json_object_user(callbacki->call_user)); newraw = forge_raw("IDENT", jstr); newraw->priority = RAW_PRI_HI; post_raw_sub(newraw, callbacki->call_subuser, callbacki->g_ape); jstr = json_new_object(); json_set_property_strN(jstr, "sessid", 6, nuser->sessid, 32); newraw = forge_raw(RAW_LOGIN, jstr); newraw->priority = RAW_PRI_HI; post_raw(newraw, nuser, callbacki->g_ape); return (RETURN_NOTHING); }
json_item *get_json_object_channel(CHANNEL *chan) { json_item *jstr = json_new_object(); json_set_property_strN(jstr, "casttype", 8, "multi", 5); json_set_property_strN(jstr, "pubid", 5, chan->pipe->pubid, 32); json_item *jprop = json_new_object(); json_set_property_strZ(jprop, "name", chan->name); extend *eTmp = chan->properties; while (eTmp != NULL) { if (eTmp->visibility == EXTEND_ISPUBLIC) { if (eTmp->type == EXTEND_JSON) { /*json *jcopy = json_copy(eTmp->val); set_json(eTmp->key, NULL, &jprop); json_attach(jprop, jcopy, JSON_OBJECT);*/ } else { json_set_property_strZ(jprop, eTmp->key, eTmp->val); } } eTmp = eTmp->next; } json_set_property_objN(jstr, "properties", 10, jprop); //} return jstr; }
int post_to_pipe(json_item *jlist, const char *rawname, const char *pipe, subuser *from, acetables *g_ape) { USERS *sender = from->user; transpipe *recver = get_pipe_strict(pipe, sender, g_ape); json_item *jlist_copy = NULL; RAW *newraw; if (sender != NULL) { if (recver == NULL) { send_error(sender, "UNKNOWN_PIPE", "109", g_ape); return 0; } json_set_property_objN(jlist, "from", 4, get_json_object_user(sender)); } if (sender != NULL && sender->nsub > 1) { jlist_copy = json_item_copy(jlist, NULL); json_set_property_objN(jlist_copy, "pipe", 4, get_json_object_pipe(recver)); newraw = forge_raw(rawname, jlist_copy); post_raw_restricted(newraw, sender, from, g_ape); } switch(recver->type) { case USER_PIPE: json_set_property_objN(jlist, "pipe", 4, get_json_object_user(sender)); newraw = forge_raw(rawname, jlist); post_raw(newraw, recver->pipe, g_ape); break; case CHANNEL_PIPE: if (((CHANNEL*)recver->pipe)->head != NULL && ((CHANNEL*)recver->pipe)->head->next != NULL) { json_set_property_objN(jlist, "pipe", 4, get_json_object_channel(recver->pipe)); newraw = forge_raw(rawname, jlist); post_raw_channel_restricted(newraw, recver->pipe, sender, g_ape); } break; case CUSTOM_PIPE: json_set_property_objN(jlist, "pipe", 4, get_json_object_user(sender)); post_json_custom(jlist, sender, recver, g_ape); break; default: break; } return 1; }
void proxy_onevent(ape_proxy *proxy, char *event, acetables *g_ape) { RAW *newraw; json_item *jlist = json_new_object(); json_set_property_strZ(jlist, "event", event); json_set_property_objN(jlist, "pipe", 4, get_json_object_proxy(proxy)); newraw = forge_raw("PROXY_EVENT", jlist); proxy_post_raw(newraw, proxy, g_ape); }
json_item *get_json_object_user(USERS *user) { json_item *jstr = NULL; if (user != NULL) { jstr = json_new_object(); json_set_property_strN(jstr, "casttype", 8, "uni", 3); json_set_property_strN(jstr, "pubid", 5, user->pipe->pubid, 32); if (user->properties != NULL) { int has_prop = 0; json_item *jprop = NULL; extend *eTmp = user->properties; while (eTmp != NULL) { if (eTmp->visibility == EXTEND_ISPUBLIC) { if (!has_prop) { has_prop = 1; jprop = json_new_object(); } if (eTmp->type == EXTEND_JSON) { json_item *jcopy = json_item_copy(eTmp->val, NULL); json_set_property_objZ(jprop, eTmp->key, jcopy); } else { json_set_property_strZ(jprop, eTmp->key, eTmp->val); } } eTmp = eTmp->next; } if (has_prop) { json_set_property_objN(jstr, "properties", 10, jprop); } } } else { json_set_property_strZ(jstr, "pubid", SERVER_NAME); } return jstr; }
// TODO : Rewrite this f***g ugly function unsigned int setlevel(USERS *user_actif, USERS *user_passif, CHANNEL *chan, unsigned int lvl, acetables *g_ape) { RAW *newraw; userslist *user_passif_chan, *user_actif_chan; json_item *jlist; user_passif_chan = getuchan(user_passif, chan); if (user_actif != NULL) { user_actif_chan = getuchan(user_actif, chan); if (user_passif_chan == NULL || user_actif_chan == NULL || ((user_actif_chan->level < lvl || user_actif_chan->level < user_passif_chan->level) && !(user_actif->flags & FLG_AUTOOP)) || lvl < 1 || lvl > 32) { send_error(user_actif, "SETLEVEL_ERROR", "110", g_ape); return 0; } user_passif_chan->level = lvl; if (chan->interactive) { jlist = json_new_object(); json_set_property_objN(jlist, "ope", 3, get_json_object_user(user_passif)); json_set_property_objN(jlist, "oper", 4, get_json_object_user(user_actif)); json_set_property_objN(jlist, "pipe", 4, get_json_object_channel(chan)); json_set_property_intN(jlist, "level", 5, lvl); newraw = forge_raw(RAW_SETLEVEL, jlist); post_raw_channel(newraw, chan, g_ape); } return 1; } else if (user_passif_chan != NULL && lvl > 0 && lvl < 32) { user_passif_chan->level = lvl; if (chan->interactive) { jlist = json_new_object(); json_set_property_objN(jlist, "ope", 3, get_json_object_user(user_passif)); json_set_property_objN(jlist, "oper", 4, get_json_object_user(NULL)); json_set_property_objN(jlist, "pipe", 4, get_json_object_channel(chan)); json_set_property_intN(jlist, "level", 5, lvl); newraw = forge_raw(RAW_SETLEVEL, jlist); post_raw_channel(newraw, chan, g_ape); } return 1; } return 0; }
json_item *get_json_object_proxy(ape_proxy *proxy) { json_item *jstr = json_new_object(); json_item *jprop = json_new_object(); extend *eTmp = proxy->properties; json_set_property_strN(jstr, "pubid", 5, proxy->pipe->pubid, 32); json_set_property_strN(jstr, "casttype", 8, "proxy", 5); json_set_property_strZ(jprop, "host", proxy->sock.host->host); json_set_property_strZ(jprop, "ip", proxy->sock.host->ip); json_set_property_intZ(jprop, "port", proxy->sock.port); while (eTmp != NULL) { json_set_property_strZ(jprop, eTmp->key, eTmp->val); eTmp = eTmp->next; } json_set_property_objN(jstr, "properties", 10, jprop); return jstr; }
void proxy_process_eol(ape_socket *co, acetables *g_ape) { char *b64; ape_proxy *proxy = co->attach; char *data = co->buffer_in.data; RAW *newraw; json_item *jlist = json_new_object(); data[co->buffer_in.length] = '\0'; b64 = base64_encode(data, strlen(data)); json_set_property_strZ(jlist, "data", b64); json_set_property_strN(jlist, "event", 5, "READ", 4); json_set_property_objN(jlist, "pipe", 4, get_json_object_proxy(proxy)); newraw = forge_raw("PROXY_EVENT", jlist); proxy_post_raw(newraw, proxy, g_ape); free(b64); }
void sendback_session(USERS *user, session *sess, acetables *g_ape) { subuser *current = user->subuser; while (current != NULL) { if (current->need_update) { json_item *jlist = json_new_object(), *jobj_item = json_new_object(); RAW *newraw; current->need_update = 0; json_set_property_strZ(jobj_item, sess->key, sess->val); json_set_property_objN(jlist, "sessions", 8, jobj_item); newraw = forge_raw("SESSIONS", jlist); newraw->priority = RAW_PRI_HI; post_raw_sub(copy_raw_z(newraw), current, g_ape); } current = current->next; } }
void left(USERS *user, CHANNEL *chan, acetables *g_ape) // Vider la liste chainée de l'user { userslist *list, *prev; CHANLIST *clist, *ctmp; RAW *newraw; json_item *jlist; FIRE_EVENT_NULL(left, user, chan, g_ape); if (!isonchannel(user, chan)) { return; } list = chan->head; prev = NULL; clist = user->chan_foot; ctmp = NULL; while (clist != NULL) { if (clist->chaninfo == chan) { if (ctmp != NULL) { ctmp->next = clist->next; } else { user->chan_foot = clist->next; } free(clist); break; } ctmp = clist; clist = clist->next; } while (list != NULL && list->userinfo != NULL) { if (list->userinfo == user) { if (prev != NULL) { prev->next = list->next; } else { chan->head = list->next; } free(list); list = NULL; if (chan->head != NULL && chan->interactive) { jlist = json_new_object(); json_set_property_objN(jlist, "user", 4, get_json_object_user(user)); json_set_property_objN(jlist, "pipe", 4, get_json_object_channel(chan)); newraw = forge_raw(RAW_LEFT, jlist); post_raw_channel(newraw, chan, g_ape); } else if (chan->head == NULL) { rmchan(chan, g_ape); } break; } prev = list; list = list->next; } }
void join(USERS *user, CHANNEL *chan, acetables *g_ape) { userslist *list, *ulist; CHANLIST *chanl; FIRE_EVENT_NULL(join, user, chan, g_ape); RAW *newraw; json_item *jlist = json_new_object(); if (isonchannel(user, chan)) { return; } list = xmalloc(sizeof(*list)); // TODO is it free ? list->userinfo = user; list->level = 1; list->next = chan->head; chan->head = list; chanl = xmalloc(sizeof(*chanl)); // TODO is it free ? chanl->chaninfo = chan; chanl->next = user->chan_foot; user->chan_foot = chanl; if (chan->interactive) { json_item *user_list = json_new_array(); json_item *uinfo = json_new_object(); json_set_property_objN(uinfo, "user", 4, get_json_object_user(user)); json_set_property_objN(uinfo, "pipe", 4, get_json_object_channel(chan)); newraw = forge_raw(RAW_JOIN, uinfo); post_raw_channel_restricted(newraw, chan, user, g_ape); ulist = chan->head; while (ulist != NULL) { json_item *juser = get_json_object_user(ulist->userinfo); if (ulist->userinfo != user) { //make_link(user, ulist->userinfo); } json_set_property_intN(juser, "level", 5, ulist->level); json_set_element_obj(user_list, juser); ulist = ulist->next; } json_set_property_objN(jlist, "users", 5, user_list); } json_set_property_objN(jlist, "pipe", 4, get_json_object_channel(chan)); newraw = forge_raw(RAW_CHANNEL, jlist); post_raw(newraw, user, g_ape); #if 0 if (user->flags & FLG_AUTOOP) { setlevel(NULL, user, chan, 3); } #endif }
void json_set_element_obj(json_item *obj, json_item *value) { json_set_property_objN(obj, NULL, 0, value); }
void json_set_property_objZ(json_item *obj, const char *key, json_item *value) { json_set_property_objN(obj, key, strlen(key), value); }