aboutsummaryrefslogtreecommitdiffstats
path: root/plugin_imon.c
blob: 67217a56b0d568bf77ac49c64cb629fbc81ce0de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
pre { line-height: 125%; margin: 0; }
td.linenos pre { color: #000000; background-color: #f0f0f0; padding: 0 5px 0 5px; }
span.linenos { color: #000000; background-color: #f0f0f0; padding: 0 5px 0 5px; }
td.linenos pre.special { color: #000000; background-color: #ffffc0; padding: 0 5px 0 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding: 0 5px 0 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
# France ALL (All channel 21 to 60)
# T freq bw fec_hi fec_lo mod transmission-mode guard-interval hierarchy
T 474000000 8MHz 2/3 NONE QAM64 8k 1/32 NONE #Channel UHF 21
T 482000000 8MHz 2/3 NONE QAM64 8k 1/32 NONE #Channel UHF 22
T 490000000 8MHz 2/3 NONE QAM64 8k 1/32 NONE #Channel UHF 23
T 498000000 8MHz 2/3 NONE QAM64 8k 1/32 NONE #Channel UHF 24
T 506000000 8MHz 2/3 NONE QAM64 8k 1/32 NONE #Channel UHF 25
T 514000000 8MHz 2/3 NONE QAM64 8k 1/32 NONE #Channel UHF 26
T 522000000 8MHz 2/3 NONE QAM64 8k 1/32 NONE #Channel UHF 27
T 530000000 8MHz 2/3 NONE QAM64 8k 1/32 NONE #Channel UHF 28
T 538000000 8MHz 2/3 NONE QAM64 8k 1/32 NONE #Channel UHF 29
T 546000000 8MHz 2/3 NONE QAM64 8k 1/32 NONE #Channel UHF 30
T 554000000 8MHz 2/3 NONE QAM64 8k 1/32 NONE #Channel UHF 31
T 562000000 8MHz 2/3 NONE QAM64 8k 1/32 NONE #Channel UHF 32
T 570000000 8MHz 2/3 NONE QAM64 8k 1/32 NONE #Channel UHF 33
T 578000000 8MHz 2/3 NONE QAM64 8k 1/32 NONE #Channel UHF 34
T 586000000 8MHz 2/3 NONE QAM64 8k 1/32 NONE #Channel UHF 35
T 594000000 8MHz 2/3 NONE QAM64 8k 1/32 NONE #Channel UHF 36
T 602000000 8MHz 2/3 NONE QAM64 8k 1/32 NONE #Channel UHF 37
T 610000000 8MHz 2/3 NONE QAM64 8k 1/32 NONE #Channel UHF 38
T 618000000 8MHz 2/3 NONE QAM64 8k 1/32 NONE #Channel UHF 39
T 626000000 8MHz 2/3 NONE QAM64 8k 1/32 NONE #Channel UHF 40
T 634000000 8MHz 2/3 NONE QAM64 8k 1/32 NONE #Channel UHF 41
T 642000000 8MHz 2/3 NONE QAM64 8k 1/32 NONE #Channel UHF 42
T 650000000 8MHz 2/3 NONE QAM64 8k 1/32 NONE #Channel UHF 43
T 658000000 8MHz 2/3 NONE QAM64 8k 1/32 NONE #Channel UHF 44
T 666000000 8MHz 2/3 NONE QAM64 8k 1/32 NONE #Channel UHF 45
T 674000000 8MHz 2/3 NONE QAM64 8k 1/32 NONE #Channel UHF 46
T 682000000 8MHz 2/3 NONE QAM64 8k 1/32 NONE #Channel UHF 47
T 690000000 8MHz 2/3 NONE QAM64 8k 1/32 NONE #Channel UHF 48
T 698000000 8MHz 2/3 NONE QAM64 8k 1/32 NONE #Channel UHF 49
T 706000000 8MHz 2/3 NONE QAM64 8k 1/32 NONE #Channel UHF 50
T 714000000 8MHz 2/3 NONE QAM64 8k 1/32 NONE #Channel UHF 51
T 722000000 8MHz 2/3 NONE QAM64 8k 1/32 NONE #Channel UHF 52
T 730000000 8MHz 2/3 NONE QAM64 8k 1/32 NONE #Channel UHF 53
T 738000000 8MHz 2/3 NONE QAM64 8k 1/32 NONE #Channel UHF 54
T 746000000 8MHz 2/3 NONE QAM64 8k 1/32 NONE #Channel UHF 55
T 754000000 8MHz 2/3 NONE QAM64 8k 1/32 NONE #Channel UHF 56
T 762000000 8MHz 2/3 NONE QAM64 8k 1/32 NONE #Channel UHF 57
T 770000000 8MHz 2/3 NONE QAM64 8k 1/32 NONE #Channel UHF 58
T 778000000 8MHz 2/3 NONE QAM64 8k 1/32 NONE #Channel UHF 59
T 786000000 8MHz 2/3 NONE QAM64 8k 1/32 NONE #Channel UHF 60
'n315' href='#n315'>315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
/* $Id$
 * $URL$
 *
 * imond/telmond data processing
 *
 * Copyright (C) 2003 Nico Wallmeier <nico.wallmeier@post.rwth-aachen.de>
 *
 * This file is part of LCD4Linux.
 *
 * LCD4Linux 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 2, or (at your option)
 * any later version.
 *
 * LCD4Linux 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, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#include "config.h"
#include "debug.h"
#include "plugin.h"
#include "qprintf.h"
#include "cfg.h"
#include "hash.h"

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/fcntl.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <linux/errno.h>
#include <sys/stat.h>
#include <string.h>
#include <time.h>

#include <netdb.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>		/* decl of inet_addr()      */
#include <sys/socket.h>


static HASH TELMON;
static HASH IMON;

static char thost[256];
static int tport;
static char phoneb[256];

static char ihost[256];
static char ipass[256];
static int iport;

static int fd = 0;
static int err = 0;

/*----------------------------------------------------------------------------
 *  service_connect (host_name, port)       - connect to tcp-service
 *----------------------------------------------------------------------------
 */
static int service_connect(const char *host_name, const int port)
{
    struct sockaddr_in addr;
    struct hostent *host_p;
    int fd;
    int opt = 1;

    (void) memset((char *) &addr, 0, sizeof(addr));

    if ((addr.sin_addr.s_addr = inet_addr((char *) host_name)) == INADDR_NONE) {
	host_p = gethostbyname(host_name);
	if (!host_p) {
	    error("%s: host not found\n", host_name);
	    return (-1);
	}
	(void) memcpy((char *) (&addr.sin_addr), host_p->h_addr, host_p->h_length);
    }

    addr.sin_family = AF_INET;
    addr.sin_port = htons((unsigned short) port);

    /* open socket */
    if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
	perror("socket");
	return (-1);
    }

    (void) setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &opt, sizeof(opt));

    if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) != 0) {
	(void) close(fd);
	perror(host_name);
	return (-1);
    }

    return (fd);
}				/* service_connect (char * host_name, int port) */


/*----------------------------------------------------------------------------
 *  send_command (int fd, char * str)       - send command to imond
 *----------------------------------------------------------------------------
 */
static void send_command(const int fd, const char *str)
{
    char buf[256];
    int len = strlen(str);

    sprintf(buf, "%s\r\n", str);
    write(fd, buf, len + 2);

    return;
}				/* send_command (int fd, char * str) */


/*----------------------------------------------------------------------------
 *  get_answer (int fd)                     - get answer from imond
 *----------------------------------------------------------------------------
 */
static char *get_answer(const int fd)
{
    static char buf[8192];
    int len;

    len = read(fd, buf, 8192);

    if (len <= 0) {
	return ((char *) NULL);
    }

    while (len > 1 && (buf[len - 1] == '\n' || buf[len - 1] == '\r')) {
	buf[len - 1] = '\0';
	len--;
    }

    if (!strncmp(buf, "OK ", 3)) {	/* OK xxxx */
	return (buf + 3);
    } else if (len > 2 && !strcmp(buf + len - 2, "OK")) {
	*(buf + len - 2) = '\0';
	return (buf);
    } else if (len == 2 && !strcmp(buf + len - 2, "OK")) {
	return (buf);
    }

    return ((char *) NULL);	/* ERR xxxx */
}				/* get_answer (int fd) */

/*----------------------------------------------------------------------------
 *  get_value (char * cmd)         - send command, get value
 *----------------------------------------------------------------------------
 */
static char *get_value(const char *cmd)
{
    char *answer;

    send_command(fd, cmd);

    answer = get_answer(fd);

    if (answer) {
	return (answer);
    }

    return ("");
}				/* get_value (char * cmd, int arg) */


static void phonebook(char *number)
{
    FILE *fp;
    char line[256];

    fp = fopen(phoneb, "r");

    if (!fp)
	return;

    while (fgets(line, sizeof(line), fp)) {
	if (*line == '#')
	    continue;
	if (!strncmp(line, number, strlen(number))) {
	    char *komma = strchr(line, ',');
	    char *beginn = strchr(line, '=');
	    if (!beginn)
		return;
	    while (strrchr(line, '\r'))
		strrchr(line, '\r')[0] = '\0';
	    while (strrchr(line, '\n'))
		strrchr(line, '\n')[0] = '\0';
	    if (komma)
		komma[0] = '\0';
	    strcpy(number, beginn + 1);
	    break;
	}
    }

    fclose(fp);
}


static int parse_telmon()
{
    static int telmond_fd = -2;
    static char oldanswer[128];
    int age;

    /* reread every 1 sec only */
    age = hash_age(&TELMON, NULL);
    if (age > 0 && age <= 1000)
	return 0;

    if (telmond_fd != -1) {
	char telbuf[128];

	telmond_fd = service_connect(thost, tport);
	if (telmond_fd >= 0) {
	    int l = read(telmond_fd, telbuf, 127);
	    if ((l > 0) && (strcmp(telbuf, oldanswer))) {
		char date[11];
		char time[11];
		char number[256];
		char msn[256];
		sscanf(telbuf, "%s %s %s %s", date, time, number, msn);
		hash_put(&TELMON, "time", time);
		date[4] = '\0';
		date[7] = '\0';
		qprintf(time, sizeof(time), "%s.%s.%s", date + 8, date + 5, date);
		hash_put(&TELMON, "number", number);
		hash_put(&TELMON, "msn", msn);
		hash_put(&TELMON, "date", time);
		phonebook(number);
		phonebook(msn);
		hash_put(&TELMON, "name", number);
		hash_put(&TELMON, "msnname", msn);
	    }
	    close(telmond_fd);
	    strcpy(oldanswer, telbuf);
	}
    }
    return 0;
}


static int configure_telmon(void)
{
    static int configured = 0;

    char *s;

    if (configured != 0)
	return configured;

    hash_create(&TELMON);

    s = cfg_get("Plugin:Telmon", "Host", "127.0.0.1");
    if (*s == '\0') {
	error("[Telmon] empty 'Host' entry in %s", cfg_source());
	configured = -1;
	return configured;
    }
    strcpy(thost, s);
    free(s);

    if (cfg_number("Plugin:Telmon", "Port", 5001, 1, 65536, &tport) < 0) {
	error("[Telmon] no valid port definition");
	configured = -1;
	return configured;
    }

    s = cfg_get("Plugin:Telmon", "Phonebook", "/etc/phonebook");
    strcpy(phoneb, s);
    free(s);

    configured = 1;
    return configured;
}


static void my_telmon(RESULT * result, RESULT * arg1)
{
    char *val = NULL;

    if (configure_telmon() < 0) {
	SetResult(&result, R_STRING, "");
	return;
    }

    if (parse_telmon() < 0) {
	SetResult(&result, R_STRING, "");
	return;
    }

    val = hash_get(&TELMON, R2S(arg1), NULL);
    if (val == NULL)
	val = "";
    SetResult(&result, R_STRING, val);
}


void init()
{
    if (fd != 0)
	return;

    fd = service_connect(ihost, iport);

    if (fd < 0) {
	err++;
    } else if ((ipass != NULL) && (*ipass != '\0')) {	/* Passwort senden */
	char buf[40];
	qprintf(buf, sizeof(buf), "pass %s", ipass);
	send_command(fd, buf);
	get_answer(fd);
    }
}


static int parse_imon(const char *cmd)
{
    /* reread every half sec only */
    int age = hash_age(&IMON, cmd);
    if (age > 0 && age <= 500)
	return 0;

    init();			/* establish connection */

    if (err)
	return -1;

    hash_put(&IMON, cmd, get_value(cmd));

    return 0;
}


static int configure_imon(void)
{
    static int configured = 0;

    char *s;

    if (configured != 0)
	return configured;

    hash_create(&IMON);

    s = cfg_get("Plugin:Imon", "Host", "127.0.0.1");
    if (*s == '\0') {
	error("[Imon] empty 'Host' entry in %s", cfg_source());
	configured = -1;
	return configured;
    }
    strcpy(ihost, s);
    free(s);

    if (cfg_number("Plugin:Imon", "Port", 5000, 1, 65536, &iport) < 0) {
	error("[Imon] no valid port definition");
	configured = -1;
	return configured;
    }

    s = cfg_get("Plugin:Imon", "Pass", "");
    strcpy(ipass, s);
    free(s);

    configured = 1;
    return configured;
}


static void my_imon_version(RESULT * result)
{
    char *val;
    int age;

    if (configure_imon() < 0) {
	SetResult(&result, R_STRING, "");
	return;
    }

    /* read only once */
    age = hash_age(&IMON, "version");
    if (age < 0) {
	char *s;
	init();
	if (err) {
	    SetResult(&result, R_STRING, "");
	    return;
	}
	s = get_value("version");
	for (;;) {		/* interne Versionsnummer killen */
	    if (s[0] == ' ') {
		s = s + 1;
		break;
	    }
	    s = s + 1;
	}
	hash_put(&IMON, "version", s);
    }

    val = hash_get(&IMON, "version", NULL);
    if (val == NULL)
	val = "";
    SetResult(&result, R_STRING, val);
}


static int parse_imon_rates(const char *channel)
{
    char buf[128], in[25], out[25];
    char *s;
    int age;

    qprintf(buf, sizeof(buf), "rate %s in", channel);

    /* reread every half sec only */
    age = hash_age(&IMON, buf);
    if (age > 0 && age <= 500)
	return 0;

    init();			/* establish connection */

    if (err)
	return -1;

    qprintf(buf, sizeof(buf), "rate %s", channel);
    s = get_value(buf);

    if (sscanf(s, "%s %s", in, out) != 2)
	return -1;

    qprintf(buf, sizeof(buf), "rate %s in", channel);
    hash_put(&IMON, buf, in);
    qprintf(buf, sizeof(buf), "rate %s out", channel);
    hash_put(&IMON, buf, out);

    return 0;
}


static void my_imon_rates(RESULT * result, RESULT * arg1, RESULT * arg2)
{
    char *val;
    char buf[128];

    if (configure_imon() < 0) {
	SetResult(&result, R_STRING, "");
	return;
    }

    if (parse_imon_rates(R2S(arg1)) < 0) {
	SetResult(&result, R_STRING, "");
	return;
    }

    qprintf(buf, sizeof(buf), "rate %s %s", R2S(arg1), R2S(arg2));

    val = hash_get(&IMON, buf, NULL);
    if (val == NULL)
	val = "";
    SetResult(&result, R_STRING, val);
}

static int parse_imon_quantity(const char *channel)
{
    char buf[256], fill1[25], in[25], fill2[25], out[25];
    char *s;
    int age;

    qprintf(buf, sizeof(buf), "quantity %s in", channel);

    /* reread every half sec only */
    age = hash_age(&IMON, buf);
    if (age > 0 && age <= 500)
	return 0;

    init();			/* establish connection */

    if (err)
	return -1;

    qprintf(buf, sizeof(buf), "quantity %s", channel);
    s = get_value(buf);

    if (sscanf(s, "%s %s %s %s", fill1, in, fill2, out) != 4)
	return -1;

    qprintf(buf, sizeof(buf), "quantity %s in", channel);
    hash_put(&IMON, buf, in);
    qprintf(buf, sizeof(buf), "quantity %s out", channel);
    hash_put(&IMON, buf, out);

    return 0;
}

static int parse_imon_status(const char *channel)
{
    char buf[256], status[25];
    char *s;
    int age;

    qprintf(buf, sizeof(buf), "status %s", channel);

    /* reread every half sec only */
    age = hash_age(&IMON, buf);
    if (age > 0 && age <= 500)
	return 0;

    init();			/* establish connection */

    if (err)
	return -1;

    qprintf(buf, sizeof(buf), "status %s", channel);
    s = get_value(buf);

    if (sscanf(s, "%s", status) != 1)
	return -1;

    qprintf(buf, sizeof(buf), "status %s", channel);
    if (strcasecmp(status, "Online") == 0)
	hash_put(&IMON, buf, "1");
    else
	hash_put(&IMON, buf, "0");

    return 0;
}

static void my_imon_quantity(RESULT * result, RESULT * arg1, RESULT * arg2)
{
    char *val;
    char buf[256];

    if (configure_imon() < 0) {
	SetResult(&result, R_STRING, "");
	return;
    }

    if (parse_imon_quantity(R2S(arg1)) < 0) {
	SetResult(&result, R_STRING, "");
	return;
    }

    qprintf(buf, sizeof(buf), "quantity %s %s", R2S(arg1), R2S(arg2));

    val = hash_get(&IMON, buf, NULL);
    if (val == NULL)
	val = "";
    SetResult(&result, R_STRING, val);
}

static void my_imon_status(RESULT * result, RESULT * arg1)
{
    char *val;
    char buf[256];

    if (configure_imon() < 0) {
	SetResult(&result, R_NUMBER, "-1");
	return;
    }

    if (parse_imon_status(R2S(arg1)) < 0) {
	SetResult(&result, R_STRING, "-1");
	return;
    }

    qprintf(buf, sizeof(buf), "status %s", R2S(arg1));

    val = hash_get(&IMON, buf, NULL);
    if (val == NULL)
	val = "-1";
    SetResult(&result, R_STRING, val);
}

static void my_imon(RESULT * result, RESULT * arg1)
{
    char *val;
    char *cmd;

    if (configure_imon() < 0) {
	SetResult(&result, R_STRING, "");
	return;
    }

    cmd = R2S(arg1);
    if (parse_imon(cmd) < 0) {
	SetResult(&result, R_STRING, "");
	return;
    }

    val = hash_get(&IMON, cmd, NULL);
    if (val == NULL)
	val = "";
    SetResult(&result, R_STRING, val);
}


int plugin_init_imon(void)
{
    AddFunction("imon", 1, my_imon);
    AddFunction("imon::version", 0, my_imon_version);
    AddFunction("imon::rates", 2, my_imon_rates);
    AddFunction("imon::quantity", 2, my_imon_quantity);
    AddFunction("imon::status", 1, my_imon_status);
    AddFunction("imon::telmon", 1, my_telmon);

    return 0;
}


void plugin_exit_imon(void)
{
    if (fd > 0) {
	send_command(fd, "quit");
	close(fd);
    }
    hash_destroy(&TELMON);
    hash_destroy(&IMON);
}