#!/usr/bin/env python3
# -*- coding: UTF-8 -*-

#   Copyright 2009-2021 Oli Schacher, Fumail Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#


# This tool is used to send messages to the fuglu control port

import sys
import socket
from optparse import OptionParser
from fuglu.stringencode import force_bString, force_uString

def usage():
    return """
    
    Usage fuglu_control [-p <sock>] <command> [<args>]
    Available commands:
    stats : show statistics
    uptime: show long fuglu has been running
    workerlist: show current status of all mail scanning threads
    threadlist: show current status of ALL threads (core + workers)
    exceptionlist: last 10 exception tracebacks
    netconsole [<port> [<bind address>]] : start a python interactive shell on a network socket
    
    <sock> unix socket or port to connect to
    
    ----    
    Object graph memory options (needs objgraph module > 3.3):
    ----    
    
    objgraph_growth [dict in json format] : new objects since last call
        dict parameters: nresults, dont_startwith, must_startwith, cont_contain, must_contain
        example: objgraph_growth '{"must_contain": ["fuglu"], "nresults": 5}'
        
    objgraph_common_objects [dict in json format] : most common objects
        dict parameters: nresults, dont_startwith, must_startwith, cont_contain, must_contain
        example: objgraph_common_objects '{"must_contain": ["fuglu"], "nresults": 5}'
        
    objgraph_count_types [dict in json format] : count objects in memory given by type
        dict parameters: typelist
        example: objgraph_count_types '{"typelist":["Worker","Suspect","SessionHandler"]}'
        
    objgraph_leaking_objects [dict in json format] : C-level leaking objects
        dict parameters: nresults, dont_startwith, must_startwith, cont_contain, must_contain
        example: objgraph_leaking_objects '{"nresults": 5}'
        
    objgraph_creategraph_backrefchain '{"typelist": ["SMTPServer"]}'
        dict parameters: max_depth, filename, selector, typelist
            * max_depth": length of the chain
            * filename": output filename (not defined will use objectname)
            * selector": Which object to pick from available objects "random"/"first"/"last/all"
            * typelist": list containing object type(s) (CASE SENSITIVE)
        example: objgraph_creategraph_backrefchain '{"typelist": ["SMTPServer"]}'
        
    objgraph_creategraph_backrefs '{"typelist": ["SuspectFilter"]}'
        dict parameters: max_depth, filename, selector, maxobjects, typelist, lowercase,
                         dont_startwith, dont_contain, must_startwith, must_contain
            * max_depth": length of the chain
            * filename": output filename (not defined will use objectname)
            * selector": Which object to pick from available objects "random"/"first"/"last/all"
            * typelist": list containing 1 object type (CASE SENSITIVE)
        example: fuglu_control objgraph_creategraph_backrefs '{"typelist": ["ControlSession"],"dont_startwith":[],"max_depth":5}'
        
    ----    
    common dict parameters for objgraph:
    ----    
    - dict must be surrounded by single quotes 
    - lists contain strings (quoted with "")
    - arguments:
        * nresults : how many results to display
        * lowercase : use lowercase comparison for filtering
        * dont_startwith : don't show objects starting with one of the strings in list (object name starts with package)
        * must_startwith : show objects starting with one of the strings in list (object name starts with package) if not blocked by a dont_*
        * dont_contain : don't show objects containing one of the strings in list
        * must_contain : show objects starting with one of the strings in list if not blocked by a dont_*
        * typelist : a typelist (case sensitive)
    
    """

optionparser = OptionParser(usage=usage())
optionparser.add_option("-p", dest="port", action="store",
                        default="/tmp/fuglu_control.sock", help="Unix socket or port to connect to")
(options, pargs) = optionparser.parse_args()


if len(pargs) < 1:
    optionparser.print_usage()
    sys.exit(1)


localport = options.port
iport = None
try:
    iport = int(localport)
except (TypeError, ValueError):
    pass

cmd = " ".join(pargs)
# connect to debug port
if iport != None:
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
else:
    s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)

try:
    if iport != None:
        s.connect(('127.0.0.1', iport))
    else:
        s.connect(localport)
except Exception as e:
    print("Cannot connect to fuglu control deamon (%s) - is fuglu running?" % localport)
    print(str(e))
    sys.exit(1)

s.sendall(force_bString(cmd))
data = s.recv(32768)
s.close()
print(force_uString(data))
