2018-05-12 16:03:00 +00:00
|
|
|
/*
|
2010-04-30 17:32:42 +00:00
|
|
|
*
|
|
|
|
* Conky, a system monitor, based on torsmo
|
|
|
|
*
|
|
|
|
* Please see COPYING for details
|
|
|
|
*
|
2018-05-12 16:03:00 +00:00
|
|
|
* Copyright (c) 2005-2018 Brenden Matthews, Philip Kovacs, et. al.
|
2010-04-30 17:32:42 +00:00
|
|
|
* (see AUTHORS)
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
#include <libircclient.h>
|
2010-04-30 17:32:42 +00:00
|
|
|
#include <pthread.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2018-05-12 16:03:00 +00:00
|
|
|
#include "conky.h"
|
|
|
|
#include "logging.h"
|
2010-04-30 17:32:42 +00:00
|
|
|
#include "text_object.h"
|
|
|
|
|
|
|
|
struct ll_text {
|
2018-05-12 16:03:00 +00:00
|
|
|
char *text;
|
|
|
|
struct ll_text *next;
|
2010-04-30 17:32:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct obj_irc {
|
2018-05-12 16:03:00 +00:00
|
|
|
pthread_t *thread;
|
|
|
|
irc_session_t *session;
|
|
|
|
char *arg;
|
2010-04-30 17:32:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct ctx {
|
2018-05-12 16:03:00 +00:00
|
|
|
char *chan;
|
|
|
|
int max_msg_lines;
|
|
|
|
struct ll_text *messages;
|
2010-04-30 17:32:42 +00:00
|
|
|
};
|
|
|
|
|
2018-05-13 22:46:09 +00:00
|
|
|
void ev_connected(irc_session_t *session, const char *, const char *,
|
|
|
|
const char **, unsigned int) {
|
2018-05-12 16:03:00 +00:00
|
|
|
struct ctx *ctxptr = (struct ctx *)irc_get_ctx(session);
|
2018-05-12 23:26:31 +00:00
|
|
|
if (irc_cmd_join(session, ctxptr->chan, nullptr) != 0) {
|
2018-05-12 16:03:00 +00:00
|
|
|
NORM_ERR("irc: %s", irc_strerror(irc_errno(session)));
|
|
|
|
}
|
2010-04-30 17:32:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void addmessage(struct ctx *ctxptr, char *nick, const char *text) {
|
2018-05-12 16:03:00 +00:00
|
|
|
struct ll_text *lastmsg = ctxptr->messages;
|
|
|
|
struct ll_text *newmsg = (struct ll_text *)malloc(sizeof(struct ll_text));
|
|
|
|
newmsg->text = (char *)malloc(strlen(nick) + strlen(text) + 4); // 4 = ": \n"
|
|
|
|
sprintf(newmsg->text, "%s: %s\n", nick, text);
|
2018-05-12 23:26:31 +00:00
|
|
|
newmsg->next = nullptr;
|
2018-05-12 16:03:00 +00:00
|
|
|
int msgcnt = 1;
|
|
|
|
if (!lastmsg) {
|
|
|
|
ctxptr->messages = newmsg;
|
|
|
|
} else {
|
|
|
|
msgcnt++;
|
|
|
|
while (lastmsg->next) {
|
|
|
|
lastmsg = lastmsg->next;
|
|
|
|
msgcnt++;
|
|
|
|
if (msgcnt < 0) {
|
|
|
|
NORM_ERR("irc: too many messages, discarding the last one.");
|
|
|
|
free(newmsg->text);
|
|
|
|
free(newmsg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lastmsg->next = newmsg;
|
|
|
|
}
|
|
|
|
if (ctxptr->max_msg_lines > 0) {
|
|
|
|
newmsg = ctxptr->messages;
|
|
|
|
msgcnt -= ctxptr->max_msg_lines;
|
|
|
|
while ((msgcnt > 0) && (ctxptr->messages)) {
|
|
|
|
msgcnt--;
|
|
|
|
newmsg = ctxptr->messages->next;
|
|
|
|
free(ctxptr->messages->text);
|
|
|
|
free(ctxptr->messages);
|
|
|
|
ctxptr->messages = newmsg;
|
|
|
|
}
|
|
|
|
}
|
2010-04-30 17:32:42 +00:00
|
|
|
}
|
|
|
|
|
2018-05-13 22:46:09 +00:00
|
|
|
void ev_talkinchan(irc_session_t *session, const char *, const char *origin,
|
|
|
|
const char **params, unsigned int) {
|
2018-05-12 16:03:00 +00:00
|
|
|
char nickname[64];
|
|
|
|
struct ctx *ctxptr = (struct ctx *)irc_get_ctx(session);
|
2010-04-30 17:32:42 +00:00
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
irc_target_get_nick(origin, nickname, sizeof(nickname));
|
|
|
|
addmessage(ctxptr, nickname, params[1]);
|
2010-04-30 17:32:42 +00:00
|
|
|
}
|
|
|
|
|
2018-05-13 22:46:09 +00:00
|
|
|
void ev_num(irc_session_t *session, unsigned int event, const char *,
|
|
|
|
const char **params, unsigned int) {
|
2018-05-12 16:03:00 +00:00
|
|
|
char attachment[4] = "_00";
|
|
|
|
|
|
|
|
if (event == 433) { // nick in use
|
2018-05-13 22:46:09 +00:00
|
|
|
int len = strlen(params[1]) + 4;
|
|
|
|
char *newnick = (char *)malloc(len);
|
2018-05-12 16:03:00 +00:00
|
|
|
strcpy(newnick, params[1]);
|
|
|
|
attachment[1] += rand() % 10;
|
|
|
|
attachment[2] += rand() % 10;
|
2018-05-13 22:46:09 +00:00
|
|
|
strncat(newnick, attachment, len - 1);
|
2018-05-12 16:03:00 +00:00
|
|
|
irc_cmd_nick(session, newnick);
|
|
|
|
free(newnick);
|
|
|
|
}
|
2010-04-30 17:32:42 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
#define IRCSYNTAX \
|
|
|
|
"The correct syntax is ${irc server(:port) #channel (max_msg_lines)}"
|
2010-04-30 17:32:42 +00:00
|
|
|
#define IRCPORT 6667
|
|
|
|
#define IRCNICK "conky"
|
2018-05-12 23:26:31 +00:00
|
|
|
#define IRCSERVERPASS nullptr
|
|
|
|
#define IRCUSER nullptr
|
|
|
|
#define IRCREAL nullptr
|
2010-04-30 17:32:42 +00:00
|
|
|
|
|
|
|
void *ircclient(void *ptr) {
|
2018-05-12 16:03:00 +00:00
|
|
|
struct obj_irc *ircobj = (struct obj_irc *)ptr;
|
|
|
|
struct ctx *ctxptr = (struct ctx *)malloc(sizeof(struct ctx));
|
|
|
|
irc_callbacks_t callbacks;
|
|
|
|
char *server;
|
|
|
|
char *strport;
|
|
|
|
char *str_max_msg_lines;
|
|
|
|
unsigned int port;
|
|
|
|
|
|
|
|
memset(&callbacks, 0, sizeof(callbacks));
|
|
|
|
callbacks.event_connect = ev_connected;
|
|
|
|
callbacks.event_channel = ev_talkinchan;
|
|
|
|
callbacks.event_numeric = ev_num;
|
|
|
|
ircobj->session = irc_create_session(&callbacks);
|
|
|
|
server = strtok(ircobj->arg, " ");
|
2018-05-12 23:26:31 +00:00
|
|
|
ctxptr->chan = strtok(nullptr, " ");
|
2018-05-13 22:46:09 +00:00
|
|
|
if (!ctxptr->chan) { NORM_ERR("irc: %s", IRCSYNTAX); }
|
2018-05-12 23:26:31 +00:00
|
|
|
str_max_msg_lines = strtok(nullptr, " ");
|
2018-05-12 16:03:00 +00:00
|
|
|
if (str_max_msg_lines) {
|
2018-05-12 23:26:31 +00:00
|
|
|
ctxptr->max_msg_lines = strtol(str_max_msg_lines, nullptr, 10);
|
2018-05-12 16:03:00 +00:00
|
|
|
}
|
2018-05-12 23:26:31 +00:00
|
|
|
ctxptr->messages = nullptr;
|
2018-05-12 16:03:00 +00:00
|
|
|
irc_set_ctx(ircobj->session, ctxptr);
|
|
|
|
server = strtok(server, ":");
|
2018-05-12 23:26:31 +00:00
|
|
|
strport = strtok(nullptr, ":");
|
2018-05-12 16:03:00 +00:00
|
|
|
if (strport) {
|
2018-05-12 23:26:31 +00:00
|
|
|
port = strtol(strport, nullptr, 10);
|
2018-05-12 16:03:00 +00:00
|
|
|
if (port < 1 || port > 65535) port = IRCPORT;
|
|
|
|
} else {
|
|
|
|
port = IRCPORT;
|
|
|
|
}
|
|
|
|
int err = irc_connect(ircobj->session, server, port, IRCSERVERPASS, IRCNICK,
|
|
|
|
IRCUSER, IRCREAL);
|
2018-05-13 22:46:09 +00:00
|
|
|
if (err != 0) { err = irc_errno(ircobj->session); }
|
2018-01-19 14:06:27 +00:00
|
|
|
#ifdef BUILD_IPV6
|
2018-05-12 16:03:00 +00:00
|
|
|
if (err == LIBIRC_ERR_RESOLV) {
|
|
|
|
err = irc_connect6(ircobj->session, server, port, IRCSERVERPASS, IRCNICK,
|
|
|
|
IRCUSER, IRCREAL);
|
2018-05-13 22:46:09 +00:00
|
|
|
if (err != 0) { err = irc_errno(ircobj->session); }
|
2018-05-12 16:03:00 +00:00
|
|
|
}
|
2018-01-19 14:06:27 +00:00
|
|
|
#endif /* BUILD_IPV6 */
|
2018-05-13 22:46:09 +00:00
|
|
|
if (err != 0) { NORM_ERR("irc: %s", irc_strerror(err)); }
|
2018-05-12 16:03:00 +00:00
|
|
|
if (irc_run(ircobj->session) != 0) {
|
|
|
|
int ircerror = irc_errno(ircobj->session);
|
|
|
|
if (irc_is_connected(ircobj->session)) {
|
|
|
|
NORM_ERR("irc: %s", irc_strerror(ircerror));
|
|
|
|
} else {
|
|
|
|
NORM_ERR("irc: disconnected");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(ircobj->arg);
|
|
|
|
free(ctxptr);
|
2018-05-12 23:26:31 +00:00
|
|
|
return nullptr;
|
2010-04-30 17:32:42 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 16:03:00 +00:00
|
|
|
void parse_irc_args(struct text_object *obj, const char *arg) {
|
|
|
|
struct obj_irc *opaque = (struct obj_irc *)malloc(sizeof(struct obj_irc));
|
|
|
|
opaque->thread = (pthread_t *)malloc(sizeof(pthread_t));
|
2018-05-12 23:26:31 +00:00
|
|
|
srand(time(nullptr));
|
|
|
|
opaque->session = nullptr;
|
2018-05-12 16:03:00 +00:00
|
|
|
opaque->arg = strdup(arg);
|
2018-05-12 23:26:31 +00:00
|
|
|
pthread_create(opaque->thread, nullptr, ircclient, opaque);
|
2018-05-12 16:03:00 +00:00
|
|
|
obj->data.opaque = opaque;
|
2010-04-30 17:32:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void print_irc(struct text_object *obj, char *p, int p_max_size) {
|
2018-05-12 16:03:00 +00:00
|
|
|
struct obj_irc *ircobj = (struct obj_irc *)obj->data.opaque;
|
|
|
|
struct ctx *ctxptr;
|
|
|
|
struct ll_text *nextmsg, *curmsg;
|
|
|
|
|
|
|
|
if (!ircobj->session) return;
|
|
|
|
if (!irc_is_connected(ircobj->session)) return;
|
|
|
|
ctxptr = (struct ctx *)irc_get_ctx(ircobj->session);
|
|
|
|
curmsg = ctxptr->messages;
|
|
|
|
while (curmsg) {
|
|
|
|
nextmsg = curmsg->next;
|
|
|
|
strncat(p, curmsg->text, p_max_size - strlen(p) - 1);
|
|
|
|
if (!ctxptr->max_msg_lines) {
|
|
|
|
free(curmsg->text);
|
|
|
|
free(curmsg);
|
|
|
|
}
|
|
|
|
curmsg = nextmsg;
|
|
|
|
}
|
2018-05-13 22:46:09 +00:00
|
|
|
if (p[0] != 0) { p[strlen(p) - 1] = 0; }
|
|
|
|
if (!ctxptr->max_msg_lines) { ctxptr->messages = nullptr; }
|
2010-04-30 17:32:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void free_irc(struct text_object *obj) {
|
2018-05-12 16:03:00 +00:00
|
|
|
struct obj_irc *ircobj = (struct obj_irc *)obj->data.opaque;
|
|
|
|
struct ctx *ctxptr;
|
2018-05-12 23:26:31 +00:00
|
|
|
struct ll_text *nextmsg, *curmsg = nullptr;
|
2018-05-12 16:03:00 +00:00
|
|
|
|
|
|
|
if (ircobj->session) {
|
|
|
|
if (irc_is_connected(ircobj->session)) {
|
|
|
|
ctxptr = (struct ctx *)irc_get_ctx(ircobj->session);
|
|
|
|
curmsg = ctxptr->messages;
|
|
|
|
irc_disconnect(ircobj->session);
|
|
|
|
}
|
2018-05-12 23:26:31 +00:00
|
|
|
pthread_join(*(ircobj->thread), nullptr);
|
2018-05-12 16:03:00 +00:00
|
|
|
irc_destroy_session(ircobj->session);
|
|
|
|
}
|
|
|
|
free(ircobj->thread);
|
|
|
|
free(obj->data.opaque);
|
|
|
|
while (curmsg) {
|
|
|
|
nextmsg = curmsg->next;
|
|
|
|
free(curmsg->text);
|
|
|
|
free(curmsg);
|
|
|
|
curmsg = nextmsg;
|
|
|
|
}
|
2010-04-30 17:32:42 +00:00
|
|
|
}
|