void SlackRTM::handlePayloadReceived(const std::string &payload) { Json::Value d; Json::CharReaderBuilder rbuilder; std::unique_ptr<Json::CharReader> const reader(rbuilder.newCharReader()); if (!reader->parse(payload.c_str(), payload.c_str() + payload.size(), &d, nullptr)) { LOG4CXX_ERROR(logger, "Error while parsing JSON"); LOG4CXX_ERROR(logger, payload); return; } STORE_STRING(d, type); if (type == "message") { STORE_STRING(d, channel); STORE_STRING(d, text); STORE_STRING(d, ts); STORE_STRING_OPTIONAL(d, subtype); STORE_STRING_OPTIONAL(d, purpose); Json::Value &attachments = d["attachments"]; if (attachments.isArray()) { for (unsigned i = 0; i < attachments.size(); i++) { STORE_STRING_OPTIONAL(attachments[i], fallback); if (!fallback.empty()) { text += fallback; } } } if (subtype == "bot_message") { STORE_STRING(d, bot_id); onMessageReceived(channel, bot_id, text, ts); } else if (subtype == "me_message") { text = "/me " + text; STORE_STRING(d, user); onMessageReceived(channel, user, text, ts); } else if (subtype == "channel_join") { } else if (!purpose.empty()) { } else { STORE_STRING(d, user); onMessageReceived(channel, user, text, ts); } } else if (type == "channel_joined" || type == "channel_created") { std::map<std::string, SlackChannelInfo> &channels = m_idManager->getChannels(); SlackAPI::getSlackChannelInfo(NULL, true, d, payload, channels); } else if (type == "error") { GET_OBJECT(d, error); STORE_INT(error, code); if (code == 1) { LOG4CXX_INFO(logger, "Reconnecting to Slack network"); m_pingTimer->stop(); m_client->disconnectServer(); start(); } } }
int main(int argc, char *argv[]) { static XtAppContext appContext; IceConn iceConn; IceProtocolSetupStatus setupstat; char *vendor = NULL; char *release = NULL; pmGetProxyAddrMsg *pMsg; char *pData; int i; size_t len; IceReplyWaitInfo replyWait; GetProxyAddrReply reply; int majorVersion, minorVersion; Bool gotReply, ioErrorOccured; char errorString[255]; char *serviceName = NULL, *serverAddress = NULL; char *hostAddress = NULL, *startOptions = NULL; char *managerAddress = NULL; Bool haveAuth = 0; char authName[40]; char authData[128]; char *authDataBinary = NULL; int authLen = 0; for (i = 1; i < argc; i++) { if (argv[i][0] == '-') { switch (argv[i][1]) { case 'a': /* -auth */ haveAuth = 1; continue; case 'm': /* -manager */ if (++i >= argc) goto usage; managerAddress = (char *) XtNewString (argv[i]); continue; case 's': /* -server */ if (++i >= argc) goto usage; serverAddress = (char *) XtNewString (argv[i]); continue; case 'n': /* -name */ if (++i >= argc) goto usage; serviceName = XtNewString (argv[i]); continue; case 'h': /* -host */ if (++i >= argc) goto usage; hostAddress = XtNewString (argv[i]); continue; case 'o': /* -options */ if (++i >= argc) goto usage; startOptions = XtNewString (argv[i]); continue; case 'v': puts(PACKAGE_STRING); exit(0); } } usage: if (i >= argc) fprintf (stderr, "%s: %s requires an argument\n", argv[0], argv[i-1]); else fprintf (stderr, "%s: unrecognized argument '%s'\n", argv[0], argv[i]); usage(); } if (serviceName == NULL) { fprintf (stderr, "%s: -name serviceName must be specified\n", argv[0]); usage(); } if (serverAddress == NULL) { fprintf (stderr, "%s: -server serverAddr must be specified\n", argv[0]); usage(); } if (managerAddress == NULL) { managerAddress = getenv("PROXY_MANAGER"); if (managerAddress == NULL) { fprintf (stderr, "Error: -manager option must be specified when PROXY_MANAGER is not in the environment\n"); exit (1); } } /* * Register support for PROXY_MANAGEMENT. */ if ((PMopcode = IceRegisterForProtocolSetup ( PM_PROTOCOL_NAME, "XC", "1.0", PMversionCount, PMversions, 0, /* authcount */ NULL, /* authnames */ NULL, /* authprocs */ NULL /* IceIOErrorProc */ )) < 0) { fprintf (stderr, "Could not register PROXY_MANAGEMENT protocol with ICE"); exit (1); } appContext = XtCreateApplicationContext (); InitWatchProcs (appContext); if ((iceConn = IceOpenConnection ( managerAddress, NULL, 0, 0, 256, errorString)) == NULL) { fprintf (stderr, "Could not open ICE connection to proxy manager: %s", errorString); exit (1); } setupstat = IceProtocolSetup (iceConn, PMopcode, NULL, False /* mustAuthenticate */, &majorVersion, &minorVersion, &vendor, &release, 256, errorString); if (setupstat != IceProtocolSetupSuccess) { IceCloseConnection (iceConn); fprintf (stderr, "Could not initialize proxy management protocol: %s\n", errorString); exit (1); } /* * If auth data is supplied, read it from stdin. */ if (haveAuth) { fgets (authName, sizeof (authName), stdin); fgets (authData, sizeof (authData), stdin); for (i = 0; i < strlen (authName); i++) if (authName[i] == '\n') { authName[i] = '\0'; break; } for (i = 0; i < strlen (authData); i++) if (authData[i] == '\n') { authData[i] = '\0'; break; } /* * Convert the hex auth data to binary. */ authLen = cvthexkey (authData, &authDataBinary); if (authLen == -1) { fprintf (stderr, "Could not convert hex auth data to binary\n"); exit (1); } } /* * Now send the GetProxyAddr request. */ len = STRING_BYTES (serviceName) + STRING_BYTES (serverAddress) + STRING_BYTES (hostAddress) + STRING_BYTES (startOptions) + (authLen > 0 ? (STRING_BYTES (authName) + authLen) : 0); IceGetHeaderExtra (iceConn, PMopcode, PM_GetProxyAddr, SIZEOF (pmGetProxyAddrMsg), WORD64COUNT (len), pmGetProxyAddrMsg, pMsg, pData); pMsg->authLen = (CARD16) authLen; STORE_STRING (pData, serviceName); STORE_STRING (pData, serverAddress); STORE_STRING (pData, hostAddress); STORE_STRING (pData, startOptions); if (authLen > 0) { STORE_STRING (pData, authName); memcpy (pData, authDataBinary, authLen); } IceFlush (iceConn); replyWait.sequence_of_request = IceLastSentSequenceNumber (iceConn); replyWait.major_opcode_of_request = PMopcode; replyWait.minor_opcode_of_request = PM_GetProxyAddr; replyWait.reply = (IcePointer) &reply; gotReply = False; ioErrorOccured = False; while (!gotReply && !ioErrorOccured) { ioErrorOccured = (IceProcessMessages ( iceConn, &replyWait, &gotReply) == IceProcessMessagesIOError); if (ioErrorOccured) { fprintf (stderr, "IO error occured\n"); exit (1); } else if (gotReply) { if (reply.status == PM_Success) { fprintf (stdout, "%s\n", reply.addr); exit (0); } else { fprintf (stderr, "Error from proxy manager: %s\n", reply.error); exit (1); } } } /*NOTREACHED*/ exit(0); }
void FWPprocessMessages( IceConn iceConn, IcePointer * client_data, int opcode, unsigned long length, Bool swap) { switch (opcode) { /* * this is really the only opcode we care about -- the one * which indicates an XFindProxy request for a connection * to a specified server */ case PM_GetProxyAddr: { pmGetProxyAddrMsg *pMsg; char *pData, *pStart; char *serviceName = NULL, *serverAddress = NULL; char *hostAddress = NULL, *startOptions = NULL; char *authName = NULL, *authData = NULL; int authLen; struct clientDataStruct * program_data; char * listen_port_string; int pm_send_msg_len; pmGetProxyAddrReplyMsg * pReply; char * pReplyData; struct hostent * hostptr; struct sockaddr_in server_sockaddr_in; struct sockaddr_in dummy_sockaddr_in; char * server_name_base; char * config_failure = "unrecognized server or permission denied"; char * tmp_str; int rule_number = -1; char * colon; char * tmpAddress = NULL; /* * this is where we need and get access to that client data we * went through such contortions to set up earlier! */ program_data = (struct clientDataStruct *) client_data; /* * initial check on expected message size */ CHECK_AT_LEAST_SIZE (iceConn, global_data.major_opcode, opcode, length, SIZEOF (pmGetProxyAddrMsg), IceFatalToProtocol); IceReadCompleteMessage (iceConn, SIZEOF (pmGetProxyAddrMsg), pmGetProxyAddrMsg, pMsg, pStart); if (!IceValidIO (iceConn)) { IceDisposeCompleteMessage (iceConn, pStart); return; } authLen = swap ? lswaps (pMsg->authLen) : pMsg->authLen; pData = pStart; SKIP_STRING (pData, swap); /* proxy-service */ SKIP_STRING (pData, swap); /* server-address */ SKIP_STRING (pData, swap); /* host-address */ SKIP_STRING (pData, swap); /* start-options */ if (authLen > 0) { SKIP_STRING (pData, swap); /* auth-name */ pData += (authLen + PAD64 (authLen)); /* auth-data */ } /* * now a detailed check on message size */ CHECK_COMPLETE_SIZE (iceConn, global_data.major_opcode, opcode, length, pData - pStart + SIZEOF (pmGetProxyAddrMsg), pStart, IceFatalToProtocol); pData = pStart; /* * extract message data, based on known characteristics * of this message type */ EXTRACT_STRING (pData, swap, serviceName); EXTRACT_STRING (pData, swap, serverAddress); EXTRACT_STRING (pData, swap, hostAddress); EXTRACT_STRING (pData, swap, startOptions); if (authLen > 0) { EXTRACT_STRING (pData, swap, authName); authData = (char *) malloc (authLen); memcpy (authData, pData, authLen); } #ifdef DEBUG (void) fprintf (stderr, "Got GetProxyAddr, serviceName = %s, serverAddr = %s\n", serviceName, serverAddress); (void) fprintf (stderr, "\thostAddr = %s, options = %s, authLen = %d\n", hostAddress, startOptions, authLen); if (authLen > 0) (void) fprintf (stderr, "\tauthName = %s\n", authName); #endif /* * need to copy the host port string because strtok() changes it */ if ((tmp_str = strdup (serverAddress)) == NULL) { (void) fprintf(stderr, "malloc - serverAddress copy\n"); goto sendFailure; } /* * before proceeding we want to verify that we are allowed to * accept connections from the host who called xfindproxy(); * the thing is, we don't get that host name from Proxy Manager * even if the "-host <hostname>" command-line option was present * in xfindproxy (and even if it was we shouldn't rely on it -- * much better to have ProxyMngr query the xfindproxy connect * socket for its origin); the upshot of all this that we do * a configuration check *only* on the destination (which we * assume in this case to be the serverAddress passed in by * xfindproxy(); so get the destination IP address! */ server_name_base = strtok(tmp_str, ":"); if ((hostptr = gethostbyname(server_name_base)) == NULL) { (void) fprintf(stderr, "gethostbyname (%s) failed\n", server_name_base); goto sendFailure; } memset(&server_sockaddr_in, 0, sizeof(server_sockaddr_in)); memset(&dummy_sockaddr_in, 0, sizeof(dummy_sockaddr_in)); memcpy((char *) &server_sockaddr_in.sin_addr, hostptr->h_addr, hostptr->h_length); /* * need to initialize dummy to something, but doesn't matter * what (should eventually be the true host address); * NOTE: source configuration will always match (see XFWP man * page) unless sysadmin explicitly chooses to deny */ memcpy((char *) &dummy_sockaddr_in.sin_addr, hostptr->h_addr, hostptr->h_length); if ((doConfigCheck(&dummy_sockaddr_in, &server_sockaddr_in, global_data.config_info, FINDPROXY, &rule_number)) == FAILURE) { (void) fprintf(stderr, "xfindproxy failed config check\n"); sendFailure: /* * report failure back to the ProxyMgr * */ pm_send_msg_len = STRING_BYTES(config_failure) + STRING_BYTES(NULL); IceGetHeaderExtra(iceConn, program_data->major_opcode, PM_GetProxyAddrReply, SIZEOF(pmGetProxyAddrReplyMsg), WORD64COUNT (pm_send_msg_len), pmGetProxyAddrReplyMsg, pReply, pReplyData); pReply->status = PM_Failure; STORE_STRING(pReplyData, NULL); STORE_STRING(pReplyData, config_failure); IceFlush(iceConn); free(tmp_str); return; } /* * okay, you got what you need from the PM to proceed, * so extract the fd of the selected connection and use * it to set up the remote client listen port and add * the name of the X server to your list of server connections */ /* * Before checking to see if you already have a PM connection * request for this server, make serverAddress a * FQDN so that synonomous names like oregon:0 and oregon.com:0 * will be recognized as the same Xserver. If this server * already exists, don't allocate another listen port for it - * use the already allocated one */ colon = strchr (serverAddress, ':'); if (colon) { struct hostent *hostent; *colon = '\0'; hostent = gethostbyname (serverAddress); *colon = ':'; if (hostent && hostent->h_name) { tmpAddress = (char *) malloc (strlen (hostent->h_name) + strlen (colon) + 1); (void) sprintf (tmpAddress, "%s%s", hostent->h_name, colon); serverAddress = tmpAddress; } } if ((doCheckServerList(serverAddress, &listen_port_string, program_data->config_info->num_servers)) == FAILURE) { /* * this server name isn't in your list; so set up a new * remote client listen port for it; extract the fd from * the connection and pass it in as index to array lookup */ if ((doSetupRemClientListen(&listen_port_string, program_data, serverAddress)) == FAILURE) { goto sendFailure; } } if (tmpAddress) free (tmpAddress); /* * the PM-sent server address *was* in your list, so send back * the rem client listen port you had already associated with * that server (it will presumably be forwarded to the remote * client through some other channel) * use IceGetHeaderExtra() and the */ pm_send_msg_len = STRING_BYTES(listen_port_string) + STRING_BYTES(NULL); IceGetHeaderExtra(iceConn, program_data->major_opcode, PM_GetProxyAddrReply, SIZEOF(pmGetProxyAddrReplyMsg), WORD64COUNT (pm_send_msg_len), pmGetProxyAddrReplyMsg, pReply, pReplyData); pReply->status = PM_Success; STORE_STRING(pReplyData, listen_port_string); STORE_STRING(pReplyData, NULL); IceFlush(iceConn); /* * before leaving this routine, change the select() timeout * here to be equal to the configured client listen timeout * (otherwise you'll never *get* to your listen timeout * if it's shorter than the startup select() default */ program_data->config_info->select_timeout.tv_sec = program_data->config_info->client_listen_timeout; break; } case ICE_Error: { iceErrorMsg *pMsg; char *pStart; CHECK_AT_LEAST_SIZE (iceConn, global_data.major_opcode, ICE_Error, length, sizeof(iceErrorMsg), IceFatalToProtocol); IceReadCompleteMessage (iceConn, SIZEOF (iceErrorMsg), iceErrorMsg, pMsg, pStart); if (!IceValidIO (iceConn)) { IceDisposeCompleteMessage (iceConn, pStart); return; } if (swap) { pMsg->errorClass = lswaps (pMsg->errorClass); pMsg->offendingSequenceNum = lswapl (pMsg->offendingSequenceNum); } (void) fprintf(stderr, "Proxy Manager reported ICE Error:\n"); (void) fprintf(stderr, "\tclass = 0x%x, offending minor opcode = %d\n", pMsg->errorClass, pMsg->offendingMinorOpcode); (void) fprintf(stderr, "\tseverity = %d, sequence = %ld\n", pMsg->severity, (long)pMsg->offendingSequenceNum); IceDisposeCompleteMessage (iceConn, pStart); break; } default: break; } /* end switch */ }
IceProtocolSetupStatus IceProtocolSetup ( IceConn iceConn, int myOpcode, IcePointer clientData, Bool mustAuthenticate, int *majorVersionRet, int *minorVersionRet, char **vendorRet, char **releaseRet, int errorLength, char *errorStringRet ) { iceProtocolSetupMsg *pMsg; char *pData; _IceProtocol *myProtocol; int extra; Bool gotReply, ioErrorOccured; int accepted, i; int hisOpcode; unsigned long setup_sequence; IceReplyWaitInfo replyWait; _IceReply reply; IcePoVersionRec *versionRec = NULL; int authCount; int *authIndices; if (errorStringRet && errorLength > 0) *errorStringRet = '\0'; *majorVersionRet = 0; *minorVersionRet = 0; *vendorRet = NULL; *releaseRet = NULL; if (myOpcode < 1 || myOpcode > _IceLastMajorOpcode) { strncpy (errorStringRet, "myOpcode out of range", errorLength); return (IceProtocolSetupFailure); } myProtocol = &_IceProtocols[myOpcode - 1]; if (myProtocol->orig_client == NULL) { strncpy (errorStringRet, "IceRegisterForProtocolSetup was not called", errorLength); return (IceProtocolSetupFailure); } /* * Make sure this protocol hasn't been activated already. */ if (iceConn->process_msg_info) { for (i = iceConn->his_min_opcode; i <= iceConn->his_max_opcode; i++) { if (iceConn->process_msg_info[ i - iceConn->his_min_opcode].in_use && iceConn->process_msg_info[ i - iceConn->his_min_opcode ].my_opcode == myOpcode) break; } if (i <= iceConn->his_max_opcode) { return (IceProtocolAlreadyActive); } } /* * Generate the message. */ if (myProtocol->orig_client->auth_count > 0) { authIndices = malloc ( myProtocol->orig_client->auth_count * sizeof (int)); _IceGetPoValidAuthIndices (myProtocol->protocol_name, iceConn->connection_string, myProtocol->orig_client->auth_count, (const char **) myProtocol->orig_client->auth_names, &authCount, authIndices); } else { authCount = 0; authIndices = NULL; } extra = STRING_BYTES (myProtocol->protocol_name) + STRING_BYTES (myProtocol->orig_client->vendor) + STRING_BYTES (myProtocol->orig_client->release); for (i = 0; i < authCount; i++) { extra += STRING_BYTES (myProtocol->orig_client->auth_names[ authIndices[i]]); } extra += (myProtocol->orig_client->version_count * 4); IceGetHeaderExtra (iceConn, 0, ICE_ProtocolSetup, SIZEOF (iceProtocolSetupMsg), WORD64COUNT (extra), iceProtocolSetupMsg, pMsg, pData); setup_sequence = iceConn->send_sequence; pMsg->protocolOpcode = myOpcode; pMsg->versionCount = myProtocol->orig_client->version_count; pMsg->authCount = authCount; pMsg->mustAuthenticate = mustAuthenticate; STORE_STRING (pData, myProtocol->protocol_name); STORE_STRING (pData, myProtocol->orig_client->vendor); STORE_STRING (pData, myProtocol->orig_client->release); for (i = 0; i < authCount; i++) { STORE_STRING (pData, myProtocol->orig_client->auth_names[ authIndices[i]]); } for (i = 0; i < myProtocol->orig_client->version_count; i++) { STORE_CARD16 (pData, myProtocol->orig_client->version_recs[i].major_version); STORE_CARD16 (pData, myProtocol->orig_client->version_recs[i].minor_version); } IceFlush (iceConn); /* * Process messages until we get a Protocol Reply. */ replyWait.sequence_of_request = setup_sequence; replyWait.major_opcode_of_request = 0; replyWait.minor_opcode_of_request = ICE_ProtocolSetup; replyWait.reply = (IcePointer) &reply; iceConn->protosetup_to_you = malloc (sizeof (_IceProtoSetupToYouInfo)); iceConn->protosetup_to_you->my_opcode = myOpcode; iceConn->protosetup_to_you->my_auth_count = authCount; iceConn->protosetup_to_you->auth_active = 0; iceConn->protosetup_to_you->my_auth_indices = authIndices; gotReply = False; ioErrorOccured = False; accepted = 0; while (!gotReply && !ioErrorOccured) { ioErrorOccured = (IceProcessMessages ( iceConn, &replyWait, &gotReply) == IceProcessMessagesIOError); if (ioErrorOccured) { strncpy (errorStringRet, "IO error occured doing Protocol Setup on connection", errorLength); return (IceProtocolSetupIOError); } else if (gotReply) { if (reply.type == ICE_PROTOCOL_REPLY) { if (reply.protocol_reply.version_index >= myProtocol->orig_client->version_count) { strncpy (errorStringRet, "Got a bad version index in the Protocol Reply", errorLength); free (reply.protocol_reply.vendor); free (reply.protocol_reply.release); } else { versionRec = &(myProtocol->orig_client->version_recs[ reply.protocol_reply.version_index]); accepted = 1; } } else /* reply.type == ICE_PROTOCOL_ERROR */ { /* Protocol Setup failed */ strncpy (errorStringRet, reply.protocol_error.error_message, errorLength); free (reply.protocol_error.error_message); } if (iceConn->protosetup_to_you->my_auth_indices) free (iceConn->protosetup_to_you->my_auth_indices); free (iceConn->protosetup_to_you); iceConn->protosetup_to_you = NULL; } } if (accepted) { _IceProcessMsgInfo *process_msg_info; *majorVersionRet = versionRec->major_version; *minorVersionRet = versionRec->minor_version; *vendorRet = reply.protocol_reply.vendor; *releaseRet = reply.protocol_reply.release; /* * Increase the reference count for the number of active protocols. */ iceConn->proto_ref_count++; /* * We may be using a different major opcode for this protocol * than the other client. Whenever we get a message, we must * map to our own major opcode. */ hisOpcode = reply.protocol_reply.major_opcode; _IceAddOpcodeMapping (iceConn, hisOpcode, myOpcode); process_msg_info = &iceConn->process_msg_info[hisOpcode - iceConn->his_min_opcode]; process_msg_info->client_data = clientData; process_msg_info->accept_flag = 0; process_msg_info->process_msg_proc.orig_client = versionRec->process_msg_proc; return (IceProtocolSetupSuccess); } else { return (IceProtocolSetupFailure); } }