void CommThread :: SendMessage() { // This is called if there is a message to send, it'll take the global // string sendmessage and send it through the socket. Its important to // note that it'll send the string sendmessage exactly as it is (once it // has converted it to const char) so all editing such as the adding of // PRIVMSG needs to be done before SendMessage is called. fd_set writefds; struct timeval tv; int n, rv; gowrite = 0; FD_ZERO(&writefds); FD_SET(sockfd, &writefds); n = sockfd + 1; cout << "write n = " << n << endl; tv.tv_sec = 2; tv.tv_usec = 500000; rv = select(n, NULL, &writefds, NULL, &tv); cout << "write rv = " << rv << endl; if (rv == -1) { perror("select"); //error occured in select() } else if (rv == 0 ) { printf("(Write) Timeout occured! Blocked after 2.5 seconds. \n"); gowrite = 1; } else { if (FD_ISSET(sockfd, &writefds)) { cout << "write rv > 1" << endl; gowrite = -1; cout << "Clear to send" << endl; } } if (gowrite == -1) { // As gowrite == 1 then we convert the string sendmessage into const chars, measure its length // and then send it through sockfd. const char* finalmsg; finalmsg = sendmessage.c_str(); int finalmessagelen, finalmessagebytes_sent; finalmessagelen = sendmessage.length(); finalmessagebytes_sent = send(sockfd, finalmsg, finalmessagelen, MSG_DONTWAIT); cout << "Sent string sendmessage: " << sendmessage << endl; cout << "Final Message Length: " << finalmessagelen << endl; cout << "Bytes Sent: " << finalmessagebytes_sent << endl; sendmessage = ""; messagetosend = 0; Message *msg1 = new Message(MSG_TOLOOPER_START); Mail("Commthread", msg1); } else if (gowrite == 1) { // If gowrite == -1 then there its not possible to send a message, the sendreceive loop starts again // and will attempt to send the message. messagetosend = 0; Message *msg1 = new Message(MSG_TOLOOPER_START); Mail("Commthread", msg1); } }
void CommThread :: ReceiveMessage() { // This section of code checks sockfd (socket to irc server) for data, // if there is data it sends a message to MainView to execute Update (), // which after collecting the data and putting it into the textOutputView // will restart the SendReceiveLoop(). // If not it instead sends a message to start the SendReceiveLoop() again. fd_set readfds; struct timeval tv; int n, rv; go = 0; FD_ZERO(&readfds); FD_SET(sockfd, &readfds); n = sockfd +1; cout << "n = " << n << endl; tv.tv_sec = 2; tv.tv_usec = 500000; rv = select(n, &readfds, NULL, NULL, &tv); cout << "rv = " << rv << endl; if (rv == -1) { perror("select"); //error occured in select() } else if (rv == 0 ) { printf("(Read) Timeout occured! No data after 2.5 seconds. \n"); go = -1; } else { if (FD_ISSET(sockfd, &readfds)) { cout << "rv > 1" << endl; go = 1; } } // If "go" is set to -1 there is no data the SendReceiveLoop is started // again, if it is 1 then a message is sent (via Mail) to MainView. cout << "go = " << go << endl; if (go == 1) { bzero(buf, 50000); numbytes = recv (sockfd, buf,50000, 0); printf("buffer = %d\n", numbytes); // cout << "Raw buf = " << buf << endl; Message *msg = new Message(MSG_FROMLOOPER_NEW_MESSAGE); Mail("MainView", msg); } else if (go == -1) { Message *msg1 = new Message(MSG_TOLOOPER_START); Mail("Commthread", msg1); } }
// Join really does what is says on the tin, messagesend is set to 1 and therefore on the next loop the message // will be send. erase is also sent to 1. This tells the program to start editing the buf before sending it to // the textOutputView - removes all the junk. Finally it puts together a string, places it in the sendmessage // variable and starts the loop void MainView::Join(void) { messagetosend = 1; erase = 1; sendmessage = join + channel + end; cout << "Sent JOIN" << endl; Message *msg1 = new Message(MSG_TOLOOPER_START); Mail("Commthread", msg1); }
void MainView::SendMsg (void) { textfrominput = textInputView->GetBuffer()[0].const_str(); cout << "Text from input: " << textfrominput << endl; messagetosend = 1; sendmessage = priv + channel + " :" + textfrominput + end; Message *msg1 = new Message(MSG_TOLOOPER_START); Mail("Commthread", msg1); string printinputmessage = nick + channel + " : " + textfrominput + end; cout << printinputmessage << endl; const char* msgupdate; msgupdate = printinputmessage.c_str(); textOutputView->Insert(msgupdate, true); textInputView->Clear(true); }
void MainView::Connect(void) { erase = 0; struct hostent *he; struct sockaddr_in their_addr; // connector's address information argv[1] = "chat.freenode.net"; if ((he=gethostbyname(argv[1])) == NULL) { // get the host info perror("gethostbyname"); printf("get the host info"); exit(1); } if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { perror("socket"); exit(1); } their_addr.sin_family = AF_INET; // host byte order their_addr.sin_port = htons(PORT); // short, network byte order their_addr.sin_addr = *((struct in_addr *)he->h_addr); memset(&(their_addr.sin_zero), '\0', 8); // zero the rest of the struct if (connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct sockaddr)) == -1) { perror("connect"); printf("connecting"); exit(1); } int nrv, counter; nrv = read(sockfd,buf1,5000); printf(" read: %s\n", buf1); string nickmessage; nickmessage = "Nick " + nick + end; const char* nickmsg; nickmsg = nickmessage.c_str(); int nickmessagelen, nickmessagebytes_sent; nickmessagelen = nickmessage.length(); nickmessagebytes_sent = send(sockfd, nickmsg, nickmessagelen, MSG_DONTWAIT); // send(sockfd, "NICK jcoxon_\n", 13, 0); cout << "Sent NICK" << endl; send(sockfd, "USER sIRC sIRC_ sIRC__ :sIRC\n", 29, 0); cout << "Sent USER" << endl; go = 1; Message *msg1 = new Message(MSG_TOLOOPER_RECEIVE); Mail("Commthread", msg1); }
static bool SendMemoMail(NickCore *nc, MemoInfo *mi, Memo *m) { Anope::string subject = translate(nc, Config->MailMemoSubject.c_str()); Anope::string message = translate(nc, Config->MailMemoMessage.c_str()); subject = subject.replace_all_cs("%n", nc->display); subject = subject.replace_all_cs("%s", m->sender); subject = subject.replace_all_cs("%d", mi->GetIndex(m)); subject = subject.replace_all_cs("%t", m->text); message = message.replace_all_cs("%n", nc->display); message = message.replace_all_cs("%s", m->sender); message = message.replace_all_cs("%d", mi->GetIndex(m)); message = message.replace_all_cs("%t", m->text); return Mail(nc, subject, message); }
Mail Mailbox::read(const std::string& fname, const std::time_t& timestamp) const { std::ifstream fd(fname.c_str()); if (!fd) { ERROR1("error opening mail file: %s", fname.c_str()); throw ReadError("error opening mail file: " + fname); } std::string sender; std::getline(fd, sender); std::string message; std::getline(fd, message); return Mail(sender, message, timestamp); }