root/branches/gajim_0.5/plugins/logger/logger.py

Revision 359, 5.0 kB (checked in by asterix, 4 years ago)

update my email adress
update copyright
add missing headers

  • Property svn:keywords set to LastChangedDate LastChangedRevision LastChangedBy HeadURL Id
Line 
1##      plugins/logger.py
2##
3## Gajim Team:
4##      - Yann Le Boulanger <asterix@lagaule.org>
5##      - Vincent Hanquez <tab@snarc.org>
6##
7##      Copyright (C) 2003-2005 Gajim Team
8##
9## This program is free software; you can redistribute it and/or modify
10## it under the terms of the GNU General Public License as published
11## by the Free Software Foundation; version 2 only.
12##
13## This program is distributed in the hope that it will be useful,
14## but WITHOUT ANY WARRANTY; without even the implied warranty of
15## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16## GNU General Public License for more details.
17##
18
19def usage():
20        #TODO: use i18n
21        print "usage :", sys.argv[0], ' [OPTION]'
22        print "  -p\tport on whitch the sock plugin listen"
23        print "  -h, --help\tdisplay this help and exit"
24
25if __name__ == "__main__":
26        import getopt, sys, pickle, socket
27        try:
28                opts, args = getopt.getopt(sys.argv[1:], "p:h", ["help"])
29        except getopt.GetoptError:
30                # print help information and exit:
31                usage()
32                sys.exit(2)
33        port = 8255
34        for o, a in opts:
35                if o == '-p':
36                        port = a
37                if o in ("-h", "--help"):
38                        usage()
39                        sys.exit()
40        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
41        try:
42                sock.connect(('', 8255))
43        except:
44                #TODO: use i18n
45                print "unable to connect to localhost on port "+str(port)
46        else:
47                evp = pickle.dumps(('EXEC_PLUGIN', '', 'logger'))
48                sock.send('<'+evp+'>')
49                sock.close()
50        sys.exit()
51
52import os
53import string
54import time
55import common.optparser
56from common import i18n
57LOGPATH = os.path.expanduser("~/.gajim/logs/")
58_ = i18n._
59
60class plugin:
61        def read_queue(self):
62                while 1:
63                        while self.queueIN.empty() == 0:
64                                if self.config.has_key('lognotsep'):
65                                        lognotsep = self.config['lognotsep']
66                                else:
67                                        lognotsep = 1
68                                if self.config.has_key('lognotusr'):
69                                        lognotusr = self.config['lognotusr']
70                                else:
71                                        lognotusr = 1
72                                tim = time.time()
73                               
74                                ev = self.queueIN.get()
75                                if ev[0] == 'QUIT':
76                                        print _("plugin logger stopped")
77                                        return
78                                elif ev[0] == 'NOTIFY':
79                                        jid = string.split(ev[2][0], '/')[0]
80                                        if jid in self.no_log_for[ev[1]]:
81                                                break
82                                        status = ev[2][2]
83                                        if not status:
84                                                status = ""
85                                        status = string.replace(status, '\n', '\\n')
86                                        if lognotsep == 1:
87                                                fic = open(LOGPATH + "notify.log", "a")
88                                                fic.write("%s:%s:%s:%s\n" % (tim, ev[2][0] + '/' + ev[2][3], \
89                                                        ev[2][1], status))
90                                                fic.close()
91                                        if lognotusr == 1:
92                                                fic = open(LOGPATH + jid, "a")
93                                                fic.write("%s:%s:%s:%s\n" % (tim, ev[2][0] + '/' + ev[2][3], \
94                                                        ev[2][1], status))
95                                                fic.close()
96                                elif ev[0] == 'MSG':
97                                        jid = string.split(ev[2][0], '/')[0]
98                                        if jid in self.no_log_for[ev[1]]:
99                                                break
100                                        msg = string.replace(ev[2][1], '\n', '\\n')
101                                        fic = open(LOGPATH + jid, "a")
102                                        t = time.mktime(ev[2][2])
103                                        fic.write("%s:recv:%s\n" % (t, msg))
104                                        fic.close()
105                                elif ev[0] == 'MSGSENT':
106                                        jid = string.split(ev[2][0], '/')[0]
107                                        if jid in self.no_log_for[ev[1]]:
108                                                break
109                                        msg = string.replace(ev[2][1], '\n', '\\n')
110                                        fic = open(LOGPATH + jid, "a")
111                                        fic.write("%s:sent:%s\n" % (tim, msg))
112                                        fic.close()
113                                elif ev[0] == 'GC_MSG':
114                                        msg = string.replace(ev[2][1], '\n', '\\n')
115                                        jids = string.split(ev[2][0], '/')
116                                        jid = jids[0]
117                                        nick = ''
118                                        if len(jids) > 1:
119                                                nick = string.split(ev[2][0], '/')[1]
120                                        fic = open(LOGPATH + jid, "a")
121                                        t = time.mktime(ev[2][2])
122                                        fic.write("%s:recv:%s:%s\n" % (t, nick, msg))
123                                        fic.close()
124                                elif ev[0] == 'CONFIG':
125                                        if ev[2][0] == 'Logger':
126                                                self.config = ev[2][1]
127                                        if ev[2][0] == 'accounts':
128                                                accounts = ev[2][1]
129                                                self.no_log_for = {}
130                                                for acct in accounts.keys():
131                                                        self.no_log_for[acct] = []
132                                                        if accounts[acct].has_key('no_log_for'):
133                                                                self.no_log_for[acct] = \
134                                                                        string.split(accounts[acct]['no_log_for'], ' ')
135                        time.sleep(0.1)
136
137        def wait(self, what):
138                """Wait for a message from Core"""
139                #TODO: timeout, save messages that don't fit
140                while 1:
141                        if not self.queueIN.empty():
142                                ev = self.queueIN.get()
143                                if ev[0] == what and ev[2][0] == 'Logger':
144                                        return ev[2][1]
145                        time.sleep(0.1)
146
147        def __init__(self, quIN, quOUT):
148                self.queueIN = quIN
149                self.queueOUT = quOUT
150                quOUT.put(('REG_MESSAGE', 'logger', ['CONFIG', 'NOTIFY', 'MSG', \
151                        'MSGSENT', 'GC_MSG', 'QUIT']))
152                quOUT.put(('ASK_CONFIG', None, ('Logger', 'Logger', {\
153                        'lognotsep':1, 'lognotusr':1})))
154                self.config = self.wait('CONFIG')
155                quOUT.put(('ASK_CONFIG', None, ('Logger', 'accounts')))
156                accounts = self.wait('CONFIG')
157                self.no_log_for = {}
158                for acct in accounts.keys():
159                        self.no_log_for[acct] = []
160                        if accounts[acct].has_key('no_log_for'):
161                                self.no_log_for[acct] = \
162                                        string.split(accounts[acct]['no_log_for'], ' ')
163                #create ~/.gajim/logs/ if it doesn't exist
164                try:
165                        os.stat(os.path.expanduser("~/.gajim"))
166                except OSError:
167                        os.mkdir(os.path.expanduser("~/.gajim"))
168                        print _("creating ~/.gajim/")
169                try:
170                        os.stat(LOGPATH)
171                except OSError:
172                        os.mkdir(LOGPATH)
173                        print _("creating ~/.gajim/logs/")
174                self.read_queue()
175               
176print _("plugin logger loaded")
Note: See TracBrowser for help on using the browser.